Skip to content

Commit

Permalink
[composer] More cleanups - move font utils from QgsComposerItem to Qg…
Browse files Browse the repository at this point in the history
…sComposerUtils, add tests
  • Loading branch information
nyalldawson committed Jul 19, 2014
1 parent 57dcfca commit f76c257
Show file tree
Hide file tree
Showing 30 changed files with 529 additions and 109 deletions.
40 changes: 27 additions & 13 deletions python/core/composer/qgscomposeritem.sip
Expand Up @@ -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
Expand All @@ -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
Expand Down
93 changes: 92 additions & 1 deletion python/core/composer/qgscomposerutils.sip
Expand Up @@ -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
Expand All @@ -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 );

};
5 changes: 3 additions & 2 deletions src/core/composer/qgscomposerattributetable.cpp 100644 → 100755
Expand Up @@ -18,6 +18,7 @@
#include "qgscomposerattributetable.h"
#include "qgscomposertablecolumn.h"
#include "qgscomposermap.h"
#include "qgscomposerutils.h"
#include "qgsmaplayerregistry.h"
#include "qgsvectorlayer.h"

Expand Down Expand Up @@ -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;
Expand Down
47 changes: 8 additions & 39 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -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
Expand All @@ -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
Expand Down
42 changes: 28 additions & 14 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -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
Expand All @@ -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
Expand Down
11 changes: 4 additions & 7 deletions src/core/composer/qgscomposerlabel.cpp 100644 → 100755
Expand Up @@ -17,6 +17,7 @@

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

Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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;

Expand Down

0 comments on commit f76c257

Please sign in to comment.