[composer] More cleanups - move font utils from QgsComposerItem to QgsComposerUtils, add tests

This commit is contained in:
Nyall Dawson 2014-07-19 22:01:58 +10:00
parent 57dcfca487
commit f76c257f89
30 changed files with 529 additions and 109 deletions

View File

@ -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

View File

@ -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 );
};

5
src/core/composer/qgscomposerattributetable.cpp Normal file → Executable file
View File

@ -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;

View File

@ -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

View File

@ -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

11
src/core/composer/qgscomposerlabel.cpp Normal file → Executable file
View File

@ -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;

View File

@ -16,6 +16,7 @@
***************************************************************************/
#include "qgscomposermapgrid.h"
#include "qgscomposerutils.h"
#include "qgsclipper.h"
#include "qgsgeometry.h"
#include "qgscomposermap.h"
@ -653,9 +654,9 @@ void QgsComposerMapGrid::drawCoordinateAnnotation( QPainter* p, const QPointF& p
return;
}
QgsComposerMap::Border frameBorder = borderForLineCoord( pos );
double textWidth = mComposerMap->textWidthMillimeters( mGridAnnotationFont, annotationString );
double textWidth = QgsComposerUtils::textWidthMM( mGridAnnotationFont, annotationString );
//relevant for annotations is the height of digits
double textHeight = mComposerMap->fontHeightCharacterMM( mGridAnnotationFont, QChar( '0' ) );
double textHeight = QgsComposerUtils::fontHeightCharacterMM( mGridAnnotationFont, QChar( '0' ) );
double xpos = pos.x();
double ypos = pos.y();
int rotation = 0;
@ -818,7 +819,7 @@ void QgsComposerMapGrid::drawAnnotation( QPainter* p, const QPointF& pos, int ro
p->save();
p->translate( pos );
p->rotate( rotation );
mComposerMap->drawText( p, 0, 0, annotationText, mGridAnnotationFont, mGridAnnotationFontColor );
QgsComposerUtils::drawText( p, QPointF( 0, 0 ), annotationText, mGridAnnotationFont, mGridAnnotationFontColor );
p->restore();
}
@ -1268,7 +1269,7 @@ double QgsComposerMapGrid::maxExtension() const
QStringList::const_iterator coordIt = coordStrings.constBegin();
for ( ; coordIt != coordStrings.constEnd(); ++coordIt )
{
currentExtension = qMax( mComposerMap->textWidthMillimeters( mGridAnnotationFont, *coordIt ), mComposerMap->fontAscentMillimeters( mGridAnnotationFont ) );
currentExtension = qMax( QgsComposerUtils::textWidthMM( mGridAnnotationFont, *coordIt ), QgsComposerUtils::fontAscentMM( mGridAnnotationFont ) );
maxExtension = qMax( maxExtension, currentExtension );
}

3
src/core/composer/qgscomposerscalebar.cpp Normal file → Executable file
View File

