Skip to content

Commit f76c257

Browse files
committedJul 19, 2014
[composer] More cleanups - move font utils from QgsComposerItem to QgsComposerUtils, add tests
1 parent 57dcfca commit f76c257

30 files changed

+529
-109
lines changed
 

‎python/core/composer/qgscomposeritem.sip

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,8 +397,10 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
397397
//painter down by the same factor for drawing
398398

399399
/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
400-
to work around the Qt font bug)*/
401-
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
400+
* to work around the Qt font bug)
401+
* @deprecated use QgsComposerUtils::drawText instead
402+
*/
403+
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const /Deprecated/;
402404

403405
/**Like the above, but with a rectangle for multiline text
404406
* @param p painter to use
@@ -408,34 +410,46 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
408410
* @param halignment optional horizontal alignment
409411
* @param valignment optional vertical alignment
410412
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
413+
* @deprecated use QgsComposerUtils::drawText instead
411414
*/
412-
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const;
415+
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const /Deprecated/;
413416

414-
/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
415-
double textWidthMillimeters( const QFont& font, const QString& text ) const;
417+
/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
418+
* @deprecated use QgsComposerUtils::textWidthMM instead
419+
*/
420+
double textWidthMillimeters( const QFont& font, const QString& text ) const /Deprecated/;
416421

417422
/**Returns the font height of a character in millimeters
418-
@note this method was added in version 1.7*/
419-
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
423+
* @note this method was added in version 1.7
424+
* @deprecated use QgsComposerUtils::fontHeightCharacterMM instead
425+
*/
426+
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const /Deprecated/;
420427

421-
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
422-
double fontAscentMillimeters( const QFont& font ) const;
428+
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
429+
* @deprecated use QgsComposerUtils::fontAscentMM instead
430+
*/
431+
double fontAscentMillimeters( const QFont& font ) const /Deprecated/;
423432

424-
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
425-
double fontDescentMillimeters( const QFont& font ) const;
433+
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
434+
* @deprecated use QgsComposerUtils::fontDescentMM instead
435+
*/
436+
double fontDescentMillimeters( const QFont& font ) const /Deprecated/;
426437

427438
/**Returns the font height in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.
428439
* Font height equals the font ascent+descent+1 (for baseline).
429440
* @note Added in version 2.4
441+
* @deprecated use QgsComposerUtils::fontHeightMM instead
430442
*/
431-
double fontHeightMillimeters( const QFont& font ) const;
443+
double fontHeightMillimeters( const QFont& font ) const /Deprecated/;
432444

433445
/**Calculates font size in mm from a font point size
434446
* @deprecated use QgsComposerUtils::mmFontSize instead
435447
*/
436448
double pixelFontSize( double pointSize ) const /Deprecated/;
437449

438-
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
450+
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE
451+
* @deprecated use QgsComposerUtils::scaledFontPixelSize instead
452+
*/
439453
QFont scaledFontPixelSize( const QFont& font ) const;
440454

441455
/**Locks / unlocks the item position for mouse drags

‎python/core/composer/qgscomposerutils.sip

Lines changed: 92 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,6 @@ class QgsComposerUtils
9393
static void readDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, const QDomElement &ddElem,
9494
QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );
9595

96-
9796
/**Writes data defined properties to xml
9897
* @param itemElem DOM element in which to store data defined properties
9998
* @param doc DOM document
@@ -105,5 +104,97 @@ class QgsComposerUtils
105104
const QMap< QgsComposerObject::DataDefinedProperty, QString >* dataDefinedNames,
106105
const QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );
107106

107+
/**Returns a font where size is set in pixels and the size has been upscaled with FONT_WORKAROUND_SCALE
108+
* to workaround QT font rendering bugs
109+
* @param font source font with size set in points
110+
* @returns font with size set in pixels
111+
* @note added in version 2.5
112+
*/
113+
static QFont scaledFontPixelSize( const QFont& font );
114+
115+
/**Calculate font ascent in millimeters, including workarounds for QT font rendering issues
116+
* @param font input font
117+
* @returns font ascent in millimeters
118+
* @note added in version 2.5
119+
* @see fontDescentMM
120+
* @see fontHeightMM
121+
* @see fontHeightCharacterMM
122+
* @see textWidthMM
123+
*/
124+
static double fontAscentMM( const QFont& font );
125+
126+
/**Calculate font descent in millimeters, including workarounds for QT font rendering issues
127+
* @param font input font
128+
* @returns font descent in millimeters
129+
* @note added in version 2.5
130+
* @see fontAscentMM
131+
* @see fontHeightMM
132+
* @see fontHeightCharacterMM
133+
* @see textWidthMM
134+
*/
135+
static double fontDescentMM( const QFont& font );
136+
137+
/**Calculate font height in millimeters, including workarounds for QT font rendering issues
138+
* The font height is the font ascent + descent + 1 (for the baseline).
139+
* @param font input font
140+
* @returns font height in millimeters
141+
* @note added in version 2.5
142+
* @see fontAscentMM
143+
* @see fontDescentMM
144+
* @see fontHeightCharacterMM
145+
* @see textWidthMM
146+
*/
147+
static double fontHeightMM( const QFont& font );
148+
149+
/**Calculate font height in millimeters of a single character, including workarounds for QT font
150+
* rendering issues
151+
* @param font input font
152+
* @param character character to calculate height for
153+
* @returns character height in millimeters
154+
* @note added in version 2.5
155+
* @see fontAscentMM
156+
* @see fontDescentMM
157+
* @see fontHeightMM
158+
* @see textWidthMM
159+
*/
160+
static double fontHeightCharacterMM( const QFont& font, const QChar& character );
161+
162+
/**Calculate font width in millimeters for a string, including workarounds for QT font
163+
* rendering issues
164+
* @param font input font
165+
* @param text string to calculate width of
166+
* @returns string width in millimeters
167+
* @note added in version 2.5
168+
* @see fontAscentMM
169+
* @see fontDescentMM
170+
* @see fontHeightMM
171+
* @see fontHeightCharacterMM
172+
*/
173+
static double textWidthMM( const QFont& font, const QString& text );
174+
175+
/**Draws text on a painter at a specific position, taking care of composer specific issues (calculation to pixel,
176+
* scaling of font and painter to work around Qt font bugs)
177+
* @param painter destination QPainter
178+
* @param pos position to draw text
179+
* @param text string to draw
180+
* @param font font to use for drawing text
181+
* @param color color to draw text
182+
* @note added in version 2.5
183+
*/
184+
static void drawText( QPainter* painter, const QPointF& pos, const QString& text, const QFont& font, const QColor& color = QColor() );
185+
186+
/**Draws text on a painter within a rectangle, taking care of composer specific issues (calculation to pixel,
187+
* scaling of font and painter to work around Qt font bugs)
188+
* @param painter destination QPainter
189+
* @param rect rectangle to draw into
190+
* @param text string to draw
191+
* @param font font to use for drawing text
192+
* @param color color to draw text
193+
* @param halignment optional horizontal alignment
194+
* @param valignment optional vertical alignment
195+
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
196+
* @note added in version 2.5
197+
*/
198+
static void drawText( QPainter* painter, const QRectF& rect, const QString& text, const QFont& font, const QColor& color = QColor(), const Qt::AlignmentFlag halignment = Qt::AlignLeft, const Qt::AlignmentFlag valignment = Qt::AlignTop, const int flags = Qt::TextWordWrap );
108199

