1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:37:44 +00:00

LibWeb: Wrap font metrics into a struct

Rather than passing an increasingly-unwieldy number of font parameters
individually to every function that resolves lengths, let's wrap them
up.

This is frustratingly close to being `Gfx::FontPixelMetrics`, but bitmap
fonts cause issues: We choose the closest font to what the CSS
requests, but that might have a wildly different size than what the
page expects, so we have to fudge the numbers.

No behaviour changes.
This commit is contained in:
Sam Atkins 2023-04-28 16:29:12 +01:00 committed by Andreas Kling
parent 4a191875a9
commit 0679b4e0b9
14 changed files with 107 additions and 62 deletions

View file

@ -153,34 +153,34 @@ NonnullRefPtr<Gfx::Font const> StyleProperties::font_fallback(bool monospace, bo
}
// FIXME: This implementation is almost identical to line_height(Layout::Node) below. Maybe they can be combined somehow.
CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Gfx::FontPixelMetrics const& font_metrics, CSSPixels font_size, CSSPixels root_font_size, CSSPixels parent_line_height, CSSPixels root_line_height) const
CSSPixels StyleProperties::line_height(CSSPixelRect const& viewport_rect, Length::FontMetrics const& font_metrics, Length::FontMetrics const& root_font_metrics) const
{
auto line_height = property(CSS::PropertyID::LineHeight);
if (line_height->is_identifier() && line_height->to_identifier() == ValueID::Normal)
return font_metrics.line_spacing();
return font_metrics.line_height;
if (line_height->is_length()) {
auto line_height_length = line_height->to_length();
if (!line_height_length.is_auto())
return line_height_length.to_px(viewport_rect, font_metrics, font_size, root_font_size, parent_line_height, root_line_height);
return line_height_length.to_px(viewport_rect, font_metrics, root_font_metrics);
}
if (line_height->is_numeric())
return Length(line_height->to_number(), Length::Type::Em).to_px(viewport_rect, font_metrics, font_size, root_font_size, parent_line_height, root_line_height);
return Length(line_height->to_number(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
if (line_height->is_percentage()) {
// Percentages are relative to 1em. https://www.w3.org/TR/css-inline-3/#valdef-line-height-percentage
auto& percentage = line_height->as_percentage().percentage();
return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, font_size, root_font_size, parent_line_height, root_line_height);
return Length(percentage.as_fraction(), Length::Type::Em).to_px(viewport_rect, font_metrics, root_font_metrics);
}
if (line_height->is_calculated()) {
// FIXME: Handle `line-height: calc(...)` despite not having a LayoutNode here.
return font_metrics.line_spacing();
return font_metrics.line_height;
}
return font_metrics.line_spacing();
return font_metrics.line_height;
}
CSSPixels StyleProperties::line_height(Layout::Node const& layout_node) const