Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Add data-driven-styling support for text-font #10850

Merged
merged 2 commits into from
Jan 10, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions include/mbgl/style/expression/array_assertion.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ class ArrayAssertion : public Expression {
return false;
}

std::vector<optional<Value>> possibleOutputs() const override {
return input->possibleOutputs();
}

private:
std::unique_ptr<Expression> input;
};
Expand Down
5 changes: 4 additions & 1 deletion include/mbgl/style/expression/assertion.hpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
#pragma once

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/conversion.hpp>
#include <mbgl/style/expression/parsing_context.hpp>

#include <memory>
#include <vector>

Expand All @@ -23,11 +25,12 @@ class Assertion : public Expression {

bool operator==(const Expression& e) const override;

std::vector<optional<Value>> possibleOutputs() const override;

private:
std::vector<std::unique_ptr<Expression>> inputs;
};

} // namespace expression
} // namespace style
} // namespace mbgl

4 changes: 4 additions & 0 deletions include/mbgl/style/expression/at.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class At : public Expression {
return false;
}

std::vector<optional<Value>> possibleOutputs() const override {
return { nullopt };
}

private:
std::unique_ptr<Expression> index;
std::unique_ptr<Expression> input;
Expand Down
5 changes: 3 additions & 2 deletions include/mbgl/style/expression/boolean_operator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/conversion.hpp>

#include <memory>

