Skip to content

Commit 64c3319

Browse files
AtkinsSJkalenikaliaksandr
authored andcommitted
LibWeb/CSS: Take AbstractElement in font computation methods
1 parent 263c51f commit 64c3319

File tree

4 files changed

+24
-28
lines changed

4 files changed

+24
-28
lines changed

Libraries/LibWeb/CSS/StyleComputer.cpp

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -972,7 +972,7 @@ void StyleComputer::collect_animation_into(DOM::Element& element, Optional<CSS::
972972
return camel_case_string_from_property_id(a) < camel_case_string_from_property_id(b);
973973
};
974974

975-
compute_font(computed_properties, &element, pseudo_element);
975+
compute_font(computed_properties, DOM::AbstractElement { element, pseudo_element });
976976
compute_property_values(computed_properties);
977977
Length::FontMetrics font_metrics {
978978
computed_properties.font_size(),
@@ -1760,22 +1760,23 @@ CSSPixelFraction StyleComputer::absolute_size_mapping(Keyword keyword)
17601760
}
17611761
}
17621762

1763-
RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(DOM::Element const* element, Optional<CSS::PseudoElement> pseudo_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch, int math_depth) const
1763+
RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(Optional<DOM::AbstractElement> abstract_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch, int math_depth) const
17641764
{
1765-
auto parent_element = element ? element->element_to_inherit_style_from(pseudo_element) : nullptr;
1765+
auto parent_element = abstract_element.has_value() ? abstract_element->element_to_inherit_style_from() : OptionalNone {};
1766+
GC::Ptr element = abstract_element.has_value() ? &abstract_element->element() : nullptr;
17661767

17671768
auto width = font_stretch.to_font_width();
17681769
auto weight = font_weight.to_font_weight();
17691770

17701771
auto font_size_in_px = default_user_font_size();
17711772

17721773
Gfx::FontPixelMetrics font_pixel_metrics;
1773-
if (parent_element && parent_element->computed_properties())
1774+
if (parent_element.has_value() && parent_element->computed_properties())
17741775
font_pixel_metrics = parent_element->computed_properties()->first_available_computed_font().pixel_metrics();
17751776
else
17761777
font_pixel_metrics = Platform::FontPlugin::the().default_font(font_size_in_px.to_float())->pixel_metrics();
17771778
auto parent_font_size = [&]() -> CSSPixels {
1778-
if (!parent_element || !parent_element->computed_properties())
1779+
if (!parent_element.has_value() || !parent_element->computed_properties())
17791780
return font_size_in_px;
17801781
auto const& value = parent_element->computed_properties()->property(CSS::PropertyID::FontSize);
17811782
if (value.is_length()) {
@@ -1797,7 +1798,7 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
17971798
// If the specified value font-size is math then the computed value of font-size is obtained by multiplying
17981799
// the inherited value of font-size by a nonzero scale factor calculated by the following procedure:
17991800
// 1. Let A be the inherited math-depth value, B the computed math-depth value, C be 0.71 and S be 1.0
1800-
int inherited_math_depth = parent_element && parent_element->computed_properties()
1801+
int inherited_math_depth = parent_element.has_value() && parent_element->computed_properties()
18011802
? parent_element->computed_properties()->math_depth()
18021803
: InitialValues::math_depth();
18031804
int computed_math_depth = math_depth;
@@ -1837,7 +1838,7 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
18371838
// larger may compute the font size to the next entry in the table,
18381839
// and smaller may compute the font size to the previous entry in the table.
18391840
if (keyword == Keyword::Smaller || keyword == Keyword::Larger) {
1840-
if (parent_element && parent_element->computed_properties()) {
1841+
if (parent_element.has_value() && parent_element->computed_properties()) {
18411842
font_size_in_px = CSSPixels::nearest_value_for(parent_element->computed_properties()->first_available_computed_font().pixel_metrics().size);
18421843
}
18431844
}
@@ -1984,15 +1985,15 @@ RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(
19841985
return font_list;
19851986
}
19861987

1987-
void StyleComputer::compute_font(ComputedProperties& style, DOM::Element const* element, Optional<CSS::PseudoElement> pseudo_element) const
1988+
void StyleComputer::compute_font(ComputedProperties& style, Optional<DOM::AbstractElement> abstract_element) const
19881989
{
19891990
auto const& font_family = style.property(CSS::PropertyID::FontFamily);
19901991
auto const& font_size = style.property(CSS::PropertyID::FontSize);
19911992
auto const& font_style = style.property(CSS::PropertyID::FontStyle);
19921993
auto const& font_weight = style.property(CSS::PropertyID::FontWeight);
19931994
auto const& font_width = style.property(CSS::PropertyID::FontWidth);
19941995

1995-
auto font_list = compute_font_for_style_values(element, pseudo_element, font_family, font_size, font_style, font_weight, font_width, style.math_depth());
1996+
auto font_list = compute_font_for_style_values(abstract_element, font_family, font_size, font_style, font_weight, font_width, style.math_depth());
19961997
VERIFY(font_list);
19971998
VERIFY(!font_list->is_empty());
19981999

@@ -2009,7 +2010,7 @@ void StyleComputer::compute_font(ComputedProperties& style, DOM::Element const*
20092010

20102011
style.set_computed_font_list(*font_list);
20112012

2012-
if (element && is<HTML::HTMLHtmlElement>(*element)) {
2013+
if (abstract_element.has_value() && is<HTML::HTMLHtmlElement>(abstract_element->element())) {
20132014
const_cast<StyleComputer&>(*this).m_root_element_font_metrics = calculate_root_element_font_metrics(style);
20142015
}
20152016
}
@@ -2322,7 +2323,7 @@ GC::Ref<ComputedProperties> StyleComputer::create_document_style() const
23222323
}
23232324

23242325
compute_math_depth(style, {});
2325-
compute_font(style, nullptr, {});
2326+
compute_font(style, {});
23262327
compute_property_values(style);
23272328
style->set_property(CSS::PropertyID::Width, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect().width())));
23282329
style->set_property(CSS::PropertyID::Height, CSS::LengthStyleValue::create(CSS::Length::make_px(viewport_rect().height())));
@@ -2439,10 +2440,7 @@ static bool is_monospace(StyleValue const& value)
24392440
// instead of the default font size (16px).
24402441
// See this blog post for a lot more details about this weirdness:
24412442
// https://manishearth.github.io/blog/2017/08/10/font-size-an-unexpectedly-complex-css-property/
2442-
RefPtr<StyleValue const> StyleComputer::recascade_font_size_if_needed(
2443-
DOM::Element& element,
2444-
Optional<CSS::PseudoElement> pseudo_element,
2445-
CascadedProperties& cascaded_properties) const
2443+
RefPtr<StyleValue const> StyleComputer::recascade_font_size_if_needed(DOM::AbstractElement abstract_element, CascadedProperties& cascaded_properties) const
24462444
{
24472445
// Check for `font-family: monospace`. Note that `font-family: monospace, AnythingElse` does not trigger this path.
24482446
// Some CSS frameworks use `font-family: monospace, monospace` to work around this behavior.
@@ -2457,17 +2455,15 @@ RefPtr<StyleValue const> StyleComputer::recascade_font_size_if_needed(
24572455

24582456
// Reconstruct the line of ancestor elements we need to inherit style from, and then do the cascade again
24592457
// but only for the font-size property.
2460-
Vector<DOM::Element const&> ancestors;
2461-
if (pseudo_element.has_value())
2462-
ancestors.append(element);
2463-
for (auto ancestor = element.element_to_inherit_style_from(pseudo_element); ancestor; ancestor = ancestor->element_to_inherit_style_from({}))
2458+
Vector<DOM::AbstractElement> ancestors;
2459+
for (auto ancestor = abstract_element.element_to_inherit_style_from(); ancestor.has_value(); ancestor = ancestor->element_to_inherit_style_from())
24642460
ancestors.append(*ancestor);
24652461

24662462
NonnullRefPtr<StyleValue const> new_font_size = CSS::LengthStyleValue::create(CSS::Length::make_px(default_monospace_font_size_in_px));
24672463
CSSPixels current_size_in_px = default_monospace_font_size_in_px;
24682464

24692465
for (auto& ancestor : ancestors.in_reverse()) {
2470-
auto& ancestor_cascaded_properties = *ancestor.cascaded_properties({});
2466+
auto& ancestor_cascaded_properties = *ancestor.cascaded_properties();
24712467
auto font_size_value = ancestor_cascaded_properties.property(CSS::PropertyID::FontSize);
24722468

24732469
if (!font_size_value)
@@ -2508,7 +2504,7 @@ GC::Ref<ComputedProperties> StyleComputer::compute_properties(DOM::Element& elem
25082504
DOM::AbstractElement abstract_element { element, pseudo_element };
25092505
auto computed_style = document().heap().allocate<CSS::ComputedProperties>();
25102506

2511-
auto new_font_size = recascade_font_size_if_needed(element, pseudo_element, cascaded_properties);
2507+
auto new_font_size = recascade_font_size_if_needed(abstract_element, cascaded_properties);
25122508
if (new_font_size)
25132509
computed_style->set_property(PropertyID::FontSize, *new_font_size, ComputedProperties::Inherited::No, Important::No);
25142510

@@ -2630,7 +2626,7 @@ GC::Ref<ComputedProperties> StyleComputer::compute_properties(DOM::Element& elem
26302626
compute_math_depth(computed_style, abstract_element);
26312627

26322628
// 3. Compute the font, since that may be needed for font-relative CSS units
2633-
compute_font(computed_style, &element, pseudo_element);
2629+
compute_font(computed_style, DOM::AbstractElement { element, pseudo_element });
26342630

26352631
// 4. Convert properties into their computed forms
26362632
compute_property_values(computed_style);

Libraries/LibWeb/CSS/StyleComputer.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -176,9 +176,9 @@ class WEB_API StyleComputer final : public GC::Cell {
176176

177177
static CSSPixels default_user_font_size();
178178
static CSSPixelFraction absolute_size_mapping(Keyword);
179-
RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(DOM::Element const* element, Optional<CSS::PseudoElement> pseudo_element, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch, int math_depth = 0) const;
179+
RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(Optional<DOM::AbstractElement>, StyleValue const& font_family, StyleValue const& font_size, StyleValue const& font_style, StyleValue const& font_weight, StyleValue const& font_stretch, int math_depth = 0) const;
180180

181-
[[nodiscard]] RefPtr<StyleValue const> recascade_font_size_if_needed(DOM::Element&, Optional<CSS::PseudoElement> pseudo_element, CascadedProperties&) const;
181+
[[nodiscard]] RefPtr<StyleValue const> recascade_font_size_if_needed(DOM::AbstractElement, CascadedProperties&) const;
182182

183183
void set_viewport_rect(Badge<DOM::Document>, CSSPixelRect const& viewport_rect) { m_viewport_rect = viewport_rect; }
184184

@@ -192,7 +192,7 @@ class WEB_API StyleComputer final : public GC::Cell {
192192
[[nodiscard]] GC::Ref<ComputedProperties> compute_properties(DOM::Element&, Optional<PseudoElement>, CascadedProperties&) const;
193193

194194
void compute_property_values(ComputedProperties&) const;
195-
void compute_font(ComputedProperties&, DOM::Element const*, Optional<CSS::PseudoElement>) const;
195+
void compute_font(ComputedProperties&, Optional<DOM::AbstractElement>) const;
196196

197197
[[nodiscard]] inline bool should_reject_with_ancestor_filter(Selector const&) const;
198198

Libraries/LibWeb/DOM/Element.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ CSS::RequiredInvalidationAfterStyleChange Element::recompute_inherited_style()
835835
if (invalidation.is_none() && old_values_with_relative_units.is_empty())
836836
return invalidation;
837837

838-
document().style_computer().compute_font(*computed_properties, this, {});
838+
document().style_computer().compute_font(*computed_properties, AbstractElement { *this });
839839
document().style_computer().compute_property_values(*computed_properties);
840840

841841
for (auto [property_id, old_value] : old_values_with_relative_units) {

Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -103,9 +103,9 @@ class CanvasTextDrawingStyles {
103103
auto font_list = font_source.visit(
104104
[&](DOM::Document* document) -> RefPtr<Gfx::FontCascadeList const> {
105105
if constexpr (SameAs<CanvasType, HTML::HTMLCanvasElement>) {
106-
return document->style_computer().compute_font_for_style_values(&canvas_element, {}, font_family, font_size, font_style, font_weight, font_width);
106+
return document->style_computer().compute_font_for_style_values(DOM::AbstractElement { canvas_element, {} }, font_family, font_size, font_style, font_weight, font_width);
107107
} else {
108-
return document->style_computer().compute_font_for_style_values(nullptr, {}, font_family, font_size, font_style, font_weight, font_width);
108+
return document->style_computer().compute_font_for_style_values({}, font_family, font_size, font_style, font_weight, font_width);
109109
}
110110
},
111111
[](HTML::WorkerGlobalScope*) -> RefPtr<Gfx::FontCascadeList const> {

0 commit comments

Comments
 (0)