Compare commits

..

No commits in common. "aeed3e3848891622e879eab0f22b8aebd91d69f2" and "59545622003ac8065e768b2a517182ee08b78341" have entirely different histories.

8 changed files with 59 additions and 125 deletions

View File

@ -11,15 +11,13 @@ jobs:
runs-on: macos-latest runs-on: macos-latest
steps: steps:
- name: Set environment variables - name: Set environment variables
run: echo "PATH=/opt/homebrew/opt/qt@5/bin:/opt/homebrew/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV run: echo "PATH=/usr/local/opt/qt@5/bin:/usr/local/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install dependencies - name: Install dependencies
run: | run: brew install qt@5 protobuf@21
brew update
brew install qt@5 protobuf@21
- name: Configure build - name: Configure build
run: qmake PROTOBUF=/opt/homebrew/opt/protobuf@21 pbfplugin.pro run: qmake PROTOBUF=/usr/local/opt/protobuf@21 pbfplugin.pro
- name: Build project - name: Build project
run: make -j3 run: make -j3
@ -28,14 +26,12 @@ jobs:
runs-on: macos-latest runs-on: macos-latest
steps: steps:
- name: Set environment variables - name: Set environment variables
run: echo "PATH=/opt/homebrew/opt/qt@6/bin:/opt/homebrew/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV run: echo "PATH=/usr/local/opt/qt@6/bin:/usr/local/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
- name: Checkout - name: Checkout
uses: actions/checkout@v4 uses: actions/checkout@v4
- name: Install dependencies - name: Install dependencies
run: | run: brew install qt@6 protobuf@21
brew update
brew install qt@6 protobuf@21
- name: Configure build - name: Configure build
run: qmake PROTOBUF=/opt/homebrew/opt/protobuf@21 pbfplugin.pro run: qmake PROTOBUF=/usr/local/opt/protobuf@21 pbfplugin.pro
- name: Build project - name: Build project
run: make -j3 run: make -j3

View File

@ -11,7 +11,7 @@ class Style;
class PBFHandler : public QImageIOHandler class PBFHandler : public QImageIOHandler
{ {
public: public:
PBFHandler(Style *style) : _style(style) {} PBFHandler(const Style *style) : _style(style) {}
~PBFHandler() {} ~PBFHandler() {}
bool canRead() const; bool canRead() const;
@ -24,7 +24,7 @@ public:
static bool canRead(QIODevice *device); static bool canRead(QIODevice *device);
private: private:
Style *_style; const Style *_style;
QSize _scaledSize; QSize _scaledSize;
}; };

View File

@ -4,20 +4,14 @@
#include <QDebug> #include <QDebug>
#include "sprites.h" #include "sprites.h"
static QImage sdf2img(const QImage &sdf, const QColor &color)
/*
Loading the sprites atlas image must be deferred until all image plugins
are loaded, otherwise reading the image will cause a deadlock!
*/
static const QImage &atlas(const QString &fileName)
{ {
QImage img(sdf.convertToFormat(QImage::Format_ARGB32)); static QImage img(fileName);
quint32 argb = color.rgba();
uchar *bits = img.bits();
int bpl = img.bytesPerLine();
for (int y = 0; y < img.height(); y++) {
for (int x = 0; x < img.width(); x++) {
quint32 *pixel = (quint32*)(bits + y * bpl + x * 4);
*pixel = ((*pixel >> 24) < 192) ? 0 : argb;
}
}
return img; return img;
} }
@ -25,6 +19,7 @@ Sprites::Sprite::Sprite(const QJsonObject &json)
{ {
int x, y, width, height; int x, y, width, height;
if (json.contains("x") && json["x"].isDouble()) if (json.contains("x") && json["x"].isDouble())
x = json["x"].toInt(); x = json["x"].toInt();
else else
@ -49,15 +44,12 @@ Sprites::Sprite::Sprite(const QJsonObject &json)
_pixelRatio = json["pixelRatio"].toDouble(); _pixelRatio = json["pixelRatio"].toDouble();
else else
_pixelRatio = 1.0; _pixelRatio = 1.0;
if (json.contains("sdf") && json["sdf"].isBool())
_sdf = json["sdf"].toBool();
else
_sdf = false;
} }
bool Sprites::load(const QString &jsonFile, const QString &imageFile) bool Sprites::load(const QString &jsonFile, const QString &imageFile)
{ {
_imageFile = imageFile;
QFile file(jsonFile); QFile file(jsonFile);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) { if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qCritical() << jsonFile << ": error opening file"; qCritical() << jsonFile << ": error opening file";
@ -86,60 +78,27 @@ bool Sprites::load(const QString &jsonFile, const QString &imageFile)
qWarning() << it.key() << ": invalid sprite definition"; qWarning() << it.key() << ": invalid sprite definition";
} }
// Loading the sprites atlas image must be deferred until all image plugins
// are loaded, otherwise reading the image will cause a deadlock in Qt!
_imageFile = imageFile;
return true; return true;
} }
QImage Sprites::sprite(const Sprite &sprite, const QColor &color, qreal scale) QImage Sprites::icon(const QString &name) const
{ {
if (!_img.rect().contains(sprite.rect())) if (_imageFile.isEmpty())
return QImage(); return QImage();
QImage img(_img.copy(sprite.rect())); const QImage &img = atlas(_imageFile);
img.setDevicePixelRatio(sprite.pixelRatio()); if (img.isNull())
if (sprite.sdf()) {
if (scale != 1.0) {
QSize size(img.size().width() * scale, img.size().height() * scale);
QImage simg(img.scaled(size, Qt::IgnoreAspectRatio,
Qt::SmoothTransformation));
return sdf2img(simg, color);
} else
return sdf2img(img, color);
} else
return img;
}
QImage Sprites::icon(const QString &name, const QColor &color, qreal size)
{
if (name.isNull())
return QImage(); return QImage();
_lock.lock(); QMap<QString, Sprite>::const_iterator it = _sprites.find(name);
if (_init <= 0) {
if (_init < 0) {
_lock.unlock();
return QImage();
}
_img = QImage(_imageFile);
if (_img.isNull()) {
qWarning() << _imageFile << ": invalid sprite atlas image";
_init = -1;
_lock.unlock();
return QImage();
}
_init = 1;
}
_lock.unlock();
QMap<QString, Sprite>::const_iterator it = _sprites.constFind(name);
if (it == _sprites.constEnd()) if (it == _sprites.constEnd())
return QImage(); return QImage();
return sprite(*it, color, size); if (!img.rect().contains(it->rect()))
return QImage();
QImage ret(img.copy(it->rect()));
ret.setDevicePixelRatio(it->pixelRatio());
return ret;
} }

