Skip to content

Commit b83234b

Browse files
author
rblazek
committed
fixed font scaling in PS
git-svn-id: http://svn.osgeo.org/qgis/trunk@5467 c8812cc2-4d05-0410-92ff-de0c093fc19c
1 parent 82f6586 commit b83234b

7 files changed

+58
-99
lines changed

src/composer/qgscomposer.cpp

+12-4
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include <QPainter>
3131
#include <Q3Picture>
3232
#include <QPrinter>
33+
#include <QPrintDialog>
3334
#include <QSettings>
3435
#include <QIcon>
3536
#include <QPixmap>
@@ -280,7 +281,10 @@ void QgsComposer::on_mActionPrint_activated(void)
280281

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

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

307311
mComposition->setPlotStyle ( QgsComposition::Postscript );
308312

309-
if ( mPrinter->outputToFile() ) {
313+
if ( !mPrinter->outputFileName().isNull() ) {
310314
try {
311315
std::cout << "Print to file" << std::endl;
312316

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

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

331337
p.end();
332338

333-
#ifdef Q_WS_X11
339+
// Hopefully these hacks will not be necessary with Qt4
340+
//#ifdef Q_WS_X11
341+
#ifdef XXX
334342
// reset the page
335343
mPrinter->setPageSize ( psize );
336344

src/composer/qgscomposerlabel.cpp

+8-28
Original file line numberDiff line numberDiff line change
@@ -125,40 +125,20 @@ void QgsComposerLabel::draw ( QPainter & painter )
125125
painter.setBrush ( brush );
126126
painter.drawRect ( boxRect );
127127
}
128+
painter.setPen ( mPen );
128129

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

133-
if ( plotStyle() == QgsComposition::Postscript ) {
134-
// TODO: For output to Postscript the font must be scaled. But how?
135-
// The factor is an empirical value.
136-
// In any case, each font scales in in different way even if painter.scale()
137-
// is used instead of font size!!! -> Postscript is never exactly the same as
138-
// in preview.
139-
double factor = QgsComposition::psFontScaleFactor();
140-
141-
double pssize = factor * 72.0 * mFont.pointSizeFloat() / mComposition->resolution();
142-
double psscale = pssize/size;
143-
144-
painter.save();
145-
//painter.translate(x-w/2,(int)(y+metrics.height()/2-metrics.descent()));
146-
painter.translate(x,y);
147-
148-
painter.scale ( psscale, psscale );
149-
150-
/// rect can be too small in PS -> add buf
151-
int buf = metrics.width ( "x" );
152-
QRect psr ( (int)( -1.*(w+2*buf)/2/psscale), (int) (-1.*h/2/psscale),(int)(1.*(w+2*buf)/psscale), (int)(1.*h/psscale) );
153-
154-
//painter.drawText ( 0, 0, mText );
155-
painter.drawText ( psr, Qt::AlignCenter|Qt::TextSingleLine , mText );
156-
157-
painter.restore();
158-
} else {
159-
//painter.drawText ( tr, Qt::AlignCenter|Qt::SingleLine , mText );
160-
painter.drawText ( x-w/2,(int)(y+metrics.height()/2-metrics.descent()), mText );
134+
if ( plotStyle() == QgsComposition::Postscript )
135+
{
136+
// This metrics.ascent() is empirical
137+
size = metrics.ascent() * 72.0 / mComposition->resolution();
138+
font.setPointSizeF ( size );
139+
painter.setFont ( font );
161140
}
141+
painter.drawText ( x-w/2,(int)(y+metrics.height()/2-metrics.descent()), mText );
162142

163143
// Show selected / Highlight
164144
if ( mSelected && plotStyle() == QgsComposition::Preview ) {

src/composer/qgscomposermap.cpp

+10-2
Original file line numberDiff line numberDiff line change
@@ -169,8 +169,16 @@ void QgsComposerMap::draw ( QPainter *painter, QgsRect *extent, QgsMapToPixel *t
169169

170170
if ( vector->labelOn() ) {
171171
double fontScale = 25.4 * mFontScale * mComposition->scale() / 72;
172-
if ( plotStyle() == QgsComposition::Postscript ) {
173-
fontScale = QgsComposition::psFontScaleFactor() * 72.0 / mComposition->resolution();
172+
if ( plotStyle() == QgsComposition::Postscript )
173+
{
174+
//fontScale = QgsComposition::psFontScaleFactor() * 72.0 / mComposition->resolution();
175+
176+
// TODO
177+
// This is not completely correct because fonts written to postscript
178+
// should use size metrics.ascent() * 72.0 / mComposition->resolution();
179+
// We could add a factor for metrics.ascent()/size but it is variable
180+
// Add a parrameter in drawLables() ?
181+
fontScale = 72.0 / mComposition->resolution();
174182
}
175183
vector->drawLabels ( painter, extent, transform, fontScale );
176184
}

src/composer/qgscomposerscalebar.cpp

+5-18
Original file line numberDiff line numberDiff line change
@@ -180,15 +180,10 @@ QRect QgsComposerScalebar::render ( QPainter *p )
180180
font.setPointSizeFloat ( size );
181181
QFontMetrics metrics ( font );
182182

183-
// TODO: For output to Postscript the font must be scaled. But how?
184-
// The factor is an empirical value.
185-
// In any case, each font scales in in different way even if painter.scale()
186-
// is used instead of font size!!! -> Postscript is never exactly the same as
187-
// in preview.
188-
double factor = QgsComposition::psFontScaleFactor();
189-
190-
double pssize = factor * 72.0 * mFont.pointSizeFloat() / mComposition->resolution();
191-
double psscale = pssize/size;
183+
if ( plotStyle() == QgsComposition::Postscript )
184+
{
185+
font.setPointSizeF ( metrics.ascent() * 72.0 / mComposition->resolution() );
186+
}
192187

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

259-
if ( plotStyle() == QgsComposition::Postscript ) {
260-
painter->save();
261-
painter->translate(x,y);
262-
painter->scale ( psscale, psscale );
263-
painter->drawText( 0, 0, txt );
264-
painter->restore();
265-
} else {
266-
painter->drawText( x, y, txt );
267-
}
254+
painter->drawText( x, y, txt );
268255
}
269256

270257
ymin = cy - ticksize - offset - h;

src/composer/qgscomposervectorlegend.cpp

+18-46
Original file line numberDiff line numberDiff line change
@@ -178,25 +178,22 @@ QRect QgsComposerVectorLegend::render ( QPainter *p )
178178
QFontMetrics metrics ( font );
179179

180180
// Fonts for rendering
181+
double psTitleSize = titleMetrics.ascent() * 72.0 / mComposition->resolution();
182+
double psSectionSize = sectionMetrics.ascent() * 72.0 / mComposition->resolution();
183+
double psSize = metrics.ascent() * 72.0 / mComposition->resolution();
181184

182-
// TODO: For output to Postscript the font must be scaled. But how?
183-
// The factor is an empirical value.
184-
// In any case, each font scales in in different way even if painter.scale()
185-
// is used instead of font size!!! -> Postscript is never exactly the same as
186-
// in preview.
187-
double psFontFactor = QgsComposition::psFontScaleFactor();
188-
189-
double psTitleSize = psFontFactor * 72.0 * mTitleFont.pointSizeFloat() / mComposition->resolution();
190-
double psSectionSize = psFontFactor * 72.0 * mSectionFont.pointSizeFloat() / mComposition->resolution();
191-
double psSize = psFontFactor * 72.0 * mFont.pointSizeFloat() / mComposition->resolution();
192-
193-
double psTitleFontScale = psTitleSize / titleSize;
194-
double psSectionFontScale = psSectionSize / sectionSize;
195-
double psFontScale = psSize / size;
196-
197-
titleFont.setPointSizeFloat ( titleSize );
198-
sectionFont.setPointSizeFloat ( sectionSize );
199-
font.setPointSizeFloat ( size );
185+
if ( plotStyle() == QgsComposition::Postscript)
186+
{
187+
titleFont.setPointSizeFloat ( psTitleSize );
188+
sectionFont.setPointSizeFloat ( psSectionSize );
189+
font.setPointSizeFloat ( psSize );
190+
}
191+
else
192+
{
193+
titleFont.setPointSizeFloat ( titleSize );
194+
sectionFont.setPointSizeFloat ( sectionSize );
195+
font.setPointSizeFloat ( size );
196+
}
200197

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

213-
if ( plotStyle() == QgsComposition::Postscript) {
214-
painter->save();
215-
painter->translate ( (int) (2*mMargin), y );
216-
painter->scale ( psTitleFontScale, psTitleFontScale );
217-
painter->drawText( 0, 0, mTitle );
218-
painter->restore();
219-
} else {
220-
painter->drawText( (int) (2*mMargin), y, mTitle );
221-
}
210+
painter->drawText( (int) (2*mMargin), y, mTitle );
222211

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

240229
int group = layerGroup ( layerId );
241230
if ( group > 0 ) {
242-
//std::map<int,int>::iterator it= doneGroups.find();
243231
if ( doneGroups.find(group) != doneGroups.end() ) {
244232
continue;
245233
} else {
@@ -337,15 +325,7 @@ QRect QgsComposerVectorLegend::render ( QPainter *p )
337325
painter->setPen ( mPen );
338326
painter->setFont ( sectionFont );
339327

340-
if ( plotStyle() == QgsComposition::Postscript) {
341-
painter->save();
342-
painter->translate ( x, y );
343-
painter->scale ( psSectionFontScale, psSectionFontScale );
344-
painter->drawText( 0, 0, sectionTitle );
345-
painter->restore();
346-
} else {
347-
painter->drawText( x, y, sectionTitle );
348-
}
328+
painter->drawText( x, y,sectionTitle );
349329

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

421-
if ( plotStyle() == QgsComposition::Postscript) {
422-
painter->save();
423-
painter->translate ( x, y );
424-
painter->scale ( psFontScale, psFontScale );
425-
painter->drawText( 0, 0, lab );
426-
painter->restore();
427-
} else {
428-
painter->drawText( x, y, lab );
429-
}
401+
painter->drawText( x, y, lab );
430402

431403
int w = 3*mMargin + mSymbolWidth + metrics.width(lab);
432404
if ( w > width ) width = w;

src/composer/qgscomposition.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ QgsComposition::QgsComposition( QgsComposer *c, int id )
6969
// cannot be smoothly moved even if mPreviewMode == Rectangle and no zoom in
7070
// scale 2 results in minimum line width 0.5 mmm which is too much
7171
// scale 3 is compromise
72-
mScale = 5;
72+
mScale = 1;
7373

7474
// Add paper sizes and set default.
7575
mPapers.push_back ( QgsCompositionPaper( tr("Custom"), 0, 0, 1 ) );

src/composer/qgscomposition.h

+4
Original file line numberDiff line numberDiff line change
@@ -98,13 +98,17 @@ class QgsComposition: public QWidget, private Ui::QgsCompositionBase
9898
* in in different way even if painter.scale() is used instead of
9999
* font size!!! -> Postscript is never exactly the same as in preview.
100100
*/
101+
102+
// Hopefully no more needed
103+
/*
101104
static const double psFontScaleFactor() {
102105
#ifdef Q_OS_MACX
103106
return 1.85;
104107
#else
105108
return 2.45; // I have no idea why 2.45 - it is an empirical value
106109
#endif
107110
}
111+
*/
108112

109113
/** \brief Composition ID */
110114
int id ( void );

0 commit comments

Comments
 (0)