Support for Mapbox fontstack API

This commit is contained in:
John Firebaugh
2014-08-22 15:31:57 -07:00
parent 3b43c4ddf2
commit 3d9a293258
9 changed files with 112 additions and 23 deletions

View File

@@ -0,0 +1,17 @@
#ifndef MBGL_UTIL_MAPBOX
#define MBGL_UTIL_MAPBOX
#include <string>
namespace mbgl {
namespace util {
namespace mapbox {
std::string normalizeSourceURL(const std::string& url, const std::string& accessToken);
std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken);
}
}
}
#endif

14
include/mbgl/util/url.hpp Normal file
View File

@@ -0,0 +1,14 @@
#ifndef MBGL_UTIL_URL
#define MBGL_UTIL_URL
#include <string>
namespace mbgl {
namespace util {
std::string percentEncode(const std::string&);
}
}
#endif

View File

@@ -11,6 +11,7 @@
#include <mbgl/util/constants.hpp>
#include <mbgl/util/uv_detail.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/style/style.hpp>
#include <mbgl/text/glyph_store.hpp>
#include <mbgl/geometry/glyph_atlas.hpp>
@@ -211,7 +212,7 @@ void Map::setStyleJSON(std::string newStyleJSON, const std::string &base) {
sprite.reset();
style->loadJSON((const uint8_t *)styleJSON.c_str());
fileSource->setBase(base);
glyphStore->setURL(style->glyph_url);
glyphStore->setURL(util::mapbox::normalizeGlyphsURL(style->glyph_url, getAccessToken()));
update();
}

View File

@@ -9,6 +9,7 @@
#include <mbgl/util/filesource.hpp>
#include <mbgl/util/vec.hpp>
#include <mbgl/util/std.hpp>
#include <mbgl/util/mapbox.hpp>
#include <mbgl/geometry/glyph_atlas.hpp>
#include <mbgl/style/style_layer.hpp>
#include <mbgl/platform/log.hpp>
@@ -23,21 +24,6 @@ Source::Source(SourceInfo& info)
{
}
std::string normalizeURL(const std::string& url, const std::string& access_token) {
const std::string t = "mapbox://";
if (url.compare(0, t.length(), t) != 0)
return url;
if (access_token.empty())
throw std::runtime_error("You must provide a Mapbox API access token for Mapbox tile sources");
return std::string("https://api.tiles.mapbox.com/v4/")
+ url.substr(t.length())
+ ".json?access_token="
+ access_token;
}
// Note: This is a separate function that must be called exactly once after creation
// The reason this isn't part of the constructor is that calling shared_from_this() in
// the constructor fails.
@@ -47,7 +33,7 @@ void Source::load(Map& map) {
return;
}
std::string url = normalizeURL(info.url, map.getAccessToken());
std::string url = util::mapbox::normalizeSourceURL(info.url, map.getAccessToken());
std::shared_ptr<Source> source = shared_from_this();
map.getFileSource()->load(ResourceType::JSON, url, [source, &map](platform::Response *res) {

View File

@@ -4,6 +4,7 @@
#include <mbgl/util/string.hpp>
#include <mbgl/util/utf.hpp>
#include <mbgl/util/pbf.hpp>
#include <mbgl/util/url.hpp>
#include <mbgl/util/constants.hpp>
#include <mbgl/util/token.hpp>
#include <mbgl/util/math.hpp>
@@ -141,14 +142,11 @@ GlyphPBF::GlyphPBF(const std::string &glyphURL, const std::string &fontStack, Gl
{
// Load the glyph set URL
std::string url = util::replaceTokens(glyphURL, [&](const std::string &name) -> std::string {
if (name == "fontstack") return fontStack;
if (name == "fontstack") return util::percentEncode(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(), ' ', '+');
#if defined(DEBUG)
fprintf(stderr, "%s\n", url.c_str());
#endif

44
src/util/mapbox.cpp Normal file
View File

@@ -0,0 +1,44 @@
#include <mbgl/util/mapbox.hpp>
#include <stdexcept>
namespace mbgl {
namespace util {
namespace mapbox {
const std::string mapbox = "mapbox://";
std::string normalizeURL(const std::string& url, const std::string& accessToken) {
if (accessToken.empty())
throw std::runtime_error("You must provide a Mapbox API access token for Mapbox tile sources");
return std::string("https://api.tiles.mapbox.com/v4/")
+ url.substr(mapbox.length())
+ "?access_token="
+ accessToken;
}
std::string normalizeSourceURL(const std::string& url, const std::string& accessToken) {
if (url.compare(0, mapbox.length(), mapbox) != 0)
return url;
std::string result = normalizeURL(url + ".json", accessToken);
// TileJSON requests need a secure flag appended to their URLs so
// that the server knows to send SSL-ified resource references.
if (url.compare(0, 5, "https") == 0)
result += "&secure";
return result;
}
std::string normalizeGlyphsURL(const std::string& url, const std::string& accessToken) {
if (url.compare(0, mapbox.length(), mapbox) != 0)
return url;
return normalizeURL(url, accessToken);
}
}
}
}

29
src/util/url.cpp Normal file
View File

@@ -0,0 +1,29 @@
#include <mbgl/util/url.hpp>
#include <cctype>
#include <iomanip>
#include <sstream>
#include <string>
namespace mbgl {
namespace util {
std::string percentEncode(const std::string& input) {
std::ostringstream encoded;
encoded.fill('0');
encoded << std::hex;
for (auto c : input) {
if (std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '~') {
encoded << c;
} else {
encoded << '%' << std::setw(2) << int(c);
}
}
return encoded.str();
}
}
}

View File

@@ -1,7 +1,7 @@
{
"version": 4,
"sprite": "img/sprite",
"glyphs": "https://mapbox.s3.amazonaws.com/gl-glyphs-256/{fontstack}/{range}.pbf",
"glyphs": "mapbox://fontstack/{fontstack}/{range}.pbf",
"constants": {
"@name": "{name_en}",
"@sans": "Open Sans Regular, Arial Unicode MS Regular",

View File

@@ -1,7 +1,7 @@
{
"version": 4,
"sprite": "https://www.mapbox.com/mapbox-gl-styles/sprites/outdoors",
"glyphs": "https://mapbox.s3.amazonaws.com/gl-glyphs-256/{fontstack}/{range}.pbf",
"glyphs": "mapbox://fontstack/{fontstack}/{range}.pbf",
"constants": {
"@land": "rgb(244,239,225)",
"@water": "#cdd",