Improved/optimized text rendering

This commit is contained in:
Martin Tůma 2019-10-01 22:11:45 +02:00
parent df4134c03a
commit f5c15ece52
3 changed files with 18 additions and 21 deletions

View File

@ -21,12 +21,12 @@ int TextItem::avgCharWidth() const
ratio = 1.0;
// Greek & Cyrilic
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())
ratio *= 1.1;
// The rest (Latin scripts, Arabic, ...)
} else {
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.7 : 0.58;
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.63;
if (_font.bold())
ratio *= 1.1;
}

View File

@ -9,17 +9,13 @@
QRectF TextPointItem::exactBoundingRect() const
{
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());
// Expand the bounding rect back to the real content size
// Italic fonts overflow the computed bounding rect, so expand it
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;
}
@ -74,7 +70,7 @@ QRectF TextPointItem::computeTextRect(bool exact) const
QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0),
QSizeF(_icon.size()));
#endif // ENABLE_HIDPI
QRectF textRect = exact ? exactBoundingRect() : fuzzyBoundingRect();
QRectF textRect = exact ? exactBoundingRect() : _textRect;
switch (_anchor) {
case Text::Center:
@ -106,6 +102,7 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
_anchor(anchor)
{
_textRect = fuzzyBoundingRect();
_boundingRect = computeTextRect(false);
if (!_icon.isNull()) {
@ -124,15 +121,10 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
void TextPointItem::paint(QPainter *painter) const
{
//painter->setPen(Qt::red);
//painter->drawRect(_boundingRect);
QRectF textRect;
bool hasHalo = halo().color().isValid() && halo().width() > 0;
if (!_icon.isNull()) {
textRect = (_anchor != Text::Center || hasHalo)
? computeTextRect(true) : _boundingRect;
textRect = computeTextRect(true);
#ifdef ENABLE_HIDPI
painter->drawImage(_pos - QPointF(_icon.width()
/ _icon.devicePixelRatioF() / 2, _icon.height()
@ -142,10 +134,9 @@ void TextPointItem::paint(QPainter *painter) const
_icon.height() / 2), _icon);
#endif // ENABLE_HIDPI
} else
textRect = hasHalo ? computeTextRect(true) : _boundingRect;
textRect = _boundingRect;
if (hasHalo) {
if (halo().color().isValid() && halo().width() > 0) {
QRect ir(textRect.toRect());
QImage img(ir.size(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
@ -171,4 +162,10 @@ void TextPointItem::paint(QPainter *painter) const
painter->setPen(pen());
painter->drawText(textRect, FLAGS, text());
}
//painter->setBrush(Qt::NoBrush);
//painter->setPen(Qt::red);
//painter->drawRect(_boundingRect);
//painter->setPen(Qt::blue);
//painter->drawRect(textRect);
}

View File

@ -22,7 +22,7 @@ private:
QPointF _pos;
QPainterPath _shape;
QRectF _boundingRect;
QRectF _textRect, _boundingRect;
QImage _icon;
int _maxWidth;
Text::Anchor _anchor;