Skip to content
Permalink
Browse files
[composer] More cleanups - move font utils from QgsComposerItem to Qg…
…sComposerUtils, add tests
  • Loading branch information
nyalldawson committed Jul 19, 2014
1 parent 57dcfca commit f76c257f89f944f853ab27aba8256d9fec63d207
Showing with 529 additions and 109 deletions.
  1. +27 −13 python/core/composer/qgscomposeritem.sip
  2. +92 −1 python/core/composer/qgscomposerutils.sip
  3. +3 −2 src/core/composer/qgscomposerattributetable.cpp
  4. +8 −39 src/core/composer/qgscomposeritem.cpp
  5. +28 −14 src/core/composer/qgscomposeritem.h
  6. +4 −7 src/core/composer/qgscomposerlabel.cpp
  7. +5 −4 src/core/composer/qgscomposermapgrid.cpp
  8. +2 −1 src/core/composer/qgscomposerscalebar.cpp
  9. +11 −12 src/core/composer/qgscomposertable.cpp
  10. +104 −0 src/core/composer/qgscomposerutils.cpp
  11. +93 −0 src/core/composer/qgscomposerutils.h
  12. +2 −1 src/core/composer/qgsdoubleboxscalebarstyle.cpp
  13. +4 −4 src/core/composer/qgsnumericscalebarstyle.cpp
  14. +9 −6 src/core/composer/qgsscalebarstyle.cpp
  15. +2 −1 src/core/composer/qgssingleboxscalebarstyle.cpp
  16. +2 −1 src/core/composer/qgsticksscalebarstyle.cpp
  17. +3 −3 src/plugins/georeferencer/qgsresidualplotitem.cpp
  18. +130 −0 tests/src/core/testqgscomposerutils.cpp
  19. BIN tests/testdata/control_images/expected_composerutils_drawtext_pos/anomaly_win7.png
  20. BIN ...stdata/control_images/expected_composerutils_drawtext_pos/expected_composerutils_drawtext_pos.png
  21. BIN tests/testdata/control_images/expected_composerutils_drawtext_posnocolor/anomaly_win7.png
  22. BIN ..._images/expected_composerutils_drawtext_posnocolor/expected_composerutils_drawtext_posnocolor.png
  23. BIN tests/testdata/control_images/expected_composerutils_drawtext_rect/anomaly_win7.png
  24. BIN ...data/control_images/expected_composerutils_drawtext_rect/expected_composerutils_drawtext_rect.png
  25. BIN tests/testdata/control_images/expected_composerutils_drawtext_rectalign/anomaly_win7.png
  26. BIN ...ol_images/expected_composerutils_drawtext_rectalign/expected_composerutils_drawtext_rectalign.png
  27. BIN tests/testdata/control_images/expected_composerutils_drawtext_rectflag/anomaly_win7.png
  28. BIN ...trol_images/expected_composerutils_drawtext_rectflag/expected_composerutils_drawtext_rectflag.png
  29. BIN tests/testdata/control_images/expected_composerutils_drawtext_rectnocolor/anomaly_win7.png
  30. BIN ...mages/expected_composerutils_drawtext_rectnocolor/expected_composerutils_drawtext_rectnocolor.png
@@ -397,8 +397,10 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
//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 around the Qt font bug)*/
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const;
* to work around the Qt font bug)
* @deprecated use QgsComposerUtils::drawText instead
*/
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font ) const /Deprecated/;

/**Like the above, but with a rectangle for multiline text
* @param p painter to use
@@ -408,34 +410,46 @@ class QgsComposerItem : QgsComposerObject, QGraphicsRectItem
* @param halignment optional horizontal alignment
* @param valignment optional vertical alignment
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
* @deprecated use QgsComposerUtils::drawText instead
*/
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;
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/;

/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMillimeters( const QFont& font, const QString& text ) const;
/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::textWidthMM instead
*/
double textWidthMillimeters( const QFont& font, const QString& text ) const /Deprecated/;

/**Returns the font height of a character in millimeters
@note this method was added in version 1.7*/
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
* @note this method was added in version 1.7
* @deprecated use QgsComposerUtils::fontHeightCharacterMM instead
*/
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const /Deprecated/;

/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontAscentMillimeters( const QFont& font ) const;
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::fontAscentMM instead
*/
double fontAscentMillimeters( const QFont& font ) const /Deprecated/;

/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontDescentMillimeters( const QFont& font ) const;
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::fontDescentMM instead
*/
double fontDescentMillimeters( const QFont& font ) const /Deprecated/;

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

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

/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::scaledFontPixelSize instead
*/
QFont scaledFontPixelSize( const QFont& font ) const;

