mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2024-11-24 03:35:54 +01:00
Use QStaticText instead of own text caching
This commit is contained in:
parent
e8e9139740
commit
ff1c29e078
@ -1,46 +1,12 @@
|
|||||||
#include <QPainter>
|
#include <QPainter>
|
||||||
#include <QtMath>
|
#include <QtMath>
|
||||||
|
#include <QStaticText>
|
||||||
#include "config.h"
|
#include "config.h"
|
||||||
#include "textpointitem.h"
|
#include "textpointitem.h"
|
||||||
|
|
||||||
|
|
||||||
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
|
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
|
||||||
|
|
||||||
static QImage textImage(const QString &text, const QRectF &rect, qreal scale,
|
|
||||||
const QColor &color, const QFont &font)
|
|
||||||
{
|
|
||||||
QImage img(QSize(rect.size().width() * scale, rect.size().height() * scale),
|
|
||||||
QImage::Format_ARGB32_Premultiplied);
|
|
||||||
|
|
||||||
img.fill(Qt::transparent);
|
|
||||||
|
|
||||||
QFont f(font);
|
|
||||||
f.setPixelSize(font.pixelSize() * scale);
|
|
||||||
|
|
||||||
QPainter ip(&img);
|
|
||||||
ip.setPen(color);
|
|
||||||
ip.setFont(f);
|
|
||||||
ip.drawText(img.rect(), FLAGS, text);
|
|
||||||
|
|
||||||
return img;
|
|
||||||
}
|
|
||||||
|
|
||||||
QRectF TextPointItem::exactBoundingRect() const
|
|
||||||
{
|
|
||||||
QFontMetrics fm(font());
|
|
||||||
|
|
||||||
QRect br = fm.boundingRect(_textRect.toRect(), FLAGS, text());
|
|
||||||
Q_ASSERT(br.isValid());
|
|
||||||
|
|
||||||
// Italic fonts overflow the computed bounding rect, so expand it
|
|
||||||
if (font().italic())
|
|
||||||
br.adjust(-font().pixelSize() / 2.0, 0, font().pixelSize() / 2.0, 0);
|
|
||||||
if (hasHalo())
|
|
||||||
br.adjust(-1, -1, 1, 1);
|
|
||||||
|
|
||||||
return br;
|
|
||||||
}
|
|
||||||
|
|
||||||
QRectF TextPointItem::fuzzyBoundingRect() const
|
QRectF TextPointItem::fuzzyBoundingRect() const
|
||||||
{
|
{
|
||||||
int fs = font().pixelSize();
|
int fs = font().pixelSize();
|
||||||
@ -64,7 +30,7 @@ QRectF TextPointItem::fuzzyBoundingRect() const
|
|||||||
if (wl + pl < limit) {
|
if (wl + pl < limit) {
|
||||||
pl += wl + cw;
|
pl += wl + cw;
|
||||||
} else {
|
} else {
|
||||||
if (wl > limit) {
|
if (wl >= limit) {
|
||||||
if (pl > 0)
|
if (pl > 0)
|
||||||
lines++;
|
lines++;
|
||||||
} else
|
} else
|
||||||
@ -85,7 +51,7 @@ QRectF TextPointItem::fuzzyBoundingRect() const
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
QRectF TextPointItem::computeTextRect(bool exact) const
|
QRectF TextPointItem::moveTextRect(const QRectF &rect) const
|
||||||
{
|
{
|
||||||
#ifdef ENABLE_HIDPI
|
#ifdef ENABLE_HIDPI
|
||||||
QRectF iconRect = _icon.isNull() ? QRectF()
|
QRectF iconRect = _icon.isNull() ? QRectF()
|
||||||
@ -94,7 +60,7 @@ QRectF TextPointItem::computeTextRect(bool exact) const
|
|||||||
QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0),
|
QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0),
|
||||||
QSizeF(_icon.size()));
|
QSizeF(_icon.size()));
|
||||||
#endif // ENABLE_HIDPI
|
#endif // ENABLE_HIDPI
|
||||||
QRectF textRect = exact ? exactBoundingRect() : _textRect;
|
QRectF textRect(rect);
|
||||||
|
|
||||||
switch (_anchor) {
|
switch (_anchor) {
|
||||||
case Text::Center:
|
case Text::Center:
|
||||||
@ -127,7 +93,7 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
|||||||
_anchor(anchor)
|
_anchor(anchor)
|
||||||
{
|
{
|
||||||
_textRect = fuzzyBoundingRect();
|
_textRect = fuzzyBoundingRect();
|
||||||
_boundingRect = computeTextRect(false);
|
_boundingRect = moveTextRect(_textRect);
|
||||||
|
|
||||||
if (!_icon.isNull()) {
|
if (!_icon.isNull()) {
|
||||||
#ifdef ENABLE_HIDPI
|
#ifdef ENABLE_HIDPI
|
||||||
@ -154,10 +120,13 @@ void TextPointItem::setPos(const QPointF &pos)
|
|||||||
|
|
||||||
void TextPointItem::paint(QPainter *painter) const
|
void TextPointItem::paint(QPainter *painter) const
|
||||||
{
|
{
|
||||||
QRectF textRect = (!_icon.isNull() || hasHalo())
|
QRectF textRect;
|
||||||
? computeTextRect(true) : _boundingRect;
|
|
||||||
|
painter->setFont(font());
|
||||||
|
painter->setPen(pen());
|
||||||
|
|
||||||
if (!_icon.isNull()) {
|
if (!_icon.isNull()) {
|
||||||
|
textRect = moveTextRect(painter->boundingRect(_textRect, FLAGS, text()));
|
||||||
#ifdef ENABLE_HIDPI
|
#ifdef ENABLE_HIDPI
|
||||||
painter->drawImage(_pos - QPointF(_icon.width()
|
painter->drawImage(_pos - QPointF(_icon.width()
|
||||||
/ _icon.devicePixelRatioF() / 2, _icon.height()
|
/ _icon.devicePixelRatioF() / 2, _icon.height()
|
||||||
@ -166,34 +135,28 @@ void TextPointItem::paint(QPainter *painter) const
|
|||||||
painter->drawImage(_pos - QPointF(_icon.width() / 2,
|
painter->drawImage(_pos - QPointF(_icon.width() / 2,
|
||||||
_icon.height() / 2), _icon);
|
_icon.height() / 2), _icon);
|
||||||
#endif // ENABLE_HIDPI
|
#endif // ENABLE_HIDPI
|
||||||
}
|
} else
|
||||||
|
textRect = moveTextRect(_textRect);
|
||||||
|
|
||||||
if (hasHalo()) {
|
if (hasHalo()) {
|
||||||
const QTransform &t = painter->worldTransform();
|
QStaticText st(text());
|
||||||
QPoint p((textRect.topLeft() * t.m11()).toPoint());
|
st.setTextWidth(textRect.width());
|
||||||
QImage img(textImage(text(), textRect, t.m11(), halo().color(), font()));
|
st.setTextOption(QTextOption(Qt::AlignHCenter));
|
||||||
|
st.setPerformanceHint(QStaticText::AggressiveCaching);
|
||||||
|
|
||||||
painter->save();
|
painter->setPen(halo().color());
|
||||||
painter->resetTransform();
|
painter->drawStaticText(textRect.topLeft() + QPointF(-1, -1), st);
|
||||||
|
painter->drawStaticText(textRect.topLeft() + QPointF(+1, +1), st);
|
||||||
painter->drawImage(p.x() - 1, p.y() - 1, img);
|
painter->drawStaticText(textRect.topLeft() + QPointF(-1, +1), st);
|
||||||
painter->drawImage(p.x() + 1, p.y() + 1, img);
|
painter->drawStaticText(textRect.topLeft() + QPointF(+1, -1), st);
|
||||||
painter->drawImage(p.x() - 1, p.y() + 1, img);
|
painter->drawStaticText(textRect.topLeft() + QPointF(0, -1), st);
|
||||||
painter->drawImage(p.x() + 1, p.y() - 1, img);
|
painter->drawStaticText(textRect.topLeft() + QPointF(0, +1), st);
|
||||||
painter->drawImage(p.x(), p.y() - 1, img);
|
painter->drawStaticText(textRect.topLeft() + QPointF(-1, 0), st);
|
||||||
painter->drawImage(p.x(), p.y() + 1, img);
|
painter->drawStaticText(textRect.topLeft() + QPointF(+1, 0), st);
|
||||||
painter->drawImage(p.x() - 1, p.y(), img);
|
|
||||||
painter->drawImage(p.x() + 1, p.y(), img);
|
|
||||||
|
|
||||||
painter->drawImage(p.x(), p.y(), textImage(text(), textRect, t.m11(),
|
|
||||||
pen().color(), font()));
|
|
||||||
|
|
||||||
painter->restore();
|
|
||||||
} else {
|
|
||||||
painter->setFont(font());
|
|
||||||
painter->setPen(pen());
|
painter->setPen(pen());
|
||||||
|
painter->drawStaticText(textRect.topLeft(), st);
|
||||||
|
} else
|
||||||
painter->drawText(textRect, FLAGS, text());
|
painter->drawText(textRect, FLAGS, text());
|
||||||
}
|
|
||||||
|
|
||||||
//painter->setBrush(Qt::NoBrush);
|
//painter->setBrush(Qt::NoBrush);
|
||||||
//painter->setPen(Qt::red);
|
//painter->setPen(Qt::red);
|
||||||
|
@ -18,9 +18,8 @@ public:
|
|||||||
void setPos(const QPointF &pos);
|
void setPos(const QPointF &pos);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QRectF exactBoundingRect() const;
|
|
||||||
QRectF fuzzyBoundingRect() const;
|
QRectF fuzzyBoundingRect() const;
|
||||||
QRectF computeTextRect(bool exact) const;
|
QRectF moveTextRect(const QRectF &rect) const;
|
||||||
bool hasHalo() const
|
bool hasHalo() const
|
||||||
{return halo().color().isValid() && halo().width() > 0;}
|
{return halo().color().isValid() && halo().width() > 0;}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user