Skip to content

Commit

Permalink
Font workaround enabled for composer scale bar
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9261 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
mhugent committed Sep 5, 2008
1 parent 8c0dddb commit 8247cd3
Show file tree
Hide file tree
Showing 9 changed files with 46 additions and 87 deletions.
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -523,7 +523,7 @@ void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
}
}

void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font)
void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font) const
{
QFont textFont = scaledFontPixelSize(font);

Expand All @@ -535,7 +535,7 @@ void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, c
p->restore();
}

void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font)
void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font) const
{
QFont textFont = scaledFontPixelSize(font);

Expand Down
41 changes: 22 additions & 19 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -105,6 +105,28 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem

const QgsComposition* composition() const {return mComposition;}

//functions that encapsulate the workaround for the Qt font bug (that is to scale the font size up and then scale the
//painter down by the same factor for drawing

/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
to work arount the Qt font bug)*/
void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font) const;

/**Like the above, but with a rectangle for multiline text*/
void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font) const;

/**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMM(const QFont& font, const QString& text) const;

/**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontAscentMM(const QFont& font) const;

/**Calculates font to from point size to pixel size*/
double pixelFontSize(double pointSize) const;

/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
QFont scaledFontPixelSize(const QFont& font) const;

protected:

QgsComposition* mComposition;
Expand Down Expand Up @@ -151,25 +173,6 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem

/**Draw background*/
virtual void drawBackground( QPainter* p );

/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
to work arount the Qt font bug)*/
void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font);

/**Like the above, but with a rectangle for multiline text*/
void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font);

/**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMM(const QFont& font, const QString& text) const;

/**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontAscentMM(const QFont& font) const;

/**Calculates font to from point size to pixel size*/
double pixelFontSize(double pointSize) const;

/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
QFont scaledFontPixelSize(const QFont& font) const;
};

#endif
45 changes: 5 additions & 40 deletions src/core/composer/qgscomposerscalebar.cpp
Expand Up @@ -48,11 +48,11 @@ void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsIt
drawBackground( painter );
painter->setPen( QPen( QColor( 0, 0, 0 ) ) ); //draw all text black

//calculate half of first label width as labels are drawn centered
QFontMetricsF fontMetrics( mFont );
//x-offset is half of first label width because labels are drawn centered
QString firstLabel = firstLabelString();
double firstLabelWidth = textWidthMM(mFont, firstLabel);

mStyle->draw( painter, fontMetrics.width( firstLabel ) / 2 );
mStyle->draw( painter, firstLabelWidth / 2 );

//draw frame and selection boxes if necessary
drawFrame( painter );
Expand Down Expand Up @@ -126,15 +126,7 @@ void QgsComposerScaleBar::applyDefaultSettings()
mBrush.setColor( QColor( 0, 0, 0 ) );
mBrush.setStyle( Qt::SolidPattern );

//default size 12 point
if ( mComposition )
{
mFont.setPixelSize( mComposition->pixelFontSize( 12 ) );
}
else
{
mFont.setPixelSize( 5 );
}
mFont.setPointSizeF(12.0);

mLabelBarSpace = 3.0;
mBoxContentSpace = 1.0;
Expand Down Expand Up @@ -173,12 +165,6 @@ void QgsComposerScaleBar::update()
QgsComposerItem::update();
}

double QgsComposerScaleBar::fontHeight() const
{
QFontMetricsF labelFontMetrics( mFont );
return labelFontMetrics.ascent();
}

void QgsComposerScaleBar::updateSegmentSize()
{
refreshSegmentMM();
Expand Down Expand Up @@ -267,34 +253,13 @@ QString QgsComposerScaleBar::firstLabelString() const
}

QFont QgsComposerScaleBar::font() const
{
if ( mComposition ) //make pixel to point conversion to show correct point value in dialogs
{
double pointSize = mComposition->pointFontSize( mFont.pixelSize() );
QFont returnFont = mFont;
returnFont.setPointSize( pointSize );
return returnFont;
}
}

QFont QgsComposerScaleBar::fontPixelSize() const
{
return mFont;
}

void QgsComposerScaleBar::setFont( const QFont& font )
{
//set font size in pixels for proper preview and printout
if ( mComposition )
{
int pixelSize = mComposition->pixelFontSize( font.pointSizeF() );
mFont = font;
mFont.setPixelSize( pixelSize );
}
else
{
mFont = font;
}
mFont = font;
adjustBoxSize();
update();
}
Expand Down
6 changes: 0 additions & 6 deletions src/core/composer/qgscomposerscalebar.h
Expand Up @@ -56,9 +56,6 @@ class CORE_EXPORT QgsComposerScaleBar: public QObject, public QgsComposerItem
void setUnitLabeling( const QString& label ) {mUnitLabeling = label;}

QFont font() const;

/**Returns font that has size set in pixels. Used from QgsComposerScaleBarStyle*/
QFont fontPixelSize() const;

void setFont( const QFont& font );

Expand Down Expand Up @@ -96,9 +93,6 @@ class CORE_EXPORT QgsComposerScaleBar: public QObject, public QgsComposerItem
of the segment*/
void segmentPositions( QList<QPair<double, double> >& posWidthList ) const;

/**Returns height of mFont in points*/
double fontHeight() const;

