From 8ac87ec5cb2374d212a31942ca19ce395704eb69 Mon Sep 17 00:00:00 2001 From: Mikhail Pozdnyakov Date: Mon, 19 Nov 2018 15:30:19 +0200 Subject: Refactor util::fontStacks() So that it does not downcast `Layer::Impl` instances. Also, an unneeded `std::vector` construction was removed from `Parser::fontStacks()`. --- src/mbgl/style/layer_impl.cpp | 2 ++ src/mbgl/style/layer_impl.hpp | 4 ++++ src/mbgl/style/layers/symbol_layer_impl.cpp | 27 ++++++++++++++++++++++++++ src/mbgl/style/layers/symbol_layer_impl.hpp | 1 + src/mbgl/style/parser.cpp | 5 ++--- src/mbgl/style/parser.hpp | 2 +- src/mbgl/util/font_stack.cpp | 30 +---------------------------- 7 files changed, 38 insertions(+), 33 deletions(-) (limited to 'src/mbgl') diff --git a/src/mbgl/style/layer_impl.cpp b/src/mbgl/style/layer_impl.cpp index a9a3941f3e..c6a248de90 100644 --- a/src/mbgl/style/layer_impl.cpp +++ b/src/mbgl/style/layer_impl.cpp @@ -9,5 +9,7 @@ Layer::Impl::Impl(LayerType type_, std::string layerID, std::string sourceID) source(std::move(sourceID)) { } +void Layer::Impl::populateFontStack(std::set&) const {} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layer_impl.hpp b/src/mbgl/style/layer_impl.hpp index 014fb60348..948bbab619 100644 --- a/src/mbgl/style/layer_impl.hpp +++ b/src/mbgl/style/layer_impl.hpp @@ -41,8 +41,12 @@ public: // Utility function for automatic layer grouping. virtual void stringifyLayout(rapidjson::Writer&) const = 0; + // Returns pointer to the statically allocated layer type info structure. virtual const LayerTypeInfo* getTypeInfo() const noexcept = 0; + // Populates the given \a fontStack with fonts being used by the layer. + virtual void populateFontStack(std::set& fontStack) const; + // Note: LayerType is deprecated, do not use it. const LayerType type; std::string id; diff --git a/src/mbgl/style/layers/symbol_layer_impl.cpp b/src/mbgl/style/layers/symbol_layer_impl.cpp index 753b2fa184..e177391686 100644 --- a/src/mbgl/style/layers/symbol_layer_impl.cpp +++ b/src/mbgl/style/layers/symbol_layer_impl.cpp @@ -1,5 +1,7 @@ #include +#include + namespace mbgl { namespace style { @@ -12,5 +14,30 @@ bool SymbolLayer::Impl::hasLayoutDifference(const Layer::Impl& other) const { paint.hasDataDrivenPropertyDifference(impl.paint); } +void SymbolLayer::Impl::populateFontStack(std::set& fontStack) const { + if (layout.get().isUndefined()) { + return; + } + + layout.get().match( + [&] (Undefined) { + fontStack.insert({"Open Sans Regular", "Arial Unicode MS Regular"}); + }, + [&] (const FontStack& constant) { + fontStack.insert(constant); + }, + [&] (const auto& function) { + for (const auto& value : function.possibleOutputs()) { + if (value) { + fontStack.insert(*value); + } else { + Log::Warning(Event::ParseStyle, "Layer '%s' has an invalid value for text-font and will not render text. Output values must be contained as literals within the expression.", id.c_str()); + break; + } + } + } + ); +} + } // namespace style } // namespace mbgl diff --git a/src/mbgl/style/layers/symbol_layer_impl.hpp b/src/mbgl/style/layers/symbol_layer_impl.hpp index 21e47ad89a..01ff9772b8 100644 --- a/src/mbgl/style/layers/symbol_layer_impl.hpp +++ b/src/mbgl/style/layers/symbol_layer_impl.hpp @@ -14,6 +14,7 @@ public: bool hasLayoutDifference(const Layer::Impl&) const override; void stringifyLayout(rapidjson::Writer&) const override; const LayerTypeInfo* getTypeInfo() const noexcept final; + void populateFontStack(std::set& fontStack) const final; SymbolLayoutProperties::Unevaluated layout; SymbolPaintProperties::Transitionable paint; diff --git a/src/mbgl/style/parser.cpp b/src/mbgl/style/parser.cpp index 77cf23bad0..ae298bd915 100644 --- a/src/mbgl/style/parser.cpp +++ b/src/mbgl/style/parser.cpp @@ -267,14 +267,13 @@ void Parser::parseLayer(const std::string& id, const JSValue& value, std::unique } } -std::vector Parser::fontStacks() const { +std::set Parser::fontStacks() const { std::vector> impls; impls.reserve(layers.size()); for (const auto& layer : layers) { impls.emplace_back(layer->baseImpl); } - std::set result = mbgl::fontStacks(impls); - return std::vector(result.begin(), result.end()); + return mbgl::fontStacks(impls); } } // namespace style diff --git a/src/mbgl/style/parser.hpp b/src/mbgl/style/parser.hpp index ec7be038fb..f992519083 100644 --- a/src/mbgl/style/parser.hpp +++ b/src/mbgl/style/parser.hpp @@ -43,7 +43,7 @@ public: double pitch = 0; // Statically evaluate layer properties to determine what font stacks are used. - std::vector fontStacks() const; + std::set fontStacks() const; private: void parseTransition(const JSValue&); diff --git a/src/mbgl/util/font_stack.cpp b/src/mbgl/util/font_stack.cpp index 4093a21793..362fdf1845 100644 --- a/src/mbgl/util/font_stack.cpp +++ b/src/mbgl/util/font_stack.cpp @@ -1,5 +1,4 @@ #include -#include #include #include @@ -23,35 +22,8 @@ FontStackHash FontStackHasher::operator()(const FontStack& fontStack) const { std::set fontStacks(const std::vector>& layers) { std::set result; - for (const auto& layer : layers) { - if (layer->type != LayerType::Symbol) { - continue; - } - - const SymbolLayer::Impl& impl = dynamic_cast(*layer); - if (impl.layout.get().isUndefined()) { - continue; - } - - impl.layout.get().match( - [&] (Undefined) { - result.insert({"Open Sans Regular", "Arial Unicode MS Regular"}); - }, - [&] (const FontStack& constant) { - result.insert(constant); - }, - [&] (const auto& function) { - for (const auto& value : function.possibleOutputs()) { - if (value) { - result.insert(*value); - } else { - Log::Warning(Event::ParseStyle, "Layer '%s' has an invalid value for text-font and will not render text. Output values must be contained as literals within the expression.", impl.id.c_str()); - break; - } - } - } - ); + layer->populateFontStack(result); } return result; -- cgit v1.2.1