Skip to content

Commit 3877db4

Browse files
committedNov 29, 2022
Fix missing shadows in legend text
1 parent f1447be commit 3877db4

File tree

3 files changed

+57
-6
lines changed

3 files changed

+57
-6
lines changed
 

‎src/core/textrenderer/qgstextrenderer.cpp

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ void QgsTextRenderer::drawText( const QRectF &rect, double rotation, Qgis::TextH
8484
QgsTextFormat tmpFormat = format;
8585
if ( format.dataDefinedProperties().hasActiveProperties() ) // note, we use format instead of tmpFormat here, it's const and potentially avoids a detach
8686
tmpFormat.updateDataDefinedProperties( context );
87-
tmpFormat = updateShadowPosition( tmpFormat );
8887

8988
QStringList textLines;
9089
for ( const QString &line : text )
@@ -110,17 +109,19 @@ void QgsTextRenderer::drawText( const QRectF &rect, double rotation, Qgis::TextH
110109

111110
void QgsTextRenderer::drawDocument( const QRectF &rect, const QgsTextFormat &format, const QgsTextDocument &document, const QgsTextDocumentMetrics &metrics, QgsRenderContext &context, Qgis::TextHorizontalAlignment horizontalAlignment, Qgis::TextVerticalAlignment verticalAlignment, double rotation, Qgis::TextLayoutMode mode, Qgis::TextRendererFlags )
112111
{
113-
if ( format.background().enabled() )
112+
const QgsTextFormat tmpFormat = updateShadowPosition( format );
113+
114+
if ( tmpFormat.background().enabled() )
114115
{
115-
drawPart( rect, rotation, horizontalAlignment, verticalAlignment, document, metrics, context, format, Qgis::TextComponent::Background, mode );
116+
drawPart( rect, rotation, horizontalAlignment, verticalAlignment, document, metrics, context, tmpFormat, Qgis::TextComponent::Background, mode );
116117
}
117118

118-
if ( format.buffer().enabled() )
119+
if ( tmpFormat.buffer().enabled() )
119120
{
120-
drawPart( rect, rotation, horizontalAlignment, verticalAlignment, document, metrics, context, format, Qgis::TextComponent::Buffer, mode );
121+
drawPart( rect, rotation, horizontalAlignment, verticalAlignment, document, metrics, context, tmpFormat, Qgis::TextComponent::Buffer, mode );
121122
}
122123

123-
drawPart( rect, rotation, horizontalAlignment, verticalAlignment, document, metrics, context, format, Qgis::TextComponent::Text, mode );
124+
drawPart( rect, rotation, horizontalAlignment, verticalAlignment, document, metrics, context, tmpFormat, Qgis::TextComponent::Text, mode );
124125
}
125126

126127
void QgsTextRenderer::drawText( QPointF point, double rotation, Qgis::TextHorizontalAlignment alignment, const QStringList &textLines, QgsRenderContext &context, const QgsTextFormat &format, bool )

‎tests/src/python/test_qgstextrenderer.py

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,56 @@ def testDrawDocumentRectAscentMode(self):
17711771

17721772
self.assertTrue(self.imageCheck('draw_document_rect_ascent', 'draw_document_rect_ascent', image))
17731773

1774+
def testDrawDocumentShadowPlacement(self):
1775+
"""
1776+
Test drawing text document with shadow placement lowest
1777+
"""
1778+
format = QgsTextFormat()
1779+
format.setFont(getTestFont('bold'))
1780+
format.setAllowHtmlFormatting(True)
1781+
format.setSize(30)
1782+
format.setColor(QColor(255, 255, 255))
1783+
1784+
format.shadow().setEnabled(True)
1785+
format.shadow().setShadowPlacement(QgsTextShadowSettings.ShadowLowest)
1786+
format.shadow().setOpacity(1.0)
1787+
format.shadow().setBlurRadius(0)
1788+
format.shadow().setOffsetDistance(5)
1789+
format.shadow().setOffsetUnit(QgsUnitTypes.RenderMillimeters)
1790+
1791+
image = QImage(400, 400, QImage.Format_RGB32)
1792+
1793+
painter = QPainter()
1794+
ms = QgsMapSettings()
1795+
ms.setExtent(QgsRectangle(0, 0, 50, 50))
1796+
ms.setOutputSize(image.size())
1797+
context = QgsRenderContext.fromMapSettings(ms)
1798+
context.setPainter(painter)
1799+
context.setScaleFactor(96 / 25.4) # 96 DPI
1800+
context.setFlag(QgsRenderContext.ApplyScalingWorkaroundForTextRendering, True)
1801+
1802+
painter.begin(image)
1803+
painter.setRenderHint(QPainter.Antialiasing)
1804+
image.fill(QColor(152, 219, 249))
1805+
1806+
painter.setBrush(QBrush(QColor(182, 239, 255)))
1807+
painter.setPen(Qt.NoPen)
1808+
1809+
doc = QgsTextDocument.fromHtml(['first <span style="font-size:50pt">line</span>', 'second <span style="font-size:50pt">line</span>', 'third line'])
1810+
1811+
metrics = QgsTextDocumentMetrics.calculateMetrics(doc, format, context, QgsTextRenderer.FONT_WORKAROUND_SCALE)
1812+
1813+
QgsTextRenderer.drawDocument(QRectF(100, 100, 100, 100),
1814+
format,
1815+
doc,
1816+
metrics,
1817+
context,
1818+
mode=Qgis.TextLayoutMode.RectangleAscentBased)
1819+
1820+
painter.end()
1821+
1822+
self.assertTrue(self.imageCheck('draw_document_shadow_lowest', 'draw_document_shadow_lowest', image))
1823+
17741824
def testDrawForcedItalic(self):
17751825
"""
17761826
Test drawing with forced italic

0 commit comments

Comments
 (0)
Please sign in to comment.