109200
};

‎src/core/composer/qgscomposerattributetable.cpp

100644100755
Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgscomposerattributetable.h"
1919
#include "qgscomposertablecolumn.h"
2020
#include "qgscomposermap.h"
21+
#include "qgscomposerutils.h"
2122
#include "qgsmaplayerregistry.h"
2223
#include "qgsvectorlayer.h"
2324

@@ -484,8 +485,8 @@ void QgsComposerAttributeTable::setSceneRect( const QRectF& rectangle )
484485
//update rect for data defined size and position
485486
QRectF evaluatedRect = evalItemRect( rectangle );
486487

487-
double titleHeight = 2 * mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mHeaderFont );
488-
double attributeHeight = mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mContentFont );
488+
double titleHeight = 2 * mGridStrokeWidth + 2 * mLineTextDistance + QgsComposerUtils::fontAscentMM( mHeaderFont );
489+
double attributeHeight = mGridStrokeWidth + 2 * mLineTextDistance + QgsComposerUtils::fontAscentMM( mContentFont );
489490
if (( evaluatedRect.height() - titleHeight ) > 0 )
490491
{
491492
mMaximumNumberOfFeatures = ( evaluatedRect.height() - titleHeight ) / attributeHeight;

‎src/core/composer/qgscomposeritem.cpp

Lines changed: 8 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -861,64 +861,36 @@ void QgsComposerItem::setEffectsEnabled( const bool effectsEnabled )
861861

862862
void QgsComposerItem::drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c ) const
863863
{
864-
QFont textFont = scaledFontPixelSize( font );
865-
866-
p->save();
867-
p->setFont( textFont );
868-
p->setPen( c );
869-
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
870-
p->scale( scaleFactor, scaleFactor );
871-
p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
872-
p->restore();
864+
QgsComposerUtils::drawText( p, QPointF( x, y ), text, font, c );
873865
}
874866

875867
void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
876868
{
877-
QFont textFont = scaledFontPixelSize( font );
878-
879-
QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
880-
rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
881-
882-
p->save();
883-
p->setFont( textFont );
884-
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
885-
p->scale( scaleFactor, scaleFactor );
886-
p->drawText( scaledRect, halignment | valignment | flags, text );
887-
p->restore();
869+
QgsComposerUtils::drawText( p, rect, text, font, QColor(), halignment, valignment, flags );
888870
}
889871
double QgsComposerItem::textWidthMillimeters( const QFont& font, const QString& text ) const
890872
{
891-
QFont metricsFont = scaledFontPixelSize( font );
892-
QFontMetricsF fontMetrics( metricsFont );
893-
return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
873+
return QgsComposerUtils::textWidthMM( font, text );
894874
}
895875

896876
double QgsComposerItem::fontHeightCharacterMM( const QFont& font, const QChar& c ) const
897877
{
898-
QFont metricsFont = scaledFontPixelSize( font );
899-
QFontMetricsF fontMetrics( metricsFont );
900-
return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
878+
return QgsComposerUtils::fontHeightCharacterMM( font, c );
901879
}
902880

903881
double QgsComposerItem::fontAscentMillimeters( const QFont& font ) const
904882
{
905-
QFont metricsFont = scaledFontPixelSize( font );
906-
QFontMetricsF fontMetrics( metricsFont );
907-
return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
883+
return QgsComposerUtils::fontAscentMM( font );
908884
}
909885

910886
double QgsComposerItem::fontDescentMillimeters( const QFont& font ) const
911887
{
912-
QFont metricsFont = scaledFontPixelSize( font );
913-
QFontMetricsF fontMetrics( metricsFont );
914-
return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
888+
return QgsComposerUtils::fontDescentMM( font );
915889
}
916890

917891
double QgsComposerItem::fontHeightMillimeters( const QFont& font ) const
918892
{
919-
QFont metricsFont = scaledFontPixelSize( font );
920-
QFontMetricsF fontMetrics( metricsFont );
921-
return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
893+
return QgsComposerUtils::fontHeightMM( font );
922894
}
923895

924896
double QgsComposerItem::pixelFontSize( double pointSize ) const
@@ -928,10 +900,7 @@ double QgsComposerItem::pixelFontSize( double pointSize ) const
928900

929901
QFont QgsComposerItem::scaledFontPixelSize( const QFont& font ) const
930902
{
931-
QFont scaledFont = font;
932-
double pixelSize = QgsComposerUtils::pointsToMM( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
933-
scaledFont.setPixelSize( pixelSize );
934-
return scaledFont;
903+
return QgsComposerUtils::scaledFontPixelSize( font );
935904
}
936905

937906
double QgsComposerItem::horizontalViewScaleFactor() const

‎src/core/composer/qgscomposeritem.h

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -353,8 +353,10 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
353353
//painter down by the same factor for drawing
354354

355355
/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
356-
to work around the Qt font bug)*/
357-
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c = QColor( 0, 0, 0 ) ) const;
356+
* to work around the Qt font bug)
357+
* @deprecated use QgsComposerUtils::drawText instead
358+
*/
359+
Q_DECL_DEPRECATED void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c = QColor() ) const;
358360

