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,
Tile &tile)
Tile &tile, const QPointF &scale)
{
if (layer.data()->version() > 2)
return;
QSizeF factor(tile.size().width() / (qreal)layer.data()->extent(),
tile.size().height() / (qreal)layer.data()->extent());
QSizeF factor(tile.size().width() / scale.x() / (qreal)layer.data()->extent(),
tile.size().height() / scale.y() / (qreal)layer.data()->extent());
style->setPainter(styleLayer, tile);
for (int i = 0; i < layer.features().size(); i++)
drawFeature(layer.features().at(i), style, styleLayer, factor, tile);
}
bool PBF::render(const QByteArray &data, int zoom, Style *style, qreal scale,
QImage *image)
bool PBF::render(const QByteArray &data, int zoom, Style *style,
const QPointF &scale, QImage *image)
{
vector_tile::Tile tile;
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())
continue;
drawLayer(*it, style, i, t);
drawLayer(*it, style, i, t, scale);
}
t.painter().restore();

View File

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

View File

@ -67,9 +67,9 @@ bool PBFHandler::read(QImage *image)
QSize size = _scaledSize.isValid()
? _scaledSize : QSize(TILE_SIZE, TILE_SIZE);
qreal scale = _scaledSize.isValid()
? qMax(_scaledSize.width() / TILE_SIZE, _scaledSize.height() / TILE_SIZE)
: 1.0;
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);
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);
tile.painter().drawPath(path);
}
tile.painter().setPen(Qt::red);
tile.painter().drawRect(rect);
}

View File

@ -157,12 +157,7 @@ void Text::addLabel(const QString &text, const QPointF &pos,
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());
addItem(ti);
@ -179,20 +174,17 @@ void Text::addLabel(const QString &text, const QPainterPath &path,
if (text.isEmpty())
return;
QFont scaledFont(painter.font());
scaledFont.setPixelSize(painter.font().pixelSize() * _fontScale);
int textWidth = text.size() * scaledFont.pixelSize() * 0.6;
int textWidth = text.size() * painter.font().pixelSize() * 0.6;
if (textWidth > path.length())
return;
QPainterPath tp(textPath(path, textWidth, maxAngle,
scaledFont.pixelSize() / 2, _sceneRect));
painter.font().pixelSize() / 2, _sceneRect));
if (tp.isEmpty())
return;
TextPathItem *pi = new TextPathItem(text, reverse(tp) ? tp.toReversed()
: tp, scaledFont);
: tp, painter.font());
if (!_sceneRect.contains(pi->boundingRect())) {
delete pi;
return;

View File

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

View File

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

View File

@ -7,8 +7,10 @@
class Tile {
public:
Tile(QImage *img, qreal scale)
: _size(img->size()), _text(img->size(), scale), _painter(img) {}
Tile(QImage *img, const QPointF &scale)
: _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;}
Text &text() {return _text;}