Skip to content

Commit 5495f20

Browse files
committedOct 24, 2016
Minor refactor of label drawing
Remove use of scale factors from PAL layer settings and use render context factors directly
1 parent aac2622 commit 5495f20

File tree

5 files changed

+25
-49
lines changed

5 files changed

+25
-49
lines changed
 

‎python/core/qgspallabeling.sip

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -515,10 +515,6 @@ class QgsPalLayerSettings
515515
//! Z-Index of label, where labels with a higher z-index are rendered on top of labels with a lower z-index
516516
double zIndex;
517517

518-
//-- scale factors
519-
double vectorScaleFactor; //scale factor painter units->pixels
520-
double rasterCompressFactor; //pixel resolution scale factor
521-
522518
// called from register feature hook
523519
void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY, QgsFeature* f = 0, QgsRenderContext* context = 0 );
524520

@@ -836,15 +832,15 @@ class QgsPalLabeling : QgsLabelingEngineInterface
836832

837833
static void drawLabelBuffer( QgsRenderContext& context,
838834
const QgsLabelComponent &component,
839-
const QgsPalLayerSettings& tmpLyr );
835+
const QgsTextFormat& format );
840836

841837
static void drawLabelBackground( QgsRenderContext& context,
842838
QgsLabelComponent component,
843-
const QgsPalLayerSettings& tmpLyr );
839+
const QgsTextFormat& format );
844840

845841
static void drawLabelShadow( QgsRenderContext &context,
846842
const QgsLabelComponent &component,
847-
const QgsPalLayerSettings& tmpLyr );
843+
const QgsTextFormat& format );
848844

849845
//! load/save engine settings to project file
850846
void loadEngineSettings();

‎src/app/qgslabelpreview.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,7 @@ void QgsLabelPreview::paintEvent( QPaintEvent *e )
7575
mContext.setPainter( &p );
7676
QgsLabelComponent component;
7777
component.setText( text() );
78-
QgsPalLayerSettings tmpLyr;
79-
tmpLyr.setFormat( mFormat );
80-
QgsPalLabeling::drawLabelBuffer( mContext, component, tmpLyr );
78+
QgsPalLabeling::drawLabelBuffer( mContext, component, mFormat );
8179
}
8280

8381
QPainterPath path;

‎src/core/qgspallabeling.cpp

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,6 @@ QgsPalLayerSettings::QgsPalLayerSettings()
175175
obstacleType = PolygonInterior;
176176
zIndex = 0.0;
177177

178-
// scale factors
179-
vectorScaleFactor = 1.0;
180-
rasterCompressFactor = 1.0;
181-
182178
// data defined string and old-style index values
183179
// NOTE: in QPair use -1 for second value (other values are for old-style layer properties migration)
184180

@@ -390,9 +386,6 @@ QgsPalLayerSettings& QgsPalLayerSettings::operator=( const QgsPalLayerSettings &
390386
}
391387
mDataDefinedNames = s.mDataDefinedNames;
392388

393-
// scale factors
394-
vectorScaleFactor = s.vectorScaleFactor;
395-
rasterCompressFactor = s.rasterCompressFactor;
396389
return *this;
397390
}
398391

@@ -1431,7 +1424,7 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
14311424
double labelHeight = fm->ascent() + fm->descent(); // ignore +1 for baseline
14321425

14331426
h += fm->height() + static_cast< double >(( lines - 1 ) * labelHeight * multilineH );
1434-
h /= rasterCompressFactor;
1427+
h /= context->rasterScaleFactor();
14351428

14361429
for ( int i = 0; i < lines; ++i )
14371430
{
@@ -1441,7 +1434,7 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
14411434
w = width;
14421435
}
14431436
}
1444-
w /= rasterCompressFactor;
1437+
w /= context->rasterScaleFactor();
14451438

14461439
#if 0 // XXX strk
14471440
QgsPoint ptSize = xform->toMapCoordinatesF( w, h );
@@ -2252,7 +2245,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
22522245
double topMargin = qMax( 0.25 * labelFontMetrics->ascent(), 0.0 );
22532246
double bottomMargin = 1.0 + labelFontMetrics->descent();
22542247
QgsLabelFeature::VisualMargin vm( topMargin, 0.0, bottomMargin, 0.0 );
2255-
vm *= xform->mapUnitsPerPixel() / rasterCompressFactor;
2248+
vm *= xform->mapUnitsPerPixel() / context.rasterScaleFactor();
22562249
( *labelFeature )->setVisualMargin( vm );
22572250

