Skip to content

Truly independent web browser

LibGfx+Compositor: Draw installed bitmap fonts by name on macOS

Andreas Klingfc2194ca6364
diff --git a/Libraries/LibGfx/Font/TypefaceCoreText.cpp b/Libraries/LibGfx/Font/TypefaceCoreText.cpp
index d451f8f963b..31e4ba268b7 100644
--- a/Libraries/LibGfx/Font/TypefaceCoreText.cpp
+++ b/Libraries/LibGfx/Font/TypefaceCoreText.cpp
@@ -319,6 +319,11 @@ CTFontRef create_core_text_font_from_data(NonnullRefPtr<Typeface::FontDataBackin
return font;
}
+bool typeface_has_bitmap_glyphs(Typeface const& typeface)
+{
+ return hb_ot_color_has_png(typeface.harfbuzz_typeface());
+}
+
bool core_text_accepts_font_data(NonnullRefPtr<Typeface::FontDataBacking> backing, u32 ttc_index)
{
auto bytes = backing->bytes();
diff --git a/Libraries/LibGfx/Font/TypefaceCoreText.h b/Libraries/LibGfx/Font/TypefaceCoreText.h
index 4776627df1f..9598f3e5106 100644
--- a/Libraries/LibGfx/Font/TypefaceCoreText.h
+++ b/Libraries/LibGfx/Font/TypefaceCoreText.h
@@ -63,6 +63,9 @@ CTFontRef create_core_text_font_from_data(NonnullRefPtr<Typeface::FontDataBackin
// Whether CoreText loads a face of font data the way the compositor does to draw it.
bool core_text_accepts_font_data(NonnullRefPtr<Typeface::FontDataBacking>, u32 ttc_index);
+// Whether a typeface has glyphs that are bitmaps, as Apple Color Emoji does.
+bool typeface_has_bitmap_glyphs(Typeface const&);
+
// A copy of a CoreText font at a size, with these coordinates on top of the variation of the font. Without
// coordinates, the copy keeps the variation of the font. Null if CoreText does not make the copy.
CTFontRef create_core_text_font_with_variations(CTFontRef, CGFloat size, ReadonlySpan<FontVariationAxis>);
diff --git a/Services/Compositor/FontSkia.cpp b/Services/Compositor/FontSkia.cpp
index 30f1fa76f93..bc6c06d2c7f 100644
--- a/Services/Compositor/FontSkia.cpp
+++ b/Services/Compositor/FontSkia.cpp
@@ -82,6 +82,24 @@ struct TypefaceData final : public Gfx::RasterizerData {
sk_sp<SkTypeface> typeface;
};
+#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
+// the font that draws the glyphs changes, which has the same glyphs as the data that shapes them.
+static sk_sp<SkTypeface> make_installed_bitmap_font_skia_typeface(Gfx::Typeface const& typeface)
+{
+ if (!typeface.system_font_identifier().has_value() || !Gfx::typeface_has_bitmap_glyphs(typeface))
+ return nullptr;
+ auto postscript_name = typeface.postscript_name();
+ if (!postscript_name.has_value())
+ return nullptr;
+ auto installed_typeface = Gfx::TypefaceCoreText::try_load_postscript_name(*postscript_name);
+ if (installed_typeface.is_error())
+ return nullptr;
+ return SkMakeTypefaceFromCTFont(installed_typeface.value()->core_text_font());
+}
+#endif
+
// The Skia typeface of font data from the font manager of the platform. Null where FreeType is that font manager.
// The typeface loaders are called directly, because a font manager also finds the system fonts when it is made.
sk_sp<SkTypeface> make_platform_skia_typeface([[maybe_unused]] sk_sp<SkData> const& data, [[maybe_unused]] Gfx::Typeface const& typeface)
@@ -89,6 +107,9 @@ sk_sp<SkTypeface> make_platform_skia_typeface([[maybe_unused]] sk_sp<SkData> con
if (s_font_rasterizer == FontRasterizer::FreeType)
return nullptr;
#if defined(AK_OS_MACOS)
+ if (auto installed_typeface = make_installed_bitmap_font_skia_typeface(typeface))
+ return installed_typeface;
+
// The CoreText typeface loader of Skia loads only the first face of font data.
if (typeface.collection_index() != 0) {
auto font = Gfx::create_core_text_font_from_data(typeface.font_data_backing().release_nonnull(), typeface.font_data(), typeface.collection_index());