Redesigned text path handling

This commit is contained in:
2018-11-04 23:54:34 +01:00
parent 622136de6a
commit c13af94275
6 changed files with 130 additions and 73 deletions

View File

@ -6,61 +6,123 @@
#include "textpathitem.h"
static QPainterPath subpath(const QPainterPath &path, int start, int end,
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)
{
QPainterPath p(path.elementAt(start));
qreal ss = 0, es = 0;
int si = start, ei = end;
for (int i = start + 1; i < end; i++)
p.lineTo(path.elementAt(i));
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 l(path.elementAt(end - 1), path.elementAt(end));
l.setLength(l.length() - cut);
p.lineTo(l.p2());
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<QPainterPath> segments(const QPainterPath &path, qreal textLength,
qreal maxAngle, qreal charWidth, int symbolSpacing, const QRectF &tileRect)
static QList<QLineF> lineString(const QPainterPath &path,
const QRectF &boundingRect)
{
QList<QPainterPath> list;
int start = 0;
qreal length = 0;
qreal angle = -1;
qreal dist = symbolSpacing;
QList<QLineF> lines;
int start = 0, end = path.elementCount() - 1;
for (int i = 1; i < path.elementCount(); i++) {
QLineF l(path.elementAt(i-1), path.elementAt(i));
qreal sl = l.length();
if (dist < symbolSpacing) {
dist += sl;
start++;
continue;
}
qreal a = l.angle();
if (angle < 0)
angle = a;
if (!tileRect.contains(path.elementAt(i))
|| sl < charWidth || qAbs(angle - a) > maxAngle
|| length > textLength) {
if (length > textLength) {
list.append(subpath(path, start, i - 1, length - textLength - 1));
dist = 0;
}
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;
dist += sl;
angle = a;
}
return list;
return QPainterPath();
}
static bool reverse(const QPainterPath &path)
@ -93,9 +155,9 @@ void Text::addLabel(const QString &text, const QPointF &pos, const QFont &font,
}
void Text::addLabel(const QString &text, const QPainterPath &path,
const QFont &font, const QPen &pen, qreal maxAngle, qreal symbolSpacing)
const QFont &font, const QPen &pen, qreal maxAngle)
{
if (path.elementCount() < 2 || !path.elementAt(0).isMoveTo())
if (path.isEmpty())
return;
if (text.isEmpty())
return;
@ -106,22 +168,22 @@ void Text::addLabel(const QString &text, const QPainterPath &path,
if (textWidth > path.length())
return;
QList<QPainterPath> list(segments(path, textWidth, maxAngle,
fm.averageCharWidth(), symbolSpacing, sceneRect()));
for (int i = 0; i < list.size(); i++) {
const QPainterPath &segment = list.at(i);
TextPathItem *pi = new TextPathItem(text, reverse(segment)
? segment.toReversed() : segment, font);
addItem(pi);
if (!sceneRect().contains(pi->sceneBoundingRect())) {
delete pi;
continue;
}
QPainterPath tp(textPath(path, textWidth, maxAngle, fm.averageCharWidth(),
sceneRect()));
if (tp.isEmpty())
return;
pi->setPen(pen);
QList<QGraphicsItem*> ci = collidingItems(pi);
for (int j = 0; j < ci.size(); j++)
ci[j]->setVisible(false);
TextPathItem *pi = new TextPathItem(text, reverse(tp) ? tp.toReversed()
: tp, font);
addItem(pi);
if (!sceneRect().contains(pi->sceneBoundingRect())) {
delete pi;
return;
}
pi->setPen(pen);
QList<QGraphicsItem*> ci = collidingItems(pi);
for (int j = 0; j < ci.size(); j++)
ci[j]->setVisible(false);
}