Skip to content

Commit

Permalink
First try to fix font scale problem for map labels
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk@9557 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Oct 31, 2008
1 parent 5c1a061 commit 31725ec
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 35 deletions.
2 changes: 1 addition & 1 deletion python/core/qgslabel.sip
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public:
void renderLabel ( QPainter* painter, QgsRect& viewExtent,
QgsCoordinateTransform* coordinateTransform,
QgsMapToPixel *transform,
QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes=0, double sizeScale = 1);
QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes=0, double sizeScale = 1, double rasterScaleFactor = 1);

/** Reads the renderer configuration from an XML file
@param rnode the Dom node to read
Expand Down
5 changes: 0 additions & 5 deletions python/core/qgsvectorlayer.sip
Original file line number Diff line number Diff line change
Expand Up @@ -298,11 +298,6 @@ public:
/** Draws the layer labels using coordinate transformation */
void drawLabels(QgsRenderContext& rendererContext);

/** \brief Draws the layer labels using coordinate transformation
* \param scale size scale, applied to all values in pixels
*/
void drawLabels(QPainter * p, const QgsRect& viewExtent, const QgsMapToPixel* cXf, const QgsCoordinateTransform* ct, double scale);

/** returns list of attributes */
QList<int> pendingAllAttributesList();

