srctree

Robin Linden parent d0e3e825 068bff42
layout: Parse the border-radius properties into vert/horiz radii

inlinesplit
layout/layout.cpp added: 53, removed: 4, total 49
@@ -337,6 +337,14 @@ void print_box(LayoutBox const &box, std::ostream &os, uint8_t depth = 0) {
 
} // namespace
 
std::pair<int, int> LayoutBox::get_border_radius_property(css::PropertyId id) const {
auto raw = node->get_raw_property(id);
auto [horizontal, vertical] = raw.contains('/') ? util::split_once(raw, "/") : std::pair{raw, raw};
 
int font_size = node->get_property<css::PropertyId::FontSize>();
return {to_px(horizontal, font_size), to_px(vertical, font_size)};
}
 
std::optional<LayoutBox> create_layout(style::StyledNode const &node, int width) {
auto tree = create_tree(node);
if (!tree) {
 
layout/layout.h added: 53, removed: 4, total 49
@@ -15,6 +15,7 @@
#include <optional>
#include <string>
#include <string_view>
#include <utility>
#include <variant>
#include <vector>
 
@@ -39,8 +40,16 @@ struct LayoutBox {
// doesn't have a StyleNode) is a programming error.
assert(type != LayoutType::AnonymousBlock);
assert(node);
return node->get_property<T>();
if constexpr (T == css::PropertyId::BorderBottomLeftRadius || T == css::PropertyId::BorderBottomRightRadius
|| T == css::PropertyId::BorderTopLeftRadius || T == css::PropertyId::BorderTopRightRadius) {
return get_border_radius_property(T);
} else {
return node->get_property<T>();
}
}
 
private:
std::pair<int, int> get_border_radius_property(css::PropertyId) const;
};
 
std::optional<LayoutBox> create_layout(style::StyledNode const &node, int width);
 
filename was Deleted added: 53, removed: 4, total 49
@@ -0,0 +1,32 @@
// SPDX-FileCopyrightText: 2023 Robin Lindén <dev@robinlinden.eu>
//
// SPDX-License-Identifier: BSD-2-Clause
 
#include "layout/layout.h"
 
#include "etest/etest.h"
 
using namespace std::literals;
using etest::expect_eq;
 
int main() {
etest::test("border radius", [] {
dom::Node html_node = dom::Element{"html"s};
style::StyledNode styled_node{
.node = html_node,
.properties{
{css::PropertyId::FontSize, "30px"},
{css::PropertyId::BorderTopLeftRadius, "2em"},
{css::PropertyId::BorderBottomRightRadius, "10px/3em"},
},
};
auto layout = layout::create_layout(styled_node, 123).value();
 
expect_eq(layout.get_property<css::PropertyId::BorderTopLeftRadius>(), std::pair{60, 60});
expect_eq(layout.get_property<css::PropertyId::BorderTopRightRadius>(), std::pair{0, 0});
expect_eq(layout.get_property<css::PropertyId::BorderBottomLeftRadius>(), std::pair{0, 0});
expect_eq(layout.get_property<css::PropertyId::BorderBottomRightRadius>(), std::pair{10, 90});
});
 
return etest::run_all_tests();
}