srctree

Robin Linden parent c28c6cb2 4328dc92
layout: Add ch/ex-unit-support to UnresolvedValue

inlinesplit
layout/layout_box.cpp added: 28, removed: 2, total 26
@@ -167,6 +167,16 @@ int to_px(std::string_view property,
return static_cast<int>(res);
}
 
// https://www.w3.org/TR/css3-values/#ex
// https://www.w3.org/TR/css3-values/#ch
if (unit == "ex" || unit == "ch") {
// Technically, these are the height of an 'x' or '0' glyph
// respectively, but we're allowed to approximate it as 50% of the em
// value.
static constexpr float kExToEmRatio = 0.5f;
return static_cast<int>(res * kExToEmRatio * font_size);
}
 
spdlog::warn("Bad property '{}' w/ unit '{}' in to_px", property, unit);
return static_cast<int>(res);
}
 
layout/unresolved_value_test.cpp added: 28, removed: 2, total 26
@@ -25,6 +25,22 @@ int main() {
a.expect_eq(uv.resolve(0, 0), 0);
});
 
s.add_test("unit/ex", [](etest::IActions &a) {
// Based on the first argument, the current element's font-size.
auto const uv = layout::UnresolvedValue{.raw = "1ex"};
a.expect_eq(uv.resolve(100, 100), 50);
a.expect_eq(uv.resolve(123, 456), 61);
a.expect_eq(uv.resolve(0, 0), 0);
});
 
s.add_test("unit/ch", [](etest::IActions &a) {
// Based on the first argument, the current element's font-size.
auto const uv = layout::UnresolvedValue{.raw = "1ch"};
a.expect_eq(uv.resolve(100, 100), 50);
a.expect_eq(uv.resolve(123, 456), 61);
a.expect_eq(uv.resolve(0, 0), 0);
});
 
s.add_test("unit/rem", [](etest::IActions &a) {
// Based on the second argument, the root element's font-size.
auto const uv = layout::UnresolvedValue{.raw = "2rem"};