Yet another code cleanup

This commit is contained in:
Martin Tůma 2018-11-27 19:53:08 +01:00
parent ccfcc370d6
commit 085a90e0e5
6 changed files with 32 additions and 26 deletions

View File

@ -69,18 +69,3 @@ QList<TextItem*> Text::collidingItems(const TextItem *item) const
return list; 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;
}

View File

@ -38,8 +38,6 @@ public:
void render(QPainter *painter) const; void render(QPainter *painter) const;
static qreal avgCharRatio(const QString &str, const QFont &font);
private: private:
void addItem(TextItem *item) {_items.append(item);} void addItem(TextItem *item) {_items.append(item);}
QList<TextItem *> collidingItems(const TextItem *item) const; QList<TextItem *> collidingItems(const TextItem *item) const;

View File

@ -2,6 +2,7 @@
#define TEXTITEM_H #define TEXTITEM_H
#include <QPainterPath> #include <QPainterPath>
#include <QFont>
class TextItem class TextItem
{ {
@ -29,6 +30,23 @@ public:
return other->shape().intersects(shape()); 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: private:
QString _text; QString _text;
bool _visible; bool _visible;

View File

@ -136,13 +136,12 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
const QFont &font, int maxAngle, const QRectF &tileRect) const QFont &font, int maxAngle, const QRectF &tileRect)
: TextItem(text), _font(font) : TextItem(text), _font(font)
{ {
qreal acr = Text::avgCharRatio(text, _font); int cw = avgCharWidth(text, font);
int textWidth = text.size() * _font.pixelSize() * acr; int textWidth = text.size() * cw;
if (textWidth > path.length()) if (textWidth > path.length())
return; return;
QPainterPath tp(textPath(path, textWidth, maxAngle, _font.pixelSize() * acr, QPainterPath tp(textPath(path, textWidth, maxAngle, cw, tileRect));
tileRect));
if (tp.isEmpty()) if (tp.isEmpty())
return; return;

View File

@ -5,8 +5,8 @@
#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, QRectF TextPointItem::exactBoundingRect(const QString &str,
int maxWidth) const QFont &font, int maxWidth)
{ {
QFontMetrics fm(font); QFontMetrics fm(font);
int limit = font.pixelSize() * maxWidth; int limit = font.pixelSize() * maxWidth;
@ -24,11 +24,11 @@ static QRectF exactBoundingRect(const QString &str, const QFont &font,
return br; return br;
} }
static QRectF fuzzyBoundingRect(const QString &str, const QFont &font, QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
int maxWidth) const QFont &font, int maxWidth)
{ {
int limit = font.pixelSize() * 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; qreal lh = font.pixelSize() * 1.25;
int width = 0, lines = 0; int width = 0, lines = 0;

View File

@ -21,6 +21,12 @@ public:
private: private:
typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, int); 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; QRectF computeTextRect(BoundingRectFunction brf) const;
QPointF _pos; QPointF _pos;