Skip to content

Commit

Permalink
Minor refactor of label drawing
Browse files Browse the repository at this point in the history
Remove use of scale factors from PAL layer settings and use
render context factors directly
  • Loading branch information
nyalldawson committed Oct 24, 2016
1 parent aac2622 commit 5495f20
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 49 deletions.
10 changes: 3 additions & 7 deletions python/core/qgspallabeling.sip
Expand Up @@ -515,10 +515,6 @@ class QgsPalLayerSettings
//! Z-Index of label, where labels with a higher z-index are rendered on top of labels with a lower z-index
double zIndex;

//-- scale factors
double vectorScaleFactor; //scale factor painter units->pixels
double rasterCompressFactor; //pixel resolution scale factor

// called from register feature hook
void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY, QgsFeature* f = 0, QgsRenderContext* context = 0 );

Expand Down Expand Up @@ -836,15 +832,15 @@ class QgsPalLabeling : QgsLabelingEngineInterface

static void drawLabelBuffer( QgsRenderContext& context,
const QgsLabelComponent &component,
const QgsPalLayerSettings& tmpLyr );
const QgsTextFormat& format );

static void drawLabelBackground( QgsRenderContext& context,
QgsLabelComponent component,
const QgsPalLayerSettings& tmpLyr );
const QgsTextFormat& format );

static void drawLabelShadow( QgsRenderContext &context,
const QgsLabelComponent &component,
const QgsPalLayerSettings& tmpLyr );
const QgsTextFormat& format );

//! load/save engine settings to project file
void loadEngineSettings();
Expand Down
4 changes: 1 addition & 3 deletions src/app/qgslabelpreview.cpp
Expand Up @@ -75,9 +75,7 @@ void QgsLabelPreview::paintEvent( QPaintEvent *e )
mContext.setPainter( &p );
QgsLabelComponent component;
component.setText( text() );
QgsPalLayerSettings tmpLyr;
tmpLyr.setFormat( mFormat );
QgsPalLabeling::drawLabelBuffer( mContext, component, tmpLyr );
QgsPalLabeling::drawLabelBuffer( mContext, component, mFormat );
}

QPainterPath path;
Expand Down
38 changes: 14 additions & 24 deletions src/core/qgspallabeling.cpp
Expand Up @@ -175,10 +175,6 @@ QgsPalLayerSettings::QgsPalLayerSettings()
obstacleType = PolygonInterior;
zIndex = 0.0;

// scale factors
vectorScaleFactor = 1.0;
rasterCompressFactor = 1.0;

// data defined string and old-style index values
// NOTE: in QPair use -1 for second value (other values are for old-style layer properties migration)

Expand Down Expand Up @@ -390,9 +386,6 @@ QgsPalLayerSettings& QgsPalLayerSettings::operator=( const QgsPalLayerSettings &
}
mDataDefinedNames = s.mDataDefinedNames;

// scale factors
vectorScaleFactor = s.vectorScaleFactor;
rasterCompressFactor = s.rasterCompressFactor;
return *this;
}

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

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

for ( int i = 0; i < lines; ++i )
{
Expand All @@ -1441,7 +1434,7 @@ void QgsPalLayerSettings::calculateLabelSize( const QFontMetricsF* fm, QString t
w = width;
}
}
w /= rasterCompressFactor;
w /= context->rasterScaleFactor();

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

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

// TODO: allow layer-wide feature dist in PAL...?
Expand Down Expand Up @@ -2297,7 +2290,7 @@ void QgsPalLayerSettings::registerFeature( QgsFeature& f, QgsRenderContext &cont
}
else //mm
{
distance *= vectorScaleFactor;
distance *= context.scaleFactor();
}

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

