From ab062cc3ffd9d918c007f76a640ee030dff85408 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Martin=20T=C5=AFma?= Date: Mon, 16 Aug 2021 09:00:36 +0200 Subject: [PATCH] Improved handling of labels with separators --- src/map/IMG/lblfile.cpp | 63 +++++++++++++++++++++++++++++--------- src/map/IMG/lblfile.h | 14 ++++++--- src/map/IMG/rastertile.cpp | 18 ----------- src/map/IMG/rgnfile.cpp | 13 +++++--- src/map/IMG/style.h | 2 -- 5 files changed, 65 insertions(+), 45 deletions(-) diff --git a/src/map/IMG/lblfile.cpp b/src/map/IMG/lblfile.cpp index 415c4aeb..f52ecf8c 100644 --- a/src/map/IMG/lblfile.cpp +++ b/src/map/IMG/lblfile.cpp @@ -57,6 +57,12 @@ static QString capitalized(const QString &str) return ret; } +static QByteArray ft2m(const QByteArray &str) +{ + bool ok; + int number = str.toInt(&ok); + return ok ? QByteArray::number(qRound(number * 0.3048)) : str; +} LBLFile::~LBLFile() { @@ -130,13 +136,15 @@ void LBLFile::clear() _rasters = 0; } -Label LBLFile::label6b(Handle &hdl, quint32 offset, bool capitalize) const +Label LBLFile::label6b(Handle &hdl, quint32 offset, bool capitalize, + bool convert) const { Shield::Type shieldType = Shield::None; QByteArray label, shieldLabel; QByteArray *bap = &label; Charset curCharSet = Normal; quint8 b1, b2, b3; + int split = -1; if (!seek(hdl, offset)) return Label(); @@ -149,6 +157,10 @@ Label LBLFile::label6b(Handle &hdl, quint32 offset, bool capitalize) const for (int cpt = 0; cpt < 4; cpt++) { if (c[cpt] > 0x2f || (curCharSet == Normal && c[cpt] == 0x1d)) { + if (split >= 0) + label = label.left(split) + ft2m(label.mid(split)); + else if (convert) + label = ft2m(label); QString text(QString::fromLatin1(label)); return Label(capitalize && isAllUpperCase(text) ? capitalized(text) : text, Shield(shieldType, shieldLabel)); @@ -159,7 +171,16 @@ Label LBLFile::label6b(Handle &hdl, quint32 offset, bool capitalize) const curCharSet = Symbol; else if (c[cpt] == 0x1b) curCharSet = Special; - else if (c[cpt] >= 0x2a && c[cpt] <= 0x2f) { + else if (c[cpt] >= 0x1e && c[cpt] <= 0x1f) { + if (bap == &shieldLabel) + bap = &label; + else { + if (!bap->isEmpty()) + bap->append('\n'); + if (c[cpt] == 0x1f && split < 0) + split = bap->size(); + } + } else if (c[cpt] >= 0x2a && c[cpt] <= 0x2f) { shieldType = static_cast(c[cpt] - 0x29); bap = &shieldLabel; } else if (bap == &shieldLabel @@ -181,11 +202,13 @@ Label LBLFile::label6b(Handle &hdl, quint32 offset, bool capitalize) const } } -Label LBLFile::str2label(const QVector &str, bool capitalize) const +Label LBLFile::str2label(const QVector &str, bool capitalize, + bool convert) const { Shield::Type shieldType = Shield::None; QByteArray label, shieldLabel; QByteArray *bap = &label; + int split = -1; for (int i = 0; i < str.size(); i++) { const quint8 &c = str.at(i); @@ -198,8 +221,12 @@ Label LBLFile::str2label(const QVector &str, bool capitalize) const else if ((c >= 0x1e && c <= 0x1f)) { if (bap == &shieldLabel) bap = &label; - else - bap->append(' '); + else { + if (!bap->isEmpty()) + bap->append('\n'); + if (c == 0x1f && split < 0) + split = bap->size(); + } } else if (c < 0x07) { shieldType = static_cast(c); bap = &shieldLabel; @@ -209,13 +236,17 @@ Label LBLFile::str2label(const QVector &str, bool capitalize) const bap->append(c); } + if (split >= 0) + label = label.left(split) + ft2m(label.mid(split)); + else if (convert) + label = ft2m(label); QString text(_codec.toString(label)); - return Label(capitalize && isAllUpperCase(text) ? capitalized(text) : text, Shield(shieldType, _codec.toString(shieldLabel))); } -Label LBLFile::label8b(Handle &hdl, quint32 offset, bool capitalize) const +Label LBLFile::label8b(Handle &hdl, quint32 offset, bool capitalize, + bool convert) const { QVector str; quint8 c; @@ -229,10 +260,11 @@ Label LBLFile::label8b(Handle &hdl, quint32 offset, bool capitalize) const str.append(c); } while (c); - return str2label(str, capitalize); + return str2label(str, capitalize, convert); } -Label LBLFile::labelHuffman(Handle &hdl, quint32 offset, bool capitalize) const +Label LBLFile::labelHuffman(Handle &hdl, quint32 offset, bool capitalize, + bool convert) const { QVector str; @@ -241,7 +273,7 @@ Label LBLFile::labelHuffman(Handle &hdl, quint32 offset, bool capitalize) const if (!_huffmanText->decode(this, hdl, str)) return Label(); if (!_table) - return str2label(str, capitalize); + return str2label(str, capitalize, convert); QVector str2; @@ -268,10 +300,11 @@ Label LBLFile::labelHuffman(Handle &hdl, quint32 offset, bool capitalize) const } } - return str2label(str2, capitalize); + return str2label(str2, capitalize, convert); } -Label LBLFile::label(Handle &hdl, quint32 offset, bool poi, bool capitalize) const +Label LBLFile::label(Handle &hdl, quint32 offset, bool poi, bool capitalize, + bool convert) const { quint32 labelOffset; if (poi) { @@ -289,12 +322,12 @@ Label LBLFile::label(Handle &hdl, quint32 offset, bool poi, bool capitalize) con switch (_encoding) { case 6: - return label6b(hdl, labelOffset, capitalize); + return label6b(hdl, labelOffset, capitalize, convert); case 9: case 10: - return label8b(hdl, labelOffset, capitalize); + return label8b(hdl, labelOffset, capitalize, convert); case 11: - return labelHuffman(hdl, labelOffset, capitalize); + return labelHuffman(hdl, labelOffset, capitalize, convert); default: return Label(); } diff --git a/src/map/IMG/lblfile.h b/src/map/IMG/lblfile.h index bb07e669..d4fab03f 100644 --- a/src/map/IMG/lblfile.h +++ b/src/map/IMG/lblfile.h @@ -32,7 +32,7 @@ public: void clear(); Label label(Handle &hdl, quint32 offset, bool poi = false, - bool capitalize = true) const; + bool capitalize = true, bool convert = false) const; quint8 imageIdSize() const {return _imgOffsetIdSize;} QPixmap image(Handle &hdl, quint32 id) const; @@ -43,10 +43,14 @@ private: quint32 size; }; - Label str2label(const QVector &str, bool capitalize) const; - Label label6b(Handle &hdl, quint32 offset, bool capitalize) const; - Label label8b(Handle &hdl, quint32 offset, bool capitalize) const; - Label labelHuffman(Handle &hdl, quint32 offset, bool capitalize) const; + Label str2label(const QVector &str, bool capitalize, + bool convert) const; + Label label6b(Handle &hdl, quint32 offset, bool capitalize, + bool convert) const; + Label label8b(Handle &hdl, quint32 offset, bool capitalize, + bool convert) const; + Label labelHuffman(Handle &hdl, quint32 offset, bool capitalize, + bool convert) const; bool loadRasterTable(Handle &hdl, quint32 offset, quint32 size, quint32 recordSize); diff --git a/src/map/IMG/rastertile.cpp b/src/map/IMG/rastertile.cpp index 7078162e..6af76606 100644 --- a/src/map/IMG/rastertile.cpp +++ b/src/map/IMG/rastertile.cpp @@ -21,13 +21,6 @@ static const QColor shieldBgColor1("#dd3e3e"); static const QColor shieldBgColor2("#379947"); static const QColor shieldBgColor3("#4a7fc1"); -static QString convertUnits(const QString &str) -{ - bool ok; - int number = str.toInt(&ok); - return ok ? QString::number(qRound(number * 0.3048)) : str; -} - static QFont pixelSizeFont(int pixelSize) { QFont f; @@ -350,9 +343,6 @@ void RasterTile::processStreetNames(const QRect &tileRect, || style.textFontSize() == Style::None) continue; - if (Style::isContourLine(poly.type)) - poly.label.setText(convertUnits(poly.label.text())); - const QFont *fnt = font(style.textFontSize(), Style::Small); const QColor *color = style.textColor().isValid() ? &style.textColor() : 0; @@ -450,14 +440,6 @@ void RasterTile::processPoints(QList &textItems) if ((!label || !fnt) && !img) continue; - if (Style::isSpot(point.type)) - point.label.setText(convertUnits(point.label.text())); - if (Style::isSummit(point.type) && !point.label.text().isEmpty()) { - QStringList list = point.label.text().split(" "); - list.last() = convertUnits(list.last()); - point.label = list.join(" "); - } - TextPointItem *item = new TextPointItem(QPoint(point.coordinates.lon(), point.coordinates.lat()), label, fnt, img, color, &haloColor); if (item->isValid() && !item->collides(textItems)) diff --git a/src/map/IMG/rgnfile.cpp b/src/map/IMG/rgnfile.cpp index b975105d..ac781f85 100644 --- a/src/map/IMG/rgnfile.cpp +++ b/src/map/IMG/rgnfile.cpp @@ -237,9 +237,11 @@ bool RGNFile::polyObjects(Handle &hdl, const SubDiv *subdiv, quint32 lblOff; if (net && net->lblOffset(netHdl, labelPtr & 0x3FFFFF, lblOff) && lblOff) - poly.label = lbl->label(lblHdl, lblOff); + poly.label = lbl->label(lblHdl, lblOff, false, true, + Style::isContourLine(poly.type)); } else - poly.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF); + poly.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF, false, + true, Style::isContourLine(poly.type)); } polys->append(poly); @@ -351,7 +353,8 @@ bool RGNFile::extPolyObjects(Handle &hdl, const SubDiv *subdiv, quint32 shift, return false; if (lbl && (labelPtr & 0x3FFFFF)) - poly.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF); + poly.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF, false, true, + Style::isContourLine(poly.type)); polys->append(poly); } @@ -396,7 +399,7 @@ bool RGNFile::pointObjects(Handle &hdl, const SubDiv *subdiv, if (lbl && (labelPtr & 0x3FFFFF)) point.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF, labelPtr & 0x400000, !(Style::isCountry(point.type) - || Style::isState(point.type))); + || Style::isState(point.type)), Style::isSpot(point.type)); points->append(point); } @@ -446,7 +449,7 @@ bool RGNFile::extPointObjects(Handle &hdl, const SubDiv *subdiv, point.coordinates = Coordinates(toWGS24(pos.x()), toWGS24(pos.y())); point.id = pointId(pos, point.type, labelPtr & 0x3FFFFF); if (lbl && (labelPtr & 0x3FFFFF)) - point.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF, false); + point.label = lbl->label(lblHdl, labelPtr & 0x3FFFFF); points->append(point); } diff --git a/src/map/IMG/style.h b/src/map/IMG/style.h index 82afbed1..6f3ef389 100644 --- a/src/map/IMG/style.h +++ b/src/map/IMG/style.h @@ -111,8 +111,6 @@ public: {return (type == TYPE(0x16) || type == 0x10a03);} static bool isSpot(quint32 type) {return (type == TYPE(0x62) || type == TYPE(0x63));} - static bool isSummit(quint32 type) - {return (type == 0x6616);} static bool isMajorRoad(quint32 type) {return (type <= TYPE(0x04));} static bool isCountry(quint32 type)