Properly scale the whole tile content

This commit is contained in:
Martin Tůma 2018-11-17 21:54:51 +01:00
parent 14adb0925c
commit 1f9e044493
8 changed files with 25 additions and 29 deletions

View File

@ -155,21 +155,21 @@ static void drawFeature(const Feature &feature, Style *style, int styleLayer,
} }
static void drawLayer(const Layer &layer, Style *style, int styleLayer, static void drawLayer(const Layer &layer, Style *style, int styleLayer,
Tile &tile) Tile &tile, const QPointF &scale)
{ {
if (layer.data()->version() > 2) if (layer.data()->version() > 2)
return; return;
QSizeF factor(tile.size().width() / (qreal)layer.data()->extent(), QSizeF factor(tile.size().width() / scale.x() / (qreal)layer.data()->extent(),
tile.size().height() / (qreal)layer.data()->extent()); tile.size().height() / scale.y() / (qreal)layer.data()->extent());
style->setPainter(styleLayer, tile); style->setPainter(styleLayer, tile);
for (int i = 0; i < layer.features().size(); i++) for (int i = 0; i < layer.features().size(); i++)
drawFeature(layer.features().at(i), style, styleLayer, factor, tile); drawFeature(layer.features().at(i), style, styleLayer, factor, tile);
} }
bool PBF::render(const QByteArray &data, int zoom, Style *style, qreal scale, bool PBF::render(const QByteArray &data, int zoom, Style *style,
QImage *image) const QPointF &scale, QImage *image)
{ {
vector_tile::Tile tile; vector_tile::Tile tile;
if (!tile.ParseFromArray(data.constData(), data.size())) { if (!tile.ParseFromArray(data.constData(), data.size())) {
@ -200,7 +200,7 @@ bool PBF::render(const QByteArray &data, int zoom, Style *style, qreal scale,
if (it == layers.constEnd()) if (it == layers.constEnd())
continue; continue;
drawLayer(*it, style, i, t); drawLayer(*it, style, i, t, scale);
} }
t.painter().restore(); t.painter().restore();

View File

@ -8,8 +8,8 @@ class Style;
namespace PBF namespace PBF
{ {
bool render(const QByteArray &data, int zoom, Style *style, qreal scale, bool render(const QByteArray &data, int zoom, Style *style,
QImage *render); const QPointF &scale, QImage *render);
} }
#endif // PBF_H #endif // PBF_H

View File

@ -67,9 +67,9 @@ 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);
qreal scale = _scaledSize.isValid() QPointF scale = _scaledSize.isValid()
? qMax(_scaledSize.width() / TILE_SIZE, _scaledSize.height() / TILE_SIZE) ? QPointF(_scaledSize.width() / TILE_SIZE, _scaledSize.height() / TILE_SIZE)
: 1.0; : QPointF(1.0, 1.0);
*image = QImage(size, QImage::Format_ARGB32_Premultiplied); *image = QImage(size, QImage::Format_ARGB32_Premultiplied);
return PBF::render(ba, ok ? zoom : -1, _style, scale, image); return PBF::render(ba, ok ? zoom : -1, _style, scale, image);

View File

@ -490,4 +490,6 @@ void Style::drawBackground(Tile &tile)
_styles.first().setPathPainter(_zoom, tile); _styles.first().setPathPainter(_zoom, tile);
tile.painter().drawPath(path); tile.painter().drawPath(path);
} }
tile.painter().setPen(Qt::red);
tile.painter().drawRect(rect);
} }

View File