namespace mbgl {
Expand All @@ -20,6 +21,7 @@ class Any : public Expression {
EvaluationResult evaluate(const EvaluationContext& params) const override;
void eachChild(const std::function<void(const Expression&)>& visit) const override;
bool operator==(const Expression& e) const override;
std::vector<optional<Value>> possibleOutputs() const override;

private:
std::vector<std::unique_ptr<Expression>> inputs;
Expand All @@ -36,8 +38,8 @@ class All : public Expression {

EvaluationResult evaluate(const EvaluationContext& params) const override;
void eachChild(const std::function<void(const Expression&)>& visit) const override;

bool operator==(const Expression& e) const override;
std::vector<optional<Value>> possibleOutputs() const override;

private:
std::vector<std::unique_ptr<Expression>> inputs;
Expand All @@ -46,4 +48,3 @@ class All : public Expression {
} // namespace expression
} // namespace style
} // namespace mbgl

2 changes: 2 additions & 0 deletions include/mbgl/style/expression/case.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ class Case : public Expression {

bool operator==(const Expression& e) const override;

std::vector<optional<Value>> possibleOutputs() const override;

private:
std::vector<Branch> branches;
std::unique_ptr<Expression> otherwise;
Expand Down
4 changes: 3 additions & 1 deletion include/mbgl/style/expression/coalesce.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,9 @@ class Coalesce : public Expression {
void eachChild(const std::function<void(const Expression&)>& visit) const override;

bool operator==(const Expression& e) const override;


std::vector<optional<Value>> possibleOutputs() const override;

std::size_t getLength() const {
return args.size();
}
Expand Down
5 changes: 5 additions & 0 deletions include/mbgl/style/expression/coercion.hpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#pragma once

#include <mbgl/style/expression/expression.hpp>
#include <mbgl/style/conversion.hpp>

#include <memory>
#include <vector>

Expand All @@ -23,6 +25,9 @@ class Coercion : public Expression {
void eachChild(const std::function<void(const Expression&)>& visit) const override;

bool operator==(const Expression& e) const override;

std::vector<optional<Value>> possibleOutputs() const override;

private:
EvaluationResult (*coerceSingleValue) (const Value& v);
std::vector<std::unique_ptr<Expression>> inputs;
Expand Down
8 changes: 6 additions & 2 deletions include/mbgl/style/expression/compound_expression.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,11 @@ class CompoundExpressionBase : public Expression {
[&](const std::vector<type::Type>& p) -> optional<std::size_t> { return p.size(); }
);
}


std::vector<optional<Value>> possibleOutputs() const override {
return { nullopt };
}

private:
std::string name;
variant<std::vector<type::Type>, VarargsType> params;
Expand Down Expand Up @@ -107,7 +111,7 @@ class CompoundExpression : public CompoundExpressionBase {
}
return false;
}

private:
Signature signature;
typename Signature::Args args;
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/expression/equals.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ class Equals : public Expression {
void eachChild(const std::function<void(const Expression&)>& visit) const override;
bool operator==(const Expression&) const override;
EvaluationResult evaluate(const EvaluationContext&) const override;
std::vector<optional<Value>> possibleOutputs() const override;

private:
std::unique_ptr<Expression> lhs;
Expand Down
18 changes: 12 additions & 6 deletions include/mbgl/style/expression/expression.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,16 @@
#pragma once

#include <array>
#include <vector>
#include <memory>
#include <mbgl/util/optional.hpp>
#include <mbgl/util/variant.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/style/expression/type.hpp>
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/expression/parsing_context.hpp>

#include <array>
#include <vector>
#include <memory>

namespace mbgl {

class GeometryTileFeature;
Expand Down Expand Up @@ -38,7 +39,7 @@ class EvaluationContext {
optional<double> heatmapDensity;
};

template<typename T>
template <typename T>
class Result : private variant<EvaluationError, T> {
public:
using variant<EvaluationError, T>::variant;
Expand Down Expand Up @@ -128,6 +129,13 @@ class Expression {

EvaluationResult evaluate(optional<float> zoom, const Feature& feature, optional<double> heatmapDensity) const;

/**
* Statically analyze the expression, attempting to enumerate possible outputs. Returns
* an array of values plus the sentinel null optional value, used to indicate that the
* complete set of outputs is statically undecidable.
*/
virtual std::vector<optional<Value>> possibleOutputs() const = 0;

protected:
template <typename T>
static bool childrenEqual(const T& lhs, const T& rhs) {
Expand Down Expand Up @@ -161,8 +169,6 @@ class Expression {
const std::pair<std::unique_ptr<Expression>, std::unique_ptr<Expression>>& rhs) {
return *(lhs.first) == *(rhs.first) && *(lhs.second) == *(rhs.second);
}



private:
type::Type type;
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/style/expression/interpolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,8 @@ class InterpolateBase : public Expression {
);
}

std::vector<optional<Value>> possibleOutputs() const override;

protected:
const Interpolator interpolator;
const std::unique_ptr<Expression> input;
Expand Down
6 changes: 5 additions & 1 deletion include/mbgl/style/expression/let.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Let : public Expression {
return false;
}

std::vector<optional<Value>> possibleOutputs() const override;

Expression* getResult() const {
return result.get();
}
Expand Down Expand Up @@ -61,7 +63,9 @@ class Var : public Expression {
}
return false;
}


std::vector<optional<Value>> possibleOutputs() const override;

private:
std::string name;
std::shared_ptr<Expression> value;
Expand Down
6 changes: 5 additions & 1 deletion include/mbgl/style/expression/literal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,11 @@ class Literal : public Expression {
}
return false;
}


std::vector<optional<Value>> possibleOutputs() const override {
return {{ value }};
}

private:
Value value;
};
Expand Down
7 changes: 4 additions & 3 deletions include/mbgl/style/expression/match.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,15 @@ class Match : public Expression {
otherwise(std::move(otherwise_))
{}

EvaluationResult evaluate(const EvaluationContext& params) const override;

void eachChild(const std::function<void(const Expression&)>& visit) const override;

bool operator==(const Expression& e) const override;

EvaluationResult evaluate(const EvaluationContext& params) const override;
std::vector<optional<Value>> possibleOutputs() const override;

private:

std::unique_ptr<Expression> input;
Branches branches;
std::unique_ptr<Expression> otherwise;
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/style/expression/step.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Step : public Expression {

bool operator==(const Expression& e) const override;

std::vector<optional<Value>> possibleOutputs() const override;

static ParseResult parse(const mbgl::style::conversion::Convertible& value, ParsingContext& ctx);

private:
Expand Down
9 changes: 9 additions & 0 deletions include/mbgl/style/expression/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,15 @@ struct ValueConverter<T, std::enable_if_t< std::is_enum<T>::value >> {
static optional<T> fromExpressionValue(const Value& value);
};

template <typename T>
std::vector<optional<T>> fromExpressionValues(const std::vector<optional<Value>>& values) {
std::vector<optional<T>> result;
for (const auto& value : values) {
result.push_back(value ? fromExpressionValue<T>(*value) : nullopt);
}
return result;
}

} // namespace expression
} // namespace style
} // namespace mbgl
7 changes: 5 additions & 2 deletions include/mbgl/style/function/camera_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include <mbgl/util/interpolate.hpp>
#include <mbgl/util/variant.hpp>


namespace mbgl {
namespace style {

Expand Down Expand Up @@ -66,7 +65,11 @@ class CameraFunction {
[&](auto z) { return z->getCoveringStops(lower, upper); }
);
}


std::vector<optional<T>> possibleOutputs() const {
return expression::fromExpressionValues<T>(expression->possibleOutputs());
}

friend bool operator==(const CameraFunction& lhs,
const CameraFunction& rhs) {
return *lhs.expression == *rhs.expression;
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/style/function/composite_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,10 @@ class CompositeFunction {
);
}

std::vector<optional<T>> possibleOutputs() const {
return expression::fromExpressionValues<T>(expression->possibleOutputs());
}

friend bool operator==(const CompositeFunction& lhs,
const CompositeFunction& rhs) {
return *lhs.expression == *rhs.expression;
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/style/function/convert.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ class ErrorExpression : public Expression {
return EvaluationError{message};
}

std::vector<optional<Value>> possibleOutputs() const override {
return {};
}

private:
std::string message;
};
Expand Down
4 changes: 4 additions & 0 deletions include/mbgl/style/function/source_function.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ class SourceFunction {
return defaultValue ? *defaultValue : finalDefaultValue;
}

std::vector<optional<T>> possibleOutputs() const {
return expression::fromExpressionValues<T>(expression->possibleOutputs());
}

friend bool operator==(const SourceFunction& lhs,
const SourceFunction& rhs) {
return *lhs.expression == *rhs.expression;
Expand Down
6 changes: 3 additions & 3 deletions include/mbgl/style/layers/symbol_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,9 @@ class SymbolLayer : public Layer {
DataDrivenPropertyValue<std::string> getTextField() const;
void setTextField(DataDrivenPropertyValue<std::string>);

static PropertyValue<std::vector<std::string>> getDefaultTextFont();
PropertyValue<std::vector<std::string>> getTextFont() const;
void setTextFont(PropertyValue<std::vector<std::string>>);
static DataDrivenPropertyValue<std::vector<std::string>> getDefaultTextFont();
DataDrivenPropertyValue<std::vector<std::string>> getTextFont() const;
void setTextFont(DataDrivenPropertyValue<std::vector<std::string>>);

static DataDrivenPropertyValue<float> getDefaultTextSize();
DataDrivenPropertyValue<float> getTextSize() const;
Expand Down
3 changes: 3 additions & 0 deletions include/mbgl/util/optional.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,7 @@ namespace mbgl {
template <typename T>
using optional = std::experimental::optional<T>;

using nullopt_t = std::experimental::nullopt_t;
constexpr nullopt_t nullopt = std::experimental::nullopt;

} // namespace mbgl
Original file line number Diff line number Diff line change
Expand Up @@ -2888,11 +2888,11 @@ public static PropertyValue<Expression> textFont(Expression value) {
/**
* Font stack to use for displaying text.
*
* @param <Z> the zoom parameter type
* @param function a wrapper {@link CameraFunction} for String[]
* @param <T> the function input type
* @param function a wrapper function for String[]
* @return property wrapper around a String[] function
*/
public static <Z extends Number> PropertyValue<CameraFunction<Z, String[]>> textFont(CameraFunction<Z, String[]> function) {
public static <T> PropertyValue<Function<T, String[]>> textFont(Function<T, String[]> function) {
return new LayoutPropertyValue<>("text-font", function);
}

Expand Down
Loading