mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2024-11-24 03:35:54 +01:00
Improved/optimized text rendering
This commit is contained in:
parent
df4134c03a
commit
f5c15ece52
@ -21,12 +21,12 @@ int TextItem::avgCharWidth() const
|
|||||||
ratio = 1.0;
|
ratio = 1.0;
|
||||||
// Greek & Cyrilic
|
// Greek & Cyrilic
|
||||||
else if (cp >= 0x03FF && cp <= 0x04FF) {
|
else if (cp >= 0x03FF && cp <= 0x04FF) {
|
||||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.67;
|
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.80 : 0.72;
|
||||||
if (_font.bold())
|
if (_font.bold())
|
||||||
ratio *= 1.1;
|
ratio *= 1.1;
|
||||||
// The rest (Latin scripts, Arabic, ...)
|
// The rest (Latin scripts, Arabic, ...)
|
||||||
} else {
|
} else {
|
||||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.7 : 0.58;
|
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.63;
|
||||||
if (_font.bold())
|
if (_font.bold())
|
||||||
ratio *= 1.1;
|
ratio *= 1.1;
|
||||||
}
|
}
|
||||||
|
@ -9,17 +9,13 @@
|
|||||||
QRectF TextPointItem::exactBoundingRect() const
|
QRectF TextPointItem::exactBoundingRect() const
|
||||||
{
|
{
|
||||||
QFontMetrics fm(font());
|
QFontMetrics fm(font());
|
||||||
int limit = font().pixelSize() * _maxWidth;
|
|
||||||
// Italic fonts overflow the computed bounding rect, so reduce it
|
|
||||||
// a little bit.
|
|
||||||
if (font().italic())
|
|
||||||
limit -= font().pixelSize() / 2.0;
|
|
||||||
|
|
||||||
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, text());
|
QRect br = fm.boundingRect(_textRect.toRect(), FLAGS, text());
|
||||||
Q_ASSERT(br.isValid());
|
Q_ASSERT(br.isValid());
|
||||||
// Expand the bounding rect back to the real content size
|
|
||||||
|
// Italic fonts overflow the computed bounding rect, so expand it
|
||||||
if (font().italic())
|
if (font().italic())
|
||||||
br.adjust(-font().pixelSize() / 4.0, 0, font().pixelSize() / 4.0, 0);
|
br.adjust(-font().pixelSize() / 2.0, 0, font().pixelSize() / 2.0, 0);
|
||||||
|
|
||||||
return br;
|
return br;
|
||||||
}
|
}
|
||||||
@ -74,7 +70,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() : fuzzyBoundingRect();
|
QRectF textRect = exact ? exactBoundingRect() : _textRect;
|
||||||
|
|
||||||
switch (_anchor) {
|
switch (_anchor) {
|
||||||
case Text::Center:
|
case Text::Center:
|
||||||
@ -106,6 +102,7 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
|||||||
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||||
_anchor(anchor)
|
_anchor(anchor)
|
||||||
{
|
{
|
||||||
|
_textRect = fuzzyBoundingRect();
|
||||||
_boundingRect = computeTextRect(false);
|
_boundingRect = computeTextRect(false);
|
||||||
|
|
||||||
if (!_icon.isNull()) {
|
if (!_icon.isNull()) {
|
||||||
@ -124,15 +121,10 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
|||||||
|
|
||||||
void TextPointItem::paint(QPainter *painter) const
|
void TextPointItem::paint(QPainter *painter) const
|
||||||
{
|
{
|
||||||
//painter->setPen(Qt::red);
|
|
||||||
//painter->drawRect(_boundingRect);
|
|
||||||
|
|
||||||
QRectF textRect;
|
QRectF textRect;
|
||||||
bool hasHalo = halo().color().isValid() && halo().width() > 0;
|
|
||||||
|
|
||||||
if (!_icon.isNull()) {
|
if (!_icon.isNull()) {
|
||||||
textRect = (_anchor != Text::Center || hasHalo)
|
textRect = computeTextRect(true);
|
||||||
? computeTextRect(true) : _boundingRect;
|
|
||||||
#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()
|
||||||
@ -142,10 +134,9 @@ void TextPointItem::paint(QPainter *painter) const
|
|||||||
_icon.height() / 2), _icon);
|
_icon.height() / 2), _icon);
|
||||||
#endif // ENABLE_HIDPI
|
#endif // ENABLE_HIDPI
|
||||||
} else
|
} else
|
||||||
textRect = hasHalo ? computeTextRect(true) : _boundingRect;
|
textRect = _boundingRect;
|
||||||
|
|
||||||
|
if (halo().color().isValid() && halo().width() > 0) {
|
||||||
if (hasHalo) {
|
|
||||||
QRect ir(textRect.toRect());
|
QRect ir(textRect.toRect());
|
||||||
QImage img(ir.size(), QImage::Format_ARGB32_Premultiplied);
|
QImage img(ir.size(), QImage::Format_ARGB32_Premultiplied);
|
||||||
img.fill(Qt::transparent);
|
img.fill(Qt::transparent);
|
||||||
@ -171,4 +162,10 @@ void TextPointItem::paint(QPainter *painter) const
|
|||||||
painter->setPen(pen());
|
painter->setPen(pen());
|
||||||
painter->drawText(textRect, FLAGS, text());
|
painter->drawText(textRect, FLAGS, text());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//painter->setBrush(Qt::NoBrush);
|
||||||
|
//painter->setPen(Qt::red);
|
||||||
|
//painter->drawRect(_boundingRect);
|
||||||
|
//painter->setPen(Qt::blue);
|
||||||
|
//painter->drawRect(textRect);
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ private:
|
|||||||
|
|
||||||
QPointF _pos;
|
QPointF _pos;
|
||||||
QPainterPath _shape;
|
QPainterPath _shape;
|
||||||
QRectF _boundingRect;
|
QRectF _textRect, _boundingRect;
|
||||||
QImage _icon;
|
QImage _icon;
|
||||||
int _maxWidth;
|
int _maxWidth;
|
||||||
Text::Anchor _anchor;
|
Text::Anchor _anchor;
|
||||||
|
Loading…
Reference in New Issue
Block a user