Skip to content
Permalink
Browse files
fixed font scaling in PS
git-svn-id: http://svn.osgeo.org/qgis/trunk@5467 c8812cc2-4d05-0410-92ff-de0c093fc19c
  • Loading branch information
rblazek committed May 18, 2006
1 parent 82f6586 commit b83234b9c49dc8fce7e6edeb17a94a8314cb7158
@@ -30,6 +30,7 @@
#include <QPainter>
#include <Q3Picture>
#include <QPrinter>
#include <QPrintDialog>
#include <QSettings>
#include <QIcon>
#include <QPixmap>
@@ -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 ();
@@ -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

@@ -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 );

@@ -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 ) {
@@ -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 );
}
@@ -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) );
@@ -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;
@@ -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) );
@@ -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?
@@ -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 {
@@ -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;
@@ -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;
@@ -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 ) );
@@ -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 );

0 comments on commit b83234b

Please sign in to comment.