Skip to content

Commit

Permalink
Add support for text-variable-anchor-offset property (maplibre#2921)
Browse files Browse the repository at this point in the history
Co-authored-by: Yingfang Wang <[email protected]>
Co-authored-by: Alexey Kon <[email protected]>
  • Loading branch information
3 people authored Oct 28, 2024
1 parent 90a798b commit 1a63194
Show file tree
Hide file tree
Showing 192 changed files with 5,932 additions and 108 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/style/transition_options.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/types.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/undefined.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/style/variable_anchor_offset_collection.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/text/glyph.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/text/glyph_range.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/tile/tile_id.hpp
Expand Down Expand Up @@ -864,6 +865,7 @@ list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/style/style_impl.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/style_impl.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/types.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/variable_anchor_offset_collection.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_layer.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/style/layers/custom_layer_render_parameters.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/layermanager/custom_layer_factory.cpp
Expand Down
2 changes: 2 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,7 @@ MLN_CORE_SOURCE = [
"src/mbgl/style/style_impl.cpp",
"src/mbgl/style/style_impl.hpp",
"src/mbgl/style/types.cpp",
"src/mbgl/style/variable_anchor_offset_collection.cpp",
"src/mbgl/text/bidi.hpp",
"src/mbgl/text/check_max_angle.cpp",
"src/mbgl/text/check_max_angle.hpp",
Expand Down Expand Up @@ -802,6 +803,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/style/transition_options.hpp",
"include/mbgl/style/types.hpp",
"include/mbgl/style/undefined.hpp",
"include/mbgl/style/variable_anchor_offset_collection.hpp",
"include/mbgl/text/glyph.hpp",
"include/mbgl/text/glyph_range.hpp",
"include/mbgl/tile/tile_id.hpp",
Expand Down
6 changes: 6 additions & 0 deletions include/mbgl/style/conversion/constant.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <mbgl/style/conversion.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/style/variable_anchor_offset_collection.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/padding.hpp>
Expand Down Expand Up @@ -50,6 +51,11 @@ struct Converter<Padding> {
std::optional<Padding> operator()(const Convertible& value, Error& error) const;
};

template <>
struct Converter<VariableAnchorOffsetCollection> {
std::optional<VariableAnchorOffsetCollection> operator()(const Convertible& value, Error& error) const;
};

template <size_t N>
struct Converter<std::array<float, N>> {
std::optional<std::array<float, N>> operator()(const Convertible& value, Error& error) const;
Expand Down
10 changes: 10 additions & 0 deletions include/mbgl/style/conversion_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,13 @@ struct ValueFactory<Padding> {
static Value make(const Padding& padding) { return padding.serialize(); }
};

template <>
struct ValueFactory<VariableAnchorOffsetCollection> {
static Value make(const VariableAnchorOffsetCollection& variableAnchorOffset) {
return variableAnchorOffset.serialize();
}
};

template <typename T>
struct ValueFactory<T, typename std::enable_if_t<(!std::is_enum_v<T> && !is_linear_container<T>::value)>> {
static Value make(const T& arg) { return {arg}; }
Expand Down Expand Up @@ -370,6 +377,9 @@ StyleProperty makeStyleProperty(const PropertyValue<T>& value) {
[](const Undefined&) -> StyleProperty { return {}; },
[](const Color& c) -> StyleProperty { return {makeValue(c), StyleProperty::Kind::Expression}; },
[](const Padding& p) -> StyleProperty { return {makeValue(p), StyleProperty::Kind::Expression}; },
[](const VariableAnchorOffsetCollection& v) -> StyleProperty {
return {makeValue(v), StyleProperty::Kind::Expression};
},
[](const PropertyExpression<T>& fn) -> StyleProperty {
return {fn.getExpression().serialize(), StyleProperty::Kind::Expression};
},
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/style/expression/dsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ std::unique_ptr<Expression> boolean(std::unique_ptr<Expression>, std::unique_ptr

std::unique_ptr<Expression> toColor(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toPadding(std::unique_ptr<Expression> value, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toVariableAnchorOffset(std::unique_ptr<Expression>,
std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toString(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toFormatted(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
std::unique_ptr<Expression> toImage(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);
Expand Down
8 changes: 8 additions & 0 deletions include/mbgl/style/expression/type.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,12 @@ struct PaddingType {
bool operator==(const PaddingType&) const { return true; }
};

struct VariableAnchorOffsetCollectionType {
constexpr VariableAnchorOffsetCollectionType() = default;
std::string getName() const { return "variableAnchorOffsetCollection"; }
bool operator==(const VariableAnchorOffsetCollectionType&) const { return true; }
};

struct ObjectType {
constexpr ObjectType() = default;
std::string getName() const { return "object"; }
Expand Down Expand Up @@ -92,6 +98,7 @@ constexpr StringType String;
constexpr BooleanType Boolean;
constexpr ColorType Color;
constexpr PaddingType Padding;
constexpr VariableAnchorOffsetCollectionType VariableAnchorOffsetCollection;
constexpr ValueType Value;
constexpr ObjectType Object;
constexpr CollatorType Collator;
Expand All @@ -107,6 +114,7 @@ using Type = variant<NullType,
StringType,
ColorType,
PaddingType,
VariableAnchorOffsetCollectionType,
ObjectType,
ValueType,
mapbox::util::recursive_wrapper<Array>,
Expand Down
2 changes: 2 additions & 0 deletions include/mbgl/style/expression/value.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <mbgl/style/position.hpp>
#include <mbgl/style/rotation.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/style/variable_anchor_offset_collection.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/feature.hpp>
Expand All @@ -28,6 +29,7 @@ using ValueBase = variant<NullValue,
double,
std::string,
Color,
VariableAnchorOffsetCollection,
Collator,
Formatted,
Image,
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/layers/layer.hpp.ejs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
<% } -%>
<% if (type === 'symbol') { -%>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/style/variable_anchor_offset_collection.hpp>
<% } -%>
#include <mbgl/util/color.hpp>

Expand Down
5 changes: 5 additions & 0 deletions include/mbgl/style/layers/symbol_layer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include <mbgl/style/filter.hpp>
#include <mbgl/style/property_value.hpp>
#include <mbgl/style/expression/formatted.hpp>
#include <mbgl/style/variable_anchor_offset_collection.hpp>
#include <mbgl/util/color.hpp>

#include <vector>
Expand Down Expand Up @@ -184,6 +185,10 @@ class SymbolLayer final : public Layer {
const PropertyValue<std::vector<TextVariableAnchorType>>& getTextVariableAnchor() const;
void setTextVariableAnchor(const PropertyValue<std::vector<TextVariableAnchorType>>&);

static PropertyValue<VariableAnchorOffsetCollection> getDefaultTextVariableAnchorOffset();
const PropertyValue<VariableAnchorOffsetCollection>& getTextVariableAnchorOffset() const;
void setTextVariableAnchorOffset(const PropertyValue<VariableAnchorOffsetCollection>&);

static PropertyValue<std::vector<TextWritingModeType>> getDefaultTextWritingMode();
const PropertyValue<std::vector<TextWritingModeType>>& getTextWritingMode() const;
void setTextWritingMode(const PropertyValue<std::vector<TextWritingModeType>>&);
Expand Down
61 changes: 61 additions & 0 deletions include/mbgl/style/variable_anchor_offset_collection.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#pragma once

#include <mbgl/style/types.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/geometry.hpp>

#include <array>
#include <string>
#include <vector>

namespace mbgl {

struct AnchorOffsetPair {
style::SymbolAnchorType anchorType;
std::array<float, 2> offset;

AnchorOffsetPair(style::SymbolAnchorType anchorType_, std::array<float, 2> offset_)
: anchorType(anchorType_),
offset(offset_) {}

bool operator==(const AnchorOffsetPair& other) const = default;
};

class VariableAnchorOffsetCollection {
private:
using CollectionType = std::vector<AnchorOffsetPair>;
CollectionType anchorOffsets;

public:
VariableAnchorOffsetCollection() = default;

VariableAnchorOffsetCollection(const VariableAnchorOffsetCollection& other) = default;

VariableAnchorOffsetCollection(VariableAnchorOffsetCollection&& other) noexcept = default;

VariableAnchorOffsetCollection(std::vector<AnchorOffsetPair>&& values) { anchorOffsets = std::move(values); }

std::array<float, 2> getOffsetByAnchor(const style::SymbolAnchorType& anchorType) const;

std::string toString() const;

mbgl::Value serialize() const;

bool empty() const { return anchorOffsets.size() == 0; }

CollectionType::size_type size() const { return anchorOffsets.size(); }

CollectionType::const_iterator begin() const { return anchorOffsets.begin(); }

CollectionType::const_iterator end() const { return anchorOffsets.end(); }

const AnchorOffsetPair& operator[](size_t index) const { return anchorOffsets[index]; }

VariableAnchorOffsetCollection& operator=(const VariableAnchorOffsetCollection& other) = default;

VariableAnchorOffsetCollection& operator=(VariableAnchorOffsetCollection&& other) noexcept = default;

bool operator==(const VariableAnchorOffsetCollection& other) const = default;
};

} // namespace mbgl
34 changes: 34 additions & 0 deletions include/mbgl/util/interpolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,12 @@
#include <mbgl/style/expression/value.hpp>
#include <mbgl/style/position.hpp>
#include <mbgl/style/rotation.hpp>
#include <mbgl/style/types.hpp>
#include <mbgl/style/variable_anchor_offset_collection.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/range.hpp>
#include <mbgl/util/string.hpp>

#include <array>
#include <vector>
Expand Down Expand Up @@ -143,6 +147,36 @@ struct Interpolator<Padding> {
}
};

template <>
struct Interpolator<VariableAnchorOffsetCollection> {
public:
VariableAnchorOffsetCollection operator()(const VariableAnchorOffsetCollection& a,
const VariableAnchorOffsetCollection& b,
const float t) const {
if (a.size() != b.size()) {
throw std::runtime_error("Cannot interpolate values of different length. from: " + a.toString() +
", to: " + b.toString());
}
std::vector<AnchorOffsetPair> offsetMap;
offsetMap.reserve(a.size());
for (size_t index = 0; index < a.size(); index++) {
const auto& aPair = a[index];
const auto& bPair = b[index];
if (aPair.anchorType != bPair.anchorType) {
throw std::runtime_error(
"Cannot interpolate values containing mismatched anchors. index: " + util::toString(index) +
"from: " + Enum<style::SymbolAnchorType>::toString(aPair.anchorType) +
", to: " + Enum<style::SymbolAnchorType>::toString(bPair.anchorType));
}
auto offset = std::array<float, 2>{interpolate(aPair.offset[0], bPair.offset[0], t),
interpolate(aPair.offset[1], bPair.offset[1], t)};
offsetMap.emplace_back(aPair.anchorType, offset);
}

return VariableAnchorOffsetCollection(std::move(offsetMap));
}
};

template <>
struct Interpolator<style::Rotation> {
public:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"network": [
[
"probeNetwork - default - end",
7,
1778473
],
[
"probeNetwork - default - start",
0,
0
]
],
"gfx": [
[
"probeGFX - default - end",
8,
13,
37,
1,
[
90448,
90448
],
[
100366,
100366
],
[
1337984,
1337984
]
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"network": [
[
"probeNetwork - default - end",
4,
686859
],
[
"probeNetwork - default - start",
0,
0
]
],
"gfx": [
[
"probeGFX - default - end",
1,
5,
9,
1,
[
52212,
52212
],
[
5494,
5494
],
[
73024,
73024
]
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
{
"network": [
[
"probeNetwork - default - end",
7,
1778473
],
[
"probeNetwork - default - start",
0,
0
]
],
"gfx": [
[
"probeGFX - default - end",
4,
13,
21,
1,
[
143312,
143312
],
[
97654,
97654
],
[
1301824,
1301824
]
]
]
}
Loading

0 comments on commit 1a63194

Please sign in to comment.