LibWeb: Parse oblique font-style with an angle value

This commit is contained in:
Tim Ledbetter 2025-05-02 13:55:58 +01:00 committed by Andreas Kling
commit c0f9b11070
Notes: github-actions[bot] 2025-05-03 10:06:24 +00:00
13 changed files with 172 additions and 31 deletions

View file

@ -0,0 +1,42 @@
/*
* Copyright (c) 2025, Tim Ledbetter <tim.ledbetter@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include "FontStyleStyleValue.h"
#include <LibWeb/CSS/CSSStyleValue.h>
#include <LibWeb/CSS/Serialize.h>
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
namespace Web::CSS {
FontStyleStyleValue::FontStyleStyleValue(FontStyle font_style, ValueComparingRefPtr<CSSStyleValue const> angle_value)
: StyleValueWithDefaultOperators(Type::FontStyle)
, m_font_style(font_style)
, m_angle_value(angle_value)
{
}
FontStyleStyleValue::~FontStyleStyleValue() = default;
String FontStyleStyleValue::to_string(SerializationMode mode) const
{
Optional<String> angle_string;
if (m_angle_value) {
angle_string = m_angle_value->to_string(mode);
if (m_font_style == FontStyle::Oblique && angle_string == "0deg"sv)
return "normal"_string;
}
StringBuilder builder;
builder.append(CSS::to_string(m_font_style));
// https://drafts.csswg.org/css-fonts/#valdef-font-style-oblique-angle--90deg-90deg
// The lack of an <angle> represents 14deg. (Note that a font might internally provide its own mapping for "oblique", but the mapping within the font is disregarded.)
static auto default_angle = Angle::make_degrees(14);
if (angle_string.has_value() && !(m_angle_value->is_angle() && m_angle_value->as_angle().angle() == default_angle))
builder.appendff(" {}", angle_string);
return MUST(builder.to_string());
}
}

View file

@ -0,0 +1,45 @@
/*
* Copyright (c) 2025, Tim Ledbetter <tim.ledbetter@ladybird.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <LibWeb/CSS/CSSStyleValue.h>
#include <LibWeb/CSS/Enums.h>
namespace Web::CSS {
class FontStyleStyleValue final : public StyleValueWithDefaultOperators<FontStyleStyleValue> {
public:
static ValueComparingNonnullRefPtr<FontStyleStyleValue const> create(FontStyle font_style, ValueComparingRefPtr<CSSStyleValue const> angle_value = {})
{
return adopt_ref(*new (nothrow) FontStyleStyleValue(font_style, angle_value));
}
virtual ~FontStyleStyleValue() override;
FontStyle font_style() const { return m_font_style; }
ValueComparingRefPtr<CSSStyleValue const> angle() const { return m_angle_value; }
virtual String to_string(SerializationMode) const override;
bool equals(CSSStyleValue const& other) const override
{
if (type() != other.type())
return false;
auto const& other_font_style = other.as_font_style();
return m_font_style == other_font_style.m_font_style && m_angle_value == other_font_style.m_angle_value;
}
bool properties_equal(FontStyleStyleValue const& other) const { return m_font_style == other.m_font_style && m_angle_value == other.m_angle_value; }
private:
FontStyleStyleValue(FontStyle, ValueComparingRefPtr<CSSStyleValue const> angle_value);
FontStyle m_font_style;
ValueComparingRefPtr<CSSStyleValue const> m_angle_value;
};
}

View file

@ -233,8 +233,9 @@ String ShorthandStyleValue::to_string(SerializationMode mode) const
builder.append(' ');
builder.append(string);
};
if (font_style->to_keyword() != Keyword::Normal && font_style->to_keyword() != Keyword::Initial)
append(font_style->to_string(mode));
auto font_style_string = font_style->to_string(mode);
if (font_style_string != "normal"sv)
append(font_style_string);
if (font_variant_string != "normal"sv && font_variant_string != "initial"sv)
append(font_variant_string);
if (font_weight->to_font_weight() != Gfx::FontWeight::Regular && font_weight->to_keyword() != Keyword::Initial)