-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Multiple text formats for symbols via "format" expression (native port) #12624
Changes from all commits
fcf395b
d21e39c
2f63b7e
3016300
b030d93
3010467
01c435b
6ab0e43
69ebae6
9328c27
37e88c0
fc71883
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -133,6 +133,7 @@ enum class Kind : int32_t { | |
Any, | ||
All, | ||
Comparison, | ||
FormatExpression, | ||
}; | ||
|
||
class Expression { | ||
|
Original file line number | Diff line number | Diff line change | ||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,52 @@ | ||||||||||||||||||
#pragma once | ||||||||||||||||||
|
||||||||||||||||||
#include <mbgl/style/expression/expression.hpp> | ||||||||||||||||||
#include <mbgl/style/expression/formatted.hpp> | ||||||||||||||||||
#include <mbgl/style/expression/parsing_context.hpp> | ||||||||||||||||||
#include <mbgl/style/conversion.hpp> | ||||||||||||||||||
|
||||||||||||||||||
#include <memory> | ||||||||||||||||||
|
||||||||||||||||||
namespace mbgl { | ||||||||||||||||||
namespace style { | ||||||||||||||||||
namespace expression { | ||||||||||||||||||
|
||||||||||||||||||
struct FormatExpressionSection { | ||||||||||||||||||
FormatExpressionSection(std::unique_ptr<Expression> text_, | ||||||||||||||||||
optional<std::unique_ptr<Expression>> fontScale_, | ||||||||||||||||||
optional<std::unique_ptr<Expression>> textFont_); | ||||||||||||||||||
|
||||||||||||||||||
std::shared_ptr<Expression> text; | ||||||||||||||||||
optional<std::shared_ptr<Expression>> fontScale; | ||||||||||||||||||
optional<std::shared_ptr<Expression>> textFont; | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
class FormatExpression : public Expression { | ||||||||||||||||||
public: | ||||||||||||||||||
FormatExpression(std::vector<FormatExpressionSection> sections); | ||||||||||||||||||
|
||||||||||||||||||
EvaluationResult evaluate(const EvaluationContext&) const override; | ||||||||||||||||||
static ParseResult parse(const mbgl::style::conversion::Convertible&, ParsingContext&); | ||||||||||||||||||
|
||||||||||||||||||
void eachChild(const std::function<void(const Expression&)>&) const override; | ||||||||||||||||||
|
||||||||||||||||||
bool operator==(const Expression& e) const override; | ||||||||||||||||||
|
||||||||||||||||||
std::vector<optional<Value>> possibleOutputs() const override { | ||||||||||||||||||
// Technically the combinatoric set of all children | ||||||||||||||||||
// Usually, this.text will be undefined anyway | ||||||||||||||||||
return { nullopt }; | ||||||||||||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why does this value contain an empty optional rather than being an empty vector? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The direct answer is "because CompoundExpression does and I copied it". But once I started looking for a justification I found at least one place where we have logic that depends on a mapbox-gl-native/src/mbgl/util/font_stack.cpp Lines 41 to 48 in d21c126
This actually makes me wonder if the |
||||||||||||||||||
} | ||||||||||||||||||
|
||||||||||||||||||
mbgl::Value serialize() const override; | ||||||||||||||||||
std::string getOperator() const override { return "format"; } | ||||||||||||||||||
private: | ||||||||||||||||||
std::vector<FormatExpressionSection> sections; | ||||||||||||||||||
std::unique_ptr<Expression> text; | ||||||||||||||||||
optional<std::unique_ptr<Expression>> fontScale; | ||||||||||||||||||
optional<std::unique_ptr<Expression>> textFont; | ||||||||||||||||||
}; | ||||||||||||||||||
|
||||||||||||||||||
} // namespace expression | ||||||||||||||||||
} // namespace style | ||||||||||||||||||
} // namespace mbgl |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
#pragma once | ||
|
||
#include <mbgl/style/conversion.hpp> | ||
#include <mbgl/util/font_stack.hpp> | ||
#include <mbgl/util/optional.hpp> | ||
#include <mbgl/util/variant.hpp> | ||
|
||
#include <vector> | ||
#include <string> | ||
|
||
namespace mbgl { | ||
namespace style { | ||
namespace expression { | ||
|
||
struct FormattedSection { | ||
FormattedSection(std::string text_, optional<double> fontScale_, optional<FontStack> fontStack_) | ||
: text(std::move(text_)) | ||
, fontScale(std::move(fontScale_)) | ||
, fontStack(std::move(fontStack_)) | ||
{} | ||
std::string text; | ||
optional<double> fontScale; | ||
optional<FontStack> fontStack; | ||
}; | ||
|
||
class Formatted { | ||
public: | ||
Formatted() = default; | ||
|
||
Formatted(const char* plainU8String) { | ||
sections.emplace_back(std::string(plainU8String), nullopt, nullopt); | ||
} | ||
|
||
Formatted(std::vector<FormattedSection> sections_) | ||
: sections(std::move(sections_)) | ||
{} | ||
|
||
bool operator==(const Formatted& ) const; | ||
|
||
std::string toString() const; | ||
|
||
bool empty() const { | ||
return sections.empty() || sections.at(0).text.empty(); | ||
} | ||
|
||
std::vector<FormattedSection> sections; | ||
}; | ||
|
||
} // namespace expression | ||
|
||
namespace conversion { | ||
|
||
template <> | ||
struct Converter<mbgl::style::expression::Formatted> { | ||
public: | ||
optional<mbgl::style::expression::Formatted> operator()(const Convertible& value, Error& error) const; | ||
}; | ||
|
||
} // namespace conversion | ||
|
||
} // namespace style | ||
} // namespace mbgl |
This comment was marked as resolved.
Sorry, something went wrong.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Actually I think that header comment may be outdated? It implies
Coercion
is just forto-number
andto-color
, which is no longer true. But it does make me wonder whether the defaultCoercion
behavior of serial evaluation should really be supported for theformat
coercion -- since I don't think there's even any way to use it (the only way to create this coercion is through the implicit coerce on thetext-field
property).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I moved the comment and disabled the coercion for
type::Formatted
(although this is a divergence from gl-js, there shouldn't actually be a way to exercise this code path either way, and this seems like a clearer expression of intent).