View File

@ -5,19 +5,16 @@
#include <QMap> #include <QMap>
#include <QImage> #include <QImage>
#include <QString> #include <QString>
#include <QMutex>
class QJsonObject; class QJsonObject;
class Sprites class Sprites
{ {
public: public:
Sprites() : _init(0) {}
bool load(const QString &jsonFile, const QString &imageFile); bool load(const QString &jsonFile, const QString &imageFile);
bool isNull() const {return _imageFile.isNull();} bool isNull() const {return _imageFile.isNull();}
QImage icon(const QString &name, const QColor &color = Qt::black, QImage icon(const QString &name) const;
qreal size = 1.0);
private: private:
class Sprite { class Sprite {
@ -26,21 +23,14 @@ private:
const QRect &rect() const {return _rect;} const QRect &rect() const {return _rect;}
qreal pixelRatio() const {return _pixelRatio;} qreal pixelRatio() const {return _pixelRatio;}
bool sdf() const {return _sdf;}
private: private:
QRect _rect; QRect _rect;
qreal _pixelRatio; qreal _pixelRatio;
bool _sdf;
}; };
QImage sprite(const Sprite &sprite, const QColor &color, qreal scale);
QMap<QString, Sprite> _sprites; QMap<QString, Sprite> _sprites;
QImage _img;
QMutex _lock;
QString _imageFile; QString _imageFile;
int _init;
}; };
#endif // SPRITES_H #endif // SPRITES_H

View File

