16 Commits
1.4 ... 2.0

9 changed files with 120 additions and 109 deletions

View File

@ -8,7 +8,7 @@
#include "pbfhandler.h" #include "pbfhandler.h"
#define TILE_SIZE 256 #define TILE_SIZE 512
#define GZIP_MAGIC 0x1F8B0800 #define GZIP_MAGIC 0x1F8B0800
#define GZIP_MAGIC_MASK 0xFFFFFF00 #define GZIP_MAGIC_MASK 0xFFFFFF00
@ -75,8 +75,8 @@ bool PBFHandler::read(QImage *image)
QSize size = _scaledSize.isValid() QSize size = _scaledSize.isValid()
? _scaledSize : QSize(TILE_SIZE, TILE_SIZE); ? _scaledSize : QSize(TILE_SIZE, TILE_SIZE);
QPointF scale = _scaledSize.isValid() QPointF scale = _scaledSize.isValid()
? QPointF(_scaledSize.width() / TILE_SIZE, _scaledSize.height() / TILE_SIZE) ? QPointF((qreal)_scaledSize.width() / TILE_SIZE,
: QPointF(1.0, 1.0); (qreal)_scaledSize.height() / TILE_SIZE) : QPointF(1.0, 1.0);
*image = QImage(size, QImage::Format_ARGB32_Premultiplied); *image = QImage(size, QImage::Format_ARGB32_Premultiplied);
Tile tile(image, ok ? zoom : -1, scale); Tile tile(image, ok ? zoom : -1, scale);

View File