/**Locks / unlocks the item position for mouse drags
@@ -93,7 +93,6 @@ class QgsComposerUtils
static void readDataDefinedProperty( const QgsComposerObject::DataDefinedProperty property, const QDomElement &ddElem,
QMap< QgsComposerObject::DataDefinedProperty, QgsDataDefined* >* dataDefinedProperties );


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

/**Returns a font where size is set in pixels and the size has been upscaled with FONT_WORKAROUND_SCALE
* to workaround QT font rendering bugs
* @param font source font with size set in points
* @returns font with size set in pixels
* @note added in version 2.5
*/
static QFont scaledFontPixelSize( const QFont& font );

/**Calculate font ascent in millimeters, including workarounds for QT font rendering issues
* @param font input font
* @returns font ascent in millimeters
* @note added in version 2.5
* @see fontDescentMM
* @see fontHeightMM
* @see fontHeightCharacterMM
* @see textWidthMM
*/
static double fontAscentMM( const QFont& font );

/**Calculate font descent in millimeters, including workarounds for QT font rendering issues
* @param font input font
* @returns font descent in millimeters
* @note added in version 2.5
* @see fontAscentMM
* @see fontHeightMM
* @see fontHeightCharacterMM
* @see textWidthMM
*/
static double fontDescentMM( const QFont& font );

/**Calculate font height in millimeters, including workarounds for QT font rendering issues
* The font height is the font ascent + descent + 1 (for the baseline).
* @param font input font
* @returns font height in millimeters
* @note added in version 2.5
* @see fontAscentMM
* @see fontDescentMM
* @see fontHeightCharacterMM
* @see textWidthMM
*/
static double fontHeightMM( const QFont& font );

/**Calculate font height in millimeters of a single character, including workarounds for QT font
* rendering issues
* @param font input font
* @param character character to calculate height for
* @returns character height in millimeters
* @note added in version 2.5
* @see fontAscentMM
* @see fontDescentMM
* @see fontHeightMM
* @see textWidthMM
*/
static double fontHeightCharacterMM( const QFont& font, const QChar& character );

/**Calculate font width in millimeters for a string, including workarounds for QT font
* rendering issues
* @param font input font
* @param text string to calculate width of
* @returns string width in millimeters
* @note added in version 2.5
* @see fontAscentMM
* @see fontDescentMM
* @see fontHeightMM
* @see fontHeightCharacterMM
*/
static double textWidthMM( const QFont& font, const QString& text );

/**Draws text on a painter at a specific position, taking care of composer specific issues (calculation to pixel,
* scaling of font and painter to work around Qt font bugs)
* @param painter destination QPainter
* @param pos position to draw text
* @param text string to draw
* @param font font to use for drawing text
* @param color color to draw text
* @note added in version 2.5
*/
static void drawText( QPainter* painter, const QPointF& pos, const QString& text, const QFont& font, const QColor& color = QColor() );

/**Draws text on a painter within a rectangle, taking care of composer specific issues (calculation to pixel,
* scaling of font and painter to work around Qt font bugs)
* @param painter destination QPainter
* @param rect rectangle to draw into
* @param text string to draw
* @param font font to use for drawing text
* @param color color to draw text
* @param halignment optional horizontal alignment
* @param valignment optional vertical alignment
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
* @note added in version 2.5
*/
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 );

};
@@ -18,6 +18,7 @@
#include "qgscomposerattributetable.h"
#include "qgscomposertablecolumn.h"
#include "qgscomposermap.h"
#include "qgscomposerutils.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"

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

double titleHeight = 2 * mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mHeaderFont );
double attributeHeight = mGridStrokeWidth + 2 * mLineTextDistance + fontAscentMillimeters( mContentFont );
double titleHeight = 2 * mGridStrokeWidth + 2 * mLineTextDistance + QgsComposerUtils::fontAscentMM( mHeaderFont );
double attributeHeight = mGridStrokeWidth + 2 * mLineTextDistance + QgsComposerUtils::fontAscentMM( mContentFont );
if (( evaluatedRect.height() - titleHeight ) > 0 )
{
mMaximumNumberOfFeatures = ( evaluatedRect.height() - titleHeight ) / attributeHeight;
@@ -861,64 +861,36 @@ void QgsComposerItem::setEffectsEnabled( const bool effectsEnabled )

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

p->save();
p->setFont( textFont );
p->setPen( c );
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale( scaleFactor, scaleFactor );
p->drawText( QPointF( x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE ), text );
p->restore();
QgsComposerUtils::drawText( p, QPointF( x, y ), text, font, c );
}

void QgsComposerItem::drawText( QPainter* p, const QRectF& rect, const QString& text, const QFont& font, Qt::AlignmentFlag halignment, Qt::AlignmentFlag valignment, int flags ) const
{
QFont textFont = scaledFontPixelSize( font );

QRectF scaledRect( rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE );

p->save();
p->setFont( textFont );
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale( scaleFactor, scaleFactor );
p->drawText( scaledRect, halignment | valignment | flags, text );
p->restore();
QgsComposerUtils::drawText( p, rect, text, font, QColor(), halignment, valignment, flags );
}
double QgsComposerItem::textWidthMillimeters( const QFont& font, const QString& text ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
return QgsComposerUtils::textWidthMM( font, text );
}

