Skip to content

Commit 5ee6a71

Browse files
author
wonder
committed
Applied patch #2739 from Marco - correct scaling of labeling from the labeling plugin in composer.
Includes few tweaks and improvements, additionally the buffer is not considered for the label frame. git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@13599 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 5ead969 commit 5ee6a71

File tree

6 files changed

+47
-31
lines changed

6 files changed

+47
-31
lines changed

src/plugins/labeling/labelinggui.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ LabelingGui::LabelingGui( PalLabeling* lbl, QgsVectorLayer* layer, QWidget* pare
4141
connect( btnChangeFont, SIGNAL( clicked() ), this, SLOT( changeTextFont() ) );
4242
connect( chkBuffer, SIGNAL( toggled( bool ) ), this, SLOT( updatePreview() ) );
4343
connect( btnBufferColor, SIGNAL( clicked() ), this, SLOT( changeBufferColor() ) );
44-
connect( spinBufferSize, SIGNAL( valueChanged( int ) ), this, SLOT( updatePreview() ) );
44+
connect( spinBufferSize, SIGNAL( valueChanged( double ) ), this, SLOT( updatePreview() ) );
4545
connect( btnEngineSettings, SIGNAL( clicked() ), this, SLOT( showEngineConfigDialog() ) );
4646

4747
// set placement methods page based on geometry type

src/plugins/labeling/labelingguibase.ui

+5-6
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,7 @@
206206
<item row="0" column="2">
207207
<widget class="QLabel" name="label_9">
208208
<property name="text">
209-
<string>pixels</string>
209+
<string>mm</string>
210210
</property>
211211
</widget>
212212
</item>
@@ -312,7 +312,7 @@
312312
<item>
313313
<widget class="QLabel" name="label_15">
314314
<property name="text">
315-
<string>pixels</string>
315+
<string>mm</string>
316316
</property>
317317
</widget>
318318
</item>
@@ -472,9 +472,9 @@
472472
</widget>
473473
</item>
474474
<item>
475-
<widget class="QSpinBox" name="spinBufferSize">
476-
<property name="minimum">
477-
<number>1</number>
475+
<widget class="QDoubleSpinBox" name="spinBufferSize">
476+
<property name="suffix">
477+
<string> mm</string>
478478
</property>
479479
</widget>
480480
</item>
@@ -771,7 +771,6 @@
771771
<tabstop>btnChangeFont</tabstop>
772772
<tabstop>btnTextColor</tabstop>
773773
<tabstop>chkBuffer</tabstop>
774-
<tabstop>spinBufferSize</tabstop>
775774
<tabstop>btnBufferColor</tabstop>
776775
<tabstop>sliderPriority</tabstop>
777776
<tabstop>chkScaleBasedVisibility</tabstop>

src/plugins/labeling/labelpreview.cpp

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ void LabelPreview::setTextColor( QColor color )
1515
update();
1616
}
1717

18-
void LabelPreview::setBuffer( int size, QColor color )
18+
void LabelPreview::setBuffer( double size, QColor color )
1919
{
20-
mBufferSize = size;
20+
mBufferSize = size * 88 / 25.4; //assume standard dpi for preview
2121
mBufferColor = color;
2222
update();
2323
}

src/plugins/labeling/labelpreview.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ class LabelPreview : public QLabel
1010

1111
void setTextColor( QColor color );
1212

13-
void setBuffer( int size, QColor color );
13+
void setBuffer( double size, QColor color );
1414

1515
void paintEvent( QPaintEvent* e );
1616

src/plugins/labeling/pallabeling.cpp

+33-17
Original file line numberDiff line numberDiff line change
@@ -60,19 +60,19 @@ class MyLabel : public PalGeometry
6060
const char* strId() { return mStrId.data(); }
6161
QString text() { return mText; }
6262

