From 7db9ce37e648e8145816f11dcdb3251f0ce81d74 Mon Sep 17 00:00:00 2001 From: Molly Lloyd Date: Thu, 13 Dec 2018 17:05:43 -0800 Subject: [core] add dynamic-text-anchor property --- include/mbgl/style/conversion/constant.hpp | 5 ++ include/mbgl/style/layers/symbol_layer.hpp | 8 +++ include/mbgl/style/types.hpp | 13 +++++ scripts/generate-style-code.js | 2 +- src/mbgl/style/conversion/constant.cpp | 22 ++++++++ src/mbgl/style/conversion/function.cpp | 2 + src/mbgl/style/conversion/property_value.cpp | 1 + src/mbgl/style/expression/value.cpp | 6 +++ src/mbgl/style/layers/symbol_layer.cpp | 65 ++++++++++++++++++++++- src/mbgl/style/layers/symbol_layer_properties.hpp | 12 +++++ src/mbgl/style/types.cpp | 13 +++++ 11 files changed, 147 insertions(+), 2 deletions(-) diff --git a/include/mbgl/style/conversion/constant.hpp b/include/mbgl/style/conversion/constant.hpp index 40657528c4..83785eed4e 100644 --- a/include/mbgl/style/conversion/constant.hpp +++ b/include/mbgl/style/conversion/constant.hpp @@ -54,6 +54,11 @@ struct Converter> { optional> operator()(const Convertible& value, Error& error) const; }; +template <> +struct Converter> { + optional> operator()(const Convertible& value, Error& error) const; +}; + } // namespace conversion } // namespace style } // namespace mbgl diff --git a/include/mbgl/style/layers/symbol_layer.hpp b/include/mbgl/style/layers/symbol_layer.hpp index 764f1585f6..eaba375ade 100644 --- a/include/mbgl/style/layers/symbol_layer.hpp +++ b/include/mbgl/style/layers/symbol_layer.hpp @@ -134,6 +134,14 @@ public: PropertyValue getTextJustify() const; void setTextJustify(PropertyValue); + static PropertyValue getDefaultDynamicTextOffset(); + PropertyValue getDynamicTextOffset() const; + void setDynamicTextOffset(PropertyValue); + + static PropertyValue> getDefaultDynamicTextAnchor(); + PropertyValue> getDynamicTextAnchor() const; + void setDynamicTextAnchor(PropertyValue>); + static PropertyValue getDefaultTextAnchor(); PropertyValue getTextAnchor() const; void setTextAnchor(PropertyValue); diff --git a/include/mbgl/style/types.hpp b/include/mbgl/style/types.hpp index ed875733c7..2a7635ff2d 100644 --- a/include/mbgl/style/types.hpp +++ b/include/mbgl/style/types.hpp @@ -98,6 +98,19 @@ enum class SymbolAnchorType : uint8_t { BottomRight }; +enum class DynamicTextAnchorType : uint8_t { + Auto, + Center, + Left, + Right, + Top, + Bottom, + TopLeft, + TopRight, + BottomLeft, + BottomRight +}; + enum class TextTransformType : uint8_t { None, Uppercase, diff --git a/scripts/generate-style-code.js b/scripts/generate-style-code.js index 121d2d774a..8c8d0c3979 100755 --- a/scripts/generate-style-code.js +++ b/scripts/generate-style-code.js @@ -49,7 +49,7 @@ global.evaluatedType = function (property) { if (property.length) { return `std::array<${evaluatedType({type: property.value})}, ${property.length}>`; } else { - return `std::vector<${evaluatedType({type: property.value})}>`; + return `std::vector<${evaluatedType({type: property.value, values: property.values, name: property.name})}>`; } default: throw new Error(`unknown type for ${property.name}`) } diff --git a/src/mbgl/style/conversion/constant.cpp b/src/mbgl/style/conversion/constant.cpp index bdc6371722..dc2a86d89b 100644 --- a/src/mbgl/style/conversion/constant.cpp +++ b/src/mbgl/style/conversion/constant.cpp @@ -64,6 +64,7 @@ template optional Converter::operator()(const template optional Converter::operator()(const Convertible&, Error&) const; template optional Converter::operator()(const Convertible&, Error&) const; template optional Converter::operator()(const Convertible&, Error&) const; +template optional Converter::operator()(const Convertible&, Error&) const; optional Converter::operator()(const Convertible& value, Error& error) const { optional string = toString(value); @@ -146,6 +147,27 @@ optional> Converter>::operator return result; } +optional> Converter>::operator()(const Convertible& value, Error& error) const { + if (!isArray(value)) { + error.message = "value must be an array"; + return nullopt; + } + + std::vector result; + result.reserve(arrayLength(value)); + + for (std::size_t i = 0; i < arrayLength(value); ++i) { + optional string = Converter{}(arrayMember(value, i), error); + if (!string) { + error.message = "value must be an array of strings"; + return nullopt; + } + result.push_back(*string); + } + + return result; +} + } // namespace conversion } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/conversion/function.cpp b/src/mbgl/style/conversion/function.cpp index 5877d0eb7c..76db1aadae 100644 --- a/src/mbgl/style/conversion/function.cpp +++ b/src/mbgl/style/conversion/function.cpp @@ -136,6 +136,8 @@ template optional>> convertFunctionToExpression>(const Convertible&, Error&, bool); template optional> convertFunctionToExpression(const Convertible&, Error&, bool); +template optional>> + convertFunctionToExpression>(const Convertible&, Error&, bool); template optional> convertFunctionToExpression(const Convertible&, Error&, bool); template optional> diff --git a/src/mbgl/style/conversion/property_value.cpp b/src/mbgl/style/conversion/property_value.cpp index ff038908b6..8d85f2596c 100644 --- a/src/mbgl/style/conversion/property_value.cpp +++ b/src/mbgl/style/conversion/property_value.cpp @@ -73,6 +73,7 @@ template optional> Converter> Converter>::operator()(conversion::Convertible const&, conversion::Error&, bool, bool) const; template optional> Converter>::operator()(conversion::Convertible const&, conversion::Error&, bool, bool) const; template optional> Converter>::operator()(conversion::Convertible const&, conversion::Error&, bool, bool) const; +template optional>> Converter>>::operator()(conversion::Convertible const&, conversion::Error&, bool, bool) const; template optional> Converter>::operator()(conversion::Convertible const&, conversion::Error&, bool, bool) const; template optional> Converter>::operator()(conversion::Convertible const&, conversion::Error&, bool, bool) const; template optional> Converter>::operator()(conversion::Convertible const&, conversion::Error&, bool, bool) const; diff --git a/src/mbgl/style/expression/value.cpp b/src/mbgl/style/expression/value.cpp index 436ed83ecd..c6757042a4 100644 --- a/src/mbgl/style/expression/value.cpp +++ b/src/mbgl/style/expression/value.cpp @@ -324,6 +324,9 @@ template struct ValueConverter>; template type::Type valueTypeToExpressionType>(); template struct ValueConverter>; +template type::Type valueTypeToExpressionType>(); +template struct ValueConverter>; + template type::Type valueTypeToExpressionType(); template struct ValueConverter; @@ -354,6 +357,9 @@ template struct ValueConverter; template type::Type valueTypeToExpressionType(); template struct ValueConverter; +template type::Type valueTypeToExpressionType(); +template struct ValueConverter; + template type::Type valueTypeToExpressionType(); template struct ValueConverter; diff --git a/src/mbgl/style/layers/symbol_layer.cpp b/src/mbgl/style/layers/symbol_layer.cpp index be25594f54..3cb5bd131a 100644 --- a/src/mbgl/style/layers/symbol_layer.cpp +++ b/src/mbgl/style/layers/symbol_layer.cpp @@ -491,6 +491,38 @@ void SymbolLayer::setTextJustify(PropertyValue value) { baseImpl = std::move(impl_); observer->onLayerChanged(*this); } +PropertyValue SymbolLayer::getDefaultDynamicTextOffset() { + return DynamicTextOffset::defaultValue(); +} + +PropertyValue SymbolLayer::getDynamicTextOffset() const { + return impl().layout.get(); +} + +void SymbolLayer::setDynamicTextOffset(PropertyValue value) { + if (value == getDynamicTextOffset()) + return; + auto impl_ = mutableImpl(); + impl_->layout.get() = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} +PropertyValue> SymbolLayer::getDefaultDynamicTextAnchor() { + return DynamicTextAnchor::defaultValue(); +} + +PropertyValue> SymbolLayer::getDynamicTextAnchor() const { + return impl().layout.get(); +} + +void SymbolLayer::setDynamicTextAnchor(PropertyValue> value) { + if (value == getDynamicTextAnchor()) + return; + auto impl_ = mutableImpl(); + impl_->layout.get() = value; + baseImpl = std::move(impl_); + observer->onLayerChanged(*this); +} PropertyValue SymbolLayer::getDefaultTextAnchor() { return TextAnchor::defaultValue(); } @@ -1438,6 +1470,8 @@ optional SymbolLayer::setLayoutProperty(const std::string& name, const Co TextLineHeight, TextLetterSpacing, TextJustify, + DynamicTextOffset, + DynamicTextAnchor, TextAnchor, TextMaxAngle, TextRotate, @@ -1615,6 +1649,18 @@ optional SymbolLayer::setLayoutProperty(const std::string& name, const Co } break; + case util::hashFNV1a("dynamic-text-offset"): + if (name == "dynamic-text-offset") { + property = Property::DynamicTextOffset; + } + break; + + case util::hashFNV1a("dynamic-text-anchor"): + if (name == "dynamic-text-anchor") { + property = Property::DynamicTextAnchor; + } + break; + case util::hashFNV1a("text-anchor"): if (name == "text-anchor") { property = Property::TextAnchor; @@ -1823,7 +1869,7 @@ optional SymbolLayer::setLayoutProperty(const std::string& name, const Co } - if (property == Property::IconSize || property == Property::IconRotate || property == Property::TextSize || property == Property::TextMaxWidth || property == Property::TextLetterSpacing || property == Property::TextRotate) { + if (property == Property::IconSize || property == Property::IconRotate || property == Property::TextSize || property == Property::TextMaxWidth || property == Property::TextLetterSpacing || property == Property::DynamicTextOffset || property == Property::TextRotate) { Error error; optional> typedValue = convert>(value, error, true, false); if (!typedValue) { @@ -1855,6 +1901,11 @@ optional SymbolLayer::setLayoutProperty(const std::string& name, const Co return nullopt; } + if (property == Property::DynamicTextOffset) { + setDynamicTextOffset(*typedValue); + return nullopt; + } + if (property == Property::TextRotate) { setTextRotate(*typedValue); return nullopt; @@ -1972,6 +2023,18 @@ optional SymbolLayer::setLayoutProperty(const std::string& name, const Co } + if (property == Property::DynamicTextAnchor) { + Error error; + optional>> typedValue = convert>>(value, error, false, false); + if (!typedValue) { + return error; + } + + setDynamicTextAnchor(*typedValue); + return nullopt; + + } + if (property == Property::TextTransform) { Error error; optional> typedValue = convert>(value, error, true, false); diff --git a/src/mbgl/style/layers/symbol_layer_properties.hpp b/src/mbgl/style/layers/symbol_layer_properties.hpp index 6c147f440d..5c34841162 100644 --- a/src/mbgl/style/layers/symbol_layer_properties.hpp +++ b/src/mbgl/style/layers/symbol_layer_properties.hpp @@ -147,6 +147,16 @@ struct TextJustify : DataDrivenLayoutProperty { static TextJustifyType defaultValue() { return TextJustifyType::Center; } }; +struct DynamicTextOffset : DataDrivenLayoutProperty { + static constexpr const char * key = "dynamic-text-offset"; + static float defaultValue() { return 0; } +}; + +struct DynamicTextAnchor : LayoutProperty> { + static constexpr const char * key = "dynamic-text-anchor"; + static std::vector defaultValue() { return { }; } +}; + struct TextAnchor : DataDrivenLayoutProperty { static constexpr const char * key = "text-anchor"; static SymbolAnchorType defaultValue() { return SymbolAnchorType::Center; } @@ -281,6 +291,8 @@ class SymbolLayoutProperties : public Properties< TextLineHeight, TextLetterSpacing, TextJustify, + DynamicTextOffset, + DynamicTextAnchor, TextAnchor, TextMaxAngle, TextRotate, diff --git a/src/mbgl/style/types.cpp b/src/mbgl/style/types.cpp index 51174cf152..26e3a58d97 100644 --- a/src/mbgl/style/types.cpp +++ b/src/mbgl/style/types.cpp @@ -77,6 +77,19 @@ MBGL_DEFINE_ENUM(SymbolAnchorType, { { SymbolAnchorType::BottomRight, "bottom-right" } }); +MBGL_DEFINE_ENUM(DynamicTextAnchorType, { + { DynamicTextAnchorType::Auto, "auto" }, + { DynamicTextAnchorType::Center, "center" }, + { DynamicTextAnchorType::Left, "left" }, + { DynamicTextAnchorType::Right, "right" }, + { DynamicTextAnchorType::Top, "top" }, + { DynamicTextAnchorType::Bottom, "bottom" }, + { DynamicTextAnchorType::TopLeft, "top-left" }, + { DynamicTextAnchorType::TopRight, "top-right" }, + { DynamicTextAnchorType::BottomLeft, "bottom-left" }, + { DynamicTextAnchorType::BottomRight, "bottom-right" } +}); + MBGL_DEFINE_ENUM(SymbolZOrderType, { { SymbolZOrderType::ViewportY, "viewport-y" }, { SymbolZOrderType::Source, "source" } -- cgit v1.2.1