mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2025-04-19 20:19:10 +02:00
Compare commits
No commits in common. "master" and "2.6" have entirely different histories.
@ -1,4 +1,4 @@
|
||||
version: 4.2.{build}
|
||||
version: 2.6.{build}
|
||||
|
||||
configuration:
|
||||
- Release
|
||||
@ -7,25 +7,26 @@ image:
|
||||
- Visual Studio 2022
|
||||
|
||||
environment:
|
||||
VCPKGDIR: C:\tools\vcpkg\installed\x64-windows-static-md-release
|
||||
VCPKGDIR: C:\tools\vcpkg\installed\x64-windows-static-md
|
||||
matrix:
|
||||
- QTDIR: C:\Qt\5.15\msvc2019_64
|
||||
- QTDIR: C:\Qt\6.8\msvc2022_64
|
||||
- QTDIR: C:\Qt\6.5\msvc2019_64
|
||||
|
||||
install:
|
||||
- cmd: |-
|
||||
set PATH=%QTDIR%\bin;%PATH%
|
||||
set PATH=%QTDIR%\bin;%VCPKGDIR%\tools\protobuf;%PATH%
|
||||
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat
|
||||
vcpkg install zlib:x64-windows-static-md-release
|
||||
vcpkg install protobuf:x64-windows-static-md
|
||||
vcpkg install zlib:x64-windows-static-md
|
||||
copy /y %VCPKGDIR%\lib\zlib.lib %VCPKGDIR%\lib\zlibstatic.lib
|
||||
|
||||
build_script:
|
||||
- cmd: |-
|
||||
qmake ZLIB=%VCPKGDIR% pbfplugin.pro
|
||||
qmake PROTOBUF=%VCPKGDIR% ZLIB=%VCPKGDIR% pbfplugin.pro
|
||||
nmake release
|
||||
|
||||
artifacts:
|
||||
- path: release\pbf4.dll
|
||||
- path: release\pbf2.dll
|
||||
|
||||
cache:
|
||||
- C:\tools\vcpkg\installed\
|
||||
|
48
.github/workflows/android.yml
vendored
48
.github/workflows/android.yml
vendored
@ -1,48 +0,0 @@
|
||||
name: Android
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: QtPBFImagePlugin
|
||||
runs-on: ubuntu-22.04
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- 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: Configure build
|
||||
run: qmake pbfplugin.pro
|
||||
- name: Build project
|
||||
run: make -j4
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
name: libplugins_imageformats_libpbf_arm64-v8a.so
|
||||
path: plugins/libplugins_imageformats_libpbf_arm64-v8a.so
|
13
.github/workflows/linux.yml
vendored
13
.github/workflows/linux.yml
vendored
@ -8,18 +8,15 @@ on:
|
||||
jobs:
|
||||
build:
|
||||
name: QtPBFImagePlugin
|
||||
runs-on: ubuntu-22.04
|
||||
strategy:
|
||||
matrix:
|
||||
config: ['release', 'debug']
|
||||
runs-on: ubuntu-20.04
|
||||
steps:
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
uses: actions/checkout@v3
|
||||
- name: Install dependencies
|
||||
run: |
|
||||
sudo apt-get update
|
||||
sudo apt-get install qtbase5-dev qtbase5-dev-tools qt5-qmake zlib1g-dev
|
||||
sudo apt-get install qtbase5-dev qtbase5-dev-tools qt5-qmake libprotobuf-dev protobuf-compiler zlib1g-dev
|
||||
- name: Configure build
|
||||
run: qmake CONFIG+=${{ matrix.config }} pbfplugin.pro
|
||||
run: qmake pbfplugin.pro
|
||||
- name: Build project
|
||||
run: make -j4
|
||||
run: make -j2
|
||||
|
36
.github/workflows/osx.yml
vendored
36
.github/workflows/osx.yml
vendored
@ -6,22 +6,32 @@ on:
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
name: QtPBFImagePlugin
|
||||
qt5:
|
||||
name: QtPBFImagePlugin Qt5 build
|
||||
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
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v4
|
||||
- name: Install Qt
|
||||
uses: jurplel/install-qt-action@v4
|
||||
with:
|
||||
version: '6.8.2'
|
||||
uses: actions/checkout@v3
|
||||
- name: Install dependencies
|
||||
run: brew install qt@5 protobuf@21
|
||||
- name: Configure build
|
||||
run: qmake pbfplugin.pro QMAKE_APPLE_DEVICE_ARCHS="x86_64h arm64"
|
||||
run: qmake PROTOBUF=/usr/local/opt/protobuf@21 pbfplugin.pro
|
||||
- name: Build project
|
||||
run: make -j3
|
||||
|
||||
qt6:
|
||||
name: QtPBFImagePlugin Qt6 build
|
||||
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
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: Install dependencies
|
||||
run: brew install qt@6 protobuf@21
|
||||
- name: Configure build
|
||||
run: qmake PROTOBUF=/usr/local/opt/protobuf@21 pbfplugin.pro
|
||||
- name: Build project
|
||||
run: make -j3
|
||||
- name: Upload artifacts
|
||||
uses: actions/upload-artifact@v4
|
||||
with:
|
||||
path: libpbf.dylib
|
||||
name: libpbf.dylib
|
||||
|
8
.gitignore
vendored
8
.gitignore
vendored
@ -1,9 +1,17 @@
|
||||
# Object files
|
||||
*.o
|
||||
|
||||
# Protobuf stuff
|
||||
protobuf/vector_tile.pb.cc
|
||||
protobuf/vector_tile.pb.h
|
||||
|
||||
# Qt stuff
|
||||
/.qmake.stash
|
||||
moc_*.cpp
|
||||
moc_*.h
|
||||
qrc_*.cpp
|
||||
Makefile*
|
||||
|
||||
# lib
|
||||
libpbf.so
|
||||
pbf.dylib
|
||||
|
66
README.md
66
README.md
@ -18,39 +18,24 @@ 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::loadFromData()* or
|
||||
*QPixmap::loadFromData()* functions. The zoom number is passed as ASCII string
|
||||
parameter of the QImage constructor or the QImage::fromData() or
|
||||
QPixmap::loadFromData() functions. The zoom number is passed as ASCII string
|
||||
to the functions:
|
||||
```cpp
|
||||
QImage img;
|
||||
img.loadFromData(data, QByteArray::number(zoom));
|
||||
QPixmap pm;
|
||||
pm.loadFromData(tileData, QString::number(zoom).toLatin1());
|
||||
```
|
||||
|
||||
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;
|
||||
QImageReader reader(file, QByteArray::number(zoom));
|
||||
QImageReader reader(file, QString::number(zoom).toLatin1());
|
||||
reader.setScaledSize(QSize(1024, 1024));
|
||||
reader.read(&img);
|
||||
reader.read(&image);
|
||||
```
|
||||
you will get 1024x1024px tiles with a pixel ratio of 2 (= HiDPI tiles).
|
||||
|
||||
### Overzoom
|
||||
Since version 3 of the plugin tile overzoom is supported. If you set *format*
|
||||
to `$zoom;$overzoom`:
|
||||
```cpp
|
||||
QImage img;
|
||||
QByteArray fmt(QByteArray::number(zoom) + ';' + QByteArray::number(overzoom));
|
||||
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
|
||||
tile size.
|
||||
For a sample code see the [pbf2png](https://github.com/tumic0/pbf2png)
|
||||
conversion utility.
|
||||
|
||||
## Styles
|
||||
The map style is loaded from the
|
||||
@ -67,45 +52,48 @@ repository.
|
||||
|
||||
## Build
|
||||
### Requirements
|
||||
* Qt5 >= 5.15 or Qt6
|
||||
* Qt5 >= 5.11 or Qt6
|
||||
* Google Protocol Buffers (protobuf-lite)
|
||||
* Zlib
|
||||
|
||||
### Build steps
|
||||
#### Linux, OS X and Android
|
||||
#### Linux
|
||||
```shell
|
||||
qmake pbfplugin.pro
|
||||
make
|
||||
```
|
||||
#### Windows
|
||||
```shell
|
||||
qmake ZLIB=path/to/zlib pbfplugin.pro
|
||||
qmake PROTOBUF=path/to/protobuf ZLIB=path/to/zlib pbfplugin.pro
|
||||
nmake
|
||||
```
|
||||
#### OS X
|
||||
```shell
|
||||
qmake PROTOBUF=path/to/protobuf pbfplugin.pro
|
||||
make
|
||||
```
|
||||
|
||||
## Install
|
||||
Copy the plugin to the system Qt image plugins path to make it work. You may
|
||||
also set the QT_PLUGIN_PATH system variable before starting the application.
|
||||
For Linux, there are RPM and DEB [packages](https://build.opensuse.org/project/show/home:tumic:QtPBFImagePlugin)
|
||||
also set the QT_PLUGIN_PATH system variable before starting the application. For
|
||||
Linux, there are RPM and DEB [packages](https://build.opensuse.org/project/show/home:tumic:QtPBFImagePlugin)
|
||||
for most common distros available on OBS.
|
||||
|
||||
## Limitations
|
||||
* Only data that is part of the PBF file is displayed. External layers defined
|
||||
in the style are ignored.
|
||||
* Text PBF features must have a unique id (OpenMapTiles >= v3.7) for the text
|
||||
layout algorithm to work properly. Additionally, the tile buffer must be large
|
||||
enough to contain all neighboring text features overlapping to the tile bounds
|
||||
(only data from the tile itself can be drawn to the resulting image).
|
||||
* Expressions are not supported in the styles, only property functions are
|
||||
implemented.
|
||||
* Only data that is part of the PBF file is displayed. External layers defined in the
|
||||
style are ignored.
|
||||
* Text PBF features must have a unique id (OpenMapTiles >= v3.7) for the text layout
|
||||
algorithm to work properly.
|
||||
* Expressions are not supported in the styles, only property functions are implemented.
|
||||
|
||||
## Changelog
|
||||
[Changelog](https://build.opensuse.org/projects/home:tumic:QtPBFImagePlugin/packages/QtPBFImagePlugin/files/qt6-qtpbfimageformat.changes)
|
||||
[Changelog](https://build.opensuse.org/package/view_file/home:tumic:QtPBFImagePlugin/QtPBFImagePlugin/libqt5-qtpbfimageformat.changes)
|
||||
|
||||
## Status
|
||||
A picture is worth a thousand words.
|
||||
#### OpenMapTiles
|
||||
|
||||
* Data: [MapTiler](https://github.com/tumic0/GPXSee-maps/blob/master/World/MapTiler-OpenMapTiles.tpl)
|
||||
* Data: [MapTiler](https://github.com/tumic0/GPXSee-maps/blob/master/World/MapTiler.tpl)
|
||||
* Style: [OSM-liberty](https://github.com/tumic0/QtPBFImagePlugin-styles/blob/master/OpenMapTiles/osm-liberty/style.json)
|
||||
|
||||

|
||||
|
@ -2,10 +2,13 @@ TARGET = pbf
|
||||
TEMPLATE = lib
|
||||
CONFIG += plugin
|
||||
QT += gui
|
||||
VERSION = 4.2
|
||||
VERSION = 2.6
|
||||
|
||||
PROTOS = protobuf/vector_tile.proto
|
||||
include(protobuf/vector_tile.pri)
|
||||
|
||||
INCLUDEPATH += ./protobuf
|
||||
HEADERS += src/pbfhandler.h \
|
||||
src/data.h \
|
||||
src/pbfplugin.h \
|
||||
src/gzip.h \
|
||||
src/pbf.h \
|
||||
@ -20,7 +23,6 @@ HEADERS += src/pbfhandler.h \
|
||||
src/textitem.h \
|
||||
src/sprites.h
|
||||
SOURCES += src/pbfplugin.cpp \
|
||||
src/data.cpp \
|
||||
src/pbfhandler.cpp \
|
||||
src/gzip.cpp \
|
||||
src/pbf.cpp \
|
||||
@ -37,27 +39,25 @@ RESOURCES += pbfplugin.qrc
|
||||
|
||||
DEFINES += QT_NO_DEPRECATED_WARNINGS
|
||||
|
||||
unix:!macx:!android {
|
||||
LIBS += -lz
|
||||
|
||||
target.path += $$[QT_INSTALL_PLUGINS]/imageformats
|
||||
INSTALLS += target
|
||||
unix:!macx{
|
||||
LIBS += -lprotobuf-lite \
|
||||
-lz
|
||||
}
|
||||
win32 {
|
||||
INCLUDEPATH += $$ZLIB/include
|
||||
LIBS += $$ZLIB/lib/zlibstatic.lib
|
||||
INCLUDEPATH += $$PROTOBUF/include \
|
||||
$$ZLIB/include
|
||||
LIBS += $$PROTOBUF/lib/libprotobuf-lite.lib \
|
||||
$$ZLIB/lib/zlibstatic.lib
|
||||
|
||||
QMAKE_TARGET_PRODUCT = QtPBFImagePlugin
|
||||
QMAKE_TARGET_DESCRIPTION = Qt $$QT_VERSION MVT/PBF image plugin
|
||||
QMAKE_TARGET_COPYRIGHT = Copyright (c) 2018-2025 Martin Tuma
|
||||
QMAKE_TARGET_COPYRIGHT = Copyright (c) 2023 Martin Tuma
|
||||
}
|
||||
macx {
|
||||
LIBS += -lz
|
||||
INCLUDEPATH += $$PROTOBUF/include
|
||||
LIBS += $$PROTOBUF/lib/libprotobuf-lite.a \
|
||||
-lz
|
||||
}
|
||||
android {
|
||||
LIBS += -lz
|
||||
|
||||
top_builddir=$$shadowed($$PWD)
|
||||
DESTDIR = $$top_builddir/plugins
|
||||
TARGET = $$qt5LibraryTarget(libpbf, "plugins/imageformats/")
|
||||
}
|
||||
target.path += $$[QT_INSTALL_PLUGINS]/imageformats
|
||||
INSTALLS += target
|
||||
|
17
protobuf/vector_tile.pri
Normal file
17
protobuf/vector_tile.pri
Normal file
@ -0,0 +1,17 @@
|
||||
INCLUDEPATH += $$PWD
|
||||
DEPENDPATH += $$PWD
|
||||
|
||||
protobuf_decl.name = protobuf headers
|
||||
protobuf_decl.input = PROTOS
|
||||
protobuf_decl.output = ${QMAKE_FILE_IN_PATH}/${QMAKE_FILE_BASE}.pb.h
|
||||
protobuf_decl.commands = protoc --cpp_out=${QMAKE_FILE_IN_PATH} --proto_path=${QMAKE_FILE_IN_PATH} ${QMAKE_FILE_NAME}
|
||||
protobuf_decl.variable_out = HEADERS
|
||||
QMAKE_EXTRA_COMPILERS += protobuf_decl
|
||||
|
||||
protobuf_impl.name = protobuf sources
|
||||
protobuf_impl.input = PROTOS
|
||||
protobuf_impl.output = ${QMAKE_FILE_IN_PATH}/${QMAKE_FILE_BASE}.pb.cc
|
||||
protobuf_impl.depends = ${QMAKE_FILE_IN_PATH}/${QMAKE_FILE_BASE}.pb.h
|
||||
protobuf_impl.commands = $$escape_expand(\n)
|
||||
protobuf_impl.variable_out = SOURCES
|
||||
QMAKE_EXTRA_COMPILERS += protobuf_impl
|
78
protobuf/vector_tile.proto
Normal file
78
protobuf/vector_tile.proto
Normal file
@ -0,0 +1,78 @@
|
||||
package vector_tile;
|
||||
|
||||
option optimize_for = LITE_RUNTIME;
|
||||
|
||||
message Tile {
|
||||
|
||||
// GeomType is described in section 4.3.4 of the specification
|
||||
enum GeomType {
|
||||
UNKNOWN = 0;
|
||||
POINT = 1;
|
||||
LINESTRING = 2;
|
||||
POLYGON = 3;
|
||||
}
|
||||
|
||||
// Variant type encoding
|
||||
// The use of values is described in section 4.1 of the specification
|
||||
message Value {
|
||||
// Exactly one of these values must be present in a valid message
|
||||
optional string string_value = 1;
|
||||
optional float float_value = 2;
|
||||
optional double double_value = 3;
|
||||
optional int64 int_value = 4;
|
||||
optional uint64 uint_value = 5;
|
||||
optional sint64 sint_value = 6;
|
||||
optional bool bool_value = 7;
|
||||
|
||||
extensions 8 to max;
|
||||
}
|
||||
|
||||
// Features are described in section 4.2 of the specification
|
||||
message Feature {
|
||||
optional uint64 id = 1 [ default = 0 ];
|
||||
|
||||
// Tags of this feature are encoded as repeated pairs of
|
||||
// integers.
|
||||
// A detailed description of tags is located in sections
|
||||
// 4.2 and 4.4 of the specification
|
||||
repeated uint32 tags = 2 [ packed = true ];
|
||||
|
||||
// The type of geometry stored in this feature.
|
||||
optional GeomType type = 3 [ default = UNKNOWN ];
|
||||
|
||||
// Contains a stream of commands and parameters (vertices).
|
||||
// A detailed description on geometry encoding is located in
|
||||
// section 4.3 of the specification.
|
||||
repeated uint32 geometry = 4 [ packed = true ];
|
||||
}
|
||||
|
||||
// Layers are described in section 4.1 of the specification
|
||||
message Layer {
|
||||
// Any compliant implementation must first read the version
|
||||
// number encoded in this message and choose the correct
|
||||
// implementation for this version number before proceeding to
|
||||
// decode other parts of this message.
|
||||
required uint32 version = 15 [ default = 1 ];
|
||||
|
||||
required string name = 1;
|
||||
|
||||
// The actual features in this tile.
|
||||
repeated Feature features = 2;
|
||||
|
||||
// Dictionary encoding for keys
|
||||
repeated string keys = 3;
|
||||
|
||||
// Dictionary encoding for values
|
||||
repeated Value values = 4;
|
||||
|
||||
// Although this is an "optional" field it is required by the specification.
|
||||
// See https://github.com/mapbox/vector-tile-spec/issues/47
|
||||
optional uint32 extent = 5 [ default = 4096 ];
|
||||
|
||||
extensions 16 to max;
|
||||
}
|
||||
|
||||
repeated Layer layers = 3;
|
||||
|
||||
extensions 16 to 8191;
|
||||
}
|
354
src/data.cpp
354
src/data.cpp
@ -1,354 +0,0 @@
|
||||
#include "data.h"
|
||||
|
||||
#define TYPE(tag) (tag & 0x07)
|
||||
#define FIELD(tag) (tag >> 3)
|
||||
|
||||
#define VARINT 0
|
||||
#define I64 1
|
||||
#define LEN 2
|
||||
#define I32 5
|
||||
|
||||
struct CTX
|
||||
{
|
||||
CTX(const QByteArray &ba)
|
||||
: bp(ba.constData()), be(bp + ba.size()), tag(0) {}
|
||||
|
||||
const char *bp;
|
||||
const char *be;
|
||||
quint32 tag;
|
||||
};
|
||||
|
||||
static inline qint64 zigzag64decode(quint64 value)
|
||||
{
|
||||
return static_cast<qint64>((value >> 1u) ^ static_cast<quint64>(
|
||||
-static_cast<qint64>(value & 1u)));
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
static bool varint(CTX &ctx, T &val)
|
||||
{
|
||||
unsigned int shift = 0;
|
||||
val = 0;
|
||||
|
||||
while (ctx.bp < ctx.be) {
|
||||
val |= ((quint8)*ctx.bp & 0x7F) << shift;
|
||||
shift += 7;
|
||||
if (!((quint8)*ctx.bp++ & 0x80))
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool length(CTX &ctx, qint32 &val)
|
||||
{
|
||||
if (TYPE(ctx.tag) != LEN)
|
||||
return false;
|
||||
|
||||
if (!varint(ctx, val))
|
||||
return false;
|
||||
|
||||
return (val >= 0);
|
||||
}
|
||||
|
||||
static bool str(CTX &ctx, QByteArray &val)
|
||||
{
|
||||
qint32 len;
|
||||
|
||||
if (!length(ctx, len))
|
||||
return false;
|
||||
if (ctx.bp + len > ctx.be)
|
||||
return false;
|
||||
|
||||
/* In Qt5 the (later) conversion to QString is broken when the QByteArray is
|
||||
not nul terminated so we have to use the "deep copy" constructor that
|
||||
nul-terminates the byte array when it is created. */
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
val = QByteArray(ctx.bp, len);
|
||||
#else
|
||||
val = QByteArray::fromRawData(ctx.bp, len);
|
||||
#endif
|
||||
ctx.bp += len;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool dbl(CTX &ctx, double &val)
|
||||
{
|
||||
if (TYPE(ctx.tag) != I64)
|
||||
return false;
|
||||
if (ctx.bp + sizeof(val) > ctx.be)
|
||||
return false;
|
||||
|
||||
memcpy(&val, ctx.bp, sizeof(val));
|
||||
ctx.bp += sizeof(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool flt(CTX &ctx, float &val)
|
||||
{
|
||||
if (TYPE(ctx.tag) != I32)
|
||||
return false;
|
||||
if (ctx.bp + sizeof(val) > ctx.be)
|
||||
return false;
|
||||
|
||||
memcpy(&val, ctx.bp, sizeof(val));
|
||||
ctx.bp += sizeof(val);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool packed(CTX &ctx, QVector<quint32> &vals)
|
||||
{
|
||||
quint32 v;
|
||||
|
||||
if (TYPE(ctx.tag) == LEN) {
|
||||
qint32 len;
|
||||
if (!varint(ctx, len) || len < 0)
|
||||
return false;
|
||||
const char *ee = ctx.bp + len;
|
||||
if (ee > ctx.be)
|
||||
return false;
|
||||
while (ctx.bp < ee) {
|
||||
if (!varint(ctx, v))
|
||||
return false;
|
||||
vals.append(v);
|
||||
}
|
||||
return (ctx.bp == ee);
|
||||
} else if (TYPE(ctx.tag) == VARINT) {
|
||||
if (!varint(ctx, v))
|
||||
return false;
|
||||
vals.append(v);
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
static bool skip(CTX &ctx)
|
||||
{
|
||||
qint32 len = 0;
|
||||
|
||||
switch (TYPE(ctx.tag)) {
|
||||
case VARINT:
|
||||
return varint(ctx, len);
|
||||
case I64:
|
||||
len = 8;
|
||||
break;
|
||||
case LEN:
|
||||
if (!varint(ctx, len) || len < 0)
|
||||
return false;
|
||||
break;
|
||||
case I32:
|
||||
len = 4;
|
||||
break;
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
|
||||
if (ctx.bp + len > ctx.be)
|
||||
return false;
|
||||
ctx.bp += len;
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
static bool value(CTX &ctx, QVariant &val)
|
||||
{
|
||||
QByteArray ba;
|
||||
quint64 num;
|
||||
double dnum;
|
||||
float fnum;
|
||||
qint32 len;
|
||||
|
||||
if (!length(ctx, len))
|
||||
return false;
|
||||
|
||||
const char *ee = ctx.bp + len;
|
||||
if (ee > ctx.be)
|
||||
return false;
|
||||
|
||||
while (ctx.bp < ee) {
|
||||
if (!varint(ctx, ctx.tag))
|
||||
return false;
|
||||
|
||||
switch (FIELD(ctx.tag)) {
|
||||
case 1:
|
||||
if (!str(ctx, ba))
|
||||
return false;
|
||||
val = QVariant(ba);
|
||||
break;
|
||||
case 2:
|
||||
if (!flt(ctx, fnum))
|
||||
return false;
|
||||
val = QVariant(fnum);
|
||||
break;
|
||||
case 3:
|
||||
if (!dbl(ctx, dnum))
|
||||
return false;
|
||||
val = QVariant(dnum);
|
||||
break;
|
||||
case 4:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, num))
|
||||
return false;
|
||||
val = QVariant(static_cast<qint64>(num));
|
||||
break;
|
||||
case 5:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, num))
|
||||
return false;
|
||||
val = QVariant(num);
|
||||
break;
|
||||
case 6:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, num))
|
||||
return false;
|
||||
val = QVariant(zigzag64decode(num));
|
||||
break;
|
||||
case 7:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, num))
|
||||
return false;
|
||||
val = QVariant(num ? true : false);
|
||||
break;
|
||||
default:
|
||||
if (!skip(ctx))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx.bp == ee);
|
||||
}
|
||||
|
||||
static bool feature(CTX &ctx, Data::Feature &f)
|
||||
{
|
||||
qint32 len;
|
||||
quint32 e;
|
||||
|
||||
if (!length(ctx, len))
|
||||
return false;
|
||||
|
||||
const char *ee = ctx.bp + len;
|
||||
if (ee > ctx.be)
|
||||
return false;
|
||||
|
||||
while (ctx.bp < ee) {
|
||||
if (!varint(ctx, ctx.tag))
|
||||
return false;
|
||||
|
||||
switch (FIELD(ctx.tag)) {
|
||||
case 1:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, f.id))
|
||||
return false;
|
||||
break;
|
||||
case 2:
|
||||
if (!packed(ctx, f.tags))
|
||||
return false;
|
||||
break;
|
||||
case 3:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, e))
|
||||
return false;
|
||||
if (e > Data::GeomType::POLYGON)
|
||||
return false;
|
||||
f.type = static_cast<Data::GeomType>(e);
|
||||
break;
|
||||
case 4:
|
||||
if (!packed(ctx, f.geometry))
|
||||
return false;
|
||||
break;
|
||||
default:
|
||||
if (!skip(ctx))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx.bp == ee);
|
||||
}
|
||||
|
||||
static bool layer(CTX &ctx, Data::Layer &l)
|
||||
{
|
||||
qint32 len;
|
||||
|
||||
if (!length(ctx, len))
|
||||
return false;
|
||||
|
||||
const char *ee = ctx.bp + len;
|
||||
if (ee > ctx.be)
|
||||
return false;
|
||||
|
||||
while (ctx.bp < ee) {
|
||||
if (!varint(ctx, ctx.tag))
|
||||
return false;
|
||||
|
||||
switch (FIELD(ctx.tag)) {
|
||||
case 1:
|
||||
if (!str(ctx, l.name))
|
||||
return false;
|
||||
break;
|
||||
case 2:
|
||||
l.features.append(Data::Feature());
|
||||
if (!feature(ctx, l.features.last()))
|
||||
return false;
|
||||
break;
|
||||
case 3:
|
||||
l.keys.append(QByteArray());
|
||||
if (!str(ctx, l.keys.last()))
|
||||
return false;
|
||||
break;
|
||||
case 4:
|
||||
l.values.append(QVariant());
|
||||
if (!value(ctx, l.values.last()))
|
||||
return false;
|
||||
break;
|
||||
case 5:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, l.extent))
|
||||
return false;
|
||||
break;
|
||||
case 15:
|
||||
if (TYPE(ctx.tag) != VARINT)
|
||||
return false;
|
||||
if (!varint(ctx, l.version))
|
||||
return false;
|
||||
break;
|
||||
default:
|
||||
if (!skip(ctx))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx.bp == ee);
|
||||
}
|
||||
|
||||
bool Data::load(const QByteArray &ba)
|
||||
{
|
||||
CTX ctx(ba);
|
||||
|
||||
while (ctx.bp < ctx.be) {
|
||||
if (!varint(ctx, ctx.tag))
|
||||
return false;
|
||||
|
||||
switch (FIELD(ctx.tag)) {
|
||||
case 3:
|
||||
_layers.append(Layer());
|
||||
if (!layer(ctx, _layers.last()))
|
||||
return false;
|
||||
break;
|
||||
default:
|
||||
if (!skip(ctx))
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return (ctx.bp == ctx.be);
|
||||
}
|
47
src/data.h
47
src/data.h
@ -1,47 +0,0 @@
|
||||
#ifndef DATA_H
|
||||
#define DATA_H
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QVector>
|
||||
#include <QVariant>
|
||||
|
||||
class Data
|
||||
{
|
||||
public:
|
||||
enum GeomType {
|
||||
UNKNOWN = 0,
|
||||
POINT = 1,
|
||||
LINESTRING = 2,
|
||||
POLYGON = 3
|
||||
};
|
||||
|
||||
struct Feature
|
||||
{
|
||||
Feature() : id(0), type(UNKNOWN) {}
|
||||
|
||||
quint64 id;
|
||||
QVector<quint32> tags;
|
||||
GeomType type;
|
||||
QVector<quint32> geometry;
|
||||
};
|
||||
|
||||
struct Layer
|
||||
{
|
||||
Layer() : version(1), extent(4096) {}
|
||||
|
||||
quint32 version;
|
||||
QByteArray name;
|
||||
QVector<Feature> features;
|
||||
QVector<QByteArray> keys;
|
||||
QVector<QVariant> values;
|
||||
quint32 extent;
|
||||
};
|
||||
|
||||
bool load(const QByteArray &ba);
|
||||
const QVector<Layer> &layers() const {return _layers;}
|
||||
|
||||
private:
|
||||
QVector<Layer> _layers;
|
||||
};
|
||||
|
||||
#endif // DATA_H
|
@ -6,7 +6,6 @@
|
||||
#include <QColor>
|
||||
#include <QPair>
|
||||
#include <QString>
|
||||
#include <QJsonValue>
|
||||
|
||||
class QJsonObject;
|
||||
|
||||
|
68
src/pbf.cpp
68
src/pbf.cpp
@ -4,6 +4,7 @@
|
||||
#define LINE_TO 2
|
||||
#define CLOSE_PATH 7
|
||||
|
||||
|
||||
static inline qint32 zigzag32decode(quint32 value)
|
||||
{
|
||||
return static_cast<qint32>((value >> 1u) ^ static_cast<quint32>(
|
||||
@ -15,17 +16,37 @@ static inline QPoint parameters(quint32 v1, quint32 v2)
|
||||
return QPoint(zigzag32decode(v1), zigzag32decode(v2));
|
||||
}
|
||||
|
||||
const QVariant *PBF::Feature::value(const QByteArray &key) const
|
||||
static QVariant value(const vector_tile::Tile_Value &val)
|
||||
{
|
||||
if (val.has_bool_value())
|
||||
return QVariant(val.bool_value());
|
||||
else if (val.has_int_value())
|
||||
return QVariant((qlonglong)val.int_value());
|
||||
else if (val.has_sint_value())
|
||||
return QVariant((qlonglong)val.sint_value());
|
||||
else if (val.has_uint_value())
|
||||
return QVariant((qulonglong)val.uint_value());
|
||||
else if (val.has_float_value())
|
||||
return QVariant(val.float_value());
|
||||
else if (val.has_double_value())
|
||||
return QVariant(val.double_value());
|
||||
else if (val.has_string_value())
|
||||
return QVariant(QString::fromStdString(val.string_value()));
|
||||
else
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
const QVariant *PBF::Feature::value(const QString &key) const
|
||||
{
|
||||
const KeyHash &keys(_layer->keys());
|
||||
KeyHash::const_iterator it(keys.find(key));
|
||||
if (it == keys.constEnd())
|
||||
return 0;
|
||||
|
||||
quint32 index = *it;
|
||||
for (int i = 0; i < _data->tags.size(); i = i + 2)
|
||||
if (_data->tags.at(i) == index)
|
||||
return &(_layer->values().at(_data->tags.at(i+1)));
|
||||
google::protobuf::uint32 index = *it;
|
||||
for (int i = 0; i < _data->tags_size(); i = i + 2)
|
||||
if (_data->tags(i) == index)
|
||||
return &(_layer->values().at(_data->tags(i+1)));
|
||||
|
||||
return 0;
|
||||
}
|
||||
@ -35,15 +56,15 @@ QPainterPath PBF::Feature::path(const QSizeF &factor) const
|
||||
QPoint cursor;
|
||||
QPainterPath path;
|
||||
|
||||
for (int i = 0; i < _data->geometry.size(); i++) {
|
||||
quint32 g = _data->geometry.at(i);
|
||||
for (int i = 0; i < _data->geometry_size(); i++) {
|
||||
quint32 g = _data->geometry(i);
|
||||
unsigned cmdId = g & 0x7;
|
||||
unsigned cmdCount = g >> 3;
|
||||
|
||||
if (cmdId == MOVE_TO) {
|
||||
for (unsigned j = 0; j < cmdCount; j++) {
|
||||
QPoint offset = parameters(_data->geometry.at(i+1),
|
||||
_data->geometry.at(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.moveTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -51,8 +72,8 @@ QPainterPath PBF::Feature::path(const QSizeF &factor) const
|
||||
}
|
||||
} else if (cmdId == LINE_TO) {
|
||||
for (unsigned j = 0; j < cmdCount; j++) {
|
||||
QPoint offset = parameters(_data->geometry.at(i+1),
|
||||
_data->geometry.at(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.lineTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -67,23 +88,26 @@ QPainterPath PBF::Feature::path(const QSizeF &factor) const
|
||||
return path;
|
||||
}
|
||||
|
||||
PBF::Layer::Layer(const Data::Layer *layer) : _data(layer)
|
||||
PBF::Layer::Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
_keys.reserve(layer->keys.size());
|
||||
for (int i = 0; i < layer->keys.size(); i++)
|
||||
_keys.insert(layer->keys.at(i), i);
|
||||
_keys.reserve(data->keys_size());
|
||||
for (int i = 0; i < data->keys_size(); i++)
|
||||
_keys.insert(QString::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)));
|
||||
|
||||
_features.reserve(layer->features.size());
|
||||
for (int i = 0; i < layer->features.size(); i++)
|
||||
_features.append(Feature(&(layer->features.at(i)), this));
|
||||
_features.reserve(data->features_size());
|
||||
for (int i = 0; i < data->features_size(); i++)
|
||||
_features.append(Feature(&(data->features(i)), this));
|
||||
std::sort(_features.begin(), _features.end());
|
||||
}
|
||||
|
||||
PBF::PBF(const Data &data)
|
||||
PBF::PBF(const vector_tile::Tile &tile)
|
||||
{
|
||||
for (int i = 0; i < data.layers().size(); i++) {
|
||||
const Data::Layer &layer = data.layers().at(i);
|
||||
_layers.insert(layer.name, new Layer(&layer));
|
||||
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));
|
||||
}
|
||||
}
|
||||
|
||||
|
29
src/pbf.h
29
src/pbf.h
@ -5,10 +5,10 @@
|
||||
#include <QVector>
|
||||
#include <QHash>
|
||||
#include <QPainterPath>
|
||||
#include "data.h"
|
||||
#include "vector_tile.pb.h"
|
||||
|
||||
|
||||
typedef QHash<QByteArray, quint32> KeyHash;
|
||||
typedef QHash<QString, google::protobuf::uint32> KeyHash;
|
||||
|
||||
class PBF
|
||||
{
|
||||
@ -19,17 +19,17 @@ public:
|
||||
{
|
||||
public:
|
||||
Feature() : _data(0), _layer(0) {}
|
||||
Feature(const Data::Feature *data, const Layer *layer)
|
||||
Feature(const vector_tile::Tile_Feature *data, const Layer *layer)
|
||||
: _data(data), _layer(layer) {}
|
||||
|
||||
const QVariant *value(const QByteArray &key) const;
|
||||
Data::GeomType type() const {return _data->type;}
|
||||
const QVariant *value(const QString &key) const;
|
||||
vector_tile::Tile_GeomType type() const {return _data->type();}
|
||||
QPainterPath path(const QSizeF &factor) const;
|
||||
|
||||
friend bool operator<(const Feature &f1, const Feature &f2);
|
||||
|
||||
private:
|
||||
const Data::Feature *_data;
|
||||
const vector_tile::Tile_Feature *_data;
|
||||
const Layer *_layer;
|
||||
};
|
||||
|
||||
@ -37,30 +37,31 @@ public:
|
||||
{
|
||||
public:
|
||||
|
||||
Layer(const Data::Layer *layer);
|
||||
Layer(const vector_tile::Tile_Layer *data);
|
||||
|
||||
const QVector<Feature> &features() const {return _features;}
|
||||
const QVector<QVariant> &values() const {return _data->values;}
|
||||
const QVector<QVariant> &values() const {return _values;}
|
||||
const KeyHash &keys() const {return _keys;}
|
||||
const Data::Layer *data() const {return _data;}
|
||||
const vector_tile::Tile_Layer *data() const {return _data;}
|
||||
|
||||
private:
|
||||
const Data::Layer *_data;
|
||||
const vector_tile::Tile_Layer *_data;
|
||||
QVector<Feature> _features;
|
||||
QVector<QVariant> _values;
|
||||
KeyHash _keys;
|
||||
};
|
||||
|
||||
|
||||
PBF(const Data &data);
|
||||
PBF(const vector_tile::Tile &tile);
|
||||
~PBF();
|
||||
|
||||
const QHash<QByteArray, Layer*> &layers() const {return _layers;}
|
||||
const QHash<QString, Layer*> &layers() const {return _layers;}
|
||||
|
||||
private:
|
||||
QHash<QByteArray, Layer*> _layers;
|
||||
QHash<QString, Layer*> _layers;
|
||||
};
|
||||
|
||||
inline bool operator<(const PBF::Feature &f1, const PBF::Feature &f2)
|
||||
{return f1._data->id < f2._data->id;}
|
||||
{return f1._data->id() < f2._data->id();}
|
||||
|
||||
#endif // PBF_H
|
||||
|
@ -3,7 +3,6 @@
|
||||
#include <QtEndian>
|
||||
#include <QDebug>
|
||||
#include "gzip.h"
|
||||
#include "data.h"
|
||||
#include "tile.h"
|
||||
#include "style.h"
|
||||
#include "pbfhandler.h"
|
||||
@ -74,25 +73,22 @@ bool PBFHandler::read(QImage *image)
|
||||
}
|
||||
} else if (isPlainPBF(magic))
|
||||
ba = device()->readAll();
|
||||
Data data;
|
||||
if (!data.load(ba)) {
|
||||
vector_tile::Tile data;
|
||||
if (!data.ParseFromArray(ba.constData(), ba.size())) {
|
||||
qCritical() << "Invalid PBF data";
|
||||
return false;
|
||||
}
|
||||
|
||||
QList<QByteArray> list(format().split(';'));
|
||||
int zoom = list.size() ? list.first().toInt() : 0;
|
||||
int overzoom = (list.size() > 1) ? list.at(1).toInt() : 0;
|
||||
|
||||
QSize scaledSize(_scaledSize.isValid()
|
||||
? _scaledSize : QSize(TILE_SIZE, TILE_SIZE));
|
||||
QSize size(scaledSize.width()<<overzoom,
|
||||
scaledSize.height()<<overzoom);
|
||||
QPointF scale((qreal)scaledSize.width() / TILE_SIZE,
|
||||
(qreal)scaledSize.height() / TILE_SIZE);
|
||||
bool ok;
|
||||
int zoom = format().toInt(&ok);
|
||||
|
||||
QSize size = _scaledSize.isValid()
|
||||
? _scaledSize : QSize(TILE_SIZE, TILE_SIZE);
|
||||
QPointF scale = _scaledSize.isValid()
|
||||
? QPointF((qreal)_scaledSize.width() / TILE_SIZE,
|
||||
(qreal)_scaledSize.height() / TILE_SIZE) : QPointF(1.0, 1.0);
|
||||
*image = QImage(size, QImage::Format_ARGB32_Premultiplied);
|
||||
Tile tile(image, zoom, scale);
|
||||
Tile tile(image, ok ? zoom : -1, scale);
|
||||
|
||||
_style->render(data, tile);
|
||||
|
||||
|
@ -11,7 +11,7 @@ class Style;
|
||||
class PBFHandler : public QImageIOHandler
|
||||
{
|
||||
public:
|
||||
PBFHandler(Style *style) : _style(style) {}
|
||||
PBFHandler(const Style *style) : _style(style) {}
|
||||
~PBFHandler() {}
|
||||
|
||||
bool canRead() const;
|
||||
@ -24,7 +24,7 @@ public:
|
||||
static bool canRead(QIODevice *device);
|
||||
|
||||
private:
|
||||
Style *_style;
|
||||
const Style *_style;
|
||||
QSize _scaledSize;
|
||||
};
|
||||
|
||||
|
@ -4,28 +4,22 @@
|
||||
#include <QDebug>
|
||||
#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));
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
static QImage img(fileName);
|
||||
return img;
|
||||
}
|
||||
|
||||
Sprites::Sprite::Sprite(const QJsonObject &json)
|
||||
: _pixelRatio(1.0), _sdf(false)
|
||||
{
|
||||
int x, y, width, height;
|
||||
|
||||
|
||||
if (json.contains("x") && json["x"].isDouble())
|
||||
x = json["x"].toInt();
|
||||
else
|
||||
@ -45,14 +39,17 @@ Sprites::Sprite::Sprite(const QJsonObject &json)
|
||||
|
||||
_rect = QRect(x, y, width, height);
|
||||
|
||||
|
||||
if (json.contains("pixelRatio") && json["pixelRatio"].isDouble())
|
||||
_pixelRatio = json["pixelRatio"].toDouble();
|
||||
if (json.contains("sdf") && json["sdf"].isBool())
|
||||
_sdf = json["sdf"].toBool();
|
||||
else
|
||||
_pixelRatio = 1.0;
|
||||
}
|
||||
|
||||
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";
|
||||
@ -81,60 +78,27 @@ 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::sprite(const Sprite &sprite, const QColor &color, qreal scale)
|
||||
QImage Sprites::icon(const QString &name) const
|
||||
{
|
||||
_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()))
|
||||
if (_imageFile.isEmpty())
|
||||
return QImage();
|
||||
|
||||
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())
|
||||
const QImage &img = atlas(_imageFile);
|
||||
if (img.isNull())
|
||||
return QImage();
|
||||
|
||||
QMap<QString, Sprite>::const_iterator it = _sprites.constFind(name);
|
||||
QMap<QString, Sprite>::const_iterator it = _sprites.find(name);
|
||||
if (it == _sprites.constEnd())
|
||||
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;
|
||||
}
|
||||
|
@ -5,19 +5,16 @@
|
||||
#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 QColor &color = Qt::black,
|
||||
qreal size = 1.0);
|
||||
QImage icon(const QString &name) const;
|
||||
|
||||
private:
|
||||
class Sprite {
|
||||
@ -26,21 +23,14 @@ 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
|
||||
|
133
src/style.cpp
133
src/style.cpp
@ -8,36 +8,23 @@
|
||||
#include <QRegularExpression>
|
||||
#include <QDebug>
|
||||
#include "text.h"
|
||||
#include "color.h"
|
||||
#include "font.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
#include "style.h"
|
||||
|
||||
|
||||
static Data::GeomType geometryType(const QString &str)
|
||||
static vector_tile::Tile_GeomType geometryType(const QString &str)
|
||||
{
|
||||
if (str == "Point")
|
||||
return Data::GeomType::POINT;
|
||||
return vector_tile::Tile_GeomType_POINT;
|
||||
else if (str == "LineString")
|
||||
return Data::GeomType::LINESTRING;
|
||||
return vector_tile::Tile_GeomType_LINESTRING;
|
||||
else if (str == "Polygon")
|
||||
return Data::GeomType::POLYGON;
|
||||
return vector_tile::Tile_GeomType_POLYGON;
|
||||
else
|
||||
return Data::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();
|
||||
}
|
||||
return vector_tile::Tile_GeomType_UNKNOWN;
|
||||
}
|
||||
|
||||
Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
@ -49,50 +36,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<QByteArray, QVariant>(QByteArray(),
|
||||
_kv = QPair<QString, QVariant>(QString(),
|
||||
QVariant(geometryType(json.at(2).toString())));
|
||||
} else {
|
||||
_type = EQ;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
}
|
||||
} else if (type == "!=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = NE;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
} else if (type == "<") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = LT;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
} else if (type == "<=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = LE;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
} else if (type == ">") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = GT;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
} else if (type == ">=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = GE;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
variant(json.at(2)));
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
} else if (type == "all") {
|
||||
_type = All;
|
||||
for (int i = 1; i < json.size(); i++)
|
||||
@ -105,32 +92,28 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
if (json.size() < 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = In;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
for (int i = 2; i < json.size(); i++)
|
||||
_set.insert(json.at(i).toString().toUtf8());
|
||||
_set.insert(json.at(i).toString());
|
||||
} else if (type == "!in") {
|
||||
if (json.size() < 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = In;
|
||||
_not = true;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
for (int i = 2; i < json.size(); i++)
|
||||
_set.insert(json.at(i).toString().toUtf8());
|
||||
_set.insert(json.at(i).toString());
|
||||
} else if (type == "has") {
|
||||
if (json.size() < 2)
|
||||
INVALID_FILTER(json);
|
||||
_type = Has;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
} else if (type == "!has") {
|
||||
if (json.size() < 2)
|
||||
INVALID_FILTER(json);
|
||||
_type = Has;
|
||||
_not = true;
|
||||
_kv = QPair<QByteArray, QVariant>(json.at(1).toString().toUtf8(),
|
||||
QVariant());
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(), QVariant());
|
||||
} else
|
||||
INVALID_FILTER(json);
|
||||
}
|
||||
@ -200,7 +183,7 @@ bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return _not;
|
||||
else
|
||||
return _set.contains((*v).toByteArray()) ^ _not;
|
||||
return _set.contains((*v).toString()) ^ _not;
|
||||
case Has:
|
||||
return (feature.value(_kv.first) ? true : false) ^ _not;
|
||||
case All:
|
||||
@ -222,7 +205,7 @@ bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
||||
|
||||
QString Style::Layer::Template::value(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
static QRegularExpression rx("\\{[^\\}]*\\}");
|
||||
QRegularExpression rx("\\{[^\\}]*\\}");
|
||||
QString text(_field.value(zoom));
|
||||
QRegularExpressionMatchIterator it = rx.globalMatch(text);
|
||||
QStringList keys;
|
||||
@ -234,7 +217,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.toUtf8());
|
||||
const QVariant *val = feature.value(key);
|
||||
text.replace(QString("{%1}").arg(key), val ? val->toString() : "");
|
||||
}
|
||||
|
||||
@ -275,9 +258,6 @@ 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
|
||||
@ -313,7 +293,7 @@ QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
||||
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
|
||||
{
|
||||
QColor color;
|
||||
@ -390,7 +370,6 @@ 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"]);
|
||||
@ -504,7 +483,7 @@ Style::Layer::Layer(const QJsonObject &json)
|
||||
_type = Symbol;
|
||||
|
||||
// source-layer
|
||||
_sourceLayer = json["source-layer"].toString().toUtf8();
|
||||
_sourceLayer = json["source-layer"].toString();
|
||||
|
||||
// zooms
|
||||
if (json.contains("minzoom") && json["minzoom"].isDouble())
|
||||
@ -533,7 +512,7 @@ bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
|
||||
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();
|
||||
int zoom = tile.zoom();
|
||||
@ -566,18 +545,14 @@ void Style::Layer::setTextProperties(Tile &tile) const
|
||||
}
|
||||
|
||||
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 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())
|
||||
QString text = _layout.text(tile.zoom(), feature);
|
||||
if (text.isEmpty())
|
||||
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,
|
||||
@ -590,7 +565,7 @@ static bool loadSprites(const QDir &styleDir, const QString &json,
|
||||
if (QFileInfo::exists(spritesImg))
|
||||
return sprites.load(spritesJSON, spritesImg);
|
||||
else {
|
||||
qWarning() << spritesImg << ": no such file";
|
||||
qCritical() << spritesImg << ": no such file";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
@ -623,20 +598,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;
|
||||
}
|
||||
|
||||
Sprites &Style::sprites(const QPointF &scale)
|
||||
const Sprites &Style::sprites(const QPointF &scale) const
|
||||
{
|
||||
return (scale.x() > 1.0 || scale.y() > 1.0)
|
||||
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
||||
}
|
||||
|
||||
void Style::setupLayer(Tile &tile, const Layer &layer)
|
||||
void Style::setupLayer(Tile &tile, const Layer &layer) const
|
||||
{
|
||||
if (layer.isSymbol())
|
||||
layer.setTextProperties(tile);
|
||||
@ -644,7 +619,7 @@ void Style::setupLayer(Tile &tile, const Layer &layer)
|
||||
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(),
|
||||
tile.size().height() / tile.scale().y()));
|
||||
@ -659,10 +634,13 @@ void Style::drawBackground(Tile &tile)
|
||||
_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)
|
||||
Tile &tile, const QSizeF &factor) const
|
||||
{
|
||||
if (!layer.match(tile.zoom(), feature))
|
||||
return;
|
||||
@ -678,17 +656,17 @@ void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
}
|
||||
|
||||
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;
|
||||
|
||||
if (!styleLayer.isVisible())
|
||||
return;
|
||||
|
||||
QSizeF factor(tile.size().width() / tile.scale().x() /
|
||||
(qreal)pbfLayer.data()->extent, tile.size().height() / tile.scale().y()
|
||||
/ (qreal)pbfLayer.data()->extent);
|
||||
(qreal)pbfLayer.data()->extent(), tile.size().height() / tile.scale().y()
|
||||
/ (qreal)pbfLayer.data()->extent());
|
||||
|
||||
tile.painter().save();
|
||||
setupLayer(tile, styleLayer);
|
||||
@ -697,12 +675,12 @@ void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
tile.painter().restore();
|
||||
}
|
||||
|
||||
void Style::render(const PBF &data, Tile &tile)
|
||||
void Style::render(const PBF &data, Tile &tile) const
|
||||
{
|
||||
drawBackground(tile);
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++) {
|
||||
QHash<QByteArray, PBF::Layer*>::const_iterator it = data.layers().find(
|
||||
QHash<QString, PBF::Layer*>::const_iterator it = data.layers().find(
|
||||
_layers.at(i).sourceLayer());
|
||||
if (it == data.layers().constEnd())
|
||||
continue;
|
||||
@ -711,11 +689,4 @@ void Style::render(const PBF &data, Tile &tile)
|
||||
}
|
||||
|
||||
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);
|
||||
void render(const PBF &data, Tile &tile) const;
|
||||
|
||||
private:
|
||||
class Layer {
|
||||
@ -37,17 +37,17 @@ private:
|
||||
Layer() : _type(Unknown), _minZoom(0), _maxZoom(24) {}
|
||||
Layer(const QJsonObject &json);
|
||||
|
||||
const QByteArray &sourceLayer() const {return _sourceLayer;}
|
||||
const QString &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, Sprites &sprites) const;
|
||||
void setPathPainter(Tile &tile, const Sprites &sprites) const;
|
||||
void setTextProperties(Tile &tile) const;
|
||||
void addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const PBF::Feature &feature, Sprites &sprites) const;
|
||||
const PBF::Feature &feature, const Sprites &sprites) const;
|
||||
|
||||
private:
|
||||
enum Type {
|
||||
@ -74,8 +74,8 @@ private:
|
||||
|
||||
Type _type;
|
||||
bool _not;
|
||||
QSet<QByteArray> _set;
|
||||
QPair<QByteArray, QVariant> _kv;
|
||||
QSet<QString> _set;
|
||||
QPair<QString, QVariant> _kv;
|
||||
QVector<Filter> _filters;
|
||||
};
|
||||
|
||||
@ -103,8 +103,6 @@ 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;
|
||||
@ -118,7 +116,6 @@ private:
|
||||
|
||||
Template _text;
|
||||
Template _icon;
|
||||
FunctionF _iconSize;
|
||||
FunctionF _textSize;
|
||||
FunctionF _textMaxWidth;
|
||||
FunctionF _textMaxAngle;
|
||||
@ -138,15 +135,13 @@ private:
|
||||
Paint(const QJsonObject &json);
|
||||
|
||||
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;
|
||||
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;
|
||||
@ -155,7 +150,6 @@ private:
|
||||
FunctionC _fillColor;
|
||||
FunctionC _fillOutlineColor;
|
||||
FunctionC _backgroundColor;
|
||||
FunctionC _iconColor;
|
||||
FunctionF _fillOpacity;
|
||||
FunctionF _lineOpacity;
|
||||
FunctionF _lineWidth;
|
||||
@ -167,21 +161,21 @@ private:
|
||||
};
|
||||
|
||||
Type _type;
|
||||
QByteArray _sourceLayer;
|
||||
QString _sourceLayer;
|
||||
int _minZoom, _maxZoom;
|
||||
Filter _filter;
|
||||
Layout _layout;
|
||||
Paint _paint;
|
||||
};
|
||||
|
||||
Sprites &sprites(const QPointF &scale);
|
||||
const Sprites &sprites(const QPointF &scale) const;
|
||||
|
||||
void drawBackground(Tile &tile);
|
||||
void setupLayer(Tile &tile, const Layer &layer);
|
||||
void drawBackground(Tile &tile) const;
|
||||
void setupLayer(Tile &tile, const Layer &layer) const;
|
||||
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,
|
||||
Tile &tile);
|
||||
Tile &tile) const;
|
||||
|
||||
QVector<Layer> _layers;
|
||||
Sprites _sprites, _sprites2x;
|
||||
|
11
src/text.cpp
11
src/text.cpp
@ -14,10 +14,15 @@ 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()))
|
||||
if (ti->isVisible() && _sceneRect.intersects(ti->boundingRect())
|
||||
&& !set.contains(ti->text())) {
|
||||
ti->paint(painter);
|
||||
set.insert(ti->text());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -46,8 +51,6 @@ 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:
|
||||
@ -70,7 +73,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);
|
||||
}
|
||||
|
@ -2,6 +2,13 @@
|
||||
#include <QPainter>
|
||||
#include "textpathitem.h"
|
||||
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
|
||||
#define INTERSECTS intersect
|
||||
#else // QT 5.15
|
||||
#define INTERSECTS intersects
|
||||
#endif // QT 5.15
|
||||
|
||||
static void swap(const QLineF &line, QPointF *p1, QPointF *p2)
|
||||
{
|
||||
|
||||
@ -23,10 +30,10 @@ static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
||||
{
|
||||
QPointF *p = p1;
|
||||
|
||||
if (line.intersects(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
p = p2;
|
||||
if (line.intersects(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection) {
|
||||
if (p == p2) {
|
||||
swap(line, p1, p2);
|
||||
@ -34,7 +41,7 @@ static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
||||
}
|
||||
p = p2;
|
||||
}
|
||||
if (line.intersects(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection) {
|
||||
if (p == p2) {
|
||||
swap(line, p1, p2);
|
||||
@ -42,7 +49,7 @@ static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
||||
}
|
||||
p = p2;
|
||||
}
|
||||
if (line.intersects(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection) {
|
||||
if (p == p2) {
|
||||
swap(line, p1, p2);
|
||||
@ -57,16 +64,16 @@ static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
||||
|
||||
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p)
|
||||
{
|
||||
if (line.intersects(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return true;
|
||||
if (line.intersects(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return true;
|
||||
if (line.intersects(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return true;
|
||||
if (line.intersects(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return true;
|
||||
|
||||
|
@ -85,7 +85,7 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
||||
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||
_anchor(anchor)
|
||||
{
|
||||
_textRect = text.isEmpty() ? QRectF() : fuzzyBoundingRect();
|
||||
_textRect = fuzzyBoundingRect();
|
||||
_boundingRect = moveTextRect(_textRect);
|
||||
|
||||
if (!_icon.isNull()) {
|
||||
|
@ -2265,6 +2265,7 @@
|
||||
"source": "openmaptiles",
|
||||
"source-layer": "building",
|
||||
"minzoom": 13,
|
||||
"maxzoom": 14,
|
||||
"layout": {
|
||||
"visibility": "visible"
|
||||
},
|
||||
@ -4058,4 +4059,4 @@
|
||||
}
|
||||
],
|
||||
"id": "osm-liberty"
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user