Skip to content

Commit 8c0dddb

Browse files
author
mhugent
committed
Use qt font bug workaround for composer label. Other items will follow soon. All the up-and downscaling is done in a central place in composer item base class.
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@9260 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 9e39589 commit 8c0dddb

File tree

3 files changed

+85
-31
lines changed

3 files changed

+85
-31
lines changed

src/core/composer/qgscomposeritem.cpp

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
#include "qgsrect.h" //just for debugging
2828
#include "qgslogger.h"
2929

30+
#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter
31+
3032
QgsComposerItem::QgsComposerItem( QgsComposition* composition ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
3133
{
3234
setFlag( QGraphicsItem::ItemIsSelectable, true );
@@ -520,3 +522,57 @@ void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
520522
setCursor( cursorForPosition( event->pos() ) );
521523
}
522524
}
525+
526+
void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font)
527+
{
528+
QFont textFont = scaledFontPixelSize(font);
529+
530+
p->save();
531+
p->setFont(textFont);
532+
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
533+
p->scale(scaleFactor, scaleFactor);
534+
p->drawText(x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, text);
535+
p->restore();
536+
}
537+
538+
void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font)
539+
{
540+
QFont textFont = scaledFontPixelSize(font);
541+
542+
QRectF scaledRect(rect.x() * FONT_WORKAROUND_SCALE, rect.y() * FONT_WORKAROUND_SCALE,
543+
rect.width() * FONT_WORKAROUND_SCALE, rect.height() * FONT_WORKAROUND_SCALE);
544+
545+
p->save();
546+
p->setFont(textFont);
547+
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
548+
p->scale(scaleFactor, scaleFactor);
549+
p->drawText(scaledRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text);
550+
p->restore();
551+
}
552+
553+
double QgsComposerItem::textWidthMM(const QFont& font, const QString& text) const
554+
{
555+
QFont metricsFont = scaledFontPixelSize(font);
556+
QFontMetrics fontMetrics(metricsFont);
557+
return (fontMetrics.width(text) / FONT_WORKAROUND_SCALE);
558+
}
559+
560+
double QgsComposerItem::fontAscentMM(const QFont& font) const
561+
{
562+
QFont metricsFont = scaledFontPixelSize(font);
563+
QFontMetrics fontMetrics(metricsFont);
564+
return (fontMetrics.ascent() / FONT_WORKAROUND_SCALE);
565+
}
566+
567+
double QgsComposerItem::pixelFontSize(double pointSize) const
568+
{
569+
return (pointSize * 0.3527);
570+
}
571+
572+
QFont QgsComposerItem::scaledFontPixelSize(const QFont& font) const
573+
{
574+
QFont scaledFont = font;
575+
double pixelSize = pixelFontSize(font.pointSizeF()) * FONT_WORKAROUND_SCALE + 0.5;
576+
scaledFont.setPixelSize(pixelSize);
577+
return scaledFont;
578+
}

src/core/composer/qgscomposeritem.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,25 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem
151151

152152
/**Draw background*/
153153
virtual void drawBackground( QPainter* p );
154+
155+
/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
156+
to work arount the Qt font bug)*/
157+
void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font);
158+
159+
/**Like the above, but with a rectangle for multiline text*/
160+
void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font);
161+
162+
/**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
163+
double textWidthMM(const QFont& font, const QString& text) const;
164+
165+
/**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
166+
double fontAscentMM(const QFont& font) const;
167+
168+
/**Calculates font to from point size to pixel size*/
169+
double pixelFontSize(double pointSize) const;
170+
171+
/**Returns a font where size is in pixel and font size is upscaled with FONT_WORKAROUND_SCALE*/
172+
QFont scaledFontPixelSize(const QFont& font) const;
154173
};
155174

156175
#endif

src/core/composer/qgscomposerlabel.cpp

Lines changed: 10 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,10 @@
1919
#include <QDomElement>
2020
#include <QPainter>
2121

22-
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 0.0 )
22+
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 )
2323
{
2424
//default font size is 10 point
25-
if ( mComposition )
26-
{
27-
mFont.setPixelSize( mComposition->pixelFontSize( 10 ) );
28-
}
29-
else
30-
{
31-
mFont.setPixelSize( 4 );
32-
}
25+
mFont.setPointSizeF(10);
3326
}
3427

3528
QgsComposerLabel::~QgsComposerLabel()
@@ -53,8 +46,8 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
5346
double penWidth = pen().widthF();
5447
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
5548
rect().height() - 2 * penWidth - 2 * mMargin);
56-
painter->drawText( painterRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, mText );
57-
49+
//painter->drawText( painterRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, mText );
50+
drawText(painter, painterRect, mText, mFont);
5851

5952
drawFrame( painter );
6053
if ( isSelected() )
@@ -70,34 +63,20 @@ void QgsComposerLabel::setText( const QString& text )
7063

7164
void QgsComposerLabel::setFont( const QFont& f )
7265
{
73-
//set font size in pixels for proper preview and printout
74-
if ( mComposition )
75-
{
76-
int pixelSize = mComposition->pixelFontSize( f.pointSizeF() );
77-
mFont = f;
78-
mFont.setPixelSize( pixelSize );
79-
}
80-
else
81-
{
82-
mFont = f;
83-
}
66+
mFont = f;
8467
}
8568

8669
void QgsComposerLabel::adjustSizeToText()
8770
{
88-
QFontMetricsF fontInfo( mFont );
89-
setSceneRect( QRectF( transform().dx(), transform().dy(), fontInfo.width( mText ) + 2 * mMargin + 2 * pen().widthF(), fontInfo.ascent() + 2 * mMargin + 2 * pen().widthF() ) );
71+
double textWidth = textWidthMM(mFont, mText);
72+
double fontAscent = fontAscentMM(mFont);
73+
74+
setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \
75+
fontAscent + 2 * mMargin + 2 * pen().widthF() + 1) );
9076
}
9177

9278
QFont QgsComposerLabel::font() const
9379
{
94-
if ( mComposition ) //make pixel to point conversion to show correct point value in dialogs
95-
{
96-
double pointSize = mComposition->pointFontSize( mFont.pixelSize() );
97-
QFont returnFont = mFont;
98-
returnFont.setPointSize( pointSize );
99-
return returnFont;
100-
}
10180
return mFont;
10281
}
10382

0 commit comments

Comments
 (0)