/* * 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 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); } #define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \ TitleCaseName##StyleValue const& StyleValue::as_##SnakeCaseName() const \ { \ VERIFY(is_##SnakeCaseName()); \ return static_cast(*this); \ } ENUMERATE_STYLE_VALUE_TYPES #undef __ENUMERATE_STYLE_VALUE_TYPE ValueComparingNonnullRefPtr 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; } int StyleValue::to_font_stretch_width() const { int width = Gfx::FontWidth::Normal; if (is_identifier()) { switch (static_cast(*this).id()) { case CSS::ValueID::UltraCondensed: width = Gfx::FontWidth::UltraCondensed; break; case CSS::ValueID::ExtraCondensed: width = Gfx::FontWidth::ExtraCondensed; break; case CSS::ValueID::Condensed: width = Gfx::FontWidth::Condensed; break; case CSS::ValueID::SemiCondensed: width = Gfx::FontWidth::SemiCondensed; break; case CSS::ValueID::Normal: width = Gfx::FontWidth::Normal; break; case CSS::ValueID::SemiExpanded: width = Gfx::FontWidth::SemiExpanded; break; case CSS::ValueID::Expanded: width = Gfx::FontWidth::Expanded; break; case CSS::ValueID::ExtraExpanded: width = Gfx::FontWidth::ExtraExpanded; break; case CSS::ValueID::UltraExpanded: width = Gfx::FontWidth::UltraExpanded; break; default: break; } } else if (is_percentage()) { float percentage = as_percentage().percentage().value(); if (percentage <= 50) { width = Gfx::FontWidth::UltraCondensed; } else if (percentage <= 62.5f) { width = Gfx::FontWidth::ExtraCondensed; } else if (percentage <= 75.0f) { width = Gfx::FontWidth::Condensed; } else if (percentage <= 87.5f) { width = Gfx::FontWidth::SemiCondensed; } else if (percentage <= 100.0f) { width = Gfx::FontWidth::Normal; } else if (percentage <= 112.5f) { width = Gfx::FontWidth::SemiExpanded; } else if (percentage <= 125.0f) { width = Gfx::FontWidth::Expanded; } else if (percentage <= 150.0f) { width = Gfx::FontWidth::ExtraExpanded; } else { width = Gfx::FontWidth::UltraExpanded; } } return width; } }