@ -437,7 +437,7 @@ Text::RotationAlignment Style::Layer::Layout::textRotationAlignment(int zoom)
} }
Style::Layer::Layer(const QJsonObject &json) Style::Layer::Layer(const QJsonObject &json)
: _type(Unknown), _minZoom(-1), _maxZoom(-1) : _type(Unknown), _minZoom(0), _maxZoom(24)
{ {
// type // type
QString type(json["type"].toString()); 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 bool Style::Layer::match(int zoom, const PBF::Feature &feature) const
{ {
if (zoom >= 0) { if (zoom >= 0 && (zoom < _minZoom || zoom > _maxZoom))
if (_minZoom > 0 && zoom < _minZoom)
return false; return false;
if (_maxZoom > 0 && zoom > _maxZoom)
return false;
}
return _filter.match(feature); return _filter.match(feature);
} }
@ -601,7 +597,8 @@ void Style::setupLayer(Tile &tile, const Layer &layer) const
void Style::drawBackground(Tile &tile) 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; QPainterPath path;
path.addRect(rect); path.addRect(rect);

View File

@ -35,7 +35,7 @@ public:
private: private:
class Layer { class Layer {
public: public:
Layer() : _type(Unknown), _minZoom(-1), _maxZoom(-1) {} Layer() : _type(Unknown), _minZoom(0), _maxZoom(24) {}
Layer(const QJsonObject &json); Layer(const QJsonObject &json);
const QString &sourceLayer() const {return _sourceLayer;} const QString &sourceLayer() const {return _sourceLayer;}

View File

@ -1,6 +1,7 @@
#include <QFontMetrics> #include <QFontMetrics>
#include <QPainter> #include <QPainter>
#include <QSet> #include <QSet>
#include <QMap>
#include "text.h" #include "text.h"
#include "textpointitem.h" #include "textpointitem.h"
#include "textpathitem.h" #include "textpathitem.h"
@ -31,12 +32,26 @@ void Text::addLabel(const QString &text, const QImage &icon,
{ {
TextItem *ti; 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) { switch (_placement) {
case Line: 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); ti = new TextPathItem(text, path, _font, _maxAngle, _sceneRect);
break; break;
case LineCenter: case LineCenter:
@ -44,9 +59,9 @@ void Text::addLabel(const QString &text, const QImage &icon,
_maxWidth, _anchor, icon); _maxWidth, _anchor, icon);
break; break;
default: default:
ti = new TextPointItem(text, path.elementAt(0), _font, _maxWidth, ti = new TextPointItem(text, path.elementAt(0), _font,
_anchor, icon); _maxWidth, _anchor, icon);
break; }
} }
// Note: empty path == point geometry (single move) // 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()); return other->shape().intersects(shape());
} }
int TextItem::avgCharWidth() const qreal TextItem::avgCharWidth() const
{ {
qreal ratio; qreal ratio;
ushort cp = _text.at(0).unicode(); ushort cp = _text.at(0).unicode();
@ -21,14 +21,18 @@ int TextItem::avgCharWidth() const
ratio = 1.0; ratio = 1.0;
// Greek & Cyrilic // Greek & Cyrilic
else if (cp >= 0x03FF && cp <= 0x04FF) { else if (cp >= 0x03FF && cp <= 0x04FF) {
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.67; ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.80 : 0.73;
if (_font.bold()) if (_font.bold())
ratio *= 1.1; ratio *= 1.1;
if (_font.italic())
ratio *= 0.9;
// The rest (Latin scripts, Arabic, ...) // The rest (Latin scripts, Arabic, ...)
} else { } else {
ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.7 : 0.58; ratio = (_font.capitalization() == QFont::AllUppercase) ? 0.75 : 0.63;
if (_font.bold()) if (_font.bold())
ratio *= 1.1; ratio *= 1.1;
if (_font.italic())
ratio *= 0.9;
} }
return ratio * _font.pixelSize(); return ratio * _font.pixelSize();

View File

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

View File

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

View File

@ -1,34 +1,20 @@
#include <QPainter> #include <QPainter>
#include <QtMath> #include <QtMath>
#include <QStaticText>
#include "config.h" #include "config.h"
#include "textpointitem.h" #include "textpointitem.h"
#define FLAGS (Qt::AlignCenter | Qt::TextWordWrap | Qt::TextDontClip) #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 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 cw = avgCharWidth();
qreal lh = font().pixelSize() * 1.25;
int width = 0, lines = 0; int width = 0, lines = 0;
QStringList l(text().split('\n')); QStringList l(text().split('\n'));
@ -44,7 +30,7 @@ QRectF TextPointItem::fuzzyBoundingRect() const
if (wl + pl < limit) { if (wl + pl < limit) {
pl += wl + cw; pl += wl + cw;
} else { } else {
if (wl > limit) { if (wl >= limit) {
if (pl > 0) if (pl > 0)
lines++; lines++;
} else } 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 #ifdef ENABLE_HIDPI
QRectF iconRect = _icon.isNull() ? QRectF() QRectF iconRect = _icon.isNull() ? QRectF()
@ -74,19 +60,19 @@ QRectF TextPointItem::computeTextRect(bool exact) const
QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0), QRectF iconRect = _icon.isNull() ? QRectF() : QRectF(QPointF(0, 0),
QSizeF(_icon.size())); QSizeF(_icon.size()));
#endif // ENABLE_HIDPI #endif // ENABLE_HIDPI
QRectF textRect = exact ? exactBoundingRect() : fuzzyBoundingRect(); QRectF textRect(rect);
switch (_anchor) { switch (_anchor) {
case Text::Center: case Text::Center:
textRect.moveCenter(_pos); textRect.moveCenter(_pos);
break; break;
case Text::Left: case Text::Left:
textRect.moveTopLeft(_pos - QPointF(-iconRect.width() / 2, textRect.moveTopLeft(_pos - QPointF(-iconRect.width() / 2
textRect.height() / 2)); - font().pixelSize()/4.0, textRect.height() / 2));
break; break;
case Text::Right: case Text::Right:
textRect.moveTopRight(_pos - QPointF(iconRect.width() / 2, textRect.moveTopRight(_pos - QPointF(iconRect.width() / 2
textRect.height() / 2)); + font().pixelSize()/4.0, textRect.height() / 2));
break; break;
case Text::Bottom: case Text::Bottom:
textRect.moveTopLeft(_pos - QPointF(textRect.width() / 2, 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), : TextItem(text, font), _pos(pos), _icon(icon), _maxWidth(maxWidth),
_anchor(anchor) _anchor(anchor)
{ {
_boundingRect = computeTextRect(false); _textRect = fuzzyBoundingRect();
_boundingRect = moveTextRect(_textRect);
if (!_icon.isNull()) { if (!_icon.isNull()) {
#ifdef ENABLE_HIDPI #ifdef ENABLE_HIDPI
@ -122,17 +109,24 @@ TextPointItem::TextPointItem(const QString &text, const QPointF &pos,
_shape.addRect(_boundingRect); _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 void TextPointItem::paint(QPainter *painter) const
{ {
//painter->setPen(Qt::red);
//painter->drawRect(_boundingRect);
QRectF textRect; QRectF textRect;
bool hasHalo = halo().color().isValid() && halo().width() > 0;
painter->setFont(font());
painter->setPen(pen());
if (!_icon.isNull()) { if (!_icon.isNull()) {
textRect = (_anchor != Text::Center || hasHalo) textRect = moveTextRect(painter->boundingRect(_textRect, FLAGS, text()));
? computeTextRect(true) : _boundingRect;
#ifdef ENABLE_HIDPI #ifdef ENABLE_HIDPI
painter->drawImage(_pos - QPointF(_icon.width() painter->drawImage(_pos - QPointF(_icon.width()
/ _icon.devicePixelRatioF() / 2, _icon.height() / _icon.devicePixelRatioF() / 2, _icon.height()
@ -142,33 +136,32 @@ void TextPointItem::paint(QPainter *painter) const
_icon.height() / 2), _icon); _icon.height() / 2), _icon);
#endif // ENABLE_HIDPI #endif // ENABLE_HIDPI
} else } 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) { painter->setPen(halo().color());
QRect ir(textRect.toRect()); painter->drawStaticText(textRect.topLeft() + QPointF(-1, -1), st);
QImage img(ir.size(), QImage::Format_ARGB32_Premultiplied); painter->drawStaticText(textRect.topLeft() + QPointF(+1, +1), st);
img.fill(Qt::transparent); painter->drawStaticText(textRect.topLeft() + QPointF(-1, +1), st);
QPainter ip(&img); painter->drawStaticText(textRect.topLeft() + QPointF(+1, -1), st);
ip.setPen(halo().color()); painter->drawStaticText(textRect.topLeft() + QPointF(0, -1), st);
ip.setFont(font()); painter->drawStaticText(textRect.topLeft() + QPointF(0, +1), st);
ip.drawText(img.rect(), FLAGS, text()); painter->drawStaticText(textRect.topLeft() + QPointF(-1, 0), st);
painter->drawStaticText(textRect.topLeft() + QPointF(+1, 0), st);
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(pen()); painter->setPen(pen());
painter->drawStaticText(textRect.topLeft(), st);
} else
painter->drawText(textRect, FLAGS, text()); 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;} QPainterPath shape() const {return _shape;}
void paint(QPainter *painter) const; void paint(QPainter *painter) const;
void setPos(const QPointF &pos);
private: private:
QRectF exactBoundingRect() const;
QRectF fuzzyBoundingRect() 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; QPointF _pos;
QPainterPath _shape; QPainterPath _shape;
QRectF _boundingRect; QRectF _textRect, _boundingRect;
QImage _icon; QImage _icon;
int _maxWidth; int _maxWidth;
Text::Anchor _anchor; Text::Anchor _anchor;