From 2c615b613c3f4031cf7d40d37cee7ca5c6c96265 Mon Sep 17 00:00:00 2001 From: Ansis Brammanis Date: Wed, 14 Jan 2015 22:24:44 -0500 Subject: [PATCH] support dash arrays that are any length --- src/mbgl/geometry/line_atlas.cpp | 4 ++-- src/mbgl/geometry/line_atlas.hpp | 4 ++-- src/mbgl/renderer/painter_line.cpp | 5 ++--- src/mbgl/style/property_fallback.cpp | 2 -- src/mbgl/style/property_key.hpp | 2 -- src/mbgl/style/property_value.hpp | 5 ++++- src/mbgl/style/style_layer.cpp | 3 +-- src/mbgl/style/style_parser.cpp | 21 +++++++++++++++++++-- src/mbgl/style/style_properties.hpp | 3 ++- 9 files changed, 32 insertions(+), 17 deletions(-) diff --git a/src/mbgl/geometry/line_atlas.cpp b/src/mbgl/geometry/line_atlas.cpp index 28ab0ba45..ae5617bf3 100644 --- a/src/mbgl/geometry/line_atlas.cpp +++ b/src/mbgl/geometry/line_atlas.cpp @@ -17,7 +17,7 @@ LineAtlas::LineAtlas(uint16_t w, uint16_t h) LineAtlas::~LineAtlas() { } -LinePatternPos LineAtlas::getDashPosition(std::vector &dasharray, bool round) { +LinePatternPos LineAtlas::getDashPosition(const std::vector &dasharray, bool round) { std::ostringstream sskey; for (const float &part : dasharray) { @@ -34,7 +34,7 @@ LinePatternPos LineAtlas::getDashPosition(std::vector &dasharray, bool ro return positions[key]; } -LinePatternPos LineAtlas::addDash(std::vector &dasharray, bool round) { +LinePatternPos LineAtlas::addDash(const std::vector &dasharray, bool round) { int n = round ? 7 : 0; int dashheight = 2 * n + 1; diff --git a/src/mbgl/geometry/line_atlas.hpp b/src/mbgl/geometry/line_atlas.hpp index 6e4d8ab5b..800c047dc 100644 --- a/src/mbgl/geometry/line_atlas.hpp +++ b/src/mbgl/geometry/line_atlas.hpp @@ -21,8 +21,8 @@ public: void bind(); - LinePatternPos getDashPosition(std::vector&, bool); - LinePatternPos addDash(std::vector &dasharray, bool round); + LinePatternPos getDashPosition(const std::vector&, bool); + LinePatternPos addDash(const std::vector &dasharray, bool round); const int width; const int height; diff --git a/src/mbgl/renderer/painter_line.cpp b/src/mbgl/renderer/painter_line.cpp index 654d891d3..0aadd9219 100644 --- a/src/mbgl/renderer/painter_line.cpp +++ b/src/mbgl/renderer/painter_line.cpp @@ -70,7 +70,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c bucket.drawPoints(*linejoinShader); } - if (properties.dash_array.size() && properties.dash_array[1] > 0) { + if (properties.dash_array.size()) { useProgram(linesdfShader->program); @@ -81,8 +81,7 @@ void Painter::renderLine(LineBucket& bucket, util::ptr layer_desc, c linesdfShader->u_blur = blur; linesdfShader->u_color = color; - std::vector dash = { properties.dash_array[0], properties.dash_array[1] }; - LinePatternPos pos = lineAtlas.getDashPosition(dash, false); + LinePatternPos pos = lineAtlas.getDashPosition(properties.dash_array, false); lineAtlas.bind(); float patternratio = std::pow(2.0, std::floor(std::log2(state.getScale())) - id.z) / 8.0; diff --git a/src/mbgl/style/property_fallback.cpp b/src/mbgl/style/property_fallback.cpp index 965baf6c4..dc747b37e 100644 --- a/src/mbgl/style/property_fallback.cpp +++ b/src/mbgl/style/property_fallback.cpp @@ -20,8 +20,6 @@ const std::map PropertyFallbackValue::properties = { { PropertyKey::LineWidth, defaultStyleProperties().width }, { PropertyKey::LineGapWidth, defaultStyleProperties().gap_width }, { PropertyKey::LineBlur, defaultStyleProperties().blur }, - { PropertyKey::LineDashLand, defaultStyleProperties().dash_array[0] }, - { PropertyKey::LineDashGap, defaultStyleProperties().dash_array[1] }, { PropertyKey::IconOpacity, defaultStyleProperties().icon.opacity }, { PropertyKey::IconRotate, defaultStyleProperties().icon.rotate }, diff --git a/src/mbgl/style/property_key.hpp b/src/mbgl/style/property_key.hpp index efeebf024..e283f1157 100644 --- a/src/mbgl/style/property_key.hpp +++ b/src/mbgl/style/property_key.hpp @@ -24,8 +24,6 @@ enum class PropertyKey { LineGapWidth, LineBlur, LineDashArray, // for transitions only - LineDashLand, - LineDashGap, LineImage, IconOpacity, diff --git a/src/mbgl/style/property_value.hpp b/src/mbgl/style/property_value.hpp index 1b22b3117..b6ebcf3f6 100644 --- a/src/mbgl/style/property_value.hpp +++ b/src/mbgl/style/property_value.hpp @@ -5,6 +5,8 @@ #include #include +#include + namespace mbgl { typedef mapbox::util::variant< @@ -13,7 +15,8 @@ typedef mapbox::util::variant< RotateAnchorType, Function, Function, - Function + Function, + std::vector > PropertyValue; } diff --git a/src/mbgl/style/style_layer.cpp b/src/mbgl/style/style_layer.cpp index 024ac2f2c..54dfff300 100644 --- a/src/mbgl/style/style_layer.cpp +++ b/src/mbgl/style/style_layer.cpp @@ -185,8 +185,7 @@ void StyleLayer::applyStyleProperties(const float z, const times applyTransitionedStyleProperty(PropertyKey::LineWidth, line.width, z, now); applyTransitionedStyleProperty(PropertyKey::LineGapWidth, line.gap_width, z, now); applyTransitionedStyleProperty(PropertyKey::LineBlur, line.blur, z, now); - applyTransitionedStyleProperty(PropertyKey::LineDashLand, line.dash_array[0], z, now); - applyTransitionedStyleProperty(PropertyKey::LineDashGap, line.dash_array[1], z, now); + applyStyleProperty(PropertyKey::LineDashArray, line.dash_array, z, now); applyStyleProperty(PropertyKey::LineImage, line.image, z, now); // for scaling dasharrays diff --git a/src/mbgl/style/style_parser.cpp b/src/mbgl/style/style_parser.cpp index 928392a8d..b000fd5b5 100644 --- a/src/mbgl/style/style_parser.cpp +++ b/src/mbgl/style/style_parser.cpp @@ -332,6 +332,24 @@ template<> std::tuple StyleParser::parseProperty(JSVal value, return std::tuple { true, { value.GetString(), value.GetStringLength() } }; } +template<> std::tuple> StyleParser::parseProperty(JSVal value, const char *property_name) { + if (!value.IsArray()) { + Log::Warning(Event::ParseStyle, "value of '%s' must be an array", property_name); + return std::tuple> { false, 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, "value of '%s' must be an array of numbers", property_name); + return std::tuple> { false, std::vector() }; + } + vec.push_back(part.GetDouble()); + } + return std::tuple> { true, vec }; +} + template<> std::tuple StyleParser::parseProperty(JSVal value, const char *property_name) { if (!value.IsString()) { Log::Warning(Event::ParseStyle, "value of '%s' must be a string", property_name); @@ -567,8 +585,7 @@ void StyleParser::parsePaint(JSVal value, ClassProperties &klass) { 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::LineDashLand, Key::LineDashGap }, klass, value); - parseOptionalProperty("line-dasharray-transition", Key::LineDashArray, klass, value); + parseOptionalProperty>("line-dasharray", Key::LineDashArray, klass, value); parseOptionalProperty("line-image", Key::LineImage, klass, value); parseOptionalProperty>("icon-opacity", Key::IconOpacity, klass, value); diff --git a/src/mbgl/style/style_properties.hpp b/src/mbgl/style/style_properties.hpp index e50cd87d3..c5149b2d1 100644 --- a/src/mbgl/style/style_properties.hpp +++ b/src/mbgl/style/style_properties.hpp @@ -9,6 +9,7 @@ #include #include #include +#include namespace mbgl { @@ -36,7 +37,7 @@ struct LineProperties { float width = 1; float gap_width = 0; float blur = 0; - std::array dash_array = {{ 1, -1 }}; + std::vector dash_array; float dash_line_width = 1; std::string image;