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
{
if (text.length() == 0)
if (text.isEmpty())
return QRect();
QTextEngine layout(text, d.data());
layout.itemize();
glyph_metrics_t gm = layout.boundingBox();
return QRect(qRound(gm.x), qRound(gm.y), qRound(gm.width), qRound(gm.height));
QTextLayout textlayout(text, d.data());
textlayout.beginLayout();
QTextLine textline = textlayout.createLine();
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())
return QRectF();
QTextEngine layout(text, d.data());
layout.itemize();
glyph_metrics_t gm = layout.boundingBox();
return QRectF(gm.x.toReal(), gm.y.toReal(),
gm.width.toReal(), gm.height.toReal());
QTextLayout textlayout(text, d.data());
textlayout.beginLayout();
QTextLine textline = textlayout.createLine();
while (textline.isValid()) {
textline = textlayout.createLine();
}
textlayout.endLayout();
return textlayout.boundingRect();
}
/*!