@ -17,6 +17,7 @@
#include "qgscomposerscalebar.h"
#include "qgscomposermap.h"
#include "qgscomposition.h"
#include "qgscomposerutils.h"
#include "qgsdistancearea.h"
#include "qgsscalebarstyle.h"
#include "qgsdoubleboxscalebarstyle.h"
@ -68,7 +69,7 @@ void QgsComposerScaleBar::paint( QPainter* painter, const QStyleOptionGraphicsIt
//x-offset is half of first label width because labels are drawn centered
QString firstLabel = firstLabelString();
double firstLabelWidth = textWidthMillimeters( mFont, firstLabel );
double firstLabelWidth = QgsComposerUtils::textWidthMM( mFont, firstLabel );
mStyle->draw( painter, firstLabelWidth / 2 );

23
src/core/composer/qgscomposertable.cpp Normal file → Executable file
View File

@ -18,6 +18,7 @@
#include "qgscomposertable.h"
#include "qgscomposertablecolumn.h"
#include "qgssymbollayerv2utils.h"
#include "qgscomposerutils.h"
#include <QPainter>
#include <QSettings>
@ -95,8 +96,8 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
QList<QgsComposerTableColumn*>::const_iterator columnIt = mColumns.constBegin();
int col = 0;
double cellHeaderHeight = fontAscentMillimeters( mHeaderFont ) + 2 * mLineTextDistance;
double cellBodyHeight = fontAscentMillimeters( mContentFont ) + 2 * mLineTextDistance;
double cellHeaderHeight = QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mLineTextDistance;
double cellBodyHeight = QgsComposerUtils::fontAscentMM( mContentFont ) + 2 * mLineTextDistance;
QRectF cell;
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
@ -123,14 +124,12 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
break;
}
painter->setPen( mHeaderFontColor );
drawText( painter, cell, ( *columnIt )->heading(), mHeaderFont, headerAlign, Qt::AlignVCenter, Qt::TextDontClip );
QgsComposerUtils::drawText( painter, cell, ( *columnIt )->heading(), mHeaderFont, mHeaderFontColor, headerAlign, Qt::AlignVCenter, Qt::TextDontClip );
currentY += cellHeaderHeight;
currentY += mGridStrokeWidth;
//draw the attribute values
painter->setPen( mContentFontColor );
QList<QgsAttributeMap>::const_iterator attIt = mAttributeMaps.begin();
for ( ; attIt != mAttributeMaps.end(); ++attIt )
{
@ -138,7 +137,7 @@ void QgsComposerTable::paint( QPainter* painter, const QStyleOptionGraphicsItem*
const QgsAttributeMap &currentAttributeMap = *attIt;
QString str = currentAttributeMap[ col ].toString();
drawText( painter, cell, str, mContentFont, ( *columnIt )->hAlignment(), Qt::AlignVCenter, Qt::TextDontClip );
QgsComposerUtils::drawText( painter, cell, str, mContentFont, mContentFontColor, ( *columnIt )->hAlignment(), Qt::AlignVCenter, Qt::TextDontClip );
currentY += cellBodyHeight;
currentY += mGridStrokeWidth;
@ -352,7 +351,7 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
int col = 0;
for ( ; columnIt != mColumns.constEnd(); ++columnIt )
{
maxWidthMap.insert( col, textWidthMillimeters( mHeaderFont, ( *columnIt )->heading() ) );
maxWidthMap.insert( col, QgsComposerUtils::textWidthMM( mHeaderFont, ( *columnIt )->heading() ) );
col++;
}
@ -366,7 +365,7 @@ bool QgsComposerTable::calculateMaxColumnWidths( QMap<int, double>& maxWidthMap,
QgsAttributeMap::const_iterator attIt2 = attIt->constBegin();
for ( ; attIt2 != attIt->constEnd(); ++attIt2 )
{
currentAttributeTextWidth = textWidthMillimeters( mContentFont, attIt2.value().toString() );
currentAttributeTextWidth = QgsComposerUtils::textWidthMM( mContentFont, attIt2.value().toString() );
if ( currentAttributeTextWidth > maxWidthMap[ attIt2.key()] )
{
maxWidthMap[ attIt2.key()] = currentAttributeTextWidth;
@ -380,8 +379,8 @@ void QgsComposerTable::adaptItemFrame( const QMap<int, double>& maxWidthMap, con
{
//calculate height
int n = attributeMaps.size();
double totalHeight = fontAscentMillimeters( mHeaderFont )
+ n * fontAscentMillimeters( mContentFont )
double totalHeight = QgsComposerUtils::fontAscentMM( mHeaderFont )
+ n * QgsComposerUtils::fontAscentMM( mContentFont )
+ ( n + 1 ) * mLineTextDistance * 2
+ ( n + 2 ) * mGridStrokeWidth;
@ -408,12 +407,12 @@ void QgsComposerTable::drawHorizontalGridLines( QPainter* p, int nAttributes )
double currentY = halfGridStrokeWidth;
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
currentY += mGridStrokeWidth;
currentY += ( fontAscentMillimeters( mHeaderFont ) + 2 * mLineTextDistance );
currentY += ( QgsComposerUtils::fontAscentMM( mHeaderFont ) + 2 * mLineTextDistance );
for ( int i = 0; i < nAttributes; ++i )
{
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
currentY += mGridStrokeWidth;
currentY += ( fontAscentMillimeters( mContentFont ) + 2 * mLineTextDistance );
currentY += ( QgsComposerUtils::fontAscentMM( mContentFont ) + 2 * mLineTextDistance );
}
p->drawLine( QPointF( halfGridStrokeWidth, currentY ), QPointF( rect().width() - halfGridStrokeWidth, currentY ) );
}

View File

@ -371,3 +371,107 @@ void QgsComposerUtils::writeDataDefinedPropertyMap( QDomElement &itemElem, QDomD
}
}
}
QFont QgsComposerUtils::scaledFontPixelSize( const QFont &font )
{
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
QFont scaledFont = font;
double pixelSize = pointsToMM( scaledFont.pointSizeF() ) * FONT_WORKAROUND_SCALE + 0.5;
scaledFont.setPixelSize( pixelSize );
return scaledFont;
}
double QgsComposerUtils::fontAscentMM( const QFont &font )
{
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.ascent() / FONT_WORKAROUND_SCALE );
}
double QgsComposerUtils::fontDescentMM( const QFont &font )
{
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
}
double QgsComposerUtils::fontHeightMM( const QFont &font )
{
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
}
double QgsComposerUtils::fontHeightCharacterMM( const QFont &font, const QChar &character )
{
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.boundingRect( character ).height() / FONT_WORKAROUND_SCALE );
}
double QgsComposerUtils::textWidthMM( const QFont &font, const QString &text )
{
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
QFont metricsFont = scaledFontPixelSize( font );
QFontMetricsF fontMetrics( metricsFont );
return ( fontMetrics.width( text ) / FONT_WORKAROUND_SCALE );
}
void QgsComposerUtils::drawText( QPainter *painter, const QPointF &pos, const QString &text, const QFont &font, const QColor &color )
{
if ( !painter )
{
return;
}
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
QFont textFont = scaledFontPixelSize( font );
painter->save();
painter->setFont( textFont );
if ( color.isValid() )
{
painter->setPen( color );
}
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
painter->scale( scaleFactor, scaleFactor );
painter->drawText( pos * FONT_WORKAROUND_SCALE, text );
painter->restore();
}
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 )
{
if ( !painter )
{
return;
}
//upscale using FONT_WORKAROUND_SCALE
//ref: http://osgeo-org.1560.x6.nabble.com/Multi-line-labels-and-font-bug-td4157152.html
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 );
painter->save();
painter->setFont( textFont );
if ( color.isValid() )
{
painter->setPen( color );
}
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
painter->scale( scaleFactor, scaleFactor );
painter->drawText( scaledRect, halignment | valignment | flags, text );
painter->restore();
}

