1
0
mirror of https://github.com/tumic0/GPXSee.git synced 2025-01-31 09:05:14 +01:00

Improved area labels rendering

This commit is contained in:
Martin Tůma 2021-04-17 13:23:53 +02:00
parent c29acb1aea
commit a4e39a98c0
4 changed files with 85 additions and 52 deletions

View File

@ -79,11 +79,11 @@ static QString *pathLabel(const Style::TextRender *ri, MapData::Path &path,
return 0; return 0;
} }
void RasterTile::processPoints(QList<TextItem*> &textItems) void RasterTile::processPointLabels(QList<TextItem*> &textItems)
{ {
const Style &s = style(); const Style &s = style();
QList<const Style::TextRender*> labels(s.pointLabels(_zoom)); QList<const Style::TextRender*> labels(s.pointLabels(_zoom));
QList<const Style::Symbol*> symbols(s.symbols(_zoom)); QList<const Style::Symbol*> symbols(s.pointSymbols(_zoom));
for (int i = 0; i < _points.size(); i++) { for (int i = 0; i < _points.size(); i++) {
MapData::Point &point = _points[i]; MapData::Point &point = _points[i];
@ -125,45 +125,60 @@ void RasterTile::processPoints(QList<TextItem*> &textItems)
} }
} }
void RasterTile::processAreaNames(const QRect &tileRect, void RasterTile::processAreaLabels(const QRect &tileRect,
QList<TextItem*> &textItems) QList<TextItem*> &textItems)
{ {
const Style &s = style(); const Style &s = style();
QList<const Style::TextRender*> instructions(s.areaLabels(_zoom)); QList<const Style::TextRender*> labels(s.areaLabels(_zoom));
QSet<QString> set; QList<const Style::Symbol*> symbols(s.areaSymbols(_zoom));
for (int i = 0; i < instructions.size(); i++) { for (int i = 0; i < _paths.size(); i++) {
const Style::TextRender *ri = instructions.at(i); MapData::Path &path = _paths[i];
for (int j = 0; j < _paths.size(); j++) {
MapData::Path &path = _paths[j];
QString *label = 0; QString *label = 0;
const Style::TextRender *ti = 0;
const Style::Symbol *si = 0;
if (!path.closed || !path.path.elementCount()) if (!path.closed || !path.path.elementCount())
continue; continue;
if (!ri->rule().match(path.closed, path.tags))
continue; for (int j = 0; j < labels.size(); j++) {
if (!(label = pathLabel(ri, path))) const Style::TextRender *ri = labels.at(j);
continue; if (ri->rule().match(path.closed, path.tags)) {
if (set.contains(path.label)) if ((label = pathLabel(ri, path))) {
ti = ri;
break;
}
}
}
for (int j = 0; j < symbols.size(); j++) {
const Style::Symbol *ri = symbols.at(j);
if (ri->rule().match(path.tags)) {
si = ri;
break;
}
}
if (!ti && !si)
continue; continue;
const QImage *img = si ? &si->img() : 0;
const QFont *font = ti ? &ti->font() : 0;
const QColor *color = ti ? &ti->fillColor() : 0;
QPointF pos = path.labelPos.isNull() QPointF pos = path.labelPos.isNull()
? centroid(path.path) : ll2xy(path.labelPos); ? centroid(path.path) : ll2xy(path.labelPos);
TextPointItem *item = new TextPointItem(pos.toPoint(), label, TextPointItem *item = new TextPointItem(pos.toPoint(), label, font, img,
&ri->font(), 0, &ri->fillColor(), 0, false); color, 0, false);
if (item->isValid() && tileRect.contains(item->boundingRect().toRect()) if (item->isValid() && tileRect.contains(item->boundingRect().toRect())
&& !item->collides(textItems)) { && !item->collides(textItems))
textItems.append(item); textItems.append(item);
set.insert(path.label); else
} else
delete item; delete item;
} }
}
} }
void RasterTile::processStreetNames(const QRect &tileRect, void RasterTile::processLineLabels(const QRect &tileRect,
QList<TextItem*> &textItems) QList<TextItem*> &textItems)
{ {
const Style &s = style(); const Style &s = style();
@ -307,9 +322,9 @@ void RasterTile::render()
drawPaths(&painter); drawPaths(&painter);
processPoints(textItems); processPointLabels(textItems);
processAreaNames(tileRect, textItems); processAreaLabels(tileRect, textItems);
processStreetNames(tileRect, textItems); processLineLabels(tileRect, textItems);
drawTextItems(&painter, textItems); drawTextItems(&painter, textItems);
//painter.setPen(Qt::red); //painter.setPen(Qt::red);

View File

@ -71,9 +71,9 @@ private:
QVector<PathInstruction> pathInstructions(); QVector<PathInstruction> pathInstructions();
QPointF ll2xy(const Coordinates &c) const QPointF ll2xy(const Coordinates &c) const
{return _transform.proj2img(_proj.ll2xy(c));} {return _transform.proj2img(_proj.ll2xy(c));}
void processPoints(QList<TextItem*> &textItems); void processPointLabels(QList<TextItem*> &textItems);
void processAreaNames(const QRect &tileRect, QList<TextItem*> &textItems); void processAreaLabels(const QRect &tileRect, QList<TextItem*> &textItems);
void processStreetNames(const QRect &tileRect, QList<TextItem*> &textItems); void processLineLabels(const QRect &tileRect, QList<TextItem*> &textItems);
QPainterPath painterPath(const Polygon &polygon) const; QPainterPath painterPath(const Polygon &polygon) const;
void drawTextItems(QPainter *painter, const QList<TextItem*> &textItems); void drawTextItems(QPainter *painter, const QList<TextItem*> &textItems);
void drawPaths(QPainter *painter); void drawPaths(QPainter *painter);

View File

@ -342,7 +342,7 @@ Style::Style(const QString &path)
QVector<const Style::PathRender *> Style::paths(int zoom, bool closed, QVector<const Style::PathRender *> Style::paths(int zoom, bool closed,
const QVector<MapData::Tag> &tags) const const QVector<MapData::Tag> &tags) const
{ {
QVector<const Style::PathRender*> ri; QVector<const PathRender*> ri;
for (int i = 0; i < _paths.size(); i++) for (int i = 0; i < _paths.size(); i++)
if (_paths.at(i).rule().match(zoom, closed, tags)) if (_paths.at(i).rule().match(zoom, closed, tags))
@ -353,10 +353,10 @@ QVector<const Style::PathRender *> Style::paths(int zoom, bool closed,
QList<const Style::TextRender*> Style::pathLabels(int zoom) const QList<const Style::TextRender*> Style::pathLabels(int zoom) const
{ {
QList<const Style::TextRender*> list; QList<const TextRender*> list;
for (int i = 0; i < _pathLabels.size(); i++) for (int i = 0; i < _pathLabels.size(); i++)
if (_pathLabels.at(i).rule().zooms().contains(zoom)) if (_pathLabels.at(i).rule()._zooms.contains(zoom))
list.append(&_pathLabels.at(i)); list.append(&_pathLabels.at(i));
return list; return list;
@ -364,10 +364,10 @@ QList<const Style::TextRender*> Style::pathLabels(int zoom) const
QList<const Style::TextRender*> Style::pointLabels(int zoom) const QList<const Style::TextRender*> Style::pointLabels(int zoom) const
{ {
QList<const Style::TextRender*> list; QList<const TextRender*> list;
for (int i = 0; i < _pointLabels.size(); i++) for (int i = 0; i < _pointLabels.size(); i++)
if (_pointLabels.at(i).rule().zooms().contains(zoom)) if (_pointLabels.at(i).rule()._zooms.contains(zoom))
list.append(&_pointLabels.at(i)); list.append(&_pointLabels.at(i));
return list; return list;
@ -375,22 +375,41 @@ QList<const Style::TextRender*> Style::pointLabels(int zoom) const
QList<const Style::TextRender*> Style::areaLabels(int zoom) const QList<const Style::TextRender*> Style::areaLabels(int zoom) const
{ {
QList<const Style::TextRender*> list; QList<const TextRender*> list;
for (int i = 0; i < _areaLabels.size(); i++) for (int i = 0; i < _areaLabels.size(); i++)
if (_areaLabels.at(i).rule().zooms().contains(zoom)) if (_areaLabels.at(i).rule()._zooms.contains(zoom))
list.append(&_areaLabels.at(i)); list.append(&_areaLabels.at(i));
return list; return list;
} }
QList<const Style::Symbol*> Style::symbols(int zoom) const QList<const Style::Symbol*> Style::pointSymbols(int zoom) const
{ {
QList<const Style::Symbol*> list; QList<const Symbol*> list;
for (int i = 0; i < _symbols.size(); i++) for (int i = 0; i < _symbols.size(); i++) {
if (_symbols.at(i).rule().zooms().contains(zoom)) const Symbol &symbol = _symbols.at(i);
list.append(&_symbols.at(i)); const Rule & rule = symbol.rule();
if (rule._zooms.contains(zoom) && (rule._type == Rule::AnyType
|| rule._type == Rule::NodeType))
list.append(&symbol);
}
return list;
}
QList<const Style::Symbol*> Style::areaSymbols(int zoom) const
{
QList<const Symbol*> list;
for (int i = 0; i < _symbols.size(); i++) {
const Symbol &symbol = _symbols.at(i);
const Rule & rule = symbol.rule();
if (rule._zooms.contains(zoom) && (rule._type == Rule::AnyType
|| rule._type == Rule::WayType))
list.append(&symbol);
}
return list; return list;
} }

View File

@ -34,8 +34,6 @@ public:
bool match(int zoom, bool closed, bool match(int zoom, bool closed,
const QVector<MapData::Tag> &tags) const; const QVector<MapData::Tag> &tags) const;
const Range &zooms() const {return _zooms;}
private: private:
enum Type { enum Type {
AnyType = 0, AnyType = 0,
@ -224,7 +222,8 @@ public:
QList<const TextRender*> pathLabels(int zoom) const; QList<const TextRender*> pathLabels(int zoom) const;
QList<const TextRender*> pointLabels(int zoom) const; QList<const TextRender*> pointLabels(int zoom) const;
QList<const TextRender*> areaLabels(int zoom) const; QList<const TextRender*> areaLabels(int zoom) const;
QList<const Symbol*> symbols(int zoom) const; QList<const Symbol*> pointSymbols(int zoom) const;
QList<const Symbol*> areaSymbols(int zoom) const;
private: private:
QList<PathRender> _paths; QList<PathRender> _paths;