1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 17:07:34 +00:00

LibWeb: Parse CSS "font-variant" as part of "font"

This allows us to parse CSS "font" values that contain e.g "small-caps"
or "normal", as used on Acid3.
This commit is contained in:
Andreas Kling 2022-03-23 14:54:21 +01:00
parent 632928a11e
commit 5118a4c1e7
6 changed files with 37 additions and 1 deletions

View file

@ -16,6 +16,7 @@ class InitialValues {
public:
static float font_size() { return 10; }
static int font_weight() { return 400; }
static CSS::FontVariant font_variant() { return CSS::FontVariant::Normal; }
static CSS::Float float_() { return CSS::Float::None; }
static CSS::Clear clear() { return CSS::Clear::None; }
static CSS::Cursor cursor() { return CSS::Cursor::Auto; }
@ -178,6 +179,7 @@ public:
float font_size() const { return m_inherited.font_size; }
int font_weight() const { return m_inherited.font_weight; }
CSS::FontVariant font_variant() const { return m_inherited.font_variant; }
ComputedValues clone_inherited_values() const
{
@ -190,6 +192,7 @@ protected:
struct {
float font_size { InitialValues::font_size() };
int font_weight { InitialValues::font_weight() };
CSS::FontVariant font_variant { InitialValues::font_variant() };
Color color { InitialValues::color() };
CSS::Cursor cursor { InitialValues::cursor() };
CSS::ImageRendering image_rendering { InitialValues::image_rendering() };
@ -261,6 +264,7 @@ class MutableComputedValues final : public ComputedValues {
public:
void set_font_size(float font_size) { m_inherited.font_size = font_size; }
void set_font_weight(int font_weight) { m_inherited.font_weight = font_weight; }
void set_font_variant(CSS::FontVariant font_variant) { m_inherited.font_variant = font_variant; }
void set_color(const Color& color) { m_inherited.color = color; }
void set_content(ContentData const& content) { m_noninherited.content = content; }
void set_cursor(CSS::Cursor cursor) { m_inherited.cursor = cursor; }

View file

@ -3590,7 +3590,8 @@ RefPtr<StyleValue> Parser::parse_font_value(Vector<StyleComponentValueRule> cons
RefPtr<StyleValue> font_size;
RefPtr<StyleValue> line_height;
RefPtr<StyleValue> font_families;
// FIXME: Implement font-stretch and font-variant.
RefPtr<StyleValue> font_variant;
// FIXME: Implement font-stretch.
// FIXME: Handle system fonts. (caption, icon, menu, message-box, small-caption, status-bar)
@ -3620,6 +3621,12 @@ RefPtr<StyleValue> Parser::parse_font_value(Vector<StyleComponentValueRule> cons
font_weight = value.release_nonnull();
continue;
}
if (property_accepts_value(PropertyID::FontVariant, *value)) {
if (font_variant)
return nullptr;
font_variant = value.release_nonnull();
continue;
}
if (property_accepts_value(PropertyID::FontSize, *value)) {
if (font_size)
return nullptr;

View file

@ -999,4 +999,20 @@ Variant<CSS::VerticalAlign, CSS::LengthPercentage> StyleProperties::vertical_ali
VERIFY_NOT_REACHED();
}
Optional<CSS::FontVariant> StyleProperties::font_variant() const
{
auto value = property(CSS::PropertyID::FontVariant);
if (!value.has_value())
return {};
switch (value.value()->to_identifier()) {
case CSS::ValueID::Normal:
return CSS::FontVariant::Normal;
case CSS::ValueID::SmallCaps:
return CSS::FontVariant::SmallCaps;
default:
return {};
}
}
}

View file

@ -75,6 +75,7 @@ public:
CSS::BoxSizing box_sizing() const;
Optional<CSS::PointerEvents> pointer_events() const;
Variant<CSS::VerticalAlign, CSS::LengthPercentage> vertical_align() const;
Optional<CSS::FontVariant> font_variant() const;
Vector<CSS::Transformation> transformations() const;
CSS::TransformOrigin transform_origin() const;

View file

@ -146,6 +146,11 @@ enum class Float {
Right,
};
enum class FontVariant {
Normal,
SmallCaps,
};
enum class ImageRendering {
Auto,
CrispEdges,

View file

@ -363,6 +363,9 @@ void NodeWithStyle::apply_style(const CSS::StyleProperties& specified_style)
computed_values.set_font_size(specified_style.property(CSS::PropertyID::FontSize).value()->to_length().to_px(*this));
computed_values.set_font_weight(specified_style.property(CSS::PropertyID::FontWeight).value()->to_integer());
if (auto maybe_font_variant = specified_style.font_variant(); maybe_font_variant.has_value())
computed_values.set_font_variant(maybe_font_variant.release_value());
// FIXME: BorderXRadius properties are now BorderRadiusStyleValues, so make use of that.
auto border_bottom_left_radius = specified_style.property(CSS::PropertyID::BorderBottomLeftRadius);
if (border_bottom_left_radius.has_value() && border_bottom_left_radius.value()->is_border_radius())