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

LibWeb: Move resolution of font weights and slopes to StyleValue

This isn't exactly ideal factoring (though I'm not sure what is) but
this will make it possible to reuse this code in the parser.
This commit is contained in:
Andreas Kling 2023-05-24 15:28:09 +02:00
parent 17d6ab2416
commit be10360350
3 changed files with 63 additions and 50 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2018-2023, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
* Copyright (c) 2022-2023, MacDue <macdue@dueutil.tech>
@ -7,6 +7,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <LibGfx/Font/FontStyleMapping.h>
#include <LibWeb/CSS/StyleValue.h>
#include <LibWeb/CSS/StyleValues/AbstractImageStyleValue.h>
#include <LibWeb/CSS/StyleValues/AngleStyleValue.h>
@ -369,4 +370,59 @@ ValueID StyleValue::to_identifier() const
return ValueID::Invalid;
}
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;
}
}
if (has_integer()) {
int font_weight_integer = to_integer();
if (font_weight_integer <= Gfx::FontWeight::Regular)
return Gfx::FontWeight::Regular;
if (font_weight_integer <= Gfx::FontWeight::Bold)
return Gfx::FontWeight::Bold;
return Gfx::FontWeight::Black;
}
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;
}
}