Skip to content
Permalink
Browse files

Fix scalebar text size calculation when HTML formatting is used

  • Loading branch information
nyalldawson committed May 12, 2020
1 parent 5092939 commit 7dfc5107716cd5f6623a08f4ec313f84b1019b98
@@ -163,7 +163,7 @@ Returns the width of a text based on a given format.
:param fontMetrics: font metrics
%End

static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode,
static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode = Point,
QFontMetricsF *fontMetrics = 0 );
%Docstring
Returns the height of a text based on a given format.
@@ -84,13 +84,13 @@ void QgsNumericScaleBarRenderer::draw( QgsRenderContext &context, const QgsScale
painter->restore();
}

QSizeF QgsNumericScaleBarRenderer::calculateBoxSize( QgsRenderContext &, const QgsScaleBarSettings &settings,
QSizeF QgsNumericScaleBarRenderer::calculateBoxSize( QgsRenderContext &context, const QgsScaleBarSettings &settings,
const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
{
QFont font = settings.textFormat().toQFont();
const double painterToMm = 1.0 / context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );

double textWidth = QgsLayoutUtils::textWidthMM( font, scaleText( scaleContext.scale, settings ) );
double textHeight = QgsLayoutUtils::fontAscentMM( font );
double textWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << scaleText( scaleContext.scale, settings ) ) * painterToMm;
double textHeight = QgsTextRenderer::textHeight( context, settings.textFormat(), QStringList() << scaleText( scaleContext.scale, settings ) ) * painterToMm;

return QSizeF( 2 * settings.boxContentSpace() + textWidth,
textHeight + 2 * settings.boxContentSpace() );
@@ -222,10 +222,10 @@ QSizeF QgsScaleBarRenderer::calculateBoxSize( const QgsScaleBarSettings &setting

QSizeF QgsScaleBarRenderer::calculateBoxSize( QgsRenderContext &context, const QgsScaleBarSettings &settings, const QgsScaleBarRenderer::ScaleBarContext &scaleContext ) const
{
QFont font = settings.textFormat().toQFont();

const double painterToMm = 1.0 / context.convertToPainterUnits( 1, QgsUnitTypes::RenderMillimeters );
//consider centered first label
double firstLabelWidth = QgsLayoutUtils::textWidthMM( font, firstLabelString( settings ) );
double firstLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << firstLabelString( settings ) ) * painterToMm;

if ( settings.labelHorizontalPlacement() == QgsScaleBarSettings::LabelCenteredSegment )
{
if ( firstLabelWidth > scaleContext.segmentWidth )
@@ -249,7 +249,8 @@ QSizeF QgsScaleBarRenderer::calculateBoxSize( QgsRenderContext &context, const Q
double largestLabelWidth;
if ( settings.labelHorizontalPlacement() == QgsScaleBarSettings::LabelCenteredSegment )
{
largestLabelWidth = QgsLayoutUtils::textWidthMM( font, largestLabel );
largestLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << largestLabel ) * painterToMm;

if ( largestLabelWidth > scaleContext.segmentWidth )
{
largestLabelWidth = ( largestLabelWidth - scaleContext.segmentWidth ) / 2;
@@ -261,7 +262,8 @@ QSizeF QgsScaleBarRenderer::calculateBoxSize( QgsRenderContext &context, const Q
}
else
{
largestLabelWidth = QgsLayoutUtils::textWidthMM( font, largestLabel ) - QgsLayoutUtils::textWidthMM( font, largestNumberLabel ) / 2;
largestLabelWidth = QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << largestLabel ) * painterToMm
- QgsTextRenderer::textWidth( context, settings.textFormat(), QStringList() << largestNumberLabel ) * painterToMm / 2;
}

double totalBarLength = scaleContext.segmentWidth * ( settings.numberOfSegments() + ( settings.numberOfSegmentsLeft() > 0 ? 1 : 0 ) );
@@ -280,7 +282,9 @@ QSizeF QgsScaleBarRenderer::calculateBoxSize( QgsRenderContext &context, const Q
{
height = settings.height();
}
height += settings.labelBarSpace() + 2 * settings.boxContentSpace() + QgsLayoutUtils::fontAscentMM( font );

// TODO -- we technically should check the height of ALL labels here and take the maximum
height += settings.labelBarSpace() + 2 * settings.boxContentSpace() + QgsTextRenderer::textHeight( context, settings.textFormat(), QStringList() << largestLabel ) * painterToMm;

return QSizeF( width, height );
}
@@ -178,7 +178,7 @@ class CORE_EXPORT QgsTextRenderer
* \param mode draw mode
* \param fontMetrics font metrics
*/
static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode,
static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines, DrawMode mode = Point,
QFontMetricsF *fontMetrics = nullptr );

private:
@@ -216,7 +216,7 @@ class CORE_EXPORT QgsTextRenderer
};

static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document );
static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, DrawMode mode );
static double textHeight( const QgsRenderContext &context, const QgsTextFormat &format, const QgsTextDocument &document, DrawMode mode = Point );

/**
* Draws a single component of rendered text using the specified settings.

0 comments on commit 7dfc510

Please sign in to comment.
You can’t perform that action at this time.