srctree

Robin Linden parent 9ab90744 c28c6cb2
style: Handle ch and ex font sizes

inlinesplit
style/styled_node.cpp added: 20, removed: 2, total 18
@@ -514,6 +514,16 @@ int StyledNode::get_font_size_property() const {
return static_cast<int>(value * kPtToPxRatio);
}
 
// 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>(value * kExToEmRatio * parent_or_default_font_size());
}
 
spdlog::warn("Unhandled unit '{}'", unit);
return 0;
}
 
style/styled_node_test.cpp added: 20, removed: 2, total 18
@@ -207,6 +207,14 @@ int main() {
child.properties[0] = {css::PropertyId::FontSize, "smaller"};
expect(child.get_property<css::PropertyId::FontSize>() < 50);
 
// ex
child.properties[0] = {css::PropertyId::FontSize, "1ex"};
expect_eq(child.get_property<css::PropertyId::FontSize>(), 25);
 
// ch
child.properties[0] = {css::PropertyId::FontSize, "1ch"};
expect_eq(child.get_property<css::PropertyId::FontSize>(), 25);
 
// rem
auto &child2 = child.children.emplace_back(
style::StyledNode{.node{dom_node}, .properties{{css::PropertyId::FontSize, "2rem"}}, .parent = &child});