From 794b851f02b98c5f1652edc141b1a6feb459d59b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=81ukasz=20Paczos?= Date: Tue, 13 Nov 2018 18:29:25 +0100 Subject: [android] expose Formatted text-field setter --- src/mbgl/style/expression/formatted.cpp | 60 +++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) (limited to 'src/mbgl/style/expression') diff --git a/src/mbgl/style/expression/formatted.cpp b/src/mbgl/style/expression/formatted.cpp index 6eb106dfec..8232d0c698 100644 --- a/src/mbgl/style/expression/formatted.cpp +++ b/src/mbgl/style/expression/formatted.cpp @@ -44,13 +44,67 @@ namespace conversion { using namespace mbgl::style::expression; -optional Converter::operator()(const Convertible& value, Error&) const { +optional Converter::operator()(const Convertible& value, Error& error) const { using namespace mbgl::style::expression; - auto result = toString(value); - if (result) { + if (isArray(value)) { + std::vector sections; + for (std::size_t i = 0; i < arrayLength(value); ++i) { + Convertible section = arrayMember(value, i); + std::size_t sectionLength = arrayLength(section); + if (sectionLength < 1) { + error.message = "Section has to contain a text and optional parameters."; + return nullopt; + } + + optional sectionText = toString(arrayMember(section, 0)); + if (!sectionText) { + error.message = "Section has to contain a text."; + return nullopt; + } + + optional fontScale; + optional textFont; + if (sectionLength > 1) { + Convertible sectionParams = arrayMember(section, 1); + if (!isObject(sectionParams)) { + error.message = "Parameters have to be enclosed in an object."; + return nullopt; + } + + optional fontScaleMember = objectMember(sectionParams, "font-scale"); + if (fontScaleMember) { + fontScale = toDouble(*fontScaleMember); + } + + optional textFontMember = objectMember(sectionParams, "text-font"); + if (textFontMember) { + if (isArray(*textFontMember)) { + std::vector fontsVector; + for (std::size_t j = 0; j < arrayLength(*textFontMember); ++j) { + auto font = toString(arrayMember(*textFontMember, j)); + if (font) { + fontsVector.push_back(*font); + } else { + error.message = "Font has to be a string."; + return nullopt; + } + } + textFont = fontsVector; + } else { + error.message = "Font stack has to be an array."; + return nullopt; + } + } + } + + sections.push_back(FormattedSection(*sectionText, fontScale, textFont)); + } + return Formatted(sections); + } else if (optional result = toString(value)) { return Formatted(result->c_str()); } else { + error.message = "Formatted must be plain string or array type."; return nullopt; } } -- cgit v1.2.1