mirror of
https://github.com/tumic0/QtPBFImagePlugin.git
synced 2025-06-30 21:19:16 +02:00
Compare commits
120 Commits
1.1
...
c86eb7bac2
Author | SHA1 | Date | |
---|---|---|---|
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 | |||
ee49c05eb9 | |||
29d4008068 | |||
60691060ea | |||
9b63d1d362 | |||
7822bdd42c | |||
fe97e9d238 | |||
fd02b83b47 | |||
7684f4fdcf | |||
9772c2f67f | |||
e3c940dd07 | |||
e2bb9f5b14 | |||
032c699e8b | |||
a298cfc47d | |||
402e1e3adc | |||
254e93e583 | |||
95d4fee453 | |||
a6547d27d3 | |||
3e065f87fa | |||
61291bb202 | |||
154114dd2a | |||
c0a7d64a1d | |||
d8df5cb595 | |||
543bf63017 | |||
7ea19d260c | |||
0185cd904f | |||
ec6302d524 | |||
0916d6330c | |||
cc0c16cfd6 | |||
8da172c0ff | |||
148f79eaef | |||
1c42f104d1 | |||
536cad2d61 | |||
ff1c29e078 | |||
e8e9139740 | |||
a58962ab93 | |||
aacc42c25d | |||
3f6b5faa2c | |||
5b6a222999 | |||
e00c8feab6 | |||
82308857ba | |||
044e95e061 | |||
f5c15ece52 | |||
df4134c03a | |||
c28d10eafc | |||
76fb129651 | |||
3dfcf35e53 | |||
5ccb93bd59 | |||
c3df78a455 | |||
f96b530fa3 | |||
9fdfd35752 | |||
20f2e7c7d2 | |||
75e6da8903 | |||
c8e8f0013f | |||
a7294b30f8 | |||
2a89ef8dfd | |||
54176640d0 | |||
4042b3ccdf | |||
c1af3d82a5 | |||
368c53307d | |||
75e61e956e | |||
094641eb4b | |||
cdf40f774b | |||
0950caae0d | |||
0188273c29 | |||
13d682bdf6 | |||
738ada91c2 | |||
6d02dafeb6 | |||
a559b5237b | |||
12b91c9c6f | |||
1677eb7a67 | |||
03f4315a5b | |||
a3d555bb6a | |||
a83d4909ed |
32
.appveyor.yml
Normal file
32
.appveyor.yml
Normal file
@ -0,0 +1,32 @@
|
||||
version: 3.0.{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.5\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\pbf2.dll
|
||||
|
||||
cache:
|
||||
- C:\tools\vcpkg\installed\
|
1
.github/FUNDING.yml
vendored
Normal file
1
.github/FUNDING.yml
vendored
Normal file
@ -0,0 +1 @@
|
||||
github: tumic0
|
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@v3
|
||||
- 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
|
37
.github/workflows/osx.yml
vendored
Normal file
37
.github/workflows/osx.yml
vendored
Normal file
@ -0,0 +1,37 @@
|
||||
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=/usr/local/opt/qt@5/bin:/usr/local/opt/protobuf@21/bin:$PATH" >> $GITHUB_ENV
|
||||
- name: Checkout
|
||||
uses: actions/checkout@v3
|
||||
- name: Install dependencies
|
||||
run: brew install qt@5 protobuf@21
|
||||
- name: Configure build
|
||||
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
|
17
.gitignore
vendored
Normal file
17
.gitignore
vendored
Normal file
@ -0,0 +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
|
112
README.md
112
README.md
@ -3,20 +3,16 @@ Qt image plugin for displaying Mapbox vector tiles
|
||||
|
||||
## Description
|
||||
QtPBFImagePlugin is a Qt image plugin that enables applications capable of
|
||||
displaying raster MBTiles maps or raster XYZ online maps to also display PBF
|
||||
vector tiles without (almost, see usage) any application modifications.
|
||||
displaying raster MBTiles maps or raster XYZ online maps to also display
|
||||
PBF(MVT) vector tiles without (almost, see usage) any application modifications.
|
||||
|
||||
Standard Mapbox GL Styles are used for styling the maps. Most relevant style
|
||||
features used by [Maputnik](http://editor.openmaptiles.org) are supported.
|
||||
The style is loaded from the
|
||||
[$AppDataLocation](http://doc.qt.io/qt-5/qstandardpaths.html)/style/style.json
|
||||
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
|
||||
placed in the same directory as the style itself. A default fallback style
|
||||
(OSM-Liberty) for OpenMapTiles is part of the plugin.
|
||||
features used by [Maputnik](https://maputnik.github.io/editor) are supported.
|
||||
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
|
||||
supported by the plugin.
|
||||
supported by the plugin. The tile size is (since version 2.0 of the plugin) 512px
|
||||
to fit the styles and available data (OpenMapTiles, Mapbox tiles).
|
||||
|
||||
## Usage
|
||||
Due to a major design flaw in the Mapbox vector tiles specification - the zoom
|
||||
@ -27,20 +23,51 @@ QPixmap::loadFromData() functions. The zoom number is passed as ASCII string
|
||||
to the functions:
|
||||
```cpp
|
||||
QPixmap pm;
|
||||
pm.loadFromData(tileData, QString::number(zoom).toLatin1());
|
||||
pm.loadFromData(data, QByteArray::number(zoom));
|
||||
```
|
||||
|
||||
For a complete code sample see the [pbf2png](https://github.com/tumic0/pbf2png)
|
||||
conversion utility.
|
||||
|
||||
### HiDPI
|
||||
The plugin supports vector scaling using QImageReader's setScaledSize() method,
|
||||
so when used like in the following example:
|
||||
```cpp
|
||||
QImageReader reader(file, QString::number(zoom).toLatin1());
|
||||
reader.setScaledSize(QSize(512, 512));
|
||||
reader.read(&image);
|
||||
QImage img;
|
||||
QImageReader reader(file, QByteArray::number(zoom));
|
||||
reader.setScaledSize(QSize(1024, 1024));
|
||||
reader.read(&img);
|
||||
```
|
||||
you will get 512x512px tiles with a pixel ratio of 2 (= HiDPI tiles).
|
||||
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
|
||||
QPixmap pm;
|
||||
QByteArray fmt(QByteArray::number(zoom) + ';' + QByteArray::number(overzoom));
|
||||
pm.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
|
||||
The map style is loaded from the
|
||||
[$AppDataLocation](http://doc.qt.io/qt-5/qstandardpaths.html)/style/style.json
|
||||
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
|
||||
placed in the same directory as the style itself. *A style compatible with the
|
||||
tiles data schema (Mapbox, OpenMapTiles, Tilezen, Ordnance Survey, Esri, ...)
|
||||
must be used.*
|
||||
|
||||
For a list of "ready to use" styles see the
|
||||
[QtPBFImagePlugin-styles](https://github.com/tumic0/QtPBFImagePlugin-styles)
|
||||
repository.
|
||||
|
||||
## Build
|
||||
### Requirements
|
||||
* Qt 5
|
||||
* Qt5 >= 5.11 or Qt6
|
||||
* Google Protocol Buffers (protobuf-lite)
|
||||
* Zlib
|
||||
|
||||
@ -63,25 +90,54 @@ 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.
|
||||
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
|
||||
Text PBF features must have a unique id (OpenMapTiles >= v3.7) for the text layout
|
||||
* 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/package/view_file/home:tumic:QtPBFImagePlugin/QtPBFImagePlugin/libqt5-qtpbfimageformat.changes)
|
||||
|
||||
## Status
|
||||
A picture is worth a thousand words. Data and styles from https://openmaptiles.org.
|
||||
#### OSM-liberty
|
||||

|
||||
A picture is worth a thousand words.
|
||||
#### OpenMapTiles
|
||||
|
||||
* 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)
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
#### Klokantech-basic
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
#### Mapbox
|
||||
|
||||
* Data: [Mapbox](https://github.com/tumic0/GPXSee-maps/blob/master/World/Mapbox.tpl)
|
||||
* Style: [Bright](https://github.com/tumic0/QtPBFImagePlugin-styles/blob/master/Mapbox/bright/style.json)
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
#### Tilezen
|
||||
|
||||
* Data: [HERE](https://github.com/tumic0/GPXSee-maps/blob/master/World/here-vector.tpl)
|
||||
* Style: [Apollo-Bright](https://github.com/tumic0/QtPBFImagePlugin-styles/blob/master/Tilezen/apollo-bright/style.json)
|
||||
|
||||

|
||||

|
||||

|
||||

|
||||

|
||||
|
||||
## Applications using QtPBFImagePlugin
|
||||
* [GPXSee](https://www.gpxsee.org)
|
||||
|
@ -1,4 +1,4 @@
|
||||
{
|
||||
"Keys": [ "pbf" ],
|
||||
"MimeTypes": [ "image/pbf" ]
|
||||
"Keys": [ "mvt" ],
|
||||
"MimeTypes": [ "application/vnd.mapbox-vector-tile" ]
|
||||
}
|
||||
|
@ -2,6 +2,7 @@ TARGET = pbf
|
||||
TEMPLATE = lib
|
||||
CONFIG += plugin
|
||||
QT += gui
|
||||
VERSION = 3.0
|
||||
|
||||
PROTOS = protobuf/vector_tile.proto
|
||||
include(protobuf/vector_tile.pri)
|
||||
@ -32,9 +33,12 @@ SOURCES += src/pbfplugin.cpp \
|
||||
src/textpathitem.cpp \
|
||||
src/textpointitem.cpp \
|
||||
src/font.cpp \
|
||||
src/sprites.cpp
|
||||
src/sprites.cpp \
|
||||
src/textitem.cpp
|
||||
RESOURCES += pbfplugin.qrc
|
||||
|
||||
DEFINES += QT_NO_DEPRECATED_WARNINGS
|
||||
|
||||
unix:!macx{
|
||||
LIBS += -lprotobuf-lite \
|
||||
-lz
|
||||
@ -44,6 +48,10 @@ win32 {
|
||||
$$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) 2023 Martin Tuma
|
||||
}
|
||||
macx {
|
||||
INCLUDEPATH += $$PROTOBUF/include
|
||||
|
9
pkg/pbfplugin.xml
Normal file
9
pkg/pbfplugin.xml
Normal file
@ -0,0 +1,9 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<mime-info xmlns="http://www.freedesktop.org/standards/shared-mime-info">
|
||||
<mime-type type="application/vnd.mapbox-vector-tile">
|
||||
<comment>Mapbox Vector Tile</comment>
|
||||
<sub-class-of type="application/octet-stream"/>
|
||||
<generic-icon name="application/octet-stream"/>
|
||||
<glob pattern="*.mvt"/>
|
||||
</mime-type>
|
||||
</mime-info>
|
@ -13,9 +13,7 @@ QColor Color::fromJsonString(const QString &str)
|
||||
{
|
||||
QColor ret;
|
||||
|
||||
if (str.startsWith('#'))
|
||||
return QColor(str);
|
||||
else if (str.startsWith("rgb(")) {
|
||||
if (str.startsWith("rgb(")) {
|
||||
QStringList comp(str.mid(4, str.size() - 5).split(','));
|
||||
if (comp.size() != 3)
|
||||
return QColor();
|
||||
@ -39,7 +37,8 @@ QColor Color::fromJsonString(const QString &str)
|
||||
return QColor();
|
||||
ret = QColor::fromHslF(comp.at(0).toFloat() / 360.0, pval(comp.at(1)),
|
||||
pval(comp.at(2)), comp.at(3).toFloat());
|
||||
}
|
||||
} else
|
||||
ret = QColor(str);
|
||||
|
||||
if (!ret.isValid())
|
||||
qWarning() << str << ": invalid color";
|
||||
|
@ -34,13 +34,19 @@ static QColor interpolate(const QPair<qreal, QColor> &p0,
|
||||
? progress / difference
|
||||
: (pow(base, progress) - 1) / (pow(base, difference) - 1);
|
||||
|
||||
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
qreal p0h, p0s, p0l, p0a;
|
||||
p0.second.getHslF(&p0h, &p0s, &p0l, &p0a);
|
||||
qreal p1h, p1s, p1l, p1a;
|
||||
#else // QT6
|
||||
float p0h, p0s, p0l, p0a;
|
||||
float p1h, p1s, p1l, p1a;
|
||||
#endif // QT6
|
||||
p0.second.getHslF(&p0h, &p0s, &p0l, &p0a);
|
||||
p1.second.getHslF(&p1h, &p1s, &p1l, &p1a);
|
||||
|
||||
return QColor::fromHslF(f(p0h, p1h, ratio), f(p0s, p1s, ratio),
|
||||
/* Qt returns a hue of -1 for achromatic colors. We convert it to a hue of 1
|
||||
using qAbs() to enable interpolation of grayscale colors */
|
||||
return QColor::fromHslF(f(qAbs(p0h), qAbs(p1h), ratio), f(p0s, p1s, ratio),
|
||||
f(p0l, p1l, ratio), f(p0a, p1a, ratio));
|
||||
}
|
||||
|
||||
|
69
src/gzip.cpp
69
src/gzip.cpp
@ -1,35 +1,58 @@
|
||||
#include <QtEndian>
|
||||
#include <QDebug>
|
||||
#include <QIODevice>
|
||||
#include <zlib.h>
|
||||
#include "gzip.h"
|
||||
|
||||
#define CHUNK 16384
|
||||
|
||||
QByteArray Gzip::uncompress(const QByteArray &data)
|
||||
QByteArray Gzip::uncompress(QIODevice *device, int limit)
|
||||
{
|
||||
int ret = Z_STREAM_END;
|
||||
z_stream strm;
|
||||
unsigned char in[CHUNK];
|
||||
unsigned char out[CHUNK];
|
||||
qint64 rs;
|
||||
QByteArray uba;
|
||||
z_stream stream;
|
||||
|
||||
quint32 *size = (quint32*)(data.constData() + data.size() - sizeof(quint32));
|
||||
uba.resize(qFromLittleEndian(*size));
|
||||
|
||||
stream.zalloc = Z_NULL;
|
||||
stream.zfree = Z_NULL;
|
||||
stream.opaque = Z_NULL;
|
||||
strm.zalloc = Z_NULL;
|
||||
strm.zfree = Z_NULL;
|
||||
strm.opaque = Z_NULL;
|
||||
strm.avail_in = 0;
|
||||
strm.next_in = Z_NULL;
|
||||
if (inflateInit2(&strm, MAX_WBITS + 16) != Z_OK)
|
||||
return QByteArray();
|
||||
|
||||
stream.next_in = (Bytef*)data.constData();
|
||||
stream.avail_in = data.size();
|
||||
stream.next_out = (Bytef*)uba.data();
|
||||
stream.avail_out = uba.size();
|
||||
do {
|
||||
rs = device->read((char*)in, CHUNK);
|
||||
if (rs < 0) {
|
||||
(void)inflateEnd(&strm);
|
||||
return QByteArray();
|
||||
} else if (rs == 0)
|
||||
break;
|
||||
else
|
||||
strm.avail_in = (uInt)rs;
|
||||
strm.next_in = in;
|
||||
|
||||
if (inflateInit2(&stream, MAX_WBITS + 16) != Z_OK)
|
||||
return uba;
|
||||
do {
|
||||
strm.avail_out = CHUNK;
|
||||
strm.next_out = out;
|
||||
ret = inflate(&strm, Z_NO_FLUSH);
|
||||
Q_ASSERT(ret != Z_STREAM_ERROR);
|
||||
switch (ret) {
|
||||
case Z_NEED_DICT:
|
||||
case Z_DATA_ERROR:
|
||||
case Z_MEM_ERROR:
|
||||
(void)inflateEnd(&strm);
|
||||
return QByteArray();
|
||||
}
|
||||
uba.append((char*)out, CHUNK - strm.avail_out);
|
||||
if (limit && uba.size() >= limit) {
|
||||
(void)inflateEnd(&strm);
|
||||
return uba;
|
||||
}
|
||||
} while (!strm.avail_out);
|
||||
} while (ret != Z_STREAM_END);
|
||||
|
||||
if (inflate(&stream, Z_NO_FLUSH) != Z_STREAM_END) {
|
||||
qCritical() << "Invalid gzip data";
|
||||
uba = QByteArray();
|
||||
}
|
||||
|
||||
inflateEnd(&stream);
|
||||
|
||||
return uba;
|
||||
(void)inflateEnd(&strm);
|
||||
return (ret == Z_STREAM_END) ? uba : QByteArray();
|
||||
}
|
||||
|
@ -3,9 +3,11 @@
|
||||
|
||||
#include <QByteArray>
|
||||
|
||||
class QIODevice;
|
||||
|
||||
namespace Gzip
|
||||
{
|
||||
QByteArray uncompress(const QByteArray &data);
|
||||
QByteArray uncompress(QIODevice *device, int limit = 0);
|
||||
}
|
||||
|
||||
#endif // GZIP_H
|
||||
|
185
src/pbf.cpp
185
src/pbf.cpp
@ -1,21 +1,20 @@
|
||||
#include <QByteArray>
|
||||
#include <QPainter>
|
||||
#include <QDebug>
|
||||
#include <QVariantHash>
|
||||
#include "vector_tile.pb.h"
|
||||
#include "style.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
|
||||
|
||||
#define MOVE_TO 1
|
||||
#define LINE_TO 2
|
||||
#define CLOSE_PATH 7
|
||||
|
||||
#define POLYGON vector_tile::Tile_GeomType_POLYGON
|
||||
#define LINESTRING vector_tile::Tile_GeomType_LINESTRING
|
||||
#define POINT vector_tile::Tile_GeomType_POINT
|
||||
|
||||
static inline qint32 zigzag32decode(quint32 value)
|
||||
{
|
||||
return static_cast<qint32>((value >> 1u) ^ static_cast<quint32>(
|
||||
-static_cast<qint32>(value & 1u)));
|
||||
}
|
||||
|
||||
static inline QPoint parameters(quint32 v1, quint32 v2)
|
||||
{
|
||||
return QPoint(zigzag32decode(v1), zigzag32decode(v2));
|
||||
}
|
||||
|
||||
static QVariant value(const vector_tile::Tile_Value &val)
|
||||
{
|
||||
@ -37,100 +36,35 @@ static QVariant value(const vector_tile::Tile_Value &val)
|
||||
return QVariant();
|
||||
}
|
||||
|
||||
class Feature
|
||||
const QVariant *PBF::Feature::value(const QString &key) const
|
||||
{
|
||||
public:
|
||||
Feature() : _data(0) {}
|
||||
Feature(const vector_tile::Tile_Feature *data, const QVector<QString> *keys,
|
||||
const QVector<QVariant> *values) : _data(data)
|
||||
{
|
||||
for (int i = 0; i < data->tags_size(); i = i + 2)
|
||||
_tags.insert(keys->at(data->tags(i)), values->at(data->tags(i+1)));
|
||||
const KeyHash &keys(_layer->keys());
|
||||
KeyHash::const_iterator it(keys.find(key));
|
||||
if (it == keys.constEnd())
|
||||
return 0;
|
||||
|
||||
switch (data->type()) {
|
||||
case POLYGON:
|
||||
_tags.insert("$type", QVariant("Polygon"));
|
||||
break;
|
||||
case LINESTRING:
|
||||
_tags.insert("$type", QVariant("LineString"));
|
||||
break;
|
||||
case POINT:
|
||||
_tags.insert("$type", QVariant("Point"));
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
}
|
||||
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)));
|
||||
|
||||
const QVariantHash &tags() const {return _tags;}
|
||||
const vector_tile::Tile_Feature *data() const {return _data;}
|
||||
|
||||
private:
|
||||
QVariantHash _tags;
|
||||
const vector_tile::Tile_Feature *_data;
|
||||
};
|
||||
|
||||
bool cmp(const Feature &f1, const Feature &f2)
|
||||
{
|
||||
return f1.data()->id() < f2.data()->id();
|
||||
return 0;
|
||||
}
|
||||
|
||||
class Layer
|
||||
QPainterPath PBF::Feature::path(const QSizeF &factor) const
|
||||
{
|
||||
public:
|
||||
Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
QVector<QString> keys;
|
||||
QVector<QVariant> values;
|
||||
|
||||
for (int i = 0; i < data->keys_size(); i++)
|
||||
keys.append(QString::fromStdString(data->keys(i)));
|
||||
for (int i = 0; i < data->values_size(); i++)
|
||||
values.append(value(data->values(i)));
|
||||
|
||||
_features.reserve(data->features_size());
|
||||
for (int i = 0; i < data->features_size(); i++)
|
||||
_features.append(Feature(&(data->features(i)), &keys, &values));
|
||||
qSort(_features.begin(), _features.end(), cmp);
|
||||
}
|
||||
|
||||
const QVector<Feature> &features() const {return _features;}
|
||||
const vector_tile::Tile_Layer *data() const {return _data;}
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Layer *_data;
|
||||
QVector<Feature> _features;
|
||||
};
|
||||
|
||||
static inline qint32 zigzag32decode(quint32 value)
|
||||
{
|
||||
return static_cast<qint32>((value >> 1u) ^ static_cast<quint32>(
|
||||
-static_cast<qint32>(value & 1u)));
|
||||
}
|
||||
|
||||
static inline QPoint parameters(quint32 v1, quint32 v2)
|
||||
{
|
||||
return QPoint(zigzag32decode(v1), zigzag32decode(v2));
|
||||
}
|
||||
|
||||
static void drawFeature(const Feature &feature, Style *style, int styleLayer,
|
||||
const QSizeF &factor, Tile &tile)
|
||||
{
|
||||
if (!style->match(tile.zoom(), styleLayer, feature.tags()))
|
||||
return;
|
||||
|
||||
QPoint cursor;
|
||||
QPainterPath path;
|
||||
|
||||
for (int i = 0; i < feature.data()->geometry_size(); i++) {
|
||||
quint32 g = feature.data()->geometry(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(feature.data()->geometry(i+1),
|
||||
feature.data()->geometry(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.moveTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -138,8 +72,8 @@ static void drawFeature(const Feature &feature, Style *style, int styleLayer,
|
||||
}
|
||||
} else if (cmdId == LINE_TO) {
|
||||
for (unsigned j = 0; j < cmdCount; j++) {
|
||||
QPoint offset = parameters(feature.data()->geometry(i+1),
|
||||
feature.data()->geometry(i+2));
|
||||
QPoint offset = parameters(_data->geometry(i+1),
|
||||
_data->geometry(i+2));
|
||||
i += 2;
|
||||
cursor += offset;
|
||||
path.lineTo(QPointF(cursor.x() * factor.width(),
|
||||
@ -151,60 +85,33 @@ static void drawFeature(const Feature &feature, Style *style, int styleLayer,
|
||||
}
|
||||
}
|
||||
|
||||
if (path.elementCount())
|
||||
style->drawFeature(tile, styleLayer, path, feature.tags());
|
||||
return path;
|
||||
}
|
||||
|
||||
static void drawLayer(const Layer &layer, Style *style, int styleLayer,
|
||||
Tile &tile)
|
||||
PBF::Layer::Layer(const vector_tile::Tile_Layer *data) : _data(data)
|
||||
{
|
||||
if (layer.data()->version() > 2)
|
||||
return;
|
||||
_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)));
|
||||
|
||||
QSizeF factor(tile.size().width() / tile.scale().x() /
|
||||
(qreal)layer.data()->extent(), tile.size().height() / tile.scale().y()
|
||||
/ (qreal)layer.data()->extent());
|
||||
|
||||
tile.painter().save();
|
||||
style->setupLayer(tile, styleLayer);
|
||||
for (int i = 0; i < layer.features().size(); i++)
|
||||
drawFeature(layer.features().at(i), style, styleLayer, factor, tile);
|
||||
tile.painter().restore();
|
||||
_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());
|
||||
}
|
||||
|
||||
bool PBF::render(const QByteArray &data, int zoom, Style *style,
|
||||
const QPointF &scale, QImage *image)
|
||||
PBF::PBF(const vector_tile::Tile &tile)
|
||||
{
|
||||
vector_tile::Tile tile;
|
||||
if (!tile.ParseFromArray(data.constData(), data.size())) {
|
||||
qCritical() << "Invalid tile protocol buffer data";
|
||||
return false;
|
||||
}
|
||||
|
||||
Tile t(image, zoom, scale);
|
||||
|
||||
style->drawBackground(t);
|
||||
|
||||
// Prepare source layers
|
||||
QMap<QString, Layer> layers;
|
||||
for (int i = 0; i < tile.layers_size(); i++) {
|
||||
const vector_tile::Tile_Layer &layer = tile.layers(i);
|
||||
QString name(QString::fromStdString(layer.name()));
|
||||
if (style->sourceLayers().contains(name))
|
||||
layers.insert(name, Layer(&layer));
|
||||
_layers.insert(QString::fromStdString(layer.name()), new Layer(&layer));
|
||||
}
|
||||
|
||||
// Process source layers in order of style layers
|
||||
for (int i = 0; i < style->sourceLayers().size(); i++) {
|
||||
QMap<QString, Layer>::const_iterator it = layers.find(
|
||||
style->sourceLayers().at(i));
|
||||
if (it == layers.constEnd())
|
||||
continue;
|
||||
|
||||
drawLayer(*it, style, i, t);
|
||||
}
|
||||
|
||||
t.text().render(&t.painter());
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
PBF::~PBF()
|
||||
{
|
||||
qDeleteAll(_layers);
|
||||
}
|
||||
|
66
src/pbf.h
66
src/pbf.h
@ -1,15 +1,67 @@
|
||||
#ifndef PBF_H
|
||||
#define PBF_H
|
||||
|
||||
#include <QImage>
|
||||
#include <QVariant>
|
||||
#include <QVector>
|
||||
#include <QHash>
|
||||
#include <QPainterPath>
|
||||
#include "vector_tile.pb.h"
|
||||
|
||||
class QByteArray;
|
||||
class Style;
|
||||
|
||||
namespace PBF
|
||||
typedef QHash<QString, google::protobuf::uint32> KeyHash;
|
||||
|
||||
class PBF
|
||||
{
|
||||
bool render(const QByteArray &data, int zoom, Style *style,
|
||||
const QPointF &scale, QImage *render);
|
||||
}
|
||||
public:
|
||||
class Layer;
|
||||
|
||||
class Feature
|
||||
{
|
||||
public:
|
||||
Feature() : _data(0), _layer(0) {}
|
||||
Feature(const vector_tile::Tile_Feature *data, const Layer *layer)
|
||||
: _data(data), _layer(layer) {}
|
||||
|
||||
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 vector_tile::Tile_Feature *_data;
|
||||
const Layer *_layer;
|
||||
};
|
||||
|
||||
class Layer
|
||||
{
|
||||
public:
|
||||
|
||||
Layer(const vector_tile::Tile_Layer *data);
|
||||
|
||||
const QVector<Feature> &features() const {return _features;}
|
||||
const QVector<QVariant> &values() const {return _values;}
|
||||
const KeyHash &keys() const {return _keys;}
|
||||
const vector_tile::Tile_Layer *data() const {return _data;}
|
||||
|
||||
private:
|
||||
const vector_tile::Tile_Layer *_data;
|
||||
QVector<Feature> _features;
|
||||
QVector<QVariant> _values;
|
||||
KeyHash _keys;
|
||||
};
|
||||
|
||||
|
||||
PBF(const vector_tile::Tile &tile);
|
||||
~PBF();
|
||||
|
||||
const QHash<QString, Layer*> &layers() const {return _layers;}
|
||||
|
||||
private:
|
||||
QHash<QString, Layer*> _layers;
|
||||
};
|
||||
|
||||
inline bool operator<(const PBF::Feature &f1, const PBF::Feature &f2)
|
||||
{return f1._data->id() < f2._data->id();}
|
||||
|
||||
#endif // PBF_H
|
||||
|
@ -1,29 +1,31 @@
|
||||
#include <QImage>
|
||||
#include <QIODevice>
|
||||
#include <QtEndian>
|
||||
#include <QDebug>
|
||||
#include "gzip.h"
|
||||
#include "pbf.h"
|
||||
#include "tile.h"
|
||||
#include "style.h"
|
||||
#include "pbfhandler.h"
|
||||
|
||||
|
||||
#define TILE_SIZE 256
|
||||
#define TILE_SIZE 512
|
||||
|
||||
#define GZIP_MAGIC 0x1F8B0800
|
||||
#define GZIP_MAGIC_MASK 0xFFFFFF00
|
||||
#define PBF_MAGIC 0x1A000000
|
||||
#define PBF_MAGIC_MASK 0xFF000000
|
||||
#define GZIP_MAGIC 0x1F8B
|
||||
#define GZIP_MAGIC_MASK 0xFFFF
|
||||
#define PBF_MAGIC 0x1A00
|
||||
#define PBF_MAGIC_MASK 0xFF00
|
||||
|
||||
static bool isMagic(quint32 magic, quint32 mask, quint32 value)
|
||||
static bool isMagic(quint16 magic, quint16 mask, quint16 value)
|
||||
{
|
||||
return ((qFromBigEndian(value) & mask) == magic);
|
||||
}
|
||||
|
||||
static bool isGZIPPBF(quint32 magic)
|
||||
static bool isGZIPPBF(quint16 magic)
|
||||
{
|
||||
return isMagic(GZIP_MAGIC, GZIP_MAGIC_MASK, magic);
|
||||
}
|
||||
|
||||
static bool isPlainPBF(quint32 magic)
|
||||
static bool isPlainPBF(quint16 magic)
|
||||
{
|
||||
return isMagic(PBF_MAGIC, PBF_MAGIC_MASK, magic);
|
||||
}
|
||||
@ -32,7 +34,7 @@ static bool isPlainPBF(quint32 magic)
|
||||
bool PBFHandler::canRead() const
|
||||
{
|
||||
if (canRead(device())) {
|
||||
setFormat("pbf");
|
||||
setFormat("mvt");
|
||||
return true;
|
||||
} else
|
||||
return false;
|
||||
@ -40,39 +42,60 @@ bool PBFHandler::canRead() const
|
||||
|
||||
bool PBFHandler::canRead(QIODevice *device)
|
||||
{
|
||||
quint32 magic;
|
||||
quint16 magic;
|
||||
qint64 size = device->peek((char*)&magic, sizeof(magic));
|
||||
if (size != sizeof(magic))
|
||||
return false;
|
||||
|
||||
return (isGZIPPBF(magic) || isPlainPBF(magic));
|
||||
if (isPlainPBF(magic))
|
||||
return true;
|
||||
else if (isGZIPPBF(magic)) {
|
||||
QByteArray ba(Gzip::uncompress(device, sizeof(magic)));
|
||||
if (ba.size() < (int)sizeof(magic))
|
||||
return false;
|
||||
return isPlainPBF(*((quint16*)ba.constData()));
|
||||
} else
|
||||
return false;
|
||||
}
|
||||
|
||||
bool PBFHandler::read(QImage *image)
|
||||
{
|
||||
quint32 magic;
|
||||
quint16 magic;
|
||||
if (device()->peek((char*)&magic, sizeof(magic)) != sizeof(magic))
|
||||
return false;
|
||||
|
||||
QByteArray ba;
|
||||
if (isGZIPPBF(magic))
|
||||
ba = Gzip::uncompress(device()->readAll());
|
||||
else if (isPlainPBF(magic))
|
||||
if (isGZIPPBF(magic)) {
|
||||
ba = Gzip::uncompress(device());
|
||||
if (ba.isNull()) {
|
||||
qCritical() << "Invalid gzip data";
|
||||
return false;
|
||||
}
|
||||
} else if (isPlainPBF(magic))
|
||||
ba = device()->readAll();
|
||||
if (ba.isNull())
|
||||
vector_tile::Tile data;
|
||||
if (!data.ParseFromArray(ba.constData(), ba.size())) {
|
||||
qCritical() << "Invalid PBF data";
|
||||
return false;
|
||||
}
|
||||
|
||||
bool ok;
|
||||
int zoom = format().toInt(&ok);
|
||||
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);
|
||||
|
||||
QSize size = _scaledSize.isValid()
|
||||
? _scaledSize : QSize(TILE_SIZE, TILE_SIZE);
|
||||
QPointF scale = _scaledSize.isValid()
|
||||
? QPointF(_scaledSize.width() / TILE_SIZE, _scaledSize.height() / TILE_SIZE)
|
||||
: QPointF(1.0, 1.0);
|
||||
*image = QImage(size, QImage::Format_ARGB32_Premultiplied);
|
||||
Tile tile(image, zoom, scale);
|
||||
|
||||
return PBF::render(ba, ok ? zoom : -1, _style, scale, image);
|
||||
_style->render(data, tile);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool PBFHandler::supportsOption(ImageOption option) const
|
||||
|
@ -2,16 +2,16 @@
|
||||
#define PBFHANDLER_H
|
||||
|
||||
#include <QImageIOHandler>
|
||||
#include <QImage>
|
||||
#include <QVariant>
|
||||
#include <QSize>
|
||||
|
||||
class QImage;
|
||||
class Style;
|
||||
|
||||
class PBFHandler : public QImageIOHandler
|
||||
{
|
||||
public:
|
||||
PBFHandler(Style *style) : _style(style) {}
|
||||
PBFHandler(const Style *style) : _style(style) {}
|
||||
~PBFHandler() {}
|
||||
|
||||
bool canRead() const;
|
||||
@ -19,12 +19,12 @@ public:
|
||||
|
||||
QVariant option(ImageOption option) const;
|
||||
bool supportsOption(ImageOption option) const;
|
||||
void setOption(QImageIOHandler::ImageOption option, const QVariant &value);
|
||||
void setOption(ImageOption option, const QVariant &value);
|
||||
|
||||
static bool canRead(QIODevice *device);
|
||||
|
||||
private:
|
||||
Style *_style;
|
||||
const Style *_style;
|
||||
QSize _scaledSize;
|
||||
};
|
||||
|
||||
|
@ -21,7 +21,7 @@ QImageIOPlugin::Capabilities PBFPlugin::capabilities(QIODevice *device,
|
||||
const QByteArray &format) const
|
||||
{
|
||||
if (device == 0)
|
||||
return (format == "pbf") ? Capabilities(CanRead) : Capabilities();
|
||||
return (format == "mvt") ? Capabilities(CanRead) : Capabilities();
|
||||
else
|
||||
return (device->isReadable() && PBFHandler::canRead(device))
|
||||
? Capabilities(CanRead) : Capabilities();
|
||||
|
@ -9,9 +9,9 @@
|
||||
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 const QImage &atlas(const QString &fileName)
|
||||
{
|
||||
static QImage *img = new QImage(fileName);
|
||||
static QImage img(fileName);
|
||||
return img;
|
||||
}
|
||||
|
||||
@ -83,18 +83,21 @@ bool Sprites::load(const QString &jsonFile, const QString &imageFile)
|
||||
|
||||
QImage Sprites::icon(const QString &name) const
|
||||
{
|
||||
const QImage *img = atlas(_imageFile);
|
||||
if (img->isNull())
|
||||
if (_imageFile.isEmpty())
|
||||
return QImage();
|
||||
|
||||
const QImage &img = atlas(_imageFile);
|
||||
if (img.isNull())
|
||||
return QImage();
|
||||
|
||||
QMap<QString, Sprite>::const_iterator it = _sprites.find(name);
|
||||
if (it == _sprites.constEnd())
|
||||
return QImage();
|
||||
|
||||
if (!img->rect().contains(it->rect()))
|
||||
if (!img.rect().contains(it->rect()))
|
||||
return QImage();
|
||||
|
||||
QImage ret(img->copy(it->rect()));
|
||||
QImage ret(img.copy(it->rect()));
|
||||
ret.setDevicePixelRatio(it->pixelRatio());
|
||||
|
||||
return ret;
|
||||
|
@ -4,6 +4,9 @@
|
||||
#include <QRect>
|
||||
#include <QMap>
|
||||
#include <QImage>
|
||||
#include <QString>
|
||||
|
||||
class QJsonObject;
|
||||
|
||||
class Sprites
|
||||
{
|
||||
|
234
src/style.cpp
234
src/style.cpp
@ -5,14 +5,28 @@
|
||||
#include <QJsonArray>
|
||||
#include <QFileInfo>
|
||||
#include <QDir>
|
||||
#include <QRegularExpression>
|
||||
#include <QDebug>
|
||||
#include "text.h"
|
||||
#include "color.h"
|
||||
#include "font.h"
|
||||
#include "tile.h"
|
||||
#include "pbf.h"
|
||||
#include "style.h"
|
||||
|
||||
|
||||
static vector_tile::Tile_GeomType geometryType(const QString &str)
|
||||
{
|
||||
if (str == "Point")
|
||||
return vector_tile::Tile_GeomType_POINT;
|
||||
else if (str == "LineString")
|
||||
return vector_tile::Tile_GeomType_LINESTRING;
|
||||
else if (str == "Polygon")
|
||||
return vector_tile::Tile_GeomType_POLYGON;
|
||||
else
|
||||
return vector_tile::Tile_GeomType_UNKNOWN;
|
||||
}
|
||||
|
||||
Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
: _type(Unknown), _not(false)
|
||||
{
|
||||
@ -27,9 +41,15 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
if (type == "==") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
_type = EQ;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
if (json.at(1).toString() == "$type") {
|
||||
_type = GeometryType;
|
||||
_kv = QPair<QString, QVariant>(QString(),
|
||||
QVariant(geometryType(json.at(2).toString())));
|
||||
} else {
|
||||
_type = EQ;
|
||||
_kv = QPair<QString, QVariant>(json.at(1).toString(),
|
||||
json.at(2).toVariant());
|
||||
}
|
||||
} else if (type == "!=") {
|
||||
if (json.size() != 3)
|
||||
INVALID_FILTER(json);
|
||||
@ -98,60 +118,107 @@ Style::Layer::Filter::Filter(const QJsonArray &json)
|
||||
INVALID_FILTER(json);
|
||||
}
|
||||
|
||||
bool Style::Layer::Filter::match(const QVariantHash &tags) const
|
||||
bool Style::Layer::Filter::match(const PBF::Feature &feature) const
|
||||
{
|
||||
const QVariant *v;
|
||||
|
||||
switch (_type) {
|
||||
case None:
|
||||
return true;
|
||||
case EQ:
|
||||
return tags.value(_kv.first) == _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
return *v == _kv.second;
|
||||
case NE:
|
||||
return tags.value(_kv.first) != _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return true;
|
||||
else
|
||||
return *v != _kv.second;
|
||||
case GT:
|
||||
return tags.value(_kv.first) > _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
return *v > _kv.second;
|
||||
#else // QT6
|
||||
return (QVariant::compare(*v, _kv.second)
|
||||
== QPartialOrdering::Greater);
|
||||
#endif // QT6
|
||||
case GE:
|
||||
return tags.value(_kv.first) >= _kv.second;
|
||||
{if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else {
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
return *v >= _kv.second;
|
||||
#else // QT6
|
||||
QPartialOrdering res = QVariant::compare(*v, _kv.second);
|
||||
return (res == QPartialOrdering::Greater
|
||||
|| res == QPartialOrdering::Equivalent);
|
||||
#endif // QT6
|
||||
}}
|
||||
case LT:
|
||||
return tags.value(_kv.first) < _kv.second;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
return *v < _kv.second;
|
||||
#else // QT6
|
||||
return (QVariant::compare(*v, _kv.second)
|
||||
== QPartialOrdering::Less);
|
||||
#endif // QT6
|
||||
case LE:
|
||||
return tags.value(_kv.first) <= _kv.second;
|
||||
{if (!(v = feature.value(_kv.first)))
|
||||
return false;
|
||||
else {
|
||||
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
|
||||
return *v <= _kv.second;
|
||||
#else // QT6
|
||||
QPartialOrdering res = QVariant::compare(*v, _kv.second);
|
||||
return (res == QPartialOrdering::Less
|
||||
|| res == QPartialOrdering::Equivalent);
|
||||
#endif // QT6
|
||||
}}
|
||||
case In:
|
||||
return _set.contains(tags.value(_kv.first).toString()) ^ _not;
|
||||
if (!(v = feature.value(_kv.first)))
|
||||
return _not;
|
||||
else
|
||||
return _set.contains((*v).toString()) ^ _not;
|
||||
case Has:
|
||||
return tags.contains(_kv.first) ^ _not;
|
||||
return (feature.value(_kv.first) ? true : false) ^ _not;
|
||||
case All:
|
||||
for (int i = 0; i < _filters.size(); i++) {
|
||||
if (!_filters.at(i).match(tags))
|
||||
for (int i = 0; i < _filters.size(); i++)
|
||||
if (!_filters.at(i).match(feature))
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
case Any:
|
||||
for (int i = 0; i < _filters.size(); i++) {
|
||||
if (_filters.at(i).match(tags))
|
||||
for (int i = 0; i < _filters.size(); i++)
|
||||
if (_filters.at(i).match(feature))
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
case GeometryType:
|
||||
return feature.type() == _kv.second.toInt();
|
||||
default:
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
QString Style::Layer::Template::value(int zoom, const QVariantHash &tags) const
|
||||
QString Style::Layer::Template::value(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
QRegExp rx = QRegExp("\\{[^\\}]*\\}");
|
||||
QRegularExpression rx("\\{[^\\}]*\\}");
|
||||
QString text(_field.value(zoom));
|
||||
QRegularExpressionMatchIterator it = rx.globalMatch(text);
|
||||
QStringList keys;
|
||||
int pos = 0;
|
||||
|
||||
while ((pos = rx.indexIn(text, pos)) != -1) {
|
||||
QString match = rx.capturedTexts().first();
|
||||
keys.append(match.mid(1, match.size() - 2));
|
||||
pos += rx.matchedLength();
|
||||
while (it.hasNext()) {
|
||||
QRegularExpressionMatch match = it.next();
|
||||
QString val = match.captured(0);
|
||||
keys.append(val.mid(1, val.size() - 2));
|
||||
}
|
||||
for (int i = 0; i < keys.size(); i++) {
|
||||
const QString &key = keys.at(i);
|
||||
const QVariant val = tags.value(key);
|
||||
text.replace(QString("{%1}").arg(key), val.toString());
|
||||
const QVariant *val = feature.value(key);
|
||||
text.replace(QString("{%1}").arg(key), val ? val->toString() : "");
|
||||
}
|
||||
|
||||
return text;
|
||||
@ -188,6 +255,9 @@ Style::Layer::Paint::Paint(const QJsonObject &json)
|
||||
|
||||
// text
|
||||
_textColor = FunctionC(json["text-color"]);
|
||||
_textHaloColor = FunctionC(json["text-halo-color"], QColor());
|
||||
_textHaloWidth = FunctionF(json["text-halo-width"]);
|
||||
_textHaloBlur = FunctionF(json["text-halo-blur"]);
|
||||
}
|
||||
|
||||
QPen Style::Layer::Paint::pen(Type type, int zoom) const
|
||||
@ -303,6 +373,12 @@ Style::Layer::Layout::Layout(const QJsonObject &json)
|
||||
|
||||
// symbol
|
||||
_symbolPlacement = FunctionS(json["symbol-placement"]);
|
||||
|
||||
// visibility
|
||||
if (json.contains("visibility") && json["visibility"].isString())
|
||||
_visible = !(json["visibility"].toString() == "none");
|
||||
else
|
||||
_visible = true;
|
||||
}
|
||||
|
||||
QFont Style::Layer::Layout::font(int zoom) const
|
||||
@ -392,7 +468,7 @@ Text::RotationAlignment Style::Layer::Layout::textRotationAlignment(int zoom)
|
||||
}
|
||||
|
||||
Style::Layer::Layer(const QJsonObject &json)
|
||||
: _type(Unknown), _minZoom(-1), _maxZoom(-1)
|
||||
: _type(Unknown), _minZoom(0), _maxZoom(24)
|
||||
{
|
||||
// type
|
||||
QString type(json["type"].toString());
|
||||
@ -428,16 +504,12 @@ Style::Layer::Layer(const QJsonObject &json)
|
||||
_paint = Paint(json["paint"].toObject());
|
||||
}
|
||||
|
||||
bool Style::Layer::match(int zoom, const QVariantHash &tags) const
|
||||
bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
|
||||
{
|
||||
if (zoom >= 0) {
|
||||
if (_minZoom > 0 && zoom < _minZoom)
|
||||
return false;
|
||||
if (_maxZoom > 0 && zoom > _maxZoom)
|
||||
return false;
|
||||
}
|
||||
if (zoom >= 0 && (zoom < _minZoom || zoom > _maxZoom))
|
||||
return false;
|
||||
|
||||
return _filter.match(tags);
|
||||
return _filter.match(feature);
|
||||
}
|
||||
|
||||
void Style::Layer::setPathPainter(Tile &tile, const Sprites &sprites) const
|
||||
@ -469,16 +541,17 @@ void Style::Layer::setTextProperties(Tile &tile) const
|
||||
t.setFont(_layout.font(zoom));
|
||||
t.setSymbolPlacement(_layout.symbolPlacement(zoom));
|
||||
t.setRotationAlignment(_layout.textRotationAlignment(zoom));
|
||||
t.setHalo(_paint.halo(zoom));
|
||||
}
|
||||
|
||||
void Style::Layer::addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const QVariantHash &tags, const Sprites &sprites) const
|
||||
const PBF::Feature &feature, const Sprites &sprites) const
|
||||
{
|
||||
QString text = _layout.text(tile.zoom(), tags);
|
||||
QString text = _layout.text(tile.zoom(), feature);
|
||||
if (text.isEmpty())
|
||||
return;
|
||||
|
||||
QString icon = _layout.icon(tile.zoom(), tags);
|
||||
QString icon = _layout.icon(tile.zoom(), feature);
|
||||
tile.text().addLabel(text, sprites.icon(icon), path);
|
||||
}
|
||||
|
||||
@ -525,9 +598,6 @@ bool Style::load(const QString &fileName)
|
||||
_layers.append(Layer(layers[i].toObject()));
|
||||
}
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++)
|
||||
_sourceLayers.append(_layers.at(i).sourceLayer());
|
||||
|
||||
QDir styleDir = QFileInfo(fileName).absoluteDir();
|
||||
loadSprites(styleDir, "sprite.json", "sprite.png", _sprites);
|
||||
loadSprites(styleDir, "sprite@2x.json", "sprite@2x.png", _sprites2x);
|
||||
@ -535,34 +605,24 @@ bool Style::load(const QString &fileName)
|
||||
return true;
|
||||
}
|
||||
|
||||
void Style::setupLayer(Tile &tile, int layer) const
|
||||
const Sprites &Style::sprites(const QPointF &scale) const
|
||||
{
|
||||
const Layer &sl = _layers.at(layer);
|
||||
const Sprites &sprites = (tile.scale().x() > 1.0 || tile.scale().y() > 1.0)
|
||||
return (scale.x() > 1.0 || scale.y() > 1.0)
|
||||
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
||||
|
||||
if (sl.isSymbol())
|
||||
sl.setTextProperties(tile);
|
||||
else if (sl.isPath())
|
||||
sl.setPathPainter(tile, sprites);
|
||||
}
|
||||
|
||||
void Style::drawFeature(Tile &tile, int layer, const QPainterPath &path,
|
||||
const QVariantHash &tags) const
|
||||
void Style::setupLayer(Tile &tile, const Layer &layer) const
|
||||
{
|
||||
const Layer &sl = _layers.at(layer);
|
||||
const Sprites &sprites = (tile.scale().x() > 1.0 || tile.scale().y() > 1.0)
|
||||
&& !_sprites2x.isNull() ? _sprites2x : _sprites;
|
||||
|
||||
if (sl.isPath())
|
||||
tile.painter().drawPath(path);
|
||||
else if (sl.isSymbol())
|
||||
sl.addSymbol(tile, path, tags, sprites);
|
||||
if (layer.isSymbol())
|
||||
layer.setTextProperties(tile);
|
||||
else if (layer.isPath())
|
||||
layer.setPathPainter(tile, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawBackground(Tile &tile) const
|
||||
{
|
||||
QRectF rect(QPointF(0, 0), tile.size());
|
||||
QRectF rect(QPointF(0, 0), QSizeF(tile.size().width() / tile.scale().x(),
|
||||
tile.size().height() / tile.scale().y()));
|
||||
QPainterPath path;
|
||||
path.addRect(rect);
|
||||
|
||||
@ -578,3 +638,55 @@ void Style::drawBackground(Tile &tile) const
|
||||
//tile.painter().setPen(Qt::red);
|
||||
//tile.painter().drawRect(rect);
|
||||
}
|
||||
|
||||
void Style::drawFeature(const PBF::Feature &feature, const Layer &layer,
|
||||
Tile &tile, const QSizeF &factor) const
|
||||
{
|
||||
if (!layer.match(tile.zoom(), feature))
|
||||
return;
|
||||
|
||||
QPainterPath path(feature.path(factor));
|
||||
if (!path.elementCount())
|
||||
return;
|
||||
|
||||
if (layer.isPath())
|
||||
tile.painter().drawPath(path);
|
||||
else if (layer.isSymbol())
|
||||
layer.addSymbol(tile, path, feature, sprites(tile.scale()));
|
||||
}
|
||||
|
||||
void Style::drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const
|
||||
{
|
||||
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());
|
||||
|
||||
tile.painter().save();
|
||||
setupLayer(tile, styleLayer);
|
||||
for (int i = 0; i < pbfLayer.features().size(); i++)
|
||||
drawFeature(pbfLayer.features().at(i), styleLayer, tile, factor);
|
||||
tile.painter().restore();
|
||||
}
|
||||
|
||||
void Style::render(const PBF &data, Tile &tile) const
|
||||
{
|
||||
drawBackground(tile);
|
||||
|
||||
for (int i = 0; i < _layers.size(); i++) {
|
||||
QHash<QString, PBF::Layer*>::const_iterator it = data.layers().find(
|
||||
_layers.at(i).sourceLayer());
|
||||
if (it == data.layers().constEnd())
|
||||
continue;
|
||||
|
||||
drawLayer(**it, _layers.at(i), tile);
|
||||
}
|
||||
|
||||
tile.text().render(&tile.painter());
|
||||
}
|
||||
|
59
src/style.h
59
src/style.h
@ -3,12 +3,15 @@
|
||||
|
||||
#include <QObject>
|
||||
#include <QString>
|
||||
#include <QVector>
|
||||
#include <QPair>
|
||||
#include <QVariantHash>
|
||||
#include <QStringList>
|
||||
#include <QSet>
|
||||
#include <QPen>
|
||||
#include <QBrush>
|
||||
#include <QFont>
|
||||
#include "pbf.h"
|
||||
#include "text.h"
|
||||
#include "function.h"
|
||||
#include "sprites.h"
|
||||
@ -16,6 +19,8 @@
|
||||
|
||||
class QPainter;
|
||||
class QPainterPath;
|
||||
class QJsonArray;
|
||||
class QJsonObject;
|
||||
class Tile;
|
||||
|
||||
class Style : public QObject
|
||||
@ -24,33 +29,25 @@ public:
|
||||
Style(QObject *parent = 0) : QObject(parent) {}
|
||||
|
||||
bool load(const QString &fileName);
|
||||
|
||||
const QStringList &sourceLayers() const
|
||||
{return _sourceLayers;}
|
||||
|
||||
bool match(int zoom, int layer, const QVariantHash &tags) const
|
||||
{return _layers.at(layer).match(zoom, tags);}
|
||||
void drawBackground(Tile &tile) const;
|
||||
void setupLayer(Tile &tile, int layer) const;
|
||||
void drawFeature(Tile &tile, int layer, const QPainterPath &path,
|
||||
const QVariantHash &tags) const;
|
||||
void render(const PBF &data, Tile &tile) const;
|
||||
|
||||
private:
|
||||
class Layer {
|
||||
public:
|
||||
Layer() : _type(Unknown), _minZoom(-1), _maxZoom(-1) {}
|
||||
Layer() : _type(Unknown), _minZoom(0), _maxZoom(24) {}
|
||||
Layer(const QJsonObject &json);
|
||||
|
||||
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 QVariantHash &tags) const;
|
||||
bool match(int zoom, const PBF::Feature &feature) const;
|
||||
void setPathPainter(Tile &tile, const Sprites &sprites) const;
|
||||
void setTextProperties(Tile &tile) const;
|
||||
void addSymbol(Tile &tile, const QPainterPath &path,
|
||||
const QVariantHash &tags, const Sprites &sprites) const;
|
||||
const PBF::Feature &feature, const Sprites &sprites) const;
|
||||
|
||||
private:
|
||||
enum Type {
|
||||
@ -66,13 +63,13 @@ private:
|
||||
Filter() : _type(None) {}
|
||||
Filter(const QJsonArray &json);
|
||||
|
||||
bool match(const QVariantHash &tags) const;
|
||||
bool match(const PBF::Feature &feature) const;
|
||||
private:
|
||||
enum Type {
|
||||
None, Unknown,
|
||||
EQ, NE, GE, GT, LE, LT,
|
||||
All, Any,
|
||||
In, Has
|
||||
In, Has, GeometryType
|
||||
};
|
||||
|
||||
Type _type;
|
||||
@ -86,7 +83,7 @@ private:
|
||||
public:
|
||||
Template() {}
|
||||
Template(const FunctionS &str) : _field(str) {}
|
||||
QString value(int zoom, const QVariantHash &tags) const;
|
||||
QString value(int zoom, const PBF::Feature &feature) const;
|
||||
|
||||
private:
|
||||
FunctionS _field;
|
||||
@ -95,23 +92,24 @@ private:
|
||||
class Layout {
|
||||
public:
|
||||
Layout() : _textSize(16), _textMaxWidth(10), _textMaxAngle(45),
|
||||
_font("Open Sans") {}
|
||||
_font("Open Sans"), _visible(true) {}
|
||||
Layout(const QJsonObject &json);
|
||||
|
||||
qreal maxTextWidth(int zoom) const
|
||||
{return _textMaxWidth.value(zoom);}
|
||||
qreal maxTextAngle(int zoom) const
|
||||
{return _textMaxAngle.value(zoom);}
|
||||
QString text(int zoom, const QVariantHash &tags) const
|
||||
{return _text.value(zoom, tags).trimmed();}
|
||||
QString icon(int zoom, const QVariantHash &tags) const
|
||||
{return _icon.value(zoom, tags);}
|
||||
QString text(int zoom, const PBF::Feature &feature) const
|
||||
{return _text.value(zoom, feature).trimmed();}
|
||||
QString icon(int zoom, const PBF::Feature &feature) const
|
||||
{return _icon.value(zoom, feature);}
|
||||
QFont font(int zoom) const;
|
||||
Qt::PenCapStyle lineCap(int zoom) const;
|
||||
Qt::PenJoinStyle lineJoin(int zoom) const;
|
||||
Text::Anchor textAnchor(int zoom) const;
|
||||
Text::SymbolPlacement symbolPlacement(int zoom) const;
|
||||
Text::RotationAlignment textRotationAlignment(int zoom) const;
|
||||
bool visible() const {return _visible;}
|
||||
|
||||
private:
|
||||
QFont::Capitalization textTransform(int zoom) const;
|
||||
@ -128,6 +126,7 @@ private:
|
||||
FunctionS _symbolPlacement;
|
||||
FunctionS _textRotationAlignment;
|
||||
QFont _font;
|
||||
bool _visible;
|
||||
};
|
||||
|
||||
class Paint {
|
||||
@ -140,11 +139,13 @@ private:
|
||||
const;
|
||||
qreal opacity(Layer::Type type, int zoom) const;
|
||||
bool antialias(Layer::Type type, int zoom) const;
|
||||
QString fillPattern(int zoom) const
|
||||
{return _fillPattern.value(zoom);}
|
||||
Text::Halo halo(int zoom) const
|
||||
{return Text::Halo(_textHaloColor.value(zoom),
|
||||
_textHaloWidth.value(zoom), _textHaloBlur.value(zoom));}
|
||||
|
||||
private:
|
||||
FunctionC _textColor;
|
||||
FunctionC _textHaloColor;
|
||||
FunctionC _lineColor;
|
||||
FunctionC _fillColor;
|
||||
FunctionC _fillOutlineColor;
|
||||
@ -152,6 +153,8 @@ private:
|
||||
FunctionF _fillOpacity;
|
||||
FunctionF _lineOpacity;
|
||||
FunctionF _lineWidth;
|
||||
FunctionF _textHaloWidth;
|
||||
FunctionF _textHaloBlur;
|
||||
FunctionB _fillAntialias;
|
||||
QVector<qreal> _lineDasharray;
|
||||
FunctionS _fillPattern;
|
||||
@ -165,8 +168,16 @@ private:
|
||||
Paint _paint;
|
||||
};
|
||||
|
||||
const Sprites &sprites(const QPointF &scale) const;
|
||||
|
||||
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) const;
|
||||
void drawLayer(const PBF::Layer &pbfLayer, const Layer &styleLayer,
|
||||
Tile &tile) const;
|
||||
|
||||
QVector<Layer> _layers;
|
||||
QStringList _sourceLayers;
|
||||
Sprites _sprites, _sprites2x;
|
||||
};
|
||||
|
||||
|
47
src/text.cpp
47
src/text.cpp
@ -1,6 +1,7 @@
|
||||
#include <QFontMetrics>
|
||||
#include <QPainter>
|
||||
#include <QSet>
|
||||
#include <QMap>
|
||||
#include "text.h"
|
||||
#include "textpointitem.h"
|
||||
#include "textpathitem.h"
|
||||
@ -8,8 +9,7 @@
|
||||
|
||||
Text::~Text()
|
||||
{
|
||||
for (int i = 0; i < _items.size(); i++)
|
||||
delete _items[i];
|
||||
qDeleteAll(_items);
|
||||
}
|
||||
|
||||
void Text::render(QPainter *painter) const
|
||||
@ -31,22 +31,36 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
||||
{
|
||||
TextItem *ti;
|
||||
|
||||
switch (_placement) {
|
||||
case Line:
|
||||
if (_alignment == Viewport)
|
||||
if (_alignment == Viewport) {
|
||||
QMap<qreal, int> map;
|
||||
for (int j = 0; j < path.elementCount(); j++) {
|
||||
QLineF l(path.elementAt(j), _sceneRect.center());
|
||||
map.insert(l.length(), j);
|
||||
}
|
||||
QMap<qreal, int>::const_iterator jt = map.constBegin();
|
||||
ti = new TextPointItem(text, path.elementAt(jt.value()), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
while (true) {
|
||||
if (_sceneRect.contains(ti->boundingRect()))
|
||||
break;
|
||||
if (++jt == map.constEnd())
|
||||
break;
|
||||
static_cast<TextPointItem*>(ti)->setPos(path.elementAt(
|
||||
jt.value()));
|
||||
}
|
||||
} else {
|
||||
switch (_placement) {
|
||||
case Line:
|
||||
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
|
||||
break;
|
||||
case LineCenter:
|
||||
ti = new TextPointItem(text, path.pointAtPercent(0.5), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
break;
|
||||
default:
|
||||
ti = new TextPointItem(text, path.elementAt(0), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
else
|
||||
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
|
||||
break;
|
||||
case LineCenter:
|
||||
ti = new TextPointItem(text, path.pointAtPercent(0.5), _font,
|
||||
_maxWidth, _anchor, icon);
|
||||
break;
|
||||
default:
|
||||
ti = new TextPointItem(text, path.elementAt(0), _font, _maxWidth,
|
||||
_anchor, icon);
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
// Note: empty path == point geometry (single move)
|
||||
@ -56,6 +70,7 @@ void Text::addLabel(const QString &text, const QImage &icon,
|
||||
}
|
||||
|
||||
ti->setPen(_pen);
|
||||
ti->setHalo(_halo);
|
||||
addItem(ti);
|
||||
|
||||
QList<TextItem*> ci = collidingItems(ti);
|
||||
|
18
src/text.h
18
src/text.h
@ -34,6 +34,22 @@ public:
|
||||
Auto
|
||||
};
|
||||
|
||||
class Halo {
|
||||
public:
|
||||
Halo() : _width(0), _blur(0) {}
|
||||
Halo(const QColor &color, qreal width, qreal blur)
|
||||
: _color(color), _width(width), _blur(blur) {}
|
||||
|
||||
const QColor &color() const {return _color;}
|
||||
qreal width() const {return _width;}
|
||||
qreal blur() const {return _blur;}
|
||||
|
||||
private:
|
||||
QColor _color;
|
||||
qreal _width;
|
||||
qreal _blur;
|
||||
};
|
||||
|
||||
Text(const QSize &size) : _sceneRect(QRectF(QPointF(0, 0), size)) {}
|
||||
~Text();
|
||||
|
||||
@ -45,6 +61,7 @@ public:
|
||||
void setSymbolPlacement(SymbolPlacement placement);
|
||||
void setRotationAlignment(RotationAlignment alignment)
|
||||
{_alignment = alignment;}
|
||||
void setHalo(const Halo &halo) {_halo = halo;}
|
||||
|
||||
void addLabel(const QString &text, const QImage &icon,
|
||||
const QPainterPath &path);
|
||||
@ -65,6 +82,7 @@ private:
|
||||
RotationAlignment _alignment;
|
||||
QFont _font;
|
||||
QPen _pen;
|
||||
Halo _halo;
|
||||
};
|
||||
|
||||
#endif // TEXT_H
|
||||
|
39
src/textitem.cpp
Normal file
39
src/textitem.cpp
Normal file
@ -0,0 +1,39 @@
|
||||
#include "textitem.h"
|
||||
|
||||
bool TextItem::collidesWithItem(const TextItem *other) const
|
||||
{
|
||||
QRectF r1(boundingRect());
|
||||
QRectF r2(other->boundingRect());
|
||||
|
||||
if (r1.isEmpty() || r2.isEmpty() || !r1.intersects(r2))
|
||||
return false;
|
||||
|
||||
return other->shape().intersects(shape());
|
||||
}
|
||||
|
||||
qreal TextItem::avgCharWidth() const
|
||||
{
|
||||
qreal ratio;
|
||||
ushort cp = _text.at(0).unicode();
|
||||
|
||||
// CJK & East Asian scripts
|
||||
if (cp >= 0x2E80)
|
||||
ratio = 1.0;
|
||||
// Greek & Cyrilic
|
||||
else if (cp >= 0x03FF && cp <= 0x04FF) {
|
||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.80 : 0.70;
|
||||
if (_font.bold())
|
||||
ratio *= 1.1;
|
||||
if (_font.italic())
|
||||
ratio *= 0.9;
|
||||
// The rest (Latin scripts, Arabic, ...)
|
||||
} else {
|
||||
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.60;
|
||||
if (_font.bold())
|
||||
ratio *= 1.1;
|
||||
if (_font.italic())
|
||||
ratio *= 0.9;
|
||||
}
|
||||
|
||||
return ratio * _font.pixelSize();
|
||||
}
|
@ -1,20 +1,28 @@
|
||||
#ifndef TEXTITEM_H
|
||||
#define TEXTITEM_H
|
||||
|
||||
#include <QString>
|
||||
#include <QPainterPath>
|
||||
#include <QPen>
|
||||
#include <QFont>
|
||||
#include <QRectF>
|
||||
#include "text.h"
|
||||
|
||||
class QPainter;
|
||||
|
||||
class TextItem
|
||||
{
|
||||
public:
|
||||
TextItem(const QString &text) : _text(text), _visible(true) {}
|
||||
TextItem(const QString &text, const QFont &font)
|
||||
: _text(text), _font(font), _visible(true) {}
|
||||
virtual ~TextItem() {}
|
||||
|
||||
const QString &text() const {return _text;}
|
||||
const QFont &font() const {return _font;}
|
||||
void setFont(const QFont &font) {_font = font;}
|
||||
const QPen &pen() const {return _pen;}
|
||||
const Text::Halo &halo() const {return _halo;}
|
||||
void setPen(const QPen &pen) {_pen = pen;}
|
||||
void setHalo(const Text::Halo &halo) {_halo = halo;}
|
||||
|
||||
virtual QPainterPath shape() const = 0;
|
||||
virtual QRectF boundingRect() const = 0;
|
||||
@ -23,38 +31,16 @@ public:
|
||||
bool isVisible() const {return _visible;}
|
||||
void setVisible(bool visible) {_visible = visible;}
|
||||
|
||||
bool collidesWithItem(const TextItem *other) const
|
||||
{
|
||||
QRectF r1(boundingRect());
|
||||
QRectF r2(other->boundingRect());
|
||||
|
||||
if (r1.isEmpty() || r2.isEmpty() || !r1.intersects(r2))
|
||||
return false;
|
||||
|
||||
return other->shape().intersects(shape());
|
||||
}
|
||||
bool collidesWithItem(const TextItem *other) const;
|
||||
|
||||
protected:
|
||||
static int avgCharWidth(const QString &str, const QFont &font)
|
||||
{
|
||||
qreal ratio;
|
||||
|
||||
if (str.at(0).unicode() >= 0x2E80)
|
||||
ratio = 1.0;
|
||||
else {
|
||||
ratio = (font.capitalization() == QFont::AllUppercase)
|
||||
? 0.66 : 0.55;
|
||||
if (font.bold())
|
||||
ratio *= 1.1;
|
||||
}
|
||||
|
||||
return ratio * font.pixelSize();
|
||||
}
|
||||
qreal avgCharWidth() const;
|
||||
|
||||
private:
|
||||
QString _text;
|
||||
QFont _font;
|
||||
QPen _pen;
|
||||
Text::Halo _halo;
|
||||
bool _visible;
|
||||
};
|
||||
|
||||
|
@ -3,41 +3,101 @@
|
||||
#include "textpathitem.h"
|
||||
|
||||
|
||||
static QPointF intersection(const QLineF &line, const QRectF &rect)
|
||||
{
|
||||
QPointF p;
|
||||
if (line.intersect(QLineF(rect.topLeft(), rect.topRight()), &p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
if (line.intersect(QLineF(rect.topLeft(), rect.bottomLeft()), &p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
if (line.intersect(QLineF(rect.bottomRight(), rect.bottomLeft()), &p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
if (line.intersect(QLineF(rect.bottomRight(), rect.topRight()), &p)
|
||||
== QLineF::BoundedIntersection)
|
||||
return p;
|
||||
#if QT_VERSION < QT_VERSION_CHECK(5, 15, 0)
|
||||
#define INTERSECTS intersect
|
||||
#else // QT 5.15
|
||||
#define INTERSECTS intersects
|
||||
#endif // QT 5.15
|
||||
|
||||
return rect.center();
|
||||
static void swap(const QLineF &line, QPointF *p1, QPointF *p2)
|
||||
{
|
||||
|
||||
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 QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
|
||||
static bool intersection(const QLineF &line, const QRectF &rect, QPointF *p1,
|
||||
QPointF *p2)
|
||||
{
|
||||
QPointF *p = p1;
|
||||
|
||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection)
|
||||
p = p2;
|
||||
if (line.INTERSECTS(QLineF(rect.topLeft(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection) {
|
||||
if (p == p2) {
|
||||
swap(line, p1, p2);
|
||||
return true;
|
||||
}
|
||||
p = p2;
|
||||
}
|
||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
|
||||
== QLineF::BoundedIntersection) {
|
||||
if (p == p2) {
|
||||
swap(line, p1, p2);
|
||||
return true;
|
||||
}
|
||||
p = p2;
|
||||
}
|
||||
if (line.INTERSECTS(QLineF(rect.bottomRight(), rect.topRight()), p)
|
||||
== QLineF::BoundedIntersection) {
|
||||
if (p == p2) {
|
||||
swap(line, p1, p2);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
Q_ASSERT(p != p2);
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
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 ss = 0, es = 0;
|
||||
int si = start, ei = end;
|
||||
|
||||
for (int i = start; i <= end; i++) {
|
||||
qreal len = lines.at(i).length();
|
||||
for (int i = start; i < end; i++) {
|
||||
QLineF l(path.at(i), path.at(i+1));
|
||||
qreal len = l.length();
|
||||
if (ss + len < cut / 2) {
|
||||
ss += len;
|
||||
si++;
|
||||
} else
|
||||
break;
|
||||
}
|
||||
for (int i = end; i >= start; i--) {
|
||||
qreal len = lines.at(i).length();
|
||||
for (int i = end; i > start; i--) {
|
||||
QLineF l(path.at(i), path.at(i-1));
|
||||
qreal len = l.length();
|
||||
if (es + len < cut / 2) {
|
||||
es += len;
|
||||
ei--;
|
||||
@ -45,83 +105,117 @@ static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
|
||||
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));
|
||||
QLineF el(lines.at(ei));
|
||||
QLineF el(path.at(ei-1), path.at(ei));
|
||||
el.setLength(el.length() - (cut / 2 - es));
|
||||
|
||||
QPainterPath p(sl.p2());
|
||||
for (int i = si; i <= ei; i++)
|
||||
p.lineTo(lines.at(i).p2());
|
||||
p.setElementPositionAt(p.elementCount() - 1, el.p2().x(), el.p2().y());
|
||||
for (int i = si + 1; i < ei; i++)
|
||||
p.lineTo(path.at(i));
|
||||
p.lineTo(el.p2());
|
||||
|
||||
return p;
|
||||
}
|
||||
|
||||
static QList<QLineF> lineString(const QPainterPath &path,
|
||||
const QRectF &boundingRect)
|
||||
static QList<QPolygonF> polyLines(const QPainterPath &path, const QRectF &rect)
|
||||
{
|
||||
QList<QLineF> lines;
|
||||
int start = 0, end = path.elementCount() - 1;
|
||||
QList<QPolygonF> lines;
|
||||
QPolygonF line;
|
||||
bool lastIn = rect.contains(path.elementAt(0));
|
||||
|
||||
for (int i = 0; i < path.elementCount(); i++) {
|
||||
if (boundingRect.contains(path.elementAt(i))) {
|
||||
start = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
for (int i = path.elementCount() - 1; i >= 0; i--) {
|
||||
if (boundingRect.contains(path.elementAt(i))) {
|
||||
end = i;
|
||||
break;
|
||||
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));
|
||||
|
||||
if (intersection(l, rect, &p))
|
||||
line.append(p);
|
||||
line.append(path.elementAt(i));
|
||||
}
|
||||
|
||||
lastIn = true;
|
||||
} else {
|
||||
QLineF l(path.elementAt(i-1), path.elementAt(i));
|
||||
|
||||
if (lastIn) {
|
||||
QPointF p;
|
||||
if (line.isEmpty())
|
||||
line.append(path.elementAt(i-1));
|
||||
if (intersection(l, rect, &p))
|
||||
line.append(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 (start > 0) {
|
||||
QLineF l(path.elementAt(start-1), path.elementAt(start));
|
||||
QPointF p(intersection(l, boundingRect));
|
||||
if (p != boundingRect.center())
|
||||
lines.append(QLineF(p, path.elementAt(start)));
|
||||
}
|
||||
for (int i = start + 1; i <= end; i++)
|
||||
lines.append(QLineF(path.elementAt(i-1), path.elementAt(i)));
|
||||
if (end < path.elementCount() - 1) {
|
||||
QLineF l(path.elementAt(end), path.elementAt(end+1));
|
||||
QPointF p(intersection(l, boundingRect));
|
||||
if (p != boundingRect.center())
|
||||
lines.append(QLineF(path.elementAt(end), p));
|
||||
}
|
||||
if (!line.isEmpty())
|
||||
lines.append(line);
|
||||
|
||||
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,
|
||||
qreal maxAngle, qreal charWidth, const QRectF &tileRect)
|
||||
{
|
||||
QList<QLineF> lines(lineString(path, tileRect));
|
||||
qreal length = 0;
|
||||
qreal angle = lines.first().angle();
|
||||
int last = 0;
|
||||
if (path.isEmpty())
|
||||
return QPainterPath();
|
||||
|
||||
QList<QPolygonF> lines(polyLines(path, tileRect));
|
||||
|
||||
for (int i = 0; i < lines.size(); i++) {
|
||||
qreal sl = lines.at(i).length();
|
||||
qreal a = lines.at(i).angle();
|
||||
const QPolygonF &pl = lines.at(i);
|
||||
qreal angle = 0, length = 0;
|
||||
int last = 0;
|
||||
|
||||
if (!tileRect.contains(lines.at(i).p2()) || sl < charWidth
|
||||
|| qAbs(angle - a) > maxAngle) {
|
||||
if (length > textWidth)
|
||||
return subpath(lines, last, i - 1, length - textWidth);
|
||||
last = i;
|
||||
length = 0;
|
||||
} else
|
||||
length += sl;
|
||||
for (int j = 1; j < pl.size(); j ++) {
|
||||
QLineF l(pl.at(j-1), pl.at(j));
|
||||
qreal sl = l.length();
|
||||
qreal a = l.angle();
|
||||
|
||||
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)
|
||||
? subpath(lines, last, lines.size() - 1, length - textWidth)
|
||||
: QPainterPath();
|
||||
return QPainterPath();
|
||||
}
|
||||
|
||||
static bool reverse(const QPainterPath &path)
|
||||
@ -134,9 +228,9 @@ static bool reverse(const QPainterPath &path)
|
||||
|
||||
TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
||||
const QFont &font, int maxAngle, const QRectF &tileRect)
|
||||
: TextItem(text)
|
||||
: TextItem(text, font)
|
||||
{
|
||||
int cw = avgCharWidth(text, font);
|
||||
qreal cw = avgCharWidth();
|
||||
int textWidth = text.size() * cw;
|
||||
if (textWidth > path.length())
|
||||
return;
|
||||
@ -146,7 +240,6 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
||||
return;
|
||||
|
||||
_path = reverse(tp) ? tp.toReversed() : tp;
|
||||
setFont(font);
|
||||
|
||||
QPainterPathStroker s;
|
||||
s.setWidth(font.pixelSize());
|
||||
@ -157,20 +250,42 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
|
||||
|
||||
void TextPathItem::paint(QPainter *painter) const
|
||||
{
|
||||
//painter->setPen(Qt::red);
|
||||
//painter->drawPath(_shape);
|
||||
|
||||
QFontMetrics fm(font());
|
||||
int textWidth = fm.width(text());
|
||||
int textWidth = fm.boundingRect(text()).width();
|
||||
|
||||
qreal factor = (textWidth) / qMax(_path.length(), (qreal)textWidth);
|
||||
qreal percent = (1.0 - factor) / 2.0;
|
||||
|
||||
painter->setFont(font());
|
||||
painter->setPen(pen());
|
||||
|
||||
QTransform t = painter->transform();
|
||||
|
||||
if (halo().color().isValid() && halo().width() > 0) {
|
||||
painter->setPen(halo().color());
|
||||
for (int i = 0; i < text().size(); i++) {
|
||||
QPointF point = _path.pointAtPercent(percent);
|
||||
qreal angle = _path.angleAtPercent(percent);
|
||||
|
||||
painter->translate(point);
|
||||
painter->rotate(-angle);
|
||||
painter->drawText(QPoint(-1, fm.descent() - 1), text().at(i));
|
||||
painter->drawText(QPoint(1, fm.descent() + 1), text().at(i));
|
||||
painter->drawText(QPoint(-1, fm.descent() + 1), text().at(i));
|
||||
painter->drawText(QPoint(1, fm.descent() -1), text().at(i));
|
||||
painter->drawText(QPoint(0, fm.descent() - 1), text().at(i));
|
||||
painter->drawText(QPoint(0, fm.descent() + 1), text().at(i));
|
||||
painter->drawText(QPoint(-1, fm.descent()), text().at(i));
|
||||
painter->drawText(QPoint(1, fm.descent()), text().at(i));
|
||||
painter->setTransform(t);
|
||||
|
||||
int width = fm.horizontalAdvance(text().at(i));
|
||||
percent += ((qreal)width / (qreal)textWidth) * factor;
|
||||
}
|
||||
|
||||
percent = (1.0 - factor) / 2.0;
|
||||
}
|
||||
|
||||
painter->setPen(pen());
|
||||
for (int i = 0; i < text().size(); i++) {
|
||||
QPointF point = _path.pointAtPercent(percent);
|
||||
qreal angle = _path.angleAtPercent(percent);
|
||||
@ -180,7 +295,12 @@ void TextPathItem::paint(QPainter *painter) const
|
||||
painter->drawText(QPoint(0, fm.descent()), text().at(i));
|
||||
painter->setTransform(t);
|
||||
|
||||
int width = fm.charWidth(text(), i);
|
||||
int width = fm.horizontalAdvance(text().at(i));
|
||||
percent += ((qreal)width / (qreal)textWidth) * factor;
|
||||
}
|
||||
|
||||
//painter->setBrush(Qt::NoBrush);
|
||||
//painter->setPen(Qt::red);
|
||||
//painter->setRenderHint(QPainter::Antialiasing, false);
|
||||
//painter->drawPath(_shape);
|
||||
}
|
||||
|
@ -1,10 +1,7 @@
|
||||
#ifndef TEXTPATHITEM_H
|
||||
#define TEXTPATHITEM_H
|
||||
|
||||
#include <QFont>
|
||||
#include <QString>
|
||||
#include "textitem.h"
|
||||
#include "text.h"
|
||||
|
||||
class TextPathItem : public TextItem
|
||||
{
|
||||
|
@ -1,41 +1,22 @@
|
||||
#include <QPainter>
|
||||
#include <QtMath>
|
||||
#include <QStaticText>
|
||||
#include "textpointitem.h"
|
||||
|
||||
|
||||
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
|
||||
|
||||
QRectF TextPointItem::exactBoundingRect(const QString &str,
|
||||
const QFont &font, int maxWidth)
|
||||
QRectF TextPointItem::fuzzyBoundingRect() const
|
||||
{
|
||||
QFontMetrics fm(font);
|
||||
int limit = font.pixelSize() * maxWidth;
|
||||
// Italic fonts overflow the computed bounding rect, so reduce it
|
||||
// a little bit.
|
||||
if (font.italic())
|
||||
limit -= font.pixelSize() / 2.0;
|
||||
int fs = font().pixelSize();
|
||||
if (text().size() <= 3)
|
||||
return QRectF(0, 0, text().size() * fs, fs * 1.6);
|
||||
|
||||
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, str);
|
||||
Q_ASSERT(br.isValid());
|
||||
// Expand the bounding rect back to the real content size
|
||||
if (font.italic())
|
||||
br.adjust(-font.pixelSize() / 4.0, 0, font.pixelSize() / 4.0, 0);
|
||||
|
||||
return br;
|
||||
}
|
||||
|
||||
QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
|
||||
const QFont &font, int maxWidth)
|
||||
{
|
||||
int limit = font.pixelSize() * maxWidth;
|
||||
qreal cw = avgCharWidth(str, font);
|
||||
qreal lh = font.pixelSize() * 1.25;
|
||||
int limit = fs * _maxWidth;
|
||||
qreal cw = avgCharWidth();
|
||||
int width = 0, lines = 0;
|
||||
|
||||
if (font.italic())
|
||||
limit -= font.pixelSize() / 2.0;
|
||||
|
||||
QStringList l(str.split('\n'));
|
||||
QStringList l(text().split('\n'));
|
||||
for (int i = 0; i < l.size(); i++) {
|
||||
int lw = (int)(l.at(i).length() * cw);
|
||||
if (lw > limit) {
|
||||
@ -48,7 +29,7 @@ QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
|
||||
if (wl + pl < limit) {
|
||||
pl += wl + cw;
|
||||
} else {
|
||||
if (wl > limit) {
|
||||
if (wl >= limit) {
|
||||
if (pl > 0)
|
||||
lines++;
|
||||
} else
|
||||
@ -65,27 +46,26 @@ QRectF TextPointItem::fuzzyBoundingRect(const QString &str,
|
||||
}
|
||||
}
|
||||
|
||||
return QRectF(0, 0, width, lines * lh);
|
||||
return QRectF(0, 0, width, lines * fs * 1.6);
|
||||
}
|
||||
|
||||
|
||||
QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
|
||||
QRectF TextPointItem::moveTextRect(const QRectF &rect) const
|
||||
{
|
||||
QRectF iconRect = _icon.isNull() ? QRectF()
|
||||
: QRectF(QPointF(0, 0), QSizeF(_icon.size()) / _icon.devicePixelRatioF());
|
||||
QRectF textRect = brf(text(), font(), _maxWidth);
|
||||
QRectF textRect(rect);
|
||||
|
||||
switch (_anchor) {
|
||||
case Text::Center:
|
||||
textRect.moveCenter(_pos);
|
||||
break;
|
||||
case Text::Left:
|
||||
textRect.moveTopLeft(_pos - QPointF(-iconRect.width() / 2,
|
||||
textRect.height() / 2));
|
||||
textRect.moveTopLeft(_pos - QPointF(-iconRect.width() / 2
|
||||
- font().pixelSize()/4.0, textRect.height() / 2));
|
||||
break;
|
||||
case Text::Right:
|
||||
textRect.moveTopRight(_pos - QPointF(iconRect.width() / 2,
|
||||
textRect.height() / 2));
|
||||
textRect.moveTopRight(_pos - QPointF(iconRect.width() / 2
|
||||
+ font().pixelSize()/4.0, textRect.height() / 2));
|
||||
break;
|
||||
case Text::Bottom:
|
||||
textRect.moveTopLeft(_pos - QPointF(textRect.width() / 2,
|
||||
@ -102,11 +82,11 @@ QRectF TextPointItem::computeTextRect(BoundingRectFunction brf) const
|
||||
|
||||
TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
||||
const QFont &font, int maxWidth, Text::Anchor anchor, const QImage &icon)
|
||||
: TextItem(text), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
|
||||
_anchor(anchor)
|
||||
{
|
||||
setFont(font);
|
||||
_boundingRect = computeTextRect(fuzzyBoundingRect);
|
||||
_textRect = fuzzyBoundingRect();
|
||||
_boundingRect = moveTextRect(_textRect);
|
||||
|
||||
if (!_icon.isNull()) {
|
||||
QRectF iconRect(QPointF(0, 0), QSizeF(_icon.size())
|
||||
@ -118,22 +98,54 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
|
||||
_shape.addRect(_boundingRect);
|
||||
}
|
||||
|
||||
void TextPointItem::setPos(const QPointF &pos)
|
||||
{
|
||||
QPointF d(_boundingRect.left() - _pos.x(), _boundingRect.top() - _pos.y());
|
||||
_boundingRect.moveTopLeft(pos + d);
|
||||
_shape = QPainterPath();
|
||||
_shape.addRect(_boundingRect);
|
||||
_pos = pos;
|
||||
}
|
||||
|
||||
void TextPointItem::paint(QPainter *painter) const
|
||||
{
|
||||
//painter->setPen(Qt::red);
|
||||
//painter->drawRect(_boundingRect);
|
||||
|
||||
QRectF textRect;
|
||||
|
||||
painter->setFont(font());
|
||||
painter->setPen(pen());
|
||||
|
||||
if (!_icon.isNull()) {
|
||||
textRect = computeTextRect(exactBoundingRect);
|
||||
textRect = moveTextRect(painter->boundingRect(_textRect, FLAGS, text()));
|
||||
painter->drawImage(_pos - QPointF(_icon.width()
|
||||
/ _icon.devicePixelRatioF() / 2, _icon.height()
|
||||
/ _icon.devicePixelRatioF() / 2), _icon);
|
||||
} else
|
||||
textRect = computeTextRect(fuzzyBoundingRect);
|
||||
textRect = _boundingRect;
|
||||
|
||||
painter->setFont(font());
|
||||
painter->setPen(pen());
|
||||
painter->drawText(textRect, FLAGS, text());
|
||||
if (hasHalo()) {
|
||||
QStaticText st(text());
|
||||
st.setTextFormat(Qt::PlainText);
|
||||
st.setTextWidth(textRect.width());
|
||||
st.setTextOption(QTextOption(Qt::AlignHCenter));
|
||||
st.setPerformanceHint(QStaticText::AggressiveCaching);
|
||||
|
||||
painter->setPen(halo().color());
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(-1, -1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(+1, +1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(-1, +1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(+1, -1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(0, -1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(0, +1), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(-1, 0), st);
|
||||
painter->drawStaticText(textRect.topLeft() + QPointF(+1, 0), st);
|
||||
painter->setPen(pen());
|
||||
painter->drawStaticText(textRect.topLeft(), st);
|
||||
} else
|
||||
painter->drawText(textRect, FLAGS, text());
|
||||
|
||||
//painter->setBrush(Qt::NoBrush);
|
||||
//painter->setPen(Qt::red);
|
||||
//painter->drawRect(_boundingRect);
|
||||
//painter->setPen(Qt::blue);
|
||||
//painter->drawRect(textRect);
|
||||
}
|
||||
|
@ -1,11 +1,9 @@
|
||||
#ifndef TEXTPOINTITEM_H
|
||||
#define TEXTPOINTITEM_H
|
||||
|
||||
#include <QPen>
|
||||
#include <QFont>
|
||||
#include <QString>
|
||||
#include "textitem.h"
|
||||
#include <QImage>
|
||||
#include "text.h"
|
||||
#include "textitem.h"
|
||||
|
||||
class TextPointItem : public TextItem
|
||||
{
|
||||
@ -17,19 +15,17 @@ public:
|
||||
QPainterPath shape() const {return _shape;}
|
||||
void paint(QPainter *painter) const;
|
||||
|
||||
void setPos(const QPointF &pos);
|
||||
|
||||
private:
|
||||
typedef QRectF (*BoundingRectFunction)(const QString &, const QFont &, int);
|
||||
|
||||
static QRectF exactBoundingRect(const QString &str, const QFont &font,
|
||||
int maxWidth);
|
||||
static QRectF fuzzyBoundingRect(const QString &str, const QFont &font,
|
||||
int maxWidth);
|
||||
|
||||
QRectF computeTextRect(BoundingRectFunction brf) const;
|
||||
QRectF fuzzyBoundingRect() const;
|
||||
QRectF moveTextRect(const QRectF &rect) const;
|
||||
bool hasHalo() const
|
||||
{return halo().color().isValid() && halo().width() > 0;}
|
||||
|
||||
QPointF _pos;
|
||||
QPainterPath _shape;
|
||||
QRectF _boundingRect;
|
||||
QRectF _textRect, _boundingRect;
|
||||
QImage _icon;
|
||||
int _maxWidth;
|
||||
Text::Anchor _anchor;
|
||||
|
@ -11,7 +11,10 @@ public:
|
||||
: _zoom(zoom), _size(img->size()), _scale(scale),
|
||||
_text(QSize(img->size().width() / scale.x(),
|
||||
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;}
|
||||
const QSize &size() const {return _size;}
|
||||
|
@ -2265,7 +2265,6 @@
|
||||
"source": "openmaptiles",
|
||||
"source-layer": "building",
|
||||
"minzoom": 13,
|
||||
"maxzoom": 14,
|
||||
"layout": {
|
||||
"visibility": "visible"
|
||||
},
|
||||
@ -4059,4 +4058,4 @@
|
||||
}
|
||||
],
|
||||
"id": "osm-liberty"
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user