Skip to content

Commit

Permalink
Avoid unnecessary font scaling by precalculating if format has a null
Browse files Browse the repository at this point in the history
font size
  • Loading branch information
nyalldawson committed Nov 10, 2022
1 parent e478a52 commit ed84780
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 23 deletions.
Expand Up @@ -37,6 +37,13 @@ The optional ``scaleFactor`` parameter can specify a font size scaling factor. I
:py:class:`QgsTextRenderer`.FONT_WORKAROUND_SCALE and then manually calculations
based on the resultant font metrics. Failure to do so will result in poor quality text rendering
at small font sizes.
%End

bool isNullFontSize() const;
%Docstring
Returns ``True`` if the metrics could not be calculated because the text format has a null font size.

.. versionadded:: 3.30
%End

QSizeF documentSize( Qgis::TextLayoutMode mode, Qgis::TextOrientation orientation ) const;
Expand Down
5 changes: 2 additions & 3 deletions src/core/textrenderer/qgstextdocumentmetrics.cpp
Expand Up @@ -27,9 +27,8 @@ QgsTextDocumentMetrics QgsTextDocumentMetrics::calculateMetrics( const QgsTextDo
{
QgsTextDocumentMetrics res;

bool isNullSize = false;
const QFont font = format.scaledFont( context, scaleFactor, &isNullSize );
if ( isNullSize )
const QFont font = format.scaledFont( context, scaleFactor, &res.mIsNullSize );
if ( res.isNullFontSize() )
return res;

// for absolute line heights
Expand Down
9 changes: 9 additions & 0 deletions src/core/textrenderer/qgstextdocumentmetrics.h
Expand Up @@ -52,6 +52,13 @@ class CORE_EXPORT QgsTextDocumentMetrics
*/
static QgsTextDocumentMetrics calculateMetrics( const QgsTextDocument &document, const QgsTextFormat &format, const QgsRenderContext &context, double scaleFactor = 1.0 );

/**
* Returns TRUE if the metrics could not be calculated because the text format has a null font size.
*
* \since QGIS 3.30
*/
bool isNullFontSize() const { return mIsNullSize; }

/**
* Returns the overall size of the document.
*/
Expand Down Expand Up @@ -106,6 +113,8 @@ class CORE_EXPORT QgsTextDocumentMetrics

private:

bool mIsNullSize = false;

QSizeF mDocumentSizeLabelMode;
QSizeF mDocumentSizePointRectMode;
QSizeF mDocumentSizeVerticalOrientation;
Expand Down
31 changes: 11 additions & 20 deletions src/core/textrenderer/qgstextrenderer.cpp
Expand Up @@ -342,9 +342,7 @@ double QgsTextRenderer::drawBuffer( QgsRenderContext &context, const QgsTextRend
referenceScaleOverride.emplace( QgsScopedRenderContextReferenceScaleOverride( context, -1.0 ) );
}

bool isNullSize = false;
const QFont font = format.scaledFont( context, scaleFactor, &isNullSize );
if ( isNullSize )
if ( metrics.isNullFontSize() )
return 0;

referenceScaleOverride.reset();
Expand Down Expand Up @@ -516,9 +514,7 @@ void QgsTextRenderer::drawMask( QgsRenderContext &context, const QgsTextRenderer
referenceScaleOverride.emplace( QgsScopedRenderContextReferenceScaleOverride( context, -1.0 ) );
}

bool isNullSize = false;
const QFont font = format.scaledFont( context, scaleFactor, &isNullSize );
if ( isNullSize )
if ( metrics.isNullFontSize() )
return;

referenceScaleOverride.reset();
Expand Down Expand Up @@ -634,6 +630,7 @@ double QgsTextRenderer::textHeight( const QgsRenderContext &context, const QgsTe
double QgsTextRenderer::textHeight( const QgsRenderContext &context, const QgsTextFormat &format, QChar character, bool includeEffects )
{
const double scaleFactor = calculateScaleFactorForFormat( context, format );

bool isNullSize = false;
const QFont baseFont = format.scaledFont( context, scaleFactor, &isNullSize );
if ( isNullSize )
Expand Down Expand Up @@ -759,12 +756,10 @@ double QgsTextRenderer::textHeight( const QgsRenderContext &context, const QgsTe
//calculate max height of text lines
const double scaleFactor = calculateScaleFactorForFormat( context, format );

bool isNullSize = false;
format.scaledFont( context, scaleFactor, &isNullSize );
if ( isNullSize )
const QgsTextDocumentMetrics metrics = QgsTextDocumentMetrics::calculateMetrics( document, format, context, scaleFactor );
if ( metrics.isNullFontSize() )
return 0;

const QgsTextDocumentMetrics metrics = QgsTextDocumentMetrics::calculateMetrics( document, format, context, scaleFactor );
return metrics.documentSize( mode, format.orientation() ).height();
}

Expand Down Expand Up @@ -1342,9 +1337,7 @@ void QgsTextRenderer::drawTextInternal( Qgis::TextComponent drawType,
referenceScaleOverride.emplace( QgsScopedRenderContextReferenceScaleOverride( context, -1.0 ) );
}

bool isNullSize = false;
format.scaledFont( context, fontScale, &isNullSize );
if ( isNullSize )
if ( metrics.isNullFontSize() )
return;

referenceScaleOverride.reset();
Expand Down Expand Up @@ -1608,11 +1601,10 @@ void QgsTextRenderer::drawTextInternalHorizontal( QgsRenderContext &context, con
// to temporarily remove the reference scale here or we'll be applying the scaling twice
referenceScaleOverride.emplace( QgsScopedRenderContextReferenceScaleOverride( context, -1.0 ) );
}
bool isNullSize = false;
const QFont font = format.scaledFont( context, fontScale, &isNullSize );

referenceScaleOverride.reset();

if ( !isNullSize )
if ( !metrics.isNullFontSize() )
{
textp.scale( 1 / fontScale, 1 / fontScale );

Expand Down Expand Up @@ -1692,7 +1684,8 @@ void QgsTextRenderer::drawTextInternalHorizontal( QgsRenderContext &context, con
context.painter()->drawText( xOffset, 0, fragment.text() );
context.painter()->scale( fontScale, fontScale );

xOffset += fragment.horizontalAdvance( fragmentFont, context, true, fontScale );
xOffset += metrics.fragmentHorizontalAdvance( blockIndex, fragmentIndex, mode );
fragmentIndex++;
}
}
}
Expand All @@ -1717,9 +1710,7 @@ void QgsTextRenderer::drawTextInternalVertical( QgsRenderContext &context, const
referenceScaleOverride.emplace( QgsScopedRenderContextReferenceScaleOverride( context, -1.0 ) );
}

bool isNullSize = false;
const QFont font = format.scaledFont( context, fontScale, &isNullSize );
if ( isNullSize )
if ( metrics.isNullFontSize() )
return;

referenceScaleOverride.reset();
Expand Down

0 comments on commit ed84780

Please sign in to comment.