srctree

Robin Linden parent bf72fe40 9bb769c6
layout: Add API to get the text a layout box is responsible for

inlinesplit
layout/layout.cpp added: 35, removed: 7, total 28
@@ -178,8 +178,8 @@ void calculate_position(LayoutBox &box, geom::Rect const &parent) {
 
void calculate_height(LayoutBox &box, int const font_size, int const root_font_size) {
assert(box.node != nullptr);
if (auto const *text = std::get_if<dom::Text>(&box.node->node)) {
int lines = static_cast<int>(std::ranges::count(text->text, '\n')) + 1;
if (auto text = box.text()) {
int lines = static_cast<int>(std::ranges::count(*text, '\n')) + 1;
box.dimensions.content.height = lines * font_size;
}
 
@@ -255,9 +255,9 @@ void layout(LayoutBox &box, geom::Rect const &bounds, int const root_font_size)
calculate_padding(box, font_size, root_font_size);
calculate_border(box, font_size, root_font_size);
 
if (auto const *text_node = std::get_if<dom::Text>(&box.node->node)) {
if (auto text = box.text()) {
// TODO(robinlinden): Measure the text for real.
box.dimensions.content.width = static_cast<int>(text_node->text.size()) * font_size / 2;
box.dimensions.content.width = static_cast<int>(text->size()) * font_size / 2;
}
 
if (box.node->parent) {
@@ -369,6 +369,18 @@ int get_root_font_size(style::StyledNode const &node) {
 
} // namespace
 
std::optional<std::string_view> LayoutBox::text() const {
if (node == nullptr) {
return std::nullopt;
}
 
if (auto const *text = std::get_if<dom::Text>(&node->node)) {
return text->text;
}
 
return std::nullopt;
}
 
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};
 
layout/layout.h added: 35, removed: 7, total 28
@@ -34,6 +34,8 @@ struct LayoutBox {
std::vector<LayoutBox> children;
[[nodiscard]] bool operator==(LayoutBox const &) const = default;
 
std::optional<std::string_view> text() const;
 
template<css::PropertyId T>
auto get_property() const {
// Calling get_property on an anonymous block (the only type that
 
layout/layout_test.cpp added: 35, removed: 7, total 28
@@ -1190,5 +1190,19 @@ int main() {
expect_eq(layout.children.at(0).dimensions.border_box().width, 32);
});
 
etest::test("layout box text accessor", [] {
dom::Node dom = dom::Text{"hello"s};
style::StyledNode style{.node = dom};
layout::LayoutBox layout{.node = &style};
 
expect_eq(layout.text(), "hello");
 
dom = dom::Element{.name = "asdf"};
expect_eq(layout.text(), std::nullopt);
 
layout.node = nullptr;
expect_eq(layout.text(), std::nullopt);
});
 
return etest::run_all_tests();
}