32 Commits
1.4 ... 2.2

Author SHA1 Message Date
032c699e8b Code cleanup 2020-04-15 10:06:57 +02:00
a298cfc47d Only accept PBF gziped files in canRead() 2020-04-15 09:47:43 +02:00
402e1e3adc Consistently use mvt as the format 2020-04-13 19:47:41 +02:00
254e93e583 Added Tilezen examples 2020-04-11 11:49:37 +02:00
95d4fee453 Added examples data/style sources 2020-04-11 11:27:17 +02:00
a6547d27d3 Added style compatibility info 2020-04-10 13:13:20 +02:00
3e065f87fa Updated screenshots 2020-04-09 23:42:02 +02:00
61291bb202 Updated styles info 2020-04-09 22:47:21 +02:00
154114dd2a Added missing brew update to travis builds 2020-02-08 13:04:36 +01:00
c0a7d64a1d Silenced all the deprecated warnings 2020-01-31 22:20:57 +01:00
d8df5cb595 Added style expressions not supported info 2019-10-22 09:14:06 +02:00
543bf63017 Merge branch 'master' of https://github.com/tumic0/QtPBFImagePlugin 2019-10-06 13:27:10 +02:00
7ea19d260c Fixed broken interpolation of grayscale colors 2019-10-06 13:26:30 +02:00
0185cd904f Added change log link 2019-10-05 22:06:37 +02:00
ec6302d524 Updated scaling sample to fit v2.0 2019-10-05 18:49:34 +02:00
0916d6330c Added tile size info 2019-10-05 18:47:39 +02:00
cc0c16cfd6 Explicitly set the text format (optimization) 2019-10-05 18:38:24 +02:00
8da172c0ff Adjusted icon text alignment 2019-10-05 13:21:36 +02:00
148f79eaef Some more bounding rect computation tweaking 2019-10-04 20:23:34 +02:00
1c42f104d1 Merge branch 'master' of https://github.com/tumic0/QtPBFImagePlugin 2019-10-04 18:57:19 +02:00
536cad2d61 Use QStaticText instead of own text caching 2019-10-04 18:56:45 +02:00
ff1c29e078 Use QStaticText instead of own text caching 2019-10-04 18:54:59 +02:00
e8e9139740 Some more text layout tweeking 2019-10-04 09:26:15 +02:00
a58962ab93 Yet another text rendering improvement/optimization 2019-10-03 22:39:05 +02:00
aacc42c25d Fixed broken down-scaling 2019-10-03 08:38:48 +02:00
3f6b5faa2c Switched to 512x512px tile size 2019-10-03 01:01:40 +02:00
5b6a222999 Added correct maxzoom default value 2019-10-02 23:19:49 +02:00
e00c8feab6 Use the "official" min/max zoom defaults in zoom check 2019-10-02 23:14:20 +02:00
82308857ba Added missing include 2019-10-02 23:12:53 +02:00
044e95e061 Some more text layout fiddeling 2019-10-02 22:57:11 +02:00
f5c15ece52 Improved/optimized text rendering 2019-10-01 22:11:45 +02:00
df4134c03a Alittle bit more sane API... 2019-05-04 12:50:16 +02:00
15 changed files with 252 additions and 170 deletions

View File

@ -10,6 +10,7 @@ env:
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

View File

