Skip to content

Commit

Permalink
Fix offset background when drawing rotated text using QgsTextRenderer
Browse files Browse the repository at this point in the history
  • Loading branch information
nyalldawson authored and github-actions[bot] committed Oct 6, 2021
1 parent 08b7b48 commit 5a24e63
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/core/textrenderer/qgstextrenderer.cpp
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
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
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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 5a24e63

Please sign in to comment.