mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2024-11-24 03:35:54 +01:00
Text rendering algorithm improvements
This commit is contained in:
parent
f0a8b5c05e
commit
a4089f9a29
@ -22,9 +22,9 @@ private:
|
|||||||
|
|
||||||
class FunctionC {
|
class FunctionC {
|
||||||
public:
|
public:
|
||||||
FunctionC(const QColor &deflt = QColor())
|
FunctionC(const QColor &deflt = QColor(Qt::black))
|
||||||
: _default(deflt), _base(1.0) {}
|
: _default(deflt), _base(1.0) {}
|
||||||
FunctionC(const QJsonObject &json, const QColor &dflt = QColor());
|
FunctionC(const QJsonObject &json, const QColor &dflt = QColor(Qt::black));
|
||||||
|
|
||||||
QColor value(qreal x) const;
|
QColor value(qreal x) const;
|
||||||
|
|
||||||
|
@ -141,11 +141,14 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
|
|||||||
_fillOpacity = FunctionF(json["fill-opacity"].toDouble());
|
_fillOpacity = FunctionF(json["fill-opacity"].toDouble());
|
||||||
else if (json.contains("fill-opacity") && json["fill-opacity"].isObject())
|
else if (json.contains("fill-opacity") && json["fill-opacity"].isObject())
|
||||||
_fillOpacity = FunctionF(json["fill-opacity"].toObject());
|
_fillOpacity = FunctionF(json["fill-opacity"].toObject());
|
||||||
if (json.contains("fill-color") && json["fill-color"].isString())
|
if (json.contains("fill-color") && json["fill-color"].isString()) {
|
||||||
_fillColor = FunctionC(Color::fromJsonString(
|
_fillColor = FunctionC(Color::fromJsonString(
|
||||||
json["fill-color"].toString()));
|
json["fill-color"].toString()));
|
||||||
if (json.contains("fill-color") && json["fill-color"].isObject())
|
_fillOutlineColor = _fillColor;
|
||||||
|
} else if (json.contains("fill-color") && json["fill-color"].isObject()) {
|
||||||
_fillColor = FunctionC(json["fill-color"].toObject());
|
_fillColor = FunctionC(json["fill-color"].toObject());
|
||||||
|
_fillOutlineColor = _fillColor;
|
||||||
|
}
|
||||||
if (json.contains("fill-outline-color")
|
if (json.contains("fill-outline-color")
|
||||||
&& json["fill-outline-color"].isString())
|
&& json["fill-outline-color"].isString())
|
||||||
_fillOutlineColor = FunctionC(Color::fromJsonString(
|
_fillOutlineColor = FunctionC(Color::fromJsonString(
|
||||||
@ -155,6 +158,11 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
|
|||||||
_fillOutlineColor = FunctionC(json["fill-outline-color"].toObject());
|
_fillOutlineColor = FunctionC(json["fill-outline-color"].toObject());
|
||||||
if (json.contains("fill-antialias") && json["fill-antialias"].isBool())
|
if (json.contains("fill-antialias") && json["fill-antialias"].isBool())
|
||||||
_fillAntialias = json["fill-antialias"].toBool();
|
_fillAntialias = json["fill-antialias"].toBool();
|
||||||
|
if (json.contains("fill-pattern")) {
|
||||||
|
_fillColor = FunctionC(QColor());
|
||||||
|
_fillOutlineColor = FunctionC(QColor());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
if (json.contains("line-color") && json["line-color"].isString())
|
if (json.contains("line-color") && json["line-color"].isString())
|
||||||
_lineColor = FunctionC(Color::fromJsonString(json["line-color"]
|
_lineColor = FunctionC(Color::fromJsonString(json["line-color"]
|
||||||
@ -208,11 +216,6 @@ QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
|||||||
color = _fillOutlineColor.value(zoom);
|
color = _fillOutlineColor.value(zoom);
|
||||||
if (color.isValid())
|
if (color.isValid())
|
||||||
pen = QPen(color);
|
pen = QPen(color);
|
||||||
else {
|
|
||||||
color = _fillColor.value(zoom);
|
|
||||||
if (color.isValid())
|
|
||||||
pen = QPen(color);
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
case Symbol:
|
case Symbol:
|
||||||
color = _textColor.value(zoom);
|
color = _textColor.value(zoom);
|
||||||
@ -267,8 +270,8 @@ 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), _textMaxAngle(45), _lineCap(Qt::FlatCap),
|
: _textSize(16), _textMaxWidth(10), _textMaxAngle(45), _symbolSpacing(250),
|
||||||
_lineJoin(Qt::MiterJoin), _capitalize(false)
|
_lineCap(Qt::FlatCap), _lineJoin(Qt::MiterJoin), _capitalize(false)
|
||||||
{
|
{
|
||||||
if (!(json.contains("text-field") && json["text-field"].isString()))
|
if (!(json.contains("text-field") && json["text-field"].isString()))
|
||||||
return;
|
return;
|
||||||
@ -296,6 +299,11 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
|
|||||||
else if (json.contains("text-max-angle") && json["text-max-angle"].isDouble())
|
else if (json.contains("text-max-angle") && json["text-max-angle"].isDouble())
|
||||||
_textMaxWidth = json["text-max-angle"].toDouble();
|
_textMaxWidth = json["text-max-angle"].toDouble();
|
||||||
|
|
||||||
|
if (json.contains("symbol-spacing") && json["symbol-spacing"].isObject())
|
||||||
|
_symbolSpacing = json["symbol-spacing"].toObject();
|
||||||
|
else if (json.contains("symbol-spacing") && json["symbol-spacing"].isDouble())
|
||||||
|
_symbolSpacing = json["symbol-spacing"].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";
|
||||||
|
|
||||||
@ -409,7 +417,7 @@ void Style::Layer::drawSymbol(int zoom, const QPainterPath &path,
|
|||||||
_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));
|
_layout.maxTextAngle(zoom), _layout.symbolSpacing(zoom));
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Style::load(const QString &fileName)
|
bool Style::load(const QString &fileName)
|
||||||
|
13
src/style.h
13
src/style.h
@ -79,13 +79,17 @@ private:
|
|||||||
class Layout {
|
class Layout {
|
||||||
public:
|
public:
|
||||||
Layout() : _textSize(16), _textMaxWidth(10), _textMaxAngle(45),
|
Layout() : _textSize(16), _textMaxWidth(10), _textMaxAngle(45),
|
||||||
_lineCap(Qt::FlatCap), _lineJoin(Qt::MiterJoin),
|
_symbolSpacing(250), _lineCap(Qt::FlatCap),
|
||||||
_capitalize(false) {}
|
_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
|
||||||
qreal maxTextAngle(int zoom) const {return _textMaxAngle.value(zoom);}
|
{return _textMaxWidth.value(zoom);}
|
||||||
|
qreal maxTextAngle(int zoom) const
|
||||||
|
{return _textMaxAngle.value(zoom);}
|
||||||
|
qreal symbolSpacing(int zoom) const
|
||||||
|
{return _symbolSpacing.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;
|
||||||
@ -98,6 +102,7 @@ private:
|
|||||||
FunctionF _textSize;
|
FunctionF _textSize;
|
||||||
FunctionF _textMaxWidth;
|
FunctionF _textMaxWidth;
|
||||||
FunctionF _textMaxAngle;
|
FunctionF _textMaxAngle;
|
||||||
|
FunctionF _symbolSpacing;
|
||||||
Qt::PenCapStyle _lineCap;
|
Qt::PenCapStyle _lineCap;
|
||||||
Qt::PenJoinStyle _lineJoin;
|
Qt::PenJoinStyle _lineJoin;
|
||||||
bool _capitalize;
|
bool _capitalize;
|
||||||
|
43
src/text.cpp
43
src/text.cpp
@ -6,43 +6,60 @@
|
|||||||
#include "textpathitem.h"
|
#include "textpathitem.h"
|
||||||
|
|
||||||
|
|
||||||
static QPainterPath subpath(const QPainterPath &path, int start, int end)
|
static QPainterPath subpath(const QPainterPath &path, int start, int end,
|
||||||
|
qreal cut)
|
||||||
{
|
{
|
||||||
QPainterPath p(path.elementAt(start));
|
QPainterPath p(path.elementAt(start));
|
||||||
for (int i = start + 1; i <= end; i++)
|
|
||||||
|
for (int i = start + 1; i < end; i++)
|
||||||
p.lineTo(path.elementAt(i));
|
p.lineTo(path.elementAt(i));
|
||||||
|
|
||||||
|
QLineF l(path.elementAt(end - 1), path.elementAt(end));
|
||||||
|
l.setLength(l.length() - cut);
|
||||||
|
p.lineTo(l.p2());
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QList<QPainterPath> segments(const QPainterPath &path, qreal pathLimit,
|
static QList<QPainterPath> segments(const QPainterPath &path, qreal textLength,
|
||||||
qreal maxAngle, qreal charWidth, const QRectF &tileRect)
|
qreal maxAngle, qreal charWidth, int symbolSpacing, const QRectF &tileRect)
|
||||||
{
|
{
|
||||||
QList<QPainterPath> list;
|
QList<QPainterPath> list;
|
||||||
int start = 0;
|
int start = 0;
|
||||||
qreal length = 0;
|
qreal length = 0;
|
||||||
qreal angle = -1;
|
qreal angle = -1;
|
||||||
|
qreal dist = symbolSpacing;
|
||||||
|
|
||||||
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 (dist < symbolSpacing) {
|
||||||
|
dist += sl;
|
||||||
|
start++;
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
qreal a = l.angle();
|
||||||
if (angle < 0)
|
if (angle < 0)
|
||||||
angle = a;
|
angle = a;
|
||||||
|
|
||||||
if (!tileRect.contains(path.elementAt(i))
|
if (!tileRect.contains(path.elementAt(i))
|
||||||
|| sl < charWidth || qAbs(angle - a) > maxAngle
|
|| sl < charWidth || qAbs(angle - a) > maxAngle
|
||||||
|| length > pathLimit) {
|
|| length > textLength) {
|
||||||
if (length > pathLimit)
|
if (length > textLength) {
|
||||||
list.append(subpath(path, start, i - 1));
|
list.append(subpath(path, start, i - 1, length - textLength - 1));
|
||||||
|
dist = 0;
|
||||||
|
}
|
||||||
start = i;
|
start = i;
|
||||||
length = 0;
|
length = 0;
|
||||||
} else
|
} else
|
||||||
length += sl;
|
length += sl;
|
||||||
|
|
||||||
|
dist += sl;
|
||||||
angle = a;
|
angle = a;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (length > pathLimit)
|
|
||||||
list.append(subpath(path, start, path.elementCount() - 1));
|
|
||||||
|
|
||||||
return list;
|
return list;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -76,7 +93,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, qreal maxAngle)
|
const QFont &font, const QPen &pen, qreal maxAngle, qreal symbolSpacing)
|
||||||
{
|
{
|
||||||
if (path.elementCount() < 2 || !path.elementAt(0).isMoveTo())
|
if (path.elementCount() < 2 || !path.elementAt(0).isMoveTo())
|
||||||
return;
|
return;
|
||||||
@ -90,7 +107,7 @@ void Text::addLabel(const QString &text, const QPainterPath &path,
|
|||||||
return;
|
return;
|
||||||
|
|
||||||
QList<QPainterPath> list(segments(path, textWidth, maxAngle,
|
QList<QPainterPath> list(segments(path, textWidth, maxAngle,
|
||||||
fm.averageCharWidth(), sceneRect()));
|
fm.averageCharWidth(), symbolSpacing, 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)
|
||||||
|
@ -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, qreal maxAngle);
|
const QFont &font, const QPen &pen, qreal maxAngle, qreal symbolSpacing);
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // TEXT_H
|
#endif // TEXT_H
|
||||||
|
Loading…
Reference in New Issue
Block a user