63-
pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform )
63+
pal::LabelInfo* info( QFontMetrics* fm, const QgsMapToPixel* xform, double fontScale )
6464
{
6565
if ( mInfo ) return mInfo;
6666

6767
// create label info!
6868
QgsPoint ptZero = xform->toMapCoordinates( 0, 0 );
69-
QgsPoint ptSize = xform->toMapCoordinates( 0, -fm->height() );
69+
QgsPoint ptSize = xform->toMapCoordinates( 0, ( int )( -fm->height() / fontScale ) );
7070

7171
mInfo = new pal::LabelInfo( mText.count(), ptSize.y() - ptZero.y() );
7272
for ( int i = 0; i < mText.count(); i++ )
7373
{
7474
mInfo->char_info[i].chr = mText[i].unicode();
75-
ptSize = xform->toMapCoordinates( fm->width( mText[i] ), 0 );
75+
ptSize = xform->toMapCoordinates(( int )( fm->width( mText[i] ) / fontScale ) , 0 );
7676
mInfo->char_info[i].width = ptSize.x() - ptZero.x();
7777
}
7878
return mInfo;
@@ -106,6 +106,8 @@ LayerSettings::LayerSettings()
106106
labelPerPart = false;
107107
mergeLines = false;
108108
minFeatureSize = 0.0;
109+
vectorScaleFactor = 1.0;
110+
rasterCompressFactor = 1.0;
109111
}
110112

111113
LayerSettings::LayerSettings( const LayerSettings& s )
@@ -127,6 +129,8 @@ LayerSettings::LayerSettings( const LayerSettings& s )
127129
labelPerPart = s.labelPerPart;
128130
mergeLines = s.mergeLines;
129131
minFeatureSize = s.minFeatureSize;
132+
vectorScaleFactor = s.vectorScaleFactor;
133+
rasterCompressFactor = s.rasterCompressFactor;
130134

