Font stacks as arrays

This commit is contained in:
Tom MacWright
2015-03-12 16:44:33 -04:00
committed by John Firebaugh
parent 58d6b84b2b
commit 6df50dba45
3 changed files with 24 additions and 18 deletions

View File

@@ -473,24 +473,30 @@ StyleParser::Status StyleParser::parseOptionalProperty(const char *property_name
}
}
std::string normalizeFontStack(const std::string &name) {
namespace algo = boost::algorithm;
std::vector<std::string> parts;
algo::split(parts, name, algo::is_any_of(","), algo::token_compress_on);
std::for_each(parts.begin(), parts.end(), [](std::string& str) { algo::trim(str); });
return algo::join(parts, ", ");
}
template<> StyleParser::Result<std::string> StyleParser::parseProperty(JSVal value, const char *property_name) {
if (!value.IsString()) {
if (std::string { "text-font" } == property_name) {
if (!value.IsArray()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be an array of strings", property_name);
return Result<std::string> { StyleParserFailure, std::string() };
} else {
std::string result = "";
for (rapidjson::SizeType i = 0; i < value.Size(); ++i) {
JSVal stop = value[i];
if (stop.IsString()) {
result += stop.GetString();
if (i < value.Size()-1) {
result += ",";
}
} else {
Log::Warning(Event::ParseStyle, "text-font members must be strings");
return Result<std::string> { StyleParserFailure, {} };
}
}
return Result<std::string> { StyleParserSuccess, result };
}
} else if (!value.IsString()) {
Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name);
return Result<std::string> { StyleParserFailure, std::string() };
}
if (std::string { "text-font" } == property_name) {
return Result<std::string> {
StyleParserSuccess, normalizeFontStack({ value.GetString(), value.GetStringLength() })
};
} else {
return Result<std::string> { StyleParserSuccess, { value.GetString(), value.GetStringLength() } };
}

View File

@@ -20,7 +20,7 @@
"source": "vectorsource",
"source-layer": "road_label",
"layout": {
"text-font": "Open Sans Regular, Arial Unicode MS Regular",
"text-font": ["Open Sans Regular", "Arial Unicode MS Regular"],
"text-field": "{name_en}"
}
}, {
@@ -29,7 +29,7 @@
"source": "vectorsource",
"source-layer": "poi_label",
"layout": {
"text-font": "Open Sans Regular, Arial Unicode MS Regular",
"text-font": ["Open Sans Regular", "Arial Unicode MS Regular"],
"icon-image": "{maki}_icon"
}
}]

View File

@@ -31,7 +31,7 @@
"source-layer": "waterway",
"layout": {
"text-font": {
"stops": [[0, "Open Sans Regular"]]
"stops": [[0, ["Open Sans Regular"]]]
},
"text-keep-upright": {
"stops": [[0, false], [10, true]]