Skip to content

Truly independent web browser

LibGfx: Map code points to glyphs with HarfBuzz

Aliaksandr Kalenikadecf72c8c0f
diff --git a/Libraries/LibGfx/Font/Typeface.cpp b/Libraries/LibGfx/Font/Typeface.cpp
index 4053b456040..739ba89e866 100644
--- a/Libraries/LibGfx/Font/Typeface.cpp
+++ b/Libraries/LibGfx/Font/Typeface.cpp
@@ -7,6 +7,8 @@
#include <harfbuzz/hb-ot.h>
#include <harfbuzz/hb.h>
+#include <AK/Atomic.h>
+#include <AK/Diagnostics.h>
#include <LibGfx/Font/Font.h>
#include <LibGfx/Font/FontDatabase.h>
#include <LibGfx/Font/FontTable.h>
@@ -50,10 +52,18 @@ ErrorOr<NonnullRefPtr<Typeface>> Typeface::try_load_from_temporary_memory(Readon
return try_load_from_anonymous_buffer(move(anonymous_buffer), ttc_index);
}
-Typeface::Typeface() = default;
+static Atomic<u64> s_next_glyph_cache_id { 1 };
+
+Typeface::Typeface()
+ : m_glyph_cache_id(s_next_glyph_cache_id.fetch_add(1, AK::MemoryOrder::memory_order_relaxed))
+{
+ VERIFY(m_glyph_cache_id != 0);
+}
Typeface::~Typeface()
{
+ if (m_cmap_font)
+ hb_font_destroy(m_cmap_font);
if (m_harfbuzz_face)
hb_face_destroy(m_harfbuzz_face);
if (m_harfbuzz_blob)
@@ -101,6 +111,122 @@ hb_face_t* Typeface::harfbuzz_typeface() const
return m_harfbuzz_face;
}
+u32 Typeface::glyph_count() const
+{
+ return hb_face_get_glyph_count(harfbuzz_typeface());
+}
+
+u16 Typeface::units_per_em() const
+{
+ return hb_face_get_upem(harfbuzz_typeface());
+}
+
+u32 Typeface::glyph_id_for_code_point(u32 code_point) const
+{
+ return glyph_page(code_point / GlyphPage::glyphs_per_page).glyph_ids[code_point % GlyphPage::glyphs_per_page];
+}
+
+Typeface::GlyphPage const& Typeface::glyph_page(size_t page_index) const
+{
+ struct GlyphPageCache {
+ AK_ALLOC_WITH_KMALLOC;
+
+ u64 last_use { 0 };
+ OwnPtr<GlyphPage> page_zero;
+ HashMap<size_t, NonnullOwnPtr<GlyphPage>> pages;
+ };
+ struct ThreadGlyphPageCaches {
+ u64 last_typeface_id { 0 };
+ GlyphPageCache* last_cache { nullptr };
+ u64 use_clock { 0 };
+ HashMap<u64, NonnullOwnPtr<GlyphPageCache>> caches;
+ };
+ // NB: -Wexit-time-destructors is a Clang-only warning, and GCC rejects the
+ // unknown option name in the pragma.
+#ifdef AK_COMPILER_CLANG
+ AK_IGNORE_DIAGNOSTIC("-Wexit-time-destructors", static thread_local ThreadGlyphPageCaches thread_caches)
+#else
+ static thread_local ThreadGlyphPageCaches thread_caches;
+#endif
+
+ auto& caches = thread_caches.caches;
+ auto* cache = thread_caches.last_cache;
+ if (thread_caches.last_typeface_id != m_glyph_cache_id) {
+ if (auto it = caches.find(m_glyph_cache_id); it != caches.end()) {
+ cache = it->value.ptr();
+ } else {
+ constexpr size_t maximum_cached_typefaces = 128;
+ if (caches.size() >= maximum_cached_typefaces) {
+ // NB: Evict the cache this thread used least recently. The caches of typefaces that are gone go first,
+ // and the ones a text run alternates between stay: evicting any other would have them evict each
+ // other at every switch once the caches of a long session filled up.
+ auto least_recently_used = caches.begin();
+ for (auto it = caches.begin(); it != caches.end(); ++it) {
+ if (it->value->last_use < least_recently_used->value->last_use)
+ least_recently_used = it;
+ }
+ caches.remove(least_recently_used);
+ }
+ auto new_cache = make<GlyphPageCache>();
+ cache = new_cache.ptr();
+ caches.set(m_glyph_cache_id, move(new_cache));
+ }
+ cache->last_use = ++thread_caches.use_clock;
+ thread_caches.last_typeface_id = m_glyph_cache_id;
+ thread_caches.last_cache = cache;
+ }
+
+ if (page_index == 0) {
+ if (!cache->page_zero) {
+ cache->page_zero = make<GlyphPage>();
+ populate_glyph_page(*cache->page_zero, 0);
+ }
+ return *cache->page_zero;
+ }
+ if (auto it = cache->pages.find(page_index); it != cache->pages.end()) {
+ return *it->value;
+ }
+
+ auto glyph_page = make<GlyphPage>();
+ populate_glyph_page(*glyph_page, page_index);
+ auto const* glyph_page_ptr = glyph_page.ptr();
+ cache->pages.set(page_index, move(glyph_page));
+ return *glyph_page_ptr;
+}
+
+static thread_local u64 s_glyph_pages_populated_on_this_thread = 0;
+
+u64 Typeface::glyph_pages_populated_on_this_thread()
+{
+ return s_glyph_pages_populated_on_this_thread;
+}
+
+void Typeface::populate_glyph_page(GlyphPage& glyph_page, size_t page_index) const
+{
+ ++s_glyph_pages_populated_on_this_thread;
+ u32 first_code_point = page_index * GlyphPage::glyphs_per_page;
+ auto* font = cmap_font();
+ auto glyph_count = this->glyph_count();
+ for (size_t i = 0; i < GlyphPage::glyphs_per_page; ++i) {
+ u32 code_point = first_code_point + i;
+ hb_codepoint_t glyph_id = 0;
+ // A cmap can name a glyph that the face does not have. Such a code point has no glyph.
+ if (!hb_font_get_nominal_glyph(font, code_point, &glyph_id) || glyph_id >= glyph_count)
+ glyph_id = 0;
+ glyph_page.glyph_ids[i] = static_cast<u16>(glyph_id);
+ }
+}
+
+// An unscaled font for code point lookups. The face's cmap gives the same glyphs at every size.
+hb_font_t* Typeface::cmap_font() const
+{
+ call_once(m_cmap_font_once, [&] {
+ m_cmap_font = hb_font_create(harfbuzz_typeface());
+ hb_font_make_immutable(m_cmap_font);
+ });
+ return m_cmap_font;
+}
+
FaceDescription const& Typeface::description() const
{
call_once(m_description_once, [&] {
diff --git a/Libraries/LibGfx/Font/Typeface.h b/Libraries/LibGfx/Font/Typeface.h
index 5ba05d1ef31..54bd064ffcd 100644
--- a/Libraries/LibGfx/Font/Typeface.h
+++ b/Libraries/LibGfx/Font/Typeface.h
@@ -29,6 +29,7 @@
class SkTypeface;
struct hb_blob_t;
struct hb_face_t;
+struct hb_font_t;
namespace Gfx {
@@ -80,9 +81,9 @@ public:
virtual ~Typeface();
- virtual u32 glyph_count() const = 0;
- virtual u16 units_per_em() const = 0;
- virtual u32 glyph_id_for_code_point(u32 code_point) const = 0;
+ 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;
@@ -123,6 +124,9 @@ public:
virtual bool is_skia() 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();
+
protected:
enum class FontDataFormat : u8 {
RawFontData,
@@ -158,11 +162,29 @@ private:
void clear_font_cache() const;
+ // This cache stores information per code point.
+ // It's segmented into pages with data about 256 code points each.
+ struct GlyphPage {
+ AK_ALLOC_WITH_KMALLOC;
+
+ static constexpr size_t glyphs_per_page = 256;
+ u16 glyph_ids[glyphs_per_page];
+ };
+
+ [[nodiscard]] GlyphPage const& glyph_page(size_t page_index) const;
+ void populate_glyph_page(GlyphPage&, size_t page_index) const;
+ hb_font_t* cmap_font() const;
+
+ // Addresses can be reused after destruction, so per-thread caches use a monotonic identity.
+ u64 m_glyph_cache_id { 0 };
+
mutable Mutex m_fonts_mutex;
mutable HashMap<FontCacheKey, NonnullRefPtr<Font>> m_fonts;
mutable OnceFlag m_harfbuzz_face_once;
mutable hb_blob_t* m_harfbuzz_blob { nullptr };
mutable hb_face_t* m_harfbuzz_face { nullptr };
+ mutable OnceFlag m_cmap_font_once;
+ mutable hb_font_t* m_cmap_font { nullptr };
mutable OnceFlag m_description_once;
mutable Optional<FaceDescription> m_description;
mutable OnceFlag m_bounding_box_once;
diff --git a/Libraries/LibGfx/Font/TypefaceSkia.cpp b/Libraries/LibGfx/Font/TypefaceSkia.cpp
index ea7aa7e2537..eb30720d532 100644
--- a/Libraries/LibGfx/Font/TypefaceSkia.cpp
+++ b/Libraries/LibGfx/Font/TypefaceSkia.cpp
@@ -47,8 +47,6 @@
namespace Gfx {
-static Atomic<u64> s_next_glyph_cache_id { 1 };
-
static auto& skia_font_manager()
{
static NeverDestroyed<sk_sp<SkFontMgr>> font_manager;
@@ -545,109 +543,7 @@ TypefaceSkia::TypefaceSkia(NonnullOwnPtr<Impl> impl, ReadonlyBytes buffer, u32 t
: m_impl(move(impl))
, m_buffer(buffer)
, m_ttc_index(ttc_index)
- , m_glyph_cache_id(s_next_glyph_cache_id.fetch_add(1, AK::MemoryOrder::memory_order_relaxed))
-{
- VERIFY(m_glyph_cache_id != 0);
-}
-
-u32 TypefaceSkia::glyph_count() const
-{
- return impl().skia_typeface->countGlyphs();
-}
-
-u16 TypefaceSkia::units_per_em() const
-{
- return impl().skia_typeface->getUnitsPerEm();
-}
-
-u32 TypefaceSkia::glyph_id_for_code_point(u32 code_point) const
-{
- return glyph_page(code_point / GlyphPage::glyphs_per_page).glyph_ids[code_point % GlyphPage::glyphs_per_page];
-}
-
-TypefaceSkia::GlyphPage const& TypefaceSkia::glyph_page(size_t page_index) const
-{
- struct GlyphPageCache {
- AK_ALLOC_WITH_KMALLOC;
-
- u64 last_use { 0 };
- OwnPtr<GlyphPage> page_zero;
- HashMap<size_t, NonnullOwnPtr<GlyphPage>> pages;
- };
- struct ThreadGlyphPageCaches {
- u64 last_typeface_id { 0 };
- GlyphPageCache* last_cache { nullptr };
- u64 use_clock { 0 };
- HashMap<u64, NonnullOwnPtr<GlyphPageCache>> caches;
- };
- // NB: -Wexit-time-destructors is a Clang-only warning, and GCC rejects the
- // unknown option name in the pragma.
-#ifdef AK_COMPILER_CLANG
- AK_IGNORE_DIAGNOSTIC("-Wexit-time-destructors", static thread_local ThreadGlyphPageCaches thread_caches)
-#else
- static thread_local ThreadGlyphPageCaches thread_caches;
-#endif
-
- auto& caches = thread_caches.caches;
- auto* cache = thread_caches.last_cache;
- if (thread_caches.last_typeface_id != m_glyph_cache_id) {
- if (auto it = caches.find(m_glyph_cache_id); it != caches.end()) {
- cache = it->value.ptr();
- } else {
- constexpr size_t maximum_cached_typefaces = 128;
- if (caches.size() >= maximum_cached_typefaces) {
- // NB: Evict the cache this thread used least recently. The caches of typefaces that are gone go first,
- // and the ones a text run alternates between stay: evicting any other would have them evict each
- // other at every switch once the caches of a long session filled up.
- auto least_recently_used = caches.begin();
- for (auto it = caches.begin(); it != caches.end(); ++it) {
- if (it->value->last_use < least_recently_used->value->last_use)
- least_recently_used = it;
- }
- caches.remove(least_recently_used);
- }
- auto new_cache = make<GlyphPageCache>();
- cache = new_cache.ptr();
- caches.set(m_glyph_cache_id, move(new_cache));
- }
- cache->last_use = ++thread_caches.use_clock;
- thread_caches.last_typeface_id = m_glyph_cache_id;
- thread_caches.last_cache = cache;
- }
-
- if (page_index == 0) {
- if (!cache->page_zero) {
- cache->page_zero = make<GlyphPage>();
- populate_glyph_page(*cache->page_zero, 0);
- }
- return *cache->page_zero;
- }
- if (auto it = cache->pages.find(page_index); it != cache->pages.end()) {
- return *it->value;
- }
-
- auto glyph_page = make<GlyphPage>();
- populate_glyph_page(*glyph_page, page_index);
- auto const* glyph_page_ptr = glyph_page.ptr();
- cache->pages.set(page_index, move(glyph_page));
- return *glyph_page_ptr;
-}
-
-static thread_local u64 s_glyph_pages_populated_on_this_thread = 0;
-
-u64 TypefaceSkia::glyph_pages_populated_on_this_thread()
{
- return s_glyph_pages_populated_on_this_thread;
-}
-
-void TypefaceSkia::populate_glyph_page(GlyphPage& glyph_page, size_t page_index) const
-{
- ++s_glyph_pages_populated_on_this_thread;
- u32 first_code_point = page_index * GlyphPage::glyphs_per_page;
- for (size_t i = 0; i < GlyphPage::glyphs_per_page; ++i) {
- u32 code_point = first_code_point + i;
- glyph_page.glyph_ids[i] = impl().skia_typeface->unicharToGlyph(code_point);
- }
}
FlyString const& TypefaceSkia::family() const
diff --git a/Libraries/LibGfx/Font/TypefaceSkia.h b/Libraries/LibGfx/Font/TypefaceSkia.h
index d2c518560dd..549f78e574a 100644
--- a/Libraries/LibGfx/Font/TypefaceSkia.h
+++ b/Libraries/LibGfx/Font/TypefaceSkia.h
@@ -43,9 +43,6 @@ public:
RefPtr<TypefaceSkia const> clone_with_variations(Vector<FontVariationAxis> const& axes) const;
- virtual u32 glyph_count() const override;
- virtual u16 units_per_em() const override;
- virtual u32 glyph_id_for_code_point(u32 code_point) const override;
virtual FlyString const& family() const override;
virtual u16 weight() const override;
virtual u16 width() const override;
@@ -58,9 +55,6 @@ public:
SkTypeface const* sk_typeface() const;
u32 platform_typeface_id() const;
- // 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();
-
protected:
virtual void encode_font_data_for_ipc(IPC::Encoder&) const override;
virtual hb_face_t* create_harfbuzz_face() const override;
@@ -81,21 +75,6 @@ private:
ReadonlyBytes m_buffer;
u32 m_ttc_index { 0 };
-
- // This cache stores information per code point.
- // It's segmented into pages with data about 256 code points each.
- struct GlyphPage {
- AK_ALLOC_WITH_KMALLOC;
-
- static constexpr size_t glyphs_per_page = 256;
- u16 glyph_ids[glyphs_per_page];
- };
-
- // Addresses can be reused after destruction, so per-thread caches use a monotonic identity.
- u64 m_glyph_cache_id { 0 };
-
- [[nodiscard]] GlyphPage const& glyph_page(size_t page_index) const;
- void populate_glyph_page(GlyphPage&, size_t page_index) const;
};
template<>
diff --git a/Tests/LibGfx/TestFont.cpp b/Tests/LibGfx/TestFont.cpp
index f78d8605a53..4f0ff247337 100644
--- a/Tests/LibGfx/TestFont.cpp
+++ b/Tests/LibGfx/TestFont.cpp
@@ -566,12 +566,12 @@ TEST_CASE(glyph_page_caches_keep_the_typefaces_in_use_once_full)
auto thread = Threading::Thread::construct("GlyphPageCache"sv, [&typefaces, &pages_populated_in_turns]() {
for (auto const& typeface : typefaces)
(void)typeface->glyph_id_for_code_point('A');
- auto populated_before_turns = Gfx::TypefaceSkia::glyph_pages_populated_on_this_thread();
+ auto populated_before_turns = Gfx::Typeface::glyph_pages_populated_on_this_thread();
for (size_t turn = 0; turn < 50; ++turn) {
for (size_t i = 0; i < 8; ++i)
(void)typefaces[i * 20]->glyph_id_for_code_point('A');
}
- pages_populated_in_turns = Gfx::TypefaceSkia::glyph_pages_populated_on_this_thread() - populated_before_turns;
+ pages_populated_in_turns = Gfx::Typeface::glyph_pages_populated_on_this_thread() - populated_before_turns;
return 0;
});
thread->start();