131135
fontMetrics = NULL;
132136
ct = NULL;
@@ -251,11 +255,12 @@ bool LayerSettings::checkMinimumSizeMM( const QgsRenderContext& ct, QgsGeometry*
251255

252256
void LayerSettings::calculateLabelSize( QString text, double& labelX, double& labelY )
253257
{
254-
//QFontMetrics fontMetrics(textFont);
255-
QRect labelRect = /*QRect(0,0,20,20);*/ fontMetrics->boundingRect( text );
258+
QRectF labelRect = fontMetrics->boundingRect( text );
259+
double w = labelRect.width() / rasterCompressFactor;
260+
double h = labelRect.height() / rasterCompressFactor;
261+
262+
QgsPoint ptSize = xform->toMapCoordinates( w, h );
256263

257-
// 2px border...
258-
QgsPoint ptSize = xform->toMapCoordinates( labelRect.width() + 2, labelRect.height() + 2 );
259264
labelX = fabs( ptSize.x() - ptZero.x() );
260265
labelY = fabs( ptSize.y() - ptZero.y() );
261266
}
@@ -295,11 +300,11 @@ void LayerSettings::registerFeature( QgsFeature& f, const QgsRenderContext& cont
295300

296301
// TODO: only for placement which needs character info
297302
pal::Feature* feat = palLayer->getFeature( lbl->strId() );
298-
feat->setLabelInfo( lbl->info( fontMetrics, xform ) );
303+
feat->setLabelInfo( lbl->info( fontMetrics, xform, rasterCompressFactor ) );
299304

300305
// TODO: allow layer-wide feature dist in PAL...?
301306
if ( dist != 0 )
302-
feat->setDistLabel( fabs( ptOne.x() - ptZero.x() )* dist );
307+
feat->setDistLabel( fabs( ptOne.x() - ptZero.x() )* dist * vectorScaleFactor );
303308
}
304309

305310

@@ -402,14 +407,19 @@ int PalLabeling::prepareLayer( QgsVectorLayer* layer, int& attrIndex, QgsRenderC
402407
l->setMergeConnectedLines( lyr.mergeLines );
403408

404409
// set font size from points to output size
405-
double size = 0.3527 * lyr.textFont.pointSizeF() * ctx.scaleFactor(); //* ctx.rasterScaleFactor();
406-
lyr.textFont.setPixelSize(( int )size );
410+
double size = 0.3527 * lyr.textFont.pointSizeF() * ctx.scaleFactor();
411+
// request larger font and then scale down painter (to avoid Qt font scale bug)
412+
lyr.textFont.setPixelSize(( int )( size*ctx.rasterScaleFactor() + 0.5 ) );
413+
414+
//raster and vector scale factors
415+
lyr.vectorScaleFactor = ctx.scaleFactor();
416+
lyr.rasterCompressFactor = ctx.rasterScaleFactor();
407417

408418
// save the pal layer to our layer context (with some additional info)
409419
lyr.palLayer = l;
410420
lyr.fieldIndex = fldIndex;
411421
lyr.fontMetrics = new QFontMetrics( lyr.textFont );
412-
lyr.fontBaseline = lyr.fontMetrics->boundingRect( "X" ).bottom(); // dummy text to find out how many pixels of the text are below the baseline
422+
413423
lyr.xform = mMapRenderer->coordinateTransform();
414424
if ( mMapRenderer->hasCrsTransformEnabled() )
415425
lyr.ct = new QgsCoordinateTransform( layer->srs(), mMapRenderer->destinationSrs() );
@@ -623,16 +633,20 @@ void PalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, const
623633

624634
//QgsDebugMsg( "drawLabel " + QString::number( drawBuffer ) + " " + txt );
625635

626-
// shift by one as we have 2px border
627636
painter->save();
628637
painter->translate( QPointF( outPt.x(), outPt.y() ) );
629638
painter->rotate( -label->getAlpha() * 180 / M_PI );
630-
painter->translate( QPointF( 1, -1 - lyr.fontBaseline ) );
639+
640+
// scale down painter: the font size has been multiplied by raster scale factor
641+
// to workaround a Qt font scaling bug with small font sizes
642+
painter->scale( 1.0 / lyr.rasterCompressFactor, 1.0 / lyr.rasterCompressFactor );
643+
644+
painter->translate( QPointF( 0, - lyr.fontMetrics->descent() ) );
631645

632646
if ( drawBuffer )
633647
{
634648
// we're drawing buffer
635-
drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize, lyr.bufferColor );
649+
drawLabelBuffer( painter, txt, lyr.textFont, lyr.bufferSize * lyr.vectorScaleFactor * lyr.rasterCompressFactor , lyr.bufferColor );
636650
}
637651
else
638652
{
@@ -654,7 +668,7 @@ void PalLabeling::drawLabel( pal::LabelPosition* label, QPainter* painter, const
654668
}
655669

656670

657-
void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font, int size, QColor color )
671+
void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font, double size, QColor color )
658672
{
659673
/*
660674
p->setFont( font );
@@ -666,7 +680,9 @@ void PalLabeling::drawLabelBuffer( QPainter* p, QString text, const QFont& font,
666680

667681
QPainterPath path;
668682
path.addText( 0, 0, font, text );
669-
p->setPen( QPen( color, size ) );
683+
QPen pen( color );
684+
pen.setWidthF( size );
685+
p->setPen( pen );
670686
p->setBrush( color );
671687
p->drawPath( path );
672688
}

src/plugins/labeling/pallabeling.h

+5-4
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,11 @@ class LayerSettings
6060
bool enabled;
6161
int priority; // 0 = low, 10 = high
6262
bool obstacle; // whether it's an obstacle
63-
double dist; // distance from the feature (in pixels)
63+
double dist; // distance from the feature (in mm)
64+
double vectorScaleFactor; //scale factor painter units->pixels
65+
double rasterCompressFactor; //pixel resolution scale factor
6466
int scaleMin, scaleMax; // disabled if both are zero
65-
int bufferSize;
67+
double bufferSize; //buffer size (in mm)
6668
QColor bufferColor;
6769
bool labelPerPart; // whether to label every feature's part or only the biggest one
6870
bool mergeLines;
@@ -81,7 +83,6 @@ class LayerSettings
8183
pal::Layer* palLayer;
8284
int fieldIndex;
8385
QFontMetrics* fontMetrics;
84-
int fontBaseline;
8586
const QgsMapToPixel* xform;
8687
const QgsCoordinateTransform* ct;
8788
QgsPoint ptZero, ptOne;
@@ -145,7 +146,7 @@ class PalLabeling : public QgsLabelingEngineInterface
145146

146147
void drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* painter, const QgsMapToPixel* xform );
147148
void drawLabel( pal::LabelPosition* label, QPainter* painter, const QgsMapToPixel* xform, bool drawBuffer = false );
148-
static void drawLabelBuffer( QPainter* p, QString text, const QFont& font, int size, QColor color );
149+
static void drawLabelBuffer( QPainter* p, QString text, const QFont& font, double size, QColor color );
149150

150151
protected:
151152

0 commit comments

Comments
 (0)