Skip to content

Commit

Permalink
Ignore final line leading when calculating text height
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson committed Jul 10, 2020
1 parent 11297b8 commit 20669a5
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/core/textrenderer/qgstextrenderer.cpp
Expand Up @@ -612,10 +612,12 @@ double QgsTextRenderer::textHeight( const QgsRenderContext &context, const QgsTe
{
int blockIndex = 0;
double totalHeight = 0;
double lastLineLeading = 0;
for ( const QgsTextBlock &block : document )
{
double maxBlockHeight = 0;
double maxBlockLineSpacing = 0;
double maxBlockLeading = 0;
for ( const QgsTextFragment &fragment : block )
{
QFont fragmentFont = baseFont;
Expand All @@ -625,7 +627,11 @@ double QgsTextRenderer::textHeight( const QgsRenderContext &context, const QgsTe
const double fragmentHeight = fm.ascent() + fm.descent(); // ignore +1 for baseline

maxBlockHeight = std::max( maxBlockHeight, fragmentHeight );
maxBlockLineSpacing = std::max( maxBlockLineSpacing, fm.lineSpacing() );
if ( fm.lineSpacing() > maxBlockLineSpacing )
{
maxBlockLineSpacing = fm.lineSpacing();
maxBlockLeading = fm.leading();
}
}

switch ( mode )
Expand All @@ -641,13 +647,15 @@ double QgsTextRenderer::textHeight( const QgsRenderContext &context, const QgsTe
case Point:
// standard rendering - designed to exactly replicate QPainter's drawText method
totalHeight += blockIndex == 0 ? maxBlockHeight : maxBlockLineSpacing * format.lineHeight();
if ( blockIndex > 0 )
lastLineLeading = maxBlockLeading;
break;
}

blockIndex++;
}

return totalHeight / scaleFactor;
return ( totalHeight - lastLineLeading ) / scaleFactor;
}

case QgsTextFormat::VerticalOrientation:
Expand Down

0 comments on commit 20669a5

Please sign in to comment.