1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-26 04:27:44 +00:00

LibWeb: Port FontFace to new Strings

This commit is contained in:
Sam Atkins 2023-02-18 15:13:29 +00:00 committed by Linus Groh
parent 33e9c4e1b2
commit c2f0b20d6b
5 changed files with 18 additions and 16 deletions

View file

@ -1,5 +1,5 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -8,7 +8,7 @@
namespace Web::CSS {
FontFace::FontFace(DeprecatedFlyString font_family, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges)
FontFace::FontFace(FlyString font_family, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges)
: m_font_family(move(font_family))
, m_sources(move(sources))
, m_unicode_ranges(move(unicode_ranges))

View file

@ -1,12 +1,12 @@
/*
* Copyright (c) 2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/FlyString.h>
#include <AK/URL.h>
#include <LibWeb/CSS/UnicodeRange.h>
@ -17,19 +17,19 @@ public:
struct Source {
AK::URL url;
// FIXME: Do we need to keep this around, or is it only needed to discard unwanted formats during parsing?
Optional<DeprecatedFlyString> format;
Optional<FlyString> format;
};
FontFace(DeprecatedFlyString font_family, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges);
FontFace(FlyString font_family, Vector<Source> sources, Vector<UnicodeRange> unicode_ranges);
~FontFace() = default;
DeprecatedFlyString font_family() const { return m_font_family; }
FlyString font_family() const { return m_font_family; }
Vector<Source> const& sources() const { return m_sources; }
Vector<UnicodeRange> const& unicode_ranges() const { return m_unicode_ranges; }
// FIXME: font-style, font-weight, font-stretch, font-feature-settings
private:
DeprecatedFlyString m_font_family;
FlyString m_font_family;
Vector<Source> m_sources;
Vector<UnicodeRange> m_unicode_ranges;
};

View file

@ -5300,7 +5300,7 @@ CSSRule* Parser::parse_font_face_rule(TokenStream<ComponentValue>& tokens)
{
auto declarations_and_at_rules = parse_a_list_of_declarations(tokens);
Optional<DeprecatedFlyString> font_family;
Optional<FlyString> font_family;
Vector<FontFace::Source> src;
Vector<UnicodeRange> unicode_range;
@ -5352,7 +5352,7 @@ CSSRule* Parser::parse_font_face_rule(TokenStream<ComponentValue>& tokens)
if (had_syntax_error || font_family_parts.is_empty())
continue;
font_family = DeprecatedString::join(' ', font_family_parts);
font_family = String::join(' ', font_family_parts).release_value_but_fixme_should_propagate_errors();
continue;
}
if (declaration.name().equals_ignoring_case("src"sv)) {
@ -5423,7 +5423,7 @@ Vector<FontFace::Source> Parser::parse_font_face_src(TokenStream<ComponentValue>
// FIXME: Implement optional tech() function from CSS-Fonts-4.
if (auto maybe_url = parse_url_function(first, AllowedDataUrlType::Font); maybe_url.has_value()) {
auto url = maybe_url.release_value();
Optional<DeprecatedFlyString> format;
Optional<FlyString> format;
source_tokens.skip_whitespace();
if (!source_tokens.has_next_token()) {
@ -5457,7 +5457,7 @@ Vector<FontFace::Source> Parser::parse_font_face_src(TokenStream<ComponentValue>
continue;
}
format = format_name;
format = FlyString::from_utf8(format_name).release_value_but_fixme_should_propagate_errors();
} else {
dbgln_if(CSS_PARSER_DEBUG, "CSSParser: @font-face src invalid (unrecognized function token `{}`); discarding.", function.name());
return {};

View file

@ -1532,9 +1532,11 @@ void StyleComputer::load_fonts_from_sheet(CSSStyleSheet const& sheet)
if (!is<CSSFontFaceRule>(rule))
continue;
auto const& font_face = static_cast<CSSFontFaceRule const&>(rule).font_face();
// FIXME: Use font_face.font_family() directly when we port to new Strings here.
auto font_family = font_face.font_family().to_string().to_deprecated_string();
if (font_face.sources().is_empty())
continue;
if (m_loaded_fonts.contains(font_face.font_family()))
if (m_loaded_fonts.contains(font_family))
continue;
// NOTE: This is rather ad-hoc, we just look for the first valid
@ -1562,8 +1564,8 @@ void StyleComputer::load_fonts_from_sheet(CSSStyleSheet const& sheet)
LoadRequest request;
auto url = m_document.parse_url(candidate_url.value().to_deprecated_string());
auto loader = make<FontLoader>(const_cast<StyleComputer&>(*this), font_face.font_family(), move(url));
const_cast<StyleComputer&>(*this).m_loaded_fonts.set(font_face.font_family(), move(loader));
auto loader = make<FontLoader>(const_cast<StyleComputer&>(*this), font_family, move(url));
const_cast<StyleComputer&>(*this).m_loaded_fonts.set(font_family, move(loader));
}
}

View file

@ -599,7 +599,7 @@ void dump_font_face_rule(StringBuilder& builder, CSS::CSSFontFaceRule const& rul
builder.append("sources:\n"sv);
for (auto const& source : font_face.sources()) {
indent(builder, indent_levels + 2);
builder.appendff("url={}, format={}\n", source.url, source.format.value_or("???"));
builder.appendff("url={}, format={}\n", source.url, source.format.value_or(String::from_utf8_short_string("???"sv)));
}
indent(builder, indent_levels + 1);