LibWeb: Port FontCache to new Strings

This commit is contained in:
Sam Atkins 2023-02-17 14:06:55 +00:00 committed by Linus Groh
parent faab2fe101
commit 1c77867c78
Notes: sideshowbarker 2024-07-17 00:03:47 +09:00
4 changed files with 18 additions and 21 deletions

View file

@ -1,7 +1,7 @@
/*
* Copyright (c) 2018-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021, the SerenityOS developers.
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -43,7 +43,7 @@ StyleComputer::~StyleComputer() = default;
class StyleComputer::FontLoader : public ResourceClient {
public:
explicit FontLoader(StyleComputer& style_computer, DeprecatedFlyString family_name, AK::URL url)
explicit FontLoader(StyleComputer& style_computer, FlyString family_name, AK::URL url)
: m_style_computer(style_computer)
, m_family_name(move(family_name))
{
@ -105,7 +105,7 @@ private:
}
StyleComputer& m_style_computer;
DeprecatedFlyString m_family_name;
FlyString m_family_name;
RefPtr<Gfx::VectorFont> m_vector_font;
HashMap<float, NonnullRefPtr<Gfx::ScaledFont>> mutable m_cached_fonts;
@ -1187,7 +1187,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
FontSelector font_selector;
bool monospace = false;
auto find_font = [&](DeprecatedString const& family) -> RefPtr<Gfx::Font> {
auto find_font = [&](String const& family) -> RefPtr<Gfx::Font> {
float font_size_in_pt = font_size_in_px * 0.75f;
font_selector = { family, font_size_in_pt, weight, width, slope };
@ -1200,7 +1200,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
if (auto found_font = FontCache::the().get(font_selector))
return found_font;
if (auto found_font = Gfx::FontDatabase::the().get(family, font_size_in_pt, weight, width, slope, Gfx::Font::AllowInexactSizeMatch::Yes))
if (auto found_font = Gfx::FontDatabase::the().get(family.to_deprecated_string(), font_size_in_pt, weight, width, slope, Gfx::Font::AllowInexactSizeMatch::Yes))
return found_font;
return {};
@ -1238,7 +1238,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
default:
return {};
}
return find_font(Platform::FontPlugin::the().generic_font_name(generic_font));
return find_font(String::from_utf8(Platform::FontPlugin::the().generic_font_name(generic_font)).release_value_but_fixme_should_propagate_errors());
};
RefPtr<Gfx::Font> found_font;
@ -1250,7 +1250,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
if (family.is_identifier()) {
found_font = find_generic_font(family.to_identifier());
} else if (family.is_string()) {
found_font = find_font(family.to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string());
found_font = find_font(family.to_string().release_value_but_fixme_should_propagate_errors());
}
if (found_font)
break;
@ -1258,7 +1258,7 @@ void StyleComputer::compute_font(StyleProperties& style, DOM::Element const* ele
} else if (family_value->is_identifier()) {
found_font = find_generic_font(family_value->to_identifier());
} else if (family_value->is_string()) {
found_font = find_font(family_value->to_string().release_value_but_fixme_should_propagate_errors().to_deprecated_string());
found_font = find_font(family_value->to_string().release_value_but_fixme_should_propagate_errors());
}
if (!found_font) {
@ -1519,7 +1519,7 @@ CSSPixelRect StyleComputer::viewport_rect() const
return {};
}
void StyleComputer::did_load_font([[maybe_unused]] DeprecatedFlyString const& family_name)
void StyleComputer::did_load_font([[maybe_unused]] FlyString const& family_name)
{
document().invalidate_layout();
}
@ -1530,11 +1530,9 @@ 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_family))
if (m_loaded_fonts.contains(font_face.font_family()))
continue;
// NOTE: This is rather ad-hoc, we just look for the first valid
@ -1562,8 +1560,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_family, move(url));
const_cast<StyleComputer&>(*this).m_loaded_fonts.set(font_family, move(loader));
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().to_string(), move(loader));
}
}

View file

@ -1,6 +1,6 @@
/*
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2021-2023, Sam Atkins <atkinssj@serenityos.org>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
@ -73,7 +73,7 @@ public:
Gfx::Font const& initial_font() const;
void did_load_font(DeprecatedFlyString const& family_name);
void did_load_font(FlyString const& family_name);
void load_fonts_from_sheet(CSSStyleSheet const&);
@ -118,7 +118,7 @@ private:
OwnPtr<RuleCache> m_rule_cache;
class FontLoader;
HashMap<DeprecatedString, NonnullOwnPtr<FontLoader>> m_loaded_fonts;
HashMap<String, NonnullOwnPtr<FontLoader>> m_loaded_fonts;
};
}

View file

@ -6,14 +6,13 @@
#pragma once
#include <AK/DeprecatedFlyString.h>
#include <AK/DeprecatedString.h>
#include <AK/FlyString.h>
#include <AK/HashMap.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Forward.h>
struct FontSelector {
DeprecatedFlyString family;
FlyString family;
float point_size { 0 };
int weight { 0 };
int width { 0 };

View file

@ -515,7 +515,7 @@ static void paint_text_fragment(PaintContext& context, Layout::TextNode const& t
auto& font = fragment.layout_node().font();
auto scaled_font = [&]() -> RefPtr<Gfx::Font> {
auto device_font_pt_size = context.enclosing_device_pixels(font.presentation_size());
FontSelector font_selector = { font.family(), static_cast<float>(device_font_pt_size.value()), font.weight(), font.width(), font.slope() };
FontSelector font_selector = { FlyString::from_utf8(font.family()).release_value_but_fixme_should_propagate_errors(), static_cast<float>(device_font_pt_size.value()), font.weight(), font.width(), font.slope() };
if (auto cached_font = FontCache::the().get(font_selector)) {
return cached_font;
}