From 3c8ac118ae02a3178947d9fe2346f1a25ab34346 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Sun, 18 Apr 2021 18:10:23 +0200 Subject: [PATCH] propper hidpi icon scaling --- src/map/mapsforge/rastertile.cpp | 14 +++++------ src/map/mapsforge/style.cpp | 40 ++++++++++++++++++-------------- src/map/mapsforge/style.h | 14 ++++++----- src/map/mapsforgemap.cpp | 5 ++-- src/map/textpointitem.cpp | 13 +++++++---- 5 files changed, 47 insertions(+), 39 deletions(-) diff --git a/src/map/mapsforge/rastertile.cpp b/src/map/mapsforge/rastertile.cpp index 578c9c08..9e35b538 100644 --- a/src/map/mapsforge/rastertile.cpp +++ b/src/map/mapsforge/rastertile.cpp @@ -8,9 +8,9 @@ using namespace Mapsforge; -static const Style& style() +static const Style& style(qreal ratio) { - static Style s(ProgramPaths::renderthemeFile()); + static Style s(ProgramPaths::renderthemeFile(), ratio); return s; } @@ -88,7 +88,7 @@ static const QColor *haloColor(const Style::TextRender *ti) void RasterTile::processPointLabels(QList &textItems) { - const Style &s = style(); + const Style &s = style(_ratio); QList labels(s.pointLabels(_zoom)); QList symbols(s.pointSymbols(_zoom)); @@ -136,7 +136,7 @@ void RasterTile::processPointLabels(QList &textItems) void RasterTile::processAreaLabels(QList &textItems) { - const Style &s = style(); + const Style &s = style(_ratio); QList labels(s.areaLabels(_zoom)); QList symbols(s.areaSymbols(_zoom)); @@ -189,7 +189,7 @@ void RasterTile::processAreaLabels(QList &textItems) void RasterTile::processLineLabels(QList &textItems) { - const Style &s = style(); + const Style &s = style(_ratio); QList instructions(s.pathLabels(_zoom)); QSet set; @@ -248,7 +248,7 @@ QVector RasterTile::pathInstructions() { QCache > cache(1024); QVector instructions; - const Style &s = style(); + const Style &s = style(_ratio); QVector *ri; for (int i = 0 ; i < _paths.size(); i++) { @@ -322,11 +322,11 @@ void RasterTile::render() QList textItems; + _pixmap.setDevicePixelRatio(_ratio); _pixmap.fill(Qt::transparent); QPainter painter(&_pixmap); painter.setRenderHint(QPainter::Antialiasing); - painter.scale(_ratio, _ratio); painter.translate(-_rect.x(), -_rect.y()); drawPaths(&painter); diff --git a/src/map/mapsforge/style.cpp b/src/map/mapsforge/style.cpp index e1134cdc..2bcf0aba 100644 --- a/src/map/mapsforge/style.cpp +++ b/src/map/mapsforge/style.cpp @@ -16,7 +16,7 @@ static QString resourcePath(const QString &src, const QString &dir) return dir + "/" + url.toLocalFile(); } -static QImage image(const QString &path, int width, int height) +static QImage image(const QString &path, int width, int height, qreal ratio) { QImageReader ir(path, "svg"); @@ -29,8 +29,10 @@ static QImage image(const QString &path, int width, int height) } else if (!height) height = ir.size().width() / (ir.size().width() / (double)width); - ir.setScaledSize(QSize(width, height)); - return ir.read(); + ir.setScaledSize(QSize(width * ratio, height * ratio)); + QImage img(ir.read()); + img.setDevicePixelRatio(ratio); + return img; } else return QImage(path); } @@ -77,7 +79,8 @@ bool Style::Rule::match(int zoom, bool closed, return true; } -void Style::area(QXmlStreamReader &reader, const QString &dir, const Rule &rule) +void Style::area(QXmlStreamReader &reader, const QString &dir, qreal ratio, + const Rule &rule) { PathRender ri(rule, _paths.size()); const QXmlStreamAttributes &attr = reader.attributes(); @@ -99,7 +102,7 @@ void Style::area(QXmlStreamReader &reader, const QString &dir, const Rule &rule) width = attr.value("symbol-width").toInt(); if (!file.isNull()) - ri._fillImage = image(file, width, height); + ri._fillImage = image(file, width, height, ratio); _paths.append(ri); @@ -184,7 +187,7 @@ void Style::text(QXmlStreamReader &reader, const Rule &rule, reader.skipCurrentElement(); } -void Style::symbol(QXmlStreamReader &reader, const QString &dir, +void Style::symbol(QXmlStreamReader &reader, const QString &dir, qreal ratio, const Rule &rule) { Symbol ri(rule); @@ -200,14 +203,14 @@ void Style::symbol(QXmlStreamReader &reader, const QString &dir, width = attr.value("symbol-width").toInt(); if (!file.isNull()) - ri._img = image(file, width, height); + ri._img = image(file, width, height, ratio); _symbols.append(ri); reader.skipCurrentElement(); } -void Style::rule(QXmlStreamReader &reader, const QString &dir, +void Style::rule(QXmlStreamReader &reader, const QString &dir, qreal ratio, const QSet &cats, const Rule &parent) { Rule r(parent); @@ -242,9 +245,9 @@ void Style::rule(QXmlStreamReader &reader, const QString &dir, while (reader.readNextStartElement()) { if (reader.name() == QLatin1String("rule")) - rule(reader, dir, cats, r); + rule(reader, dir, ratio, cats, r); else if (reader.name() == QLatin1String("area")) - area(reader, dir, r); + area(reader, dir, ratio, r); else if (reader.name() == QLatin1String("line")) line(reader, r); else if (reader.name() == QLatin1String("pathText")) { @@ -260,7 +263,7 @@ void Style::rule(QXmlStreamReader &reader, const QString &dir, text(reader, r, list); } else if (reader.name() == QLatin1String("symbol")) - symbol(reader, dir, r); + symbol(reader, dir, ratio, r); else reader.skipCurrentElement(); } @@ -298,14 +301,15 @@ void Style::stylemenu(QXmlStreamReader &reader, QSet &cats) } } -void Style::rendertheme(QXmlStreamReader &reader, const QString &dir) +void Style::rendertheme(QXmlStreamReader &reader, const QString &dir, + qreal ratio) { Rule r; QSet cats; while (reader.readNextStartElement()) { if (reader.name() == QLatin1String("rule")) - rule(reader, dir, cats, r); + rule(reader, dir, ratio, cats, r); else if (reader.name() == QLatin1String("stylemenu")) stylemenu(reader, cats); else @@ -313,7 +317,7 @@ void Style::rendertheme(QXmlStreamReader &reader, const QString &dir) } } -bool Style::loadXml(const QString &path) +bool Style::loadXml(const QString &path, qreal ratio) { QFile file(path); if (!file.open(QFile::ReadOnly)) @@ -323,7 +327,7 @@ bool Style::loadXml(const QString &path) if (reader.readNextStartElement()) { if (reader.name() == QLatin1String("rendertheme")) - rendertheme(reader, fi.absolutePath()); + rendertheme(reader, fi.absolutePath(), ratio); else reader.raiseError("Not a Mapsforge style file"); } @@ -335,10 +339,10 @@ bool Style::loadXml(const QString &path) return !reader.error(); } -Style::Style(const QString &path) +Style::Style(const QString &path, qreal ratio) { - if (!QFileInfo::exists(path) || !loadXml(path)) - loadXml(":/mapsforge/default.xml"); + if (!QFileInfo::exists(path) || !loadXml(path, ratio)) + loadXml(":/mapsforge/default.xml", ratio); } QVector Style::paths(int zoom, bool closed, diff --git a/src/map/mapsforge/style.h b/src/map/mapsforge/style.h index e9608d4a..b6a8959e 100644 --- a/src/map/mapsforge/style.h +++ b/src/map/mapsforge/style.h @@ -218,7 +218,7 @@ public: QImage _img; }; - Style(const QString &path); + Style(const QString &path, qreal ratio); QVector paths(int zoom, bool closed, const QVector &tags) const; @@ -233,18 +233,20 @@ private: QList _pathLabels, _pointLabels, _areaLabels; QList _symbols; - bool loadXml(const QString &path); - void rendertheme(QXmlStreamReader &reader, const QString &dir); + bool loadXml(const QString &path, qreal ratio); + void rendertheme(QXmlStreamReader &reader, const QString &dir, qreal ratio); void layer(QXmlStreamReader &reader, QSet &cats); void stylemenu(QXmlStreamReader &reader, QSet &cats); void cat(QXmlStreamReader &reader, QSet &cats); - void rule(QXmlStreamReader &reader, const QString &dir, + void rule(QXmlStreamReader &reader, const QString &dir, qreal ratio, const QSet &cats, const Rule &parent); - void area(QXmlStreamReader &reader, const QString &dir, const Rule &rule); + void area(QXmlStreamReader &reader, const QString &dir, qreal ratio, + const Rule &rule); void line(QXmlStreamReader &reader, const Rule &rule); void text(QXmlStreamReader &reader, const Rule &rule, QList *> &lists); - void symbol(QXmlStreamReader &reader, const QString &dir, const Rule &rule); + void symbol(QXmlStreamReader &reader, const QString &dir, qreal ratio, + const Rule &rule); }; } diff --git a/src/map/mapsforgemap.cpp b/src/map/mapsforgemap.cpp index a7c0e20b..284aa3fe 100644 --- a/src/map/mapsforgemap.cpp +++ b/src/map/mapsforgemap.cpp @@ -148,10 +148,9 @@ void MapsforgeMap::draw(QPainter *painter, const QRectF &rect, Flags flags) if (isRunning(key)) continue; - if (QPixmapCache::find(key, &pm)) { - pm.setDevicePixelRatio(_tileRatio); + if (QPixmapCache::find(key, &pm)) painter->drawPixmap(ttl, pm); - } else { + else { QList paths; QList points; diff --git a/src/map/textpointitem.cpp b/src/map/textpointitem.cpp index 08a7f4ec..7c6b70d2 100644 --- a/src/map/textpointitem.cpp +++ b/src/map/textpointitem.cpp @@ -41,10 +41,11 @@ void TextPointItem::setPos(const QPoint &point, bool padding) QRect iconRect; if (_img) { - int xOffset = padding ? _img->width() : _img->width() / 2; + QSize s(_img->size() / _img->devicePixelRatioF()); + int xOffset = padding ? s.width() : s.width() / 2; iconRect = QRect(QPoint(point.x() - xOffset, point.y() - - _img->height()/2), _img->size()); - _textRect.moveTopLeft(QPoint(point.x() + _img->width()/2, point.y() + - s.height()/2), s); + _textRect.moveTopLeft(QPoint(point.x() + s.width()/2, point.y() - _textRect.height()/2)); } else _textRect.moveCenter(point); @@ -56,9 +57,11 @@ void TextPointItem::setPos(const QPoint &point, bool padding) void TextPointItem::paint(QPainter *painter) const { - if (_img) + if (_img) { + QSize s(_img->size() / _img->devicePixelRatioF()); painter->drawImage(QPoint(_rect.left(), _rect.center().y() - - _img->height()/2), *_img); + - s.height()/2), *_img); + } if (_text) { if (_bgColor) {