Skip to content
Permalink
Browse files

Fix failing test

  • Loading branch information
nyalldawson committed May 11, 2018
1 parent 9d2c4fc commit 9ce19af552765cd003821257adb742a8f9a79ea7
@@ -1629,7 +1629,7 @@ 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 );
static QFontMetricsF fontMetrics( 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
@@ -1873,9 +1873,9 @@ void QgsTextRenderer::drawPart( QPointF origin, double rotation, QgsTextRenderer
}
}

QFontMetricsF QgsTextRenderer::fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format )
QFontMetricsF QgsTextRenderer::fontMetrics( QgsRenderContext &context, const QgsTextFormat &format )
{
return QFontMetricsF( format.scaledFont( context ) );
return QFontMetricsF( format.scaledFont( context ), context.painter() ? context.painter()->device() : nullptr );
}

void QgsTextRenderer::drawBuffer( QgsRenderContext &context, const QgsTextRenderer::Component &component, const QgsTextFormat &format )
@@ -1408,7 +1408,7 @@ class CORE_EXPORT QgsTextRenderer
* all scaling required by the render context.
* \since QGIS 3.2
*/
static QFontMetricsF fontMetrics( const QgsRenderContext &context, const QgsTextFormat &format );
static QFontMetricsF fontMetrics( QgsRenderContext &context, const QgsTextFormat &format );

/**
* Returns the width of a text based on a given format.
@@ -448,18 +448,17 @@ def testFontMetrics(self):

string = 'xxxxxxxxxxxxxxxxxxxxxx'

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

context = QgsRenderContext()
image = QImage(400, 400, QImage.Format_RGB32)
painter = QPainter(image)
context = QgsRenderContext.fromQPainter(painter)
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)
metrics2 = QgsTextRenderer.fontMetrics(context, s)
painter.end()

self.assertAlmostEqual(metrics.width(string), 51.9, 1)
self.assertAlmostEqual(metrics2.width(string), 104.15, 1)

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

0 comments on commit 9ce19af

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