From 22c8afced5970cac24a7ce98d67ac6741bb26cef Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Konstantin=20Ka=CC=88fer?= Date: Wed, 9 Jul 2014 16:27:23 -0700 Subject: [PATCH] replace {{token}} with {token}, and %d/%d/%d with {z}/{x}/{y} refs https://github.com/mapbox/mapbox-gl-style-spec/pull/87 --- bin/style.js | 56 +++++++++++++++---------------- include/llmr/style/value.hpp | 6 ---- include/llmr/util/token.hpp | 27 ++++++++------- src/map/source.cpp | 14 +++++--- src/text/glyph_store.cpp | 11 +++--- test/fixtures/styles/0.style.json | 2 +- test/fixtures/styles/1.style.json | 2 +- 7 files changed, 59 insertions(+), 59 deletions(-) diff --git a/bin/style.js b/bin/style.js index 21cca2af3..c292a2692 100644 --- a/bin/style.js +++ b/bin/style.js @@ -3,7 +3,7 @@ module.exports = { "version": 3, "sprite": "https://www.mapbox.com/mapbox-gl-styles/sprites/outdoors", - "glyphs": "https://mapbox.s3.amazonaws.com/gl-glyphs-256/{{fontstack}}/{{range}}.pbf", + "glyphs": "https://mapbox.s3.amazonaws.com/gl-glyphs-256/{fontstack}/{range}.pbf", "sources": { "mapbox": { "type": "vector", @@ -1739,7 +1739,7 @@ module.exports = { "filter": { "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold, Arial Unicode MS Bold", "text-max-size": 24, @@ -1764,7 +1764,7 @@ module.exports = { "filter": { "$type": "line", "labelrank": 1 }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 30, @@ -1794,7 +1794,7 @@ module.exports = { "filter": { "$type": "line", "labelrank": 2 }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 24, @@ -1823,7 +1823,7 @@ module.exports = { "filter": { "$type": "line", "labelrank": 3 }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 18, @@ -1852,7 +1852,7 @@ module.exports = { "filter": { "$type": "line", "labelrank": [4, 5, 6] }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 16, @@ -1881,7 +1881,7 @@ module.exports = { "filter": { "$type": "point", "labelrank": 1 }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 30, @@ -1912,7 +1912,7 @@ module.exports = { "filter": { "$type": "point", "labelrank": 2 }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 24, @@ -1943,7 +1943,7 @@ module.exports = { "filter": { "$type": "point", "labelrank": 3 }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 18, @@ -1974,7 +1974,7 @@ module.exports = { "filter": { "$type": "point", "labelrank": [4, 5, 6] }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 16, @@ -2005,7 +2005,7 @@ module.exports = { "filter": { "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Regular, Arial Unicode MS Regular", "text-max-size": 16, @@ -2036,7 +2036,7 @@ module.exports = { "filter": { "type": "city", "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold, Arial Unicode MS Bold", "text-max-size": 20, @@ -2067,7 +2067,7 @@ module.exports = { "filter": { "type": "town", "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold, Arial Unicode MS Bold", "text-max-size": 24, @@ -2098,7 +2098,7 @@ module.exports = { "filter": { "type": "village", "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold, Arial Unicode MS Bold", "text-max-size": 22, @@ -2129,7 +2129,7 @@ module.exports = { "filter": { "type": ["hamlet", "suburb", "neighbourhood"], "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold, Arial Unicode MS Bold", "text-max-size": 18, @@ -2159,7 +2159,7 @@ module.exports = { "filter": { "class": ["motorway", "main"], "$type": "line" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-padding": 2, "text-font": "Open Sans Regular, Arial Unicode MS Regular", @@ -2185,7 +2185,7 @@ module.exports = { "filter": { "class": ["street", "street_limited"], "$type": "line" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-padding": 2, "text-font": "Open Sans Regular, Arial Unicode MS Regular", @@ -2211,7 +2211,7 @@ module.exports = { "filter": { "class": ["service", "driveway", "path"], "$type": "line" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-padding": 2, "text-font": "Open Sans Regular, Arial Unicode MS Regular", @@ -2238,7 +2238,7 @@ module.exports = { "render": { "type": "text", "text-path": "curve", - "text-field": "{{ele}} m", + "text-field": "{ele} m", "text-font": "Open Sans Regular, Arial Unicode MS Regular", "text-max-size": 10, "text-max-angle": 0.5 @@ -2262,7 +2262,7 @@ module.exports = { "filter": { "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 12, @@ -2283,7 +2283,7 @@ module.exports = { "filter": { "$type": "line" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "curve", "text-font": "Open Sans Semibold Italic, Arial Unicode MS Bold", "text-max-size": 12, @@ -2305,7 +2305,7 @@ module.exports = { "filter": { "scalerank": [1, 2] }, "render": { "type": "icon", - "icon-image": "{{maki}}-12", + "icon-image": "{maki}-12", "icon-size": 12 }, "style.night": {} @@ -2316,7 +2316,7 @@ module.exports = { "filter": { "scalerank": [1, 2], "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-padding": 2, "text-font": "Open Sans Semibold, Arial Unicode MS Bold", @@ -2343,7 +2343,7 @@ module.exports = { "filter": { "scalerank": 3 }, "render": { "type": "icon", - "icon-image": "{{maki}}-12", + "icon-image": "{maki}-12", "icon-size": 12 }, "style": { @@ -2365,7 +2365,7 @@ module.exports = { "filter": { "scalerank": 3, "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-padding": 2, "text-font": "Open Sans Semibold, Arial Unicode MS Bold", @@ -2400,7 +2400,7 @@ module.exports = { "filter": { "scalerank": 4 }, "render": { "type": "icon", - "icon-image": "{{maki}}-12", + "icon-image": "{maki}-12", "icon-size": 12 }, "style": { @@ -2422,7 +2422,7 @@ module.exports = { "filter": { "scalerank": 4, "$type": "point" }, "render": { "type": "text", - "text-field": "{{name_en}}", + "text-field": "{name_en}", "text-path": "horizontal", "text-padding": 2, "text-font": "Open Sans Semibold, Arial Unicode MS Bold", @@ -2457,7 +2457,7 @@ module.exports = { "filter": { "maki": "airport" }, "render": { "type": "icon", - "icon-image": "{{maki}}-12", + "icon-image": "{maki}-12", "icon-size": 12 }, "style": { diff --git a/include/llmr/style/value.hpp b/include/llmr/style/value.hpp index bdcba4cad..fac316d3d 100644 --- a/include/llmr/style/value.hpp +++ b/include/llmr/style/value.hpp @@ -10,12 +10,6 @@ typedef util::variant Value; std::string toString(const Value &value); -inline std::string& operator+=(std::string& out, const Value& value) { - out += toString(value); - return out; -} - - Value parseValue(pbf data); namespace util { diff --git a/include/llmr/util/token.hpp b/include/llmr/util/token.hpp index db5b5fa0c..6795b97d1 100644 --- a/include/llmr/util/token.hpp +++ b/include/llmr/util/token.hpp @@ -9,40 +9,43 @@ namespace regex_impl = boost; namespace regex_impl = std; #endif +#include + namespace llmr { namespace util { namespace detail { -const regex_impl::regex tokenRegex("\\{\\{(\\w+)\\}\\}"); +const regex_impl::regex tokenRegex("\\{(\\w+)\\}"); const regex_impl::sregex_token_iterator tokensEnd = regex_impl::sregex_token_iterator(); } -template -std::string replaceTokens(const std::string &source, const Tokens &tokens) { +template +std::string replaceTokens(const std::string &source, const Lookup &lookup) { std::string result; result.reserve(source.size()); bool token = false; for (auto token_it = regex_impl::sregex_token_iterator(source.begin(), source.end(), detail::tokenRegex, {-1, 1}); - token_it != detail::tokensEnd; ++token_it, token = !token) { + token_it != detail::tokensEnd; ++token_it, token = !token) { if (!token_it->matched) { continue; } - if (token) { - const auto it_prop = tokens.find(token_it->str()); - if (it_prop != tokens.end()) { - result += it_prop->second; - } - } else { - result += token_it->str(); - } + result += token ? lookup(token_it->str()) : token_it->str(); } return result; } +template +inline std::string replaceTokens(const std::string &source, const std::map &properties) { + return replaceTokens(source, [&properties](const std::string &token) -> std::string { + const auto it_prop = properties.find(token); + return it_prop != properties.end() ? toString(it_prop->second) : ""; + }); +} + } // end namespace util } // end namespace llmr diff --git a/src/map/source.cpp b/src/map/source.cpp index c3c5e89fd..8d1593f76 100644 --- a/src/map/source.cpp +++ b/src/map/source.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include #include @@ -28,7 +29,7 @@ Source::Source(SourceType type, const std::string &url, std::string Source::normalizeSourceURL(const std::string &url) { const std::string t = "mapbox://"; if (url.compare(0, t.length(), t) == 0) { - return std::string("http://api.tiles.mapbox.com/v3/") + url.substr(t.length()) + "/%d/%d/%d.vector.pbf"; + return std::string("http://api.tiles.mapbox.com/v3/") + url.substr(t.length()) + "/{z}/{x}/{y}.vector.pbf"; } else { return url; } @@ -142,14 +143,17 @@ TileData::State Source::addTile(Map &map, const Tile::ID& id) { if (!new_tile.data) { // If we don't find working tile data, we're just going to load it. - - std::string formed_url; + const std::string formed_url = util::replaceTokens(url, [&](const std::string &token) -> std::string { + if (token == "z") return std::to_string(normalized_id.z); + if (token == "x") return std::to_string(normalized_id.x); + if (token == "y") return std::to_string(normalized_id.y); + if (token == "ratio") return (map.getState().getPixelRatio() > 1.0 ? "@2x" : ""); + return ""; + }); if (type == SourceType::Vector) { - formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y); new_tile.data = std::make_shared(normalized_id, map, formed_url); } else if (type == SourceType::Raster) { - formed_url = util::sprintf<256>(url, normalized_id.z, normalized_id.x, normalized_id.y, (map.getState().getPixelRatio() > 1.0 ? "@2x" : "")); new_tile.data = std::make_shared(normalized_id, map, formed_url); } else { throw std::runtime_error("source type not implemented"); diff --git a/src/text/glyph_store.cpp b/src/text/glyph_store.cpp index b79010670..f83d81089 100644 --- a/src/text/glyph_store.cpp +++ b/src/text/glyph_store.cpp @@ -120,12 +120,11 @@ GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, Gl : future(promise.get_future().share()) { // Load the glyph set URL - - const std::map tokens {{ - { "fontstack", fontStack }, - { "range", std::to_string(glyphRange.first) + "-" + std::to_string(glyphRange.second) } - }}; - std::string url = util::replaceTokens(glyphURL, tokens); + std::string url = util::replaceTokens(glyphURL, [&](const std::string &name) -> std::string { + if (name == "fontstack") return fontStack; + if (name == "range") return std::to_string(glyphRange.first) + "-" + std::to_string(glyphRange.second); + return ""; + }); // TODO: Find more reliable URL normalization function std::replace(url.begin(), url.end(), ' ', '+'); diff --git a/test/fixtures/styles/0.style.json b/test/fixtures/styles/0.style.json index 06d7a1db1..d5994b82c 100644 --- a/test/fixtures/styles/0.style.json +++ b/test/fixtures/styles/0.style.json @@ -3,7 +3,7 @@ "sources": { "mapbox": { "type": "vector", - "url": "tiles/%d-%d-%d.vector.pbf", + "url": "tiles/{z}-{x}-{y}.vector.pbf", "tileSize": 512, "maxZoom": 14 } diff --git a/test/fixtures/styles/1.style.json b/test/fixtures/styles/1.style.json index d35c4bc0f..0d0286b27 100644 --- a/test/fixtures/styles/1.style.json +++ b/test/fixtures/styles/1.style.json @@ -3,7 +3,7 @@ "sources": { "mapbox": { "type": "vector", - "url": "tiles/%d-%d-%d.vector.pbf", + "url": "tiles/{z}-{x}-{y}.vector.pbf", "tileSize": 512, "maxZoom": 14 }