mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2025-06-30 21:19:16 +02:00
Compare commits
35 Commits
c86eb7bac2
...
3.2
Author | SHA1 | Date | |
---|---|---|---|
14dada50c4 | |||
ab19e5770f | |||
aea6c129dc | |||
497aa2e023 | |||
e889815402 | |||
1d4640c85c | |||
aeed3e3848 | |||
7488fc8f98 | |||
549eb18ec6 | |||
5ed06f90b7 | |||
3a0694323f | |||
c8b7051eba | |||
5954562200 | |||
ee1b91329e | |||
97d5748d11 | |||
559ade2bab | |||
1fe198f26e | |||
a7b83f5d78 | |||
071a68a0dd | |||
aab4a5dc55 | |||
ff3d11e8da | |||
bca266cb86 | |||
bdffe48676 | |||
cb86e30fab | |||
407f543039 | |||
f24a8a47a9 | |||
ce7b3d9158 | |||
f064148a0f | |||
733375fb74 | |||
440f7f91a4 | |||
dbcfd9b9d3 | |||
672de3919b | |||
0a696a6ab8 | |||
d8a521623a | |||
f5f006dc7b |
@ -1,4 +1,4 @@
|
||||
version: 3.0.{build}
|
||||
version: 3.2.{build}
|
||||
|
||||
configuration:
|
||||
- Release
|
||||
@ -10,7 +10,7 @@ environment:
|
||||
VCPKGDIR: C:\tools\vcpkg\installed\x64-windows-static-md
|
||||
matrix:
|
||||
- QTDIR: C:\Qt\5.15\msvc2019_64
|
||||
- QTDIR: C:\Qt\6.5\msvc2019_64
|
||||
- QTDIR: C:\Qt\6.7\msvc2019_64
|
||||
|
||||
install:
|
||||
- cmd: |-
|
||||
@ -26,7 +26,7 @@ build_script:
|
||||
nmake release
|
||||
|
||||
artifacts:
|
||||
- path: release\pbf2.dll
|
||||
- path: release\pbf3.dll
|
||||
|
||||
cache:
|
||||
- C:\tools\vcpkg\installed\
|
||||
|
54
.github/workflows/android.yml
vendored
Normal file
54
.github/workflows/android.yml
vendored
Normal file
@ -0,0 +1,54 @@
|
||||
name: Android
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: QtPBFImagePlugin
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Install protobuf compiler
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install protobuf-compiler
|
||||
- name: set up JDK 11
|
||||
uses: actions/setup-java@v4
|
||||
with:
|
||||
distribution: 'temurin'
|
||||
java-version: '11'
|
||||
- name: Setup Android SDK
|
||||
uses: android-actions/setup-android@v3
|
||||
with:
|
||||
cmdline-tools-version: 9862592
|
||||
- name: Install android platform, build-tools and ndk
|
||||
run: ${ANDROID_HOME}/cmdline-tools/latest/bin/sdkmanager --install "platforms;android-33" "build-tools;33.0.0" "ndk;23.1.7779620"
|
||||
- name: Setup NDK path
|
||||
run: echo "ANDROID_NDK_ROOT=${ANDROID_HOME}/ndk/23.1.7779620/" >> $GITHUB_ENV
|
||||
- name: Install Qt (Desktop)
|
||||
uses: jurplel/install-qt-action@v3
|
||||
with:
|
||||
aqtversion: '==3.1.*'
|
||||
version: '6.4.0'
|
||||
- name: Install Qt (Android)
|
||||
uses: jurplel/install-qt-action@v3
|
||||
with:
|
||||
aqtversion: '==3.1.*'
|
||||
version: '6.4.0'
|
||||
target: 'android'
|
||||
arch: 'android_arm64_v8a'
|
||||
- name: Install Android Google Protocol Buffers
|
||||
run: git clone https://github.com/tumic0/android_protobuf.git
|
||||
- name: Configure build
|
||||
run: qmake pbfplugin.pro PROTOBUF=android_protobuf
|
||||
- name: Build project
|
||||
run: make -j2
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: libplugins_imageformats_libpbf_arm64-v8a.so
|
||||
path: plugins/libplugins_imageformats_libpbf_arm64-v8a.so
|
2
.github/workflows/linux.yml
vendored
2
.github/workflows/linux.yml
vendored
@ -11,7 +11,7 @@ jobs:
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
|
20
.github/workflows/osx.yml
vendored
20
.github/workflows/osx.yml
vendored
@ -11,13 +11,15 @@ jobs:
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- name: Set environment variables
|
||||
run: echo "PATH=/usr/local/opt/qt@5/bin:/usr/local/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
|
||||
run: echo "PATH=/opt/homebrew/opt/qt@5/bin:/opt/homebrew/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install dependencies
|
||||
run: brew install qt@5 protobuf@21
|
||||
run: |
|
||||
brew update
|
||||
brew install qt@5 protobuf@21
|
||||
- name: Configure build
|
||||
run: qmake PROTOBUF=/usr/local/opt/protobuf@21 pbfplugin.pro
|
||||
run: qmake PROTOBUF=/opt/homebrew/opt/protobuf@21 pbfplugin.pro
|
||||
- name: Build project
|
||||
run: make -j3
|
||||
|
||||
@ -26,12 +28,14 @@ jobs:
|
||||
runs-on: macos-latest
|
||||
steps:
|
||||
- name: Set environment variables
|
||||
run: echo "PATH=/usr/local/opt/qt@6/bin:/usr/local/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
|
||||
run: echo "PATH=/opt/homebrew/opt/qt@6/bin:/opt/homebrew/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
uses: actions/checkout@v4
|
||||
- name: Install dependencies
|
||||
run: brew install qt@6 protobuf@21
|
||||
run: |
|
||||
brew update
|
||||
brew install qt@6 protobuf@21
|
||||
- name: Configure build
|
||||
run: qmake PROTOBUF=/usr/local/opt/protobuf@21 pbfplugin.pro
|
||||
run: qmake PROTOBUF=/opt/homebrew/opt/protobuf@21 pbfplugin.pro
|
||||
- name: Build project
|
||||
run: make -j3
|
||||
|
16
README.md
16
README.md
@ -18,19 +18,19 @@ to fit the styles and available data (OpenMapTiles, Mapbox tiles).
|
||||
Due to a major design flaw in the Mapbox vector tiles specification - the zoom
|
||||
is not part of the PBF data - the plugin can not be used "as is", but passing
|
||||
the zoom level is necessary. This is done by exploiting the optional *format*
|
||||
parameter of the QImage constructor or the QImage::fromData() or
|
||||
QPixmap::loadFromData() functions. The zoom number is passed as ASCII string
|
||||
parameter of the QImage constructor or the *QImage::loadFromData()* or
|
||||
*QPixmap::loadFromData()* functions. The zoom number is passed as ASCII string
|
||||
to the functions:
|
||||
```cpp
|
||||
QPixmap pm;
|
||||
pm.loadFromData(data, QByteArray::number(zoom));
|
||||
QImage img;
|
||||
img.loadFromData(data, QByteArray::number(zoom));
|
||||
```
|
||||
|
||||
For a complete code sample see the [pbf2png](https://github.com/tumic0/pbf2png)
|
||||
conversion utility.
|
||||
|
||||
### HiDPI
|
||||
The plugin supports vector scaling using QImageReader's setScaledSize() method,
|
||||
The plugin supports vector scaling using QImageReader's *setScaledSize()* method,
|
||||
so when used like in the following example:
|
||||
```cpp
|
||||
QImage img;
|
||||
@ -44,9 +44,9 @@ you will get 1024x1024px tiles with a pixel ratio of 2 (= HiDPI tiles).
|
||||
Since version 3 of the plugin tile overzoom is supported. If you set *format*
|
||||
to `$zoom;$overzoom`:
|
||||
```cpp
|
||||
QPixmap pm;
|
||||
QImage img;
|
||||
QByteArray fmt(QByteArray::number(zoom) + ';' + QByteArray::number(overzoom));
|
||||
pm.loadFromData(data, fmt);
|
||||
img.loadFromData(data, fmt);
|
||||
```
|
||||
you will get (512<<overzoom)x(512<<overzoom)px tiles with a pixel ratio of 1.
|
||||
When overzoom is combined with setScaledSize(), the base size is the overzoomed
|
||||
@ -108,7 +108,7 @@ algorithm to work properly.
|
||||
A picture is worth a thousand words.
|
||||
#### OpenMapTiles
|
||||
|
||||
* Data: [MapTiler](https://github.com/tumic0/GPXSee-maps/blob/master/World/MapTiler.tpl)
|
||||
* Data: [MapTiler](https://github.com/tumic0/GPXSee-maps/blob/master/World/MapTiler-OpenMapTiles.tpl)
|
||||
* Style: [OSM-liberty](https://github.com/tumic0/QtPBFImagePlugin-styles/blob/master/OpenMapTiles/osm-liberty/style.json)
|
||||
|
||||

|
||||
|
@ -2,7 +2,7 @@ TARGET = pbf
|
||||
TEMPLATE = lib
|
||||
CONFIG += plugin
|
||||
QT += gui
|
||||
VERSION = 3.0
|
||||
VERSION = 3.2
|
||||
|
||||
PROTOS = protobuf/vector_tile.proto
|
||||
include(protobuf/vector_tile.pri)
|
||||
@ -39,9 +39,12 @@ RESOURCES += pbfplugin.qrc
|
||||
|
||||
DEFINES += QT_NO_DEPRECATED_WARNINGS
|
||||
|
||||
unix:!macx{
|
||||
unix:!macx:!android {
|
||||
LIBS += -lprotobuf-lite \
|
||||
-lz
|
||||
|
||||
target.path += $$[QT_INSTALL_PLUGINS]/imageformats
|
||||
INSTALLS += target
|
||||
}
|
||||
win32 {
|
||||
INCLUDEPATH += $$PROTOBUF/include \
|
||||
@ -51,13 +54,19 @@ win32 {
|
||||
|
||||
QMAKE_TARGET_PRODUCT = QtPBFImagePlugin
|
||||
QMAKE_TARGET_DESCRIPTION = Qt $$QT_VERSION MVT/PBF image plugin
|
||||
QMAKE_TARGET_COPYRIGHT = Copyright (c) 2023 Martin Tuma
|
||||
QMAKE_TARGET_COPYRIGHT = Copyright (c) 2018-2024 Martin Tuma
|
||||
}
|
||||
macx {
|
||||
INCLUDEPATH += $$PROTOBUF/include
|
||||
LIBS += $$PROTOBUF/lib/libprotobuf-lite.a \
|
||||
-lz
|
||||
}
|
||||
android {
|
||||
INCLUDEPATH += $$PROTOBUF/include
|
||||
LIBS += $$PROTOBUF/$$ANDROID_TARGET_ARCH/libprotobuf-lite.a \
|
||||
-lz
|
||||
|
||||
target.path += $$[QT_INSTALL_PLUGINS]/imageformats
|
||||
INSTALLS += target
|
||||
top_builddir=$$shadowed($$PWD)
|
||||
DESTDIR = $$top_builddir/plugins
|
||||
TARGET = $$qt5LibraryTarget(libpbf, "plugins/imageformats/")
|
||||
}
|
||||
|
@ -31,12 +31,12 @@ static QVariant value(const vector_tile::Tile_Value &val)
|
||||
else if (val.has_double_value())
|
||||
return QVariant(val.double_value());
|
||||
else if (val.has_string_value())
|
||||
return QVariant(QString::fromStdString(val.string_value()));
|
||||
return QVariant(QByteArray::fromStdString(val.string_value()));
|
||||
else
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
const QVariant *PBF::Feature::value(const QString &key) const
|
||||
const QVariant *PBF::Feature::value(const QByteArray &key) const
|
||||
{
|
||||
const KeyHash &keys(_layer->keys());
|
||||
KeyHash::const_iterator it(keys.find(key));
|
||||
@ -92,7 +92,7 @@ PBF::Layer::Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
_keys.reserve(data->keys_size());
|
||||
for (int i = 0; i < data->keys_size(); i++)
|
||||
_keys.insert(QString::fromStdString(data->keys(i)), i);
|
||||
_keys.insert(QByteArray::fromStdString(data->keys(i)), i);
|
||||
_values.reserve(data->values_size());
|
||||
for (int i = 0; i < data->values_size(); i++)
|
||||
_values.append(value(data->values(i)));
|
||||
@ -107,7 +107,8 @@ PBF::PBF(const vector_tile::Tile &tile)
|
||||
{
|
||||
for (int i = 0; i < tile.layers_size(); i++) {
|
||||
const vector_tile::Tile_Layer &layer = tile.layers(i);
|
||||
_layers.insert(QString::fromStdString(layer.name()), new Layer(&layer));
|
||||
_layers.insert(QByteArray::fromStdString(layer.name()),
|
||||
new Layer(&layer));
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -8,7 +8,7 @@
|
||||
#include "vector_tile.pb.h"
|
||||
|
||||
|
||||
typedef QHash<QString, google::protobuf::uint32> KeyHash;
|
||||
typedef QHash<QByteArray, google::protobuf::uint32> KeyHash;
|
||||
|
||||
class PBF
|
||||
{
|
||||
@ -22,7 +22,7 @@ public:
|
||||
Feature(const vector_tile::Tile_Feature *data, const Layer *layer)
|
||||
: _data(data), _layer(layer) {}
|
||||
|
||||
const QVariant *value(const QString &key) const;
|
||||
const QVariant *value(const QByteArray &key) const;
|
||||
vector_tile::Tile_GeomType type() const {return _data->type();}
|
||||
QPainterPath path(const QSizeF &factor) const;
|
||||
|
||||
@ -55,10 +55,10 @@ public:
|
||||
PBF(const vector_tile::Tile &tile);
|
||||
~PBF();
|
||||
|
||||
const QHash<QString, Layer*> &layers() const {return _layers;}
|
||||
const QHash<QByteArray, Layer*> &layers() const {return _layers;}
|
||||
|
||||
private:
|
||||
QHash<QString, Layer*> _layers;
|
||||
QHash<QByteArray, Layer*> _layers;
|
||||
};
|
||||
|
||||
inline bool operator<(const PBF::Feature &f1, const PBF::Feature &f2)
|
||||
|
@ -11,7 +11,7 @@ class Style;
|
||||
class PBFHandler : public QImageIOHandler
|
||||
{
|
||||
public:
|
||||
PBFHandler(const Style *style) : _style(style) {}
|
||||
PBFHandler(Style *style) : _style(style) {}
|
||||
~PBFHandler() {}
|
||||
|
||||
bool canRead() const;
|
||||
@ -24,7 +24,7 @@ public:
|
||||
static bool canRead(QIODevice *device);
|
||||
|
||||
private:
|
||||
const Style *_style;
|
||||
Style *_style;
|
||||
QSize _scaledSize;
|
||||
};
|
||||
|
||||
|
@ -4,14 +4,20 @@
|
||||
#include <QDebug>
|
||||
#include "sprites.h"
|
||||
|
||||
|
||||
/*
|
||||
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)
|
||||
static QImage sdf2img(const QImage &sdf, const QColor &color)
|
||||
{
|
||||
static QImage img(fileName);
|
||||
QImage img(sdf.convertToFormat(QImage::Format_ARGB32));
|
||||
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;
|
||||
}
|
||||
|
||||
@ -19,7 +25,6 @@ Sprites::Sprite::Sprite(const QJsonObject &json)
|
||||
{
|
||||
int x, y, width, height;
|
||||
|
||||
|
||||
if (json.contains("x") && json["x"].isDouble())
|
||||
x = json["x"].toInt();
|
||||
else
|
||||
@ -44,12 +49,15 @@ Sprites::Sprite::Sprite(const QJsonObject &json)
|
||||
_pixelRatio = json["pixelRatio"].toDouble();
|
||||
else
|
||||
_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)
|
||||
{
|
||||
_imageFile = imageFile;
|
||||
|
||||
QFile file(jsonFile);
|
||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||
qCritical() << jsonFile << ": error opening file";
|
||||
@ -78,27 +86,60 @@ bool Sprites::load(const QString &jsonFile, const QString &imageFile)
|
||||
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;
|
||||
}
|
||||
|
||||
QImage Sprites::icon(const QString &name) const
|
||||
QImage Sprites::sprite(const Sprite &sprite, const QColor &color, qreal scale)
|
||||
{
|
||||
if (_imageFile.isEmpty())
|
||||
_lock.lock();
|
||||
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();
|
||||
|
||||
if (!_img.rect().contains(sprite.rect()))
|
||||
return QImage();
|
||||
|
||||
const QImage &img = atlas(_imageFile);
|
||||
if (img.isNull())
|
||||
QImage img(_img.copy(sprite.rect()));
|
||||
img.setDevicePixelRatio(sprite.pixelRatio());
|
||||
|
||||
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();
|
||||
|
||||
QMap<QString, Sprite>::const_iterator it = _sprites.find(name);
|
||||
QMap<QString, Sprite>::const_iterator it = _sprites.constFind(name);
|
||||
if (it == _sprites.constEnd())
|
||||
return QImage();
|
||||
|
||||
if (!img.rect().contains(it->rect()))
|
||||
return QImage();
|
||||
|
||||
QImage ret(img.copy(it->rect()));
|
||||
ret.setDevicePixelRatio(it->pixelRatio());
|
||||
|
||||
return ret;
|
||||
return sprite(*it, color, size);
|
||||
}
|
||||
|
@ -5,16 +5,19 @@
|
||||
#include <QMap>
|
||||
#include <QImage>
|
||||
#include <QString>
|
||||
#include <QMutex>
|
||||
|
||||
class QJsonObject;
|
||||
|
||||
class Sprites
|
||||
{
|
||||
public:
|
||||
Sprites() : _init(0) {}
|
||||
bool load(const QString &jsonFile, const QString &imageFile);
|
||||
|
||||
bool isNull() const {return _imageFile.isNull();}
|
||||
QImage icon(const QString &name) const;
|
||||
QImage icon(const QString &name, const QColor &color = Qt::black,
|
||||
qreal size = 1.0);
|
||||
|
||||
private:
|
||||
class Sprite {
|
||||
@ -23,14 +26,21 @@ private:
|
||||
|
||||
const QRect &rect() const {return _rect;}
|
||||
qreal pixelRatio() const {return _pixelRatio;}
|
||||
bool sdf() const {return _sdf;}
|
||||
|
||||
private:
|
||||
QRect _rect;
|
||||
qreal _pixelRatio;
|
||||
bool _sdf;
|
||||
};
|
||||
|
||||
QImage sprite(const Sprite &sprite, const QColor &color, qreal scale);
|
||||
|
||||
QMap<QString, Sprite> _sprites;
|
||||
QImage _img;
|
||||
QMutex _lock;
|
||||
QString _imageFile;
|
||||
int _init;
|
||||
};
|
||||
|
||||
#endif // SPRITES_H
|
||||
|
117
src/style.cpp
117
src/style.cpp
@ -8,7 +8,6 @@
|
||||
#include <QRegularExpression>
|
||||
#include <QDebug>
|
||||
#include "text.h"
|
||||
#include "color.h"
|
||||
#include "font.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
@ -27,6 +26,20 @@ static vector_tile::Tile_GeomType geometryType(const QString &str)
|
||||
return vector_tile::Tile_GeomType_UNKNOWN;
|
||||
}
|
||||
|
||||
static QVariant variant(const QJsonValue &val)
|
||||
{
|
||||
switch (val.type()) {
|
||||
case QJsonValue::String:
|
||||
return QVariant(val.toString().toUtf8());
|
||||
case QJsonValue::Double:
|
||||
case QJsonValue::Bool:
|
||||
return val.toVariant();
|
||||
default:
|
||||
qWarning() << val << ": invalid filter value";
|
||||
return QVariant();
|
||||
}
|
||||
}
|
||||
|
||||
Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
: _type(Unknown), _not(false)
|
||||
{
|
||||
@ -36,50 +49,50 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
if (json.isEmpty())
|
||||
INVALID_FILTER(json);
|
||||
|
||||
QString type = json.at(0).toString();
|
||||
QString type(json.at(0).toString());
|
||||
|
||||
if (type == "==") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
if (json.at(1).toString() == "$type") {
|
||||
_type = GeometryType;
|
||||
_kv = QPair<QString, QVariant>(QString(),
|
||||
_kv = QPair<QByteArray, QVariant>(QByteArray(),
|
||||
QVariant(geometryType(json.at(2).toString())));
|
||||
} else {
|
||||
_type = EQ;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
}
|
||||
} else if (type == "!=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = NE;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
} else if (type == "<") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = LT;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
} else if (type == "<=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = LE;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
} else if (type == ">") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = GT;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
} else if (type == ">=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = GE;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
} else if (type == "all") {
|
||||
_type = All;
|
||||
for (int i = 1; i < json.size(); i++)
|
||||
@ -92,28 +105,32 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
if (json.size() < 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = In;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
for (int i = 2; i < json.size(); i++)
|
||||
_set.insert(json.at(i).toString());
|
||||
_set.insert(json.at(i).toString().toUtf8());
|
||||
} else if (type == "!in") {
|
||||
if (json.size() < 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = In;
|
||||
_not = true;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
for (int i = 2; i < json.size(); i++)
|
||||
_set.insert(json.at(i).toString());
|
||||
_set.insert(json.at(i).toString().toUtf8());
|
||||
} else if (type == "has") {
|
||||
if (json.size() < 2)
|
||||
INVALID_FILTER(json);
|
||||
_type = Has;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
} else if (type == "!has") {
|
||||
if (json.size() < 2)
|
||||
INVALID_FILTER(json);
|
||||
_type = Has;
|
||||
_not = true;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
} else
|
||||
INVALID_FILTER(json);
|
||||
}
|
||||
@ -183,7 +200,7 @@ bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return _not;
|
||||
else
|
||||
return _set.contains((*v).toString()) ^ _not;
|
||||
return _set.contains((*v).toByteArray()) ^ _not;
|
||||
case Has:
|
||||
return (feature.value(_kv.first) ? true : false) ^ _not;
|
||||
case All:
|
||||
@ -205,7 +222,7 @@ bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
||||
|
||||
QString Style::Layer::Template::value(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
QRegularExpression rx("\\{[^\\}]*\\}");
|
||||
static QRegularExpression rx("\\{[^\\}]*\\}");
|
||||
QString text(_field.value(zoom));
|
||||
QRegularExpressionMatchIterator it = rx.globalMatch(text);
|
||||
QStringList keys;
|
||||
@ -217,7 +234,7 @@ QString Style::Layer::Template::value(int zoom, const PBF::Feature &feature) con
|
||||
}
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
const QString &key = keys.at(i);
|
||||
const QVariant *val = feature.value(key);
|
||||
const QVariant *val = feature.value(key.toUtf8());
|
||||
text.replace(QString("{%1}").arg(key), val ? val->toString() : "");
|
||||
}
|
||||
|
||||
@ -258,6 +275,9 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
|
||||
_textHaloColor = FunctionC(json["text-halo-color"], QColor());
|
||||
_textHaloWidth = FunctionF(json["text-halo-width"]);
|
||||
_textHaloBlur = FunctionF(json["text-halo-blur"]);
|
||||
|
||||
// icon
|
||||
_iconColor = FunctionC(json["icon-color"]);
|
||||
}
|
||||
|
||||
QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
||||
@ -293,7 +313,7 @@ QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
||||
return pen;
|
||||
}
|
||||
|
||||
QBrush Style::Layer::Paint::brush(Type type, int zoom, const Sprites &sprites)
|
||||
QBrush Style::Layer::Paint::brush(Type type, int zoom, Sprites &sprites)
|
||||
const
|
||||
{
|
||||
QColor color;
|
||||
@ -370,6 +390,7 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
|
||||
|
||||
// icon
|
||||
_icon = Template(FunctionS(json["icon-image"]));
|
||||
_iconSize = FunctionF(json["icon-size"]);
|
||||
|
||||
// symbol
|
||||
_symbolPlacement = FunctionS(json["symbol-placement"]);
|
||||
@ -483,7 +504,7 @@ Style::Layer::Layer(const QJsonObject &json)
|
||||
_type = Symbol;
|
||||
|
||||
// source-layer
|
||||
_sourceLayer = json["source-layer"].toString();
|
||||
_sourceLayer = json["source-layer"].toString().toUtf8();
|
||||
|
||||
// zooms
|
||||
if (json.contains("minzoom") && json["minzoom"].isDouble())
|
||||
@ -512,7 +533,7 @@ bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
|
||||
return _filter.match(feature);
|
||||
}
|
||||
|
||||
void Style::Layer::setPathPainter(Tile &tile, const Sprites &sprites) const
|
||||
void Style::Layer::setPathPainter(Tile &tile, Sprites &sprites) const
|
||||
{
|
||||
QPainter &p = tile.painter();
|
||||
int zoom = tile.zoom();
|
||||
@ -545,14 +566,18 @@ void Style::Layer::setTextProperties(Tile &tile) const
|
||||
}
|
||||
|
||||
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const PBF::Feature &feature, const Sprites &sprites) const
|
||||
const PBF::Feature &feature, Sprites &sprites) const
|
||||
{
|
||||
QString text = _layout.text(tile.zoom(), feature);
|
||||
if (text.isEmpty())
|
||||
QString text(_layout.text(tile.zoom(), feature));
|
||||
QString icon(_layout.icon(tile.zoom(), feature));
|
||||
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;
|
||||
|
||||
QString icon = _layout.icon(tile.zoom(), feature);
|
||||
tile.text().addLabel(text, sprites.icon(icon), path);
|
||||
tile.text().addLabel(text, img, path);
|
||||
}
|
||||
|
||||
static bool loadSprites(const QDir &styleDir, const QString &json,
|
||||
@ -565,7 +590,7 @@ static bool loadSprites(const QDir &styleDir, const QString &json,
|
||||
if (QFileInfo::exists(spritesImg))
|
||||
return sprites.load(spritesJSON, spritesImg);
|
||||
else {
|
||||
qCritical() << spritesImg << ": no such file";
|
||||
qWarning() << spritesImg << ": no such file";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -598,20 +623,20 @@ bool Style::load(const QString &fileName)
|
||||
_layers.append(Layer(layers[i].toObject()));
|
||||
}
|
||||
|
||||
QDir styleDir = QFileInfo(fileName).absoluteDir();
|
||||
QDir styleDir(QFileInfo(fileName).absoluteDir());
|
||||
loadSprites(styleDir, "sprite.json", "sprite.png", _sprites);
|
||||
loadSprites(styleDir, "sprite@2x.json", "sprite@2x.png", _sprites2x);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
const Sprites &Style::sprites(const QPointF &scale) const
|
||||
Sprites &Style::sprites(const QPointF &scale)
|
||||
{
|
||||
return (scale.x() > 1.0 || scale.y() > 1.0)
|
||||
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
||||
}
|
||||
|
||||
void Style::setupLayer(Tile &tile, const Layer &layer) const
|
||||
void Style::setupLayer(Tile &tile, const Layer &layer)
|
||||
{
|
||||
if (layer.isSymbol())
|
||||
layer.setTextProperties(tile);
|
||||
@ -619,7 +644,7 @@ void Style::setupLayer(Tile &tile, const Layer &layer) const
|
||||
layer.setPathPainter(tile, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawBackground(Tile &tile) const
|
||||
void Style::drawBackground(Tile &tile)
|
||||
{
|
||||
QRectF rect(QPointF(0, 0), QSizeF(tile.size().width() / tile.scale().x(),
|
||||
tile.size().height() / tile.scale().y()));
|
||||
@ -634,13 +659,10 @@ void Style::drawBackground(Tile &tile) const
|
||||
_layers.first().setPathPainter(tile, _sprites);
|
||||
tile.painter().drawPath(path);
|
||||
}
|
||||
|
||||
//tile.painter().setPen(Qt::red);
|
||||
//tile.painter().drawRect(rect);
|
||||
}
|
||||
|
||||
void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
Tile &tile, const QSizeF &factor) const
|
||||
Tile &tile, const QSizeF &factor)
|
||||
{
|
||||
if (!layer.match(tile.zoom(), feature))
|
||||
return;
|
||||
@ -656,7 +678,7 @@ void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
}
|
||||
|
||||
void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const
|
||||
Tile &tile)
|
||||
{
|
||||
if (pbfLayer.data()->version() > 2)
|
||||
return;
|
||||
@ -675,12 +697,12 @@ void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
tile.painter().restore();
|
||||
}
|
||||
|
||||
void Style::render(const PBF &data, Tile &tile) const
|
||||
void Style::render(const PBF &data, Tile &tile)
|
||||
{
|
||||
drawBackground(tile);
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++) {
|
||||
QHash<QString, PBF::Layer*>::const_iterator it = data.layers().find(
|
||||
QHash<QByteArray, PBF::Layer*>::const_iterator it = data.layers().find(
|
||||
_layers.at(i).sourceLayer());
|
||||
if (it == data.layers().constEnd())
|
||||
continue;
|
||||
@ -689,4 +711,11 @@ void Style::render(const PBF &data, Tile &tile) const
|
||||
}
|
||||
|
||||
tile.text().render(&tile.painter());
|
||||
|
||||
//QRectF rect(QPointF(0, 0), QSizeF(tile.size().width() / tile.scale().x(),
|
||||
// tile.size().height() / tile.scale().y()));
|
||||
//tile.painter().setPen(Qt::red);
|
||||
//tile.painter().setBrush(Qt::NoBrush);
|
||||
//tile.painter().setRenderHint(QPainter::Antialiasing, false);
|
||||
//tile.painter().drawRect(rect);
|
||||
}
|
||||
|
32
src/style.h
32
src/style.h
@ -29,7 +29,7 @@ public:
|
||||
Style(QObject *parent = 0) : QObject(parent) {}
|
||||
|
||||
bool load(const QString &fileName);
|
||||
void render(const PBF &data, Tile &tile) const;
|
||||
void render(const PBF &data, Tile &tile);
|
||||
|
||||
private:
|
||||
class Layer {
|
||||
@ -37,17 +37,17 @@ private:
|
||||
Layer() : _type(Unknown), _minZoom(0), _maxZoom(24) {}
|
||||
Layer(const QJsonObject &json);
|
||||
|
||||
const QString &sourceLayer() const {return _sourceLayer;}
|
||||
const QByteArray &sourceLayer() const {return _sourceLayer;}
|
||||
bool isPath() const {return (_type == Line || _type == Fill);}
|
||||
bool isBackground() const {return (_type == Background);}
|
||||
bool isSymbol() const {return (_type == Symbol);}
|
||||
bool isVisible() const {return (_layout.visible());}
|
||||
|
||||
bool match(int zoom, const PBF::Feature &feature) const;
|
||||
void setPathPainter(Tile &tile, const Sprites &sprites) const;
|
||||
void setPathPainter(Tile &tile, Sprites &sprites) const;
|
||||
void setTextProperties(Tile &tile) const;
|
||||
void addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const PBF::Feature &feature, const Sprites &sprites) const;
|
||||
const PBF::Feature &feature, Sprites &sprites) const;
|
||||
|
||||
private:
|
||||
enum Type {
|
||||
@ -74,8 +74,8 @@ private:
|
||||
|
||||
Type _type;
|
||||
bool _not;
|
||||
QSet<QString> _set;
|
||||
QPair<QString, QVariant> _kv;
|
||||
QSet<QByteArray> _set;
|
||||
QPair<QByteArray, QVariant> _kv;
|
||||
QVector<Filter> _filters;
|
||||
};
|
||||
|
||||
@ -103,6 +103,8 @@ private:
|
||||
{return _text.value(zoom, feature).trimmed();}
|
||||
QString icon(int zoom, const PBF::Feature &feature) const
|
||||
{return _icon.value(zoom, feature);}
|
||||
qreal iconSize(int zoom) const
|
||||
{return _iconSize.value(zoom);}
|
||||
QFont font(int zoom) const;
|
||||
Qt::PenCapStyle lineCap(int zoom) const;
|
||||
Qt::PenJoinStyle lineJoin(int zoom) const;
|
||||
@ -116,6 +118,7 @@ private:
|
||||
|
||||
Template _text;
|
||||
Template _icon;
|
||||
FunctionF _iconSize;
|
||||
FunctionF _textSize;
|
||||
FunctionF _textMaxWidth;
|
||||
FunctionF _textMaxAngle;
|
||||
@ -135,13 +138,15 @@ private:
|
||||
Paint(const QJsonObject &json);
|
||||
|
||||
QPen pen(Layer::Type type, int zoom) const;
|
||||
QBrush brush(Layer::Type type, int zoom, const Sprites &sprites)
|
||||
QBrush brush(Layer::Type type, int zoom, Sprites &sprites)
|
||||
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));}
|
||||
QColor iconColor(int zoom) const
|
||||
{return _iconColor.value(zoom);}
|
||||
|
||||
private:
|
||||
FunctionC _textColor;
|
||||
@ -150,6 +155,7 @@ private:
|
||||
FunctionC _fillColor;
|
||||
FunctionC _fillOutlineColor;
|
||||
FunctionC _backgroundColor;
|
||||
FunctionC _iconColor;
|
||||
FunctionF _fillOpacity;
|
||||
FunctionF _lineOpacity;
|
||||
FunctionF _lineWidth;
|
||||
@ -161,21 +167,21 @@ private:
|
||||
};
|
||||
|
||||
Type _type;
|
||||
QString _sourceLayer;
|
||||
QByteArray _sourceLayer;
|
||||
int _minZoom, _maxZoom;
|
||||
Filter _filter;
|
||||
Layout _layout;
|
||||
Paint _paint;
|
||||
};
|
||||
|
||||
const Sprites &sprites(const QPointF &scale) const;
|
||||
Sprites &sprites(const QPointF &scale);
|
||||
|
||||
void drawBackground(Tile &tile) const;
|
||||
void setupLayer(Tile &tile, const Layer &layer) const;
|
||||
void drawBackground(Tile &tile);
|
||||
void setupLayer(Tile &tile, const Layer &layer);
|
||||
void drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
Tile &tile, const QSizeF &factor) const;
|
||||
Tile &tile, const QSizeF &factor);
|
||||
void drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const;
|
||||
Tile &tile);
|
||||
|
||||
QVector<Layer> _layers;
|
||||
Sprites _sprites, _sprites2x;
|
||||
|
11
src/text.cpp
11
src/text.cpp
@ -14,15 +14,10 @@ Text::~Text()
|
||||
|
||||
void Text::render(QPainter *painter) const
|
||||
{
|
||||
QSet<QString> set;
|
||||
|
||||
for (int i = 0; i < _items.size(); i++) {
|
||||
const TextItem *ti = _items.at(i);
|
||||
if (ti->isVisible() && _sceneRect.intersects(ti->boundingRect())
|
||||
&& !set.contains(ti->text())) {
|
||||
if (ti->isVisible() && _sceneRect.intersects(ti->boundingRect()))
|
||||
ti->paint(painter);
|
||||
set.insert(ti->text());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -51,6 +46,8 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
||||
} else {
|
||||
switch (_placement) {
|
||||
case Line:
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
|
||||
break;
|
||||
case LineCenter:
|
||||
@ -73,7 +70,7 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
||||
ti->setHalo(_halo);
|
||||
addItem(ti);
|
||||
|
||||
QList<TextItem*> ci = collidingItems(ti);
|
||||
QList<TextItem*> ci(collidingItems(ti));
|
||||
for (int i = 0; i < ci.size(); i++)
|
||||
ci[i]->setVisible(false);
|
||||
}
|
||||
|
@ -85,7 +85,7 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
||||
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||
_anchor(anchor)
|
||||
{
|
||||
_textRect = fuzzyBoundingRect();
|
||||
_textRect = text.isEmpty() ? QRectF() : fuzzyBoundingRect();
|
||||
_boundingRect = moveTextRect(_textRect);
|
||||
|
||||
if (!_icon.isNull()) {
|
||||
|
Reference in New Issue
Block a user