Skip to content

Commit

Permalink
Merge pull request #45427 from qgis/backport-45425-to-queued_ltr_back…
Browse files Browse the repository at this point in the history
…ports

[Backport queued_ltr_backports] Fix offset background when drawing rotated text using QgsTextRenderer
  • Loading branch information
rouault committed Oct 10, 2021
2 parents dce904e + 5a24e63 commit 03023f7
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/textrenderer/qgstextrenderer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ void QgsTextRenderer::drawBackground( QgsRenderContext &context, QgsTextRenderer
// shared calculations between shapes and SVG

// configure angles, set component rotation and rotationOffset
const double originAdjustRotationRadians = -component.rotation;
if ( background.rotationType() != QgsTextBackgroundSettings::RotationFixed )
{
component.rotation = -( component.rotation * 180 / M_PI ); // RotationSync
Expand Down Expand Up @@ -791,6 +792,15 @@ void QgsTextRenderer::drawBackground( QgsRenderContext &context, QgsTextRenderer
component.origin.y() - height / 2.0 + originAdjust );
break;
}

// apply rotation to center point
if ( !qgsDoubleNear( originAdjustRotationRadians, 0 ) )
{
const double dx = component.center.x() - component.origin.x();
const double dy = component.center.y() - component.origin.y();
component.center.setX( component.origin.x() + ( std::cos( originAdjustRotationRadians ) * dx - std::sin( originAdjustRotationRadians ) * dy ) );
component.center.setY( component.origin.y() + ( std::sin( originAdjustRotationRadians ) * dx + std::cos( originAdjustRotationRadians ) * dy ) );
}
break;
}

Expand Down
22 changes: 22 additions & 0 deletions tests/src/python/test_qgstextrenderer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1467,6 +1467,28 @@ def testDrawBackgroundRectangleFixedSizeMapUnits(self):
format.background().setSizeUnit(QgsUnitTypes.RenderMapUnits)
assert self.checkRender(format, 'background_rect_mapunits', QgsTextRenderer.Background)

def testDrawBackgroundRectangleFixedSizeWithRotatedText(self):
format = QgsTextFormat()
format.setFont(getTestFont('bold'))
format.setSize(40)
format.background().setEnabled(True)
format.background().setType(QgsTextBackgroundSettings.ShapeRectangle)
format.background().setSize(QSizeF(20, 20))
format.background().setSizeType(QgsTextBackgroundSettings.SizeFixed)
format.background().setSizeUnit(QgsUnitTypes.RenderMillimeters)
assert self.checkRenderPoint(format, 'background_rect_fixed_rotated_text', angle=3.141 / 4)

def testDrawBackgroundRectangleBufferSizeWithRotatedText(self):
format = QgsTextFormat()
format.setFont(getTestFont('bold'))
format.setSize(40)
format.background().setEnabled(True)
format.background().setType(QgsTextBackgroundSettings.ShapeRectangle)
format.background().setSize(QSizeF(2, 3))
format.background().setSizeType(QgsTextBackgroundSettings.SizeBuffer)
format.background().setSizeUnit(QgsUnitTypes.RenderMillimeters)
assert self.checkRenderPoint(format, 'background_rect_buffer_rotated_text', angle=3.141 / 4)

def testDrawBackgroundRectangleMultilineFixedSizeMapUnits(self):
format = QgsTextFormat()
format.setFont(getTestFont('bold'))
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 03023f7

Please sign in to comment.