1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-24 17:17:42 +00:00

LibWeb: Split of compute font logic in StyleComputer

This commit is contained in:
Bastiaan van der Plaat 2023-08-07 21:48:18 +02:00 committed by Andreas Kling
parent cc1f7d385c
commit b05fe22d39
2 changed files with 44 additions and 36 deletions

View file

@ -2061,28 +2061,13 @@ RefPtr<Gfx::Font const> StyleComputer::font_matching_algorithm(FontFaceKey const
return {}; return {};
} }
void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* element, Optional<CSS::Selector::PseudoElement> pseudo_element) const RefPtr<Gfx::Font const> StyleComputer::compute_font_for_style_values(DOM::Element const* element, Optional<CSS::Selector::PseudoElement> pseudo_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch) const
{ {
// To compute the font, first ensure that we've defaulted the relevant CSS font properties.
// FIXME: This should be more sophisticated.
compute_defaulted_property_value(style, element, CSS::PropertyID::FontFamily, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontSize, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontStretch, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontStyle, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontWeight, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::LineHeight, pseudo_element);
auto* parent_element = element_to_inherit_style_from(element, pseudo_element); auto* parent_element = element_to_inherit_style_from(element, pseudo_element);
auto font_size = style.property(CSS::PropertyID::FontSize); auto width = font_stretch.to_font_stretch_width();
auto font_style = style.property(CSS::PropertyID::FontStyle);
auto font_weight = style.property(CSS::PropertyID::FontWeight);
auto font_stretch = style.property(CSS::PropertyID::FontStretch);
int width = font_stretch->to_font_stretch_width();
auto weight = font_weight->to_font_weight();
auto weight = font_weight.to_font_weight();
bool bold = weight > Gfx::FontWeight::Regular; bool bold = weight > Gfx::FontWeight::Regular;
// FIXME: Should be based on "user's default font size" // FIXME: Should be based on "user's default font size"
@ -2110,7 +2095,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
}; };
Length::FontMetrics font_metrics { parent_font_size(), font_pixel_metrics, parent_line_height }; Length::FontMetrics font_metrics { parent_font_size(), font_pixel_metrics, parent_line_height };
if (font_size->is_identifier()) { if (font_size.is_identifier()) {
// https://w3c.github.io/csswg-drafts/css-fonts/#absolute-size-mapping // https://w3c.github.io/csswg-drafts/css-fonts/#absolute-size-mapping
AK::HashMap<Web::CSS::ValueID, float> absolute_size_mapping = { AK::HashMap<Web::CSS::ValueID, float> absolute_size_mapping = {
{ CSS::ValueID::XxSmall, 0.6 }, { CSS::ValueID::XxSmall, 0.6 },
@ -2125,7 +2110,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
{ CSS::ValueID::Larger, 1.25 }, { CSS::ValueID::Larger, 1.25 },
}; };
auto const identifier = static_cast<IdentifierStyleValue const&>(*font_size).id(); auto const identifier = static_cast<IdentifierStyleValue const&>(font_size).id();
// https://w3c.github.io/csswg-drafts/css-fonts/#valdef-font-size-relative-size // https://w3c.github.io/csswg-drafts/css-fonts/#valdef-font-size-relative-size
// TODO: If the parent element has a keyword font size in the absolute size keyword mapping table, // TODO: If the parent element has a keyword font size in the absolute size keyword mapping table,
@ -2147,14 +2132,14 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
}; };
Optional<Length> maybe_length; Optional<Length> maybe_length;
if (font_size->is_percentage()) { if (font_size.is_percentage()) {
// Percentages refer to parent element's font size // Percentages refer to parent element's font size
maybe_length = Length::make_px(font_size->as_percentage().percentage().as_fraction() * parent_font_size().to_double()); maybe_length = Length::make_px(font_size.as_percentage().percentage().as_fraction() * parent_font_size().to_double());
} else if (font_size->is_length()) { } else if (font_size.is_length()) {
maybe_length = font_size->as_length().length(); maybe_length = font_size.as_length().length();
} else if (font_size->is_calculated()) { } else if (font_size.is_calculated()) {
maybe_length = font_size->as_calculated().resolve_length(length_resolution_context); maybe_length = font_size.as_calculated().resolve_length(length_resolution_context);
} }
if (maybe_length.has_value()) { if (maybe_length.has_value()) {
auto px = maybe_length.value().to_px(length_resolution_context).to_int(); auto px = maybe_length.value().to_px(length_resolution_context).to_int();
@ -2163,7 +2148,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
} }
} }
auto slope = font_style->to_font_slope(); auto slope = font_style.to_font_slope();
// FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm // FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
@ -2237,9 +2222,8 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
RefPtr<Gfx::Font const> found_font; RefPtr<Gfx::Font const> found_font;
auto family_value = style.property(PropertyID::FontFamily); if (font_family.is_value_list()) {
if (family_value->is_value_list()) { auto const& family_list = static_cast<StyleValueList const&>(font_family).values();
auto const& family_list = static_cast<StyleValueList const&>(*family_value).values();
for (auto const& family : family_list) { for (auto const& family : family_list) {
if (family->is_identifier()) { if (family->is_identifier()) {
found_font = find_generic_font(family->to_identifier()); found_font = find_generic_font(family->to_identifier());
@ -2249,10 +2233,10 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
if (found_font) if (found_font)
break; break;
} }
} else if (family_value->is_identifier()) { } else if (font_family.is_identifier()) {
found_font = find_generic_font(family_value->to_identifier()); found_font = find_generic_font(font_family.to_identifier());
} else if (family_value->is_string()) { } else if (font_family.is_string()) {
found_font = find_font(family_value->to_string().release_value_but_fixme_should_propagate_errors()); found_font = find_font(font_family.to_string().release_value_but_fixme_should_propagate_errors());
} }
if (!found_font) { if (!found_font) {
@ -2265,8 +2249,30 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
FontCache::the().set(font_selector, *found_font); FontCache::the().set(font_selector, *found_font);
style.set_property(CSS::PropertyID::FontSize, LengthStyleValue::create(CSS::Length::make_px(font_size_in_px)).release_value_but_fixme_should_propagate_errors(), nullptr); return found_font;
style.set_property(CSS::PropertyID::FontWeight, NumberStyleValue::create(weight).release_value_but_fixme_should_propagate_errors(), nullptr); }
void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* element, Optional<CSS::Selector::PseudoElement> pseudo_element) const
{
// To compute the font, first ensure that we've defaulted the relevant CSS font properties.
// FIXME: This should be more sophisticated.
compute_defaulted_property_value(style, element, CSS::PropertyID::FontFamily, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontSize, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontStretch, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontStyle, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::FontWeight, pseudo_element);
compute_defaulted_property_value(style, element, CSS::PropertyID::LineHeight, pseudo_element);
auto font_family = style.property(CSS::PropertyID::FontFamily);
auto font_size = style.property(CSS::PropertyID::FontSize);
auto font_style = style.property(CSS::PropertyID::FontStyle);
auto font_weight = style.property(CSS::PropertyID::FontWeight);
auto font_stretch = style.property(CSS::PropertyID::FontStretch);
auto found_font = compute_font_for_style_values(element, pseudo_element, font_family, font_size, font_style, font_weight, font_stretch);
style.set_property(CSS::PropertyID::FontSize, LengthStyleValue::create(CSS::Length::make_px(found_font->pixel_size())).release_value_but_fixme_should_propagate_errors(), nullptr);
style.set_property(CSS::PropertyID::FontWeight, NumberStyleValue::create(found_font->weight()).release_value_but_fixme_should_propagate_errors(), nullptr);
style.set_computed_font(found_font.release_nonnull()); style.set_computed_font(found_font.release_nonnull());

View file

@ -91,6 +91,8 @@ public:
void load_fonts_from_sheet(CSSStyleSheet const&); void load_fonts_from_sheet(CSSStyleSheet const&);
RefPtr<Gfx::Font const> compute_font_for_style_values(DOM::Element const* element, Optional<CSS::Selector::PseudoElement> pseudo_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch) const;
struct AnimationKey { struct AnimationKey {
CSS::CSSStyleDeclaration const* source_declaration; CSS::CSSStyleDeclaration const* source_declaration;
DOM::Element const* element; DOM::Element const* element;