@ -8,15 +8,11 @@ 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.
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
@ -33,10 +29,22 @@ 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.setScaledSize(QSize(1024, 1024));
reader.read(&image);
```
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).
## 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) must be used.*
For a list of "ready to use" styles see the
[QtPBFImagePlugin-styles](https://github.com/tumic0/QtPBFImagePlugin-styles)
repository.
## Build
### Requirements
@ -72,22 +80,45 @@ for most common distros available on OBS.
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
![osm-liberty 2](https://tumic0.github.io/QtPBFImagePlugin/images/osm-liberty-2.png)
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)
![osm-liberty 5](https://tumic0.github.io/QtPBFImagePlugin/images/osm-liberty-5.png)
![osm-liberty 8](https://tumic0.github.io/QtPBFImagePlugin/images/osm-liberty-8.png)
![osm-liberty 11](https://tumic0.github.io/QtPBFImagePlugin/images/osm-liberty-11.png)
![osm-liberty 12](https://tumic0.github.io/QtPBFImagePlugin/images/osm-liberty-12.png)
![osm-liberty 14](https://tumic0.github.io/QtPBFImagePlugin/images/osm-liberty-14.png)
![osm-liberty 15](https://tumic0.github.io/QtPBFImagePlugin/images/osm-liberty-15.png)
#### Klokantech-basic
![klokantech-basic 2](https://tumic0.github.io/QtPBFImagePlugin/images/klokantech-basic-2.png)
![klokantech-basic 4](https://tumic0.github.io/QtPBFImagePlugin/images/klokantech-basic-4.png)
![klokantech-basic 8](https://tumic0.github.io/QtPBFImagePlugin/images/klokantech-basic-8.png)
![klokantech-basic 13](https://tumic0.github.io/QtPBFImagePlugin/images/klokantech-basic-13.png)
![klokantech-basic 14](https://tumic0.github.io/QtPBFImagePlugin/images/klokantech-basic-14.png)
#### 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)
![bright 4](https://tumic0.github.io/QtPBFImagePlugin/images/bright-4.png)
![bright 6](https://tumic0.github.io/QtPBFImagePlugin/images/bright-6.png)
![bright 13](https://tumic0.github.io/QtPBFImagePlugin/images/bright-13.png)
![bright 15](https://tumic0.github.io/QtPBFImagePlugin/images/bright-15.png)
![bright 17](https://tumic0.github.io/QtPBFImagePlugin/images/bright-17.png)
#### 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)
![apollo-bright 4](https://tumic0.github.io/QtPBFImagePlugin/images/apollo-bright-4.png)
![apollo-bright 6](https://tumic0.github.io/QtPBFImagePlugin/images/apollo-bright-6.png)
![apollo-bright 12](https://tumic0.github.io/QtPBFImagePlugin/images/apollo-bright-12.png)
![apollo-bright 15](https://tumic0.github.io/QtPBFImagePlugin/images/apollo-bright-15.png)
![apollo-bright 16](https://tumic0.github.io/QtPBFImagePlugin/images/apollo-bright-16.png)
## Applications using QtPBFImagePlugin
* [GPXSee](https://www.gpxsee.org)

View File

@ -37,6 +37,8 @@ SOURCES += src/pbfplugin.cpp \
src/textitem.cpp
RESOURCES += pbfplugin.qrc
DEFINES += QT_NO_DEPRECATED_WARNINGS
unix:!macx{
LIBS += -lprotobuf-lite \
-lz

View File

@ -40,7 +40,9 @@ static QColor interpolate(const QPair<qreal, QColor> &p0,
qreal p1h, p1s, p1l, p1a;
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));
}

View File

@ -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 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;
if (inflate(&stream, Z_NO_FLUSH) != Z_STREAM_END) {
qCritical() << "Invalid gzip data";
uba = QByteArray();
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();
}
inflateEnd(&stream);
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);
(void)inflateEnd(&strm);
return (ret == Z_STREAM_END) ? uba : QByteArray();
}

View File

@ -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

View File

@ -8,24 +8,24 @@
#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);
}
@ -34,7 +34,7 @@ static bool isPlainPBF(quint32 magic)
bool PBFHandler::canRead() const
{
if (canRead(device())) {
setFormat("pbf");
setFormat("mvt");
return true;
} else
return false;
@ -42,27 +42,37 @@ 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))
ba = device()->readAll();
if (ba.isNull())
if (isGZIPPBF(magic)) {
ba = Gzip::uncompress(device());
if (ba.isNull()) {
qCritical() << "Invalid gzip data";
return false;
}
} else if (isPlainPBF(magic))
ba = device()->readAll();
vector_tile::Tile data;
if (!data.ParseFromArray(ba.constData(), ba.size())) {
qCritical() << "Invalid PBF data";
@ -75,8 +85,8 @@ bool PBFHandler::read(QImage *image)
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);
? QPointF((qreal)_scaledSize.width() / TILE_SIZE,
(qreal)_scaledSize.height() / TILE_SIZE) : QPointF(1.0, 1.0);
*image = QImage(size, QImage::Format_ARGB32_Premultiplied);
Tile tile(image, ok ? zoom : -1, scale);

View File

@ -172,7 +172,7 @@ bool Style::Layer::Filter::match(const PBF::Feature &feature) const
return true;
return false;
case GeometryType:
return feature.type() == _kv.second.toUInt();
return feature.type() == _kv.second.toInt();
default:
return false;
}
@ -437,7 +437,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());
@ -475,12 +475,8 @@ Style::Layer::Layer(const QJsonObject &json)
bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
{
if (zoom >= 0) {
if (_minZoom > 0 && zoom < _minZoom)
if (zoom >= 0 && (zoom < _minZoom || zoom > _maxZoom))
return false;
if (_maxZoom > 0 && zoom > _maxZoom)
return false;
}
return _filter.match(feature);
}
@ -601,7 +597,8 @@ void Style::setupLayer(Tile &tile, const Layer &layer) const
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);

View File

@ -35,7 +35,7 @@ public:
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;}

View File

@ -1,6 +1,7 @@
#include <QFontMetrics>
#include <QPainter>
#include <QSet>
#include <QMap>
#include "text.h"
#include "textpointitem.h"
#include "textpathitem.h"
@ -31,12 +32,26 @@ void Text::addLabel(const QString &text, const QImage &icon,
{
TextItem *ti;
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:
if (_alignment == Viewport)
ti = new TextPointItem(text, path.elementAt(0), _font,
_maxWidth, _anchor, icon);
else
ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
break;
case LineCenter:
@ -44,9 +59,9 @@ void Text::addLabel(const QString &text, const QImage &icon,
_maxWidth, _anchor, icon);
break;
default:
ti = new TextPointItem(text, path.elementAt(0), _font, _maxWidth,
_anchor, icon);
break;
ti = new TextPointItem(text, path.elementAt(0), _font,
_maxWidth, _anchor, icon);
}
}
// Note: empty path == point geometry (single move)

View File

@ -11,7 +11,7 @@ bool TextItem::collidesWithItem(const TextItem *other) const
return other->shape().intersects(shape());
}
int TextItem::avgCharWidth() const
qreal TextItem::avgCharWidth() const
{
qreal ratio;
ushort cp = _text.at(0).unicode();
@ -21,14 +21,18 @@ int TextItem::avgCharWidth() const
ratio = 1.0;
// Greek & Cyrilic
else if (cp >= 0x03FF && cp <= 0x04FF) {
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.67;
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.80 : 0.73;
if (_font.bold())
ratio *= 1.1;
if (_font.italic())
ratio *= 0.9;
// The rest (Latin scripts, Arabic, ...)
} else {
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.7 : 0.58;
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.63;
if (_font.bold())
ratio *= 1.1;
if (_font.italic())
ratio *= 0.9;
}
return ratio * _font.pixelSize();

View File

@ -34,7 +34,7 @@ public:
bool collidesWithItem(const TextItem *other) const;
protected:
int avgCharWidth() const;
qreal avgCharWidth() const;
private:
QString _text;

View File

@ -3,23 +3,23 @@
#include "textpathitem.h"
static QPointF intersection(const QLineF &line, const QRectF &rect)
static bool intersection(const QLineF &line, const QRectF &rect,
QPointF *p)
{
QPointF p;
if (line.intersect(QLineF(rect.topLeft(), rect.topRight()), &p)
if (line.intersect(QLineF(rect.topLeft(), rect.topRight()), p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.topLeft(), rect.bottomLeft()), &p)
return true;
if (line.intersect(QLineF(rect.topLeft(), rect.bottomLeft()), p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.bottomRight(), rect.bottomLeft()), &p)
return true;
if (line.intersect(QLineF(rect.bottomRight(), rect.bottomLeft()), p)
== QLineF::BoundedIntersection)
return p;
if (line.intersect(QLineF(rect.bottomRight(), rect.topRight()), &p)
return true;
if (line.intersect(QLineF(rect.bottomRight(), rect.topRight()), p)
== QLineF::BoundedIntersection)
return p;
return true;
return rect.center();
return false;
}
static QPainterPath subpath(const QList<QLineF> &lines, int start, int end,
@ -63,6 +63,7 @@ static QList<QLineF> lineString(const QPainterPath &path,
{
QList<QLineF> lines;
int start = 0, end = path.elementCount() - 1;
QPointF p;
for (int i = 0; i < path.elementCount(); i++) {
if (boundingRect.contains(path.elementAt(i))) {
@ -79,16 +80,14 @@ static QList<QLineF> lineString(const QPainterPath &path,
if (start > 0) {
QLineF l(path.elementAt(start-1), path.elementAt(start));
QPointF p(intersection(l, boundingRect));
if (p != boundingRect.center())
if (intersection(l, boundingRect, &p))
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())
if (intersection(l, boundingRect, &p))
lines.append(QLineF(path.elementAt(end), p));
}
@ -136,7 +135,7 @@ TextPathItem::TextPathItem(const QString &text, const QPainterPath &path,
const QFont &font, int maxAngle, const QRectF &tileRect)
: TextItem(text, font)
{
int cw = avgCharWidth();
qreal cw = avgCharWidth();
int textWidth = text.size() * cw;
if (textWidth > path.length())
return;

View File

@ -1,34 +1,20 @@
#include <QPainter>
#include <QtMath>
#include <QStaticText>
#include "config.h"
#include "textpointitem.h"
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip)
QRectF TextPointItem::exactBoundingRect() 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;
QRect br = fm.boundingRect(QRect(0, 0, limit, 0), FLAGS, text());
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
{
int limit = font().pixelSize() * _maxWidth;
int fs = font().pixelSize();
if (text().size() <= 3)
return QRectF(0, 0, text().size() * fs, fs * 1.6);
int limit = fs * _maxWidth;
qreal cw = avgCharWidth();
qreal lh = font().pixelSize() * 1.25;
int width = 0, lines = 0;
QStringList l(text().split('\n'));
@ -44,7 +30,7 @@ QRectF TextPointItem::fuzzyBoundingRect() const
if (wl + pl < limit) {
pl += wl + cw;
} else {
if (wl > limit) {
if (wl >= limit) {
if (pl > 0)
lines++;
} else
@ -61,11 +47,11 @@ QRectF TextPointItem::fuzzyBoundingRect() const
}
}
return QRectF(0, 0, width, lines * lh);
return QRectF(0, 0, width, lines * fs * 1.6);
}
QRectF TextPointItem::computeTextRect(bool exact) const
QRectF TextPointItem::moveTextRect(const QRectF &rect) const
{
#ifdef ENABLE_HIDPI
QRectF iconRect = _icon.isNull() ? QRectF()
@ -74,19 +60,19 @@ QRectF TextPointItem::computeTextRect(bool exact) const
QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0),
QSizeF(_icon.size()));
#endif // ENABLE_HIDPI
QRectF textRect = exact ? exactBoundingRect() : fuzzyBoundingRect();
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,
@ -106,7 +92,8 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
: TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
_anchor(anchor)
{
_boundingRect = computeTextRect(false);
_textRect = fuzzyBoundingRect();
_boundingRect = moveTextRect(_textRect);
if (!_icon.isNull()) {
#ifdef ENABLE_HIDPI
@ -122,17 +109,24 @@ 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;
bool hasHalo = halo().color().isValid() && halo().width() > 0;
painter->setFont(font());
painter->setPen(pen());
if (!_icon.isNull()) {
textRect = (_anchor != Text::Center || hasHalo)
? computeTextRect(true) : _boundingRect;
textRect = moveTextRect(painter->boundingRect(_textRect, FLAGS, text()));
#ifdef ENABLE_HIDPI
painter->drawImage(_pos - QPointF(_icon.width()
/ _icon.devicePixelRatioF() / 2, _icon.height()
@ -142,33 +136,32 @@ void TextPointItem::paint(QPainter *painter) const
_icon.height() / 2), _icon);
#endif // ENABLE_HIDPI
} else
textRect = hasHalo ? computeTextRect(true) : _boundingRect;
textRect = _boundingRect;
if (hasHalo()) {
QStaticText st(text());
st.setTextFormat(Qt::PlainText);
st.setTextWidth(textRect.width());
st.setTextOption(QTextOption(Qt::AlignHCenter));
st.setPerformanceHint(QStaticText::AggressiveCaching);
if (hasHalo) {
QRect ir(textRect.toRect());
QImage img(ir.size(), QImage::Format_ARGB32_Premultiplied);
img.fill(Qt::transparent);
QPainter ip(&img);
ip.setPen(halo().color());
ip.setFont(font());
ip.drawText(img.rect(), FLAGS, text());
painter->drawImage(ir.x() - 1, ir.y() - 1, img);
painter->drawImage(ir.x() + 1, ir.y() + 1, img);
painter->drawImage(ir.x() - 1, ir.y() + 1, img);
painter->drawImage(ir.x() + 1, ir.y() - 1, img);
painter->drawImage(ir.x(), ir.y() - 1, img);
painter->drawImage(ir.x(), ir.y() + 1, img);
painter->drawImage(ir.x() - 1, ir.y(), img);
painter->drawImage(ir.x() + 1, ir.y(), img);
painter->setFont(font());
painter->setPen(pen());
painter->drawText(ir, FLAGS, text());
} else {
painter->setFont(font());
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);
}

View File

@ -15,14 +15,17 @@ public:
QPainterPath shape() const {return _shape;}
void paint(QPainter *painter) const;
void setPos(const QPointF &pos);
private:
QRectF exactBoundingRect() const;
QRectF fuzzyBoundingRect() const;
QRectF computeTextRect(bool exact) 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;