359361
/**Like the above, but with a rectangle for multiline text
360362
* @param p painter to use
@@ -364,35 +366,47 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
364366
* @param halignment optional horizontal alignment
365367
* @param valignment optional vertical alignment
366368
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
369+
* @deprecated use QgsComposerUtils::drawText instead
367370
*/
368-
void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const;
371+
Q_DECL_DEPRECATED void drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment = Qt::AlignLeft, Qt::AlignmentFlag valignment = Qt::AlignTop, int flags = Qt::TextWordWrap ) const;
369372

370-
/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
371-
double textWidthMillimeters( const QFont& font, const QString& text ) const;
373+
/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
374+
* @deprecated use QgsComposerUtils::textWidthMM instead
375+
*/
376+
Q_DECL_DEPRECATED double textWidthMillimeters( const QFont& font, const QString& text ) const;
372377

373378
/**Returns the font height of a character in millimeters
374-
@note this method was added in version 1.7*/
375-
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
379+
* @note this method was added in version 1.7
380+
* @deprecated use QgsComposerUtils::fontHeightCharacterMM instead
381+
*/
382+
Q_DECL_DEPRECATED double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
376383

377-
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
378-
double fontAscentMillimeters( const QFont& font ) const;
384+
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
385+
* @deprecated use QgsComposerUtils::fontAscentMM instead
386+
*/
387+
Q_DECL_DEPRECATED double fontAscentMillimeters( const QFont& font ) const;
379388

380-
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
381-
double fontDescentMillimeters( const QFont& font ) const;
389+
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
390+
* @deprecated use QgsComposerUtils::fontDescentMM instead
391+
*/
392+
Q_DECL_DEPRECATED double fontDescentMillimeters( const QFont& font ) const;
382393

383394
/**Returns the font height in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.
384395
* Font height equals the font ascent+descent+1 (for baseline).
385396
* @note Added in version 2.4
397+
* @deprecated use QgsComposerUtils::fontHeightMM instead
386398
*/
387-
double fontHeightMillimeters( const QFont& font ) const;
399+
Q_DECL_DEPRECATED double fontHeightMillimeters( const QFont& font ) const;
388400

389401
/**Calculates font size in mm from a font point size
390402
* @deprecated use QgsComposerUtils::mmFontSize instead
391403
*/
392404
Q_DECL_DEPRECATED double pixelFontSize( double pointSize ) const;
393405

394-
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
395-
QFont scaledFontPixelSize( const QFont& font ) const;
406+
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE
407+
* @deprecated use QgsComposerUtils::scaledFontPixelSize instead
408+
*/
409+
Q_DECL_DEPRECATED QFont scaledFontPixelSize( const QFont& font ) const;
396410

397411
/**Locks / unlocks the item position for mouse drags
398412
* @param lock set to true to prevent item movement and resizing via the mouse

‎src/core/composer/qgscomposerlabel.cpp

100644100755
Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717

1818
#include "qgscomposerlabel.h"
1919
#include "qgscomposition.h"
20+
#include "qgscomposerutils.h"
2021
#include "qgsexpression.h"
2122
#include "qgsnetworkaccessmanager.h"
2223

@@ -148,15 +149,11 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
148149
}
149150
else
150151
{
151-
painter->setPen( QPen( QColor( mFontColor ) ) );
152152
painter->setFont( mFont );
153-
154-
QFontMetricsF fontSize( mFont );
155-
156153
//debug
157154
//painter->setPen( QColor( Qt::red ) );
158155
//painter->drawRect( painterRect );
159-
drawText( painter, painterRect, textToDraw, mFont, mHAlignment, mVAlignment, Qt::TextWordWrap );
156+
QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
160157
}
161158

162159
painter->restore();
@@ -259,8 +256,8 @@ void QgsComposerLabel::setFont( const QFont& f )
259256

260257
void QgsComposerLabel::adjustSizeToText()
261258
{
262-
double textWidth = textWidthMillimeters( mFont, displayText() );
263-
double fontHeight = fontHeightMillimeters( mFont );
259+
double textWidth = QgsComposerUtils::textWidthMM( mFont, displayText() );
260+
double fontHeight = QgsComposerUtils::fontHeightMM( mFont );
264261

265262
double penWidth = hasFrame() ? pen().widthF() : 0;
266263

‎src/core/composer/qgscomposermapgrid.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
***************************************************************************/
1717

1818
#include "qgscomposermapgrid.h"
19+
#include "qgscomposerutils.h"
1920
#include "qgsclipper.h"
2021
#include "qgsgeometry.h"
2122
#include "qgscomposermap.h"
@@ -653,9 +654,9 @@ void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& p
653654
return;
654655
}
655656
QgsComposerMap::Border frameBorder = borderForLineCoord( pos );
656-
double textWidth = mComposerMap->textWidthMillimeters( mGridAnnotationFont, annotationString );
657+
double textWidth = QgsComposerUtils::textWidthMM( mGridAnnotationFont, annotationString );
657658
//relevant for annotations is the height of digits
658-
double textHeight = mComposerMap->fontHeightCharacterMM( mGridAnnotationFont, QChar( '0' ) );
659+
double textHeight = QgsComposerUtils::fontHeightCharacterMM( mGridAnnotationFont, QChar( '0' ) );
659660
double xpos = pos.x();
660661
double ypos = pos.y();
661662
int rotation = 0;
@@ -818,7 +819,7 @@ void QgsComposerMapGrid::drawAnnotation( QPainter* p, const QPointF& pos, int ro
818819
p->save();
819820
p->translate( pos );
820821
p->rotate( rotation );
821-
mComposerMap->drawText( p, 0, 0, annotationText, mGridAnnotationFont, mGridAnnotationFontColor );
822+
QgsComposerUtils::drawText( p, QPointF( 0, 0 ), annotationText, mGridAnnotationFont, mGridAnnotationFontColor );
822823
p->restore();
823824
}
824825

