Skip to content

Truly independent web browser

LibGfx+LibWeb+Compositor: Make system UI typefaces without Skia

Aliaksandr Kalenikf740ba99695f
diff --git a/Libraries/LibGfx/CMakeLists.txt b/Libraries/LibGfx/CMakeLists.txt
index 70e775b32ca..9aa923aba8d 100644
--- a/Libraries/LibGfx/CMakeLists.txt
+++ b/Libraries/LibGfx/CMakeLists.txt
@@ -61,7 +61,7 @@ set(SOURCES
)
if (APPLE)
- list(APPEND SOURCES MetalContext.mm VideoSurfaceImage.mm)
+ list(APPEND SOURCES Font/TypefaceCoreText.cpp MetalContext.mm VideoSurfaceImage.mm)
endif()
if (HAS_DIRECTX)
@@ -110,7 +110,7 @@ if (NOT ANDROID)
endif()
if (APPLE)
- target_link_libraries(LibGfx PRIVATE "-framework Metal" "-framework Accelerate")
+ target_link_libraries(LibGfx PRIVATE "-framework Metal" "-framework Accelerate" "-framework CoreFoundation" "-framework CoreGraphics" "-framework CoreText")
endif()
if (HAS_DIRECTX)
diff --git a/Libraries/LibGfx/Font/Font.h b/Libraries/LibGfx/Font/Font.h
index e1c4e06734c..31970f7a710 100644
--- a/Libraries/LibGfx/Font/Font.h
+++ b/Libraries/LibGfx/Font/Font.h
@@ -14,7 +14,6 @@
#include <AK/FlyString.h>
#include <AK/Once.h>
#include <AK/Optional.h>
-#include <AK/OwnPtr.h>
#include <AK/RefPtr.h>
#include <AK/Utf16String.h>
#include <LibGfx/Font/RasterizerData.h>
@@ -86,11 +85,7 @@ public:
// What a rasterizer keeps with this font. The first call makes it.
template<typename T, typename Callback>
- T& rasterizer_data(Callback make) const
- {
- call_once(m_rasterizer_data_once, [&] { m_rasterizer_data = make(); });
- return as<T>(*m_rasterizer_data);
- }
+ T& rasterizer_data(Callback make) const { return m_rasterizer_data.get<T>(make); }
private:
friend class Typeface;
@@ -103,8 +98,7 @@ private:
hb_font_t* m_harfbuzz_font { nullptr };
- mutable OnceFlag m_rasterizer_data_once;
- mutable OwnPtr<RasterizerData> m_rasterizer_data;
+ RasterizerDataSlot 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/RasterizerData.h b/Libraries/LibGfx/Font/RasterizerData.h
index d554b08aea6..b862588feef 100644
--- a/Libraries/LibGfx/Font/RasterizerData.h
+++ b/Libraries/LibGfx/Font/RasterizerData.h
@@ -6,6 +6,9 @@
#pragma once
+#include <AK/Noncopyable.h>
+#include <AK/Once.h>
+#include <AK/OwnPtr.h>
#include <AK/TypeCasts.h>
#include <AK/kmalloc.h>
@@ -19,4 +22,25 @@ 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.
+class RasterizerDataSlot {
+ AK_MAKE_NONCOPYABLE(RasterizerDataSlot);
+ AK_MAKE_NONMOVABLE(RasterizerDataSlot);
+
+public:
+ RasterizerDataSlot() = default;
+
+ template<typename T, typename Callback>
+ T& get(Callback make) const
+ {
+ call_once(m_once, [&] { m_data = make(); });
+ return as<T>(*m_data);
+ }
+
+private:
+ mutable OnceFlag m_once;
+ mutable OwnPtr<RasterizerData> m_data;
+};
+
}
diff --git a/Libraries/LibGfx/Font/Typeface.cpp b/Libraries/LibGfx/Font/Typeface.cpp
index 91a051038ee..967eebcf8d6 100644
--- a/Libraries/LibGfx/Font/Typeface.cpp
+++ b/Libraries/LibGfx/Font/Typeface.cpp
@@ -17,6 +17,9 @@
#include <LibGfx/Font/FontVariationSettings.h>
#include <LibGfx/Font/Typeface.h>
#include <LibGfx/Font/TypefaceSkia.h>
+#ifdef AK_OS_MACOS
+# include <LibGfx/Font/TypefaceCoreText.h>
+#endif
#include <LibIPC/Decoder.h>
#include <LibIPC/Encoder.h>
@@ -34,6 +37,26 @@ ErrorOr<Vector<String>> Typeface::local_font_names() const
return names;
}
+FlyString const& Typeface::family() const
+{
+ return description().family();
+}
+
+u16 Typeface::weight() const
+{
+ return fixed_style().value_or(description().style()).weight;
+}
+
+u16 Typeface::width() const
+{
+ return fixed_style().value_or(description().style()).width;
+}
+
+u8 Typeface::slope() const
+{
+ return fixed_style().value_or(description().style()).slope;
+}
+
ErrorOr<NonnullRefPtr<Typeface>> Typeface::try_load_from_mapped_file(NonnullOwnPtr<Core::MappedFile> mapped_file, u32 ttc_index)
{
auto bytes = mapped_file->bytes();
@@ -267,6 +290,8 @@ FaceDescription const& Typeface::description() const
FaceStyle Typeface::style_for_variations(ReadonlySpan<FontVariationAxis> variations) const
{
+ if (auto style = fixed_style(); style.has_value())
+ return *style;
return description().style_for_variations(variations);
}
@@ -384,10 +409,13 @@ ErrorOr<NonnullRefPtr<Gfx::Typeface const>> decode(Decoder& decoder)
}
case Gfx::Typeface::FontDataFormat::SystemUIFont: {
auto style = TRY(decoder.decode<Gfx::SystemUIFontStyle>());
- auto typeface = TRY(Gfx::TypefaceSkia::match_system_ui(style.kind, 0, style.weight, style.width, style.slope));
- if (!typeface)
- return Error::from_string_literal("Typeface IPC data referred to an unavailable system UI font");
- return typeface.release_nonnull();
+#ifdef AK_OS_MACOS
+ if (auto typeface = Gfx::TypefaceCoreText::system_ui(style))
+ return typeface.release_nonnull();
+#else
+ (void)style;
+#endif
+ return Error::from_string_literal("Typeface IPC data referred to an unavailable system UI font");
}
case Gfx::Typeface::FontDataFormat::SystemFontId: {
auto generation = TRY(decoder.decode<u64>());
@@ -402,4 +430,24 @@ ErrorOr<NonnullRefPtr<Gfx::Typeface const>> decode(Decoder& decoder)
return Error::from_string_literal("Typeface IPC data contained invalid font data format");
}
+template<>
+ErrorOr<void> encode(Encoder& encoder, Gfx::SystemUIFontStyle const& style)
+{
+ TRY(encoder.encode(style.kind));
+ TRY(encoder.encode(style.weight));
+ TRY(encoder.encode(style.width));
+ TRY(encoder.encode(style.slope));
+ return {};
+}
+
+template<>
+ErrorOr<Gfx::SystemUIFontStyle> decode(Decoder& decoder)
+{
+ auto kind = TRY(decoder.decode<Gfx::SystemUIFontKind>());
+ auto weight = TRY(decoder.decode<u16>());
+ auto width = TRY(decoder.decode<u16>());
+ auto slope = TRY(decoder.decode<u8>());
+ return Gfx::SystemUIFontStyle { kind, weight, width, slope };
+}
+
}
diff --git a/Libraries/LibGfx/Font/Typeface.h b/Libraries/LibGfx/Font/Typeface.h
index 67d170d8c64..487736e1681 100644
--- a/Libraries/LibGfx/Font/Typeface.h
+++ b/Libraries/LibGfx/Font/Typeface.h
@@ -19,6 +19,7 @@
#include <LibCore/MappedFile.h>
#include <LibGfx/Font/FaceDescription.h>
#include <LibGfx/Font/FontVariationSettings.h>
+#include <LibGfx/Font/RasterizerData.h>
#include <LibGfx/Forward.h>
#include <LibGfx/ShapeFeature.h>
#include <LibIPC/Forward.h>
@@ -42,6 +43,21 @@ struct SystemFontIdentifier {
bool operator==(SystemFontIdentifier const&) const = default;
};
+// The designs of the system UI font that CSS can name.
+enum class SystemUIFontKind : u8 {
+ System,
+ Serif,
+ Monospace,
+ Rounded,
+};
+
+struct SystemUIFontStyle {
+ SystemUIFontKind kind;
+ u16 weight;
+ u16 width;
+ u8 slope;
+};
+
struct FontCacheKey {
float point_size;
Vector<FontVariationAxis> axes;
@@ -91,10 +107,10 @@ public:
u32 glyph_count() const;
u16 units_per_em() const;
u32 glyph_id_for_code_point(u32 code_point) const;
- virtual FlyString const& family() const = 0;
- virtual u16 weight() const = 0;
- virtual u16 width() const = 0;
- virtual u8 slope() const = 0;
+ FlyString const& family() const;
+ u16 weight() const;
+ u16 width() const;
+ u8 slope() const;
ReadonlyBytes font_data() const LIFETIME_BOUND { return buffer(); }
u32 collection_index() const { return ttc_index(); }
@@ -112,9 +128,13 @@ public:
FaceDescription const& description() const;
// The style of a font of this typeface with these variations.
- virtual FaceStyle style_for_variations(ReadonlySpan<FontVariationAxis>) const;
+ FaceStyle style_for_variations(ReadonlySpan<FontVariationAxis>) const;
ErrorOr<Vector<String>> local_font_names() 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); }
+
// 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).
struct BoundingBoxInFontUnits {
@@ -132,6 +152,7 @@ public:
bool fast_is() const = delete;
virtual bool is_skia() const { return false; }
+ virtual bool is_core_text() const { return false; }
// How many glyph pages the calling thread has filled in, for tests of its glyph page caches.
static u64 glyph_pages_populated_on_this_thread();
@@ -151,6 +172,8 @@ protected:
virtual u32 ttc_index() const = 0;
virtual void encode_font_data_for_ipc(IPC::Encoder&) const;
virtual hb_face_t* create_harfbuzz_face() const;
+ // A typeface that the platform picked for a style answers with that style, whatever its tables say.
+ virtual Optional<FaceStyle> fixed_style() const { return {}; }
void set_font_data(NonnullRefPtr<FontDataBacking> backing) { m_font_data = move(backing); }
bool has_font_data_backing() const { return !m_font_data.is_null(); }
@@ -199,6 +222,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;
};
}
@@ -219,4 +243,10 @@ ErrorOr<void> encode(Encoder&, Gfx::Typeface const&);
template<>
ErrorOr<NonnullRefPtr<Gfx::Typeface const>> decode(Decoder&);
+template<>
+ErrorOr<void> encode(Encoder&, Gfx::SystemUIFontStyle const&);
+
+template<>
+ErrorOr<Gfx::SystemUIFontStyle> decode(Decoder&);
+
}
diff --git a/Libraries/LibGfx/Font/TypefaceCoreText.cpp b/Libraries/LibGfx/Font/TypefaceCoreText.cpp
new file mode 100644
index 00000000000..7c2aa29493d
--- /dev/null
+++ b/Libraries/LibGfx/Font/TypefaceCoreText.cpp
@@ -0,0 +1,147 @@
+/*
+ * Copyright (c) 2024, Aliaksandr Kalenik <[email protected]>
+ * Copyright (c) 2026, Tim Ledbetter <[email protected]>
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#include <AK/HashMap.h>
+#include <AK/Mutex.h>
+#include <AK/NeverDestroyed.h>
+#include <AK/ScopeGuard.h>
+#include <LibGfx/Font/TypefaceCoreText.h>
+#include <LibIPC/Encoder.h>
+
+#include <harfbuzz/hb-coretext.h>
+
+namespace Gfx {
+
+// NB: These are the CoreText string values behind the public AppKit NSFontDescriptorSystemDesign constants.
+// Keeping them here avoids pulling Objective-C headers into this C++ file.
+static CFStringRef core_text_ui_font_design(SystemUIFontKind kind)
+{
+ switch (kind) {
+ case SystemUIFontKind::System:
+ return CFSTR("NSCTFontUIFontDesignDefault");
+ case SystemUIFontKind::Serif:
+ return CFSTR("NSCTFontUIFontDesignSerif");
+ case SystemUIFontKind::Monospace:
+ return CFSTR("NSCTFontUIFontDesignMonospaced");
+ case SystemUIFontKind::Rounded:
+ return CFSTR("NSCTFontUIFontDesignRounded");
+ }
+ VERIFY_NOT_REACHED();
+}
+
+static CTFontDescriptorRef create_system_ui_font_descriptor(SystemUIFontKind kind, u8 slope)
+{
+ CGFloat core_text_slant = slope == 0 ? 0.0f : 1.0f;
+ auto slant_number = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &core_text_slant);
+ if (!slant_number)
+ return nullptr;
+
+ CFTypeRef trait_keys[] = { kCTFontSlantTrait, CFSTR("NSCTFontUIFontDesignTrait") };
+ CFTypeRef trait_values[] = { slant_number, core_text_ui_font_design(kind) };
+ auto traits = CFDictionaryCreate(kCFAllocatorDefault, trait_keys, trait_values, array_size(trait_keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+ CFRelease(slant_number);
+ if (!traits)
+ return nullptr;
+
+ CFTypeRef attribute_keys[] = { kCTFontTraitsAttribute };
+ CFTypeRef attribute_values[] = { traits };
+ auto attributes = CFDictionaryCreate(kCFAllocatorDefault, attribute_keys, attribute_values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
+ CFRelease(traits);
+ if (!attributes)
+ return nullptr;
+
+ auto descriptor = CTFontDescriptorCreateWithAttributes(attributes);
+ CFRelease(attributes);
+ return descriptor;
+}
+
+// The font is made at size 0, which CoreText takes as its default size. Every process makes the same font, and a font
+// of the typeface applies its own size and variations.
+static CTFontRef create_system_ui_font(SystemUIFontKind kind, u8 slope)
+{
+ auto base_font = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, 0, nullptr);
+ if (!base_font)
+ return nullptr;
+
+ auto descriptor = create_system_ui_font_descriptor(kind, slope);
+ if (!descriptor) {
+ CFRelease(base_font);
+ return nullptr;
+ }
+
+ auto font = CTFontCreateCopyWithAttributes(base_font, 0, nullptr, descriptor);
+ if (!font)
+ font = CTFontCreateWithFontDescriptor(descriptor, 0, nullptr);
+ CFRelease(base_font);
+ CFRelease(descriptor);
+ return font;
+}
+
+RefPtr<TypefaceCoreText> TypefaceCoreText::system_ui(SystemUIFontStyle style)
+{
+ static NeverDestroyed<Mutex> mutex;
+ static NeverDestroyed<HashMap<u64, NonnullRefPtr<TypefaceCoreText>>> typefaces;
+
+ u64 key = (static_cast<u64>(style.kind) << 40) | (static_cast<u64>(style.weight) << 24) | (static_cast<u64>(style.width) << 8) | style.slope;
+ MutexLocker locker(*mutex);
+ if (auto typeface = typefaces->get(key); typeface.has_value())
+ return *typeface;
+
+ auto core_text_font = create_system_ui_font(style.kind, style.slope);
+ if (!core_text_font)
+ return nullptr;
+ ScopeGuard release_core_text_font = [&] { CFRelease(core_text_font); };
+ auto graphics_font = CTFontCopyGraphicsFont(core_text_font, nullptr);
+ if (!graphics_font)
+ return nullptr;
+ ScopeGuard release_graphics_font = [&] { CFRelease(graphics_font); };
+
+ auto typeface = adopt_ref(*new TypefaceCoreText(core_text_font, graphics_font, style));
+ typefaces->set(key, typeface);
+ return typeface;
+}
+
+TypefaceCoreText::TypefaceCoreText(CTFontRef core_text_font, CGFontRef graphics_font, SystemUIFontStyle style)
+ : m_core_text_font(core_text_font)
+ , m_graphics_font(graphics_font)
+ , m_style(style)
+{
+ CFRetain(m_core_text_font);
+ CFRetain(m_graphics_font);
+}
+
+TypefaceCoreText::~TypefaceCoreText()
+{
+ CFRelease(m_graphics_font);
+ CFRelease(m_core_text_font);
+}
+
+hb_face_t* TypefaceCoreText::create_harfbuzz_face() const
+{
+ return hb_coretext_face_create(m_graphics_font);
+}
+
+void TypefaceCoreText::encode_font_data_for_ipc(IPC::Encoder& encoder) const
+{
+ if (system_font_identifier().has_value()) {
+ Typeface::encode_font_data_for_ipc(encoder);
+ return;
+ }
+
+ MUST(encoder.encode(FontDataFormat::SystemUIFont));
+ MUST(encoder.encode(m_style));
+}
+
+Optional<FaceStyle> TypefaceCoreText::fixed_style() const
+{
+ // One variable face of a system UI font serves every style, so the typeface answers with the style that it was
+ // asked for.
+ return FaceStyle { .weight = m_style.weight, .width = m_style.width, .slope = m_style.slope };
+}
+
+}
diff --git a/Libraries/LibGfx/Font/TypefaceCoreText.h b/Libraries/LibGfx/Font/TypefaceCoreText.h
new file mode 100644
index 00000000000..268d8c3df46
--- /dev/null
+++ b/Libraries/LibGfx/Font/TypefaceCoreText.h
@@ -0,0 +1,47 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <LibGfx/Font/Typeface.h>
+
+#include <CoreText/CoreText.h>
+
+namespace Gfx {
+
+// A system UI font, which CoreText gives without its font data. HarfBuzz reads its tables through CoreText.
+class TypefaceCoreText final : public Typeface {
+ AK_MAKE_NONCOPYABLE(TypefaceCoreText);
+ AK_MAKE_NONMOVABLE(TypefaceCoreText);
+
+public:
+ // The system UI font of a design, which answers with the style that it was asked for. A style always gives the
+ // same typeface.
+ static RefPtr<TypefaceCoreText> system_ui(SystemUIFontStyle);
+
+ virtual ~TypefaceCoreText() override;
+
+ CTFontRef core_text_font() const { return m_core_text_font; }
+
+private:
+ TypefaceCoreText(CTFontRef, CGFontRef, SystemUIFontStyle);
+
+ virtual ReadonlyBytes buffer() const override { return {}; }
+ virtual u32 ttc_index() const override { return 0; }
+ virtual hb_face_t* create_harfbuzz_face() const override;
+ virtual void encode_font_data_for_ipc(IPC::Encoder&) const override;
+ virtual Optional<FaceStyle> fixed_style() const override;
+ virtual bool is_core_text() const override { return true; }
+
+ CTFontRef m_core_text_font { nullptr };
+ CGFontRef m_graphics_font { nullptr };
+ SystemUIFontStyle m_style;
+};
+
+template<>
+inline bool Typeface::fast_is<TypefaceCoreText>() const { return is_core_text(); }
+
+}
diff --git a/Libraries/LibGfx/Font/TypefaceSkia.cpp b/Libraries/LibGfx/Font/TypefaceSkia.cpp
index cc9f58f8b99..c475fb8ed52 100644
--- a/Libraries/LibGfx/Font/TypefaceSkia.cpp
+++ b/Libraries/LibGfx/Font/TypefaceSkia.cpp
@@ -39,7 +39,6 @@
#ifdef AK_OS_MACOS
# include <CoreText/CoreText.h>
-# include <harfbuzz/hb-coretext.h>
# include <ports/SkFontMgr_mac_ct.h>
# include <ports/SkTypeface_mac.h>
# include <src/ports/SkTypeface_mac_ct.h>
@@ -56,41 +55,14 @@ static auto& skia_font_manager()
struct TypefaceSkia::Impl {
AK_ALLOC_WITH_KMALLOC;
- Impl(sk_sp<SkTypeface> skia_typeface, std::unique_ptr<SkStreamAsset> stream = {}, Optional<SystemUIFontStyle> system_ui_font_style = {}
-#ifdef AK_OS_MACOS
- ,
- CGFontRef cg_font = nullptr
-#endif
- )
+ Impl(sk_sp<SkTypeface> skia_typeface, std::unique_ptr<SkStreamAsset> stream = {})
: skia_typeface(move(skia_typeface))
, stream(move(stream))
- , system_ui_font_style(move(system_ui_font_style))
- {
-#ifdef AK_OS_MACOS
- if (cg_font) {
- this->cg_font = cg_font;
- CFRetain(this->cg_font);
- }
-#endif
- }
-
- ~Impl()
{
-#ifdef AK_OS_MACOS
- if (cg_font)
- CFRelease(cg_font);
-#endif
}
sk_sp<SkTypeface> skia_typeface;
std::unique_ptr<SkStreamAsset> stream;
-
- // Skia reads a CoreText UI font as upright and regular because its descriptor carries no numeric style traits,
- // so a system UI typeface answers style queries from the style it was matched for instead.
- Optional<SystemUIFontStyle> system_ui_font_style;
-#ifdef AK_OS_MACOS
- CGFontRef cg_font { nullptr };
-#endif
};
static SkFontMgr& font_manager()
@@ -143,28 +115,7 @@ static SkFontStyle::Slant slope_to_skia_slant(u8 slope)
}
}
-#ifdef AK_OS_MACOS
-static CTFontRef create_system_ui_font(SystemUIFontKind, float point_size, u8 slope);
-
-ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::typeface_from_core_text_typeface(sk_sp<SkTypeface> skia_typeface, CTFontRef ct_font, SystemUIFontStyle system_ui_font_style)
-{
- if (!skia_typeface)
- return RefPtr<TypefaceSkia> {};
-
- auto cg_font = CTFontCopyGraphicsFont(ct_font, nullptr);
- if (!cg_font)
- return Error::from_string_literal("Failed to get graphics font from CoreText font");
-
- auto typeface = adopt_ref(*new TypefaceSkia {
- make<TypefaceSkia::Impl>(move(skia_typeface), std::unique_ptr<SkStreamAsset> {}, system_ui_font_style, cg_font),
- {},
- 0 });
- CFRelease(cg_font);
- return typeface;
-}
-#endif
-
-ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::typeface_from_skia_typeface(sk_sp<SkTypeface> skia_typeface, Optional<SystemUIFontStyle> system_ui_font_style)
+ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::typeface_from_skia_typeface(sk_sp<SkTypeface> skia_typeface)
{
if (!skia_typeface)
return RefPtr<TypefaceSkia> {};
@@ -177,7 +128,7 @@ ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::typeface_from_skia_typeface(sk_sp<Sk
// NB: Safe to reference without copying because we hold on to the stream.
ReadonlyBytes bytes { static_cast<u8 const*>(stream->getMemoryBase()), stream->getLength() };
return adopt_ref(*new TypefaceSkia {
- make<TypefaceSkia::Impl>(skia_typeface, std::move(stream), system_ui_font_style),
+ make<TypefaceSkia::Impl>(skia_typeface, std::move(stream)),
bytes,
ttc_index });
}
@@ -188,7 +139,7 @@ ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::typeface_from_skia_typeface(sk_sp<Sk
auto memory_stream = copy_stream_to_memory_stream(*stream);
auto bytes = ReadonlyBytes { static_cast<u8 const*>(memory_stream->getMemoryBase()), memory_stream->getLength() };
return adopt_ref(*new TypefaceSkia {
- make<TypefaceSkia::Impl>(skia_typeface, move(memory_stream), system_ui_font_style),
+ make<TypefaceSkia::Impl>(skia_typeface, move(memory_stream)),
bytes,
ttc_index });
}
@@ -333,12 +284,6 @@ void TypefaceSkia::encode_font_data_for_ipc(IPC::Encoder& encoder) const
return;
}
- if (impl().system_ui_font_style.has_value()) {
- MUST(encoder.encode(FontDataFormat::SystemUIFont));
- MUST(encoder.encode(*impl().system_ui_font_style));
- return;
- }
-
auto family_name = family().to_string();
MUST(encoder.encode(FontDataFormat::SystemFont));
@@ -348,92 +293,6 @@ void TypefaceSkia::encode_font_data_for_ipc(IPC::Encoder& encoder) const
MUST(encoder.encode(slope()));
}
-#ifdef AK_OS_MACOS
-// NB: These are the CoreText string values behind the public AppKit NSFontDescriptorSystemDesign constants.
-// Keeping them here avoids pulling Objective-C headers into this C++ file.
-static CFStringRef core_text_ui_font_design(SystemUIFontKind kind)
-{
- switch (kind) {
- case SystemUIFontKind::System:
- return CFSTR("NSCTFontUIFontDesignDefault");
- case SystemUIFontKind::Serif:
- return CFSTR("NSCTFontUIFontDesignSerif");
- case SystemUIFontKind::Monospace:
- return CFSTR("NSCTFontUIFontDesignMonospaced");
- case SystemUIFontKind::Rounded:
- return CFSTR("NSCTFontUIFontDesignRounded");
- }
- VERIFY_NOT_REACHED();
-}
-
-static CTFontDescriptorRef create_system_ui_font_descriptor(SystemUIFontKind kind, u8 slope)
-{
- CGFloat core_text_slant = slope == 0 ? 0.0f : 1.0f;
- auto slant_number = CFNumberCreate(kCFAllocatorDefault, kCFNumberCGFloatType, &core_text_slant);
- if (!slant_number)
- return nullptr;
-
- CFTypeRef trait_keys[] = { kCTFontSlantTrait, CFSTR("NSCTFontUIFontDesignTrait") };
- CFTypeRef trait_values[] = { slant_number, core_text_ui_font_design(kind) };
- auto traits = CFDictionaryCreate(kCFAllocatorDefault, trait_keys, trait_values, array_size(trait_keys), &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
- CFRelease(slant_number);
- if (!traits)
- return nullptr;
-
- CFTypeRef attribute_keys[] = { kCTFontTraitsAttribute };
- CFTypeRef attribute_values[] = { traits };
- auto attributes = CFDictionaryCreate(kCFAllocatorDefault, attribute_keys, attribute_values, 1, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks);
- CFRelease(traits);
- if (!attributes)
- return nullptr;
-
- auto descriptor = CTFontDescriptorCreateWithAttributes(attributes);
- CFRelease(attributes);
- return descriptor;
-}
-
-static CTFontRef create_system_ui_font(SystemUIFontKind kind, float point_size, u8 slope)
-{
- auto base_font = CTFontCreateUIFontForLanguage(kCTFontUIFontSystem, point_size, nullptr);
- if (!base_font)
- return nullptr;
-
- auto descriptor = create_system_ui_font_descriptor(kind, slope);
- if (!descriptor) {
- CFRelease(base_font);
- return nullptr;
- }
-
- auto font = CTFontCreateCopyWithAttributes(base_font, point_size, nullptr, descriptor);
- if (!font)
- font = CTFontCreateWithFontDescriptor(descriptor, point_size, nullptr);
- CFRelease(base_font);
- CFRelease(descriptor);
- return font;
-}
-#endif
-
-ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::match_system_ui(SystemUIFontKind kind, float point_size, u16 weight, u16 width, u8 slope)
-{
-#ifdef AK_OS_MACOS
- auto ct_font = create_system_ui_font(kind, point_size, slope);
- if (!ct_font)
- return RefPtr<TypefaceSkia> {};
-
- auto skia_typeface = SkMakeTypefaceFromCTFont(ct_font);
- auto typeface = typeface_from_core_text_typeface(move(skia_typeface), ct_font, SystemUIFontStyle { kind, weight, width, slope });
- CFRelease(ct_font);
- return typeface;
-#else
- (void)kind;
- (void)point_size;
- (void)weight;
- (void)width;
- (void)slope;
- return RefPtr<TypefaceSkia> {};
-#endif
-}
-
ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::match_family_style(StringView family_name, u16 weight, u16 width, u8 slope)
{
auto skia_typeface = font_manager().matchFamilyStyle(ByteString(family_name).characters(), SkFontStyle { weight, width, slope_to_skia_slant(slope) });
@@ -483,15 +342,6 @@ u32 TypefaceSkia::platform_typeface_id() const
return impl().skia_typeface->uniqueID();
}
-hb_face_t* TypefaceSkia::create_harfbuzz_face() const
-{
-#ifdef AK_OS_MACOS
- if (impl().cg_font)
- return hb_coretext_face_create(impl().cg_font);
-#endif
- return Typeface::create_harfbuzz_face();
-}
-
TypefaceSkia::TypefaceSkia(NonnullOwnPtr<Impl> impl, ReadonlyBytes buffer, u32 ttc_index)
: m_impl(move(impl))
, m_buffer(buffer)
@@ -499,61 +349,4 @@ TypefaceSkia::TypefaceSkia(NonnullOwnPtr<Impl> impl, ReadonlyBytes buffer, u32 t
{
}
-FlyString const& TypefaceSkia::family() const
-{
- return description().family();
-}
-
-u16 TypefaceSkia::weight() const
-{
- if (auto const& style = impl().system_ui_font_style; style.has_value())
- return style->weight;
- return description().style().weight;
-}
-
-u16 TypefaceSkia::width() const
-{
- if (auto const& style = impl().system_ui_font_style; style.has_value())
- return style->width;
- return description().style().width;
-}
-
-u8 TypefaceSkia::slope() const
-{
- if (auto const& style = impl().system_ui_font_style; style.has_value())
- return style->slope;
- return description().style().slope;
-}
-
-FaceStyle TypefaceSkia::style_for_variations(ReadonlySpan<FontVariationAxis> variations) const
-{
- if (auto const& style = impl().system_ui_font_style; style.has_value())
- return { .weight = style->weight, .width = style->width, .slope = style->slope };
- return Typeface::style_for_variations(variations);
-}
-
-}
-
-namespace IPC {
-
-template<>
-ErrorOr<void> encode(Encoder& encoder, Gfx::SystemUIFontStyle const& style)
-{
- TRY(encoder.encode(style.kind));
- TRY(encoder.encode(style.weight));
- TRY(encoder.encode(style.width));
- TRY(encoder.encode(style.slope));
- return {};
-}
-
-template<>
-ErrorOr<Gfx::SystemUIFontStyle> decode(Decoder& decoder)
-{
- auto kind = TRY(decoder.decode<Gfx::SystemUIFontKind>());
- auto weight = TRY(decoder.decode<u16>());
- auto width = TRY(decoder.decode<u16>());
- auto slope = TRY(decoder.decode<u8>());
- return Gfx::SystemUIFontStyle { kind, weight, width, slope };
-}
-
}
diff --git a/Libraries/LibGfx/Font/TypefaceSkia.h b/Libraries/LibGfx/Font/TypefaceSkia.h
index 42c5eda3d3f..59819f45837 100644
--- a/Libraries/LibGfx/Font/TypefaceSkia.h
+++ b/Libraries/LibGfx/Font/TypefaceSkia.h
@@ -8,45 +8,20 @@
#include <LibGfx/Font/Typeface.h>
-#ifdef AK_OS_MACOS
-# include <CoreText/CoreText.h>
-#endif
-
template<typename T>
class sk_sp;
namespace Gfx {
-enum class SystemUIFontKind : u8 {
- System,
- Serif,
- Monospace,
- Rounded,
-};
-
-struct SystemUIFontStyle {
- SystemUIFontKind kind;
- u16 weight;
- u16 width;
- u8 slope;
-};
-
class TypefaceSkia : public Gfx::Typeface {
AK_MAKE_NONCOPYABLE(TypefaceSkia);
public:
static ErrorOr<NonnullRefPtr<TypefaceSkia>> load_from_buffer(ReadonlyBytes, u32 ttc_index, NonnullRefPtr<FontDataBacking>);
- static ErrorOr<RefPtr<TypefaceSkia>> match_system_ui(SystemUIFontKind, float point_size, u16 weight, u16 width, u8 slope);
static ErrorOr<RefPtr<TypefaceSkia>> match_family_style(StringView family_name, u16 weight, u16 width, u8 slope);
static ErrorOr<RefPtr<TypefaceSkia>> find_typeface_for_code_point(u32 code_point, u16 weight, u16 width, u8 slope, bool prefer_color_emoji);
static Optional<FlyString> resolve_generic_family(StringView family_name, u16 weight, u8 slope);
- virtual FlyString const& family() const override;
- virtual u16 weight() const override;
- virtual u16 width() const override;
- virtual u8 slope() const override;
- virtual FaceStyle style_for_variations(ReadonlySpan<FontVariationAxis>) const override;
-
virtual ReadonlyBytes buffer() const LIFETIME_BOUND override { return m_buffer; }
virtual u32 ttc_index() const override { return m_ttc_index; }
@@ -55,17 +30,13 @@ public:
protected:
virtual void encode_font_data_for_ipc(IPC::Encoder&) const override;
- virtual hb_face_t* create_harfbuzz_face() const override;
private:
struct Impl;
Impl& impl() const { return *m_impl; }
NonnullOwnPtr<Impl> m_impl;
- static ErrorOr<RefPtr<TypefaceSkia>> typeface_from_skia_typeface(sk_sp<SkTypeface>, Optional<SystemUIFontStyle> = {});
-#ifdef AK_OS_MACOS
- static ErrorOr<RefPtr<TypefaceSkia>> typeface_from_core_text_typeface(sk_sp<SkTypeface>, CTFontRef, SystemUIFontStyle);
-#endif
+ static ErrorOr<RefPtr<TypefaceSkia>> typeface_from_skia_typeface(sk_sp<SkTypeface>);
TypefaceSkia(NonnullOwnPtr<Impl>, ReadonlyBytes, u32 ttc_index = 0);
@@ -79,13 +50,3 @@ template<>
inline bool Typeface::fast_is<TypefaceSkia>() const { return is_skia(); }
}
-
-namespace IPC {
-
-template<>
-ErrorOr<void> encode(Encoder&, Gfx::SystemUIFontStyle const&);
-
-template<>
-ErrorOr<Gfx::SystemUIFontStyle> decode(Decoder&);
-
-}
diff --git a/Libraries/LibWeb/CSS/FontResolution.cpp b/Libraries/LibWeb/CSS/FontResolution.cpp
index d42fe8cf746..c1e4bbe539b 100644
--- a/Libraries/LibWeb/CSS/FontResolution.cpp
+++ b/Libraries/LibWeb/CSS/FontResolution.cpp
@@ -10,7 +10,9 @@
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Font/SystemFallbackFonts.h>
-#include <LibGfx/Font/TypefaceSkia.h>
+#ifdef AK_OS_MACOS
+# include <LibGfx/Font/TypefaceCoreText.h>
+#endif
#include <LibWeb/CSS/ComputedStyleWorkingSet.h>
#include <LibWeb/CSS/FontFaceState.h>
#include <LibWeb/CSS/FontResolution.h>
@@ -262,12 +264,12 @@ NonnullRefPtr<Gfx::FontCascadeList const> resolve_font_cascade(FontFaceSnapshot
#ifdef AK_OS_MACOS
auto find_macos_system_ui_font = [&](Gfx::SystemUIFontKind kind, Utf16FlyString const& family) -> RefPtr<Gfx::FontCascadeList const> {
auto shape_features = shape_features_for(key, &font_feature_values_for_family(family));
- auto typeface = Gfx::TypefaceSkia::match_system_ui(kind, font_size_used_value, weight, font_width_bucket_from_percentage(font_width.value()), slope);
- if (typeface.is_error() || !typeface.value())
+ auto typeface = Gfx::TypefaceCoreText::system_ui({ kind, static_cast<u16>(weight), static_cast<u16>(font_width_bucket_from_percentage(font_width.value())), static_cast<u8>(slope) });
+ if (!typeface)
return {};
auto font_list = Gfx::FontCascadeList::create();
- font_list->add(typeface.value()->font(font_size_in_pt, variation, shape_features));
+ font_list->add(typeface->font(font_size_in_pt, variation, shape_features));
return font_list;
};
#endif
diff --git a/Services/Compositor/FontSkia.cpp b/Services/Compositor/FontSkia.cpp
index 83ba48d9487..7be337320d7 100644
--- a/Services/Compositor/FontSkia.cpp
+++ b/Services/Compositor/FontSkia.cpp
@@ -20,6 +20,11 @@
# include <LibGfx/Font/GlobalFontConfig.h>
#endif
+#ifdef AK_OS_MACOS
+# include <LibGfx/Font/TypefaceCoreText.h>
+# include <ports/SkTypeface_mac.h>
+#endif
+
namespace Compositor {
#if defined(USE_FONTCONFIG)
@@ -55,15 +60,40 @@ static u64 encode_hinting_memo(float scale, Gfx::FontHintingOptions options)
namespace {
+// What the compositor keeps with each typeface.
+struct TypefaceData final : public Gfx::RasterizerData {
+ explicit TypefaceData(sk_sp<SkTypeface> typeface)
+ : typeface(move(typeface))
+ {
+ }
+
+ sk_sp<SkTypeface> typeface;
+};
+
+sk_sp<SkTypeface> make_skia_typeface(Gfx::Typeface const& typeface)
+{
+#ifdef AK_OS_MACOS
+ if (auto const* core_text_typeface = as_if<Gfx::TypefaceCoreText>(typeface))
+ return SkMakeTypefaceFromCTFont(core_text_typeface->core_text_font());
+#endif
+ return sk_ref_sp(as<Gfx::TypefaceSkia>(typeface).sk_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;
+}
+
// The typeface of the font's variations. Without variations, or if Skia cannot apply them, it is the typeface itself.
sk_sp<SkTypeface> typeface_with_variations(Gfx::Font const& font)
{
- auto const* typeface = as<Gfx::TypefaceSkia>(font.typeface()).sk_typeface();
+ auto const& typeface = skia_typeface(font.typeface());
if (!typeface)
return nullptr;
auto axes = font.variation_settings().to_sorted_list();
if (axes.is_empty())
- return sk_ref_sp(typeface);
+ return typeface;
Vector<SkFontArguments::VariationPosition::Coordinate> coordinates;
coordinates.ensure_capacity(axes.size());
@@ -74,7 +104,7 @@ sk_sp<SkTypeface> typeface_with_variations(Gfx::Font const& font)
arguments.setCollectionIndex(static_cast<int>(font.typeface().collection_index()));
if (auto clone = typeface->makeClone(arguments))
return clone;
- return sk_ref_sp(typeface);
+ return typeface;
}
// What the compositor keeps with each font.
diff --git a/Tests/LibGfx/TestFont.cpp b/Tests/LibGfx/TestFont.cpp
index 97eebe4d586..9c014d570d8 100644
--- a/Tests/LibGfx/TestFont.cpp
+++ b/Tests/LibGfx/TestFont.cpp
@@ -14,6 +14,9 @@
#include <LibGfx/Font/SystemFallbackFonts.h>
#include <LibGfx/Font/Typeface.h>
#include <LibGfx/Font/TypefaceSkia.h>
+#ifdef AK_OS_MACOS
+# include <LibGfx/Font/TypefaceCoreText.h>
+#endif
#include <LibGfx/FontCascadeList.h>
#include <LibGfx/TextLayout.h>
#include <LibIPC/Decoder.h>
@@ -377,12 +380,12 @@ static NonnullRefPtr<Gfx::Typeface const> round_trip_typeface_through_ipc(Gfx::T
return MUST(IPC::decode<NonnullRefPtr<Gfx::Typeface const>>(decoder));
}
-TEST_CASE(system_ui_italic_keeps_its_slope_when_the_variation_clone_collapses)
+TEST_CASE(system_ui_italic_keeps_its_slope_with_variations)
{
- // At 28px the system font's opsz axis sits at its maximum, so applying the default variation hands back the base
- // CoreText UI font, which Skia reads as upright.
+ // A system UI font is one variable face for every style, so its typeface and its fonts answer with the style
+ // that they were asked for, also after the typeface crosses IPC.
float const font_size = 28;
- auto typeface = MUST(Gfx::TypefaceSkia::match_system_ui(Gfx::SystemUIFontKind::System, font_size, 400, Gfx::FontWidth::Normal, 1));
+ auto typeface = Gfx::TypefaceCoreText::system_ui({ Gfx::SystemUIFontKind::System, 400, Gfx::FontWidth::Normal, 1 });
EXPECT(typeface);
Gfx::FontVariationSettings variations;
@@ -390,11 +393,12 @@ TEST_CASE(system_ui_italic_keeps_its_slope_when_the_variation_clone_collapses)
variations.set_width(100);
variations.set_optical_sizing(font_size);
auto font = typeface->font(font_size * 0.75f, variations);
+ EXPECT_EQ(font->slope(), 1u);
EXPECT_EQ(font->typeface().slope(), 1u);
auto decoded = round_trip_typeface_through_ipc(font->typeface());
EXPECT_EQ(decoded->slope(), 1u);
- EXPECT_EQ(decoded->glyph_id_for_code_point('m'), font->typeface().glyph_id_for_code_point('m'));
+ EXPECT_EQ(decoded.ptr(), &font->typeface());
}
#endif