2020-01-18 05:38:21 -03:00
|
|
|
/*
|
2023-05-24 10:28:09 -03:00
|
|
|
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
|
2022-11-08 13:31:01 -03:00
|
|
|
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
|
2022-01-24 14:38:29 -03:00
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
2023-01-13 20:44:44 -03:00
|
|
|
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
|
2020-01-18 05:38:21 -03:00
|
|
|
*
|
2021-04-22 05:24:48 -03:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 05:38:21 -03:00
|
|
|
*/
|
|
|
|
|
|
2023-07-09 05:10:17 -03:00
|
|
|
#include <LibGfx/Font/FontDatabase.h>
|
2023-05-24 10:28:09 -03:00
|
|
|
#include <LibGfx/Font/FontStyleMapping.h>
|
2020-03-07 06:32:51 -03:00
|
|
|
#include <LibWeb/CSS/StyleValue.h>
|
2023-03-24 13:42:50 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
|
2023-03-23 14:26:13 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
|
2023-03-23 14:44:13 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BackgroundRepeatStyleValue.h>
|
2023-03-23 14:54:05 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BackgroundSizeStyleValue.h>
|
2023-03-24 21:33:20 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/BorderRadiusStyleValue.h>
|
2023-03-30 13:34:14 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
2023-03-23 18:12:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ColorStyleValue.h>
|
2023-03-24 13:09:41 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ConicGradientStyleValue.h>
|
2023-03-23 18:17:43 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ContentStyleValue.h>
|
2023-05-25 08:43:52 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
|
2023-04-26 16:05:38 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/DisplayStyleValue.h>
|
2023-07-05 19:59:36 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/EasingStyleValue.h>
|
2023-04-02 18:56:45 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/EdgeStyleValue.h>
|
2023-03-23 18:26:03 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FilterValueListStyleValue.h>
|
2023-09-28 11:18:14 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FlexStyleValue.h>
|
2023-03-23 18:50:24 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/FrequencyStyleValue.h>
|
2023-08-17 15:25:18 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridAutoFlowStyleValue.h>
|
2023-03-24 11:25:00 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridTemplateAreaStyleValue.h>
|
2023-03-24 11:53:08 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridTrackPlacementStyleValue.h>
|
2023-04-29 12:59:07 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/GridTrackSizeListStyleValue.h>
|
2023-03-24 12:04:24 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/IdentifierStyleValue.h>
|
2023-03-24 12:17:11 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ImageStyleValue.h>
|
2023-03-24 13:45:58 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/InheritStyleValue.h>
|
2023-03-24 13:55:44 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/InitialStyleValue.h>
|
2023-06-01 13:01:09 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
|
2023-03-24 14:04:04 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/LengthStyleValue.h>
|
2023-03-24 13:17:50 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/LinearGradientStyleValue.h>
|
2023-09-07 11:29:54 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/MathDepthStyleValue.h>
|
2023-06-01 12:16:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
|
2023-03-24 14:28:43 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
|
2023-03-24 14:35:31 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/PositionStyleValue.h>
|
2023-03-24 13:35:18 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RadialGradientStyleValue.h>
|
2023-06-06 11:42:43 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RatioStyleValue.h>
|
2023-03-24 21:02:50 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RectStyleValue.h>
|
2023-03-24 14:40:28 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ResolutionStyleValue.h>
|
2023-08-20 09:48:13 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/RevertStyleValue.h>
|
2023-03-24 14:45:25 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ShadowStyleValue.h>
|
2023-09-19 08:21:15 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/ShorthandStyleValue.h>
|
2023-03-24 14:48:42 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
|
2023-03-24 21:12:21 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
|
2023-03-24 20:47:56 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TimeStyleValue.h>
|
2023-03-24 14:59:33 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/TransformationStyleValue.h>
|
2023-04-20 14:31:00 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/URLStyleValue.h>
|
2023-03-24 20:53:41 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/UnresolvedStyleValue.h>
|
2023-03-24 20:56:56 -03:00
|
|
|
#include <LibWeb/CSS/StyleValues/UnsetStyleValue.h>
|
2019-06-22 16:48:21 -03:00
|
|
|
|
2020-07-26 15:01:35 -03:00
|
|
|
namespace Web::CSS {
|
2020-03-07 06:27:02 -03:00
|
|
|
|
2019-06-22 16:48:21 -03:00
|
|
|
StyleValue::StyleValue(Type type)
|
|
|
|
|
: m_type(type)
|
|
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-30 21:11:59 -03:00
|
|
|
AbstractImageStyleValue const& StyleValue::as_abstract_image() const
|
|
|
|
|
{
|
|
|
|
|
VERIFY(is_abstract_image());
|
|
|
|
|
return static_cast<AbstractImageStyleValue const&>(*this);
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-20 09:48:13 -03:00
|
|
|
#define __ENUMERATE_STYLE_VALUE_TYPE(TitleCaseName, SnakeCaseName) \
|
|
|
|
|
TitleCaseName##StyleValue const& StyleValue::as_##SnakeCaseName() const \
|
|
|
|
|
{ \
|
|
|
|
|
VERIFY(is_##SnakeCaseName()); \
|
|
|
|
|
return static_cast<TitleCaseName##StyleValue const&>(*this); \
|
|
|
|
|
}
|
|
|
|
|
ENUMERATE_STYLE_VALUE_TYPES
|
|
|
|
|
#undef __ENUMERATE_STYLE_VALUE_TYPE
|
2021-09-23 15:54:19 -03:00
|
|
|
|
2023-08-19 11:33:21 -03:00
|
|
|
ValueComparingNonnullRefPtr<StyleValue const> StyleValue::absolutized(CSSPixelRect const&, Length::FontMetrics const&, Length::FontMetrics const&) const
|
2023-03-31 10:25:38 -03:00
|
|
|
{
|
|
|
|
|
return *this;
|
|
|
|
|
}
|
|
|
|
|
|
2023-04-19 08:00:38 -03:00
|
|
|
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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-05-24 10:28:09 -03:00
|
|
|
int StyleValue::to_font_weight() const
|
|
|
|
|
{
|
|
|
|
|
if (is_identifier()) {
|
|
|
|
|
switch (static_cast<IdentifierStyleValue const&>(*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;
|
|
|
|
|
}
|
|
|
|
|
}
|
2023-06-01 13:01:09 -03:00
|
|
|
if (is_number()) {
|
|
|
|
|
return round_to<int>(as_number().number());
|
2023-05-24 10:28:09 -03:00
|
|
|
}
|
|
|
|
|
if (is_calculated()) {
|
|
|
|
|
auto maybe_weight = const_cast<CalculatedStyleValue&>(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 <angle>
|
|
|
|
|
if (is_identifier()) {
|
|
|
|
|
switch (static_cast<IdentifierStyleValue const&>(*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;
|
|
|
|
|
}
|
|
|
|
|
|
2023-08-07 16:44:10 -03:00
|
|
|
int StyleValue::to_font_stretch_width() const
|
|
|
|
|
{
|
|
|
|
|
int width = Gfx::FontWidth::Normal;
|
|
|
|
|
if (is_identifier()) {
|
|
|
|
|
switch (static_cast<IdentifierStyleValue const&>(*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;
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-07 06:27:02 -03:00
|
|
|
}
|