@@ -1268,7 +1269,7 @@ double QgsComposerMapGrid::maxExtension() const
12681269
QStringList::const_iterator coordIt = coordStrings.constBegin();
12691270
for ( ; coordIt != coordStrings.constEnd(); ++coordIt )
12701271
{
1271-
currentExtension = qMax( mComposerMap->textWidthMillimeters( mGridAnnotationFont, *coordIt ), mComposerMap->fontAscentMillimeters( mGridAnnotationFont ) );
1272+
currentExtension = qMax( QgsComposerUtils::textWidthMM( mGridAnnotationFont, *coordIt ), QgsComposerUtils::fontAscentMM( mGridAnnotationFont ) );
12721273
maxExtension = qMax( maxExtension, currentExtension );
12731274
}
12741275

‎src/core/composer/qgscomposerscalebar.cpp

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "qgscomposerscalebar.h"
1818
#include "qgscomposermap.h"
1919
#include "qgscomposition.h"
20+
#include "qgscomposerutils.h"
2021
#include "qgsdistancearea.h"
2122
#include "qgsscalebarstyle.h"
2223
#include "qgsdoubleboxscalebarstyle.h"
@@ -68,7 +69,7 @@ void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsIt
6869

6970
//x-offset is half of first label width because labels are drawn centered
7071
QString firstLabel = firstLabelString();
71-
double firstLabelWidth = textWidthMillimeters( mFont, firstLabel );
72+
double firstLabelWidth = QgsComposerUtils::textWidthMM( mFont, firstLabel );
7273

7374
mStyle->draw( painter, firstLabelWidth / 2 );
7475

‎src/core/composer/qgscomposertable.cpp

100644100755
Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include "qgscomposertable.h"
1919
#include "qgscomposertablecolumn.h"
2020
#include "qgssymbollayerv2utils.h"
21+
#include "qgscomposerutils.h"
2122
#include <QPainter>
2223
#include <QSettings>
2324

@@ -95,8 +96,8 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
9596
QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin();
9697

9798
int col = 0;
98-
double cellHeaderHeight = fontAscentMillimeters( mHeaderFont ) + 2 * mLineTextDistance;
99-
double cellBodyHeight = fontAscentMillimeters( mContentFont ) + 2 * mLineTextDistance;
99+
double cellHeaderHeight = QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mLineTextDistance;
100+
double cellBodyHeight = QgsComposerUtils::fontAscentMM( mContentFont ) + 2 * mLineTextDistance;
100101
QRectF cell;
101102
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
102103
{
@@ -123,22 +124,20 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
123124
break;
124125
}
125126

126-
painter->setPen( mHeaderFontColor );
127-
drawText( painter, cell, ( *columnIt )->heading(), mHeaderFont, headerAlign, Qt::AlignVCenter, Qt::TextDontClip );
127+
QgsComposerUtils::drawText( painter, cell, ( *columnIt )->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, Qt::TextDontClip );
128128

129129
currentY += cellHeaderHeight;
130130
currentY += mGridStrokeWidth;
131131

