Skip to content

Commit a06000e

Browse files
committedMay 11, 2018
Add method to QgsTextRenderer to retreive scaled QFontMetricsF
from a text format in a specified render context
1 parent e53adc1 commit a06000e

File tree

4 files changed

+48
-1
lines changed

4 files changed

+48
-1
lines changed
 

‎python/core/qgstextrenderer.sip.in

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1627,6 +1627,15 @@ with the text or background parts)
16271627
:param drawAsOutlines: set to false to render text as text. This allows outputs to
16281628
formats like SVG to maintain text as text objects, but at the cost of degraded
16291629
rendering and may result in side effects like misaligned text buffers.
1630+
%End
1631+
1632+
static QFontMetricsF fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format );
1633+
%Docstring
1634+
Returns the font metrics for the given text ``format``, when rendered
1635+
in the specified render ``context``. The font metrics will take into account
1636+
all scaling required by the render context.
1637+
1638+
.. versionadded:: 3.2
16301639
%End
16311640

16321641
static double textWidth( const QgsRenderContext &context, const QgsTextFormat &format, const QStringList &textLines,

‎src/core/qgstextrenderer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1873,6 +1873,11 @@ void QgsTextRenderer::drawPart( QPointF origin, double rotation, QgsTextRenderer
18731873
}
18741874
}
18751875

1876+
QFontMetricsF QgsTextRenderer::fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format )
1877+
{
1878+
return QFontMetricsF( format.scaledFont( context ) );
1879+
}
1880+
18761881
void QgsTextRenderer::drawBuffer( QgsRenderContext &context, const QgsTextRenderer::Component &component, const QgsTextFormat &format )
18771882
{
18781883
QPainter *p = context.painter();

‎src/core/qgstextrenderer.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,6 +1402,14 @@ class CORE_EXPORT QgsTextRenderer
14021402
QgsRenderContext &context, const QgsTextFormat &format,
14031403
TextPart part, bool drawAsOutlines = true );
14041404

1405+
/**
1406+
* Returns the font metrics for the given text \a format, when rendered
1407+
* in the specified render \a context. The font metrics will take into account
1408+
* all scaling required by the render context.
1409+
* \since QGIS 3.2
1410+
*/
1411+
static QFontMetricsF fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format );
1412+
14051413
/**
14061414
* Returns the width of a text based on a given format.
14071415
* \param context render context

‎tests/src/python/test_qgstextrenderer.py

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
QgsRectangle,
3030
QgsRenderChecker,
3131
QgsBlurEffect)
32-
from qgis.PyQt.QtGui import (QColor, QPainter, QFont, QImage, QBrush, QPen)
32+
from qgis.PyQt.QtGui import (QColor, QPainter, QFont, QImage, QBrush, QPen, QFontMetricsF)
3333
from qgis.PyQt.QtCore import (Qt, QSizeF, QPointF, QRectF, QDir)
3434
from qgis.PyQt.QtXml import QDomDocument
3535
from qgis.testing import unittest, start_app
@@ -436,6 +436,31 @@ def testToQFont(self):
436436
qfont = s.toQFont()
437437
self.assertAlmostEqual(qfont.pointSizeF(), 360.0, 2)
438438

439+
def testFontMetrics(self):
440+
"""
441+
Test calculating font metrics from scaled text formats
442+
"""
443+
s = QgsTextFormat()
444+
f = getTestFont()
445+
s.setFont(f)
446+
s.setSize(12)
447+
s.setSizeUnit(QgsUnitTypes.RenderPoints)
448+
449+
string = 'xxxxxxxxxxxxxxxxxxxxxx'
450+
451+
# calculated expected width
452+
f = s.toQFont()
453+
expected = QFontMetricsF(f).width(string)
454+
scale = expected / 416.625
455+
456+
context = QgsRenderContext()
457+
context.setScaleFactor(1)
458+
metrics = QgsTextRenderer.fontMetrics(context, s)
459+
self.assertAlmostEqual(metrics.width(string), 51.9 * scale, -1)
460+
context.setScaleFactor(2)
461+
metrics = QgsTextRenderer.fontMetrics(context, s)
462+
self.assertAlmostEqual(metrics.width(string), 104.15 * scale, -1)
463+
439464
def imageCheck(self, name, reference_image, image):
440465
self.report += "<h2>Render {}</h2>\n".format(name)
441466
temp_dir = QDir.tempPath() + '/'

0 commit comments

Comments
 (0)
Please sign in to comment.