double QgsComposerItem::fontHeightCharacterMM( const QFont& font, const QChar& c ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.boundingRect( c ).height() / FONT_WORKAROUND_SCALE );
return QgsComposerUtils::fontHeightCharacterMM( font, c );
}

double QgsComposerItem::fontAscentMillimeters( const QFont& font ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
return QgsComposerUtils::fontAscentMM( font );
}

double QgsComposerItem::fontDescentMillimeters( const QFont& font ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
return QgsComposerUtils::fontDescentMM( font );
}

double QgsComposerItem::fontHeightMillimeters( const QFont& font ) const
{
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
return QgsComposerUtils::fontHeightMM( font );
}

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

QFont QgsComposerItem::scaledFontPixelSize( const QFont& font ) const
{
QFont scaledFont = font;
double pixelSize = QgsComposerUtils::pointsToMM( font.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
scaledFont.setPixelSize( pixelSize );
return scaledFont;
return QgsComposerUtils::scaledFontPixelSize( font );
}

double QgsComposerItem::horizontalViewScaleFactor() const
@@ -353,8 +353,10 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
//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 around the Qt font bug)*/
void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c = QColor( 0, 0, 0 ) ) const;
* to work around the Qt font bug)
* @deprecated use QgsComposerUtils::drawText instead
*/
Q_DECL_DEPRECATED void drawText( QPainter* p, double x, double y, const QString& text, const QFont& font, const QColor& c = QColor() ) const;

/**Like the above, but with a rectangle for multiline text
* @param p painter to use
@@ -364,35 +366,47 @@ class CORE_EXPORT QgsComposerItem: public QgsComposerObject, public QGraphicsRec
* @param halignment optional horizontal alignment
* @param valignment optional vertical alignment
* @param flags allows for passing Qt::TextFlags to control appearance of rendered text
* @deprecated use QgsComposerUtils::drawText instead
*/
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;
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;

/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMillimeters( const QFont& font, const QString& text ) const;
/**Returns the font width in millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::textWidthMM instead
*/
Q_DECL_DEPRECATED double textWidthMillimeters( const QFont& font, const QString& text ) const;

/**Returns the font height of a character in millimeters
@note this method was added in version 1.7*/
double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;
* @note this method was added in version 1.7
* @deprecated use QgsComposerUtils::fontHeightCharacterMM instead
*/
Q_DECL_DEPRECATED double fontHeightCharacterMM( const QFont& font, const QChar& c ) const;

/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontAscentMillimeters( const QFont& font ) const;
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::fontAscentMM instead
*/
Q_DECL_DEPRECATED double fontAscentMillimeters( const QFont& font ) const;

/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontDescentMillimeters( const QFont& font ) const;
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::fontDescentMM instead
*/
Q_DECL_DEPRECATED double fontDescentMillimeters( const QFont& font ) const;

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

/**Calculates font size in mm from a font point size
* @deprecated use QgsComposerUtils::mmFontSize instead
*/
Q_DECL_DEPRECATED 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;
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE
* @deprecated use QgsComposerUtils::scaledFontPixelSize instead
*/
Q_DECL_DEPRECATED QFont scaledFontPixelSize( const QFont& font ) const;

/**Locks / unlocks the item position for mouse drags
* @param lock set to true to prevent item movement and resizing via the mouse
@@ -17,6 +17,7 @@

#include "qgscomposerlabel.h"
#include "qgscomposition.h"
#include "qgscomposerutils.h"
#include "qgsexpression.h"
#include "qgsnetworkaccessmanager.h"

@@ -148,15 +149,11 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
}
else
{
painter->setPen( QPen( QColor( mFontColor ) ) );
painter->setFont( mFont );

QFontMetricsF fontSize( mFont );

//debug
//painter->setPen( QColor( Qt::red ) );
//painter->drawRect( painterRect );
drawText( painter, painterRect, textToDraw, mFont, mHAlignment, mVAlignment, Qt::TextWordWrap );
QgsComposerUtils::drawText( painter, painterRect, textToDraw, mFont, mFontColor, mHAlignment, mVAlignment, Qt::TextWordWrap );
}

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

void QgsComposerLabel::adjustSizeToText()
{
double textWidth = textWidthMillimeters( mFont, displayText() );
double fontHeight = fontHeightMillimeters( mFont );
double textWidth = QgsComposerUtils::textWidthMM( mFont, displayText() );
double fontHeight = QgsComposerUtils::fontHeightMM( mFont );

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

0 comments on commit f76c257

Please sign in to comment.