132132
//draw the attribute values
133-
painter->setPen( mContentFontColor );
134133
QList<QgsAttributeMap>::const_iterator attIt = mAttributeMaps.begin();
135134
for ( ; attIt != mAttributeMaps.end(); ++attIt )
136135
{
137136
cell = QRectF( currentX, currentY, mMaxColumnWidthMap[col], cellBodyHeight );
138137

139138
const QgsAttributeMap &currentAttributeMap = *attIt;
140139
QString str = currentAttributeMap[ col ].toString();
141-
drawText( painter, cell, str, mContentFont, ( *columnIt )->hAlignment(), Qt::AlignVCenter, Qt::TextDontClip );
140+
QgsComposerUtils::drawText( painter, cell, str, mContentFont, mContentFontColor, ( *columnIt )->hAlignment(), Qt::AlignVCenter, Qt::TextDontClip );
142141

143142
currentY += cellBodyHeight;
144143
currentY += mGridStrokeWidth;
@@ -352,7 +351,7 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
352351
int col = 0;
353352
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
354353
{
355-
maxWidthMap.insert( col, textWidthMillimeters( mHeaderFont, ( *columnIt )->heading() ) );
354+
maxWidthMap.insert( col, QgsComposerUtils::textWidthMM( mHeaderFont, ( *columnIt )->heading() ) );
356355
col++;
357356
}
358357

@@ -366,7 +365,7 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
366365
QgsAttributeMap::const_iterator attIt2 = attIt->constBegin();
367366
for ( ; attIt2 != attIt->constEnd(); ++attIt2 )
368367
{
369-
currentAttributeTextWidth = textWidthMillimeters( mContentFont, attIt2.value().toString() );
368+
currentAttributeTextWidth = QgsComposerUtils::textWidthMM( mContentFont, attIt2.value().toString() );
370369
if ( currentAttributeTextWidth > maxWidthMap[ attIt2.key()] )
371370
{
372371
maxWidthMap[ attIt2.key()] = currentAttributeTextWidth;
@@ -380,8 +379,8 @@ void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, con
380379
{
381380
//calculate height
382381
int n = attributeMaps.size();
383-
double totalHeight = fontAscentMillimeters( mHeaderFont )
384-
+ n * fontAscentMillimeters( mContentFont )
382+
double totalHeight = QgsComposerUtils::fontAscentMM( mHeaderFont )
383+
+ n * QgsComposerUtils::fontAscentMM( mContentFont )
385384
+ ( n + 1 ) * mLineTextDistance * 2
386385
+ ( n + 2 ) * mGridStrokeWidth;
387386

@@ -408,12 +407,12 @@ void QgsComposerTable::drawHorizontalGridLines( QPainter* p, int nAttributes )
408407
double currentY = halfGridStrokeWidth;
409408
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
410409
currentY += mGridStrokeWidth;
411-
currentY += ( fontAscentMillimeters( mHeaderFont ) + 2 * mLineTextDistance );
410+
currentY += ( QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mLineTextDistance );
412411
for ( int i = 0; i < nAttributes; ++i )
413412
{
414413
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
415414
currentY += mGridStrokeWidth;
416-
currentY += ( fontAscentMillimeters( mContentFont ) + 2 * mLineTextDistance );
415+
currentY += ( QgsComposerUtils::fontAscentMM( mContentFont ) + 2 * mLineTextDistance );
417416
}
418417
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
419418
}

‎src/core/composer/qgscomposerutils.cpp

Lines changed: 104 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,3 +371,107 @@ void QgsComposerUtils::writeDataDefinedPropertyMap( QDomElement &itemElem, QDomD
371371
}
372372
}
373373
}
374+
375+
QFont QgsComposerUtils::scaledFontPixelSize( const QFont &font )
376+
{
377+
//upscale using FONT_WORKAROUND_SCALE
378+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
379+
QFont scaledFont = font;
380+
double pixelSize = pointsToMM( scaledFont.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
381+
scaledFont.setPixelSize( pixelSize );
382+
return scaledFont;
383+
}
384+
385+
double QgsComposerUtils::fontAscentMM( const QFont &font )
386+
{
387+
//upscale using FONT_WORKAROUND_SCALE
388+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
389+
QFont metricsFont = scaledFontPixelSize( font );
390+
QFontMetricsF fontMetrics( metricsFont );
391+
return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
392+
}
393+
394+
double QgsComposerUtils::fontDescentMM( const QFont &font )
395+
{
396+
//upscale using FONT_WORKAROUND_SCALE
397+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
398+
QFont metricsFont = scaledFontPixelSize( font );
399+
QFontMetricsF fontMetrics( metricsFont );
400+
return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
401+
}
402+
403+
double QgsComposerUtils::fontHeightMM( const QFont &font )
404+
{
405+
//upscale using FONT_WORKAROUND_SCALE
406+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
407+
QFont metricsFont = scaledFontPixelSize( font );
408+
QFontMetricsF fontMetrics( metricsFont );
409+
return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
410+
}
411+
412+
double QgsComposerUtils::fontHeightCharacterMM( const QFont &font, const QChar &character )
413+
{
414+
//upscale using FONT_WORKAROUND_SCALE
415+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
416+
QFont metricsFont = scaledFontPixelSize( font );
417+
QFontMetricsF fontMetrics( metricsFont );
418+
return ( fontMetrics.boundingRect( character ).height() / FONT_WORKAROUND_SCALE );
419+
}
420+
421+
double QgsComposerUtils::textWidthMM( const QFont &font, const QString &text )
422+
{
423+
//upscale using FONT_WORKAROUND_SCALE
424+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
425+
QFont metricsFont = scaledFontPixelSize( font );
426+
QFontMetricsF fontMetrics( metricsFont );
427+
return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
428+
}
429+
430+
void QgsComposerUtils::drawText( QPainter *painter, const QPointF &pos, const QString &text, const QFont &font, const QColor &color )
431+
{
432+
if ( !painter )
433+
{
434+
return;
435+
}
436+
437+
//upscale using FONT_WORKAROUND_SCALE
438+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
439+
QFont textFont = scaledFontPixelSize( font );
440+
441+
painter->save();
442+
painter->setFont( textFont );
443+
if ( color.isValid() )
444+
{
445+
painter->setPen( color );
446+
}
447+
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
448+
painter->scale( scaleFactor, scaleFactor );
449+
painter->drawText( pos * FONT_WORKAROUND_SCALE, text );
450+
painter->restore();
451+
}
452+
453+
void QgsComposerUtils::drawText( QPainter *painter, const QRectF &rect, const QString &text, const QFont &font, const QColor &color, const Qt::AlignmentFlag halignment, const Qt::AlignmentFlag valignment, const int flags )
454+
{
455+
if ( !painter )
456+
{
457+
return;
458+
}
459+
460+
//upscale using FONT_WORKAROUND_SCALE
461+
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
462+
QFont textFont = scaledFontPixelSize( font );
463+
464+
QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
465+
rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );
466+
467+
painter->save();
468+
painter->setFont( textFont );
469+
if ( color.isValid() )
470+
{
471+
painter->setPen( color );
472+
}
473+
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
474+
painter->scale( scaleFactor, scaleFactor );
475+
painter->drawText( scaledRect, halignment | valignment | flags, text );
476+
painter->restore();
477+
}

‎src/core/composer/qgscomposerutils.h

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,99 @@ class CORE_EXPORT QgsComposerUtils
126126
const QMap< QgsComposerObject::DataDefinedProperty, QString >* dataDefinedNames,
127127
const QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );
128128

