Improved text path drawing

This commit is contained in:
Martin Tůma 2018-10-31 17:40:43 +01:00
parent 1ffe156768
commit 80bf56bda8
4 changed files with 33 additions and 14 deletions

View File

@ -271,7 +271,7 @@ bool Style::Layer::Paint::antialias(Layer::Type type) const
} }
Style::Layer::Layout::Layout(const QJsonObject &json) Style::Layer::Layout::Layout(const QJsonObject &json)
: _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap), : _textSize(16), _textMaxWidth(10), _textMaxAngle(45), _lineCap(Qt::FlatCap),
_lineJoin(Qt::MiterJoin), _capitalize(false) _lineJoin(Qt::MiterJoin), _capitalize(false)
{ {
if (!(json.contains("text-field") && json["text-field"].isString())) if (!(json.contains("text-field") && json["text-field"].isString()))
@ -291,11 +291,14 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
_textSize = FunctionF(json["text-size"].toObject()); _textSize = FunctionF(json["text-size"].toObject());
else if (json.contains("text-size") && json["text-size"].isDouble()) else if (json.contains("text-size") && json["text-size"].isDouble())
_textSize = json["text-size"].toDouble(); _textSize = json["text-size"].toDouble();
if (json.contains("text-max-width") && json["text-max-width"].isObject()) if (json.contains("text-max-width") && json["text-max-width"].isObject())
_textMaxWidth = FunctionF(json["text-max-width"].toObject()); _textMaxWidth = FunctionF(json["text-max-width"].toObject());
if (json.contains("text-max-width") && json["text-max-width"].isDouble()) else if (json.contains("text-max-width") && json["text-max-width"].isDouble())
_textMaxWidth = json["text-max-width"].toDouble(); _textMaxWidth = json["text-max-width"].toDouble();
if (json.contains("text-max-angle") && json["text-max-angle"].isObject())
_textMaxWidth = FunctionF(json["text-max-angle"].toObject());
else if (json.contains("text-max-angle") && json["text-max-angle"].isDouble())
_textMaxWidth = json["text-max-angle"].toDouble();
if (json.contains("text-transform") && json["text-transform"].isString()) if (json.contains("text-transform") && json["text-transform"].isString())
_capitalize = json["text-transform"].toString() == "uppercase"; _capitalize = json["text-transform"].toString() == "uppercase";
@ -411,7 +414,8 @@ void Style::Layer::drawSymbol(int zoom, const QPainterPath &path,
tile.text().addLabel(text.trimmed(), path.elementAt(0), font, pen, tile.text().addLabel(text.trimmed(), path.elementAt(0), font, pen,
_layout.maxTextWidth(zoom)); _layout.maxTextWidth(zoom));
else else
tile.text().addLabel(text.trimmed(), path, font, pen); tile.text().addLabel(text.trimmed(), path, font, pen,
_layout.maxTextAngle(zoom));
} }
bool Style::load(const QString &fileName) bool Style::load(const QString &fileName)
@ -454,12 +458,14 @@ void Style::drawFeature(int layer, const QPainterPath &path,
void Style::drawBackground(Tile &tile) void Style::drawBackground(Tile &tile)
{ {
QRectF rect(0, 0, tile.size(), tile.size());
QPainterPath path; QPainterPath path;
path.addRect(QRectF(0, 0, tile.size(), tile.size())); path.addRect(rect);
if (_styles.isEmpty()) { if (_styles.isEmpty()) {
tile.painter().setBrush(Qt::lightGray); tile.painter().setBrush(Qt::lightGray);
tile.painter().drawPath(path); tile.painter().setPen(Qt::NoPen);
tile.painter().drawRect(rect);
} else if (_styles.first().isBackground()) } else if (_styles.first().isBackground())
_styles.first().drawPath(_zoom, path, tile); _styles.first().drawPath(_zoom, path, tile);
} }

View File

@ -76,12 +76,14 @@ private:
class Layout { class Layout {
public: public:
Layout() : _textSize(16), _textMaxWidth(10), _lineCap(Qt::FlatCap), Layout() : _textSize(16), _textMaxWidth(10), _textMaxAngle(45),
_lineJoin(Qt::MiterJoin), _capitalize(false) {} _lineCap(Qt::FlatCap), _lineJoin(Qt::MiterJoin),
_capitalize(false) {}
Layout(const QJsonObject &json); Layout(const QJsonObject &json);
bool capitalize() const {return _capitalize;} bool capitalize() const {return _capitalize;}
qreal maxTextWidth(int zoom) const {return _textMaxWidth.value(zoom);} qreal maxTextWidth(int zoom) const {return _textMaxWidth.value(zoom);}
qreal maxTextAngle(int zoom) const {return _textMaxAngle.value(zoom);}
const QString &field() const {return _textField;} const QString &field() const {return _textField;}
const QStringList &keys() const {return _keys;} const QStringList &keys() const {return _keys;}
QFont font(int zoom) const; QFont font(int zoom) const;
@ -93,6 +95,7 @@ private:
QString _textField; QString _textField;
FunctionF _textSize; FunctionF _textSize;
FunctionF _textMaxWidth; FunctionF _textMaxWidth;
FunctionF _textMaxAngle;
Qt::PenCapStyle _lineCap; Qt::PenCapStyle _lineCap;
Qt::PenJoinStyle _lineJoin; Qt::PenJoinStyle _lineJoin;
bool _capitalize; bool _capitalize;

View File

@ -14,23 +14,30 @@ static QPainterPath subpath(const QPainterPath &path, int start, int end)
return p; return p;
} }
static QList<QPainterPath> segments(const QPainterPath &path, qreal segmentLimit, static QList<QPainterPath> segments(const QPainterPath &path, qreal pathLimit,
qreal pathLimit) qreal maxAngle, qreal charWidth, const QRectF &tileRect)
{ {
QList<QPainterPath> list; QList<QPainterPath> list;
int start = 0; int start = 0;
qreal length = 0; qreal length = 0;
qreal angle = -1;
for (int i = 1; i < path.elementCount(); i++) { for (int i = 1; i < path.elementCount(); i++) {
QLineF l(path.elementAt(i-1), path.elementAt(i)); QLineF l(path.elementAt(i-1), path.elementAt(i));
qreal a = l.angle();
qreal sl = l.length(); qreal sl = l.length();
if (sl < segmentLimit || length > pathLimit) { if (angle < 0)
angle = a;
if (!tileRect.contains(path.elementAt(i))
|| sl < charWidth || qAbs(angle - a) > maxAngle
|| length > pathLimit) {
if (length > pathLimit) if (length > pathLimit)
list.append(subpath(path, start, i - 1)); list.append(subpath(path, start, i - 1));
start = i; start = i;
length = 0; length = 0;
} else } else
length += sl; length += sl;
angle = a;
} }
if (length > pathLimit) if (length > pathLimit)
@ -51,6 +58,8 @@ void Text::addLabel(const QString &text, const QPointF &pos, const QFont &font,
{ {
if (text.isEmpty()) if (text.isEmpty())
return; return;
if (!sceneRect().contains(pos))
return;
TextItem *ti = new TextItem(text, pos, font, maxTextWidth); TextItem *ti = new TextItem(text, pos, font, maxTextWidth);
addItem(ti); addItem(ti);
@ -67,7 +76,7 @@ void Text::addLabel(const QString &text, const QPointF &pos, const QFont &font,
} }
void Text::addLabel(const QString &text, const QPainterPath &path, void Text::addLabel(const QString &text, const QPainterPath &path,
const QFont &font, const QPen &pen) const QFont &font, const QPen &pen, qreal maxAngle)
{ {
if (path.elementCount() < 2 || !path.elementAt(0).isMoveTo()) if (path.elementCount() < 2 || !path.elementAt(0).isMoveTo())
return; return;
@ -80,7 +89,8 @@ void Text::addLabel(const QString &text, const QPainterPath &path,
if (textWidth > path.length()) if (textWidth > path.length())
return; return;
QList<QPainterPath> list(segments(path, fm.width('M'), textWidth)); QList<QPainterPath> list(segments(path, textWidth, maxAngle,
fm.averageCharWidth(), sceneRect()));
for (int i = 0; i < list.size(); i++) { for (int i = 0; i < list.size(); i++) {
const QPainterPath &segment = list.at(i); const QPainterPath &segment = list.at(i);
TextPathItem *pi = new TextPathItem(text, reverse(segment) TextPathItem *pi = new TextPathItem(text, reverse(segment)

View File

@ -12,7 +12,7 @@ public:
void addLabel(const QString &text, const QPointF &pos, const QFont &font, void addLabel(const QString &text, const QPointF &pos, const QFont &font,
const QPen &pen, qreal maxTextWidth); const QPen &pen, qreal maxTextWidth);
void addLabel(const QString &text, const QPainterPath &path, void addLabel(const QString &text, const QPainterPath &path,
const QFont &font, const QPen &pen); const QFont &font, const QPen &pen, qreal maxAngle);
}; };
#endif // TEXT_H #endif // TEXT_H