@ -257,9 +257,6 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
_textHaloColor = FunctionC(json["text-halo-color"], QColor()); _textHaloColor = FunctionC(json["text-halo-color"], QColor());
_textHaloWidth = FunctionF(json["text-halo-width"]); _textHaloWidth = FunctionF(json["text-halo-width"]);
_textHaloBlur = FunctionF(json["text-halo-blur"]); _textHaloBlur = FunctionF(json["text-halo-blur"]);
// icon
_iconColor = FunctionC(json["icon-color"]);
} }
QPen Style::Layer::Paint::pen(Type type, int zoom) const QPen Style::Layer::Paint::pen(Type type, int zoom) const
@ -295,7 +292,7 @@ QPen Style::Layer::Paint::pen(Type type, int zoom) const
return pen; return pen;
} }
QBrush Style::Layer::Paint::brush(Type type, int zoom, Sprites &sprites) QBrush Style::Layer::Paint::brush(Type type, int zoom, const Sprites &sprites)
const const
{ {
QColor color; QColor color;
@ -372,7 +369,6 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
// icon // icon
_icon = Template(FunctionS(json["icon-image"])); _icon = Template(FunctionS(json["icon-image"]));
_iconSize = FunctionF(json["icon-size"]);
// symbol // symbol
_symbolPlacement = FunctionS(json["symbol-placement"]); _symbolPlacement = FunctionS(json["symbol-placement"]);
@ -515,7 +511,7 @@ bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
return _filter.match(feature); return _filter.match(feature);
} }
void Style::Layer::setPathPainter(Tile &tile, Sprites &sprites) const void Style::Layer::setPathPainter(Tile &tile, const Sprites &sprites) const
{ {
QPainter &p = tile.painter(); QPainter &p = tile.painter();
int zoom = tile.zoom(); int zoom = tile.zoom();
@ -548,18 +544,14 @@ void Style::Layer::setTextProperties(Tile &tile) const
} }
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path, void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
const PBF::Feature &feature, Sprites &sprites) const const PBF::Feature &feature, const Sprites &sprites) const
{ {
QString text(_layout.text(tile.zoom(), feature)); QString text(_layout.text(tile.zoom(), feature));
QString icon(_layout.icon(tile.zoom(), feature)); if (text.isEmpty())
QColor color(_paint.iconColor(tile.zoom()));
qreal size(_layout.iconSize(tile.zoom()));
QImage img(sprites.icon(icon, color, size));
if (text.isEmpty() && img.isNull())
return; return;
tile.text().addLabel(text, img, path); QString icon(_layout.icon(tile.zoom(), feature));
tile.text().addLabel(text, sprites.icon(icon), path);
} }
static bool loadSprites(const QDir &styleDir, const QString &json, static bool loadSprites(const QDir &styleDir, const QString &json,
@ -612,13 +604,13 @@ bool Style::load(const QString &fileName)
return true; return true;
} }
Sprites &Style::sprites(const QPointF &scale) const Sprites &Style::sprites(const QPointF &scale) const
{ {
return (scale.x() > 1.0 || scale.y() > 1.0) return (scale.x() > 1.0 || scale.y() > 1.0)
&& !_sprites2x.isNull() ? _sprites2x : _sprites; && !_sprites2x.isNull() ? _sprites2x : _sprites;
} }
void Style::setupLayer(Tile &tile, const Layer &layer) void Style::setupLayer(Tile &tile, const Layer &layer) const
{ {
if (layer.isSymbol()) if (layer.isSymbol())
layer.setTextProperties(tile); layer.setTextProperties(tile);
@ -626,7 +618,7 @@ void Style::setupLayer(Tile &tile, const Layer &layer)
layer.setPathPainter(tile, sprites(tile.scale())); layer.setPathPainter(tile, sprites(tile.scale()));
} }
void Style::drawBackground(Tile &tile) void Style::drawBackground(Tile &tile) const
{ {
QRectF rect(QPointF(0, 0), QSizeF(tile.size().width() / tile.scale().x(), QRectF rect(QPointF(0, 0), QSizeF(tile.size().width() / tile.scale().x(),
tile.size().height() / tile.scale().y())); tile.size().height() / tile.scale().y()));
@ -644,7 +636,7 @@ void Style::drawBackground(Tile &tile)
} }
void Style::drawFeature(const PBF::Feature &feature, const Layer &layer, void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
Tile &tile, const QSizeF &factor) Tile &tile, const QSizeF &factor) const
{ {
if (!layer.match(tile.zoom(), feature)) if (!layer.match(tile.zoom(), feature))
return; return;
@ -660,7 +652,7 @@ void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
} }
void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer, void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
Tile &tile) Tile &tile) const
{ {
if (pbfLayer.data()->version() > 2) if (pbfLayer.data()->version() > 2)
return; return;
@ -679,7 +671,7 @@ void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
tile.painter().restore(); tile.painter().restore();
} }
void Style::render(const PBF &data, Tile &tile) void Style::render(const PBF &data, Tile &tile) const
{ {
drawBackground(tile); drawBackground(tile);

View File

@ -29,7 +29,7 @@ public:
Style(QObject *parent = 0) : QObject(parent) {} Style(QObject *parent = 0) : QObject(parent) {}
bool load(const QString &fileName); bool load(const QString &fileName);
void render(const PBF &data, Tile &tile); void render(const PBF &data, Tile &tile) const;
private: private:
class Layer { class Layer {
@ -44,10 +44,10 @@ private:
bool isVisible() const {return (_layout.visible());} bool isVisible() const {return (_layout.visible());}
bool match(int zoom, const PBF::Feature &feature) const; bool match(int zoom, const PBF::Feature &feature) const;
void setPathPainter(Tile &tile, Sprites &sprites) const; void setPathPainter(Tile &tile, const Sprites &sprites) const;
void setTextProperties(Tile &tile) const; void setTextProperties(Tile &tile) const;
void addSymbol(Tile &tile, const QPainterPath &path, void addSymbol(Tile &tile, const QPainterPath &path,
const PBF::Feature &feature, Sprites &sprites) const; const PBF::Feature &feature, const Sprites &sprites) const;
private: private:
enum Type { enum Type {
@ -103,8 +103,6 @@ private:
{return _text.value(zoom, feature).trimmed();} {return _text.value(zoom, feature).trimmed();}
QString icon(int zoom, const PBF::Feature &feature) const QString icon(int zoom, const PBF::Feature &feature) const
{return _icon.value(zoom, feature);} {return _icon.value(zoom, feature);}
qreal iconSize(int zoom) const
{return _iconSize.value(zoom);}
QFont font(int zoom) const; QFont font(int zoom) const;
Qt::PenCapStyle lineCap(int zoom) const; Qt::PenCapStyle lineCap(int zoom) const;
Qt::PenJoinStyle lineJoin(int zoom) const; Qt::PenJoinStyle lineJoin(int zoom) const;
@ -118,7 +116,6 @@ private:
Template _text; Template _text;
Template _icon; Template _icon;
FunctionF _iconSize;
FunctionF _textSize; FunctionF _textSize;
FunctionF _textMaxWidth; FunctionF _textMaxWidth;
FunctionF _textMaxAngle; FunctionF _textMaxAngle;
@ -138,15 +135,13 @@ private:
Paint(const QJsonObject &json); Paint(const QJsonObject &json);
QPen pen(Layer::Type type, int zoom) const; QPen pen(Layer::Type type, int zoom) const;
QBrush brush(Layer::Type type, int zoom, Sprites &sprites) QBrush brush(Layer::Type type, int zoom, const Sprites &sprites)
const; const;
qreal opacity(Layer::Type type, int zoom) const; qreal opacity(Layer::Type type, int zoom) const;
bool antialias(Layer::Type type, int zoom) const; bool antialias(Layer::Type type, int zoom) const;
Text::Halo halo(int zoom) const Text::Halo halo(int zoom) const
{return Text::Halo(_textHaloColor.value(zoom), {return Text::Halo(_textHaloColor.value(zoom),
_textHaloWidth.value(zoom), _textHaloBlur.value(zoom));} _textHaloWidth.value(zoom), _textHaloBlur.value(zoom));}
QColor iconColor(int zoom) const
{return _iconColor.value(zoom);}
private: private:
FunctionC _textColor; FunctionC _textColor;
@ -155,7 +150,6 @@ private:
FunctionC _fillColor; FunctionC _fillColor;
FunctionC _fillOutlineColor; FunctionC _fillOutlineColor;
FunctionC _backgroundColor; FunctionC _backgroundColor;
FunctionC _iconColor;
FunctionF _fillOpacity; FunctionF _fillOpacity;
FunctionF _lineOpacity; FunctionF _lineOpacity;
FunctionF _lineWidth; FunctionF _lineWidth;
@ -174,14 +168,14 @@ private:
Paint _paint; Paint _paint;
}; };
Sprites &sprites(const QPointF &scale); const Sprites &sprites(const QPointF &scale) const;
void drawBackground(Tile &tile); void drawBackground(Tile &tile) const;
void setupLayer(Tile &tile, const Layer &layer); void setupLayer(Tile &tile, const Layer &layer) const;
void drawFeature(const PBF::Feature &feature, const Layer &layer, void drawFeature(const PBF::Feature &feature, const Layer &layer,
Tile &tile, const QSizeF &factor); Tile &tile, const QSizeF &factor) const;
void drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer, void drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
Tile &tile); Tile &tile) const;
QVector<Layer> _layers; QVector<Layer> _layers;
Sprites _sprites, _sprites2x; Sprites _sprites, _sprites2x;

View File

@ -14,10 +14,15 @@ Text::~Text()
void Text::render(QPainter *painter) const void Text::render(QPainter *painter) const
{ {
QSet<QString> set;
for (int i = 0; i < _items.size(); i++) { for (int i = 0; i < _items.size(); i++) {
const TextItem *ti = _items.at(i); const TextItem *ti = _items.at(i);
if (ti->isVisible() && _sceneRect.intersects(ti->boundingRect())) if (ti->isVisible() && _sceneRect.intersects(ti->boundingRect())
&& !set.contains(ti->text())) {
ti->paint(painter); ti->paint(painter);
set.insert(ti->text());
}
} }
} }
@ -46,8 +51,6 @@ void Text::addLabel(const QString &text, const QImage &icon,
} else { } else {
switch (_placement) { switch (_placement) {
case Line: case Line:
if (text.isEmpty())
return;
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect); ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
break; break;
case LineCenter: case LineCenter:

View File

@ -85,7 +85,7 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth), : TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
_anchor(anchor) _anchor(anchor)
{ {
_textRect = text.isEmpty() ? QRectF() : fuzzyBoundingRect(); _textRect = fuzzyBoundingRect();
_boundingRect = moveTextRect(_textRect); _boundingRect = moveTextRect(_textRect);
if (!_icon.isNull()) { if (!_icon.isNull()) {