src/gui/image/qpicture.cpp
changeset 33 3e2da88830cd
parent 30 5dc02b23752f
equal deleted inserted replaced
30:5dc02b23752f 33:3e2da88830cd
   438     }
   438     }
   439     d->pictb.close();
   439     d->pictb.close();
   440     return true;                                // no end-command
   440     return true;                                // no end-command
   441 }
   441 }
   442 
   442 
       
   443 
       
   444 //
       
   445 // QFakeDevice is used to create fonts with a custom DPI
       
   446 //
       
   447 class QFakeDevice : public QPaintDevice
       
   448 {
       
   449 public:
       
   450     QFakeDevice() { dpi_x = qt_defaultDpiX(); dpi_y = qt_defaultDpiY(); }
       
   451     void setDpiX(int dpi) { dpi_x = dpi; }
       
   452     void setDpiY(int dpi) { dpi_y = dpi; }
       
   453     QPaintEngine *paintEngine() const { return 0; }
       
   454     int metric(PaintDeviceMetric m) const
       
   455     {
       
   456         switch(m) {
       
   457             case PdmPhysicalDpiX:
       
   458             case PdmDpiX:
       
   459                 return dpi_x;
       
   460             case PdmPhysicalDpiY:
       
   461             case PdmDpiY:
       
   462                 return dpi_y;
       
   463             default:
       
   464                 return QPaintDevice::metric(m);
       
   465         }
       
   466     }
       
   467 
       
   468 private:
       
   469     int dpi_x;
       
   470     int dpi_y;
       
   471 };
       
   472 
   443 /*!
   473 /*!
   444   \internal
   474   \internal
   445   Iterates over the internal picture data and draws the picture using
   475   Iterates over the internal picture data and draws the picture using
   446   \a painter.
   476   \a painter.
   447 */
   477 */
   647             // aligned when QPicturePaintEngine::drawTextItem() serializes the
   677             // aligned when QPicturePaintEngine::drawTextItem() serializes the
   648             // drawText() call, therefore ul is unsed in this context
   678             // drawText() call, therefore ul is unsed in this context
   649 
   679 
   650             if (d->formatMajor >= 9) {
   680             if (d->formatMajor >= 9) {
   651                 s >> dbl;
   681                 s >> dbl;
   652                 QFont fnt(font, painter->device());
   682                 QFont fnt(font);
   653 
   683                 if (dbl != 1.0) {
   654                 // Fonts that specify a pixel size should not be scaled - QPicture already
   684                     QFakeDevice fake;
   655                 // have a matrix set to compensate for the DPI differences between the
   685                     fake.setDpiX(qRound(dbl*qt_defaultDpiX()));
   656                 // default Qt DPI and the actual target device DPI, and we have to take that
   686                     fake.setDpiY(qRound(dbl*qt_defaultDpiY()));
   657                 // into consideration in the case where the font has a pixel size set.
   687                     fnt = QFont(font, &fake);
   658 
   688                 }
   659                 qreal scale = fnt.pointSize() == -1 ? 1 : painter->device()->logicalDpiY() / (dbl*qt_defaultDpiY());
       
   660                 painter->save();
       
   661                 painter->scale(1/scale, 1/scale);
       
   662 
   689 
   663                 qreal justificationWidth;
   690                 qreal justificationWidth;
   664                 s >> justificationWidth;
   691                 s >> justificationWidth;
   665 
   692 
   666                 int flags = Qt::TextSingleLine | Qt::TextDontClip | Qt::TextForceLeftToRight;
   693                 int flags = Qt::TextSingleLine | Qt::TextDontClip | Qt::TextForceLeftToRight;
   667 
   694 
   668                 QSizeF size(1, 1);
   695                 QSizeF size(1, 1);
   669                 if (justificationWidth > 0) {
   696                 if (justificationWidth > 0) {
   670                     size.setWidth(justificationWidth*scale);
   697                     size.setWidth(justificationWidth);
   671                     flags |= Qt::TextJustificationForced;
   698                     flags |= Qt::TextJustificationForced;
   672                     flags |= Qt::AlignJustify;
   699                     flags |= Qt::AlignJustify;
   673                 }
   700                 }
   674 
   701 
   675                 QFontMetrics fm(fnt);
   702                 QFontMetrics fm(fnt);
   676                 QPointF pt(p.x()*scale, p.y()*scale - fm.ascent());
   703                 QPointF pt(p.x(), p.y() - fm.ascent());
   677                 qt_format_text(fnt, QRectF(pt, size), flags, /*opt*/0,
   704                 qt_format_text(fnt, QRectF(pt, size), flags, /*opt*/0,
   678                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
   705                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
   679                 painter->restore();
       
   680             } else {
   706             } else {
   681                 qt_format_text(font, QRectF(p, QSizeF(1, 1)), Qt::TextSingleLine | Qt::TextDontClip, /*opt*/0,
   707                 qt_format_text(font, QRectF(p, QSizeF(1, 1)), Qt::TextSingleLine | Qt::TextDontClip, /*opt*/0,
   682                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
   708                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
   683             }
   709             }
   684 
   710