Skip to content

Commit

Permalink
Consider also font descent and baseline in QgsComposerLabel::adjustSi…
Browse files Browse the repository at this point in the history
…zeToText. Funded by Sourcepole QGIS Enterprise.
  • Loading branch information
manisandro authored and nyalldawson committed Jun 7, 2014
1 parent 912c950 commit 456adad
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 4 deletions.
8 changes: 7 additions & 1 deletion python/core/composer/qgscomposeritem.sip
Expand Up @@ -405,8 +405,14 @@ class QgsComposerItem : QObject, QGraphicsRectItem
/**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*/
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
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
*/
double fontHeightMillimeters( const QFont& font ) const;

/**Calculates font to from point size to pixel size*/
double pixelFontSize( double pointSize ) const;
Expand Down
7 changes: 7 additions & 0 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -749,6 +749,13 @@ double QgsComposerItem::fontDescentMillimeters( const QFont& font ) const
return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
}

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

double QgsComposerItem::pixelFontSize( double pointSize ) const
{
return ( pointSize * 0.3527 );
Expand Down
8 changes: 7 additions & 1 deletion src/core/composer/qgscomposeritem.h
Expand Up @@ -360,9 +360,15 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
/**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*/
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
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
*/
double fontHeightMillimeters( const QFont& font ) const;

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

Expand Down
4 changes: 2 additions & 2 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -254,10 +254,10 @@ void QgsComposerLabel::setFont( const QFont& f )
void QgsComposerLabel::adjustSizeToText()
{
double textWidth = textWidthMillimeters( mFont, displayText() );
double fontAscent = fontAscentMillimeters( mFont );
double fontHeight = fontHeightMillimeters( mFont );

double width = textWidth + 2 * mMargin + 2 * pen().widthF() + 1;
double height = fontAscent + 2 * mMargin + 2 * pen().widthF() + 1;
double height = fontHeight + 2 * mMargin + 2 * pen().widthF();

//keep alignment point constant
double xShift = 0;
Expand Down

0 comments on commit 456adad

Please sign in to comment.