void QgsPalLabeling::drawLabelBuffer( QgsRenderContext& context,
const QgsLabelComponent& component,
const QgsPalLayerSettings& tmpLyr )
const QgsTextFormat& format )
{
QPainter* p = context.painter();

QgsTextFormat format = tmpLyr.format();
QgsTextBufferSettings buffer = format.buffer();

double penSize = QgsTextRenderer::scaleToPixelContext( buffer.size(), context,
Expand Down Expand Up @@ -4120,7 +4112,7 @@ void QgsPalLabeling::drawLabelBuffer( QgsRenderContext& context,
bufferComponent.setOrigin( QgsPoint( 0.0, 0.0 ) );
bufferComponent.setPicture( &buffPict );
bufferComponent.setPictureBuffer( penSize / 2.0 );
drawLabelShadow( context, bufferComponent, tmpLyr );
drawLabelShadow( context, bufferComponent, format );
}

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

void QgsPalLabeling::drawLabelBackground( QgsRenderContext& context,
QgsLabelComponent component,
const QgsPalLayerSettings& tmpLyr )
const QgsTextFormat& format )
{
QgsTextFormat format = tmpLyr.format();
QgsTextBackgroundSettings background = format.background();

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

drawLabelShadow( context, component, tmpLyr );
drawLabelShadow( context, component, format );
p->restore();

delete svgShdwM;
Expand Down Expand Up @@ -4415,7 +4406,7 @@ void QgsPalLabeling::drawLabelBackground( QgsRenderContext& context,

component.setSize( QgsPoint( rect.width(), rect.height() ) );
component.setOffset( QgsPoint( rect.width() / 2, -rect.height() / 2 ) );
drawLabelShadow( context, component, tmpLyr );
drawLabelShadow( context, component, format );
}

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

void QgsPalLabeling::drawLabelShadow( QgsRenderContext& context,
const QgsLabelComponent& component,
const QgsPalLayerSettings& tmpLyr )
const QgsTextFormat& format )
{
QgsTextShadowSettings shadow = format.shadow();

// incoming component sizes should be multiplied by rasterCompressFactor, as
// this allows shadows to be created at paint device dpi (e.g. high resolution),
// then scale device painter by 1.0 / rasterCompressFactor for output

QgsTextFormat format = tmpLyr.format();
QgsTextShadowSettings shadow = format.shadow();

QPainter* p = context.painter();
double componentWidth = component.size().x(), componentHeight = component.size().y();
double xOffset = component.offset().x(), yOffset = component.offset().y();
Expand All @@ -4451,7 +4441,7 @@ void QgsPalLabeling::drawLabelShadow( QgsRenderContext& context,
// generate pixmap representation of label component drawing
bool mapUnits = shadow.blurRadiusUnit() == QgsUnitTypes::RenderMapUnits;
double radius = QgsTextRenderer::scaleToPixelContext( shadow.blurRadius(), context, shadow.blurRadiusUnit(), !mapUnits, shadow.blurRadiusMapUnitScale() );
radius /= ( mapUnits ? tmpLyr.vectorScaleFactor / component.dpiRatio() : 1 );
radius /= ( mapUnits ? context.scaleFactor() / component.dpiRatio() : 1 );
radius = static_cast< int >( radius + 0.5 );

// TODO: add labeling gui option to adjust blurBufferClippingScale to minimize pixels, or
Expand Down
10 changes: 3 additions & 7 deletions src/core/qgspallabeling.h
Expand Up @@ -535,10 +535,6 @@ class CORE_EXPORT QgsPalLayerSettings
//! Z-Index of label, where labels with a higher z-index are rendered on top of labels with a lower z-index
double zIndex;

//-- scale factors
double vectorScaleFactor; //scale factor painter units->pixels
double rasterCompressFactor; //pixel resolution scale factor

// called from register feature hook
void calculateLabelSize( const QFontMetricsF* fm, QString text, double& labelX, double& labelY, QgsFeature* f = nullptr, QgsRenderContext* context = nullptr );

Expand Down Expand Up @@ -961,15 +957,15 @@ class CORE_EXPORT QgsPalLabeling : public QgsLabelingEngineInterface

static void drawLabelBuffer( QgsRenderContext& context,
const QgsLabelComponent &component,
const QgsPalLayerSettings& tmpLyr );
const QgsTextFormat& format );

static void drawLabelBackground( QgsRenderContext& context,
QgsLabelComponent component,
const QgsPalLayerSettings& tmpLyr );
const QgsTextFormat& format );

static void drawLabelShadow( QgsRenderContext &context,
const QgsLabelComponent &component,
const QgsPalLayerSettings& tmpLyr );
const QgsTextFormat& format );

//! load/save engine settings to project file
void loadEngineSettings();
Expand Down
12 changes: 4 additions & 8 deletions src/core/qgsvectorlayerlabelprovider.cpp
Expand Up @@ -213,10 +213,6 @@ bool QgsVectorLayerLabelProvider::prepare( const QgsRenderContext& context, QSet
// TODO: ideally these (non-configuration) members should get out of QgsPalLayerSettings to here
// (together with registerFeature() & related methods) and QgsPalLayerSettings just stores config

//raster and vector scale factors
lyr.vectorScaleFactor = context.scaleFactor();
lyr.rasterCompressFactor = context.rasterScaleFactor();

// save the pal layer to our layer context (with some additional info)
lyr.fieldIndex = mFields.lookupField( lyr.fieldName );

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

QgsPalLabeling::drawLabelBackground( context, component, tmpLyr );
QgsPalLabeling::drawLabelBackground( context, component, tmpLyr.format() );
}

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

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

// figure x offset for horizontal alignment of multiple lines
double xMultiLineOffset = 0.0;
Expand Down Expand Up @@ -717,7 +713,7 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
if ( drawType == QgsPalLabeling::LabelBuffer )
{
// draw label's buffer
QgsPalLabeling::drawLabelBuffer( context, component, tmpLyr );
QgsPalLabeling::drawLabelBuffer( context, component, tmpLyr.format() );
}
else
{
Expand Down Expand Up @@ -746,7 +742,7 @@ void QgsVectorLayerLabelProvider::drawLabelPrivate( pal::LabelPosition* label, Q
component.setPictureBuffer( 0.0 ); // no pen width to deal with
component.setOrigin( QgsPoint( 0.0, 0.0 ) );

QgsPalLabeling::drawLabelShadow( context, component, tmpLyr );
QgsPalLabeling::drawLabelShadow( context, component, tmpLyr.format() );
}

// paint the text
Expand Down

0 comments on commit 5495f20

Please sign in to comment.