calculate bounding rectangles via QTextLayout from QFontMetrics/QFontMetricsF

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-01-04 21:42:35 +02:00
parent 18ab4d967e
commit ddc3af4962

View file

@ -560,13 +560,17 @@ int QFontMetrics::width(QChar ch) const
*/ */
QRect QFontMetrics::boundingRect(const QString &text) const QRect QFontMetrics::boundingRect(const QString &text) const
{ {
if (text.length() == 0) if (text.isEmpty())
return QRect(); return QRect();
QTextEngine layout(text, d.data()); QTextLayout textlayout(text, d.data());
layout.itemize(); textlayout.beginLayout();
glyph_metrics_t gm = layout.boundingBox(); QTextLine textline = textlayout.createLine();
return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height)); while (textline.isValid()) {
textline = textlayout.createLine();
}
textlayout.endLayout();
return textlayout.boundingRect().toRect();
} }
/*! /*!
@ -1325,11 +1329,14 @@ QRectF QFontMetricsF::boundingRect(const QString &text) const
if (text.isEmpty()) if (text.isEmpty())
return QRectF(); return QRectF();
QTextEngine layout(text, d.data()); QTextLayout textlayout(text, d.data());
layout.itemize(); textlayout.beginLayout();
glyph_metrics_t gm = layout.boundingBox(); QTextLine textline = textlayout.createLine();
return QRectF(gm.x.toReal(), gm.y.toReal(), while (textline.isValid()) {
gm.width.toReal(), gm.height.toReal()); textline = textlayout.createLine();
}
textlayout.endLayout();
return textlayout.boundingRect();
} }
/*! /*!