Improved text bounding rect estimation

This commit is contained in:
Martin Tůma 2018-11-24 17:44:24 +01:00
parent 3ea8ce64a7
commit 0dd83979cf
2 changed files with 11 additions and 7 deletions

View File

@ -6,10 +6,10 @@
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip) #define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
static QRectF exactBoundingRect(const QString &str, const QFont &font, static QRectF exactBoundingRect(const QString &str, const QFont &font,
int maxTextWidth) const Text::Properties &prop)
{ {
QFontMetrics fm(font); QFontMetrics fm(font);
int limit = font.pixelSize() * maxTextWidth; int limit = font.pixelSize() * prop.maxWidth;
// Italic fonts overflow the computed bounding rect, so reduce it // Italic fonts overflow the computed bounding rect, so reduce it
// a little bit. // a little bit.
if (font.italic()) if (font.italic())
@ -25,10 +25,13 @@ static QRectF exactBoundingRect(const QString &str, const QFont &font,
} }
static QRectF fuzzyBoundingRect(const QString &str, const QFont &font, static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
int maxTextWidth) const Text::Properties &prop)
{ {
int limit = font.pixelSize() * maxTextWidth; int limit = font.pixelSize() * prop.maxWidth;
qreal cw = font.pixelSize() * 0.6; qreal acw = (prop.transform == Text::Uppercase) ? 0.66 : 0.55;
qreal cw = font.pixelSize() * acw;
if (font.bold())
acw *= 1.1;
qreal lh = font.pixelSize() * 1.25; qreal lh = font.pixelSize() * 1.25;
int width = 0, lines = 0; int width = 0, lines = 0;
@ -69,7 +72,7 @@ static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
{ {
QRectF iconRect = _icon.isNull() ? QRectF() : _icon.rect(); QRectF iconRect = _icon.isNull() ? QRectF() : _icon.rect();
QRectF textRect = brf(text(), _font, _properties.maxWidth); QRectF textRect = brf(text(), _font, _properties);
switch (_properties.anchor) { switch (_properties.anchor) {
case Text::Center: case Text::Center:

View File

@ -20,7 +20,8 @@ public:
void setPen(const QPen &pen) {_pen = pen;} void setPen(const QPen &pen) {_pen = pen;}
private: private:
typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, int); typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &,
const Text::Properties &);
QRectF computeTextRect(BoundingRectFunction brf) const; QRectF computeTextRect(BoundingRectFunction brf) const;
QPointF _pos; QPointF _pos;