Improved fuzzy bounding rect computation

Code cleanup
This commit is contained in:
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"
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()
{
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)
{
int textWidth = text.size() * _font.pixelSize() * 0.6;
if (textWidth > path.length())
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);
TextPathItem *ti = new TextPathItem(text, path, _font, _maxAngle,
_sceneRect);
if (!_sceneRect.contains(ti->boundingRect())) {
delete ti;
return;
@ -207,3 +69,18 @@ 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;
}