Expand Down
20 changes: 14 additions & 6 deletions src/core/qgslabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ void QgsLabel::renderLabel( QPainter * painter, const QgsRect& viewExtent,
const QgsCoordinateTransform* coordinateTransform,
const QgsMapToPixel *transform,
QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes,
double sizeScale )
double sizeScale, double rasterScaleFactor )
{

QPen pen;
Expand Down Expand Up @@ -155,6 +155,11 @@ void QgsLabel::renderLabel( QPainter * painter, const QgsRect& viewExtent,
double sizeMM = size * 0.3527;
size = sizeMM * sizeScale;
}

//Request font larger (multiplied by rasterScaleFactor) as a workaround for the Qt font bug
//and scale the painter down by rasterScaleFactor when drawing the label
size *= rasterScaleFactor;

if ( size > 0.0 )
{
font.setPixelSize( size );
Expand Down Expand Up @@ -349,7 +354,7 @@ void QgsLabel::renderLabel( QPainter * painter, const QgsRect& viewExtent,
{
renderLabel( painter, overridePoint, coordinateTransform,
transform, text, font, pen, dx, dy,
xoffset, yoffset, ang, width, height, alignment, sizeScale );
xoffset, yoffset, ang, width, height, alignment, sizeScale, rasterScaleFactor );
}
else
{
Expand All @@ -359,7 +364,7 @@ void QgsLabel::renderLabel( QPainter * painter, const QgsRect& viewExtent,
{
renderLabel( painter, points[i], coordinateTransform,
transform, text, font, pen, dx, dy,
xoffset, yoffset, ang, width, height, alignment, sizeScale );
xoffset, yoffset, ang, width, height, alignment, sizeScale, rasterScaleFactor );
}
}
}
Expand All @@ -371,7 +376,7 @@ void QgsLabel::renderLabel( QPainter* painter, QgsPoint point,
int dx, int dy,
double xoffset, double yoffset,
double ang,
int width, int height, int alignment, double sizeScale )
int width, int height, int alignment, double sizeScale, double rasterScaleFactor )
{
// Convert point to projected units
if ( coordinateTransform )
Expand All @@ -397,18 +402,21 @@ void QgsLabel::renderLabel( QPainter* painter, QgsPoint point,

x = x + xoffset * cos( rad ) - yoffset * sin( rad );
y = y - xoffset * sin( rad ) - yoffset * cos( rad );


painter->save();
painter->setFont( font );
painter->translate( x, y );
//correct oversampled font size back by scaling painter down
painter->scale(1.0 / rasterScaleFactor, 1.0 / rasterScaleFactor);
painter->rotate( -ang );

//
// Draw a buffer behind the text if one is desired
//
if ( mLabelAttributes->bufferSizeIsSet() && mLabelAttributes->bufferEnabled() )
{
double myBufferSize = mLabelAttributes->bufferSize() * 0.3527 * sizeScale;
double myBufferSize = mLabelAttributes->bufferSize() * 0.3527 * sizeScale * rasterScaleFactor;
QPen bufferPen;
if ( mLabelAttributes->bufferColorIsSet() )
{
Expand All @@ -427,7 +435,7 @@ void QgsLabel::renderLabel( QPainter* painter, QgsPoint point,
}
else //draw more dense in case of logical devices
{
bufferStepSize = 0.1;
bufferStepSize = 1 / rasterScaleFactor;
}

for ( double i = dx - myBufferSize; i <= dx + myBufferSize; i += bufferStepSize )
Expand Down
4 changes: 2 additions & 2 deletions src/core/qgslabel.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ class CORE_EXPORT QgsLabel
void renderLabel( QPainter* painter, const QgsRect& viewExtent,
const QgsCoordinateTransform* coordinateTransform,
const QgsMapToPixel *transform,
QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes = 0, double sizeScale = 1. );
QgsFeature &feature, bool selected, QgsLabelAttributes *classAttributes = 0, double sizeScale = 1., double rasterScaleFactor = 1.0);

/** Reads the renderer configuration from an XML file
@param rnode the Dom node to read
Expand Down Expand Up @@ -134,7 +134,7 @@ class CORE_EXPORT QgsLabel
int dx, int dy,
double xoffset, double yoffset,
double ang,
int width, int height, int alignment, double sizeScale = 1.0 );
int width, int height, int alignment, double sizeScale = 1.0, double rasterScaleFactor = 1.0);

/** Get label point for simple feature in map units */
void labelPoint( std::vector<QgsPoint>&, QgsFeature &feature );
Expand Down
16 changes: 3 additions & 13 deletions src/core/qgsvectorlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,20 +279,10 @@ void QgsVectorLayer::setDisplayField( QString fldName )
}
}

void QgsVectorLayer::drawLabels( QgsRenderContext& rendererContext )
{
QPainter* thePainter = rendererContext.painter();
if ( !thePainter )
{
return;
}
drawLabels( thePainter, rendererContext.extent(), &( rendererContext.mapToPixel() ), rendererContext.coordinateTransform(), rendererContext.scaleFactor() );
}

// NOTE this is a temporary method added by Tim to prevent label clipping
// which was occurring when labeller was called in the main draw loop
// This method will probably be removed again in the near future!
void QgsVectorLayer::drawLabels( QPainter * p, const QgsRect& viewExtent, const QgsMapToPixel* theMapToPixelTransform, const QgsCoordinateTransform* ct, double scale )
void QgsVectorLayer::drawLabels( QgsRenderContext& rendererContext )
{
QgsDebugMsg( "Starting draw of labels" );

Expand All @@ -311,15 +301,15 @@ void QgsVectorLayer::drawLabels( QPainter * p, const QgsRect& viewExtent, const
{
// select the records in the extent. The provider sets a spatial filter
// and sets up the selection set for retrieval
select( attributes, viewExtent );
select( attributes, rendererContext.extent() );

QgsFeature fet;
while ( nextFeature( fet ) )
{
if ( mRenderer->willRenderFeature( &fet ) )
{
bool sel = mSelectedFeatureIds.contains( fet.id() );
mLabel->renderLabel( p, viewExtent, ct, theMapToPixelTransform, fet, sel, 0, scale );
mLabel->renderLabel( rendererContext.painter(), rendererContext.extent(), rendererContext.coordinateTransform(), &(rendererContext.mapToPixel()), fet, sel, 0, rendererContext.scaleFactor(), rendererContext.rasterScaleFactor());
}
featureCount++;
}
Expand Down
8 changes: 0 additions & 8 deletions src/core/qgsvectorlayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,14 +357,6 @@ class CORE_EXPORT QgsVectorLayer : public QgsMapLayer
/** Draws the layer labels using coordinate transformation */
void drawLabels( QgsRenderContext& rendererContext );

/** \brief Draws the layer labels using coordinate transformation
* \param scale size scale, applied to all values in pixels
*/
void drawLabels( QPainter * p, const QgsRect& viewExtent,
const QgsMapToPixel* cXf,
const QgsCoordinateTransform* ct,
double scale );

/** returns field list in the to-be-committed state */
const QgsFieldMap &pendingFields();

Expand Down

0 comments on commit 31725ec

Please sign in to comment.