Skip to content

Commit

Permalink
Add array support for icon-padding (maplibre#2845)
Browse files Browse the repository at this point in the history
  • Loading branch information
random3940 authored Sep 21, 2024
1 parent 58dd529 commit 9d553c2
Show file tree
Hide file tree
Showing 56 changed files with 902 additions and 104 deletions.
2 changes: 2 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -413,6 +413,7 @@ list(APPEND INCLUDE_FILES
${PROJECT_SOURCE_DIR}/include/mbgl/util/logging.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/lru_cache.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/noncopyable.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/padding.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/platform.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/premultiply.hpp
${PROJECT_SOURCE_DIR}/include/mbgl/util/projection.hpp
Expand Down Expand Up @@ -972,6 +973,7 @@ list(APPEND SRC_FILES
${PROJECT_SOURCE_DIR}/src/mbgl/util/mat4.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/mat4.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/math.hpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/padding.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/premultiply.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/quaternion.cpp
${PROJECT_SOURCE_DIR}/src/mbgl/util/rapidjson.cpp
Expand Down
2 changes: 2 additions & 0 deletions bazel/core.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ MLN_CORE_SOURCE = [
"src/mbgl/util/mat4.cpp",
"src/mbgl/util/mat4.hpp",
"src/mbgl/util/math.hpp",
"src/mbgl/util/padding.cpp",
"src/mbgl/util/premultiply.cpp",
"src/mbgl/util/quaternion.cpp",
"src/mbgl/util/quaternion.hpp",
Expand Down Expand Up @@ -835,6 +836,7 @@ MLN_CORE_HEADERS = [
"include/mbgl/util/lru_cache.hpp",
"include/mbgl/util/monotonic_timer.hpp",
"include/mbgl/util/noncopyable.hpp",
"include/mbgl/util/padding.hpp",
"include/mbgl/util/platform.hpp",
"include/mbgl/util/premultiply.hpp",
"include/mbgl/util/projection.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 @@ -4,6 +4,7 @@
#include <mbgl/style/types.hpp>
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/padding.hpp>
#include <mbgl/util/string.hpp>

#include <array>
Expand Down Expand Up @@ -44,6 +45,11 @@ struct Converter<Color> {
std::optional<Color> operator()(const Convertible& value, Error& error) const;
};

template <>
struct Converter<Padding> {
std::optional<Padding> 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
19 changes: 13 additions & 6 deletions include/mbgl/style/conversion_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,11 @@ struct ValueFactory<Color> {
static Value make(const Color& color) { return color.serialize(); }
};

template <>
struct ValueFactory<Padding> {
static Value make(const Padding& padding) { return padding.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 @@ -361,12 +366,14 @@ Value makeValue(T&& arg) {

template <typename T>
StyleProperty makeStyleProperty(const PropertyValue<T>& value) {
return value.match([](const Undefined&) -> StyleProperty { return {}; },
[](const Color& c) -> StyleProperty { return {makeValue(c), StyleProperty::Kind::Expression}; },
[](const PropertyExpression<T>& fn) -> StyleProperty {
return {fn.getExpression().serialize(), StyleProperty::Kind::Expression};
},
[](const auto& t) -> StyleProperty { return {makeValue(t), StyleProperty::Kind::Constant}; });
return value.match(
[](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 PropertyExpression<T>& fn) -> StyleProperty {
return {fn.getExpression().serialize(), StyleProperty::Kind::Expression};
},
[](const auto& t) -> StyleProperty { return {makeValue(t), StyleProperty::Kind::Constant}; });
}

inline StyleProperty makeStyleProperty(const TransitionOptions& value) {
Expand Down
1 change: 1 addition & 0 deletions include/mbgl/style/expression/dsl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ std::unique_ptr<Expression> string(std::unique_ptr<Expression>, std::unique_ptr<
std::unique_ptr<Expression> boolean(std::unique_ptr<Expression>, std::unique_ptr<Expression> def = nullptr);

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> 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 @@ -44,6 +44,12 @@ struct ColorType {
bool operator==(const ColorType&) const { return true; }
};

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

struct ObjectType {
constexpr ObjectType() = default;
std::string getName() const { return "object"; }
Expand Down Expand Up @@ -85,6 +91,7 @@ constexpr NumberType Number;
constexpr StringType String;
constexpr BooleanType Boolean;
constexpr ColorType Color;
constexpr PaddingType Padding;
constexpr ValueType Value;
constexpr ObjectType Object;
constexpr CollatorType Collator;
Expand All @@ -99,6 +106,7 @@ using Type = variant<NullType,
BooleanType,
StringType,
ColorType,
PaddingType,
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 @@ -10,6 +10,7 @@
#include <mbgl/util/color.hpp>
#include <mbgl/util/enum.hpp>
#include <mbgl/util/feature.hpp>
#include <mbgl/util/padding.hpp>
#include <mbgl/util/variant.hpp>

#include <array>
Expand All @@ -30,6 +31,7 @@ using ValueBase = variant<NullValue,
Collator,
Formatted,
Image,
Padding,
mapbox::util::recursive_wrapper<std::vector<Value>>,
mapbox::util::recursive_wrapper<std::unordered_map<std::string, Value>>>;
struct Value : ValueBase {
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 @@ -52,9 +52,9 @@ class SymbolLayer final : public Layer {
const PropertyValue<bool>& getIconOptional() const;
void setIconOptional(const PropertyValue<bool>&);

static PropertyValue<float> getDefaultIconPadding();
const PropertyValue<float>& getIconPadding() const;
void setIconPadding(const PropertyValue<float>&);
static PropertyValue<Padding> getDefaultIconPadding();
const PropertyValue<Padding>& getIconPadding() const;
void setIconPadding(const PropertyValue<Padding>&);

static PropertyValue<AlignmentType> getDefaultIconPitchAlignment();
const PropertyValue<AlignmentType>& getIconPitchAlignment() const;
Expand Down
18 changes: 18 additions & 0 deletions include/mbgl/util/interpolate.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,24 @@ struct Interpolator<Color> {
}
};

template <>
struct Interpolator<Padding> {
public:
Padding operator()(const Padding& a, const Padding& b, const float t) const noexcept {
return {interpolate(a.top, b.top, t),
interpolate(a.right, b.right, t),
interpolate(a.bottom, b.bottom, t),
interpolate(a.left, b.left, t)};
}

Padding operator()(const Padding& a, const Padding& b, const double t) const noexcept {
return {interpolate(a.top, b.top, t),
interpolate(a.right, b.right, t),
interpolate(a.bottom, b.bottom, t),
interpolate(a.left, b.left, t)};
}
};

template <>
struct Interpolator<style::Rotation> {
public:
Expand Down
72 changes: 72 additions & 0 deletions include/mbgl/util/padding.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#pragma once

#include <mbgl/util/feature.hpp>
#include <array>
#include <span>

namespace mbgl {

// A set of four numbers representing padding around a box.
struct Padding {
// Empty/zero padding on all sides.
Padding() = default;

// Same padding on all sides.
Padding(float value)
: top(value),
right(value),
bottom(value),
left(value) {}

Padding(float t_, float r_, float b_, float l_)
: top(t_),
right(r_),
bottom(b_),
left(l_) {}

// Padding values are in CSS order: top, right, bottom, left.
Padding(const std::span<const float>& values) {
switch (values.size()) {
case 1:
top = right = bottom = left = values[0];
break;
case 2:
top = bottom = values[0];
right = left = values[1];
break;
case 3:
top = values[0];
left = right = values[1];
bottom = values[2];
break;
case 4:
top = values[0];
right = values[1];
bottom = values[2];
left = values[3];
break;
default:
throw std::invalid_argument("Padding must have between 1 and 4 elements");
}
}

float top = 0;
float right = 0;
float bottom = 0;
float left = 0;

explicit operator bool() const { return top != 0 || right != 0 || bottom != 0 || left != 0; }

bool operator==(const Padding&) const = default;

std::array<float, 4> toArray() const;

// Used by ValueFactory<Padding>::make()
mbgl::Value serialize() const;
};

inline Padding operator*(const Padding& padding, float scale) {
return {padding.top * scale, padding.right * scale, padding.bottom * scale, padding.left * scale};
}

} // namespace mbgl
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 57 additions & 0 deletions metrics/integration/render-tests/icon-padding/databind/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"version": 8,
"metadata": {
"test": {
"collisionDebug": true,
"width": 240,
"height": 100
}
},
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-60, 0] }
}, {
"type": "Feature",
"properties": { "padding": 5 },
"geometry": { "type": "Point", "coordinates": [-30, 0] }
}, {
"type": "Feature",
"properties": { "padding": [-20, -5] },
"geometry": { "type": "Point", "coordinates": [-0, 0] }
}, {
"type": "Feature",
"properties": { "padding": [2, 2, -10] },
"geometry": { "type": "Point", "coordinates": [30, 0] }
}, {
"type": "Feature",
"properties": { "padding": [2, 10, -20, 2] },
"geometry": { "type": "Point", "coordinates": [60, 0] }
}
]
}
}
},
"sprite": "local://sprites/2x",
"layers": [{
"id": "bg",
"type": "background",
"paint": {
"background-color": "#ccc"
}
}, {
"id": "symbolA",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-image": "icon",
"icon-padding": ["coalesce", ["get", "padding"], ["literal", [2]]]
}
}
]
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
58 changes: 58 additions & 0 deletions metrics/integration/render-tests/icon-padding/default/style.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"version": 8,
"metadata": {
"test": {
"collisionDebug": true,
"width": 240,
"height": 100
}
},
"sources": {
"geojson": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-60, 0] }
}, {
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-40, 0] }
}, {
"type": "Feature",
"properties": { },
"geometry": { "type": "Point", "coordinates": [-20, 0] }
}
]
}
}
},
"sprite": "local://sprites/2x",
"layers": [{
"id": "bg",
"type": "background",
"paint": {
"background-color": "#ccc"
}
}, {
"id": "symbolA",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-image": "icon",
"icon-padding": [10]
}
}, {
"id": "symbolB",
"type": "symbol",
"source": "geojson",
"layout": {
"icon-image": "icon",
"icon-padding": [2, 2, -20],
"icon-offset": [120, 0]
}
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"network": [
[
"probeNetwork - default - end",
2,
2094
],
[
"probeNetwork - default - start",
0,
0
]
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"network": [
[
"probeNetwork - default - end",
2,
2094
],
[
"probeNetwork - default - start",
0,
0
]
]
}
Loading

0 comments on commit 9d553c2

Please sign in to comment.