View File

@ -126,6 +126,99 @@ class CORE_EXPORT 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 );
};
#endif

3
src/core/composer/qgsdoubleboxscalebarstyle.cpp Normal file → Executable file
View File

@ -16,6 +16,7 @@
#include "qgsdoubleboxscalebarstyle.h"
#include "qgscomposerscalebar.h"
#include "qgscomposerutils.h"
#include <QList>
#include <QPainter>
@ -45,7 +46,7 @@ void QgsDoubleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
{
return;
}
double barTopPosition = mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double barTopPosition = QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double segmentHeight = mScaleBar->height() / 2;
p->save();

8
src/core/composer/qgsnumericscalebarstyle.cpp Normal file → Executable file
View File

@ -17,6 +17,7 @@
#include "qgsnumericscalebarstyle.h"
#include "qgscomposermap.h"
#include "qgscomposerscalebar.h"
#include "qgscomposerutils.h"
#include <QList>
#include <QPainter>
@ -52,7 +53,6 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
//antialiasing on
p->setRenderHint( QPainter::Antialiasing, true );
p->setFont( mScaleBar->font() );
p->setPen( mScaleBar->fontColor() );
//call QgsComposerItem's pen() function, since that refers to the frame pen
//and QgsComposerScalebar's pen() function refers to the scale bar line width,
@ -80,7 +80,7 @@ void QgsNumericScaleBarStyle::draw( QPainter* p, double xOffset ) const
//text destination is item's rect, excluding the margin and frame
QRectF painterRect( penWidth + margin, penWidth + margin, mScaleBar->rect().width() - 2 * penWidth - 2 * margin, mScaleBar->rect().height() - 2 * penWidth - 2 * margin );
mScaleBar->drawText( p, painterRect, scaleText(), mScaleBar->font(), hAlign, Qt::AlignTop );
QgsComposerUtils::drawText( p, painterRect, scaleText(), mScaleBar->font(), mScaleBar->fontColor(), hAlign, Qt::AlignTop );
p->restore();
}
@ -93,8 +93,8 @@ QRectF QgsNumericScaleBarStyle::calculateBoxSize() const
return rect;
}
double textWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), scaleText() );
double textHeight = mScaleBar->fontAscentMillimeters( mScaleBar->font() );
double textWidth = QgsComposerUtils::textWidthMM( mScaleBar->font(), scaleText() );
double textHeight = QgsComposerUtils::fontAscentMM( mScaleBar->font() );
rect = QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), 2 * mScaleBar->boxContentSpace()
+ 2 * mScaleBar->pen().width() + textWidth,

