#include #include #include #include #include #include #include #include #include #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wshadow" #pragma GCC diagnostic ignored "-Wunknown-pragmas" #pragma GCC diagnostic ignored "-Wunused-local-typedefs" #include #pragma GCC diagnostic pop #include namespace mbgl { using JSVal = const rapidjson::Value&; StyleParser::StyleParser(MapData& data_) : data(data_) { } void StyleParser::parse(JSVal document) { if (document.HasMember("version")) { version = document["version"].GetInt(); if (version != 8) { Log::Warning(Event::ParseStyle, "current renderer implementation only supports style spec version 8; using an outdated style will cause rendering errors"); } } if (document.HasMember("sources")) { parseSources(document["sources"]); } if (document.HasMember("layers")) { parseLayers(document["layers"]); } if (document.HasMember("sprite")) { parseSprite(document["sprite"]); } if (document.HasMember("glyphs")) { parseGlyphURL(document["glyphs"]); } } #pragma mark - Parse Render Properties template<> StyleParser::Status StyleParser::parseRenderProperty(JSVal value, bool &target, const char *name) { if (value.HasMember(name)) { JSVal property = value[name]; if (property.IsBool()) { target = property.GetBool(); return StyleParserSuccess; } else { Log::Warning(Event::ParseStyle, "'%s' must be a boolean", name); } } return StyleParserFailure; } template<> StyleParser::Status StyleParser::parseRenderProperty(JSVal value, std::string &target, const char *name) { if (value.HasMember(name)) { JSVal property = value[name]; if (property.IsString()) { target = { property.GetString(), property.GetStringLength() }; return StyleParserSuccess; } else { Log::Warning(Event::ParseStyle, "'%s' must be a string", name); } } return StyleParserFailure; } template<> StyleParser::Status StyleParser::parseRenderProperty(JSVal value, float &target, const char *name) { if (value.HasMember(name)) { JSVal property = value[name]; if (property.IsNumber()) { target = property.GetDouble(); return StyleParserSuccess; } else { Log::Warning(Event::ParseStyle, "'%s' must be a number", name); } } return StyleParserFailure; } template<> StyleParser::Status StyleParser::parseRenderProperty(JSVal value, uint16_t &target, const char *name) { if (value.HasMember(name)) { JSVal property = value[name]; if (property.IsUint()) { unsigned int int_value = property.GetUint(); if (int_value > std::numeric_limits::max()) { Log::Warning(Event::ParseStyle, "values for %s that are larger than %d are not supported", name, std::numeric_limits::max()); return StyleParserFailure; } target = int_value; return StyleParserSuccess; } else { Log::Warning(Event::ParseStyle, "%s must be an unsigned integer", name); } } return StyleParserFailure; } template<> StyleParser::Status StyleParser::parseRenderProperty(JSVal value, int32_t &target, const char *name) { if (value.HasMember(name)) { JSVal property = value[name]; if (property.IsInt()) { target = property.GetInt(); return StyleParserSuccess; } else { Log::Warning(Event::ParseStyle, "%s must be an integer", name); } } return StyleParserFailure; } template<> StyleParser::Status StyleParser::parseRenderProperty(JSVal value, vec2 &target, const char *name) { if (value.HasMember(name)) { JSVal property = value[name]; if (property.IsArray()) { if (property.Size() >= 2) { target.x = property[(rapidjson::SizeType)0].GetDouble(); target.y = property[(rapidjson::SizeType)1].GetDouble(); return StyleParserSuccess; } else { Log::Warning(Event::ParseStyle, "%s must have at least two members", name); } } else { Log::Warning(Event::ParseStyle, "%s must be an array of numbers", name); } } return StyleParserFailure; } template StyleParser::Status StyleParser::parseRenderProperty(JSVal value, T &target, const char *name) { if (value.HasMember(name)) { JSVal property = value[name]; if (property.IsString()) { target = Parser({ property.GetString(), property.GetStringLength() }); return StyleParserSuccess; } else { Log::Warning(Event::ParseStyle, "%s must have one of the enum values", name); } } return StyleParserFailure; } #pragma mark - Parse Sources void StyleParser::parseSources(JSVal value) { if (value.IsObject()) { rapidjson::Value::ConstMemberIterator itr = value.MemberBegin(); for (; itr != value.MemberEnd(); ++itr) { std::string name { itr->name.GetString(), itr->name.GetStringLength() }; std::unique_ptr source = std::make_unique(); parseRenderProperty(itr->value, source->info.type, "type"); parseRenderProperty(itr->value, source->info.url, "url"); parseRenderProperty(itr->value, source->info.tile_size, "tileSize"); source->info.source_id = name; source->info.parseTileJSONProperties(itr->value); sourcesMap.emplace(name, source.get()); sources.emplace_back(std::move(source)); } } else { Log::Warning(Event::ParseStyle, "sources must be an object"); } } #pragma mark - Parse Style Properties Color parseColor(JSVal value) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "color value must be a string"); return Color{{ 0, 0, 0, 0 }}; } CSSColorParser::Color css_color = CSSColorParser::parse({ value.GetString(), value.GetStringLength() }); // Premultiply the color. const float factor = css_color.a / 255; return Color{{(float)css_color.r * factor, (float)css_color.g * factor, (float)css_color.b * factor, css_color.a}}; } StyleParser::Result> StyleParser::parseFloatArray(JSVal value) { if (!value.IsArray()) { Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers"); return Result> { StyleParserFailure, std::vector() }; } std::vector vec; for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { JSVal part = value[i]; if (!part.IsNumber()) { Log::Warning(Event::ParseStyle, "dasharray value must be an array of numbers"); return Result> { StyleParserFailure, std::vector() }; } vec.push_back(part.GetDouble()); } return Result> { StyleParserSuccess, vec }; } template <> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char*) { if (value.IsArray() && value.Size() == 2 && value[rapidjson::SizeType(0)].IsNumber() && value[rapidjson::SizeType(1)].IsNumber()) { float first = value[rapidjson::SizeType(0)].GetDouble(); float second = value[rapidjson::SizeType(1)].GetDouble(); return Result> { StyleParserSuccess, {{ first, second }} }; } else { Log::Warning(Event::ParseStyle, "value must be array of two numbers"); return Result> { StyleParserFailure, {{ 0.0f, 0.0f }} }; } } template <> StyleParser::Result StyleParser::parseProperty(JSVal value, const char* property_name) { if (value.IsNumber()) { return Result { StyleParserSuccess, value.GetDouble() }; } else { Log::Warning(Event::ParseStyle, "value of '%s' must be a number, or a number function", property_name); return Result { StyleParserFailure, 0.0f }; } } template <> StyleParser::Result StyleParser::parseProperty(JSVal value, const char*) { return Result { StyleParserSuccess, parseColor(value) }; } template <> StyleParser::Result>> StyleParser::parseProperty(JSVal value, const char*) { Faded> parsed; parsed.to = std::get<1>(parseFloatArray(value)); return Result>> { StyleParserSuccess, parsed }; } template <> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { Faded parsed; if (value.IsString()) { parsed.to = { value.GetString(), value.GetStringLength() }; return Result> { StyleParserSuccess, parsed }; } else { Log::Warning(Event::ParseStyle, "value of '%s' must be a string, or a string function", property_name); return Result> { StyleParserFailure, parsed }; } } template StyleParser::Result>> StyleParser::parseStops(JSVal value_stops, const char *property_name) { if (!value_stops.IsArray()) { Log::Warning(Event::ParseStyle, "stops function must specify a stops array"); return Result>> { StyleParserFailure, {}}; } std::vector> stops; for (rapidjson::SizeType i = 0; i < value_stops.Size(); ++i) { JSVal stop = value_stops[i]; if (stop.IsArray()) { if (stop.Size() != 2) { Log::Warning(Event::ParseStyle, "stop must have zoom level and value specification"); return Result>> { StyleParserFailure, {}}; } JSVal z = stop[rapidjson::SizeType(0)]; if (!z.IsNumber()) { Log::Warning(Event::ParseStyle, "zoom level in stop must be a number"); return Result>> { StyleParserFailure, {}}; } stops.emplace_back(z.GetDouble(), std::get<1>(parseProperty(stop[rapidjson::SizeType(1)], property_name))); } else { Log::Warning(Event::ParseStyle, "function argument must be a numeric value"); return Result>> { StyleParserFailure, {}}; } } return Result>>(StyleParserSuccess, stops); } template StyleParser::Result> StyleParser::parseFunction(JSVal value, const char *property_name) { if (!value.IsObject()) { return parseProperty(value, property_name); } if (!value.HasMember("stops")) { Log::Warning(Event::ParseStyle, "function must specify a function type"); return Result> { StyleParserFailure, ConstantFunction(T()) }; } float base = 1.0f; if (value.HasMember("base")) { JSVal value_base = value["base"]; if (value_base.IsNumber()) { base = value_base.GetDouble(); } else { Log::Warning(Event::ParseStyle, "base must be numeric"); } } auto stops = parseStops(value["stops"], property_name); if (!std::get<0>(stops)) { return Result> { StyleParserFailure, ConstantFunction(T()) }; } return Result> { StyleParserSuccess, StopsFunction(std::get<1>(stops), base) }; } template StyleParser::Result> StyleParser::parsePiecewiseConstantFunction(JSVal value, Duration duration) { if (!value.HasMember("stops")) { Log::Warning(Event::ParseStyle, "function must specify a function type"); return Result> { StyleParserFailure, { {}, duration } }; } auto stops = parseStops(value["stops"], ""); if (!std::get<0>(stops)) { return Result> { StyleParserFailure, { {}, duration } }; } return Result> { StyleParserSuccess, { std::get<1>(stops), duration } }; } template StyleParser::Status StyleParser::setProperty(JSVal value, const char *property_name, PropertyKey key, ClassProperties &klass) { auto res = parseProperty(value, property_name); if (std::get<0>(res)) { klass.set(key, std::get<1>(res)); } return std::get<0>(res); } template StyleParser::Status StyleParser::setProperty(JSVal value, const char *property_name, PropertyKey key, ClassProperties &klass, JSVal transition) { auto res = parseProperty(value, property_name, transition); if (std::get<0>(res)) { klass.set(key, std::get<1>(res)); } return std::get<0>(res); } template void StyleParser::parseVisibility(StyleBucket &bucket, JSVal value) { if (!value.HasMember("visibility")) { return; } else if (!value["visibility"].IsString()) { Log::Warning(Event::ParseStyle, "value of 'visibility' must be a string"); bucket.visibility = VisibilityType::Visible; return; } bucket.visibility = VisibilityTypeClass({ value["visibility"].GetString(), value["visibility"].GetStringLength() }); } template StyleParser::Status StyleParser::parseOptionalProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value) { if (!value.HasMember(property_name)) { return StyleParserFailure; } else { return setProperty(value[property_name], property_name, key, klass); } } template StyleParser::Status StyleParser::parseOptionalProperty(const char *property_name, PropertyKey key, ClassProperties &klass, JSVal value, const char *transition_name) { if (!value.HasMember(property_name)) { return StyleParserFailure; } else { if (value.HasMember(transition_name)) { return setProperty(value[property_name], property_name, key, klass, value[transition_name]); } else { JSVal val = JSVal { rapidjson::kObjectType }; return setProperty(value[property_name], property_name, key, klass, val); } } } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { 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 { 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 { StyleParserFailure, {} }; } } return Result { StyleParserSuccess, result }; } } else if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, std::string() }; } else { return Result { StyleParserSuccess, { value.GetString(), value.GetStringLength() } }; } } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsBool()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a boolean", property_name); return Result { StyleParserFailure, StyleParserSuccess }; } return Result { StyleParserSuccess, value.GetBool() }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, TranslateAnchorType::Map }; } return Result { StyleParserSuccess, TranslateAnchorTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, RotateAnchorType::Map }; } return Result { StyleParserSuccess, RotateAnchorTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, CapType::Butt }; } return Result { StyleParserSuccess, CapTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, JoinType::Miter }; } return Result { StyleParserSuccess, JoinTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, PlacementType::Point }; } return Result { StyleParserSuccess, PlacementTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, TextAnchorType::Center }; } return Result { StyleParserSuccess, TextAnchorTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, TextJustifyType::Center }; } return Result { StyleParserSuccess, TextJustifyTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, TextTransformType::None }; } return Result { StyleParserSuccess, TextTransformTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); return Result { StyleParserFailure, RotationAlignmentType::Map }; } return Result { StyleParserSuccess, RotationAlignmentTypeClass({ value.GetString(), value.GetStringLength() }) }; } template<> StyleParser::Result StyleParser::parseProperty(JSVal value, const char */*property_name*/) { PropertyTransition transition { data.getDefaultTransitionDuration(), data.getDefaultTransitionDelay() }; if (value.IsObject()) { bool parsed = false; if (value.HasMember("duration") && value["duration"].IsNumber() && data.mode == MapMode::Continuous) { transition.duration = std::chrono::milliseconds(value["duration"].GetUint()); parsed = true; } if (value.HasMember("delay") && value["delay"].IsNumber() && data.mode == MapMode::Continuous) { transition.delay = std::chrono::milliseconds(value["delay"].GetUint()); parsed = true; } if (!parsed) { return Result { StyleParserFailure, std::move(transition) }; } } return Result { StyleParserSuccess, std::move(transition) }; } template<> StyleParser::Result>> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction>(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result> StyleParser::parseProperty(JSVal value, const char *property_name) { return parseFunction(value, property_name); } template<> StyleParser::Result>>> StyleParser::parseProperty(JSVal value, const char *property_name, JSVal transition) { Duration duration = data.getDefaultFadeDuration(); if (transition.HasMember("duration") && data.mode == MapMode::Continuous) { duration = std::chrono::milliseconds(transition["duration"].GetUint()); } if (value.IsObject()) { return parsePiecewiseConstantFunction>>(value, duration); } else if (value.IsArray()) { Faded> parsed; Result> floatarray = parseFloatArray(value); parsed.to = std::get<1>(floatarray); return Result>>> { std::get<0>(floatarray), { parsed, duration } }; } else { Log::Warning(Event::ParseStyle, "value of '%s' must be an array of numbers, or a number array function", property_name); return Result>>> { StyleParserFailure, { {}, duration } }; } } template<> StyleParser::Result>> StyleParser::parseProperty(JSVal value, const char *property_name, JSVal transition) { Duration duration = data.getDefaultFadeDuration(); if (transition.HasMember("duration") && data.mode == MapMode::Continuous) { duration = std::chrono::milliseconds(transition["duration"].GetUint()); } if (value.IsObject()) { return parsePiecewiseConstantFunction>(value, duration); } else if (value.IsString()) { Faded parsed; parsed.to = { value.GetString(), value.GetStringLength() }; return Result>> { StyleParserSuccess, { parsed, duration } }; } else { Log::Warning(Event::ParseStyle, "value of '%s' must be string or a string function", property_name); return Result>> { StyleParserFailure, { {}, duration } }; } } #pragma mark - Parse Layers void StyleParser::parseLayers(JSVal value) { std::vector ids; if (!value.IsArray()) { Log::Warning(Event::ParseStyle, "layers must be an array"); return; } for (rapidjson::SizeType i = 0; i < value.Size(); ++i) { JSVal layerValue = value[i]; if (!layerValue.IsObject()) { Log::Warning(Event::ParseStyle, "layer must be an object"); continue; } if (!layerValue.HasMember("id")) { Log::Warning(Event::ParseStyle, "layer must have an id"); continue; } JSVal id = layerValue["id"]; if (!id.IsString()) { Log::Warning(Event::ParseStyle, "layer id must be a string"); continue; } const std::string layerID = { id.GetString(), id.GetStringLength() }; if (layersMap.find(layerID) != layersMap.end()) { Log::Warning(Event::ParseStyle, "duplicate layer id %s", layerID.c_str()); continue; } layersMap.emplace(layerID, std::pair> { layerValue, nullptr }); ids.push_back(layerID); } for (const auto& id : ids) { auto it = layersMap.find(id); parseLayer(it->first, it->second.first, it->second.second); if (it->second.second) { layers.emplace_back(it->second.second); } } } void StyleParser::parseLayer(const std::string& id, JSVal value, util::ptr& layer) { if (layer) { // Skip parsing this again. We already have a valid layer definition. return; } // Make sure we have not previously attempted to parse this layer. if (std::find(stack.begin(), stack.end(), id) != stack.end()) { Log::Warning(Event::ParseStyle, "layer reference of '%s' is circular", id.c_str()); return; } if (value.HasMember("ref")) { // This layer is referencing another layer. Recursively parse that layer. JSVal refVal = value["ref"]; if (!refVal.IsString()) { Log::Warning(Event::ParseStyle, "layer ref of '%s' must be a string", id.c_str()); return; } const std::string ref { refVal.GetString(), refVal.GetStringLength() }; auto it = layersMap.find(ref); if (it == layersMap.end()) { Log::Warning(Event::ParseStyle, "layer '%s' references unknown layer %s", id.c_str(), ref.c_str()); return; } // Recursively parse the referenced layer. stack.push_front(id); parseLayer(it->first, it->second.first, it->second.second); stack.pop_front(); util::ptr reference = it->second.second; if (!reference) { return; } layer = StyleLayer::create(reference->type); layer->id = id; layer->type = reference->type; layer->bucket = reference->bucket; } else { // Otherwise, parse the source/source-layer/filter/render keys to form the bucket. if (!value.HasMember("type")) { Log::Warning(Event::ParseStyle, "layer '%s' is missing a type", id.c_str()); return; } JSVal typeVal = value["type"]; if (!typeVal.IsString()) { Log::Warning(Event::ParseStyle, "layer '%s' has an invalid type", id.c_str()); return; } std::string type { typeVal.GetString(), typeVal.GetStringLength() }; StyleLayerType typeClass = StyleLayerTypeClass(type); layer = StyleLayer::create(typeClass); if (!layer) { Log::Warning(Event::ParseStyle, "unknown type '%s' for layer '%s'", type.c_str(), id.c_str()); return; } layer->id = id; layer->type = typeClass; util::ptr bucket = std::make_shared(layer->type); // We name the buckets according to the layer that defined it. bucket->name = layer->id; if (value.HasMember("source")) { JSVal value_source = value["source"]; if (value_source.IsString()) { bucket->source = { value_source.GetString(), value_source.GetStringLength() }; auto source_it = sourcesMap.find(bucket->source); if (source_it == sourcesMap.end()) { Log::Warning(Event::ParseStyle, "can't find source '%s' required for layer '%s'", bucket->source.c_str(), layer->id.c_str()); } } else { Log::Warning(Event::ParseStyle, "source of layer '%s' must be a string", layer->id.c_str()); } } if (value.HasMember("source-layer")) { JSVal value_source_layer = value["source-layer"]; if (value_source_layer.IsString()) { bucket->source_layer = { value_source_layer.GetString(), value_source_layer.GetStringLength() }; } else { Log::Warning(Event::ParseStyle, "source-layer of layer '%s' must be a string", layer->id.c_str()); } } if (value.HasMember("filter")) { bucket->filter = parseFilterExpression(value["filter"]); } if (value.HasMember("layout")) { parseLayout(value["layout"], bucket); } if (value.HasMember("minzoom")) { JSVal min_zoom = value["minzoom"]; if (min_zoom.IsNumber()) { bucket->min_zoom = min_zoom.GetDouble(); } else { Log::Warning(Event::ParseStyle, "minzoom of layer %s must be numeric", layer->id.c_str()); } } if (value.HasMember("maxzoom")) { JSVal max_zoom = value["maxzoom"]; if (max_zoom.IsNumber()) { bucket->max_zoom = max_zoom.GetDouble(); } else { Log::Warning(Event::ParseStyle, "maxzoom of layer %s must be numeric", layer->id.c_str()); } } layer->bucket = bucket; } std::map paints; parsePaints(value, paints); layer->styles = std::move(paints); } #pragma mark - Parse Styles void StyleParser::parsePaints(JSVal value, std::map &paints) { rapidjson::Value::ConstMemberIterator itr = value.MemberBegin(); for (; itr != value.MemberEnd(); ++itr) { const std::string name { itr->name.GetString(), itr->name.GetStringLength() }; if (name == "paint") { parsePaint(itr->value, paints[ClassID::Default]); } else if (name.compare(0, 6, "paint.") == 0 && name.length() > 6) { const ClassID class_id = ClassDictionary::Get().lookup(name.substr(6)); parsePaint(itr->value, paints[class_id]); } } } void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { using Key = PropertyKey; parseOptionalProperty>("fill-antialias", Key::FillAntialias, klass, value); parseOptionalProperty>("fill-opacity", Key::FillOpacity, klass, value); parseOptionalProperty("fill-opacity-transition", Key::FillOpacity, klass, value); parseOptionalProperty>("fill-color", Key::FillColor, klass, value); parseOptionalProperty("fill-color-transition", Key::FillColor, klass, value); parseOptionalProperty>("fill-outline-color", Key::FillOutlineColor, klass, value); parseOptionalProperty("fill-outline-color-transition", Key::FillOutlineColor, klass, value); parseOptionalProperty>>("fill-translate", Key::FillTranslate, klass, value); parseOptionalProperty("fill-translate-transition", Key::FillTranslate, klass, value); parseOptionalProperty>("fill-translate-anchor", Key::FillTranslateAnchor, klass, value); parseOptionalProperty>>("fill-pattern", Key::FillImage, klass, value, "fill-pattern-transition"); parseOptionalProperty>("line-opacity", Key::LineOpacity, klass, value); parseOptionalProperty("line-opacity-transition", Key::LineOpacity, klass, value); parseOptionalProperty>("line-color", Key::LineColor, klass, value); parseOptionalProperty("line-color-transition", Key::LineColor, klass, value); parseOptionalProperty>>("line-translate", Key::LineTranslate, klass, value); parseOptionalProperty("line-translate-transition", Key::LineTranslate, klass, value); parseOptionalProperty>("line-translate-anchor", Key::LineTranslateAnchor, klass, value); parseOptionalProperty>("line-width", Key::LineWidth, klass, value); parseOptionalProperty("line-width-transition", Key::LineWidth, klass, value); parseOptionalProperty>("line-gap-width", Key::LineGapWidth, klass, value); parseOptionalProperty("line-gap-width-transition", Key::LineGapWidth, klass, value); parseOptionalProperty>("line-blur", Key::LineBlur, klass, value); parseOptionalProperty("line-blur-transition", Key::LineBlur, klass, value); parseOptionalProperty>>>("line-dasharray", Key::LineDashArray, klass, value, "line-dasharray-transition"); parseOptionalProperty>>("line-pattern", Key::LineImage, klass, value, "line-pattern-transition"); parseOptionalProperty>("circle-radius", Key::CircleRadius, klass, value); parseOptionalProperty>("circle-color", Key::CircleColor, klass, value); parseOptionalProperty>("circle-opacity", Key::CircleOpacity, klass, value); parseOptionalProperty>>("circle-translate", Key::CircleTranslate, klass, value); parseOptionalProperty>("circle-translate-anchor", Key::CircleTranslateAnchor, klass, value); parseOptionalProperty>("circle-blur", Key::CircleBlur, klass, value); parseOptionalProperty>("icon-opacity", Key::IconOpacity, klass, value); parseOptionalProperty("icon-opacity-transition", Key::IconOpacity, klass, value); parseOptionalProperty>("icon-size", Key::IconSize, klass, value); parseOptionalProperty("icon-size-transition", Key::IconSize, klass, value); parseOptionalProperty>("icon-color", Key::IconColor, klass, value); parseOptionalProperty("icon-color-transition", Key::IconColor, klass, value); parseOptionalProperty>("icon-halo-color", Key::IconHaloColor, klass, value); parseOptionalProperty("icon-halo-color-transition", Key::IconHaloColor, klass, value); parseOptionalProperty>("icon-halo-width", Key::IconHaloWidth, klass, value); parseOptionalProperty("icon-halo-width-transition", Key::IconHaloWidth, klass, value); parseOptionalProperty>("icon-halo-blur", Key::IconHaloBlur, klass, value); parseOptionalProperty("icon-halo-blur-transition", Key::IconHaloBlur, klass, value); parseOptionalProperty>>("icon-translate", Key::IconTranslate, klass, value); parseOptionalProperty("icon-translate-transition", Key::IconTranslate, klass, value); parseOptionalProperty>("icon-translate-anchor", Key::IconTranslateAnchor, klass, value); parseOptionalProperty>("text-opacity", Key::TextOpacity, klass, value); parseOptionalProperty("text-opacity-transition", Key::TextOpacity, klass, value); parseOptionalProperty>("text-size", Key::TextSize, klass, value); parseOptionalProperty("text-size-transition", Key::TextSize, klass, value); parseOptionalProperty>("text-color", Key::TextColor, klass, value); parseOptionalProperty("text-color-transition", Key::TextColor, klass, value); parseOptionalProperty>("text-halo-color", Key::TextHaloColor, klass, value); parseOptionalProperty("text-halo-color-transition", Key::TextHaloColor, klass, value); parseOptionalProperty>("text-halo-width", Key::TextHaloWidth, klass, value); parseOptionalProperty("text-halo-width-transition", Key::TextHaloWidth, klass, value); parseOptionalProperty>("text-halo-blur", Key::TextHaloBlur, klass, value); parseOptionalProperty("text-halo-blur-transition", Key::TextHaloBlur, klass, value); parseOptionalProperty>>("text-translate", Key::TextTranslate, klass, value); parseOptionalProperty("text-translate-transition", Key::TextTranslate, klass, value); parseOptionalProperty>("text-translate-anchor", Key::TextTranslateAnchor, klass, value); parseOptionalProperty>("raster-opacity", Key::RasterOpacity, klass, value); parseOptionalProperty("raster-opacity-transition", Key::RasterOpacity, klass, value); parseOptionalProperty>("raster-hue-rotate", Key::RasterHueRotate, klass, value); parseOptionalProperty("raster-hue-rotate-transition", Key::RasterHueRotate, klass, value); parseOptionalProperty>("raster-brightness-min", Key::RasterBrightnessLow, klass, value); parseOptionalProperty>("raster-brightness-max", Key::RasterBrightnessHigh, klass, value); parseOptionalProperty("raster-brightness-transition", Key::RasterBrightness, klass, value); parseOptionalProperty>("raster-saturation", Key::RasterSaturation, klass, value); parseOptionalProperty("raster-saturation-transition", Key::RasterSaturation, klass, value); parseOptionalProperty>("raster-contrast", Key::RasterContrast, klass, value); parseOptionalProperty("raster-contrast-transition", Key::RasterContrast, klass, value); parseOptionalProperty>("raster-fade-duration", Key::RasterFade, klass, value); parseOptionalProperty("raster-fade-duration-transition", Key::RasterFade, klass, value); parseOptionalProperty>("background-opacity", Key::BackgroundOpacity, klass, value); parseOptionalProperty>("background-color", Key::BackgroundColor, klass, value); parseOptionalProperty>>("background-pattern", Key::BackgroundImage, klass, value, "background-pattern-transition"); } void StyleParser::parseLayout(JSVal value, util::ptr &bucket) { using Key = PropertyKey; parseVisibility(*bucket, value); parseOptionalProperty>("line-cap", Key::LineCap, bucket->layout, value); parseOptionalProperty>("line-join", Key::LineJoin, bucket->layout, value); parseOptionalProperty>("line-miter-limit", Key::LineMiterLimit, bucket->layout, value); parseOptionalProperty>("line-round-limit", Key::LineRoundLimit, bucket->layout, value); parseOptionalProperty>("symbol-placement", Key::SymbolPlacement, bucket->layout, value); parseOptionalProperty>("symbol-spacing", Key::SymbolSpacing, bucket->layout, value); parseOptionalProperty>("symbol-avoid-edges", Key::SymbolAvoidEdges, bucket->layout, value); parseOptionalProperty>("icon-allow-overlap", Key::IconAllowOverlap, bucket->layout, value); parseOptionalProperty>("icon-ignore-placement", Key::IconIgnorePlacement, bucket->layout, value); parseOptionalProperty>("icon-optional", Key::IconOptional, bucket->layout, value); parseOptionalProperty>("icon-rotation-alignment", Key::IconRotationAlignment, bucket->layout, value); parseOptionalProperty>("icon-size", Key::IconSize, bucket->layout, value); parseOptionalProperty>("icon-image", Key::IconImage, bucket->layout, value); parseOptionalProperty>("icon-rotate", Key::IconRotate, bucket->layout, value); parseOptionalProperty>("icon-padding", Key::IconPadding, bucket->layout, value); parseOptionalProperty>("icon-keep-upright", Key::IconKeepUpright, bucket->layout, value); parseOptionalProperty>>("icon-offset", Key::IconOffset, bucket->layout, value); parseOptionalProperty>("text-rotation-alignment", Key::TextRotationAlignment, bucket->layout, value); parseOptionalProperty>("text-field", Key::TextField, bucket->layout, value); parseOptionalProperty>("text-font", Key::TextFont, bucket->layout, value); parseOptionalProperty>("text-size", Key::TextSize, bucket->layout, value); parseOptionalProperty>("text-max-width", Key::TextMaxWidth, bucket->layout, value); parseOptionalProperty>("text-line-height", Key::TextLineHeight, bucket->layout, value); parseOptionalProperty>("text-letter-spacing", Key::TextLetterSpacing, bucket->layout, value); parseOptionalProperty>("text-justify", Key::TextJustify, bucket->layout, value); parseOptionalProperty>("text-anchor", Key::TextAnchor, bucket->layout, value); parseOptionalProperty>("text-max-angle", Key::TextMaxAngle, bucket->layout, value); parseOptionalProperty>("text-rotate", Key::TextRotate, bucket->layout, value); parseOptionalProperty>("text-padding", Key::TextPadding, bucket->layout, value); parseOptionalProperty>("text-keep-upright", Key::TextKeepUpright, bucket->layout, value); parseOptionalProperty>("text-transform", Key::TextTransform, bucket->layout, value); parseOptionalProperty>>("text-offset", Key::TextOffset, bucket->layout, value); parseOptionalProperty>("text-allow-overlap", Key::TextAllowOverlap, bucket->layout, value); parseOptionalProperty>("text-ignore-placement", Key::TextIgnorePlacement, bucket->layout, value); parseOptionalProperty>("text-optional", Key::TextOptional, bucket->layout, value); } void StyleParser::parseSprite(JSVal value) { if (value.IsString()) { sprite = { value.GetString(), value.GetStringLength() }; } } void StyleParser::parseGlyphURL(JSVal value) { if (value.IsString()) { glyph_url = { value.GetString(), value.GetStringLength() }; } } }