/* * Copyright (c) 2018-2023, Andreas Kling * Copyright (c) 2021-2023, Sam Atkins * Copyright (c) 2021, Tobias Christiansen * Copyright (c) 2022-2023, MacDue * * SPDX-License-Identifier: BSD-2-Clause */ #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace Web::CSS { StyleValue::StyleValue(Type type) : m_type(type) { } AbstractImageStyleValue const& StyleValue::as_abstract_image() const { VERIFY(is_abstract_image()); return static_cast(*this); } AngleStyleValue const& StyleValue::as_angle() const { VERIFY(is_angle()); return static_cast(*this); } BackgroundStyleValue const& StyleValue::as_background() const { VERIFY(is_background()); return static_cast(*this); } BackgroundRepeatStyleValue const& StyleValue::as_background_repeat() const { VERIFY(is_background_repeat()); return static_cast(*this); } BackgroundSizeStyleValue const& StyleValue::as_background_size() const { VERIFY(is_background_size()); return static_cast(*this); } BorderStyleValue const& StyleValue::as_border() const { VERIFY(is_border()); return static_cast(*this); } BorderRadiusStyleValue const& StyleValue::as_border_radius() const { VERIFY(is_border_radius()); return static_cast(*this); } EasingStyleValue const& StyleValue::as_easing() const { VERIFY(is_easing()); return static_cast(*this); } BorderRadiusShorthandStyleValue const& StyleValue::as_border_radius_shorthand() const { VERIFY(is_border_radius_shorthand()); return static_cast(*this); } ShadowStyleValue const& StyleValue::as_shadow() const { VERIFY(is_shadow()); return static_cast(*this); } CalculatedStyleValue const& StyleValue::as_calculated() const { VERIFY(is_calculated()); return static_cast(*this); } ColorStyleValue const& StyleValue::as_color() const { VERIFY(is_color()); return static_cast(*this); } CompositeStyleValue const& StyleValue::as_composite() const { VERIFY(is_composite()); return static_cast(*this); } ConicGradientStyleValue const& StyleValue::as_conic_gradient() const { VERIFY(is_conic_gradient()); return static_cast(*this); } ContentStyleValue const& StyleValue::as_content() const { VERIFY(is_content()); return static_cast(*this); } CustomIdentStyleValue const& StyleValue::as_custom_ident() const { VERIFY(is_custom_ident()); return static_cast(*this); } DisplayStyleValue const& StyleValue::as_display() const { VERIFY(is_display()); return static_cast(*this); } EdgeStyleValue const& StyleValue::as_edge() const { VERIFY(is_edge()); return static_cast(*this); } FilterValueListStyleValue const& StyleValue::as_filter_value_list() const { VERIFY(is_filter_value_list()); return static_cast(*this); } FlexStyleValue const& StyleValue::as_flex() const { VERIFY(is_flex()); return static_cast(*this); } FlexFlowStyleValue const& StyleValue::as_flex_flow() const { VERIFY(is_flex_flow()); return static_cast(*this); } FontStyleValue const& StyleValue::as_font() const { VERIFY(is_font()); return static_cast(*this); } FrequencyStyleValue const& StyleValue::as_frequency() const { VERIFY(is_frequency()); return static_cast(*this); } GridTrackPlacementShorthandStyleValue const& StyleValue::as_grid_track_placement_shorthand() const { VERIFY(is_grid_track_placement_shorthand()); return static_cast(*this); } GridAreaShorthandStyleValue const& StyleValue::as_grid_area_shorthand() const { VERIFY(is_grid_area_shorthand()); return static_cast(*this); } GridTemplateAreaStyleValue const& StyleValue::as_grid_template_area() const { VERIFY(is_grid_template_area()); return static_cast(*this); } GridTrackPlacementStyleValue const& StyleValue::as_grid_track_placement() const { VERIFY(is_grid_track_placement()); return static_cast(*this); } IdentifierStyleValue const& StyleValue::as_identifier() const { VERIFY(is_identifier()); return static_cast(*this); } ImageStyleValue const& StyleValue::as_image() const { VERIFY(is_image()); return static_cast(*this); } InheritStyleValue const& StyleValue::as_inherit() const { VERIFY(is_inherit()); return static_cast(*this); } InitialStyleValue const& StyleValue::as_initial() const { VERIFY(is_initial()); return static_cast(*this); } IntegerStyleValue const& StyleValue::as_integer() const { VERIFY(is_integer()); return static_cast(*this); } LengthStyleValue const& StyleValue::as_length() const { VERIFY(is_length()); return static_cast(*this); } GridTrackSizeListStyleValue const& StyleValue::as_grid_track_size_list() const { VERIFY(is_grid_track_size_list()); return static_cast(*this); } GridTrackSizeListShorthandStyleValue const& StyleValue::as_grid_track_size_list_shorthand() const { VERIFY(is_grid_track_size_list_shorthand()); return static_cast(*this); } LinearGradientStyleValue const& StyleValue::as_linear_gradient() const { VERIFY(is_linear_gradient()); return static_cast(*this); } ListStyleStyleValue const& StyleValue::as_list_style() const { VERIFY(is_list_style()); return static_cast(*this); } NumberStyleValue const& StyleValue::as_number() const { VERIFY(is_number()); return static_cast(*this); } OverflowStyleValue const& StyleValue::as_overflow() const { VERIFY(is_overflow()); return static_cast(*this); } PercentageStyleValue const& StyleValue::as_percentage() const { VERIFY(is_percentage()); return static_cast(*this); } PlaceContentStyleValue const& StyleValue::as_place_content() const { VERIFY(is_place_content()); return static_cast(*this); } PlaceItemsStyleValue const& StyleValue::as_place_items() const { VERIFY(is_place_items()); return static_cast(*this); } PlaceSelfStyleValue const& StyleValue::as_place_self() const { VERIFY(is_place_self()); return static_cast(*this); } PositionStyleValue const& StyleValue::as_position() const { VERIFY(is_position()); return static_cast(*this); } RadialGradientStyleValue const& StyleValue::as_radial_gradient() const { VERIFY(is_radial_gradient()); return static_cast(*this); } RatioStyleValue const& StyleValue::as_ratio() const { VERIFY(is_ratio()); return static_cast(*this); } RectStyleValue const& StyleValue::as_rect() const { VERIFY(is_rect()); return static_cast(*this); } ResolutionStyleValue const& StyleValue::as_resolution() const { VERIFY(is_resolution()); return static_cast(*this); } StringStyleValue const& StyleValue::as_string() const { VERIFY(is_string()); return static_cast(*this); } TextDecorationStyleValue const& StyleValue::as_text_decoration() const { VERIFY(is_text_decoration()); return static_cast(*this); } TimeStyleValue const& StyleValue::as_time() const { VERIFY(is_time()); return static_cast(*this); } TransformationStyleValue const& StyleValue::as_transformation() const { VERIFY(is_transformation()); return static_cast(*this); } UnresolvedStyleValue const& StyleValue::as_unresolved() const { VERIFY(is_unresolved()); return static_cast(*this); } UnsetStyleValue const& StyleValue::as_unset() const { VERIFY(is_unset()); return static_cast(*this); } URLStyleValue const& StyleValue::as_url() const { VERIFY(is_url()); return static_cast(*this); } StyleValueList const& StyleValue::as_value_list() const { VERIFY(is_value_list()); return static_cast(*this); } ErrorOr> StyleValue::absolutized(CSSPixelRect const&, Length::FontMetrics const&, Length::FontMetrics const&) const { return *this; } bool StyleValue::has_auto() const { return is_identifier() && as_identifier().id() == ValueID::Auto; } ValueID StyleValue::to_identifier() const { if (is_identifier()) return as_identifier().id(); return ValueID::Invalid; } int StyleValue::to_font_weight() const { if (is_identifier()) { switch (static_cast(*this).id()) { case CSS::ValueID::Normal: return Gfx::FontWeight::Regular; case CSS::ValueID::Bold: return Gfx::FontWeight::Bold; case CSS::ValueID::Lighter: // FIXME: This should be relative to the parent. return Gfx::FontWeight::Regular; case CSS::ValueID::Bolder: // FIXME: This should be relative to the parent. return Gfx::FontWeight::Bold; default: return Gfx::FontWeight::Regular; } } if (is_number()) { return round_to(as_number().number()); } if (is_calculated()) { auto maybe_weight = const_cast(as_calculated()).resolve_integer(); if (maybe_weight.has_value()) return maybe_weight.value(); } return Gfx::FontWeight::Regular; } int StyleValue::to_font_slope() const { // FIXME: Implement oblique if (is_identifier()) { switch (static_cast(*this).id()) { case CSS::ValueID::Italic: { static int italic_slope = Gfx::name_to_slope("Italic"sv); return italic_slope; } case CSS::ValueID::Oblique: static int oblique_slope = Gfx::name_to_slope("Oblique"sv); return oblique_slope; case CSS::ValueID::Normal: default: break; } } static int normal_slope = Gfx::name_to_slope("Normal"sv); return normal_slope; } }