129+
/**Returns a font where size is set in pixels and the size has been upscaled with FONT_WORKAROUND_SCALE
130+
* to workaround QT font rendering bugs
131+
* @param font source font with size set in points
132+
* @returns font with size set in pixels
133+
* @note added in version 2.5
134+
*/
135+
static QFont scaledFontPixelSize( const QFont& font );
136+
137+
/**Calculate font ascent in millimeters, including workarounds for QT font rendering issues
138+
* @param font input font
139+
* @returns font ascent in millimeters
140+
* @note added in version 2.5
141+
* @see fontDescentMM
142+
* @see fontHeightMM
143+
* @see fontHeightCharacterMM
144+
* @see textWidthMM
145+
*/
146+
static double fontAscentMM( const QFont& font );
147+
148+
/**Calculate font descent in millimeters, including workarounds for QT font rendering issues
149+
* @param font input font
150+
* @returns font descent in millimeters
151+
* @note added in version 2.5
152+
* @see fontAscentMM
153+
* @see fontHeightMM
154+
* @see fontHeightCharacterMM
155+
* @see textWidthMM
156+
*/
157+
static double fontDescentMM( const QFont& font );
158+
159+
/**Calculate font height in millimeters, including workarounds for QT font rendering issues
160+
* The font height is the font ascent + descent + 1 (for the baseline).
161+
* @param font input font
162+
* @returns font height in millimeters
163+
* @note added in version 2.5
164+
* @see fontAscentMM
165+
* @see fontDescentMM
166+
* @see fontHeightCharacterMM
167+
* @see textWidthMM
168+
*/
169+
static double fontHeightMM( const QFont& font );
170+
171+
/**Calculate font height in millimeters of a single character, including workarounds for QT font
172+
* rendering issues
173+
* @param font input font
174+
* @param character character to calculate height for
175+
* @returns character height in millimeters
176+
* @note added in version 2.5
177+
* @see fontAscentMM
178+
* @see fontDescentMM
179+
* @see fontHeightMM
180+
* @see textWidthMM
181+
*/
182+
static double fontHeightCharacterMM( const QFont& font, const QChar& character );
183+
184+
/**Calculate font width in millimeters for a string, including workarounds for QT font
185+
* rendering issues
186+
* @param font input font
187+
* @param text string to calculate width of
188+
* @returns string width in millimeters
189+
* @note added in version 2.5
190+
* @see fontAscentMM
191+
* @see fontDescentMM
192+
* @see fontHeightMM
193+
* @see fontHeightCharacterMM
194+
*/
195+
static double textWidthMM( const QFont& font, const QString& text );
196+
197+
/**Draws text on a painter at a specific position, taking care of composer specific issues (calculation to pixel,
198+
* scaling of font and painter to work around Qt font bugs)
199+
* @param painter destination QPainter
200+
* @param pos position to draw text
201+
* @param text string to draw
202+
* @param font font to use for drawing text
203+
* @param color color to draw text
204+
* @note added in version 2.5
205+
*/
206+
static void drawText( QPainter* painter, const QPointF& pos, const QString& text, const QFont& font, const QColor& color = QColor() );
207+
208+
/**Draws text on a painter within a rectangle, taking care of composer specific issues (calculation to pixel,
209+
* scaling of font and painter to work around Qt font bugs)
210+
* @param painter destination QPainter
211+
* @param rect rectangle to draw into
212+
* @param text string to draw
213+
* @param font font to use for drawing text
214+
* @param color color to draw text
215+
* @param halignment optional horizontal alignment
216+
* @param valignment optional vertical alignment
217+
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
218+
* @note added in version 2.5
219+
*/
220+
static void drawText( QPainter* painter, const QRectF& rect, const QString& text, const QFont& font, const QColor& color = QColor(), const Qt::AlignmentFlag halignment = Qt::AlignLeft, const Qt::AlignmentFlag valignment = Qt::AlignTop, const int flags = Qt::TextWordWrap );
221+
129222
};
130223

131224
#endif

‎src/core/composer/qgsdoubleboxscalebarstyle.cpp

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgsdoubleboxscalebarstyle.h"
1818
#include "qgscomposerscalebar.h"
19+
#include "qgscomposerutils.h"
1920
#include <QList>
2021
#include <QPainter>
2122

@@ -45,7 +46,7 @@ void QgsDoubleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
4546
{
4647
return;
4748
}
48-
double barTopPosition = mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
49+
double barTopPosition = QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
4950
double segmentHeight = mScaleBar->height() / 2;
5051

5152
p->save();

‎src/core/composer/qgsnumericscalebarstyle.cpp

100644100755
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "qgsnumericscalebarstyle.h"
1818
#include "qgscomposermap.h"
1919
#include "qgscomposerscalebar.h"
20+
#include "qgscomposerutils.h"
2021
#include <QList>
2122
#include <QPainter>
2223

@@ -52,7 +53,6 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
5253
//antialiasing on
5354
p->setRenderHint( QPainter::Antialiasing, true );
5455
p->setFont( mScaleBar->font() );
55-
p->setPen( mScaleBar->fontColor() );
5656

5757
//call QgsComposerItem's pen() function, since that refers to the frame pen
5858
//and QgsComposerScalebar's pen() function refers to the scale bar line width,
@@ -80,7 +80,7 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
8080

8181
//text destination is item's rect, excluding the margin and frame
8282
QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin );
83-
mScaleBar->drawText( p, painterRect, scaleText(), mScaleBar->font(), hAlign, Qt::AlignTop );
83+
QgsComposerUtils::drawText( p, painterRect, scaleText(), mScaleBar->font(), mScaleBar->fontColor(), hAlign, Qt::AlignTop );
8484

8585
p->restore();
8686
}
@@ -93,8 +93,8 @@ QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
9393
return rect;
9494
}
9595

96-
double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() );
97-
double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() );
96+
double textWidth = QgsComposerUtils::textWidthMM( mScaleBar->font(), scaleText() );
97+
double textHeight = QgsComposerUtils::fontAscentMM( mScaleBar->font() );
9898

9999
rect = QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), 2 * mScaleBar->boxContentSpace()
100100
+ 2 * mScaleBar->pen().width() + textWidth,

‎src/core/composer/qgsscalebarstyle.cpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgsscalebarstyle.h"
1818
#include "qgscomposerscalebar.h"
19+
#include "qgscomposerutils.h"
1920
#include <QFontMetricsF>
2021
#include <QPainter>
2122

@@ -47,7 +48,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
4748
p->setPen( QPen( mScaleBar->fontColor() ) );
4849

4950
QString firstLabel = mScaleBar->firstLabelString();
50-
double xOffset = mScaleBar->textWidthMillimeters( mScaleBar->font(), firstLabel ) / 2;
51+
double xOffset = QgsComposerUtils::textWidthMM( mScaleBar->font(), firstLabel ) / 2;
5152

5253
//double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
5354
QList<QPair<double, double> > segmentInfo;
@@ -79,7 +80,8 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
7980

8081
if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
8182
{
82-
mScaleBar->drawText( p, segmentIt->first - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font() );
83+
QgsComposerUtils::drawText( p, QPointF( segmentIt->first - QgsComposerUtils::textWidthMM( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->boxContentSpace() ),
84+
currentNumericLabel, mScaleBar->font(), mScaleBar->fontColor() );
8385
}
8486

8587
if ( segmentCounter >= nSegmentsLeft )
@@ -93,7 +95,8 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
9395
if ( !segmentInfo.isEmpty() )
9496
{
9597
currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
96-
mScaleBar->drawText( p, segmentInfo.last().first + mScaleBar->segmentMillimeters() - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font() );
98+
QgsComposerUtils::drawText( p, QPointF( segmentInfo.last().first + mScaleBar->segmentMillimeters() - QgsComposerUtils::textWidthMM( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->boxContentSpace() ),
99+
currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font(), mScaleBar->fontColor() );
97100
}
98101

99102
p->restore();
@@ -107,14 +110,14 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
107110
}
108111

109112
//consider centered first label
110-
double firstLabelLeft = mScaleBar->textWidthMillimeters( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2;
113+
double firstLabelLeft = QgsComposerUtils::textWidthMM( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2;
111114

112115
//consider last number and label
113116

114117
double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit();
115118
QString largestNumberLabel = QString::number( largestLabelNumber );
116119
QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
117-
double largestLabelWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), largestLabel ) - mScaleBar->textWidthMillimeters( mScaleBar->font(), largestNumberLabel ) / 2;
120+
double largestLabelWidth = QgsComposerUtils::textWidthMM( mScaleBar->font(), largestLabel ) - QgsComposerUtils::textWidthMM( mScaleBar->font(), largestNumberLabel ) / 2;
118121

119122
double totalBarLength = 0.0;
120123

@@ -128,7 +131,7 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
128131
}
129132

