src/gui/image/qpicture.cpp
changeset 30 5dc02b23752f
parent 22 79de32ba3296
child 33 3e2da88830cd
equal deleted inserted replaced
29:b72c6db6890b 30:5dc02b23752f
   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 
       
   473 /*!
   443 /*!
   474   \internal
   444   \internal
   475   Iterates over the internal picture data and draws the picture using
   445   Iterates over the internal picture data and draws the picture using
   476   \a painter.
   446   \a painter.
   477 */
   447 */
   677             // aligned when QPicturePaintEngine::drawTextItem() serializes the
   647             // aligned when QPicturePaintEngine::drawTextItem() serializes the
   678             // drawText() call, therefore ul is unsed in this context
   648             // drawText() call, therefore ul is unsed in this context
   679 
   649 
   680             if (d->formatMajor >= 9) {
   650             if (d->formatMajor >= 9) {
   681                 s >> dbl;
   651                 s >> dbl;
   682                 QFont fnt(font);
   652                 QFont fnt(font, painter->device());
   683                 if (dbl != 1.0) {
   653 
   684                     QFakeDevice fake;
   654                 // Fonts that specify a pixel size should not be scaled - QPicture already
   685                     fake.setDpiX(qRound(dbl*qt_defaultDpiX()));
   655                 // have a matrix set to compensate for the DPI differences between the
   686                     fake.setDpiY(qRound(dbl*qt_defaultDpiY()));
   656                 // default Qt DPI and the actual target device DPI, and we have to take that
   687                     fnt = QFont(font, &fake);
   657                 // into consideration in the case where the font has a pixel size set.
   688                 }
   658 
       
   659                 qreal scale = fnt.pointSize() == -1 ? 1 : painter->device()->logicalDpiY() / (dbl*qt_defaultDpiY());
       
   660                 painter->save();
       
   661                 painter->scale(1/scale, 1/scale);
   689 
   662 
   690                 qreal justificationWidth;
   663                 qreal justificationWidth;
   691                 s >> justificationWidth;
   664                 s >> justificationWidth;
   692 
   665 
   693                 int flags = Qt::TextSingleLine | Qt::TextDontClip | Qt::TextForceLeftToRight;
   666                 int flags = Qt::TextSingleLine | Qt::TextDontClip | Qt::TextForceLeftToRight;
   694 
   667 
   695                 QSizeF size(1, 1);
   668                 QSizeF size(1, 1);
   696                 if (justificationWidth > 0) {
   669                 if (justificationWidth > 0) {
   697                     size.setWidth(justificationWidth);
   670                     size.setWidth(justificationWidth*scale);
   698                     flags |= Qt::TextJustificationForced;
   671                     flags |= Qt::TextJustificationForced;
   699                     flags |= Qt::AlignJustify;
   672                     flags |= Qt::AlignJustify;
   700                 }
   673                 }
   701 
   674 
   702                 QFontMetrics fm(fnt);
   675                 QFontMetrics fm(fnt);
   703                 QPointF pt(p.x(), p.y() - fm.ascent());
   676                 QPointF pt(p.x()*scale, p.y()*scale - fm.ascent());
   704                 qt_format_text(fnt, QRectF(pt, size), flags, /*opt*/0,
   677                 qt_format_text(fnt, QRectF(pt, size), flags, /*opt*/0,
   705                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
   678                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
       
   679                 painter->restore();
   706             } else {
   680             } else {
   707                 qt_format_text(font, QRectF(p, QSizeF(1, 1)), Qt::TextSingleLine | Qt::TextDontClip, /*opt*/0,
   681                 qt_format_text(font, QRectF(p, QSizeF(1, 1)), Qt::TextSingleLine | Qt::TextDontClip, /*opt*/0,
   708                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
   682                                str, /*brect=*/0, /*tabstops=*/0, /*...*/0, /*tabarraylen=*/0, painter);
   709             }
   683             }
   710 
   684