mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2024-11-24 03:35:54 +01:00
Added support for the text-rotation-alignment style property
Code cleanup
This commit is contained in:
parent
df073ffc2a
commit
c48ea8c878
@ -274,7 +274,8 @@ bool Style::Layer::Paint::antialias(Layer::Type type, int zoom) 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), _lineCap(Qt::FlatCap),
|
||||||
_lineJoin(Qt::MiterJoin), _font("Open Sans"), _capitalize(false)
|
_lineJoin(Qt::MiterJoin), _font("Open Sans"), _capitalize(false),
|
||||||
|
_viewportAlignment(false)
|
||||||
{
|
{
|
||||||
// line
|
// line
|
||||||
if (json.contains("line-cap") && json["line-cap"].isString()) {
|
if (json.contains("line-cap") && json["line-cap"].isString()) {
|
||||||
@ -310,6 +311,11 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
|
|||||||
_font = Font::fromJsonArray(json["text-font"].toArray());
|
_font = Font::fromJsonArray(json["text-font"].toArray());
|
||||||
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";
|
||||||
|
|
||||||
|
if (json.contains("text-rotation-alignment")
|
||||||
|
&& json["text-rotation-alignment"].isString())
|
||||||
|
if (json["text-rotation-alignment"].toString() == "viewport")
|
||||||
|
_viewportAlignment = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QFont Style::Layer::Layout::font(int zoom) const
|
QFont Style::Layer::Layout::font(int zoom) const
|
||||||
@ -411,9 +417,12 @@ void Style::Layer::addSymbol(int zoom, const QPainterPath &path,
|
|||||||
if (tt.isEmpty())
|
if (tt.isEmpty())
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (path.elementCount() == 1 && path.elementAt(0).isMoveTo())
|
if (_layout.viewportAlignment())
|
||||||
tile.text().addLabel(tt, path.elementAt(0), tile.painter(),
|
tile.text().addLabel(tt, path.elementAt(0), tile.painter(),
|
||||||
_layout.maxTextWidth(zoom));
|
_layout.maxTextWidth(zoom), false);
|
||||||
|
else if (path.elementCount() == 1 && path.elementAt(0).isMoveTo())
|
||||||
|
tile.text().addLabel(tt, path.elementAt(0), tile.painter(),
|
||||||
|
_layout.maxTextWidth(zoom), true);
|
||||||
else
|
else
|
||||||
tile.text().addLabel(tt, path, tile.painter(),
|
tile.text().addLabel(tt, path, tile.painter(),
|
||||||
_layout.maxTextAngle(zoom));
|
_layout.maxTextAngle(zoom));
|
||||||
|
@ -84,7 +84,8 @@ private:
|
|||||||
public:
|
public:
|
||||||
Layout() : _textSize(16), _textMaxWidth(10), _textMaxAngle(45),
|
Layout() : _textSize(16), _textMaxWidth(10), _textMaxAngle(45),
|
||||||
_lineCap(Qt::FlatCap), _lineJoin(Qt::MiterJoin),
|
_lineCap(Qt::FlatCap), _lineJoin(Qt::MiterJoin),
|
||||||
_font("Open Sans"), _capitalize(false) {}
|
_font("Open Sans"), _capitalize(false), _viewportAlignment(false)
|
||||||
|
{}
|
||||||
Layout(const QJsonObject &json);
|
Layout(const QJsonObject &json);
|
||||||
|
|
||||||
bool capitalize() const {return _capitalize;}
|
bool capitalize() const {return _capitalize;}
|
||||||
@ -97,6 +98,7 @@ private:
|
|||||||
QFont font(int zoom) const;
|
QFont font(int zoom) const;
|
||||||
Qt::PenCapStyle lineCap() const {return _lineCap;}
|
Qt::PenCapStyle lineCap() const {return _lineCap;}
|
||||||
Qt::PenJoinStyle lineJoin() const {return _lineJoin;}
|
Qt::PenJoinStyle lineJoin() const {return _lineJoin;}
|
||||||
|
bool viewportAlignment() const {return _viewportAlignment;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QStringList _keys;
|
QStringList _keys;
|
||||||
@ -108,6 +110,7 @@ private:
|
|||||||
Qt::PenJoinStyle _lineJoin;
|
Qt::PenJoinStyle _lineJoin;
|
||||||
QFont _font;
|
QFont _font;
|
||||||
bool _capitalize;
|
bool _capitalize;
|
||||||
|
bool _viewportAlignment;
|
||||||
};
|
};
|
||||||
|
|
||||||
class Paint {
|
class Paint {
|
||||||
|
@ -150,7 +150,7 @@ void Text::render(QPainter *painter) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Text::addLabel(const QString &text, const QPointF &pos,
|
void Text::addLabel(const QString &text, const QPointF &pos,
|
||||||
const QPainter &painter, qreal maxTextWidth)
|
const QPainter &painter, qreal maxTextWidth, bool overlap)
|
||||||
{
|
{
|
||||||
if (text.isEmpty())
|
if (text.isEmpty())
|
||||||
return;
|
return;
|
||||||
@ -158,6 +158,10 @@ void Text::addLabel(const QString &text, const QPointF &pos,
|
|||||||
TextPointItem *ti;
|
TextPointItem *ti;
|
||||||
|
|
||||||
ti = new TextPointItem(text, pos, painter.font(), maxTextWidth);
|
ti = new TextPointItem(text, pos, painter.font(), maxTextWidth);
|
||||||
|
if (!overlap && !_sceneRect.contains(ti->boundingRect())) {
|
||||||
|
delete ti;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ti->setPen(painter.pen());
|
ti->setPen(painter.pen());
|
||||||
addItem(ti);
|
addItem(ti);
|
||||||
|
@ -12,7 +12,7 @@ public:
|
|||||||
void render(QPainter *painter) const;
|
void render(QPainter *painter) const;
|
||||||
|
|
||||||
void addLabel(const QString &text, const QPointF &pos,
|
void addLabel(const QString &text, const QPointF &pos,
|
||||||
const QPainter &painter, qreal maxTextWidth);
|
const QPainter &painter, qreal maxTextWidth, bool overlap);
|
||||||
void addLabel(const QString &text, const QPainterPath &path,
|
void addLabel(const QString &text, const QPainterPath &path,
|
||||||
const QPainter &painter, qreal maxAngle);
|
const QPainter &painter, qreal maxAngle);
|
||||||
|
|
||||||
|
@ -6,9 +6,11 @@
|
|||||||
class TextItem
|
class TextItem
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
TextItem() : _visible(true) {}
|
TextItem(const QString &text) : _text(text), _visible(true) {}
|
||||||
virtual ~TextItem() {}
|
virtual ~TextItem() {}
|
||||||
|
|
||||||
|
const QString &text() const {return _text;}
|
||||||
|
|
||||||
virtual QPainterPath shape() const = 0;
|
virtual QPainterPath shape() const = 0;
|
||||||
virtual QRectF boundingRect() const = 0;
|
virtual QRectF boundingRect() const = 0;
|
||||||
virtual void paint(QPainter *painter) const = 0;
|
virtual void paint(QPainter *painter) const = 0;
|
||||||
@ -28,6 +30,7 @@ public:
|
|||||||
}
|
}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
QString _text;
|
||||||
bool _visible;
|
bool _visible;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,7 +4,7 @@
|
|||||||
|
|
||||||
|
|
||||||
TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
||||||
const QFont &font) : _text(text), _path(path), _font(font)
|
const QFont &font) : TextItem(text), _path(path), _font(font)
|
||||||
{
|
{
|
||||||
QPainterPathStroker s;
|
QPainterPathStroker s;
|
||||||
s.setWidth(font.pixelSize());
|
s.setWidth(font.pixelSize());
|
||||||
@ -16,7 +16,7 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
|||||||
void TextPathItem::paint(QPainter *painter) const
|
void TextPathItem::paint(QPainter *painter) const
|
||||||
{
|
{
|
||||||
QFontMetrics fm(_font);
|
QFontMetrics fm(_font);
|
||||||
int textWidth = fm.width(_text);
|
int textWidth = fm.width(text());
|
||||||
|
|
||||||
qreal factor = (textWidth) / qMax(_path.length(), (qreal)textWidth);
|
qreal factor = (textWidth) / qMax(_path.length(), (qreal)textWidth);
|
||||||
qreal percent = (1.0 - factor) / 2.0;
|
qreal percent = (1.0 - factor) / 2.0;
|
||||||
@ -26,16 +26,16 @@ void TextPathItem::paint(QPainter *painter) const
|
|||||||
|
|
||||||
QTransform t = painter->transform();
|
QTransform t = painter->transform();
|
||||||
|
|
||||||
for (int i = 0; i < _text.size(); i++) {
|
for (int i = 0; i < text().size(); i++) {
|
||||||
QPointF point = _path.pointAtPercent(percent);
|
QPointF point = _path.pointAtPercent(percent);
|
||||||
qreal angle = _path.angleAtPercent(percent);
|
qreal angle = _path.angleAtPercent(percent);
|
||||||
|
|
||||||
painter->translate(point);
|
painter->translate(point);
|
||||||
painter->rotate(-angle);
|
painter->rotate(-angle);
|
||||||
painter->drawText(QPoint(0, fm.descent()), _text.at(i));
|
painter->drawText(QPoint(0, fm.descent()), text().at(i));
|
||||||
painter->setTransform(t);
|
painter->setTransform(t);
|
||||||
|
|
||||||
int width = fm.charWidth(_text, i);
|
int width = fm.charWidth(text(), i);
|
||||||
percent += ((qreal)width / (qreal)textWidth) * factor;
|
percent += ((qreal)width / (qreal)textWidth) * factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,7 +18,6 @@ public:
|
|||||||
void setPen(const QPen &pen) {_pen = pen;}
|
void setPen(const QPen &pen) {_pen = pen;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString _text;
|
|
||||||
QPainterPath _path;
|
QPainterPath _path;
|
||||||
QPainterPath _shape;
|
QPainterPath _shape;
|
||||||
QRectF _boundingRect;
|
QRectF _boundingRect;
|
||||||
|
@ -5,7 +5,29 @@
|
|||||||
|
|
||||||
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
|
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
|
||||||
|
|
||||||
static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
|
#ifdef USE_EXACT_TEXT_RECT
|
||||||
|
|
||||||
|
static QRectF textBoundingRect(const QString &str, const QFont &font,
|
||||||
|
int maxTextWidth)
|
||||||
|
{
|
||||||
|
QFontMetrics fm(font);
|
||||||
|
int limit = font.pixelSize() * maxTextWidth;
|
||||||
|
// Italic fonts overflow the computed bounding rect, so reduce it
|
||||||
|
// a little bit.
|
||||||
|
if (font.italic())
|
||||||
|
limit -= font.pixelSize() / 2.0;
|
||||||
|
|
||||||
|
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, str);
|
||||||
|
Q_ASSERT(br.isValid());
|
||||||
|
// Expand the bounding rect back to the real content size
|
||||||
|
if (font.italic())
|
||||||
|
br.adjust(-font.pixelSize() / 4.0, 0, font.pixelSize() / 4.0, 0);
|
||||||
|
|
||||||
|
return br;
|
||||||
|
}
|
||||||
|
|
||||||
|
#else // USE_EXACT_TEXT_RECT
|
||||||
|
static QRectF textBoundingRect(const QString &str, const QFont &font,
|
||||||
int maxTextWidth)
|
int maxTextWidth)
|
||||||
{
|
{
|
||||||
int limit = font.pixelSize() * maxTextWidth;
|
int limit = font.pixelSize() * maxTextWidth;
|
||||||
@ -45,33 +67,12 @@ static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
|
|||||||
|
|
||||||
return QRectF(0, 0, width, lines * lh);
|
return QRectF(0, 0, width, lines * lh);
|
||||||
}
|
}
|
||||||
|
#endif // USE_EXACT_TEXT_RECT
|
||||||
/*
|
|
||||||
static QRectF exactBoundingRect(const QString &str, const QFont &font,
|
|
||||||
int maxTextWidth)
|
|
||||||
{
|
|
||||||
QFontMetrics fm(font);
|
|
||||||
int limit = font.pixelSize() * maxTextWidth;
|
|
||||||
// Italic fonts overflow the computed bounding rect, so reduce it
|
|
||||||
// a little bit.
|
|
||||||
if (font.italic())
|
|
||||||
limit -= font.pixelSize() / 2.0;
|
|
||||||
|
|
||||||
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, str);
|
|
||||||
Q_ASSERT(br.isValid());
|
|
||||||
// Expand the bounding rect back to the real content size
|
|
||||||
if (font.italic())
|
|
||||||
br.adjust(-font.pixelSize() / 4.0, 0, font.pixelSize() / 4.0, 0);
|
|
||||||
|
|
||||||
return br;
|
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
||||||
TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
||||||
const QFont &font, int maxTextWidth) :_text(text), _font(font)
|
const QFont &font, int maxTextWidth) : TextItem(text), _font(font)
|
||||||
{
|
{
|
||||||
_boundingRect = fuzzyBoundingRect(text, font, maxTextWidth);
|
_boundingRect = textBoundingRect(text, font, maxTextWidth);
|
||||||
//_boundingRect = exactBoundingRect(text, font, maxTextWidth);
|
|
||||||
|
|
||||||
_boundingRect.moveCenter(pos);
|
_boundingRect.moveCenter(pos);
|
||||||
_shape.addRect(_boundingRect);
|
_shape.addRect(_boundingRect);
|
||||||
@ -81,7 +82,7 @@ void TextPointItem::paint(QPainter *painter) const
|
|||||||
{
|
{
|
||||||
painter->setFont(_font);
|
painter->setFont(_font);
|
||||||
painter->setPen(_pen);
|
painter->setPen(_pen);
|
||||||
painter->drawText(_boundingRect, FLAGS, _text);
|
painter->drawText(_boundingRect, FLAGS, text());
|
||||||
|
|
||||||
//painter->setPen(Qt::red);
|
//painter->setPen(Qt::red);
|
||||||
//painter->drawRect(_boundingRect);
|
//painter->drawRect(_boundingRect);
|
||||||
|
@ -19,7 +19,6 @@ public:
|
|||||||
void setPen(const QPen &pen) {_pen = pen;}
|
void setPen(const QPen &pen) {_pen = pen;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString _text;
|
|
||||||
QPainterPath _shape;
|
QPainterPath _shape;
|
||||||
QRectF _boundingRect;
|
QRectF _boundingRect;
|
||||||
QFont _font;
|
QFont _font;
|
||||||
|
Loading…
Reference in New Issue
Block a user