Added support for text halo

This commit is contained in:
Martin Tůma 2019-04-27 22:52:18 +02:00
parent 5ccb93bd59
commit 3dfcf35e53
7 changed files with 68 additions and 0 deletions

View File

@ -230,6 +230,9 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
// text
_textColor = FunctionC(json["text-color"]);
_textHaloColor = FunctionC(json["text-halo-color"], QColor());
_textHaloWidth = FunctionF(json["text-halo-width"]);
_textHaloBlur = FunctionF(json["text-halo-blur"]);
}
QPen Style::Layer::Paint::pen(Type type, int zoom) const
@ -511,6 +514,7 @@ void Style::Layer::setTextProperties(Tile &tile) const
t.setFont(_layout.font(zoom));
t.setSymbolPlacement(_layout.symbolPlacement(zoom));
t.setRotationAlignment(_layout.textRotationAlignment(zoom));
t.setHalo(_paint.halo(zoom));
}
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,

View File

@ -137,9 +137,13 @@ private:
const;
qreal opacity(Layer::Type type, int zoom) const;
bool antialias(Layer::Type type, int zoom) const;
Text::Halo halo(int zoom) const
{return Text::Halo(_textHaloColor.value(zoom),
_textHaloWidth.value(zoom), _textHaloBlur.value(zoom));}
private:
FunctionC _textColor;
FunctionC _textHaloColor;
FunctionC _lineColor;
FunctionC _fillColor;
FunctionC _fillOutlineColor;
@ -147,6 +151,8 @@ private:
FunctionF _fillOpacity;
FunctionF _lineOpacity;
FunctionF _lineWidth;
FunctionF _textHaloWidth;
FunctionF _textHaloBlur;
FunctionB _fillAntialias;
QVector<qreal> _lineDasharray;
FunctionS _fillPattern;

View File

@ -56,6 +56,7 @@ void Text::addLabel(const QString &text, const QImage &icon,
}
ti->setPen(_pen);
ti->setHalo(_halo);
addItem(ti);
QList<TextItem*> ci = collidingItems(ti);

View File

@ -34,6 +34,22 @@ public:
Auto
};
class Halo {
public:
Halo() : _width(0), _blur(0) {}
Halo(const QColor &color, qreal width, qreal blur)
: _color(color), _width(width), _blur(blur) {}
const QColor &color() const {return _color;}
qreal width() const {return _width;}
qreal blur() const {return _blur;}
private:
QColor _color;
qreal _width;
qreal _blur;
};
Text(const QSize &size) : _sceneRect(QRectF(QPointF(0, 0), size)) {}
~Text();
@ -45,6 +61,7 @@ public:
void setSymbolPlacement(SymbolPlacement placement);
void setRotationAlignment(RotationAlignment alignment)
{_alignment = alignment;}
void setHalo(const Halo &halo) {_halo = halo;}
void addLabel(const QString &text, const QImage &icon,
const QPainterPath &path);
@ -65,6 +82,7 @@ private:
RotationAlignment _alignment;
QFont _font;
QPen _pen;
Halo _halo;
};
#endif // TEXT_H

View File

@ -6,6 +6,7 @@
#include <QPen>
#include <QFont>
#include <QRectF>
#include "text.h"
class QPainter;
@ -19,7 +20,9 @@ public:
const QString &text() const {return _text;}
const QFont &font() const {return _font;}
const QPen &pen() const {return _pen;}
const Text::Halo &halo() const {return _halo;}
void setPen(const QPen &pen) {_pen = pen;}
void setHalo(const Text::Halo &halo) {_halo = halo;}
virtual QPainterPath shape() const = 0;
virtual QRectF boundingRect() const = 0;
@ -37,6 +40,7 @@ private:
QString _text;
QFont _font;
QPen _pen;
Text::Halo _halo;
bool _visible;
};

View File

@ -176,6 +176,18 @@ void TextPathItem::paint(QPainter *painter) const
painter->translate(point);
painter->rotate(-angle);
if (halo().color().isValid() && halo().width() > 0) {
painter->setPen(halo().color());
painter->drawText(QPoint(-1, fm.descent() - 1), text().at(i));
painter->drawText(QPoint(1, fm.descent() + 1), text().at(i));
painter->drawText(QPoint(-1, fm.descent() + 1), text().at(i));
painter->drawText(QPoint(1, fm.descent() -1), text().at(i));
painter->drawText(QPoint(0, fm.descent() - 1), text().at(i));
painter->drawText(QPoint(0, fm.descent() + 1), text().at(i));
painter->drawText(QPoint(-1, fm.descent()), text().at(i));
painter->drawText(QPoint(1, fm.descent()), text().at(i));
painter->setPen(pen());
}
painter->drawText(QPoint(0, fm.descent()), text().at(i));
painter->setTransform(t);

View File

@ -144,6 +144,29 @@ void TextPointItem::paint(QPainter *painter) const
textRect = _boundingRect;
painter->setFont(font());
if (halo().color().isValid() && halo().width() > 0) {
QPointF center(textRect.center());
painter->setPen(halo().color());
textRect.moveCenter(QPointF(center.x() - 1, center.y() - 1));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(QPointF(center.x() + 1, center.y() + 1));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(QPointF(center.x() - 1, center.y() + 1));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(QPointF(center.x() + 1, center.y() - 1));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(QPointF(center.x(), center.y() - 1));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(QPointF(center.x(), center.y() + 1));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(QPointF(center.x() - 1, center.y()));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(QPointF(center.x() + 1, center.y()));
painter->drawText(textRect, FLAGS, text());
textRect.moveCenter(center);
}
painter->setPen(pen());
painter->drawText(textRect, FLAGS, text());
}