@ -157,11 +157,6 @@ void Text::addLabel(const QString &text, const QPointF &pos,
TextPointItem *ti; TextPointItem *ti;
if (_fontScale != 1.0) {
QFont scaledFont(painter.font());
scaledFont.setPixelSize(painter.font().pixelSize() * _fontScale);
ti = new TextPointItem(text, pos, scaledFont, maxTextWidth);
} else
ti = new TextPointItem(text, pos, painter.font(), maxTextWidth); ti = new TextPointItem(text, pos, painter.font(), maxTextWidth);
ti->setPen(painter.pen()); ti->setPen(painter.pen());
@ -179,20 +174,17 @@ void Text::addLabel(const QString &text, const QPainterPath &path,
if (text.isEmpty()) if (text.isEmpty())
return; return;
QFont scaledFont(painter.font()); int textWidth = text.size() * painter.font().pixelSize() * 0.6;
scaledFont.setPixelSize(painter.font().pixelSize() * _fontScale);
int textWidth = text.size() * scaledFont.pixelSize() * 0.6;
if (textWidth > path.length()) if (textWidth > path.length())
return; return;
QPainterPath tp(textPath(path, textWidth, maxAngle, QPainterPath tp(textPath(path, textWidth, maxAngle,
scaledFont.pixelSize() / 2, _sceneRect)); painter.font().pixelSize() / 2, _sceneRect));
if (tp.isEmpty()) if (tp.isEmpty())
return; return;
TextPathItem *pi = new TextPathItem(text, reverse(tp) ? tp.toReversed() TextPathItem *pi = new TextPathItem(text, reverse(tp) ? tp.toReversed()
: tp, scaledFont); : tp, painter.font());
if (!_sceneRect.contains(pi->boundingRect())) { if (!_sceneRect.contains(pi->boundingRect())) {
delete pi; delete pi;
return; return;

View File

@ -6,8 +6,7 @@
class Text class Text
{ {
public: public:
Text(const QSize &size, qreal scale) Text(const QSize &size) : _sceneRect(QRectF(QPointF(0, 0), size)) {}
: _sceneRect(QRectF(QPointF(0, 0), size)), _fontScale(scale) {}
~Text(); ~Text();
void render(QPainter *painter) const; void render(QPainter *painter) const;
@ -22,7 +21,6 @@ private:
QList<TextItem *> collidingItems(const TextItem *item) const; QList<TextItem *> collidingItems(const TextItem *item) const;
QRectF _sceneRect; QRectF _sceneRect;
qreal _fontScale;
QList<TextItem *> _items; QList<TextItem *> _items;
}; };

View File

@ -24,6 +24,8 @@ void TextPathItem::paint(QPainter *painter) const
painter->setFont(_font); painter->setFont(_font);
painter->setPen(_pen); painter->setPen(_pen);
QTransform t = painter->transform();
for (int i = 0; i < _text.size(); i++) { for (int i = 0; i < _text.size(); i++) {
QPointF point = _path.pointAtPercent(percent); QPointF point = _path.pointAtPercent(percent);
qreal angle = _path.angleAtPercent(percent); qreal angle = _path.angleAtPercent(percent);
@ -31,7 +33,7 @@ void TextPathItem::paint(QPainter *painter) const
painter->translate(point); painter->translate(point);
painter->rotate(-angle); painter->rotate(-angle);
painter->drawText(QPoint(0, fm.descent()), _text.at(i)); painter->drawText(QPoint(0, fm.descent()), _text.at(i));
painter->resetTransform(); painter->setTransform(t);
int width = fm.charWidth(_text, i); int width = fm.charWidth(_text, i);
percent += ((qreal)width / (qreal)textWidth) * factor; percent += ((qreal)width / (qreal)textWidth) * factor;

View File

@ -7,8 +7,10 @@
class Tile { class Tile {
public: public:
Tile(QImage *img, qreal scale) Tile(QImage *img, const QPointF &scale)
: _size(img->size()), _text(img->size(), scale), _painter(img) {} : _size(img->size()), _text(QSize(img->size().width() / scale.x(),
img->size().height() / scale.y())), _painter(img)
{_painter.scale(scale.x(), scale.y());}
QSize size() const {return _size;} QSize size() const {return _size;}
Text &text() {return _text;} Text &text() {return _text;}