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;
}
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;
static qreal avgCharRatio(const QString &str, const QFont &font);
private:
void addItem(TextItem *item) {_items.append(item);}
QList<TextItem *> collidingItems(const TextItem *item) const;

View File

@ -2,6 +2,7 @@
#define TEXTITEM_H
#include <QPainterPath>
#include <QFont>
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;

View File

@ -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;

View File

@ -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;

View File

@ -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;