22582251
// store the label's calculated font for later use during painting
@@ -2262,7 +2255,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
22622255
// TODO: only for placement which needs character info
22632256
// account for any data defined font metrics adjustments
22642257
lf->calculateInfo( placement == QgsPalLayerSettings::Curved || placement == QgsPalLayerSettings::PerimeterCurved,
2265-
labelFontMetrics.data(), xform, rasterCompressFactor, maxcharanglein, maxcharangleout );
2258+
labelFontMetrics.data(), xform, context.rasterScaleFactor(), maxcharanglein, maxcharangleout );
22662259
// for labelFeature the LabelInfo is passed to feat when it is registered
22672260

22682261
// TODO: allow layer-wide feature dist in PAL...?
@@ -2297,7 +2290,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
22972290
}
22982291
else //mm
22992292
{
2300-
distance *= vectorScaleFactor;
2293+
distance *= context.scaleFactor();
23012294
}
23022295

23032296
// when using certain placement modes, we force a tiny minimum distance. This ensures that
@@ -4082,11 +4075,10 @@ void QgsPalLabeling::drawLabelCandidateRect( pal::LabelPosition* lp, QPainter* p
40824075

40834076
void QgsPalLabeling::drawLabelBuffer( QgsRenderContext& context,
40844077
const QgsLabelComponent& component,
4085-
const QgsPalLayerSettings& tmpLyr )
4078+
const QgsTextFormat& format )
40864079
{
40874080
QPainter* p = context.painter();
40884081

4089-
QgsTextFormat format = tmpLyr.format();
40904082
QgsTextBufferSettings buffer = format.buffer();
40914083

40924084
double penSize = QgsTextRenderer::scaleToPixelContext( buffer.size(), context,
@@ -4120,7 +4112,7 @@ void QgsPalLabeling::drawLabelBuffer( QgsRenderContext& context,
41204112
bufferComponent.setOrigin( QgsPoint( 0.0, 0.0 ) );
41214113
bufferComponent.setPicture( &buffPict );
41224114
bufferComponent.setPictureBuffer( penSize / 2.0 );
4123-
drawLabelShadow( context, bufferComponent, tmpLyr );
4115+
drawLabelShadow( context, bufferComponent, format );
41244116
}
41254117

41264118
p->save();
@@ -4141,9 +4133,8 @@ void QgsPalLabeling::drawLabelBuffer( QgsRenderContext& context,
41414133

41424134
void QgsPalLabeling::drawLabelBackground( QgsRenderContext& context,
41434135
QgsLabelComponent component,
4144-
const QgsPalLayerSettings& tmpLyr )
4136+
const QgsTextFormat& format )
41454137
{
4146-
QgsTextFormat format = tmpLyr.format();
41474138
QgsTextBackgroundSettings background = format.background();
41484139

41494140
QPainter* p = context.painter();
@@ -4274,7 +4265,7 @@ void QgsPalLabeling::drawLabelBackground( QgsRenderContext& context,
42744265
p->rotate( component.rotationOffset() );
42754266
p->translate( -svgSize / 2, svgSize / 2 );
42764267

4277-
drawLabelShadow( context, component, tmpLyr );
4268+
drawLabelShadow( context, component, format );
42784269
p->restore();
42794270

42804271
delete svgShdwM;
@@ -4415,7 +4406,7 @@ void QgsPalLabeling::drawLabelBackground( QgsRenderContext& context,
44154406

44164407
component.setSize( QgsPoint( rect.width(), rect.height() ) );
44174408
component.setOffset( QgsPoint( rect.width() / 2, -rect.height() / 2 ) );
4418-
drawLabelShadow( context, component, tmpLyr );
4409+
drawLabelShadow( context, component, format );
44194410
}
44204411

44214412
p->setOpacity( background.opacity() );
@@ -4434,15 +4425,14 @@ void QgsPalLabeling::drawLabelBackground( QgsRenderContext& context,
44344425

44354426
void QgsPalLabeling::drawLabelShadow( QgsRenderContext& context,
44364427
const QgsLabelComponent& component,
4437-
const QgsPalLayerSettings& tmpLyr )
4428+
const QgsTextFormat& format )
44384429
{
4430+
QgsTextShadowSettings shadow = format.shadow();
4431+
44394432
// incoming component sizes should be multiplied by rasterCompressFactor, as
44404433
// this allows shadows to be created at paint device dpi (e.g. high resolution),
44414434
// then scale device painter by 1.0 / rasterCompressFactor for output
44424435

4443-
QgsTextFormat format = tmpLyr.format();
4444-
QgsTextShadowSettings shadow = format.shadow();
4445-
44464436
QPainter* p = context.painter();
44474437
double componentWidth = component.size().x(), componentHeight = component.size().y();
44484438
double xOffset = component.offset().x(), yOffset = component.offset().y();
@@ -4451,7 +4441,7 @@ void QgsPalLabeling::drawLabelShadow( QgsRenderContext& context,
44514441
// generate pixmap representation of label component drawing
44524442
bool mapUnits = shadow.blurRadiusUnit() == QgsUnitTypes::RenderMapUnits;
44534443
double radius = QgsTextRenderer::scaleToPixelContext( shadow.blurRadius(), context, shadow.blurRadiusUnit(), !mapUnits, shadow.blurRadiusMapUnitScale() );
4454-
radius /= ( mapUnits ? tmpLyr.vectorScaleFactor / component.dpiRatio() : 1 );
4444+
radius /= ( mapUnits ? context.scaleFactor() / component.dpiRatio() : 1 );
44554445
radius = static_cast< int >( radius + 0.5 );
44564446

44574447
// TODO: add labeling gui option to adjust blurBufferClippingScale to minimize pixels, or

‎src/core/qgspallabeling.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -535,10 +535,6 @@ class CORE_EXPORT QgsPalLayerSettings
535535
//! Z-Index of label, where labels with a higher z-index are rendered on top of labels with a lower z-index
536536
double zIndex;
537537

538-
//-- scale factors
539-
double vectorScaleFactor; //scale factor painter units->pixels
540-
double rasterCompressFactor; //pixel resolution scale factor
541-
542538
// called from register feature hook
543539
void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY, QgsFeature* f = nullptr, QgsRenderContext* context = nullptr );
544540

@@ -961,15 +957,15 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface
961957

962958
static void drawLabelBuffer( QgsRenderContext& context,
963959
const QgsLabelComponent &component,
964-
const QgsPalLayerSettings& tmpLyr );
960+
const QgsTextFormat& format );
965961

966962
static void drawLabelBackground( QgsRenderContext& context,
967963
QgsLabelComponent component,
968-
const QgsPalLayerSettings& tmpLyr );
964+
const QgsTextFormat& format );
969965

970966
static void drawLabelShadow( QgsRenderContext &context,
971967
const QgsLabelComponent &component,
972-
const QgsPalLayerSettings& tmpLyr );
968+
const QgsTextFormat& format );
973969

974970
//! load/save engine settings to project file
975971
void loadEngineSettings();

‎src/core/qgsvectorlayerlabelprovider.cpp

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -213,10 +213,6 @@ bool QgsVectorLayerLabelProvider::prepare( const QgsRenderContext& context, QSet
213213
// TODO: ideally these (non-configuration) members should get out of QgsPalLayerSettings to here
214214
// (together with registerFeature() & related methods) and QgsPalLayerSettings just stores config
215215

216-
//raster and vector scale factors
217-
lyr.vectorScaleFactor = context.scaleFactor();
218-
lyr.rasterCompressFactor = context.rasterScaleFactor();
219-
220216
// save the pal layer to our layer context (with some additional info)
221217
lyr.fieldIndex = mFields.lookupField( lyr.fieldName );
222218

@@ -575,7 +571,7 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
575571
component.setCenter( centerPt );
576572
component.setSize( QgsPoint( label->getWidth(), label->getHeight() ) );
577573

578-
QgsPalLabeling::drawLabelBackground( context, component, tmpLyr );
574+
QgsPalLabeling::drawLabelBackground( context, component, tmpLyr.format() );
579575
}
580576

581577
else if ( drawType == QgsPalLabeling::LabelBuffer
@@ -689,7 +685,7 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
689685

690686
// scale down painter: the font size has been multiplied by raster scale factor
691687
// to workaround a Qt font scaling bug with small font sizes
692-
painter->scale( 1.0 / tmpLyr.rasterCompressFactor, 1.0 / tmpLyr.rasterCompressFactor );
688+
painter->scale( 1.0 / context.rasterScaleFactor(), 1.0 / context.rasterScaleFactor() );
693689

694690
// figure x offset for horizontal alignment of multiple lines
695691
double xMultiLineOffset = 0.0;
@@ -717,7 +713,7 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
717713
if ( drawType == QgsPalLabeling::LabelBuffer )
718714
{
719715
// draw label's buffer
720-
QgsPalLabeling::drawLabelBuffer( context, component, tmpLyr );
716+
QgsPalLabeling::drawLabelBuffer( context, component, tmpLyr.format() );
721717
}
722718
else
723719
{
@@ -746,7 +742,7 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
746742
component.setPictureBuffer( 0.0 ); // no pen width to deal with
747743
component.setOrigin( QgsPoint( 0.0, 0.0 ) );
748744

749-
QgsPalLabeling::drawLabelShadow( context, component, tmpLyr );
745+
QgsPalLabeling::drawLabelShadow( context, component, tmpLyr.format() );
750746
}
751747

752748
// paint the text

0 commit comments

Comments
 (0)
Please sign in to comment.