130133
double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
131-
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() );
134+
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + QgsComposerUtils::fontAscentMM( mScaleBar->font() );
132135

133136
return QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), width, height );
134137
}

‎src/core/composer/qgssingleboxscalebarstyle.cpp

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgssingleboxscalebarstyle.h"
1818
#include "qgscomposerscalebar.h"
19+
#include "qgscomposerutils.h"
1920
#include <QList>
2021
#include <QPainter>
2122

@@ -40,7 +41,7 @@ void QgsSingleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
4041
{
4142
return;
4243
}
43-
double barTopPosition = mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
44+
double barTopPosition = QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
4445

4546
p->save();
4647
//antialiasing on

‎src/core/composer/qgsticksscalebarstyle.cpp

100644100755
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "qgsticksscalebarstyle.h"
1818
#include "qgscomposerscalebar.h"
19+
#include "qgscomposerutils.h"
1920
#include <QPainter>
2021

2122
QgsTicksScaleBarStyle::QgsTicksScaleBarStyle( const QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar )
@@ -53,7 +54,7 @@ void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
5354
{
5455
return;
5556
}
56-
double barTopPosition = mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
57+
double barTopPosition = QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
5758
double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
5859
double bottomPosition = barTopPosition + mScaleBar->height();
5960

‎src/plugins/georeferencer/qgsresidualplotitem.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ void QgsResidualPlotItem::paint( QPainter* painter, const QStyleOptionGraphicsIt
7171
painter->setBrush( disabledBrush );
7272
}
7373
painter->drawRect( QRectF( gcpItemMMX - 0.5, gcpItemMMY - 0.5, 1, 1 ) );
74-
drawText( painter, gcpItemMMX + 2, gcpItemMMY + 2, QString::number(( *gcpIt )->id() ), QFont() );
74+
QgsComposerUtils::drawText( painter, QPointF( gcpItemMMX + 2, gcpItemMMY + 2 ), QString::number(( *gcpIt )->id() ), QFont() );
7575

7676
mmPixelRatio = maxMMToPixelRatioForGCP( *gcpIt, gcpItemMMX, gcpItemMMY );
7777
if ( mmPixelRatio < minMMPixelRatio )
@@ -135,11 +135,11 @@ void QgsResidualPlotItem::paint( QPainter* painter, const QStyleOptionGraphicsIt
135135
scaleBarFont.setPointSize( 9 );
136136
if ( mConvertScaleToMapUnits )
137137
{
138-
drawText( painter, 5, rect().height() - 4 + fontAscentMillimeters( scaleBarFont ), QString( "%1 map units" ).arg( scaleBarWidthUnits ), QFont() );
138+
QgsComposerUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsComposerUtils::fontAscentMM( scaleBarFont ) ), QString( "%1 map units" ).arg( scaleBarWidthUnits ), QFont() );
139139
}
140140
else
141141
{
142-
drawText( painter, 5, rect().height() - 4 + fontAscentMillimeters( scaleBarFont ), QString( "%1 pixels" ).arg( scaleBarWidthUnits ), QFont() );
142+
QgsComposerUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsComposerUtils::fontAscentMM( scaleBarFont ) ), QString( "%1 pixels" ).arg( scaleBarWidthUnits ), QFont() );
143143
}
144144

145145
drawFrame( painter );

‎tests/src/core/testqgscomposerutils.cpp

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,12 @@
1515
* *
1616
***************************************************************************/
1717

18+
#include "qgsapplication.h" //for standard test font
1819
#include "qgscomposerutils.h"
1920
#include "qgscomposition.h"
2021
#include "qgscompositionchecker.h"
2122
#include "qgsdatadefined.h"
23+
#include "qgsfontutils.h"
2224
#include <QObject>
2325
#include <QtTest>
2426
#include <QMap>
@@ -44,21 +46,38 @@ class TestQgsComposerUtils: public QObject
4446
void readDataDefinedProperty(); //test reading a data defined property
4547
void readDataDefinedPropertyMap(); //test reading a whole data defined property map
4648
void writeDataDefinedPropertyMap(); //test reading a whole data defined property map
49+
void scaledFontPixelSize(); //test creating a scaled font
50+
void fontAscentMM(); //test calculating font ascent in mm
51+
void fontDescentMM(); //test calculating font descent in mm
52+
void fontHeightMM(); //test calculating font height in mm
53+
void fontHeightCharacterMM(); //test calculating font character height in mm
54+
void textWidthMM(); //test calculting text width in mm
55+
void drawTextPos(); //test drawing text at a pos
56+
void drawTextRect(); //test drawing text in a rect
4757

4858
private:
4959
bool renderCheck( QString testName, QImage &image, int mismatchCount = 0 );
5060
QgsComposition* mComposition;
5161
QgsMapSettings mMapSettings;
5262
QString mReport;
63+
QFont mTestFont;
5364

5465
};
5566

5667
void TestQgsComposerUtils::initTestCase()
5768
{
69+
QgsApplication::init();
70+
QgsApplication::initQgis(); //for access to test font
71+
5872
mComposition = new QgsComposition( mMapSettings );
5973
mComposition->setPaperSize( 297, 210 ); //A4 landscape
6074

6175
mReport = "<h1>Composer Utils Tests</h1>\n";
76+
77+
QgsFontUtils::loadStandardTestFonts( QStringList() << "Oblique" );
78+
mTestFont = QgsFontUtils::getStandardTestFont( "Oblique " );
79+
mTestFont.setItalic( true );
80+
6281
}
6382

