diff --git a/src/text.cpp b/src/text.cpp index 745a7ca..60d306c 100644 --- a/src/text.cpp +++ b/src/text.cpp @@ -69,18 +69,3 @@ QList Text::collidingItems(const TextItem *item) const return list; } - -qreal Text::avgCharRatio(const QString &str, const QFont &font) -{ - qreal ratio; - - if (str.at(0).unicode() > 0x2E80) - ratio = 1.0; - else { - ratio = (font.capitalization() == QFont::AllUppercase) ? 0.66 : 0.55; - if (font.bold()) - ratio *= 1.1; - } - - return ratio; -} diff --git a/src/text.h b/src/text.h index 21b7586..1ba00e1 100644 --- a/src/text.h +++ b/src/text.h @@ -38,8 +38,6 @@ public: void render(QPainter *painter) const; - static qreal avgCharRatio(const QString &str, const QFont &font); - private: void addItem(TextItem *item) {_items.append(item);} QList collidingItems(const TextItem *item) const; diff --git a/src/textitem.h b/src/textitem.h index 5478f20..2482d25 100644 --- a/src/textitem.h +++ b/src/textitem.h @@ -2,6 +2,7 @@ #define TEXTITEM_H #include +#include class TextItem { @@ -29,6 +30,23 @@ public: return other->shape().intersects(shape()); } +protected: + static int avgCharWidth(const QString &str, const QFont &font) + { + qreal ratio; + + if (str.at(0).unicode() >= 0x2E80) + ratio = 1.0; + else { + ratio = (font.capitalization() == QFont::AllUppercase) + ? 0.66 : 0.55; + if (font.bold()) + ratio *= 1.1; + } + + return ratio * font.pixelSize(); + } + private: QString _text; bool _visible; diff --git a/src/textpathitem.cpp b/src/textpathitem.cpp index f7fe9d6..bb87174 100644 --- a/src/textpathitem.cpp +++ b/src/textpathitem.cpp @@ -136,13 +136,12 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path, const QFont &font, int maxAngle, const QRectF &tileRect) : TextItem(text), _font(font) { - qreal acr = Text::avgCharRatio(text, _font); - int textWidth = text.size() * _font.pixelSize() * acr; + int cw = avgCharWidth(text, font); + int textWidth = text.size() * cw; if (textWidth > path.length()) return; - QPainterPath tp(textPath(path, textWidth, maxAngle, _font.pixelSize() * acr, - tileRect)); + QPainterPath tp(textPath(path, textWidth, maxAngle, cw, tileRect)); if (tp.isEmpty()) return; diff --git a/src/textpointitem.cpp b/src/textpointitem.cpp index 6fe042e..7f8c30d 100644 --- a/src/textpointitem.cpp +++ b/src/textpointitem.cpp @@ -5,8 +5,8 @@ #define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip) -static QRectF exactBoundingRect(const QString &str, const QFont &font, - int maxWidth) +QRectF TextPointItem::exactBoundingRect(const QString &str, + const QFont &font, int maxWidth) { QFontMetrics fm(font); int limit = font.pixelSize() * maxWidth; @@ -24,11 +24,11 @@ static QRectF exactBoundingRect(const QString &str, const QFont &font, return br; } -static QRectF fuzzyBoundingRect(const QString &str, const QFont &font, - int maxWidth) +QRectF TextPointItem::fuzzyBoundingRect(const QString &str, + const QFont &font, int maxWidth) { int limit = font.pixelSize() * maxWidth; - qreal cw = font.pixelSize() * Text::avgCharRatio(str, font); + qreal cw = avgCharWidth(str, font); qreal lh = font.pixelSize() * 1.25; int width = 0, lines = 0; diff --git a/src/textpointitem.h b/src/textpointitem.h index 7b73926..1100317 100644 --- a/src/textpointitem.h +++ b/src/textpointitem.h @@ -21,6 +21,12 @@ public: private: typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, int); + + static QRectF exactBoundingRect(const QString &str, const QFont &font, + int maxWidth); + static QRectF fuzzyBoundingRect(const QString &str, const QFont &font, + int maxWidth); + QRectF computeTextRect(BoundingRectFunction brf) const; QPointF _pos;