Skip to content

Commit

Permalink
fixed font scaling in PS
Browse files Browse the repository at this point in the history
git-svn-id: http://svn.osgeo.org/qgis/trunk/qgis@5467 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed May 18, 2006
1 parent c168d39 commit ec6183f
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 99 deletions.
16 changes: 12 additions & 4 deletions src/composer/qgscomposer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
#include <QPainter>
#include <Q3Picture>
#include <QPrinter>
#include <QPrintDialog>
#include <QSettings>
#include <QIcon>
#include <QPixmap>
Expand Down Expand Up @@ -280,7 +281,10 @@ void QgsComposer::on_mActionPrint_activated(void)

mPrinter->setResolution ( mComposition->resolution() );

if ( mPrinter->setup(this) ) {
//if ( mPrinter->setup(this) ) {
QPrintDialog printDialog ( mPrinter, this);
if ( printDialog.exec() == QDialog::Accepted )
{
// TODO: mPrinter->setup() moves the composer under Qgisapp, get it to foreground somehow
// raise() for now, is it something better?
raise ();
Expand All @@ -306,11 +310,13 @@ void QgsComposer::on_mActionPrint_activated(void)

mComposition->setPlotStyle ( QgsComposition::Postscript );

if ( mPrinter->outputToFile() ) {
if ( !mPrinter->outputFileName().isNull() ) {
try {
std::cout << "Print to file" << std::endl;

#ifdef Q_WS_X11
// Hopefully these hacks will not be necessary with Qt4
//#ifdef Q_WS_X11
#ifdef XXX
// NOTE: On UNIX setPageSize after setup() works, but setOrientation does not
// -> the BoundingBox must follow the orientation

Expand All @@ -330,7 +336,9 @@ void QgsComposer::on_mActionPrint_activated(void)

p.end();

#ifdef Q_WS_X11
// Hopefully these hacks will not be necessary with Qt4
//#ifdef Q_WS_X11
#ifdef XXX
// reset the page
mPrinter->setPageSize ( psize );

Expand Down
36 changes: 8 additions & 28 deletions src/composer/qgscomposerlabel.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,40 +125,20 @@ void QgsComposerLabel::draw ( QPainter & painter )
painter.setBrush ( brush );
painter.drawRect ( boxRect );
}
painter.setPen ( mPen );

// The width is not sufficient in postscript
QRect tr = r;
tr.setWidth ( r.width() );

if ( plotStyle() == QgsComposition::Postscript ) {
// TODO: For output to Postscript the font must be scaled. But how?
// The factor is an empirical value.
// In any case, each font scales in in different way even if painter.scale()
// is used instead of font size!!! -> Postscript is never exactly the same as
// in preview.
double factor = QgsComposition::psFontScaleFactor();

double pssize = factor * 72.0 * mFont.pointSizeFloat() / mComposition->resolution();
double psscale = pssize/size;

painter.save();
//painter.translate(x-w/2,(int)(y+metrics.height()/2-metrics.descent()));
painter.translate(x,y);

painter.scale ( psscale, psscale );

/// rect can be too small in PS -> add buf
int buf = metrics.width ( "x" );
QRect psr ( (int)( -1.*(w+2*buf)/2/psscale), (int) (-1.*h/2/psscale),(int)(1.*(w+2*buf)/psscale), (int)(1.*h/psscale) );

//painter.drawText ( 0, 0, mText );
painter.drawText ( psr, Qt::AlignCenter|Qt::TextSingleLine , mText );

painter.restore();
} else {
//painter.drawText ( tr, Qt::AlignCenter|Qt::SingleLine , mText );
painter.drawText ( x-w/2,(int)(y+metrics.height()/2-metrics.descent()), mText );
if ( plotStyle() == QgsComposition::Postscript )
{
// This metrics.ascent() is empirical
size = metrics.ascent() * 72.0 / mComposition->resolution();
font.setPointSizeF ( size );
painter.setFont ( font );
}
painter.drawText ( x-w/2,(int)(y+metrics.height()/2-metrics.descent()), mText );

// Show selected / Highlight
if ( mSelected && plotStyle() == QgsComposition::Preview ) {
Expand Down
12 changes: 10 additions & 2 deletions src/composer/qgscomposermap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -169,8 +169,16 @@ void QgsComposerMap::draw ( QPainter *painter, QgsRect *extent, QgsMapToPixel *t

if ( vector->labelOn() ) {
double fontScale = 25.4 * mFontScale * mComposition->scale() / 72;
if ( plotStyle() == QgsComposition::Postscript ) {
fontScale = QgsComposition::psFontScaleFactor() * 72.0 / mComposition->resolution();
if ( plotStyle() == QgsComposition::Postscript )
{
//fontScale = QgsComposition::psFontScaleFactor() * 72.0 / mComposition->resolution();

// TODO
// This is not completely correct because fonts written to postscript
// should use size metrics.ascent() * 72.0 / mComposition->resolution();
// We could add a factor for metrics.ascent()/size but it is variable
// Add a parrameter in drawLables() ?
fontScale = 72.0 / mComposition->resolution();
}
vector->drawLabels ( painter, extent, transform, fontScale );
}
Expand Down
23 changes: 5 additions & 18 deletions src/composer/qgscomposerscalebar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,15 +180,10 @@ QRect QgsComposerScalebar::render ( QPainter *p )
font.setPointSizeFloat ( size );
QFontMetrics metrics ( font );

// TODO: For output to Postscript the font must be scaled. But how?
// The factor is an empirical value.
// In any case, each font scales in in different way even if painter.scale()
// is used instead of font size!!! -> Postscript is never exactly the same as
// in preview.
double factor = QgsComposition::psFontScaleFactor();

double pssize = factor * 72.0 * mFont.pointSizeFloat() / mComposition->resolution();
double psscale = pssize/size;
if ( plotStyle() == QgsComposition::Postscript )
{
font.setPointSizeF ( metrics.ascent() * 72.0 / mComposition->resolution() );
}

// Not sure about Style Strategy, QFont::PreferMatch?
font.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias) );
Expand Down Expand Up @@ -256,15 +251,7 @@ QRect QgsComposerScalebar::render ( QPainter *p )
int x = barLx+i*segwidth-shift;
int y = cy-ticksize-offset-metrics.descent();

if ( plotStyle() == QgsComposition::Postscript ) {
painter->save();
painter->translate(x,y);
painter->scale ( psscale, psscale );
painter->drawText( 0, 0, txt );
painter->restore();
} else {
painter->drawText( x, y, txt );
}
painter->drawText( x, y, txt );
}

ymin = cy - ticksize - offset - h;
Expand Down
64 changes: 18 additions & 46 deletions src/composer/qgscomposervectorlegend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -178,25 +178,22 @@ QRect QgsComposerVectorLegend::render ( QPainter *p )
QFontMetrics metrics ( font );

// Fonts for rendering
double psTitleSize = titleMetrics.ascent() * 72.0 / mComposition->resolution();
double psSectionSize = sectionMetrics.ascent() * 72.0 / mComposition->resolution();
double psSize = metrics.ascent() * 72.0 / mComposition->resolution();

// TODO: For output to Postscript the font must be scaled. But how?
// The factor is an empirical value.
// In any case, each font scales in in different way even if painter.scale()
// is used instead of font size!!! -> Postscript is never exactly the same as
// in preview.
double psFontFactor = QgsComposition::psFontScaleFactor();

double psTitleSize = psFontFactor * 72.0 * mTitleFont.pointSizeFloat() / mComposition->resolution();
double psSectionSize = psFontFactor * 72.0 * mSectionFont.pointSizeFloat() / mComposition->resolution();
double psSize = psFontFactor * 72.0 * mFont.pointSizeFloat() / mComposition->resolution();

double psTitleFontScale = psTitleSize / titleSize;
double psSectionFontScale = psSectionSize / sectionSize;
double psFontScale = psSize / size;

titleFont.setPointSizeFloat ( titleSize );
sectionFont.setPointSizeFloat ( sectionSize );
font.setPointSizeFloat ( size );
if ( plotStyle() == QgsComposition::Postscript)
{
titleFont.setPointSizeFloat ( psTitleSize );
sectionFont.setPointSizeFloat ( psSectionSize );
font.setPointSizeFloat ( psSize );
}
else
{
titleFont.setPointSizeFloat ( titleSize );
sectionFont.setPointSizeFloat ( sectionSize );
font.setPointSizeFloat ( size );
}

// Not sure about Style Strategy, QFont::PreferMatch?
titleFont.setStyleStrategy ( (QFont::StyleStrategy) (QFont::PreferOutline | QFont::PreferAntialias) );
Expand All @@ -210,15 +207,7 @@ QRect QgsComposerVectorLegend::render ( QPainter *p )
painter->setPen ( mPen );
painter->setFont ( titleFont );

if ( plotStyle() == QgsComposition::Postscript) {
painter->save();
painter->translate ( (int) (2*mMargin), y );
painter->scale ( psTitleFontScale, psTitleFontScale );
painter->drawText( 0, 0, mTitle );
painter->restore();
} else {
painter->drawText( (int) (2*mMargin), y, mTitle );
}
painter->drawText( (int) (2*mMargin), y, mTitle );

int width = 4 * mMargin + titleMetrics.width ( mTitle );
int height = mMargin + mSymbolSpace + titleMetrics.height(); // mSymbolSpace?
Expand All @@ -239,7 +228,6 @@ QRect QgsComposerVectorLegend::render ( QPainter *p )

int group = layerGroup ( layerId );
if ( group > 0 ) {
//std::map<int,int>::iterator it= doneGroups.find();
if ( doneGroups.find(group) != doneGroups.end() ) {
continue;
} else {
Expand Down Expand Up @@ -337,15 +325,7 @@ QRect QgsComposerVectorLegend::render ( QPainter *p )
painter->setPen ( mPen );
painter->setFont ( sectionFont );

if ( plotStyle() == QgsComposition::Postscript) {
painter->save();
painter->translate ( x, y );
painter->scale ( psSectionFontScale, psSectionFontScale );
painter->drawText( 0, 0, sectionTitle );
painter->restore();
} else {
painter->drawText( x, y, sectionTitle );
}
painter->drawText( x, y,sectionTitle );

int w = 3*mMargin + sectionMetrics.width( sectionTitle );
if ( w > width ) width = w;
Expand Down Expand Up @@ -418,15 +398,7 @@ QRect QgsComposerVectorLegend::render ( QPainter *p )
x = (int) ( 2*mMargin + mSymbolWidth );
y = (int) ( localHeight + symbolHeight/2 + ( metrics.height()/2 - metrics.descent()) );

if ( plotStyle() == QgsComposition::Postscript) {
painter->save();
painter->translate ( x, y );
painter->scale ( psFontScale, psFontScale );
painter->drawText( 0, 0, lab );
painter->restore();
} else {
painter->drawText( x, y, lab );
}
painter->drawText( x, y, lab );

int w = 3*mMargin + mSymbolWidth + metrics.width(lab);
if ( w > width ) width = w;
Expand Down
2 changes: 1 addition & 1 deletion src/composer/qgscomposition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ QgsComposition::QgsComposition( QgsComposer *c, int id )
// cannot be smoothly moved even if mPreviewMode == Rectangle and no zoom in
// scale 2 results in minimum line width 0.5 mmm which is too much
// scale 3 is compromise
mScale = 5;
mScale = 1;

// Add paper sizes and set default.
mPapers.push_back ( QgsCompositionPaper( tr("Custom"), 0, 0, 1 ) );
Expand Down
4 changes: 4 additions & 0 deletions src/composer/qgscomposition.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,17 @@ class QgsComposition: public QWidget, private Ui::QgsCompositionBase
* in in different way even if painter.scale() is used instead of
* font size!!! -> Postscript is never exactly the same as in preview.
*/

// Hopefully no more needed
/*
static const double psFontScaleFactor() {
#ifdef Q_OS_MACX
return 1.85;
#else
return 2.45; // I have no idea why 2.45 - it is an empirical value
#endif
}
*/

/** \brief Composition ID */
int id ( void );
Expand Down

0 comments on commit ec6183f

Please sign in to comment.