diff options
author | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-28 16:29:54 -0800 |
---|---|---|
committer | John Firebaugh <john.firebaugh@gmail.com> | 2016-01-29 14:50:15 -0800 |
commit | b41a73e1c1dfea982df4a0326d45a48babc22dd4 (patch) | |
tree | fc744d5953ad6ecf8e52d5653c499760b148cb5c /src/mbgl/storage | |
parent | 849e8b32b4b2febc63ad3df8539b483ccc67ac63 (diff) | |
download | qtlocation-mapboxgl-b41a73e1c1dfea982df4a0326d45a48babc22dd4.tar.gz |
[core] Rationalize Resource initialization
Diffstat (limited to 'src/mbgl/storage')
-rw-r--r-- | src/mbgl/storage/resource.cpp | 82 |
1 files changed, 82 insertions, 0 deletions
diff --git a/src/mbgl/storage/resource.cpp b/src/mbgl/storage/resource.cpp new file mode 100644 index 0000000000..9cdedb59eb --- /dev/null +++ b/src/mbgl/storage/resource.cpp @@ -0,0 +1,82 @@ +#include <mbgl/storage/resource.hpp> +#include <mbgl/util/string.hpp> +#include <mbgl/util/token.hpp> +#include <mbgl/util/url.hpp> + +namespace mbgl { + +Resource Resource::style(const std::string& url) { + return Resource { + Resource::Kind::Style, + url + }; +} + +Resource Resource::source(const std::string& url) { + return Resource { + Resource::Kind::Source, + url + }; +} + +Resource Resource::spriteImage(const std::string& base, float pixelRatio) { + return Resource { + Resource::Kind::SpriteImage, + base + (pixelRatio > 1 ? "@2x" : "") + ".png" + }; +} + +Resource Resource::spriteJSON(const std::string& base, float pixelRatio) { + return Resource { + Resource::Kind::SpriteJSON, + base + (pixelRatio > 1 ? "@2x" : "") + ".json" + }; +} + +Resource Resource::glyphs(const std::string& urlTemplate, const std::string& fontStack, const std::pair<uint16_t, uint16_t>& glyphRange) { + return Resource { + Resource::Kind::Glyphs, + util::replaceTokens(urlTemplate, [&](const std::string& token) { + if (token == "fontstack") { + return util::percentEncode(fontStack); + } else if (token == "range") { + return util::toString(glyphRange.first) + "-" + util::toString(glyphRange.second); + } else { + return std::string(); + } + }) + }; +} + +Resource Resource::tile(const std::string& urlTemplate, float pixelRatio, int32_t x, int32_t y, int8_t z) { + return Resource { + Resource::Kind::Tile, + util::replaceTokens(urlTemplate, [&](const std::string& token) { + if (token == "z") { + return util::toString(z); + } else if (token == "x") { + return util::toString(x); + } else if (token == "y") { + return util::toString(y); + } else if (token == "prefix") { + std::string prefix{ 2 }; + prefix[0] = "0123456789abcdef"[x % 16]; + prefix[1] = "0123456789abcdef"[y % 16]; + return prefix; + } else if (token == "ratio") { + return std::string(pixelRatio > 1.0 ? "@2x" : ""); + } else { + return std::string(); + } + }), + Resource::TileData { + urlTemplate, + pixelRatio, + x, + y, + z + } + }; +} + +} |