Skip to content

Commit

Permalink
[layouts] Fix resizing multiline label items to adjust to the size
Browse files Browse the repository at this point in the history
of their text
  • Loading branch information
nyalldawson committed Oct 10, 2020
1 parent f940ffc commit 6829e63
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/core/layout/qgslayoututils.cpp
Expand Up @@ -220,13 +220,21 @@ double QgsLayoutUtils::textWidthMM( const QFont &font, const QString &text )
{
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html

const QStringList multiLineSplit = text.split( '\n' );
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );

double maxWidth = 0;
for ( const QString &line : multiLineSplit )
{
#if QT_VERSION < QT_VERSION_CHECK(5, 11, 0)
return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
maxWidth = std::max( maxWidth, ( fontMetrics.width( line ) / FONT_WORKAROUND_SCALE ) );
#else
return ( fontMetrics.horizontalAdvance( text ) / FONT_WORKAROUND_SCALE );
maxWidth = std::max( maxWidth, ( fontMetrics.horizontalAdvance( line ) / FONT_WORKAROUND_SCALE ) );
#endif
}
return maxWidth;
}

double QgsLayoutUtils::textHeightMM( const QFont &font, const QString &text, double multiLineHeight )
Expand Down

0 comments on commit 6829e63

Please sign in to comment.