Skip to content
Permalink
Browse files

Add method to QgsTextRenderer to retreive scaled QFontMetricsF

from a text format in a specified render context
  • Loading branch information
nyalldawson committed May 8, 2018
1 parent e53adc1 commit a06000e0e74ecc116f280e432e1623b3b3772ad0
@@ -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,
@@ -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();
@@ -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
@@ -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
@@ -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() + '/'

0 comments on commit a06000e

Please sign in to comment.
You can’t perform that action at this time.