6483
void TestQgsComposerUtils::cleanupTestCase()
@@ -462,6 +481,117 @@ void TestQgsComposerUtils::writeDataDefinedPropertyMap()
462481
QCOMPARE( dd3Elem.attribute( "field", "bad" ), QString( "field 3" ) );
463482
}
464483

484+
void TestQgsComposerUtils::scaledFontPixelSize()
485+
{
486+
//create a 12 point test font
487+
mTestFont.setPointSize( 12 );
488+
489+
//test scaling of font for painting
490+
QFont scaledFont = QgsComposerUtils::scaledFontPixelSize( mTestFont );
491+
QCOMPARE( scaledFont.pixelSize(), 42 );
492+
QCOMPARE( scaledFont.family(), mTestFont.family() );
493+
}
494+
495+
void TestQgsComposerUtils::fontAscentMM()
496+
{
497+
mTestFont.setPointSize( 12 );
498+
//platform specific font rendering differences mean these tests need to be very leniant
499+
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontAscentMM( mTestFont ), 3.9, 0.5 ) );
500+
}
501+
502+
void TestQgsComposerUtils::fontDescentMM()
503+
{
504+
mTestFont.setPointSize( 12 );
505+
QCOMPARE( QgsComposerUtils::fontDescentMM( mTestFont ), 0.9 );
506+
}
507+
508+
void TestQgsComposerUtils::fontHeightMM()
509+
{
510+
mTestFont.setPointSize( 12 );
511+
//platform specific font rendering differences mean these tests need to be very leniant
512+
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightMM( mTestFont ), 4.9, 0.5 ) );
513+
}
514+
515+
void TestQgsComposerUtils::fontHeightCharacterMM()
516+
{
517+
mTestFont.setPointSize( 12 );
518+
//platform specific font rendering differences mean these tests need to be very leniant
519+
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightCharacterMM( mTestFont, QChar( 'a' ) ), 2.4, 0.15 ) );
520+
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightCharacterMM( mTestFont, QChar( 'l' ) ), 3.15, 0.16 ) );
521+
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightCharacterMM( mTestFont, QChar( 'g' ) ), 3.2, 0.11 ) );
522+
}
523+
524+
void TestQgsComposerUtils::textWidthMM()
525+
{
526+
//platform specific font rendering differences mean this test needs to be very leniant
527+
mTestFont.setPointSize( 12 );
528+
QVERIFY( qgsDoubleNear( QgsComposerUtils::textWidthMM( mTestFont, QString( "test string" ) ), 20, 2 ) );
529+
}
530+
531+
void TestQgsComposerUtils::drawTextPos()
532+
{
533+
//test drawing with no painter
534+
QgsComposerUtils::drawText( 0, QPointF( 5, 15 ), QString( "Abc123" ), mTestFont );
535+
536+
//test drawing text on to image
537+
mTestFont.setPointSize( 48 );
538+
QImage testImage = QImage( 250, 250, QImage::Format_RGB32 );
539+
testImage.fill( qRgb( 152, 219, 249 ) );
540+
QPainter testPainter;
541+
testPainter.begin( &testImage );
542+
QgsComposerUtils::drawText( &testPainter, QPointF( 5, 15 ), QString( "Abc123" ), mTestFont, Qt::white );
543+
testPainter.end();
544+
QVERIFY( renderCheck( "composerutils_drawtext_pos", testImage, 100 ) );
545+
546+
//test drawing with pen color set on painter and no specified color
547+
//text should be drawn using painter pen color
548+
testImage.fill( qRgb( 152, 219, 249 ) );
549+
testPainter.begin( &testImage );
550+
testPainter.setPen( QPen( Qt::green ) );
551+
QgsComposerUtils::drawText( &testPainter, QPointF( 5, 15 ), QString( "Abc123" ), mTestFont );
552+
testPainter.end();
553+
QVERIFY( renderCheck( "composerutils_drawtext_posnocolor", testImage, 100 ) );
554+
}
555+
556+
void TestQgsComposerUtils::drawTextRect()
557+
{
558+
//test drawing with no painter
559+
QgsComposerUtils::drawText( 0, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont );
560+
561+
//test drawing text on to image
562+
mTestFont.setPointSize( 48 );
563+
QImage testImage = QImage( 250, 250, QImage::Format_RGB32 );
564+
testImage.fill( qRgb( 152, 219, 249 ) );
565+
QPainter testPainter;
566+
testPainter.begin( &testImage );
567+
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont, Qt::white );
568+
testPainter.end();
569+
QVERIFY( renderCheck( "composerutils_drawtext_rect", testImage, 100 ) );
570+
571+
//test drawing with pen color set on painter and no specified color
572+
//text should be drawn using painter pen color
573+
testImage.fill( qRgb( 152, 219, 249 ) );
574+
testPainter.begin( &testImage );
575+
testPainter.setPen( QPen( Qt::green ) );
576+
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont );
577+
testPainter.end();
578+
QVERIFY( renderCheck( "composerutils_drawtext_rectnocolor", testImage, 100 ) );
579+
580+
//test alignment settings
581+
testImage.fill( qRgb( 152, 219, 249 ) );
582+
testPainter.begin( &testImage );
583+
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont, Qt::black, Qt::AlignRight, Qt::AlignBottom );
584+
testPainter.end();
585+
QVERIFY( renderCheck( "composerutils_drawtext_rectalign", testImage, 100 ) );
586+
587+
//test extra flags - render without clipping
588+
testImage.fill( qRgb( 152, 219, 249 ) );
589+
testPainter.begin( &testImage );
590+
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 20, 50 ), QString( "Abc123" ), mTestFont, Qt::white, Qt::AlignLeft, Qt::AlignTop, Qt::TextDontClip );
591+
testPainter.end();
592+
QVERIFY( renderCheck( "composerutils_drawtext_rectflag", testImage, 100 ) );
593+
}
594+
465595
bool TestQgsComposerUtils::renderCheck( QString testName, QImage &image, int mismatchCount )
466596
{
467597
mReport += "<h2>" + testName + "</h2>\n";
Loading
Loading
Loading
Loading
Loading
Loading

0 commit comments

Comments
 (0)
Please sign in to comment.