mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2025-07-02 05:59:16 +02:00
Compare commits
77 Commits
Author | SHA1 | Date | |
---|---|---|---|
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 | |||
c86eb7bac2 | |||
3ef6c55d20 | |||
98933deb0e | |||
6e1bc09d62 | |||
ea98da4a74 | |||
8261ee2e79 | |||
d5c315efbe | |||
39ffdaf616 | |||
38b6e2320b | |||
b36fb5fa92 | |||
a7b7d16f4f | |||
8cf73b5bb5 | |||
038f4e7d64 | |||
aaff1c0bdb | |||
37c6c36204 | |||
f901243baa | |||
19c6b9854f | |||
52d15c63bd | |||
71331b06f1 | |||
d6a24a4498 | |||
a26c86dbd8 | |||
0db93fd5ee | |||
f8044efda6 | |||
450a1458a3 | |||
bff8c7229c | |||
ff569644b6 | |||
6a32f0a447 | |||
1051a1c5b2 | |||
11e360d9a4 | |||
5577590f6b | |||
9dba35082b | |||
76f1a23f8d | |||
f931b653c4 | |||
b0885a4b05 | |||
7c34a2c4e1 | |||
0cb3edda05 | |||
871aad37da | |||
12c878d8e2 | |||
44862114ab | |||
1065a27bfb | |||
3c22cd7823 | |||
1afd242302 | |||
ae8b2f00db | |||
e22ea9aa12 | |||
62a78fdaae | |||
63f86a900e | |||
ea8819a381 |
32
.appveyor.yml
Normal file
32
.appveyor.yml
Normal file
@ -0,0 +1,32 @@
|
|||||||
|
version: 3.1.{build}
|
||||||
|
|
||||||
|
configuration:
|
||||||
|
- Release
|
||||||
|
|
||||||
|
image:
|
||||||
|
- Visual Studio 2022
|
||||||
|
|
||||||
|
environment:
|
||||||
|
VCPKGDIR: C:\tools\vcpkg\installed\x64-windows-static-md
|
||||||
|
matrix:
|
||||||
|
- QTDIR: C:\Qt\5.15\msvc2019_64
|
||||||
|
- QTDIR: C:\Qt\6.7\msvc2019_64
|
||||||
|
|
||||||
|
install:
|
||||||
|
- cmd: |-
|
||||||
|
set PATH=%QTDIR%\bin;%VCPKGDIR%\tools\protobuf;%PATH%
|
||||||
|
call "C:\Program Files\Microsoft Visual Studio\2022\Community\VC\Auxiliary\Build\vcvars64.bat
|
||||||
|
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 PROTOBUF=%VCPKGDIR% ZLIB=%VCPKGDIR% pbfplugin.pro
|
||||||
|
nmake release
|
||||||
|
|
||||||
|
artifacts:
|
||||||
|
- 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
|
22
.github/workflows/linux.yml
vendored
Normal file
22
.github/workflows/linux.yml
vendored
Normal file
@ -0,0 +1,22 @@
|
|||||||
|
name: Linux
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
build:
|
||||||
|
name: QtPBFImagePlugin
|
||||||
|
runs-on: ubuntu-20.04
|
||||||
|
steps:
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
sudo apt-get update
|
||||||
|
sudo apt-get install qtbase5-dev qtbase5-dev-tools qt5-qmake libprotobuf-dev protobuf-compiler zlib1g-dev
|
||||||
|
- name: Configure build
|
||||||
|
run: qmake pbfplugin.pro
|
||||||
|
- name: Build project
|
||||||
|
run: make -j2
|
41
.github/workflows/osx.yml
vendored
Normal file
41
.github/workflows/osx.yml
vendored
Normal file
@ -0,0 +1,41 @@
|
|||||||
|
name: OS X
|
||||||
|
|
||||||
|
on:
|
||||||
|
push:
|
||||||
|
branches:
|
||||||
|
- master
|
||||||
|
|
||||||
|
jobs:
|
||||||
|
qt5:
|
||||||
|
name: QtPBFImagePlugin Qt5 build
|
||||||
|
runs-on: macos-latest
|
||||||
|
steps:
|
||||||
|
- name: Set environment variables
|
||||||
|
run: echo "PATH=/opt/homebrew/opt/qt@5/bin:/opt/homebrew/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
brew update
|
||||||
|
brew install qt@5 protobuf@21
|
||||||
|
- name: Configure build
|
||||||
|
run: qmake PROTOBUF=/opt/homebrew/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=/opt/homebrew/opt/qt@6/bin:/opt/homebrew/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
|
||||||
|
- name: Checkout
|
||||||
|
uses: actions/checkout@v4
|
||||||
|
- name: Install dependencies
|
||||||
|
run: |
|
||||||
|
brew update
|
||||||
|
brew install qt@6 protobuf@21
|
||||||
|
- name: Configure build
|
||||||
|
run: qmake PROTOBUF=/opt/homebrew/opt/protobuf@21 pbfplugin.pro
|
||||||
|
- name: Build project
|
||||||
|
run: make -j3
|
23
.travis.yml
23
.travis.yml
@ -1,23 +0,0 @@
|
|||||||
language: c++
|
|
||||||
dist: focal
|
|
||||||
|
|
||||||
os:
|
|
||||||
- linux
|
|
||||||
- osx
|
|
||||||
|
|
||||||
env:
|
|
||||||
- QT_SELECT=qt5
|
|
||||||
|
|
||||||
before_install:
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get -qq update; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew update; fi
|
|
||||||
|
|
||||||
install:
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then brew install qt protobuf; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then sudo apt-get install qtbase5-dev qtbase5-dev-tools qt5-qmake libprotobuf-dev protobuf-compiler; fi
|
|
||||||
|
|
||||||
script:
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then PATH=/usr/local/opt/qt/bin/:${PATH}; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "osx" ]]; then qmake PROTOBUF=/usr/local pbfplugin.pro; fi
|
|
||||||
- if [[ "$TRAVIS_OS_NAME" == "linux" ]]; then qmake pbfplugin.pro; fi
|
|
||||||
- make
|
|
42
README.md
42
README.md
@ -7,7 +7,7 @@ displaying raster MBTiles maps or raster XYZ online maps to also display
|
|||||||
PBF(MVT) vector tiles without (almost, see usage) any application modifications.
|
PBF(MVT) vector tiles without (almost, see usage) any application modifications.
|
||||||
|
|
||||||
Standard Mapbox GL Styles are used for styling the maps. Most relevant style
|
Standard Mapbox GL Styles are used for styling the maps. Most relevant style
|
||||||
features used by [Maputnik](http://editor.openmaptiles.org) are supported.
|
features used by [Maputnik](https://maputnik.github.io/editor) are supported.
|
||||||
A default fallback style (OSM-Liberty) for OpenMapTiles is part of the plugin.
|
A default fallback style (OSM-Liberty) for OpenMapTiles is part of the plugin.
|
||||||
|
|
||||||
"Plain" PBF files as well as gzip compressed files (as used in MBTiles) are
|
"Plain" PBF files as well as gzip compressed files (as used in MBTiles) are
|
||||||
@ -18,24 +18,39 @@ to fit the styles and available data (OpenMapTiles, Mapbox tiles).
|
|||||||
Due to a major design flaw in the Mapbox vector tiles specification - the zoom
|
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
|
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*
|
the zoom level is necessary. This is done by exploiting the optional *format*
|
||||||
parameter of the QImage constructor or the QImage::fromData() or
|
parameter of the QImage constructor or the *QImage::loadFromData()* or
|
||||||
QPixmap::loadFromData() functions. The zoom number is passed as ASCII string
|
*QPixmap::loadFromData()* functions. The zoom number is passed as ASCII string
|
||||||
to the functions:
|
to the functions:
|
||||||
```cpp
|
```cpp
|
||||||
QPixmap pm;
|
QImage img;
|
||||||
pm.loadFromData(tileData, QString::number(zoom).toLatin1());
|
img.loadFromData(data, QByteArray::number(zoom));
|
||||||
```
|
```
|
||||||
The plugin supports vector scaling using QImageReader's setScaledSize() method,
|
|
||||||
|
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,
|
||||||
so when used like in the following example:
|
so when used like in the following example:
|
||||||
```cpp
|
```cpp
|
||||||
QImageReader reader(file, QString::number(zoom).toLatin1());
|
QImage img;
|
||||||
|
QImageReader reader(file, QByteArray::number(zoom));
|
||||||
reader.setScaledSize(QSize(1024, 1024));
|
reader.setScaledSize(QSize(1024, 1024));
|
||||||
reader.read(&image);
|
reader.read(&img);
|
||||||
```
|
```
|
||||||
you will get 1024x1024px tiles with a pixel ratio of 2 (= HiDPI tiles).
|
you will get 1024x1024px tiles with a pixel ratio of 2 (= HiDPI tiles).
|
||||||
|
|
||||||
For a sample code see the [pbf2png](https://github.com/tumic0/pbf2png)
|
### Overzoom
|
||||||
conversion utility.
|
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.
|
||||||
|
|
||||||
## Styles
|
## Styles
|
||||||
The map style is loaded from the
|
The map style is loaded from the
|
||||||
@ -43,7 +58,8 @@ The map style is loaded from the
|
|||||||
file on plugin load. If the style uses a sprite, the sprite JSON file must
|
file on plugin load. If the style uses a sprite, the sprite JSON file must
|
||||||
be named `sprite.json` and the sprite image `sprite.png` and both files must be
|
be named `sprite.json` and the sprite image `sprite.png` and both files must be
|
||||||
placed in the same directory as the style itself. *A style compatible with the
|
placed in the same directory as the style itself. *A style compatible with the
|
||||||
tiles data schema (Mapbox, OpenMapTiles, Tilezen) must be used.*
|
tiles data schema (Mapbox, OpenMapTiles, Tilezen, Ordnance Survey, Esri, ...)
|
||||||
|
must be used.*
|
||||||
|
|
||||||
For a list of "ready to use" styles see the
|
For a list of "ready to use" styles see the
|
||||||
[QtPBFImagePlugin-styles](https://github.com/tumic0/QtPBFImagePlugin-styles)
|
[QtPBFImagePlugin-styles](https://github.com/tumic0/QtPBFImagePlugin-styles)
|
||||||
@ -51,7 +67,7 @@ repository.
|
|||||||
|
|
||||||
## Build
|
## Build
|
||||||
### Requirements
|
### Requirements
|
||||||
* Qt5 >= 5.12 or Qt6
|
* Qt5 >= 5.11 or Qt6
|
||||||
* Google Protocol Buffers (protobuf-lite)
|
* Google Protocol Buffers (protobuf-lite)
|
||||||
* Zlib
|
* Zlib
|
||||||
|
|
||||||
@ -92,7 +108,7 @@ algorithm to work properly.
|
|||||||
A picture is worth a thousand words.
|
A picture is worth a thousand words.
|
||||||
#### OpenMapTiles
|
#### 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)
|
* Style: [OSM-liberty](https://github.com/tumic0/QtPBFImagePlugin-styles/blob/master/OpenMapTiles/osm-liberty/style.json)
|
||||||
|
|
||||||

|

|
||||||
|
@ -2,6 +2,7 @@ TARGET = pbf
|
|||||||
TEMPLATE = lib
|
TEMPLATE = lib
|
||||||
CONFIG += plugin
|
CONFIG += plugin
|
||||||
QT += gui
|
QT += gui
|
||||||
|
VERSION = 3.1
|
||||||
|
|
||||||
PROTOS = protobuf/vector_tile.proto
|
PROTOS = protobuf/vector_tile.proto
|
||||||
include(protobuf/vector_tile.pri)
|
include(protobuf/vector_tile.pri)
|
||||||
@ -38,21 +39,34 @@ RESOURCES += pbfplugin.qrc
|
|||||||
|
|
||||||
DEFINES += QT_NO_DEPRECATED_WARNINGS
|
DEFINES += QT_NO_DEPRECATED_WARNINGS
|
||||||
|
|
||||||
unix:!macx{
|
unix:!macx:!android {
|
||||||
LIBS += -lprotobuf-lite \
|
LIBS += -lprotobuf-lite \
|
||||||
-lz
|
-lz
|
||||||
|
|
||||||
|
target.path += $$[QT_INSTALL_PLUGINS]/imageformats
|
||||||
|
INSTALLS += target
|
||||||
}
|
}
|
||||||
win32 {
|
win32 {
|
||||||
INCLUDEPATH += $$PROTOBUF/include \
|
INCLUDEPATH += $$PROTOBUF/include \
|
||||||
$$ZLIB/include
|
$$ZLIB/include
|
||||||
LIBS += $$PROTOBUF/lib/libprotobuf-lite.lib \
|
LIBS += $$PROTOBUF/lib/libprotobuf-lite.lib \
|
||||||
$$ZLIB/lib/zlibstatic.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-2024 Martin Tuma
|
||||||
}
|
}
|
||||||
macx {
|
macx {
|
||||||
INCLUDEPATH += $$PROTOBUF/include
|
INCLUDEPATH += $$PROTOBUF/include
|
||||||
LIBS += $$PROTOBUF/lib/libprotobuf-lite.a \
|
LIBS += $$PROTOBUF/lib/libprotobuf-lite.a \
|
||||||
-lz
|
-lz
|
||||||
}
|
}
|
||||||
|
android {
|
||||||
|
INCLUDEPATH += $$PROTOBUF/include
|
||||||
|
LIBS += $$PROTOBUF/$$ANDROID_TARGET_ARCH/libprotobuf-lite.a \
|
||||||
|
-lz
|
||||||
|
|
||||||
target.path += $$[QT_INSTALL_PLUGINS]/imageformats
|
top_builddir=$$shadowed($$PWD)
|
||||||
INSTALLS += target
|
DESTDIR = $$top_builddir/plugins
|
||||||
|
TARGET = $$qt5LibraryTarget(libpbf, "plugins/imageformats/")
|
||||||
|
}
|
||||||
|
@ -6,7 +6,7 @@
|
|||||||
|
|
||||||
QByteArray Gzip::uncompress(QIODevice *device, int limit)
|
QByteArray Gzip::uncompress(QIODevice *device, int limit)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret = Z_STREAM_END;
|
||||||
z_stream strm;
|
z_stream strm;
|
||||||
unsigned char in[CHUNK];
|
unsigned char in[CHUNK];
|
||||||
unsigned char out[CHUNK];
|
unsigned char out[CHUNK];
|
||||||
|
@ -79,16 +79,19 @@ bool PBFHandler::read(QImage *image)
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool ok;
|
QList<QByteArray> list(format().split(';'));
|
||||||
int zoom = format().toInt(&ok);
|
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);
|
||||||
|
|
||||||
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);
|
*image = QImage(size, QImage::Format_ARGB32_Premultiplied);
|
||||||
Tile tile(image, ok ? zoom : -1, scale);
|
Tile tile(image, zoom, scale);
|
||||||
|
|
||||||
_style->render(data, tile);
|
_style->render(data, tile);
|
||||||
|
|
||||||
|
@ -11,7 +11,7 @@ class Style;
|
|||||||
class PBFHandler : public QImageIOHandler
|
class PBFHandler : public QImageIOHandler
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
PBFHandler(const Style *style) : _style(style) {}
|
PBFHandler(Style *style) : _style(style) {}
|
||||||
~PBFHandler() {}
|
~PBFHandler() {}
|
||||||
|
|
||||||
bool canRead() const;
|
bool canRead() const;
|
||||||
@ -24,7 +24,7 @@ public:
|
|||||||
static bool canRead(QIODevice *device);
|
static bool canRead(QIODevice *device);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
const Style *_style;
|
Style *_style;
|
||||||
QSize _scaledSize;
|
QSize _scaledSize;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
@ -4,14 +4,20 @@
|
|||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include "sprites.h"
|
#include "sprites.h"
|
||||||
|
|
||||||
|
static QImage sdf2img(const QImage &sdf, const QColor &color)
|
||||||
/*
|
|
||||||
Loading the sprites atlas image must be deferred until all image plugins
|
|
||||||
are loaded, otherwise reading the image will cause a deadlock!
|
|
||||||
*/
|
|
||||||
static const QImage &atlas(const QString &fileName)
|
|
||||||
{
|
{
|
||||||
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;
|
return img;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -19,7 +25,6 @@ Sprites::Sprite::Sprite(const QJsonObject &json)
|
|||||||
{
|
{
|
||||||
int x, y, width, height;
|
int x, y, width, height;
|
||||||
|
|
||||||
|
|
||||||
if (json.contains("x") && json["x"].isDouble())
|
if (json.contains("x") && json["x"].isDouble())
|
||||||
x = json["x"].toInt();
|
x = json["x"].toInt();
|
||||||
else
|
else
|
||||||
@ -44,12 +49,15 @@ Sprites::Sprite::Sprite(const QJsonObject &json)
|
|||||||
_pixelRatio = json["pixelRatio"].toDouble();
|
_pixelRatio = json["pixelRatio"].toDouble();
|
||||||
else
|
else
|
||||||
_pixelRatio = 1.0;
|
_pixelRatio = 1.0;
|
||||||
|
|
||||||
|
if (json.contains("sdf") && json["sdf"].isBool())
|
||||||
|
_sdf = json["sdf"].toBool();
|
||||||
|
else
|
||||||
|
_sdf = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Sprites::load(const QString &jsonFile, const QString &imageFile)
|
bool Sprites::load(const QString &jsonFile, const QString &imageFile)
|
||||||
{
|
{
|
||||||
_imageFile = imageFile;
|
|
||||||
|
|
||||||
QFile file(jsonFile);
|
QFile file(jsonFile);
|
||||||
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
|
||||||
qCritical() << jsonFile << ": error opening file";
|
qCritical() << jsonFile << ": error opening file";
|
||||||
@ -78,27 +86,60 @@ bool Sprites::load(const QString &jsonFile, const QString &imageFile)
|
|||||||
qWarning() << it.key() << ": invalid sprite definition";
|
qWarning() << it.key() << ": invalid sprite definition";
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Loading the sprites atlas image must be deferred until all image plugins
|
||||||
|
// are loaded, otherwise reading the image will cause a deadlock in Qt!
|
||||||
|
_imageFile = imageFile;
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
QImage Sprites::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();
|
return QImage();
|
||||||
|
|
||||||
const QImage &img = atlas(_imageFile);
|
QImage img(_img.copy(sprite.rect()));
|
||||||
if (img.isNull())
|
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();
|
return QImage();
|
||||||
|
|
||||||
QMap<QString, Sprite>::const_iterator it = _sprites.find(name);
|
QMap<QString, Sprite>::const_iterator it = _sprites.constFind(name);
|
||||||
if (it == _sprites.constEnd())
|
if (it == _sprites.constEnd())
|
||||||
return QImage();
|
return QImage();
|
||||||
|
|
||||||
if (!img.rect().contains(it->rect()))
|
return sprite(*it, color, size);
|
||||||
return QImage();
|
|
||||||
|
|
||||||
QImage ret(img.copy(it->rect()));
|
|
||||||
ret.setDevicePixelRatio(it->pixelRatio());
|
|
||||||
|
|
||||||
return ret;
|
|
||||||
}
|
}
|
||||||
|
@ -5,16 +5,19 @@
|
|||||||
#include <QMap>
|
#include <QMap>
|
||||||
#include <QImage>
|
#include <QImage>
|
||||||
#include <QString>
|
#include <QString>
|
||||||
|
#include <QMutex>
|
||||||
|
|
||||||
class QJsonObject;
|
class QJsonObject;
|
||||||
|
|
||||||
class Sprites
|
class Sprites
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
|
Sprites() : _init(0) {}
|
||||||
bool load(const QString &jsonFile, const QString &imageFile);
|
bool load(const QString &jsonFile, const QString &imageFile);
|
||||||
|
|
||||||
bool isNull() const {return _imageFile.isNull();}
|
bool isNull() const {return _imageFile.isNull();}
|
||||||
QImage icon(const QString &name) const;
|
QImage icon(const QString &name, const QColor &color = Qt::black,
|
||||||
|
qreal size = 1.0);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class Sprite {
|
class Sprite {
|
||||||
@ -23,14 +26,21 @@ private:
|
|||||||
|
|
||||||
const QRect &rect() const {return _rect;}
|
const QRect &rect() const {return _rect;}
|
||||||
qreal pixelRatio() const {return _pixelRatio;}
|
qreal pixelRatio() const {return _pixelRatio;}
|
||||||
|
bool sdf() const {return _sdf;}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QRect _rect;
|
QRect _rect;
|
||||||
qreal _pixelRatio;
|
qreal _pixelRatio;
|
||||||
|
bool _sdf;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
QImage sprite(const Sprite &sprite, const QColor &color, qreal scale);
|
||||||
|
|
||||||
QMap<QString, Sprite> _sprites;
|
QMap<QString, Sprite> _sprites;
|
||||||
|
QImage _img;
|
||||||
|
QMutex _lock;
|
||||||
QString _imageFile;
|
QString _imageFile;
|
||||||
|
int _init;
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // SPRITES_H
|
#endif // SPRITES_H
|
||||||
|
@ -8,7 +8,6 @@
|
|||||||
#include <QRegularExpression>
|
#include <QRegularExpression>
|
||||||
#include <QDebug>
|
#include <QDebug>
|
||||||
#include "text.h"
|
#include "text.h"
|
||||||
#include "color.h"
|
|
||||||
#include "font.h"
|
#include "font.h"
|
||||||
#include "tile.h"
|
#include "tile.h"
|
||||||
#include "pbf.h"
|
#include "pbf.h"
|
||||||
@ -205,7 +204,7 @@ bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
|||||||
|
|
||||||
QString Style::Layer::Template::value(int zoom, 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));
|
QString text(_field.value(zoom));
|
||||||
QRegularExpressionMatchIterator it = rx.globalMatch(text);
|
QRegularExpressionMatchIterator it = rx.globalMatch(text);
|
||||||
QStringList keys;
|
QStringList keys;
|
||||||
@ -258,6 +257,9 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
|
|||||||
_textHaloColor = FunctionC(json["text-halo-color"], QColor());
|
_textHaloColor = FunctionC(json["text-halo-color"], QColor());
|
||||||
_textHaloWidth = FunctionF(json["text-halo-width"]);
|
_textHaloWidth = FunctionF(json["text-halo-width"]);
|
||||||
_textHaloBlur = FunctionF(json["text-halo-blur"]);
|
_textHaloBlur = FunctionF(json["text-halo-blur"]);
|
||||||
|
|
||||||
|
// icon
|
||||||
|
_iconColor = FunctionC(json["icon-color"]);
|
||||||
}
|
}
|
||||||
|
|
||||||
QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
||||||
@ -293,7 +295,7 @@ QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
|||||||
return pen;
|
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
|
const
|
||||||
{
|
{
|
||||||
QColor color;
|
QColor color;
|
||||||
@ -370,6 +372,7 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
|
|||||||
|
|
||||||
// icon
|
// icon
|
||||||
_icon = Template(FunctionS(json["icon-image"]));
|
_icon = Template(FunctionS(json["icon-image"]));
|
||||||
|
_iconSize = FunctionF(json["icon-size"]);
|
||||||
|
|
||||||
// symbol
|
// symbol
|
||||||
_symbolPlacement = FunctionS(json["symbol-placement"]);
|
_symbolPlacement = FunctionS(json["symbol-placement"]);
|
||||||
@ -512,7 +515,7 @@ bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
|
|||||||
return _filter.match(feature);
|
return _filter.match(feature);
|
||||||
}
|
}
|
||||||
|
|
||||||
void Style::Layer::setPathPainter(Tile &tile, const Sprites &sprites) const
|
void Style::Layer::setPathPainter(Tile &tile, Sprites &sprites) const
|
||||||
{
|
{
|
||||||
QPainter &p = tile.painter();
|
QPainter &p = tile.painter();
|
||||||
int zoom = tile.zoom();
|
int zoom = tile.zoom();
|
||||||
@ -545,14 +548,18 @@ void Style::Layer::setTextProperties(Tile &tile) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
|
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
|
||||||
const PBF::Feature &feature, const Sprites &sprites) const
|
const PBF::Feature &feature, Sprites &sprites) const
|
||||||
{
|
{
|
||||||
QString text = _layout.text(tile.zoom(), feature);
|
QString text(_layout.text(tile.zoom(), feature));
|
||||||
if (text.isEmpty())
|
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;
|
return;
|
||||||
|
|
||||||
QString icon = _layout.icon(tile.zoom(), feature);
|
tile.text().addLabel(text, img, path);
|
||||||
tile.text().addLabel(text, sprites.icon(icon), path);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool loadSprites(const QDir &styleDir, const QString &json,
|
static bool loadSprites(const QDir &styleDir, const QString &json,
|
||||||
@ -598,20 +605,20 @@ bool Style::load(const QString &fileName)
|
|||||||
_layers.append(Layer(layers[i].toObject()));
|
_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.json", "sprite.png", _sprites);
|
||||||
loadSprites(styleDir, "sprite@2x.json", "sprite@2x.png", _sprites2x);
|
loadSprites(styleDir, "sprite@2x.json", "sprite@2x.png", _sprites2x);
|
||||||
|
|
||||||
return true;
|
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)
|
return (scale.x() > 1.0 || scale.y() > 1.0)
|
||||||
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Style::setupLayer(Tile &tile, const Layer &layer) const
|
void Style::setupLayer(Tile &tile, const Layer &layer)
|
||||||
{
|
{
|
||||||
if (layer.isSymbol())
|
if (layer.isSymbol())
|
||||||
layer.setTextProperties(tile);
|
layer.setTextProperties(tile);
|
||||||
@ -619,7 +626,7 @@ void Style::setupLayer(Tile &tile, const Layer &layer) const
|
|||||||
layer.setPathPainter(tile, sprites(tile.scale()));
|
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(),
|
QRectF rect(QPointF(0, 0), QSizeF(tile.size().width() / tile.scale().x(),
|
||||||
tile.size().height() / tile.scale().y()));
|
tile.size().height() / tile.scale().y()));
|
||||||
@ -634,13 +641,10 @@ void Style::drawBackground(Tile &tile) const
|
|||||||
_layers.first().setPathPainter(tile, _sprites);
|
_layers.first().setPathPainter(tile, _sprites);
|
||||||
tile.painter().drawPath(path);
|
tile.painter().drawPath(path);
|
||||||
}
|
}
|
||||||
|
|
||||||
//tile.painter().setPen(Qt::red);
|
|
||||||
//tile.painter().drawRect(rect);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||||
Tile &tile, const QSizeF &factor) const
|
Tile &tile, const QSizeF &factor)
|
||||||
{
|
{
|
||||||
if (!layer.match(tile.zoom(), feature))
|
if (!layer.match(tile.zoom(), feature))
|
||||||
return;
|
return;
|
||||||
@ -656,7 +660,7 @@ void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
|||||||
}
|
}
|
||||||
|
|
||||||
void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||||
Tile &tile) const
|
Tile &tile)
|
||||||
{
|
{
|
||||||
if (pbfLayer.data()->version() > 2)
|
if (pbfLayer.data()->version() > 2)
|
||||||
return;
|
return;
|
||||||
@ -675,7 +679,7 @@ void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
|||||||
tile.painter().restore();
|
tile.painter().restore();
|
||||||
}
|
}
|
||||||
|
|
||||||
void Style::render(const PBF &data, Tile &tile) const
|
void Style::render(const PBF &data, Tile &tile)
|
||||||
{
|
{
|
||||||
drawBackground(tile);
|
drawBackground(tile);
|
||||||
|
|
||||||
@ -689,4 +693,11 @@ void Style::render(const PBF &data, Tile &tile) const
|
|||||||
}
|
}
|
||||||
|
|
||||||
tile.text().render(&tile.painter());
|
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);
|
||||||
}
|
}
|
||||||
|
24
src/style.h
24
src/style.h
@ -29,7 +29,7 @@ public:
|
|||||||
Style(QObject *parent = 0) : QObject(parent) {}
|
Style(QObject *parent = 0) : QObject(parent) {}
|
||||||
|
|
||||||
bool load(const QString &fileName);
|
bool load(const QString &fileName);
|
||||||
void render(const PBF &data, Tile &tile) const;
|
void render(const PBF &data, Tile &tile);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
class Layer {
|
class Layer {
|
||||||
@ -44,10 +44,10 @@ private:
|
|||||||
bool isVisible() const {return (_layout.visible());}
|
bool isVisible() const {return (_layout.visible());}
|
||||||
|
|
||||||
bool match(int zoom, const PBF::Feature &feature) const;
|
bool match(int zoom, const PBF::Feature &feature) const;
|
||||||
void setPathPainter(Tile &tile, const Sprites &sprites) const;
|
void setPathPainter(Tile &tile, Sprites &sprites) const;
|
||||||
void setTextProperties(Tile &tile) const;
|
void setTextProperties(Tile &tile) const;
|
||||||
void addSymbol(Tile &tile, const QPainterPath &path,
|
void addSymbol(Tile &tile, const QPainterPath &path,
|
||||||
const PBF::Feature &feature, const Sprites &sprites) const;
|
const PBF::Feature &feature, Sprites &sprites) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
enum Type {
|
enum Type {
|
||||||
@ -103,6 +103,8 @@ private:
|
|||||||
{return _text.value(zoom, feature).trimmed();}
|
{return _text.value(zoom, feature).trimmed();}
|
||||||
QString icon(int zoom, const PBF::Feature &feature) const
|
QString icon(int zoom, const PBF::Feature &feature) const
|
||||||
{return _icon.value(zoom, feature);}
|
{return _icon.value(zoom, feature);}
|
||||||
|
qreal iconSize(int zoom) const
|
||||||
|
{return _iconSize.value(zoom);}
|
||||||
QFont font(int zoom) const;
|
QFont font(int zoom) const;
|
||||||
Qt::PenCapStyle lineCap(int zoom) const;
|
Qt::PenCapStyle lineCap(int zoom) const;
|
||||||
Qt::PenJoinStyle lineJoin(int zoom) const;
|
Qt::PenJoinStyle lineJoin(int zoom) const;
|
||||||
@ -116,6 +118,7 @@ private:
|
|||||||
|
|
||||||
Template _text;
|
Template _text;
|
||||||
Template _icon;
|
Template _icon;
|
||||||
|
FunctionF _iconSize;
|
||||||
FunctionF _textSize;
|
FunctionF _textSize;
|
||||||
FunctionF _textMaxWidth;
|
FunctionF _textMaxWidth;
|
||||||
FunctionF _textMaxAngle;
|
FunctionF _textMaxAngle;
|
||||||
@ -135,13 +138,15 @@ private:
|
|||||||
Paint(const QJsonObject &json);
|
Paint(const QJsonObject &json);
|
||||||
|
|
||||||
QPen pen(Layer::Type type, int zoom) const;
|
QPen pen(Layer::Type type, int zoom) const;
|
||||||
QBrush brush(Layer::Type type, int zoom, const Sprites &sprites)
|
QBrush brush(Layer::Type type, int zoom, Sprites &sprites)
|
||||||
const;
|
const;
|
||||||
qreal opacity(Layer::Type type, int zoom) const;
|
qreal opacity(Layer::Type type, int zoom) const;
|
||||||
bool antialias(Layer::Type type, int zoom) const;
|
bool antialias(Layer::Type type, int zoom) const;
|
||||||
Text::Halo halo(int zoom) const
|
Text::Halo halo(int zoom) const
|
||||||
{return Text::Halo(_textHaloColor.value(zoom),
|
{return Text::Halo(_textHaloColor.value(zoom),
|
||||||
_textHaloWidth.value(zoom), _textHaloBlur.value(zoom));}
|
_textHaloWidth.value(zoom), _textHaloBlur.value(zoom));}
|
||||||
|
QColor iconColor(int zoom) const
|
||||||
|
{return _iconColor.value(zoom);}
|
||||||
|
|
||||||
private:
|
private:
|
||||||
FunctionC _textColor;
|
FunctionC _textColor;
|
||||||
@ -150,6 +155,7 @@ private:
|
|||||||
FunctionC _fillColor;
|
FunctionC _fillColor;
|
||||||
FunctionC _fillOutlineColor;
|
FunctionC _fillOutlineColor;
|
||||||
FunctionC _backgroundColor;
|
FunctionC _backgroundColor;
|
||||||
|
FunctionC _iconColor;
|
||||||
FunctionF _fillOpacity;
|
FunctionF _fillOpacity;
|
||||||
FunctionF _lineOpacity;
|
FunctionF _lineOpacity;
|
||||||
FunctionF _lineWidth;
|
FunctionF _lineWidth;
|
||||||
@ -168,14 +174,14 @@ private:
|
|||||||
Paint _paint;
|
Paint _paint;
|
||||||
};
|
};
|
||||||
|
|
||||||
const Sprites &sprites(const QPointF &scale) const;
|
Sprites &sprites(const QPointF &scale);
|
||||||
|
|
||||||
void drawBackground(Tile &tile) const;
|
void drawBackground(Tile &tile);
|
||||||
void setupLayer(Tile &tile, const Layer &layer) const;
|
void setupLayer(Tile &tile, const Layer &layer);
|
||||||
void drawFeature(const PBF::Feature &feature, 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,
|
void drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||||
Tile &tile) const;
|
Tile &tile);
|
||||||
|
|
||||||
QVector<Layer> _layers;
|
QVector<Layer> _layers;
|
||||||
Sprites _sprites, _sprites2x;
|
Sprites _sprites, _sprites2x;
|
||||||
|
11
src/text.cpp
11
src/text.cpp
@ -14,15 +14,10 @@ Text::~Text()
|
|||||||
|
|
||||||
void Text::render(QPainter *painter) const
|
void Text::render(QPainter *painter) const
|
||||||
{
|
{
|
||||||
QSet<QString> set;
|
|
||||||
|
|
||||||
for (int i = 0; i < _items.size(); i++) {
|
for (int i = 0; i < _items.size(); i++) {
|
||||||
const TextItem *ti = _items.at(i);
|
const TextItem *ti = _items.at(i);
|
||||||
if (ti->isVisible() && _sceneRect.intersects(ti->boundingRect())
|
if (ti->isVisible() && _sceneRect.intersects(ti->boundingRect()))
|
||||||
&& !set.contains(ti->text())) {
|
|
||||||
ti->paint(painter);
|
ti->paint(painter);
|
||||||
set.insert(ti->text());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -51,6 +46,8 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
|||||||
} else {
|
} else {
|
||||||
switch (_placement) {
|
switch (_placement) {
|
||||||
case Line:
|
case Line:
|
||||||
|
if (text.isEmpty())
|
||||||
|
return;
|
||||||
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
|
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
|
||||||
break;
|
break;
|
||||||
case LineCenter:
|
case LineCenter:
|
||||||
@ -73,7 +70,7 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
|||||||
ti->setHalo(_halo);
|
ti->setHalo(_halo);
|
||||||
addItem(ti);
|
addItem(ti);
|
||||||
|
|
||||||
QList<TextItem*> ci = collidingItems(ti);
|
QList<TextItem*> ci(collidingItems(ti));
|
||||||
for (int i = 0; i < ci.size(); i++)
|
for (int i = 0; i < ci.size(); i++)
|
||||||
ci[i]->setVisible(false);
|
ci[i]->setVisible(false);
|
||||||
}
|
}
|
||||||
|
@ -21,14 +21,14 @@ qreal TextItem::avgCharWidth() const
|
|||||||
ratio = 1.0;
|
ratio = 1.0;
|
||||||
// Greek & Cyrilic
|
// Greek & Cyrilic
|
||||||
else if (cp >= 0x03FF && cp <= 0x04FF) {
|
else if (cp >= 0x03FF && cp <= 0x04FF) {
|
||||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.80 : 0.73;
|
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.80 : 0.70;
|
||||||
if (_font.bold())
|
if (_font.bold())
|
||||||
ratio *= 1.1;
|
ratio *= 1.1;
|
||||||
if (_font.italic())
|
if (_font.italic())
|
||||||
ratio *= 0.9;
|
ratio *= 0.9;
|
||||||
// The rest (Latin scripts, Arabic, ...)
|
// The rest (Latin scripts, Arabic, ...)
|
||||||
} else {
|
} else {
|
||||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.63;
|
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.60;
|
||||||
if (_font.bold())
|
if (_font.bold())
|
||||||
ratio *= 1.1;
|
ratio *= 1.1;
|
||||||
if (_font.italic())
|
if (_font.italic())
|
||||||
|
@ -9,23 +9,20 @@
|
|||||||
#define INTERSECTS intersects
|
#define INTERSECTS intersects
|
||||||
#endif // QT 5.15
|
#endif // QT 5.15
|
||||||
|
|
||||||
static bool intersection(const QLineF &line, const QRectF &rect,
|
static void swap(const QLineF &line, QPointF *p1, QPointF *p2)
|
||||||
QPointF *p)
|
|
||||||
{
|
{
|
||||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.topRight()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
|
||||||
== QLineF::BoundedIntersection)
|
|
||||||
return true;
|
|
||||||
|
|
||||||
return false;
|
QPointF lp1(line.p1());
|
||||||
|
QPointF lp2(line.p2());
|
||||||
|
|
||||||
|
if ((lp1.rx() < lp2.rx() && p1->rx() > p2->rx())
|
||||||
|
|| (lp1.ry() < lp2.ry() && p1->ry() > p2->ry())
|
||||||
|
|| (lp1.rx() > lp2.rx() && p1->rx() < p2->rx())
|
||||||
|
|| (lp1.ry() > lp2.ry() && p1->ry() < p2->ry())) {
|
||||||
|
QPointF tmp(*p2);
|
||||||
|
*p2 = *p1;
|
||||||
|
*p1 = tmp;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
||||||
@ -38,20 +35,26 @@ static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
|||||||
p = p2;
|
p = p2;
|
||||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||||
== QLineF::BoundedIntersection) {
|
== QLineF::BoundedIntersection) {
|
||||||
if (p == p2)
|
if (p == p2) {
|
||||||
|
swap(line, p1, p2);
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
p = p2;
|
p = p2;
|
||||||
}
|
}
|
||||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||||
== QLineF::BoundedIntersection) {
|
== QLineF::BoundedIntersection) {
|
||||||
if (p == p2)
|
if (p == p2) {
|
||||||
|
swap(line, p1, p2);
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
p = p2;
|
p = p2;
|
||||||
}
|
}
|
||||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||||
== QLineF::BoundedIntersection) {
|
== QLineF::BoundedIntersection) {
|
||||||
if (p == p2)
|
if (p == p2) {
|
||||||
|
swap(line, p1, p2);
|
||||||
return true;
|
return true;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
Q_ASSERT(p != p2);
|
Q_ASSERT(p != p2);
|
||||||
@ -59,22 +62,42 @@ static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
|||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
|
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p)
|
||||||
|
{
|
||||||
|
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||||
|
== QLineF::BoundedIntersection)
|
||||||
|
return true;
|
||||||
|
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
static QPainterPath subpath(const QPolygonF &path, int start, int end,
|
||||||
qreal cut)
|
qreal cut)
|
||||||
{
|
{
|
||||||
qreal ss = 0, es = 0;
|
qreal ss = 0, es = 0;
|
||||||
int si = start, ei = end;
|
int si = start, ei = end;
|
||||||
|
|
||||||
for (int i = start; i <= end; i++) {
|
for (int i = start; i < end; i++) {
|
||||||
qreal len = lines.at(i).length();
|
QLineF l(path.at(i), path.at(i+1));
|
||||||
|
qreal len = l.length();
|
||||||
if (ss + len < cut / 2) {
|
if (ss + len < cut / 2) {
|
||||||
ss += len;
|
ss += len;
|
||||||
si++;
|
si++;
|
||||||
} else
|
} else
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
for (int i = end; i >= start; i--) {
|
for (int i = end; i > start; i--) {
|
||||||
qreal len = lines.at(i).length();
|
QLineF l(path.at(i), path.at(i-1));
|
||||||
|
qreal len = l.length();
|
||||||
if (es + len < cut / 2) {
|
if (es + len < cut / 2) {
|
||||||
es += len;
|
es += len;
|
||||||
ei--;
|
ei--;
|
||||||
@ -82,98 +105,117 @@ static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
QLineF sl(lines.at(si).p2(), lines.at(si).p1());
|
QLineF sl(path.at(si+1), path.at(si));
|
||||||
sl.setLength(sl.length() - (cut / 2 - ss));
|
sl.setLength(sl.length() - (cut / 2 - ss));
|
||||||
QLineF el(lines.at(ei));
|
QLineF el(path.at(ei-1), path.at(ei));
|
||||||
el.setLength(el.length() - (cut / 2 - es));
|
el.setLength(el.length() - (cut / 2 - es));
|
||||||
|
|
||||||
QPainterPath p(sl.p2());
|
QPainterPath p(sl.p2());
|
||||||
for (int i = si; i <= ei; i++)
|
for (int i = si + 1; i < ei; i++)
|
||||||
p.lineTo(lines.at(i).p2());
|
p.lineTo(path.at(i));
|
||||||
p.setElementPositionAt(p.elementCount() - 1, el.p2().x(), el.p2().y());
|
p.lineTo(el.p2());
|
||||||
|
|
||||||
return p;
|
return p;
|
||||||
}
|
}
|
||||||
|
|
||||||
static QList<QLineF> lineString(const QPainterPath &path,
|
static QList<QPolygonF> polyLines(const QPainterPath &path, const QRectF &rect)
|
||||||
const QRectF &boundingRect)
|
|
||||||
{
|
{
|
||||||
QList<QLineF> lines;
|
QList<QPolygonF> lines;
|
||||||
int start = -1, end = -1;
|
QPolygonF line;
|
||||||
|
bool lastIn = rect.contains(path.elementAt(0));
|
||||||
|
|
||||||
|
for (int i = 1; i < path.elementCount(); i++) {
|
||||||
|
if (rect.contains(path.elementAt(i))) {
|
||||||
|
if (lastIn) {
|
||||||
|
if (line.isEmpty())
|
||||||
|
line.append(path.elementAt(i-1));
|
||||||
|
line.append(path.elementAt(i));
|
||||||
|
} else {
|
||||||
|
QPointF p;
|
||||||
|
QLineF l(path.elementAt(i-1), path.elementAt(i));
|
||||||
|
|
||||||
for (int i = 0; i < path.elementCount(); i++) {
|
if (intersection(l, rect, &p))
|
||||||
if (boundingRect.contains(path.elementAt(i))) {
|
line.append(p);
|
||||||
start = i;
|
line.append(path.elementAt(i));
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
for (int i = path.elementCount() - 1; i >= 0; i--) {
|
|
||||||
if (boundingRect.contains(path.elementAt(i))) {
|
|
||||||
end = i;
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (start < 0) {
|
|
||||||
QPointF p1, p2;
|
|
||||||
|
|
||||||
for (int i = 1; i < path.elementCount(); i++) {
|
|
||||||
QLineF l(path.elementAt(i-1), path.elementAt(i));
|
|
||||||
if (intersection(l, boundingRect, &p1, &p2)) {
|
|
||||||
lines.append(QLineF(p1, p2));
|
|
||||||
break;
|
|
||||||
}
|
}
|
||||||
}
|
|
||||||
} else {
|
|
||||||
QPointF p;
|
|
||||||
|
|
||||||
if (start > 0) {
|
lastIn = true;
|
||||||
QLineF l(path.elementAt(start-1), path.elementAt(start));
|
} else {
|
||||||
if (intersection(l, boundingRect, &p))
|
QLineF l(path.elementAt(i-1), path.elementAt(i));
|
||||||
lines.append(QLineF(p, path.elementAt(start)));
|
|
||||||
}
|
if (lastIn) {
|
||||||
for (int i = start + 1; i <= end; i++)
|
QPointF p;
|
||||||
lines.append(QLineF(path.elementAt(i-1), path.elementAt(i)));
|
if (line.isEmpty())
|
||||||
if (end < path.elementCount() - 1) {
|
line.append(path.elementAt(i-1));
|
||||||
QLineF l(path.elementAt(end), path.elementAt(end+1));
|
if (intersection(l, rect, &p))
|
||||||
if (intersection(l, boundingRect, &p))
|
line.append(p);
|
||||||
lines.append(QLineF(path.elementAt(end), p));
|
lines.append(line);
|
||||||
|
line.clear();
|
||||||
|
} else {
|
||||||
|
QPointF p1, p2;
|
||||||
|
if (intersection(l, rect, &p1, &p2)) {
|
||||||
|
line.append(p1);
|
||||||
|
line.append(p2);
|
||||||
|
lines.append(line);
|
||||||
|
line.clear();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lastIn = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!line.isEmpty())
|
||||||
|
lines.append(line);
|
||||||
|
|
||||||
return lines;
|
return lines;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static qreal diff(qreal a1, qreal a2)
|
||||||
|
{
|
||||||
|
qreal d = qAbs(a1 - a2);
|
||||||
|
return (d > 180) ? 360 - d : d;
|
||||||
|
}
|
||||||
|
|
||||||
static QPainterPath textPath(const QPainterPath &path, qreal textWidth,
|
static QPainterPath textPath(const QPainterPath &path, qreal textWidth,
|
||||||
qreal maxAngle, qreal charWidth, const QRectF &tileRect)
|
qreal maxAngle, qreal charWidth, const QRectF &tileRect)
|
||||||
{
|
{
|
||||||
QList<QLineF> lines(lineString(path, tileRect));
|
if (path.isEmpty())
|
||||||
if (lines.isEmpty())
|
|
||||||
return QPainterPath();
|
return QPainterPath();
|
||||||
qreal length = 0;
|
|
||||||
qreal angle = lines.first().angle();
|
QList<QPolygonF> lines(polyLines(path, tileRect));
|
||||||
int last = 0;
|
|
||||||
|
|
||||||
for (int i = 0; i < lines.size(); i++) {
|
for (int i = 0; i < lines.size(); i++) {
|
||||||
qreal sl = lines.at(i).length();
|
const QPolygonF &pl = lines.at(i);
|
||||||
qreal a = lines.at(i).angle();
|
qreal angle = 0, length = 0;
|
||||||
|
int last = 0;
|
||||||
|
|
||||||
if (!tileRect.contains(lines.at(i).p2()) || sl < charWidth
|
for (int j = 1; j < pl.size(); j ++) {
|
||||||
|| qAbs(angle - a) > maxAngle) {
|
QLineF l(pl.at(j-1), pl.at(j));
|
||||||
if (length > textWidth)
|
qreal sl = l.length();
|
||||||
return subpath(lines, last, i - 1, length - textWidth);
|
qreal a = l.angle();
|
||||||
last = i;
|
|
||||||
length = 0;
|
|
||||||
} else
|
|
||||||
length += sl;
|
|
||||||
|
|
||||||
angle = a;
|
if (sl < charWidth) {
|
||||||
|
if (length > textWidth)
|
||||||
|
return subpath(pl, last, j - 1, length - textWidth);
|
||||||
|
last = j;
|
||||||
|
length = 0;
|
||||||
|
} else if (j > 1 && diff(angle, a) > maxAngle) {
|
||||||
|
if (length > textWidth)
|
||||||
|
return subpath(pl, last, j - 1, length - textWidth);
|
||||||
|
last = j - 1;
|
||||||
|
length = sl;
|
||||||
|
} else
|
||||||
|
length += sl;
|
||||||
|
|
||||||
|
angle = a;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (length > textWidth)
|
||||||
|
return subpath(pl, last, pl.size() - 1, length - textWidth);
|
||||||
}
|
}
|
||||||
|
|
||||||
return (length > textWidth)
|
return QPainterPath();
|
||||||
? subpath(lines, last, lines.size() - 1, length - textWidth)
|
|
||||||
: QPainterPath();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static bool reverse(const QPainterPath &path)
|
static bool reverse(const QPainterPath &path)
|
||||||
@ -208,9 +250,6 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
|||||||
|
|
||||||
void TextPathItem::paint(QPainter *painter) const
|
void TextPathItem::paint(QPainter *painter) const
|
||||||
{
|
{
|
||||||
//painter->setPen(Qt::red);
|
|
||||||
//painter->drawPath(_shape);
|
|
||||||
|
|
||||||
QFontMetrics fm(font());
|
QFontMetrics fm(font());
|
||||||
int textWidth = fm.boundingRect(text()).width();
|
int textWidth = fm.boundingRect(text()).width();
|
||||||
|
|
||||||
@ -259,4 +298,9 @@ void TextPathItem::paint(QPainter *painter) const
|
|||||||
int width = fm.horizontalAdvance(text().at(i));
|
int width = fm.horizontalAdvance(text().at(i));
|
||||||
percent += ((qreal)width / (qreal)textWidth) * factor;
|
percent += ((qreal)width / (qreal)textWidth) * factor;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//painter->setBrush(Qt::NoBrush);
|
||||||
|
//painter->setPen(Qt::red);
|
||||||
|
//painter->setRenderHint(QPainter::Antialiasing, false);
|
||||||
|
//painter->drawPath(_shape);
|
||||||
}
|
}
|
||||||
|
@ -85,7 +85,7 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
|||||||
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||||
_anchor(anchor)
|
_anchor(anchor)
|
||||||
{
|
{
|
||||||
_textRect = fuzzyBoundingRect();
|
_textRect = text.isEmpty() ? QRectF() : fuzzyBoundingRect();
|
||||||
_boundingRect = moveTextRect(_textRect);
|
_boundingRect = moveTextRect(_textRect);
|
||||||
|
|
||||||
if (!_icon.isNull()) {
|
if (!_icon.isNull()) {
|
||||||
|
@ -11,7 +11,10 @@ public:
|
|||||||
: _zoom(zoom), _size(img->size()), _scale(scale),
|
: _zoom(zoom), _size(img->size()), _scale(scale),
|
||||||
_text(QSize(img->size().width() / scale.x(),
|
_text(QSize(img->size().width() / scale.x(),
|
||||||
img->size().height() / scale.y())), _painter(img)
|
img->size().height() / scale.y())), _painter(img)
|
||||||
{_painter.scale(scale.x(), scale.y());}
|
{
|
||||||
|
img->fill(Qt::transparent);
|
||||||
|
_painter.scale(scale.x(), scale.y());
|
||||||
|
}
|
||||||
|
|
||||||
int zoom() const {return _zoom;}
|
int zoom() const {return _zoom;}
|
||||||
const QSize &size() const {return _size;}
|
const QSize &size() const {return _size;}
|
||||||
|
@ -2265,7 +2265,6 @@
|
|||||||
"source": "openmaptiles",
|
"source": "openmaptiles",
|
||||||
"source-layer": "building",
|
"source-layer": "building",
|
||||||
"minzoom": 13,
|
"minzoom": 13,
|
||||||
"maxzoom": 14,
|
|
||||||
"layout": {
|
"layout": {
|
||||||
"visibility": "visible"
|
"visibility": "visible"
|
||||||
},
|
},
|
||||||
@ -4059,4 +4058,4 @@
|
|||||||
}
|
}
|
||||||
],
|
],
|
||||||
"id": "osm-liberty"
|
"id": "osm-liberty"
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user