srctree

Robin Linden parent e03d3613 68b2004d
style: Support resolving the vw unit

layout/layout.cpp added: 26, removed: 7, total 19
@@ -41,8 +41,7 @@ namespace {
 
class Layouter {
public:
Layouter(int root_font_size, type::IType const &type)
: resolution_context_{.root_font_size = root_font_size}, type_{type} {}
Layouter(style::ResolutionInfo context, type::IType const &type) : resolution_context_{context}, type_{type} {}
 
void layout(LayoutBox &, geom::Rect const &bounds) const;
 
@@ -580,7 +579,12 @@ std::optional<LayoutBox> create_layout(style::StyledNode const &node, int width,
collapse_whitespace(*tree);
apply_text_transforms(*tree);
 
Layouter{node.get_property<css::PropertyId::FontSize>(), type}.layout(*tree, {0, 0, width, 0});
style::ResolutionInfo resolution_context{
.root_font_size = node.get_property<css::PropertyId::FontSize>(),
.viewport_width = width,
};
 
Layouter{resolution_context, type}.layout(*tree, {0, 0, width, 0});
return tree;
}
 
 
style/unresolved_value.cpp added: 26, removed: 7, total 19
@@ -82,6 +82,12 @@ std::optional<int> UnresolvedValue::try_resolve(int font_size,
return static_cast<int>(res * kExToEmRatio * font_size);
}
 
// https://www.w3.org/TR/css3-values/#vw
if (unit == "vw") {
res *= static_cast<float>(context.viewport_width) / 100;
return static_cast<int>(res);
}
 
spdlog::warn("{}({}:{}): Bad property '{}' w/ unit '{}' in to_px",
caller.file_name(),
caller.line(),
 
style/unresolved_value.h added: 26, removed: 7, total 19
@@ -13,6 +13,7 @@ namespace style {
 
struct ResolutionInfo {
int root_font_size{};
int viewport_width{};
};
 
struct UnresolvedValue {
 
style/unresolved_value_test.cpp added: 26, removed: 7, total 19
@@ -65,6 +65,14 @@ int main() {
a.expect_eq(uv.resolve(123, {.root_font_size = 456}), 0);
});
 
s.add_test("unit/vw", [](etest::IActions &a) {
// Based on the first argument, the current element's font-size.
auto const uv = UnresolvedValue{.raw = "25vw"};
a.expect_eq(uv.resolve(100, {.viewport_width = 100}), 25);
a.expect_eq(uv.resolve(123, {.viewport_width = 200}), 50);
a.expect_eq(uv.resolve(0, {.viewport_width = 0}), 0);
});
 
s.add_test("try_resolve", [](etest::IActions &a) {
// %, no parent provided.
auto const percent = UnresolvedValue{.raw = "50%"};