View File

@ -16,6 +16,7 @@
#include "qgsscalebarstyle.h"
#include "qgscomposerscalebar.h"
#include "qgscomposerutils.h"
#include <QFontMetricsF>
#include <QPainter>
@ -47,7 +48,7 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
p->setPen( QPen( mScaleBar->fontColor() ) );
QString firstLabel = mScaleBar->firstLabelString();
double xOffset = mScaleBar->textWidthMillimeters( mScaleBar->font(), firstLabel ) / 2;
double xOffset = QgsComposerUtils::textWidthMM( mScaleBar->font(), firstLabel ) / 2;
//double mCurrentXCoord = mScaleBar->pen().widthF() + mScaleBar->boxContentSpace();
QList<QPair<double, double> > segmentInfo;
@ -79,7 +80,8 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
if ( segmentCounter == 0 || segmentCounter >= nSegmentsLeft ) //don't draw label for intermediate left segments
{
mScaleBar->drawText( p, segmentIt->first - mScaleBar->textWidthMillimeters( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->boxContentSpace(), currentNumericLabel, mScaleBar->font() );
QgsComposerUtils::drawText( p, QPointF( segmentIt->first - QgsComposerUtils::textWidthMM( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->boxContentSpace() ),
currentNumericLabel, mScaleBar->font(), mScaleBar->fontColor() );
}
if ( segmentCounter >= nSegmentsLeft )
@ -93,7 +95,8 @@ void QgsScaleBarStyle::drawLabels( QPainter* p ) const
if ( !segmentInfo.isEmpty() )
{
currentNumericLabel = QString::number( currentLabelNumber / mScaleBar->numMapUnitsPerScaleBarUnit() );
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() );
QgsComposerUtils::drawText( p, QPointF( segmentInfo.last().first + mScaleBar->segmentMillimeters() - QgsComposerUtils::textWidthMM( mScaleBar->font(), currentNumericLabel ) / 2 + xOffset, QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->boxContentSpace() ),
currentNumericLabel + " " + mScaleBar->unitLabeling(), mScaleBar->font(), mScaleBar->fontColor() );
}
p->restore();
@ -107,14 +110,14 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
}
//consider centered first label
double firstLabelLeft = mScaleBar->textWidthMillimeters( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2;
double firstLabelLeft = QgsComposerUtils::textWidthMM( mScaleBar->font(), mScaleBar->firstLabelString() ) / 2;
//consider last number and label
double largestLabelNumber = mScaleBar->numSegments() * mScaleBar->numUnitsPerSegment() / mScaleBar->numMapUnitsPerScaleBarUnit();
QString largestNumberLabel = QString::number( largestLabelNumber );
QString largestLabel = QString::number( largestLabelNumber ) + " " + mScaleBar->unitLabeling();
double largestLabelWidth = mScaleBar->textWidthMillimeters( mScaleBar->font(), largestLabel ) - mScaleBar->textWidthMillimeters( mScaleBar->font(), largestNumberLabel ) / 2;
double largestLabelWidth = QgsComposerUtils::textWidthMM( mScaleBar->font(), largestLabel ) - QgsComposerUtils::textWidthMM( mScaleBar->font(), largestNumberLabel ) / 2;
double totalBarLength = 0.0;
@ -128,7 +131,7 @@ QRectF QgsScaleBarStyle::calculateBoxSize() const
}
double width = firstLabelLeft + totalBarLength + 2 * mScaleBar->pen().widthF() + largestLabelWidth + 2 * mScaleBar->boxContentSpace();
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + mScaleBar->fontAscentMillimeters( mScaleBar->font() );
double height = mScaleBar->height() + mScaleBar->labelBarSpace() + 2 * mScaleBar->boxContentSpace() + QgsComposerUtils::fontAscentMM( mScaleBar->font() );
return QRectF( mScaleBar->pos().x(), mScaleBar->pos().y(), width, height );
}

