Skip to content

Truly independent web browser

LibGfx: Give each rasterizer data slot a fixed type

Callum Law4a5b494c63ea
diff --git a/Libraries/LibGfx/Font/Font.h b/Libraries/LibGfx/Font/Font.h
index 950169dafb0..be8b0a75510 100644
--- a/Libraries/LibGfx/Font/Font.h
+++ b/Libraries/LibGfx/Font/Font.h
@@ -26,6 +26,8 @@ struct hb_buffer_t;
namespace Gfx {
+struct FontRasterizerData;
+
struct FontPixelMetrics {
float x_height { 0 };
float cap_height { 0 };
@@ -97,8 +99,8 @@ public:
void will_be_destroyed() const;
// What a rasterizer keeps with this font. The first call makes it.
- template<typename T, typename Callback>
- T& rasterizer_data(Callback make) const { return m_rasterizer_data.get<T>(make); }
+ template<typename Callback>
+ FontRasterizerData& rasterizer_data(Callback make) const { return m_rasterizer_data.get(make); }
private:
friend class Typeface;
@@ -111,7 +113,7 @@ private:
hb_font_t* m_harfbuzz_font { nullptr };
- RasterizerDataSlot m_rasterizer_data;
+ RasterizerDataSlot<FontRasterizerData> m_rasterizer_data;
// A layout pass classifies fonts while the document thread may be doing the same to the same
// font, so the verdict is a single atomic byte. Either winner is correct: the classification
diff --git a/Libraries/LibGfx/Font/Typeface.h b/Libraries/LibGfx/Font/Typeface.h
index 6e6db839603..979b9620c84 100644
--- a/Libraries/LibGfx/Font/Typeface.h
+++ b/Libraries/LibGfx/Font/Typeface.h
@@ -34,6 +34,7 @@ struct hb_font_t;
namespace Gfx {
class Font;
+struct TypefaceRasterizerData;
struct SystemFontIdentifier {
u64 generation { 0 };
@@ -174,8 +175,8 @@ public:
Optional<String> postscript_name() const;
// What a rasterizer keeps with this typeface. The first call makes it.
- template<typename T, typename Callback>
- T& rasterizer_data(Callback make) const { return m_rasterizer_data.get<T>(make); }
+ template<typename Callback>
+ TypefaceRasterizerData& rasterizer_data(Callback make) const { return m_rasterizer_data.get(make); }
// Union of all glyph bounding boxes as recorded in the `head` table, in font units with y pointing up.
// is_empty() when the face has no usable `head` table (e.g. bitmap-only fonts).
@@ -263,7 +264,7 @@ private:
mutable Optional<FaceDescription> m_description;
mutable OnceFlag m_bounding_box_once;
mutable BoundingBoxInFontUnits m_bounding_box_in_font_units;
- RasterizerDataSlot m_rasterizer_data;
+ RasterizerDataSlot<TypefaceRasterizerData> m_rasterizer_data;
};
}
diff --git a/Libraries/LibGfx/RasterizerData.h b/Libraries/LibGfx/RasterizerData.h
index b862588feef..c4b0ee96bb2 100644
--- a/Libraries/LibGfx/RasterizerData.h
+++ b/Libraries/LibGfx/RasterizerData.h
@@ -6,10 +6,11 @@
#pragma once
+#include <AK/Concepts.h>
#include <AK/Noncopyable.h>
+#include <AK/NonnullOwnPtr.h>
#include <AK/Once.h>
#include <AK/OwnPtr.h>
-#include <AK/TypeCasts.h>
#include <AK/kmalloc.h>
namespace Gfx {
@@ -22,8 +23,8 @@ public:
virtual ~RasterizerData() = default;
};
-// Holds the rasterizer data of one typeface or font. The first call makes the data, and every call must ask for the
-// type that it made.
+// The rasterizer defines T. LibGfx can destroy it through RasterizerData while T is incomplete.
+template<typename T>
class RasterizerDataSlot {
AK_MAKE_NONCOPYABLE(RasterizerDataSlot);
AK_MAKE_NONMOVABLE(RasterizerDataSlot);
@@ -31,11 +32,12 @@ class RasterizerDataSlot {
public:
RasterizerDataSlot() = default;
- template<typename T, typename Callback>
+ template<typename Callback>
T& get(Callback make) const
+ requires(SameAs<decltype(make()), NonnullOwnPtr<T>>)
{
call_once(m_once, [&] { m_data = make(); });
- return as<T>(*m_data);
+ return static_cast<T&>(*m_data);
}
private:
diff --git a/Services/Compositor/FontSkia.cpp b/Services/Compositor/FontSkia.cpp
index b44029939ed..1ce2160a8c7 100644
--- a/Services/Compositor/FontSkia.cpp
+++ b/Services/Compositor/FontSkia.cpp
@@ -37,7 +37,7 @@
# include <src/ports/SkTypeface_win_dw.h>
#endif
-namespace Compositor {
+using Compositor::FontRasterizer;
static FontRasterizer s_font_rasterizer { FontRasterizer::Platform };
static bool s_use_test_font_rendering { false };
@@ -72,11 +72,9 @@ static u64 encode_rendering_options(float scale, Gfx::FontRenderingOptions optio
}
#endif
-namespace {
-
// What the compositor keeps with each typeface.
-struct TypefaceData final : public Gfx::RasterizerData {
- explicit TypefaceData(sk_sp<SkTypeface> typeface)
+struct Gfx::TypefaceRasterizerData final : public Gfx::RasterizerData {
+ explicit TypefaceRasterizerData(sk_sp<SkTypeface> typeface)
: typeface(move(typeface))
{
}
@@ -84,6 +82,8 @@ struct TypefaceData final : public Gfx::RasterizerData {
sk_sp<SkTypeface> typeface;
};
+namespace {
+
#if defined(AK_OS_MACOS)
// CoreText places the bitmap glyphs of an installed font, such as Apple Color Emoji, lower than it places the same
// glyphs of font data, whose bitmaps would then sit on the baseline. So an installed font is drawn by its name. Only
@@ -157,7 +157,7 @@ sk_sp<SkTypeface> make_skia_typeface(Gfx::Typeface const& typeface)
// The Skia typeface of a typeface. Null if Skia cannot load it.
sk_sp<SkTypeface> const& skia_typeface(Gfx::Typeface const& typeface)
{
- return typeface.rasterizer_data<TypefaceData>([&] { return make<TypefaceData>(make_skia_typeface(typeface)); }).typeface;
+ return typeface.rasterizer_data([&] { return make<Gfx::TypefaceRasterizerData>(make_skia_typeface(typeface)); }).typeface;
}
// The typeface of the font's variations. Without variations, or if Skia cannot apply them, it is the typeface itself.
@@ -182,9 +182,11 @@ sk_sp<SkTypeface> typeface_with_variations(Gfx::Font const& font)
return typeface;
}
+}
+
// What the compositor keeps with each font.
-struct FontData final : public Gfx::RasterizerData {
- explicit FontData(Gfx::Font const& font)
+struct Gfx::FontRasterizerData final : public Gfx::RasterizerData {
+ explicit FontRasterizerData(Font const& font)
: typeface(typeface_with_variations(font))
{
}
@@ -197,13 +199,15 @@ struct FontData final : public Gfx::RasterizerData {
#endif
};
-FontData& font_data(Gfx::Font const& font)
+namespace {
+
+Gfx::FontRasterizerData& font_data(Gfx::Font const& font)
{
- return font.rasterizer_data<FontData>([&] { return make<FontData>(font); });
+ return font.rasterizer_data([&] { return make<Gfx::FontRasterizerData>(font); });
}
#if defined(USE_FONTCONFIG)
-Gfx::FontRenderingOptions rendering_options(Gfx::Font const& font, float scale, FontData& data)
+Gfx::FontRenderingOptions rendering_options(Gfx::Font const& font, float scale, Gfx::FontRasterizerData& data)
{
auto& memo = data.rendering_options_memo;
auto value = memo.load(AK::MemoryOrder::memory_order_relaxed);
@@ -223,6 +227,8 @@ Gfx::FontRenderingOptions rendering_options(Gfx::Font const& font, float scale,
}
+namespace Compositor {
+
void set_font_rasterizer(FontRasterizer rasterizer)
{
s_font_rasterizer = rasterizer;