Skip to content

Commit 456adad

Browse files
manisandronyalldawson
authored andcommitted
Consider also font descent and baseline in QgsComposerLabel::adjustSizeToText. Funded by Sourcepole QGIS Enterprise.
1 parent 912c950 commit 456adad

File tree

4 files changed

+23
-4
lines changed

4 files changed

+23
-4
lines changed

python/core/composer/qgscomposeritem.sip

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -405,8 +405,14 @@ class QgsComposerItem : QObject, QGraphicsRectItem
405405
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
406406
double fontAscentMillimeters( const QFont& font ) const;
407407

408-
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
408+
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
409409
double fontDescentMillimeters( const QFont& font ) const;
410+
411+
/**Returns the font height in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.
412+
* Font height equals the font ascent+descent+1 (for baseline).
413+
* @note Added in version 2.4
414+
*/
415+
double fontHeightMillimeters( const QFont& font ) const;
410416

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

src/core/composer/qgscomposeritem.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -749,6 +749,13 @@ double QgsComposerItem::fontDescentMillimeters( const QFont& font ) const
749749
return ( fontMetrics.descent() / FONT_WORKAROUND_SCALE );
750750
}
751751

752+
double QgsComposerItem::fontHeightMillimeters( const QFont& font ) const
753+
{
754+
QFont metricsFont = scaledFontPixelSize( font );
755+
QFontMetricsF fontMetrics( metricsFont );
756+
return ( fontMetrics.height() / FONT_WORKAROUND_SCALE );
757+
}
758+
752759
double QgsComposerItem::pixelFontSize( double pointSize ) const
753760
{
754761
return ( pointSize * 0.3527 );

src/core/composer/qgscomposeritem.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -360,9 +360,15 @@ class CORE_EXPORT QgsComposerItem: public QObject, public QGraphicsRectItem
360360
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
361361
double fontAscentMillimeters( const QFont& font ) const;
362362

363-
/**Returns the font ascent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
363+
/**Returns the font descent in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
364364
double fontDescentMillimeters( const QFont& font ) const;
365365

366+
/**Returns the font height in Millimeters (considers upscaling and downscaling with FONT_WORKAROUND_SCALE.
367+
* Font height equals the font ascent+descent+1 (for baseline).
368+
* @note Added in version 2.4
369+
*/
370+
double fontHeightMillimeters( const QFont& font ) const;
371+
366372
/**Calculates font to from point size to pixel size*/
367373
double pixelFontSize( double pointSize ) const;
368374

src/core/composer/qgscomposerlabel.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,10 @@ void QgsComposerLabel::setFont( const QFont& f )
254254
void QgsComposerLabel::adjustSizeToText()
255255
{
256256
double textWidth = textWidthMillimeters( mFont, displayText() );
257-
double fontAscent = fontAscentMillimeters( mFont );
257+
double fontHeight = fontHeightMillimeters( mFont );
258258

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

262262
//keep alignment point constant
263263
double xShift = 0;

0 commit comments

Comments
 (0)