Improved fuzzy bounding rect computation

Code cleanup
This commit is contained in:
Martin Tůma 2018-11-26 22:17:57 +01:00
parent 9a314c408e
commit 6f5bedb82b
5 changed files with 169 additions and 148 deletions

View File

@ -5,135 +5,6 @@
#include "textpathitem.h" #include "textpathitem.h"
static QPointF intersection(const QLineF &line, const QRectF &rect)
{
QPointF p;
if (line.intersect(QLineF(rect.topLeft(), rect.topRight()), &p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.topLeft(), rect.bottomLeft()), &p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.bottomRight(), rect.bottomLeft()), &p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.bottomRight(), rect.topRight()), &p)
== QLineF::BoundedIntersection)
return p;
return rect.center();
}
static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
qreal cut)
{
qreal ss = 0, es = 0;
int si = start, ei = end;
for (int i = start; i <= end; i++) {
qreal len = lines.at(i).length();
if (ss + len < cut / 2) {
ss += len;
si++;
} else
break;
}
for (int i = end; i >= start; i--) {
qreal len = lines.at(i).length();
if (es + len < cut / 2) {
es += len;
ei--;
} else
break;
}
QLineF sl(lines.at(si).p2(), lines.at(si).p1());
sl.setLength(sl.length() - (cut / 2 - ss));
QLineF el(lines.at(ei));
el.setLength(el.length() - (cut / 2 - es));
QPainterPath p(sl.p2());
for (int i = si; i <= ei; i++)
p.lineTo(lines.at(i).p2());
p.setElementPositionAt(p.elementCount() - 1, el.p2().x(), el.p2().y());
return p;
}
static QList<QLineF> lineString(const QPainterPath &path,
const QRectF &boundingRect)
{
QList<QLineF> lines;
int start = 0, end = path.elementCount() - 1;
for (int i = 0; i < path.elementCount(); i++) {
if (boundingRect.contains(path.elementAt(i))) {
start = i;
break;
}
}
for (int i = path.elementCount() - 1; i >= 0; i--) {
if (boundingRect.contains(path.elementAt(i))) {
end = i;
break;
}
}
if (start > 0) {
QLineF l(path.elementAt(start-1), path.elementAt(start));
QPointF p(intersection(l, boundingRect));
if (p != boundingRect.center())
lines.append(QLineF(p, path.elementAt(start)));
}
for (int i = start + 1; i <= end; i++)
lines.append(QLineF(path.elementAt(i-1), path.elementAt(i)));
if (end < path.elementCount() - 1) {
QLineF l(path.elementAt(end), path.elementAt(end+1));
QPointF p(intersection(l, boundingRect));
if (p != boundingRect.center())
lines.append(QLineF(path.elementAt(end), p));
}
return lines;
}
static QPainterPath textPath(const QPainterPath &path, qreal textWidth,
qreal maxAngle, qreal charWidth, const QRectF &tileRect)
{
QList<QLineF> lines(lineString(path, tileRect));
qreal length = 0;
qreal angle = lines.first().angle();
int last = 0;
for (int i = 0; i < lines.size(); i++) {
qreal sl = lines.at(i).length();
qreal a = lines.at(i).angle();
if (!tileRect.contains(lines.at(i).p2()) || sl < charWidth
|| qAbs(angle - a) > maxAngle) {
if (length > textWidth)
return subpath(lines, last, i - 1, length - textWidth);
last = i;
length = 0;
} else
length += sl;
angle = a;
}
return (length > textWidth)
? subpath(lines, last, lines.size() - 1, length - textWidth)
: QPainterPath();
}
static bool reverse(const QPainterPath &path)
{
QLineF l(path.elementAt(0), path.elementAt(1));
qreal angle = l.angle();
return (angle > 90 && angle < 270) ? true : false;
}
Text::~Text() Text::~Text()
{ {
for (int i = 0; i < _items.size(); i++) for (int i = 0; i < _items.size(); i++)
@ -168,17 +39,8 @@ void Text::addLabel(const QString &text, const QPointF &pos, bool overlap,
void Text::addLabel(const QString &text, const QPainterPath &path) void Text::addLabel(const QString &text, const QPainterPath &path)
{ {
int textWidth = text.size() * _font.pixelSize() * 0.6; TextPathItem *ti = new TextPathItem(text, path, _font, _maxAngle,
if (textWidth > path.length()) _sceneRect);
return;
QPainterPath tp(textPath(path, textWidth, _maxAngle, _font.pixelSize() / 2,
_sceneRect));
if (tp.isEmpty())
return;
TextPathItem *ti = new TextPathItem(text, reverse(tp) ? tp.toReversed()
: tp, _font);
if (!_sceneRect.contains(ti->boundingRect())) { if (!_sceneRect.contains(ti->boundingRect())) {
delete ti; delete ti;
return; return;
@ -207,3 +69,18 @@ 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,6 +38,8 @@ 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

@ -3,13 +3,155 @@
#include "textpathitem.h" #include "textpathitem.h"
TextPathItem::TextPathItem(const QString &text, const QPainterPath &path, static QPointF intersection(const QLineF &line, const QRectF &rect)
const QFont &font) : TextItem(text), _path(path), _font(font)
{ {
QPointF p;
if (line.intersect(QLineF(rect.topLeft(), rect.topRight()), &p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.topLeft(), rect.bottomLeft()), &p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.bottomRight(), rect.bottomLeft()), &p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.bottomRight(), rect.topRight()), &p)
== QLineF::BoundedIntersection)
return p;
return rect.center();
}
static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
qreal cut)
{
qreal ss = 0, es = 0;
int si = start, ei = end;
for (int i = start; i <= end; i++) {
qreal len = lines.at(i).length();
if (ss + len < cut / 2) {
ss += len;
si++;
} else
break;
}
for (int i = end; i >= start; i--) {
qreal len = lines.at(i).length();
if (es + len < cut / 2) {
es += len;
ei--;
} else
break;
}
QLineF sl(lines.at(si).p2(), lines.at(si).p1());
sl.setLength(sl.length() - (cut / 2 - ss));
QLineF el(lines.at(ei));
el.setLength(el.length() - (cut / 2 - es));
QPainterPath p(sl.p2());
for (int i = si; i <= ei; i++)
p.lineTo(lines.at(i).p2());
p.setElementPositionAt(p.elementCount() - 1, el.p2().x(), el.p2().y());
return p;
}
static QList<QLineF> lineString(const QPainterPath &path,
const QRectF &boundingRect)
{
QList<QLineF> lines;
int start = 0, end = path.elementCount() - 1;
for (int i = 0; i < path.elementCount(); i++) {
if (boundingRect.contains(path.elementAt(i))) {
start = i;
break;
}
}
for (int i = path.elementCount() - 1; i >= 0; i--) {
if (boundingRect.contains(path.elementAt(i))) {
end = i;
break;
}
}
if (start > 0) {
QLineF l(path.elementAt(start-1), path.elementAt(start));
QPointF p(intersection(l, boundingRect));
if (p != boundingRect.center())
lines.append(QLineF(p, path.elementAt(start)));
}
for (int i = start + 1; i <= end; i++)
lines.append(QLineF(path.elementAt(i-1), path.elementAt(i)));
if (end < path.elementCount() - 1) {
QLineF l(path.elementAt(end), path.elementAt(end+1));
QPointF p(intersection(l, boundingRect));
if (p != boundingRect.center())
lines.append(QLineF(path.elementAt(end), p));
}
return lines;
}
static QPainterPath textPath(const QPainterPath &path, qreal textWidth,
qreal maxAngle, qreal charWidth, const QRectF &tileRect)
{
QList<QLineF> lines(lineString(path, tileRect));
qreal length = 0;
qreal angle = lines.first().angle();
int last = 0;
for (int i = 0; i < lines.size(); i++) {
qreal sl = lines.at(i).length();
qreal a = lines.at(i).angle();
if (!tileRect.contains(lines.at(i).p2()) || sl < charWidth
|| qAbs(angle - a) > maxAngle) {
if (length > textWidth)
return subpath(lines, last, i - 1, length - textWidth);
last = i;
length = 0;
} else
length += sl;
angle = a;
}
return (length > textWidth)
? subpath(lines, last, lines.size() - 1, length - textWidth)
: QPainterPath();
}
static bool reverse(const QPainterPath &path)
{
QLineF l(path.elementAt(0), path.elementAt(1));
qreal angle = l.angle();
return (angle > 90 && angle < 270) ? true : false;
}
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;
if (textWidth > path.length())
return;
QPainterPath tp(textPath(path, textWidth, maxAngle, _font.pixelSize() * acr,
tileRect));
if (tp.isEmpty())
return;
_path = reverse(tp) ? tp.toReversed() : tp;
QPainterPathStroker s; QPainterPathStroker s;
s.setWidth(font.pixelSize()); s.setWidth(font.pixelSize());
s.setCapStyle(Qt::FlatCap); s.setCapStyle(Qt::FlatCap);
_shape = s.createStroke(path).simplified(); _shape = s.createStroke(_path).simplified();
_boundingRect = _shape.boundingRect(); _boundingRect = _shape.boundingRect();
} }

View File

@ -10,7 +10,7 @@ class TextPathItem : public TextItem
{ {
public: public:
TextPathItem(const QString &text, const QPainterPath &path, TextPathItem(const QString &text, const QPainterPath &path,
const QFont &font); const QFont &font, int maxAngle, const QRectF &tileRect);
QPainterPath shape() const {return _shape;} QPainterPath shape() const {return _shape;}
QRectF boundingRect() const {return _boundingRect;} QRectF boundingRect() const {return _boundingRect;}

View File

@ -28,13 +28,13 @@ static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
int maxWidth) int maxWidth)
{ {
int limit = font.pixelSize() * maxWidth; int limit = font.pixelSize() * maxWidth;
qreal acw = (font.capitalization() == QFont::AllUppercase) ? 0.66 : 0.55; qreal cw = font.pixelSize() * Text::avgCharRatio(str, font);
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;
if (font.italic())
limit -= font.pixelSize() / 2.0;
QStringList l(str.split('\n')); QStringList l(str.split('\n'));
for (int i = 0; i < l.size(); i++) { for (int i = 0; i < l.size(); i++) {
int lw = (int)(l.at(i).length() * cw); int lw = (int)(l.at(i).length() * cw);