Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Use qt font bug workaround for composer label. Other items will follo…
…w 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
  • Loading branch information
mhugent committed Sep 5, 2008
1 parent 9e39589 commit 8c0dddb
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 31 deletions.
56 changes: 56 additions & 0 deletions src/core/composer/qgscomposeritem.cpp
Expand Up @@ -27,6 +27,8 @@
#include "qgsrect.h" //just for debugging
#include "qgslogger.h"

#define FONT_WORKAROUND_SCALE 10 //scale factor for upscaling fontsize and downscaling painter

QgsComposerItem::QgsComposerItem( QgsComposition* composition ): QGraphicsRectItem( 0 ), mComposition( composition ), mBoundingResizeRectangle( 0 ), mFrame( true )
{
setFlag( QGraphicsItem::ItemIsSelectable, true );
Expand Down Expand Up @@ -520,3 +522,57 @@ void QgsComposerItem::hoverMoveEvent( QGraphicsSceneHoverEvent * event )
setCursor( cursorForPosition( event->pos() ) );
}
}

void QgsComposerItem::drawText(QPainter* p, int x, int y, const QString& text, const QFont& font)
{
QFont textFont = scaledFontPixelSize(font);

p->save();
p->setFont(textFont);
double scaleFactor = 1.0 / FONT_WORKAROUND_SCALE;
p->scale(scaleFactor, scaleFactor);
p->drawText(x * FONT_WORKAROUND_SCALE, y * FONT_WORKAROUND_SCALE, text);
p->restore();
}

void QgsComposerItem::drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font)
{
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, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, text);
p->restore();
}

double QgsComposerItem::textWidthMM(const QFont& font, const QString& text) const
{
QFont metricsFont = scaledFontPixelSize(font);
QFontMetrics fontMetrics(metricsFont);
return (fontMetrics.width(text) / FONT_WORKAROUND_SCALE);
}

double QgsComposerItem::fontAscentMM(const QFont& font) const
{
QFont metricsFont = scaledFontPixelSize(font);
QFontMetrics fontMetrics(metricsFont);
return (fontMetrics.ascent() / FONT_WORKAROUND_SCALE);
}

double QgsComposerItem::pixelFontSize(double pointSize) const
{
return (pointSize * 0.3527);
}

QFont QgsComposerItem::scaledFontPixelSize(const QFont& font) const
{
QFont scaledFont = font;
double pixelSize = pixelFontSize(font.pointSizeF()) * FONT_WORKAROUND_SCALE + 0.5;
scaledFont.setPixelSize(pixelSize);
return scaledFont;
}
19 changes: 19 additions & 0 deletions src/core/composer/qgscomposeritem.h
Expand Up @@ -151,6 +151,25 @@ class CORE_EXPORT QgsComposerItem: public QGraphicsRectItem

/**Draw background*/
virtual void drawBackground( QPainter* p );

/**Draws Text. Takes care about all the composer specific issues (calculation to pixel, scaling of font and painter
to work arount the Qt font bug)*/
void drawText(QPainter* p, int x, int y, const QString& text, const QFont& font);

/**Like the above, but with a rectangle for multiline text*/
void drawText(QPainter* p, const QRectF& rect, const QString& text, const QFont& font);

/**Returns the font width in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double textWidthMM(const QFont& font, const QString& text) const;

/**Returns the font ascent in MM (considers upscaling and downscaling with FONT_WORKAROUND_SCALE*/
double fontAscentMM(const QFont& font) const;

/**Calculates font to from point size to pixel size*/
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;
};

#endif
41 changes: 10 additions & 31 deletions src/core/composer/qgscomposerlabel.cpp
Expand Up @@ -19,17 +19,10 @@
#include <QDomElement>
#include <QPainter>

QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 0.0 )
QgsComposerLabel::QgsComposerLabel( QgsComposition *composition ): QgsComposerItem( composition ), mMargin( 1.0 )
{
//default font size is 10 point
if ( mComposition )
{
mFont.setPixelSize( mComposition->pixelFontSize( 10 ) );
}
else
{
mFont.setPixelSize( 4 );
}
mFont.setPointSizeF(10);
}

QgsComposerLabel::~QgsComposerLabel()
Expand All @@ -53,8 +46,8 @@ void QgsComposerLabel::paint( QPainter* painter, const QStyleOptionGraphicsItem*
double penWidth = pen().widthF();
QRectF painterRect( penWidth + mMargin, penWidth + mMargin, rect().width() - 2 * penWidth - 2 * mMargin,
rect().height() - 2 * penWidth - 2 * mMargin);
painter->drawText( painterRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, mText );

//painter->drawText( painterRect, Qt::AlignLeft | Qt::AlignTop | Qt::TextWordWrap, mText );
drawText(painter, painterRect, mText, mFont);

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

void QgsComposerLabel::setFont( const QFont& f )
{
//set font size in pixels for proper preview and printout
if ( mComposition )
{
int pixelSize = mComposition->pixelFontSize( f.pointSizeF() );
mFont = f;
mFont.setPixelSize( pixelSize );
}
else
{
mFont = f;
}
mFont = f;
}

void QgsComposerLabel::adjustSizeToText()
{
QFontMetricsF fontInfo( mFont );
setSceneRect( QRectF( transform().dx(), transform().dy(), fontInfo.width( mText ) + 2 * mMargin + 2 * pen().widthF(), fontInfo.ascent() + 2 * mMargin + 2 * pen().widthF() ) );
double textWidth = textWidthMM(mFont, mText);
double fontAscent = fontAscentMM(mFont);

setSceneRect( QRectF( transform().dx(), transform().dy(), textWidth + 2 * mMargin + 2 * pen().widthF() + 1, \
fontAscent + 2 * mMargin + 2 * pen().widthF() + 1) );
}

QFont QgsComposerLabel::font() const
{
if ( mComposition ) //make pixel to point conversion to show correct point value in dialogs
{
double pointSize = mComposition->pointFontSize( mFont.pixelSize() );
QFont returnFont = mFont;
returnFont.setPointSize( pointSize );
return returnFont;
}
return mFont;
}

Expand Down

0 comments on commit 8c0dddb

Please sign in to comment.