3
src/core/composer/qgssingleboxscalebarstyle.cpp Normal file → Executable file
View File

@ -16,6 +16,7 @@
#include "qgssingleboxscalebarstyle.h"
#include "qgscomposerscalebar.h"
#include "qgscomposerutils.h"
#include <QList>
#include <QPainter>
@ -40,7 +41,7 @@ void QgsSingleBoxScaleBarStyle::draw( QPainter* p, double xOffset ) const
{
return;
}
double barTopPosition = mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double barTopPosition = QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
p->save();
//antialiasing on

3
src/core/composer/qgsticksscalebarstyle.cpp Normal file → Executable file
View File

@ -16,6 +16,7 @@
#include "qgsticksscalebarstyle.h"
#include "qgscomposerscalebar.h"
#include "qgscomposerutils.h"
#include <QPainter>
QgsTicksScaleBarStyle::QgsTicksScaleBarStyle( const QgsComposerScaleBar* bar ): QgsScaleBarStyle( bar )
@ -53,7 +54,7 @@ void QgsTicksScaleBarStyle::draw( QPainter* p, double xOffset ) const
{
return;
}
double barTopPosition = mScaleBar->fontAscentMillimeters( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double barTopPosition = QgsComposerUtils::fontAscentMM( mScaleBar->font() ) + mScaleBar->labelBarSpace() + mScaleBar->boxContentSpace();
double middlePosition = barTopPosition + mScaleBar->height() / 2.0;
double bottomPosition = barTopPosition + mScaleBar->height();

View File

@ -71,7 +71,7 @@ void QgsResidualPlotItem::paint( QPainter* painter, const QStyleOptionGraphicsIt
painter->setBrush( disabledBrush );
}
painter->drawRect( QRectF( gcpItemMMX - 0.5, gcpItemMMY - 0.5, 1, 1 ) );
drawText( painter, gcpItemMMX + 2, gcpItemMMY + 2, QString::number(( *gcpIt )->id() ), QFont() );
QgsComposerUtils::drawText( painter, QPointF( gcpItemMMX + 2, gcpItemMMY + 2 ), QString::number(( *gcpIt )->id() ), QFont() );
mmPixelRatio = maxMMToPixelRatioForGCP( *gcpIt, gcpItemMMX, gcpItemMMY );
if ( mmPixelRatio < minMMPixelRatio )
@ -135,11 +135,11 @@ void QgsResidualPlotItem::paint( QPainter* painter, const QStyleOptionGraphicsIt
scaleBarFont.setPointSize( 9 );
if ( mConvertScaleToMapUnits )
{
drawText( painter, 5, rect().height() - 4 + fontAscentMillimeters( scaleBarFont ), QString( "%1 map units" ).arg( scaleBarWidthUnits ), QFont() );
QgsComposerUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsComposerUtils::fontAscentMM( scaleBarFont ) ), QString( "%1 map units" ).arg( scaleBarWidthUnits ), QFont() );
}
else
{
drawText( painter, 5, rect().height() - 4 + fontAscentMillimeters( scaleBarFont ), QString( "%1 pixels" ).arg( scaleBarWidthUnits ), QFont() );
QgsComposerUtils::drawText( painter, QPointF( 5, rect().height() - 4 + QgsComposerUtils::fontAscentMM( scaleBarFont ) ), QString( "%1 pixels" ).arg( scaleBarWidthUnits ), QFont() );
}
drawFrame( painter );

