Skip to content

Commit

Permalink
Add method to QgsTextRenderer to retreive scaled QFontMetricsF
Browse files Browse the repository at this point in the history
from a text format in a specified render context
  • Loading branch information
nyalldawson committed May 11, 2018
1 parent e53adc1 commit a06000e
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 1 deletion.
9 changes: 9 additions & 0 deletions python/core/qgstextrenderer.sip.in
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,15 @@ with the text or background parts)
:param drawAsOutlines: set to false to render text as text. This allows outputs to
formats like SVG to maintain text as text objects, but at the cost of degraded
rendering and may result in side effects like misaligned text buffers.
%End

static QFontMetricsF fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format );
%Docstring
Returns the font metrics for the given text ``format``, when rendered
in the specified render ``context``. The font metrics will take into account
all scaling required by the render context.

.. versionadded:: 3.2
%End

static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,
Expand Down
5 changes: 5 additions & 0 deletions src/core/qgstextrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1873,6 +1873,11 @@ void QgsTextRenderer::drawPart( QPointF origin, double rotation, QgsTextRenderer
}
}

QFontMetricsF QgsTextRenderer::fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format )
{
return QFontMetricsF( format.scaledFont( context ) );
}

void QgsTextRenderer::drawBuffer( QgsRenderContext &context, const QgsTextRenderer::Component &component, const QgsTextFormat &format )
{
QPainter *p = context.painter();
Expand Down
8 changes: 8 additions & 0 deletions src/core/qgstextrenderer.h
Original file line number Diff line number Diff line change
Expand Up @@ -1402,6 +1402,14 @@ class CORE_EXPORT QgsTextRenderer
QgsRenderContext &context, const QgsTextFormat &format,
TextPart part, bool drawAsOutlines = true );

/**
* Returns the font metrics for the given text \a format, when rendered
* in the specified render \a context. The font metrics will take into account
* all scaling required by the render context.
* \since QGIS 3.2
*/
static QFontMetricsF fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format );

/**
* Returns the width of a text based on a given format.
* \param context render context
Expand Down
27 changes: 26 additions & 1 deletion tests/src/python/test_qgstextrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
QgsRectangle,
QgsRenderChecker,
QgsBlurEffect)
from qgis.PyQt.QtGui import (QColor, QPainter, QFont, QImage, QBrush, QPen)
from qgis.PyQt.QtGui import (QColor, QPainter, QFont, QImage, QBrush, QPen, QFontMetricsF)
from qgis.PyQt.QtCore import (Qt, QSizeF, QPointF, QRectF, QDir)
from qgis.PyQt.QtXml import QDomDocument
from qgis.testing import unittest, start_app
Expand Down Expand Up @@ -436,6 +436,31 @@ def testToQFont(self):
qfont = s.toQFont()
self.assertAlmostEqual(qfont.pointSizeF(), 360.0, 2)

def testFontMetrics(self):
"""
Test calculating font metrics from scaled text formats
"""
s = QgsTextFormat()
f = getTestFont()
s.setFont(f)
s.setSize(12)
s.setSizeUnit(QgsUnitTypes.RenderPoints)

string = 'xxxxxxxxxxxxxxxxxxxxxx'

# calculated expected width
f = s.toQFont()
expected = QFontMetricsF(f).width(string)
scale = expected / 416.625

context = QgsRenderContext()
context.setScaleFactor(1)
metrics = QgsTextRenderer.fontMetrics(context, s)
self.assertAlmostEqual(metrics.width(string), 51.9 * scale, -1)
context.setScaleFactor(2)
metrics = QgsTextRenderer.fontMetrics(context, s)
self.assertAlmostEqual(metrics.width(string), 104.15 * scale, -1)

def imageCheck(self, name, reference_image, image):
self.report += "<h2>Render {}</h2>\n".format(name)
temp_dir = QDir.tempPath() + '/'
Expand Down

0 comments on commit a06000e

Please sign in to comment.