/**Sets box size suitable to content*/
void adjustBoxSize();

Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgsdoubleboxscalebarstyle.cpp
Expand Up @@ -45,7 +45,7 @@ void QgsDoubleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
{
return;
}
double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double segmentHeight = mScaleBar->height() / 2;

p->save();
Expand Down
13 changes: 7 additions & 6 deletions src/core/composer/qgsnumericscalebarstyle.cpp
Expand Up @@ -48,10 +48,9 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
}

p->save();

p->setFont( mScaleBar->font() );
p->drawText( QPointF( mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace()
+ mScaleBar->fontHeight() ), scaleText() );

mScaleBar->drawText(p, mScaleBar->pen().widthF() + mScaleBar->boxContentSpace(), mScaleBar->boxContentSpace() + mScaleBar->fontAscentMM(mScaleBar->font()), scaleText(), mScaleBar->font());

p->restore();
}
Expand All @@ -64,10 +63,12 @@ QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
return rect;
}

QFontMetricsF fontMetrics( mScaleBar->font() );
double textWidth = mScaleBar->textWidthMM(mScaleBar->font(), scaleText());
double textHeight = mScaleBar->fontAscentMM(mScaleBar->font());

return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), 2 * mScaleBar->boxContentSpace()
+ 2 * mScaleBar->pen().width() + fontMetrics.width( scaleText() ),
mScaleBar->fontHeight() + 2 * mScaleBar->boxContentSpace() );
+ 2 * mScaleBar->pen().width() + textWidth,
textHeight + 2 * mScaleBar->boxContentSpace() );
}

QString QgsNumericScaleBarStyle::scaleText() const
Expand Down
18 changes: 7 additions & 11 deletions src/core/composer/qgsscalebarstyle.cpp
Expand Up @@ -43,11 +43,10 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const

p->save();

p->setFont( mScaleBar->fontPixelSize() );
p->setFont(mScaleBar->font());

QFontMetricsF fontMetrics( mScaleBar->fontPixelSize() );
QString firstLabel = mScaleBar->firstLabelString();
double xOffset = fontMetrics.width( firstLabel ) / 2;
double xOffset = mScaleBar->textWidthMM(mScaleBar->font(), firstLabel) / 2;

//double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
QList<QPair<double, double> > segmentInfo;
Expand Down Expand Up @@ -79,7 +78,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const

if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
{
p->drawText( QPointF( segmentIt->first - fontMetrics.width( currentNumericLabel ) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace() ), currentNumericLabel );
mScaleBar->drawText(p, segmentIt->first - mScaleBar->textWidthMM(mScaleBar->font(), currentNumericLabel) / 2 + xOffset, mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font());
}

if ( segmentCounter >= nSegmentsLeft )
Expand All @@ -93,7 +92,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
if ( !segmentInfo.isEmpty() )
{
currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
p->drawText( QPointF( segmentInfo.last().first + mScaleBar->segmentMM() - fontMetrics.width( currentNumericLabel ) / 2 + xOffset, mScaleBar->fontHeight() + mScaleBar->boxContentSpace() ), currentNumericLabel + " " + mScaleBar->unitLabeling() );
mScaleBar->drawText(p, segmentInfo.last().first + mScaleBar->segmentMM() - mScaleBar->textWidthMM(mScaleBar->font(), currentNumericLabel) / 2 + xOffset, mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->boxContentSpace(), currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font());
}

p->restore();
Expand All @@ -106,18 +105,15 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
return QRectF();
}


QFontMetricsF fontMetrics( mScaleBar->fontPixelSize() );

//consider centered first label
double firstLabelLeft = fontMetrics.width( mScaleBar->firstLabelString() ) / 2;
double firstLabelLeft = mScaleBar->textWidthMM(mScaleBar->font(), mScaleBar->firstLabelString()) / 2;

//consider last number and label

double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit();
QString largestNumberLabel = QString::number( largestLabelNumber );
QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
double largestLabelWidth = fontMetrics.width( largestLabel ) - fontMetrics.width( largestNumberLabel ) / 2;
double largestLabelWidth = mScaleBar->textWidthMM(mScaleBar->font(), largestLabel) - mScaleBar->textWidthMM(mScaleBar->font(), largestNumberLabel) / 2;

double totalBarLength = 0.0;

Expand All @@ -131,7 +127,7 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
}

double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontHeight();
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontAscentMM(mScaleBar->font());

return QRectF( mScaleBar->transform().dx(), mScaleBar->transform().dy(), width, height );
}
2 changes: 1 addition & 1 deletion src/core/composer/qgssingleboxscalebarstyle.cpp
Expand Up @@ -40,7 +40,7 @@ void QgsSingleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
{
return;
}
double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();

p->save();
p->setPen( p->pen() );
Expand Down
2 changes: 1 addition & 1 deletion src/core/composer/qgsticksscalebarstyle.cpp
Expand Up @@ -53,7 +53,7 @@ void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
{
return;
}
double barTopPosition = mScaleBar->fontHeight() + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double barTopPosition = mScaleBar->fontAscentMM(mScaleBar->font()) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
double bottomPosition = barTopPosition + mScaleBar->height();

Expand Down

0 comments on commit 8247cd3

Please sign in to comment.