2022-03-28 20:30:26 +01:00
|
|
|
/*
|
2025-04-03 11:50:05 +01:00
|
|
|
* Copyright (c) 2022-2025, Sam Atkins <sam@ladybird.org>
|
2024-10-04 13:19:50 +02:00
|
|
|
* Copyright (c) 2023, Andreas Kling <andreas@ladybird.org>
|
2022-03-28 20:30:26 +01:00
|
|
|
*
|
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
|
*/
|
|
|
|
|
|
2025-04-03 11:50:05 +01:00
|
|
|
#include <LibWeb/CSS/CSSFontFaceDescriptors.h>
|
2025-05-02 12:07:22 +01:00
|
|
|
#include <LibWeb/CSS/CSSRule.h>
|
2025-09-02 16:16:03 +12:00
|
|
|
#include <LibWeb/CSS/CSSStyleSheet.h>
|
2026-02-19 18:30:55 +13:00
|
|
|
#include <LibWeb/CSS/Enums.h>
|
2024-05-07 09:18:37 -06:00
|
|
|
#include <LibWeb/CSS/ParsedFontFace.h>
|
2025-09-02 16:16:03 +12:00
|
|
|
#include <LibWeb/CSS/StyleComputer.h>
|
2025-04-03 11:50:05 +01:00
|
|
|
#include <LibWeb/CSS/StyleValues/CalculatedStyleValue.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleValues/CustomIdentStyleValue.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleValues/FontSourceStyleValue.h>
|
2025-09-02 22:41:48 +12:00
|
|
|
#include <LibWeb/CSS/StyleValues/FontStyleStyleValue.h>
|
2025-04-03 11:50:05 +01:00
|
|
|
#include <LibWeb/CSS/StyleValues/IntegerStyleValue.h>
|
2025-08-08 10:28:41 +01:00
|
|
|
#include <LibWeb/CSS/StyleValues/KeywordStyleValue.h>
|
2025-04-03 11:50:05 +01:00
|
|
|
#include <LibWeb/CSS/StyleValues/NumberStyleValue.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleValues/OpenTypeTaggedStyleValue.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleValues/PercentageStyleValue.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleValues/StringStyleValue.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleValues/StyleValueList.h>
|
|
|
|
|
#include <LibWeb/CSS/StyleValues/UnicodeRangeStyleValue.h>
|
2025-09-02 16:16:03 +12:00
|
|
|
#include <LibWeb/DOM/Document.h>
|
2022-03-28 20:30:26 +01:00
|
|
|
|
|
|
|
|
namespace Web::CSS {
|
|
|
|
|
|
2025-08-08 10:11:51 +01:00
|
|
|
Vector<ParsedFontFace::Source> ParsedFontFace::sources_from_style_value(StyleValue const& style_value)
|
2025-04-03 11:50:05 +01:00
|
|
|
{
|
2025-04-04 15:30:36 +01:00
|
|
|
Vector<Source> sources;
|
|
|
|
|
auto add_source = [&sources](FontSourceStyleValue const& font_source) {
|
|
|
|
|
font_source.source().visit(
|
|
|
|
|
[&](FontSourceStyleValue::Local const& local) {
|
2026-02-04 19:59:10 +13:00
|
|
|
sources.empend(string_from_style_value(local.name), OptionalNone {}, Vector<FontTech> {});
|
2025-04-04 15:30:36 +01:00
|
|
|
},
|
2025-05-02 12:07:22 +01:00
|
|
|
[&](URL const& url) {
|
2025-06-03 12:32:05 +01:00
|
|
|
sources.empend(url, font_source.format(), font_source.tech());
|
2025-04-04 15:30:36 +01:00
|
|
|
});
|
2025-04-03 11:50:05 +01:00
|
|
|
};
|
|
|
|
|
|
2025-04-04 15:30:36 +01:00
|
|
|
if (style_value.is_font_source()) {
|
|
|
|
|
add_source(style_value.as_font_source());
|
|
|
|
|
} else if (style_value.is_value_list()) {
|
|
|
|
|
for (auto const& source : style_value.as_value_list().values())
|
|
|
|
|
add_source(source->as_font_source());
|
|
|
|
|
}
|
|
|
|
|
return sources;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ParsedFontFace ParsedFontFace::from_descriptors(CSSFontFaceDescriptors const& descriptors)
|
|
|
|
|
{
|
2025-08-08 10:11:51 +01:00
|
|
|
auto extract_percentage_or_normal = [](StyleValue const& value) -> Optional<Percentage> {
|
2025-04-03 11:50:05 +01:00
|
|
|
if (value.is_percentage())
|
|
|
|
|
return value.as_percentage().percentage();
|
|
|
|
|
if (value.is_calculated()) {
|
|
|
|
|
// FIXME: These should probably be simplified already?
|
2025-09-24 14:25:03 +01:00
|
|
|
return value.as_calculated().resolve_percentage({});
|
2025-04-03 11:50:05 +01:00
|
|
|
}
|
|
|
|
|
if (value.to_keyword() == Keyword::Normal)
|
|
|
|
|
return {};
|
|
|
|
|
|
|
|
|
|
return {};
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
FlyString font_family;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontFamily)))
|
2026-02-04 19:59:10 +13:00
|
|
|
font_family = string_from_style_value(*value);
|
2025-04-03 11:50:05 +01:00
|
|
|
|
2025-10-01 01:36:05 +13:00
|
|
|
ComputationContext computation_context {
|
2025-10-15 20:55:58 +13:00
|
|
|
.length_resolution_context = Length::ResolutionContext::for_document(*descriptors.parent_rule()->parent_style_sheet()->owning_document())
|
2025-10-01 01:36:05 +13:00
|
|
|
};
|
|
|
|
|
|
2026-02-17 14:44:59 +00:00
|
|
|
Optional<FontWeightRange> weight;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontWeight))) {
|
2025-09-02 16:16:03 +12:00
|
|
|
// https://drafts.csswg.org/css-fonts-4/#font-prop-desc
|
|
|
|
|
// The auto values for these three descriptors have the following effects:
|
|
|
|
|
// - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
|
|
|
|
|
// - FIXME: For variation axis clamping, clamping does not occur
|
2025-10-06 17:09:22 +13:00
|
|
|
if (value->to_keyword() == Keyword::Auto) {
|
2026-02-17 14:44:59 +00:00
|
|
|
weight = { 400, 400 };
|
2025-10-06 17:09:22 +13:00
|
|
|
} else {
|
2026-02-17 14:44:59 +00:00
|
|
|
auto absolutized = value->absolutized(computation_context);
|
|
|
|
|
auto& weight_values = absolutized->as_value_list().values();
|
|
|
|
|
if (weight_values.size() == 1) {
|
|
|
|
|
auto one_weight = static_cast<int>(StyleComputer::compute_font_weight(weight_values[0], {})->as_number().number());
|
|
|
|
|
weight = { one_weight, one_weight };
|
|
|
|
|
} else if (weight_values.size() == 2) {
|
|
|
|
|
// https://w3c.github.io/csswg-drafts/css-fonts/#font-prop-desc
|
|
|
|
|
// User agents must swap the computed value of the startpoint and endpoint of the range in order to forbid decreasing ranges.
|
|
|
|
|
auto first_weight = static_cast<int>(StyleComputer::compute_font_weight(weight_values[0], {})->as_number().number());
|
|
|
|
|
auto second_weight = static_cast<int>(StyleComputer::compute_font_weight(weight_values[1], {})->as_number().number());
|
|
|
|
|
weight = {
|
|
|
|
|
min(first_weight, second_weight),
|
|
|
|
|
max(first_weight, second_weight)
|
|
|
|
|
};
|
|
|
|
|
} else {
|
|
|
|
|
VERIFY_NOT_REACHED();
|
|
|
|
|
}
|
2025-10-06 17:09:22 +13:00
|
|
|
}
|
2025-09-02 16:16:03 +12:00
|
|
|
}
|
2025-04-03 11:50:05 +01:00
|
|
|
|
|
|
|
|
Optional<int> slope;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontStyle))) {
|
2025-09-02 22:41:48 +12:00
|
|
|
// https://drafts.csswg.org/css-fonts-4/#font-prop-desc
|
|
|
|
|
// The auto values for these three descriptors have the following effects:
|
|
|
|
|
// - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
|
|
|
|
|
// - FIXME: For variation axis clamping, clamping does not occur
|
2025-10-06 17:09:22 +13:00
|
|
|
if (value->to_keyword() == Keyword::Auto) {
|
2025-09-02 22:41:48 +12:00
|
|
|
slope = 0;
|
2025-10-06 17:09:22 +13:00
|
|
|
} else {
|
2026-01-03 01:58:38 +13:00
|
|
|
slope = StyleComputer::compute_font_style(value->absolutized(computation_context))->as_font_style().to_font_slope();
|
2025-10-06 17:09:22 +13:00
|
|
|
}
|
2025-09-02 22:41:48 +12:00
|
|
|
}
|
2025-04-03 11:50:05 +01:00
|
|
|
|
|
|
|
|
Optional<int> width;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontWidth))) {
|
2025-09-02 21:23:31 +12:00
|
|
|
// https://drafts.csswg.org/css-fonts-4/#font-prop-desc
|
|
|
|
|
// The auto values for these three descriptors have the following effects:
|
|
|
|
|
// - For font selection purposes, the font is selected as if the appropriate normal value (normal, normal or normal) is chosen
|
|
|
|
|
// - FIXME: For variation axis clamping, clamping does not occur
|
2025-10-06 17:09:22 +13:00
|
|
|
if (value->to_keyword() == Keyword::Auto) {
|
2025-09-02 21:23:31 +12:00
|
|
|
width = 100;
|
2025-10-06 17:09:22 +13:00
|
|
|
} else {
|
2026-01-03 01:58:38 +13:00
|
|
|
width = StyleComputer::compute_font_width(value->absolutized(computation_context))->as_percentage().raw_value();
|
2025-10-06 17:09:22 +13:00
|
|
|
}
|
2025-09-02 21:23:31 +12:00
|
|
|
}
|
2025-04-03 11:50:05 +01:00
|
|
|
|
|
|
|
|
Vector<Source> sources;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::Src)))
|
2025-04-04 15:30:36 +01:00
|
|
|
sources = sources_from_style_value(*value);
|
2025-04-03 11:50:05 +01:00
|
|
|
|
|
|
|
|
Vector<Gfx::UnicodeRange> unicode_ranges;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::UnicodeRange))) {
|
2025-11-29 15:53:22 +13:00
|
|
|
for (auto const& range : value->as_value_list().values())
|
|
|
|
|
unicode_ranges.append(range->as_unicode_range().unicode_range());
|
2025-04-03 11:50:05 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<Percentage> ascent_override;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::AscentOverride)))
|
2025-04-03 11:50:05 +01:00
|
|
|
ascent_override = extract_percentage_or_normal(*value);
|
|
|
|
|
|
|
|
|
|
Optional<Percentage> descent_override;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::DescentOverride)))
|
2025-04-03 11:50:05 +01:00
|
|
|
descent_override = extract_percentage_or_normal(*value);
|
|
|
|
|
|
|
|
|
|
Optional<Percentage> line_gap_override;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::LineGapOverride)))
|
2025-04-03 11:50:05 +01:00
|
|
|
line_gap_override = extract_percentage_or_normal(*value);
|
|
|
|
|
|
|
|
|
|
FontDisplay font_display;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontDisplay)))
|
2025-04-03 11:50:05 +01:00
|
|
|
font_display = keyword_to_font_display(value->to_keyword()).value_or(FontDisplay::Auto);
|
|
|
|
|
|
|
|
|
|
Optional<FlyString> font_named_instance;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontNamedInstance))) {
|
2025-04-03 11:50:05 +01:00
|
|
|
if (value->is_string())
|
|
|
|
|
font_named_instance = value->as_string().string_value();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<FlyString> font_language_override;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontLanguageOverride))) {
|
2025-04-03 11:50:05 +01:00
|
|
|
if (value->is_string())
|
|
|
|
|
font_language_override = value->as_string().string_value();
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 21:33:07 +13:00
|
|
|
Optional<OrderedHashMap<FlyString, i32>> font_feature_settings;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontFeatureSettings))) {
|
2025-04-03 11:50:05 +01:00
|
|
|
if (value->to_keyword() == Keyword::Normal) {
|
|
|
|
|
font_feature_settings.clear();
|
|
|
|
|
} else if (value->is_value_list()) {
|
|
|
|
|
auto const& feature_tags = value->as_value_list().values();
|
2026-03-26 21:33:07 +13:00
|
|
|
OrderedHashMap<FlyString, i32> settings;
|
2025-04-03 11:50:05 +01:00
|
|
|
settings.ensure_capacity(feature_tags.size());
|
2026-02-04 19:34:33 +13:00
|
|
|
for (auto const& feature_tag_style_value : feature_tags) {
|
|
|
|
|
auto const& feature_tag = feature_tag_style_value->as_open_type_tagged();
|
|
|
|
|
|
|
|
|
|
// FIXME: We should absolutize feature_tag.value() in case there are relative lengths
|
|
|
|
|
settings.set(feature_tag.tag(), int_from_style_value(feature_tag.value()));
|
2025-04-03 11:50:05 +01:00
|
|
|
}
|
|
|
|
|
font_feature_settings = move(settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Optional<OrderedHashMap<FlyString, double>> font_variation_settings;
|
2026-03-05 00:03:57 +13:00
|
|
|
if (auto value = descriptors.descriptor_or_initial_value(DescriptorNameAndID::from_id(DescriptorID::FontVariationSettings))) {
|
2025-04-03 11:50:05 +01:00
|
|
|
if (value->to_keyword() == Keyword::Normal) {
|
|
|
|
|
font_variation_settings.clear();
|
|
|
|
|
} else if (value->is_value_list()) {
|
|
|
|
|
auto const& variation_tags = value->as_value_list().values();
|
|
|
|
|
OrderedHashMap<FlyString, double> settings;
|
|
|
|
|
settings.ensure_capacity(variation_tags.size());
|
2026-02-14 16:36:07 +13:00
|
|
|
|
|
|
|
|
// FIXME: Absolutize these values to handle relative lengths within calcs
|
|
|
|
|
for (auto const& variation_tag : variation_tags)
|
|
|
|
|
settings.set(variation_tag->as_open_type_tagged().tag(), number_from_style_value(variation_tag->as_open_type_tagged().value(), {}));
|
|
|
|
|
|
2025-04-03 11:50:05 +01:00
|
|
|
font_variation_settings = move(settings);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return ParsedFontFace {
|
2025-12-28 12:12:59 +01:00
|
|
|
*descriptors.parent_rule(),
|
2025-04-03 11:50:05 +01:00
|
|
|
move(font_family),
|
|
|
|
|
move(weight),
|
|
|
|
|
move(slope),
|
|
|
|
|
move(width),
|
|
|
|
|
move(sources),
|
|
|
|
|
move(unicode_ranges),
|
|
|
|
|
move(ascent_override),
|
|
|
|
|
move(descent_override),
|
|
|
|
|
move(line_gap_override),
|
|
|
|
|
move(font_display),
|
|
|
|
|
move(font_named_instance),
|
|
|
|
|
move(font_language_override),
|
|
|
|
|
move(font_feature_settings),
|
|
|
|
|
move(font_variation_settings)
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-26 21:33:07 +13:00
|
|
|
ParsedFontFace::ParsedFontFace(GC::Ref<CSSRule> parent_rule, FlyString font_family, Optional<FontWeightRange> weight, Optional<int> slope, Optional<int> width, Vector<Source> sources, Vector<Gfx::UnicodeRange> unicode_ranges, Optional<Percentage> ascent_override, Optional<Percentage> descent_override, Optional<Percentage> line_gap_override, FontDisplay font_display, Optional<FlyString> font_named_instance, Optional<FlyString> font_language_override, Optional<OrderedHashMap<FlyString, i32>> font_feature_settings, Optional<OrderedHashMap<FlyString, double>> font_variation_settings)
|
2025-12-28 12:12:59 +01:00
|
|
|
: m_parent_rule(parent_rule)
|
2025-05-02 12:07:22 +01:00
|
|
|
, m_font_family(move(font_family))
|
2024-09-26 14:45:55 +01:00
|
|
|
, m_font_named_instance(move(font_named_instance))
|
2026-02-17 14:44:59 +00:00
|
|
|
, m_weight(move(weight))
|
2023-05-24 15:35:04 +02:00
|
|
|
, m_slope(slope)
|
2024-09-27 14:27:55 +01:00
|
|
|
, m_width(width)
|
2022-03-28 20:30:26 +01:00
|
|
|
, m_sources(move(sources))
|
2022-03-31 16:39:52 +01:00
|
|
|
, m_unicode_ranges(move(unicode_ranges))
|
2024-09-26 12:51:41 +01:00
|
|
|
, m_ascent_override(move(ascent_override))
|
|
|
|
|
, m_descent_override(move(descent_override))
|
|
|
|
|
, m_line_gap_override(move(line_gap_override))
|
2024-09-26 14:07:46 +01:00
|
|
|
, m_font_display(font_display)
|
2024-09-27 17:11:31 +01:00
|
|
|
, m_font_language_override(font_language_override)
|
2024-10-01 11:02:05 +01:00
|
|
|
, m_font_feature_settings(move(font_feature_settings))
|
|
|
|
|
, m_font_variation_settings(move(font_variation_settings))
|
2022-03-28 20:30:26 +01:00
|
|
|
{
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|