Skip to content

Commit 917cee0

Browse files
author
Sandro Santilli
committed
Consider map rotation in rendering of horizontal and parallel labels
See http://hub.qgis.org/issues/11814 NOTE: the map rotation should likely be considered at LabelPlacement configuration rather than at rendering time Raises pixel mismatch tolerance for background labels tests
1 parent bd4087b commit 917cee0

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

src/core/qgspallabeling.cpp

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1415,10 +1415,16 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
14151415
}
14161416
}
14171417
w /= rasterCompressFactor;
1418-
QgsPoint ptSize = xform->toMapCoordinatesF( w, h );
14191418

1419+
#if 0 // XXX strk
1420+
QgsPoint ptSize = xform->toMapCoordinatesF( w, h );
14201421
labelX = qAbs( ptSize.x() - ptZero.x() );
14211422
labelY = qAbs( ptSize.y() - ptZero.y() );
1423+
#else
1424+
double uPP = xform->mapUnitsPerPixel();
1425+
labelX = w * uPP;
1426+
labelY = h * uPP;
1427+
#endif
14221428
}
14231429

14241430
void QgsPalLayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& context, QString dxfLayer )
@@ -4115,12 +4121,37 @@ QgsPalLabeling::Search QgsPalLabeling::searchMethod() const
41154121
void QgsPalLabeling::drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform )
41164122
{
41174123
QgsPoint outPt = xform->transform( lp->getX(), lp->getY() );
4118-
QgsPoint outPt2 = xform->transform( lp->getX() + lp->getWidth(), lp->getY() + lp->getHeight() );
41194124

41204125
painter->save();
4121-
painter->translate( QPointF( outPt.x(), outPt.y() ) );
4126+
4127+
#if 1 // TODO: generalize some of this
4128+
double w = lp->getWidth();
4129+
double h = lp->getHeight();
4130+
double cx = lp->getX() + w/2.0;
4131+
double cy = lp->getY() + h/2.0;
4132+
double scale = 1.0/xform->mapUnitsPerPixel();
4133+
double rotation = xform->mapRotation();
4134+
double sw = w * scale;
4135+
double sh = h * scale;
4136+
QRectF rect( -sw/2, -sh/2, sw, sh );
4137+
4138+
painter->translate( xform->transform( QPointF(cx, cy) ).toQPointF() );
4139+
if ( rotation ) {
4140+
// Only if not horizontal
4141+
if ( lp->getFeaturePart()->getLayer()->getArrangement() != P_HORIZ ) {
4142+
painter->rotate( rotation );
4143+
}
4144+
}
4145+
painter->translate( rect.bottomLeft() );
41224146
painter->rotate( -lp->getAlpha() * 180 / M_PI );
4147+
painter->translate( -rect.bottomLeft() );
4148+
#else
4149+
QgsPoint outPt2 = xform->transform( lp->getX() + lp->getWidth(), lp->getY() + lp->getHeight() );
41234150
QRectF rect( 0, 0, outPt2.x() - outPt.x(), outPt2.y() - outPt.y() );
4151+
painter->translate( QPointF( outPt.x(), outPt.y() ) );
4152+
painter->rotate( -lp->getAlpha() * 180 / M_PI );
4153+
#endif
4154+
41244155
painter->drawRect( rect );
41254156
painter->restore();
41264157

@@ -4172,7 +4203,7 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
41724203
drawLabelBackground( context, component, tmpLyr );
41734204
}
41744205

4175-
if ( drawType == QgsPalLabeling::LabelBuffer
4206+
else if ( drawType == QgsPalLabeling::LabelBuffer
41764207
|| drawType == QgsPalLabeling::LabelText )
41774208
{
41784209

@@ -4255,8 +4286,30 @@ void QgsPalLabeling::drawLabel( pal::LabelPosition* label, QgsRenderContext& con
42554286
for ( int i = 0; i < lines; ++i )
42564287
{
42574288
painter->save();
4289+
#if 1 // TODO: generalize some of this
4290+
LabelPosition* lp = label;
4291+
double w = lp->getWidth();
4292+
double h = lp->getHeight();
4293+
double cx = lp->getX() + w/2.0;
4294+
double cy = lp->getY() + h/2.0;
4295+
double scale = 1.0/xform->mapUnitsPerPixel();
4296+
double rotation = xform->mapRotation();
4297+
double sw = w * scale;
4298+
double sh = h * scale;
4299+
QRectF rect( -sw/2, -sh/2, sw, sh );
4300+
painter->translate( xform->transform( QPointF(cx, cy) ).toQPointF() );
4301+
if ( rotation ) {
4302+
// Only if not horizontal
4303+
if ( lp->getFeaturePart()->getLayer()->getArrangement() != P_HORIZ ) {
4304+
painter->rotate( rotation );
4305+
}
4306+
}
4307+
painter->translate( rect.bottomLeft() );
4308+
painter->rotate( -lp->getAlpha() * 180 / M_PI );
4309+
#else
42584310
painter->translate( QPointF( outPt.x(), outPt.y() ) );
42594311
painter->rotate( -label->getAlpha() * 180 / M_PI );
4312+
#endif
42604313

42614314
// scale down painter: the font size has been multiplied by raster scale factor
42624315
// to workaround a Qt font scaling bug with small font sizes

tests/src/python/test_qgspallabeling_tests.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,17 @@ def test_text_color(self):
7979
self.checkTest()
8080

8181
def test_background_rect(self):
82+
self._Mismatches['TestComposerImageVsCanvasPoint'] = 800
83+
self._Mismatches['TestComposerImagePoint'] = 800
8284
self.lyr.shapeDraw = True
8385
self._Mismatches['TestCanvasPoint'] = 776;
8486
self._ColorTols['TestComposerPdfPoint'] = 1;
8587
self.checkTest()
8688

8789
def test_background_rect_w_offset(self):
8890
# Label rectangular background
91+
self._Mismatches['TestComposerImageVsCanvasPoint'] = 800
92+
self._Mismatches['TestComposerImagePoint'] = 800
8993
# verify fix for issues
9094
# http://hub.qgis.org/issues/9057
9195
# http://gis.stackexchange.com/questions/86900

0 commit comments

Comments
 (0)