Skip to content
Permalink
Browse files
Fix offset background when drawing rotated text using QgsTextRenderer
  • Loading branch information
nyalldawson committed Nov 1, 2021
1 parent a495b51 commit 6603cb6faec19ff6d5309514ffc450ac45d254bc
@@ -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
@@ -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;
}

@@ -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'))
Binary file not shown.
Binary file not shown.

0 comments on commit 6603cb6

Please sign in to comment.