View File

@ -15,10 +15,12 @@
* *
***************************************************************************/
#include "qgsapplication.h" //for standard test font
#include "qgscomposerutils.h"
#include "qgscomposition.h"
#include "qgscompositionchecker.h"
#include "qgsdatadefined.h"
#include "qgsfontutils.h"
#include <QObject>
#include <QtTest>
#include <QMap>
@ -44,21 +46,38 @@ class TestQgsComposerUtils: public QObject
void readDataDefinedProperty(); //test reading a data defined property
void readDataDefinedPropertyMap(); //test reading a whole data defined property map
void writeDataDefinedPropertyMap(); //test reading a whole data defined property map
void scaledFontPixelSize(); //test creating a scaled font
void fontAscentMM(); //test calculating font ascent in mm
void fontDescentMM(); //test calculating font descent in mm
void fontHeightMM(); //test calculating font height in mm
void fontHeightCharacterMM(); //test calculating font character height in mm
void textWidthMM(); //test calculting text width in mm
void drawTextPos(); //test drawing text at a pos
void drawTextRect(); //test drawing text in a rect
private:
bool renderCheck( QString testName, QImage &image, int mismatchCount = 0 );
QgsComposition* mComposition;
QgsMapSettings mMapSettings;
QString mReport;
QFont mTestFont;
};
void TestQgsComposerUtils::initTestCase()
{
QgsApplication::init();
QgsApplication::initQgis(); //for access to test font
mComposition = new QgsComposition( mMapSettings );
mComposition->setPaperSize( 297, 210 ); //A4 landscape
mReport = "<h1>Composer Utils Tests</h1>\n";
QgsFontUtils::loadStandardTestFonts( QStringList() << "Oblique" );
mTestFont = QgsFontUtils::getStandardTestFont( "Oblique " );
mTestFont.setItalic( true );
}
void TestQgsComposerUtils::cleanupTestCase()
@ -462,6 +481,117 @@ void TestQgsComposerUtils::writeDataDefinedPropertyMap()
QCOMPARE( dd3Elem.attribute( "field", "bad" ), QString( "field 3" ) );
}
void TestQgsComposerUtils::scaledFontPixelSize()
{
//create a 12 point test font
mTestFont.setPointSize( 12 );
//test scaling of font for painting
QFont scaledFont = QgsComposerUtils::scaledFontPixelSize( mTestFont );
QCOMPARE( scaledFont.pixelSize(), 42 );
QCOMPARE( scaledFont.family(), mTestFont.family() );
}
void TestQgsComposerUtils::fontAscentMM()
{
mTestFont.setPointSize( 12 );
//platform specific font rendering differences mean these tests need to be very leniant
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontAscentMM( mTestFont ), 3.9, 0.5 ) );
}
void TestQgsComposerUtils::fontDescentMM()
{
mTestFont.setPointSize( 12 );
QCOMPARE( QgsComposerUtils::fontDescentMM( mTestFont ), 0.9 );
}
void TestQgsComposerUtils::fontHeightMM()
{
mTestFont.setPointSize( 12 );
//platform specific font rendering differences mean these tests need to be very leniant
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightMM( mTestFont ), 4.9, 0.5 ) );
}
void TestQgsComposerUtils::fontHeightCharacterMM()
{
mTestFont.setPointSize( 12 );
//platform specific font rendering differences mean these tests need to be very leniant
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightCharacterMM( mTestFont, QChar( 'a' ) ), 2.4, 0.15 ) );
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightCharacterMM( mTestFont, QChar( 'l' ) ), 3.15, 0.16 ) );
QVERIFY( qgsDoubleNear( QgsComposerUtils::fontHeightCharacterMM( mTestFont, QChar( 'g' ) ), 3.2, 0.11 ) );
}
void TestQgsComposerUtils::textWidthMM()
{
//platform specific font rendering differences mean this test needs to be very leniant
mTestFont.setPointSize( 12 );
QVERIFY( qgsDoubleNear( QgsComposerUtils::textWidthMM( mTestFont, QString( "test string" ) ), 20, 2 ) );
}
void TestQgsComposerUtils::drawTextPos()
{
//test drawing with no painter
QgsComposerUtils::drawText( 0, QPointF( 5, 15 ), QString( "Abc123" ), mTestFont );
//test drawing text on to image
mTestFont.setPointSize( 48 );
QImage testImage = QImage( 250, 250, QImage::Format_RGB32 );
testImage.fill( qRgb( 152, 219, 249 ) );
QPainter testPainter;
testPainter.begin( &testImage );
QgsComposerUtils::drawText( &testPainter, QPointF( 5, 15 ), QString( "Abc123" ), mTestFont, Qt::white );
testPainter.end();
QVERIFY( renderCheck( "composerutils_drawtext_pos", testImage, 100 ) );
//test drawing with pen color set on painter and no specified color
//text should be drawn using painter pen color
testImage.fill( qRgb( 152, 219, 249 ) );
testPainter.begin( &testImage );
testPainter.setPen( QPen( Qt::green ) );
QgsComposerUtils::drawText( &testPainter, QPointF( 5, 15 ), QString( "Abc123" ), mTestFont );
testPainter.end();
QVERIFY( renderCheck( "composerutils_drawtext_posnocolor", testImage, 100 ) );
}
void TestQgsComposerUtils::drawTextRect()
{
//test drawing with no painter
QgsComposerUtils::drawText( 0, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont );
//test drawing text on to image
mTestFont.setPointSize( 48 );
QImage testImage = QImage( 250, 250, QImage::Format_RGB32 );
testImage.fill( qRgb( 152, 219, 249 ) );
QPainter testPainter;
testPainter.begin( &testImage );
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont, Qt::white );
testPainter.end();
QVERIFY( renderCheck( "composerutils_drawtext_rect", testImage, 100 ) );
//test drawing with pen color set on painter and no specified color
//text should be drawn using painter pen color
testImage.fill( qRgb( 152, 219, 249 ) );
testPainter.begin( &testImage );
testPainter.setPen( QPen( Qt::green ) );
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont );
testPainter.end();
QVERIFY( renderCheck( "composerutils_drawtext_rectnocolor", testImage, 100 ) );
//test alignment settings
testImage.fill( qRgb( 152, 219, 249 ) );
testPainter.begin( &testImage );
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 200, 50 ), QString( "Abc123" ), mTestFont, Qt::black, Qt::AlignRight, Qt::AlignBottom );
testPainter.end();
QVERIFY( renderCheck( "composerutils_drawtext_rectalign", testImage, 100 ) );
//test extra flags - render without clipping
testImage.fill( qRgb( 152, 219, 249 ) );
testPainter.begin( &testImage );
QgsComposerUtils::drawText( &testPainter, QRectF( 5, 15, 20, 50 ), QString( "Abc123" ), mTestFont, Qt::white, Qt::AlignLeft, Qt::AlignTop, Qt::TextDontClip );
testPainter.end();
QVERIFY( renderCheck( "composerutils_drawtext_rectflag", testImage, 100 ) );
}
bool TestQgsComposerUtils::renderCheck( QString testName, QImage &image, int mismatchCount )
{
mReport += "<h2>" + testName + "</h2>\n";

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.5 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.1 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.0 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 2.4 KiB