Skip to content

Commit dc41d04

Browse files
Calme1709AtkinsSJ
authored andcommitted
LibWeb: Store font-style in ComputedProperties in computed form
1 parent 10793ae commit dc41d04

File tree

7 files changed

+47
-11
lines changed

7 files changed

+47
-11
lines changed

Libraries/LibWeb/CSS/ComputedProperties.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
1919
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
2020
#include <LibWeb/CSS/StyleValues/FitContentStyleValue.h>
21+
#include <LibWeb/CSS/StyleValues/FontStyleStyleValue.h>
2122
#include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
2223
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
2324
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
@@ -2114,4 +2115,9 @@ Percentage ComputedProperties::font_width() const
21142115
return property(PropertyID::FontWidth).as_percentage().percentage();
21152116
}
21162117

2118+
int ComputedProperties::font_slope() const
2119+
{
2120+
return property(PropertyID::FontStyle).as_font_style().to_font_slope();
2121+
}
2122+
21172123
}

Libraries/LibWeb/CSS/ComputedProperties.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ class WEB_API ComputedProperties final : public JS::Cell {
236236
[[nodiscard]] CSSPixels font_size() const;
237237
double font_weight() const;
238238
Percentage font_width() const;
239+
int font_slope() const;
239240

240241
bool operator==(ComputedProperties const&) const;
241242

Libraries/LibWeb/CSS/StyleComputer.cpp

Lines changed: 31 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,9 @@ void StyleComputer::collect_animation_into(DOM::AbstractElement abstract_element
10781078
if (auto const& font_width_specified_value = specified_values.get(PropertyID::FontWidth); font_width_specified_value.has_value())
10791079
result.set(PropertyID::FontWidth, compute_font_width(*font_width_specified_value.value(), parent_length_resolution_context));
10801080

1081+
if (auto const& font_style_specified_value = specified_values.get(PropertyID::FontStyle); font_style_specified_value.has_value())
1082+
result.set(PropertyID::FontStyle, compute_font_style(*font_style_specified_value.value(), parent_length_resolution_context));
1083+
10811084
PropertyValueComputationContext property_value_computation_context {
10821085
.length_resolution_context = {
10831086
.viewport_rect = viewport_rect(),
@@ -1849,16 +1852,14 @@ CSSPixels StyleComputer::relative_size_mapping(RelativeSize relative_size, CSSPi
18491852
VERIFY_NOT_REACHED();
18501853
}
18511854

1852-
RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, StyleValue const& font_style, double font_weight, Percentage const& font_width) const
1855+
RefPtr<Gfx::FontCascadeList const> StyleComputer::compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, int slope, double font_weight, Percentage const& font_width) const
18531856
{
18541857
// FIXME: We round to int here as that is what is expected by our font infrastructure below
18551858
auto width = round_to<int>(font_width.value());
18561859

18571860
// FIXME: We round to int here as that is what is expected by our font infrastructure below
18581861
auto weight = round_to<int>(font_weight);
18591862

1860-
auto slope = font_style.as_font_style().to_font_slope();
1861-
18621863
// FIXME: Implement the full font-matching algorithm: https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm
18631864

18641865
float const font_size_in_pt = font_size * 0.75f;
@@ -2002,10 +2003,16 @@ void StyleComputer::compute_font(ComputedProperties& style, Optional<DOM::Abstra
20022003
compute_font_width(font_width_specified_value, length_resolution_context),
20032004
style.is_property_inherited(PropertyID::FontWidth) ? ComputedProperties::Inherited::Yes : ComputedProperties::Inherited::No);
20042005

2006+
auto const& font_style_specified_value = style.property(PropertyID::FontStyle, ComputedProperties::WithAnimationsApplied::No);
2007+
2008+
style.set_property(
2009+
PropertyID::FontStyle,
2010+
compute_font_style(font_style_specified_value, length_resolution_context),
2011+
style.is_property_inherited(PropertyID::FontStyle) ? ComputedProperties::Inherited::Yes : ComputedProperties::Inherited::No);
2012+
20052013
auto const& font_family = style.property(CSS::PropertyID::FontFamily);
2006-
auto const& font_style = style.property(CSS::PropertyID::FontStyle);
20072014

2008-
auto font_list = compute_font_for_style_values(font_family, style.font_size(), font_style, style.font_weight(), style.font_width());
2015+
auto font_list = compute_font_for_style_values(font_family, style.font_size(), style.font_slope(), style.font_weight(), style.font_width());
20092016
VERIFY(font_list);
20102017
VERIFY(!font_list->is_empty());
20112018

@@ -3261,6 +3268,25 @@ NonnullRefPtr<StyleValue const> StyleComputer::compute_font_size(NonnullRefPtr<S
32613268
VERIFY_NOT_REACHED();
32623269
}
32633270

3271+
NonnullRefPtr<StyleValue const> StyleComputer::compute_font_style(NonnullRefPtr<StyleValue const> const& specified_value, Length::ResolutionContext const& parent_length_resolution_context)
3272+
{
3273+
// https://drafts.csswg.org/css-fonts-4/#font-style-prop
3274+
// the keyword specified, plus angle in degrees if specified
3275+
3276+
auto const& angle_value = specified_value->as_font_style().angle();
3277+
3278+
if (!angle_value)
3279+
return specified_value;
3280+
3281+
if (angle_value->is_angle())
3282+
return FontStyleStyleValue::create(specified_value->as_font_style().font_style(), AngleStyleValue::create(Angle::make_degrees(angle_value->as_angle().angle().to_degrees())));
3283+
3284+
if (angle_value->is_calculated())
3285+
return FontStyleStyleValue::create(specified_value->as_font_style().font_style(), AngleStyleValue::create(angle_value->as_calculated().resolve_angle({ .length_resolution_context = parent_length_resolution_context }).value()));
3286+
3287+
VERIFY_NOT_REACHED();
3288+
}
3289+
32643290
NonnullRefPtr<StyleValue const> StyleComputer::compute_font_weight(NonnullRefPtr<StyleValue const> const& specified_value, double inherited_font_weight, Length::ResolutionContext const& parent_length_resolution_context)
32653291
{
32663292
// https://drafts.csswg.org/css-fonts-4/#font-weight-prop

Libraries/LibWeb/CSS/StyleComputer.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ class WEB_API StyleComputer final : public GC::Cell {
177177
static CSSPixels default_user_font_size();
178178
static CSSPixels absolute_size_mapping(AbsoluteSize, CSSPixels default_font_size);
179179
static CSSPixels relative_size_mapping(RelativeSize, CSSPixels inherited_font_size);
180-
RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, StyleValue const& font_style, double font_weight, Percentage const& font_width) const;
180+
RefPtr<Gfx::FontCascadeList const> compute_font_for_style_values(StyleValue const& font_family, CSSPixels const& font_size, int font_slope, double font_weight, Percentage const& font_width) const;
181181
[[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; }
@@ -205,6 +205,7 @@ class WEB_API StyleComputer final : public GC::Cell {
205205
static NonnullRefPtr<StyleValue const> compute_value_of_property(PropertyID, NonnullRefPtr<StyleValue const> const& specified_value, Function<NonnullRefPtr<StyleValue const>(PropertyID)> const& get_property_specified_value, PropertyValueComputationContext const&);
206206
static NonnullRefPtr<StyleValue const> compute_border_or_outline_width(NonnullRefPtr<StyleValue const> const& specified_value, NonnullRefPtr<StyleValue const> const& style_specified_value, PropertyValueComputationContext const&);
207207
static NonnullRefPtr<StyleValue const> compute_font_size(NonnullRefPtr<StyleValue const> const& specified_value, int computed_math_depth, CSSPixels inherited_font_size, int inherited_math_depth, Length::ResolutionContext const& parent_length_resolution_context);
208+
static NonnullRefPtr<StyleValue const> compute_font_style(NonnullRefPtr<StyleValue const> const& specified_value, Length::ResolutionContext const& parent_length_resolution_context);
208209
static NonnullRefPtr<StyleValue const> compute_font_weight(NonnullRefPtr<StyleValue const> const& specified_value, double inherited_font_weight, Length::ResolutionContext const& parent_length_resolution_context);
209210
static NonnullRefPtr<StyleValue const> compute_font_width(NonnullRefPtr<StyleValue const> const& specified_value, Length::ResolutionContext const& parent_length_resolution_context);
210211
static NonnullRefPtr<StyleValue const> compute_opacity(NonnullRefPtr<StyleValue const> const& specified_value, PropertyValueComputationContext const&);

Libraries/LibWeb/HTML/Canvas/CanvasTextDrawingStyles.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include <LibWeb/CSS/Parser/Parser.h>
1010
#include <LibWeb/CSS/StyleComputer.h>
11+
#include <LibWeb/CSS/StyleValues/FontStyleStyleValue.h>
1112
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
1213
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
1314
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
@@ -128,11 +129,12 @@ class CanvasTextDrawingStyles {
128129
auto const& computed_font_size = CSS::StyleComputer::compute_font_size(font_size, computed_math_depth, inherited_font_size, inherited_math_depth, length_resolution_context);
129130
auto const& computed_font_weight = CSS::StyleComputer::compute_font_weight(font_weight, inherited_font_weight, length_resolution_context);
130131
auto const& computed_font_width = CSS::StyleComputer::compute_font_width(font_width, length_resolution_context);
132+
auto const& computed_font_style = CSS::StyleComputer::compute_font_style(font_style, length_resolution_context);
131133

132134
return document->style_computer().compute_font_for_style_values(
133135
font_family,
134136
computed_font_size->as_length().length().absolute_length_to_px(),
135-
font_style,
137+
computed_font_style->as_font_style().to_font_slope(),
136138
computed_font_weight->as_number().number(),
137139
computed_font_width->as_percentage().percentage());
138140
},
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
oblique calc(57.29578deg * sign(1em - 1px))
1+
oblique 57.29578deg

Tests/LibWeb/Text/expected/wpt-import/css/css-fonts/variations/font-style-parsing.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ Harness status: OK
22

33
Found 43 tests
44

5-
40 Pass
6-
3 Fail
5+
41 Pass
6+
2 Fail
77
Pass Font-style (supports): 'italic' is valid
88
Pass Font-style (supports): 'italic' followed by angle is invalid
99
Pass Font-style (supports): 'italic' followed by non-number is invalid
@@ -46,4 +46,4 @@ Pass Font-style (computed): 'oblique' followed by positive angle is valid
4646
Pass Font-style (computed): 'oblique' followed by calc is valid
4747
Fail Font-style (computed): 'oblique' followed by calc is valid even if it must be clamped (no computation)
4848
Fail Font-style (computed): 'oblique' followed by calc is valid even if it must be clamped (with computation)
49-
Fail Font-style (computed): 'oblique' followed by calc is valid even if it mixes units (with computation)
49+
Pass Font-style (computed): 'oblique' followed by calc is valid even if it mixes units (with computation)

0 commit comments

Comments
 (0)