Truly independent web browser
LibGfx+LibWebView+WebContent+Compositor: Load typefaces without Skia
Aliaksandr Kalenik5fe49706ff0a
diff --git a/Libraries/LibGfx/CMakeLists.txt b/Libraries/LibGfx/CMakeLists.txtindex 5b0b116652e..e01d32ecdff 100644--- a/Libraries/LibGfx/CMakeLists.txt+++ b/Libraries/LibGfx/CMakeLists.txt@@ -23,7 +23,6 @@ set(SOURCESFont/SystemFallbackFonts.cppFont/SystemFontMatcher.cppFont/Typeface.cpp- Font/TypefaceSkia.cppFont/WOFF/Loader.cppFont/WOFF2/Loader.cppFontCascadeList.cppdiff --git a/Libraries/LibGfx/Font/FontDatabase.h b/Libraries/LibGfx/Font/FontDatabase.hindex 8f5d7132979..1c494cad137 100644--- a/Libraries/LibGfx/Font/FontDatabase.h+++ b/Libraries/LibGfx/Font/FontDatabase.h@@ -44,9 +44,6 @@ public:void for_each_typeface_with_family_name(FlyString const& family_name, Function<void(Typeface const&)>);[[nodiscard]] bool has_system_font_provider() const { return m_system_font_provider; }- void set_force_freetype_rasterization(bool force) { m_force_freetype_rasterization = force; }- [[nodiscard]] bool force_freetype_rasterization() const { return m_force_freetype_rasterization; }-static ErrorOr<Vector<String>> font_directories();private:@@ -54,7 +51,6 @@ private:~FontDatabase() = default;OwnPtr<SystemFontProvider> m_system_font_provider;- bool m_force_freetype_rasterization { false };};}diff --git a/Libraries/LibGfx/Font/FontTable.cpp b/Libraries/LibGfx/Font/FontTable.cppindex c117c7be876..9511d532ab2 100644--- a/Libraries/LibGfx/Font/FontTable.cpp+++ b/Libraries/LibGfx/Font/FontTable.cpp@@ -41,22 +41,31 @@ struct TableLocation {size_t length { 0 };};-// https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-directory-static Optional<TableLocation> find_table(FontDataReader const& data, unsigned face_index, FourCC tag)+Optional<size_t> table_directory_offset(FontDataReader const& data, u32 face_index){- size_t directory_offset = 0;+ if (!data.contains(0, 4))+ return {};// https://learn.microsoft.com/en-us/typography/opentype/spec/otff#ttc-header- if (data.contains(0, 4) && data.u32_at(0) == FourCC { "ttcf" }.to_u32()) {+ if (data.u32_at(0) == FourCC { "ttcf" }.to_u32()) {if (!data.contains(8, 4) || face_index >= data.u32_at(8) || !data.contains(12 + 4 * static_cast<size_t>(face_index), 4))return {};- directory_offset = data.u32_at(12 + 4 * face_index);+ return data.u32_at(12 + 4 * face_index);}- if (!data.contains(directory_offset, 12))+ if (face_index != 0)+ return {};+ return 0;+}++// https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-directory+static Optional<TableLocation> find_table(FontDataReader const& data, unsigned face_index, FourCC tag)+{+ auto directory_offset = table_directory_offset(data, face_index);+ if (!directory_offset.has_value() || !data.contains(*directory_offset, 12))return {};- auto table_count = data.u16_at(directory_offset + 4);+ auto table_count = data.u16_at(*directory_offset + 4);for (size_t index = 0; index < table_count; ++index) {- auto record_offset = directory_offset + 12 + 16 * index;+ auto record_offset = *directory_offset + 12 + 16 * index;if (!data.contains(record_offset, 16))return {};if (data.u32_at(record_offset) != tag.to_u32())@@ -145,6 +154,7 @@ FontTable::FontTable(hb_face_t* face, FourCC tag)// The upper 16 bits of the index select a named instance, not a face.if (auto location = find_table(data, hb_face_get_index(face) & 0xFFFF, tag); location.has_value()) {static_cast<FontDataReader&>(*this) = FontDataReader { data.bytes().slice(location->offset, location->length) };+ m_with_following_data = FontDataReader { data.bytes().slice(location->offset) };}return;}@@ -157,6 +167,7 @@ FontTable::FontTable(hb_face_t* face, FourCC tag)if (table_pointer && length > 0) {FontDataReader table { { reinterpret_cast<u8 const*>(table_pointer), length } };static_cast<FontDataReader&>(*this) = table;+ m_with_following_data = table;}}diff --git a/Libraries/LibGfx/Font/FontTable.h b/Libraries/LibGfx/Font/FontTable.hindex 70ee0e24ddb..a93e688efad 100644--- a/Libraries/LibGfx/Font/FontTable.h+++ b/Libraries/LibGfx/Font/FontTable.h@@ -40,6 +40,9 @@ private:ReadonlyBytes m_bytes;};+// The offset of the table directory of a face, if the data is an SFNT font or a collection of them.+Optional<size_t> table_directory_offset(FontDataReader const&, u32 face_index);+// Whether a face has a table, by the same rules as FontTable, without reading the table.bool face_has_table(hb_face_t*, FourCC tag);@@ -56,8 +59,13 @@ public:FontTable(hb_face_t*, FourCC tag);~FontTable();+ // The table and the font data after it. FreeType reads the fixed-size fields of a table from here, so a field+ // that a table which is too short cuts off comes from the data that follows the table.+ FontDataReader const& with_following_data() const { return m_with_following_data; }+private:hb_blob_t* m_blob { nullptr };+ FontDataReader m_with_following_data;};}diff --git a/Libraries/LibGfx/Font/GlobalFontConfig.cpp b/Libraries/LibGfx/Font/GlobalFontConfig.cppindex 5da1642c6d2..c9b24803de7 100644--- a/Libraries/LibGfx/Font/GlobalFontConfig.cpp+++ b/Libraries/LibGfx/Font/GlobalFontConfig.cpp@@ -6,6 +6,7 @@#include <AK/Assertions.h>#include <AK/ByteString.h>+#include <AK/LsanSuppressions.h>#include <AK/NeverDestroyed.h>#include <LibGfx/Font/GlobalFontConfig.h>#include <fontconfig/fontconfig.h>diff --git a/Libraries/LibGfx/Font/PathFontProvider.cpp b/Libraries/LibGfx/Font/PathFontProvider.cppindex 520e9258c05..7e11fc635f4 100644--- a/Libraries/LibGfx/Font/PathFontProvider.cpp+++ b/Libraries/LibGfx/Font/PathFontProvider.cpp@@ -13,7 +13,6 @@#include <LibFileSystem/FileSystem.h>#include <LibGfx/Font/Font.h>#include <LibGfx/Font/PathFontProvider.h>-#include <LibGfx/Font/TypefaceSkia.h>#include <LibGfx/Font/WOFF/Loader.h>namespace Gfx {@@ -85,9 +84,8 @@ void PathFontProvider::for_each_typeface_in_uri(StringView uri, HashTable<Stringif (is_truetype) {auto font_count = number_of_fonts_in_ttc(resource.data());auto backing = make_ref_counted<Typeface::FontDataBacking>(resource.release_mapped_file());- auto bytes = backing->storage.get<NonnullOwnPtr<Core::MappedFile>>()->bytes();for (u32 ttc_index = 0; ttc_index < font_count; ++ttc_index) {- if (auto font_or_error = TypefaceSkia::load_from_buffer(bytes, ttc_index, backing); !font_or_error.is_error()) {+ if (auto font_or_error = Typeface::try_load_from_font_data(backing, ttc_index); !font_or_error.is_error()) {auto font = font_or_error.release_value();font->set_file_path(filesystem_path);callback(filesystem_path, ttc_index, FontFileFormat::OpenType, move(font));diff --git a/Libraries/LibGfx/Font/SystemFontMatcher.cpp b/Libraries/LibGfx/Font/SystemFontMatcher.cppindex dacd8cd3e3d..d85e7afecd4 100644--- a/Libraries/LibGfx/Font/SystemFontMatcher.cpp+++ b/Libraries/LibGfx/Font/SystemFontMatcher.cpp@@ -77,8 +77,15 @@ static Optional<SystemFontFile> find_loadable_file(StringView path, u32 ttc_indeif (mapped_file.is_error())return {};auto format = format_of(mapped_file.value()->bytes());- if (load_mapped_file(mapped_file.release_value(), ttc_index, format).is_error())+ auto typeface = load_mapped_file(mapped_file.release_value(), ttc_index, format);+ if (typeface.is_error())return {};+#ifdef AK_OS_MACOS+ // The compositor draws the faces of installed fonts with CoreText, which does not load every face that HarfBuzz+ // reads, such as one with hvgl outlines.+ if (!core_text_accepts_font_data(*typeface.value()->font_data_backing(), ttc_index))+ return {};+#endifauto string_path = String::from_byte_string(canonical_path.release_value());if (string_path.is_error())return {};diff --git a/Libraries/LibGfx/Font/Typeface.cpp b/Libraries/LibGfx/Font/Typeface.cppindex 98ef4d945c6..278aa2298fd 100644--- a/Libraries/LibGfx/Font/Typeface.cpp+++ b/Libraries/LibGfx/Font/Typeface.cpp@@ -11,12 +11,12 @@#include <AK/Atomic.h>#include <AK/Diagnostics.h>#include <AK/NeverDestroyed.h>+#include <AK/ScopeGuard.h>#include <LibGfx/Font/Font.h>#include <LibGfx/Font/FontDatabase.h>#include <LibGfx/Font/FontTable.h>#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@@ -62,16 +62,191 @@ u8 Typeface::slope() constreturn fixed_style().value_or(description().style()).slope;}+ReadonlyBytes Typeface::FontDataBacking::bytes() const+{+ return storage.visit(+ [](Core::AnonymousBuffer const& anonymous_buffer) { return anonymous_buffer.bytes(); },+ [](NonnullOwnPtr<Core::MappedFile> const& mapped_file) { return mapped_file->bytes(); });+}++static constexpr FourCC truetype_tag { "true" };++static bool is_sfnt_version(u32 tag)+{+ // https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-directory+ return tag == 0x00010000 || tag == FourCC { "OTTO" }.to_u32() || tag == truetype_tag.to_u32();+}++// https://learn.microsoft.com/en-us/typography/opentype/spec/otff#table-directory+// The rules of FreeType for a table directory. A table that starts after the end of the data does not count, and+// neither does one that ends after it, except for the metrics tables, which FreeType cuts off.+static bool is_valid_table_directory(FontDataReader const& data, size_t offset, u32 version)+{+ if (!data.contains(offset, 12))+ return false;+ size_t table_count = data.u16_at(offset + 4);+ if (table_count == 0)+ return false;+ // FreeType checks the directory of a face with CFF outlines no further.+ if (version == FourCC { "OTTO" }.to_u32())+ return data.contains(offset + 12, table_count * 16);++ size_t valid_tables = 0;+ bool has_head = false;+ bool has_sing = false;+ bool has_meta = false;+ for (size_t index = 0; index < table_count && data.contains(offset + 12 + index * 16, 16); ++index) {+ auto record = offset + 12 + index * 16;+ auto tag = data.u32_at(record);+ size_t table_offset = data.u32_at(record + 8);+ size_t table_length = data.u32_at(record + 12);+ if (table_offset > data.bytes().size())+ continue;+ if (table_length > data.bytes().size() - table_offset && tag != FourCC { "hmtx" }.to_u32() && tag != FourCC { "vmtx" }.to_u32())+ continue;+ ++valid_tables;+ if (tag == FourCC { "head" }.to_u32() || tag == FourCC { "bhed" }.to_u32()) {+ // Each font header must have its full size, whichever of them FreeType reads.+ if (table_length < 54)+ return false;+ has_head = true;+ } else if (tag == FourCC { "SING" }.to_u32()) {+ has_sing = true;+ } else if (tag == FourCC { "META" }.to_u32()) {+ has_meta = true;+ }+ }+ // A face with SING and META tables, which Adobe fonts of single glyphs have, does not need a font header.+ return valid_tables > 0 && (has_head || (has_sing && has_meta));+}++// https://learn.microsoft.com/en-us/typography/opentype/spec/fvar#instancerecord+// The number of named instances that the upper 16 bits of a face index can select. If no instance of a face has the+// default coordinates, FreeType adds an instance with them after the others. It gives a face with only CFF outlines+// no named instances.+static u32 named_instance_count(hb_face_t* face)+{+ if (face_has_table(face, FourCC { "CFF " }) && !face_has_table(face, FourCC { "glyf" }) && !face_has_table(face, FourCC { "CFF2" }))+ return 0;+ auto axis_count = hb_ot_var_get_axis_count(face);+ if (axis_count == 0)+ return 0;++ Vector<hb_ot_var_axis_info_t> axes;+ axes.resize(axis_count);+ hb_ot_var_get_axis_infos(face, 0, &axis_count, axes.data());++ auto instance_count = hb_ot_var_get_named_instance_count(face);+ Vector<float> coordinates;+ coordinates.resize(axis_count);+ for (unsigned instance = 0; instance < instance_count; ++instance) {+ auto coordinate_count = axis_count;+ hb_ot_var_named_instance_get_design_coords(face, instance, &coordinate_count, coordinates.data());+ bool has_default_coordinates = coordinate_count == axis_count;+ for (unsigned index = 0; has_default_coordinates && index < axis_count; ++index)+ has_default_coordinates = coordinates[index] == axes[index].default_value;+ if (has_default_coordinates)+ return instance_count;+ }+ return instance_count + 1;+}++// Font data must have a face that FreeType loads as an SFNT font and that HarfBuzz reads. These are the rules by which+// FreeType loads the tables of a face.+static ErrorOr<void> validate_face(ReadonlyBytes bytes, u32 ttc_index)+{+ // Skia does not load font data of more than 1 GiB.+ if (bytes.size() > 1 * GiB)+ return Error::from_string_literal("Font data is too large");++ auto face_index = ttc_index & 0xFFFF;+ FontDataReader data { bytes };+ auto directory_offset = table_directory_offset(data, face_index);+ if (!directory_offset.has_value() || !data.contains(*directory_offset, 4))+ return Error::from_string_literal("Font data has no face in the SFNT format");+ auto version = data.u32_at(*directory_offset);+ if (!is_sfnt_version(version) || !is_valid_table_directory(data, *directory_offset, version))+ return Error::from_string_literal("Font data has no face in the SFNT format");++ auto* blob = hb_blob_create(reinterpret_cast<char const*>(bytes.data()), bytes.size(), HB_MEMORY_MODE_READONLY, nullptr, nullptr);+ ScopeGuard destroy_blob = [&] { hb_blob_destroy(blob); };+ if (face_index >= hb_face_count(blob))+ return Error::from_string_literal("Font data has no face that HarfBuzz can read");+ auto* face = hb_face_create(blob, ttc_index);+ ScopeGuard destroy_face = [&] { hb_face_destroy(face); };++ auto has_table = [&](char const* tag) { return face_has_table(face, FourCC { tag }); };+ // A header of a table that FreeType reads with its fixed size, from the following data if the table is shorter.+ auto read_header = [&](char const* tag, size_t size) -> Optional<FontDataReader> {+ FontTable table { face, FourCC { tag } };+ if (table.is_empty() || !table.with_following_data().contains(0, size))+ return {};+ return table.with_following_data();+ };++ // https://learn.microsoft.com/en-us/typography/opentype/spec/head+ // A face without outlines can be an Apple bitmap font, which has a bhed table instead of a head table.+ bool has_outlines = has_table("glyf") || has_table("CFF ") || has_table("CFF2");+ auto bitmap_header = has_outlines ? Optional<FontDataReader> {} : read_header("bhed", 54);+ bool is_apple_bitmap_font = bitmap_header.has_value();+ auto header = bitmap_header;+ if (!is_apple_bitmap_font || has_table("sbix")) {+ header = read_header("head", 54);+ if (!header.has_value())+ return Error::from_string_literal("Font face has no head table");+ }+ auto units_per_em = header->u16_at(18);+ if (units_per_em < 16 || units_per_em > 16384)+ return Error::from_string_literal("Font face has an invalid number of units per em");++ // https://learn.microsoft.com/en-us/typography/opentype/spec/maxp+ if (!has_table("maxp"))+ return Error::from_string_literal("Font face has no maxp table");++ // https://learn.microsoft.com/en-us/typography/opentype/spec/hhea+ // A TrueType font for macOS does not need horizontal metrics.+ if (!is_apple_bitmap_font) {+ if (read_header("hhea", 36).has_value()) {+ if (!has_table("hmtx"))+ return Error::from_string_literal("Font face has no hmtx table");+ } else if (version != truetype_tag.to_u32()) {+ return Error::from_string_literal("Font face has no hhea table");+ }+ }++ if (version == FourCC { "OTTO" }.to_u32()) {+ // https://learn.microsoft.com/en-us/typography/opentype/spec/otff#organization-of-an-opentype-font+ if (!has_table("CFF ") && !has_table("CFF2"))+ return Error::from_string_literal("Font face with CFF outlines has no CFF table");+ } else {+ // https://learn.microsoft.com/en-us/typography/opentype/spec/loca+ // FreeType needs a loca table for every TrueType face that it does not draw from bitmap strikes alone. It+ // draws a face without outlines or strikes as a face with empty outlines, and color bitmaps replace outlines.+ bool has_scalable_outlines = has_outlines && !has_table("CBLC") && !has_table("CBDT");+ bool has_bitmap_strikes = has_table("EBLC") || has_table("CBLC") || has_table("bloc") || has_table("sbix");+ if ((has_scalable_outlines || !has_bitmap_strikes) && !has_table("loca"))+ return Error::from_string_literal("Font face has no loca table");+ }++ if ((ttc_index >> 16) > named_instance_count(face))+ return Error::from_string_literal("Font face has no such named instance");+ return {};+}++ErrorOr<NonnullRefPtr<Typeface>> Typeface::try_load_from_font_data(NonnullRefPtr<FontDataBacking> font_data, u32 ttc_index)+{+ TRY(validate_face(font_data->bytes(), ttc_index));+ return adopt_ref(*new Typeface(move(font_data), ttc_index));+}+ErrorOr<NonnullRefPtr<Typeface>> Typeface::try_load_from_mapped_file(NonnullOwnPtr<Core::MappedFile> mapped_file, u32 ttc_index){- auto bytes = mapped_file->bytes();- return TypefaceSkia::load_from_buffer(bytes, ttc_index, make_ref_counted<FontDataBacking>(move(mapped_file)));+ return try_load_from_font_data(make_ref_counted<FontDataBacking>(move(mapped_file)), ttc_index);}ErrorOr<NonnullRefPtr<Typeface>> Typeface::try_load_from_anonymous_buffer(Core::AnonymousBuffer anonymous_buffer, u32 ttc_index){- auto bytes = anonymous_buffer.bytes();- return TypefaceSkia::load_from_buffer(bytes, ttc_index, make_ref_counted<FontDataBacking>(move(anonymous_buffer)));+ return try_load_from_font_data(make_ref_counted<FontDataBacking>(move(anonymous_buffer)), ttc_index);}ErrorOr<NonnullRefPtr<Typeface>> Typeface::try_load_from_temporary_memory(ReadonlyBytes bytes, u32 ttc_index)@@ -90,6 +265,13 @@ Typeface::Typeface()VERIFY(m_glyph_cache_id != 0);}+Typeface::Typeface(NonnullRefPtr<FontDataBacking> font_data, u32 ttc_index)+ : Typeface()+{+ m_font_data = move(font_data);+ m_ttc_index = ttc_index;+}+Typeface::~Typeface(){if (m_cmap_font)@@ -332,8 +514,8 @@ Typeface::BoundingBoxInFontUnits Typeface::bounding_box_in_font_units() consthb_face_t* Typeface::create_harfbuzz_face() const{if (!m_harfbuzz_blob)- m_harfbuzz_blob = hb_blob_create(reinterpret_cast<char const*>(buffer().data()), buffer().size(), HB_MEMORY_MODE_READONLY, nullptr, [](void*) { });- return hb_face_create(m_harfbuzz_blob, ttc_index());+ m_harfbuzz_blob = hb_blob_create(reinterpret_cast<char const*>(font_data().data()), font_data().size(), HB_MEMORY_MODE_READONLY, nullptr, [](void*) { });+ return hb_face_create(m_harfbuzz_blob, m_ttc_index);}void Typeface::encode_font_data_for_ipc(IPC::Encoder& encoder) const@@ -348,7 +530,7 @@ void Typeface::encode_font_data_for_ipc(IPC::Encoder& encoder) constif (m_file_path.has_value()) {MUST(encoder.encode(FontDataFormat::MappedFile));MUST(encoder.encode(*m_file_path));- MUST(encoder.encode(ttc_index()));+ MUST(encoder.encode(m_ttc_index));return;}@@ -358,7 +540,7 @@ void Typeface::encode_font_data_for_ipc(IPC::Encoder& encoder) const[&](Core::AnonymousBuffer const& anonymous_buffer) {MUST(encoder.encode(FontDataFormat::RawFontData));MUST(encoder.encode(anonymous_buffer));- MUST(encoder.encode(ttc_index()));+ MUST(encoder.encode(m_ttc_index));},[&](NonnullOwnPtr<Core::MappedFile> const&) {// NB: SharedMappedFile backing should have an associated m_system_font_identifier or m_file_path and@@ -395,10 +577,7 @@ ErrorOr<NonnullRefPtr<Gfx::Typeface const>> decode(Decoder& decoder)auto file_path = TRY(decoder.decode<String>());auto ttc_index = TRY(decoder.decode<u32>());- auto backing = make_ref_counted<Gfx::Typeface::FontDataBacking>(TRY(Core::MappedFile::map(file_path)));- auto bytes = backing->storage.get<NonnullOwnPtr<Core::MappedFile>>()->bytes();-- auto typeface = TRY(Gfx::TypefaceSkia::load_from_buffer(bytes, ttc_index, backing));+ auto typeface = TRY(Gfx::Typeface::try_load_from_mapped_file(TRY(Core::MappedFile::map(file_path)), ttc_index));typeface->set_file_path(move(file_path));return typeface;}diff --git a/Libraries/LibGfx/Font/Typeface.h b/Libraries/LibGfx/Font/Typeface.hindex 48d54e67151..89c8f49561d 100644--- a/Libraries/LibGfx/Font/Typeface.h+++ b/Libraries/LibGfx/Font/Typeface.h@@ -27,7 +27,6 @@#define POINTS_PER_INCH 72.0f#define DEFAULT_DPI 96-class SkTypeface;struct hb_blob_t;struct hb_face_t;struct hb_font_t;@@ -95,9 +94,14 @@ public:{}+ ReadonlyBytes bytes() const;+Storage storage;};+ // The face of font data that FreeType loads as an SFNT font and that HarfBuzz reads, so that both shaping and every+ // rasterizer have the face. The upper 16 bits of the index select a named instance of a variable face, from 1.+ static ErrorOr<NonnullRefPtr<Typeface>> try_load_from_font_data(NonnullRefPtr<FontDataBacking>, u32 ttc_index = 0);static ErrorOr<NonnullRefPtr<Typeface>> try_load_from_mapped_file(NonnullOwnPtr<Core::MappedFile>, u32 ttc_index = 0);static ErrorOr<NonnullRefPtr<Typeface>> try_load_from_anonymous_buffer(Core::AnonymousBuffer, u32 ttc_index = 0);static ErrorOr<NonnullRefPtr<Typeface>> try_load_from_temporary_memory(ReadonlyBytes bytes, u32 ttc_index = 0);@@ -112,10 +116,10 @@ public:u16 width() const;u8 slope() const;- ReadonlyBytes font_data() const LIFETIME_BOUND { return buffer(); }+ ReadonlyBytes font_data() const LIFETIME_BOUND { return m_font_data ? m_font_data->bytes() : ReadonlyBytes {}; }// What keeps the font data alive. Null for a typeface without font data of its own.RefPtr<FontDataBacking> font_data_backing() const { return m_font_data; }- u32 collection_index() const { return ttc_index(); }+ u32 collection_index() const { return m_ttc_index; }[[nodiscard]] NonnullRefPtr<Font> font(float point_size, FontVariationSettings const& variations = {}, Gfx::ShapeFeatures const& shape_features = {}) const;// A font that measures and shapes text like font() but draws nothing. Each call makes a new font.@@ -154,7 +158,6 @@ public:template<typename T>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.@@ -169,19 +172,17 @@ protected:PlatformFontName,};+ // A typeface without font data of its own.Typeface();- virtual ReadonlyBytes buffer() const = 0;- 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(); }-private:+ Typeface(NonnullRefPtr<FontDataBacking>, u32 ttc_index);+friend class Font;template<typename T>@@ -191,6 +192,7 @@ private:friend ErrorOr<T> IPC::decode(IPC::Decoder&);RefPtr<FontDataBacking> m_font_data;+ u32 m_ttc_index { 0 };Optional<SystemFontIdentifier> m_system_font_identifier;Optional<String> m_file_path;diff --git a/Libraries/LibGfx/Font/TypefaceCoreText.cpp b/Libraries/LibGfx/Font/TypefaceCoreText.cppindex befe667b957..d451f8f963b 100644--- a/Libraries/LibGfx/Font/TypefaceCoreText.cpp+++ b/Libraries/LibGfx/Font/TypefaceCoreText.cpp@@ -244,6 +244,24 @@ static CTFontRef create_core_text_named_instance(CTFontRef font, hb_face_t* facereturn create_core_text_font_with_variations(font, 0, variation);}+// Font data for CoreText that keeps its backing for as long as CoreText uses the data.+static CFDataRef create_core_text_data(NonnullRefPtr<Typeface::FontDataBacking> backing, ReadonlyBytes bytes)+{+ CFAllocatorContext allocator_context {};+ allocator_context.info = backing.ptr();+ allocator_context.retain = [](void const* info) -> void const* {+ static_cast<Typeface::FontDataBacking const*>(info)->ref();+ return info;+ };+ allocator_context.release = [](void const* info) { static_cast<Typeface::FontDataBacking const*>(info)->unref(); };+ allocator_context.deallocate = [](void*, void*) { };+ auto allocator = CFAllocatorCreate(kCFAllocatorDefault, &allocator_context);+ if (!allocator)+ return nullptr;+ ScopeGuard release_allocator = [&] { CFRelease(allocator); };+ return CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, bytes.data(), static_cast<CFIndex>(bytes.size()), allocator);+}+CTFontRef create_core_text_font_from_data(NonnullRefPtr<Typeface::FontDataBacking> backing, ReadonlyBytes bytes, u32 ttc_index){if (bytes.size() > static_cast<size_t>(NumericLimits<CFIndex>::max()) || bytes.size() > NumericLimits<unsigned>::max())@@ -262,19 +280,7 @@ CTFontRef create_core_text_font_from_data(NonnullRefPtr<Typeface::FontDataBackinreturn nullptr;ScopeGuard release_name = [&] { CFRelease(postscript_name); };- CFAllocatorContext allocator_context {};- allocator_context.info = backing.ptr();- allocator_context.retain = [](void const* info) -> void const* {- static_cast<Typeface::FontDataBacking const*>(info)->ref();- return info;- };- allocator_context.release = [](void const* info) { static_cast<Typeface::FontDataBacking const*>(info)->unref(); };- allocator_context.deallocate = [](void*, void*) { };- auto allocator = CFAllocatorCreate(kCFAllocatorDefault, &allocator_context);- if (!allocator)- return nullptr;- ScopeGuard release_allocator = [&] { CFRelease(allocator); };- auto data = CFDataCreateWithBytesNoCopy(kCFAllocatorDefault, bytes.data(), static_cast<CFIndex>(bytes.size()), allocator);+ auto data = create_core_text_data(move(backing), bytes);if (!data)return nullptr;ScopeGuard release_data = [&] { CFRelease(data); };@@ -313,6 +319,35 @@ CTFontRef create_core_text_font_from_data(NonnullRefPtr<Typeface::FontDataBackinreturn font;}+bool core_text_accepts_font_data(NonnullRefPtr<Typeface::FontDataBacking> backing, u32 ttc_index)+{+ auto bytes = backing->bytes();+ if (ttc_index != 0) {+ auto font = create_core_text_font_from_data(move(backing), bytes, ttc_index);+ if (!font)+ return false;+ CFRelease(font);+ return true;+ }++ // The compositor makes the CoreText font of the first face from a font descriptor of all of the data.+ if (bytes.size() > static_cast<size_t>(NumericLimits<CFIndex>::max()))+ return false;+ auto data = create_core_text_data(move(backing), bytes);+ if (!data)+ return false;+ ScopeGuard release_data = [&] { CFRelease(data); };+ auto descriptor = CTFontManagerCreateFontDescriptorFromData(data);+ if (!descriptor)+ return false;+ ScopeGuard release_descriptor = [&] { CFRelease(descriptor); };+ auto font = CTFontCreateWithFontDescriptor(descriptor, 0, nullptr);+ if (!font)+ return false;+ CFRelease(font);+ return true;+}+TypefaceCoreText::TypefaceCoreText(CTFontRef core_text_font, CGFontRef graphics_font, Identity identity): m_core_text_font(core_text_font), m_graphics_font(graphics_font)diff --git a/Libraries/LibGfx/Font/TypefaceCoreText.h b/Libraries/LibGfx/Font/TypefaceCoreText.hindex 607f60ffce9..4776627df1f 100644--- a/Libraries/LibGfx/Font/TypefaceCoreText.h+++ b/Libraries/LibGfx/Font/TypefaceCoreText.h@@ -40,8 +40,6 @@ private:TypefaceCoreText(CTFontRef, CGFontRef, Identity);- 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;@@ -62,6 +60,9 @@ inline bool Typeface::fast_is<TypefaceCoreText>() const { return is_core_text();// not load the face.CTFontRef create_core_text_font_from_data(NonnullRefPtr<Typeface::FontDataBacking>, ReadonlyBytes, u32 ttc_index);+// 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);+// 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/Libraries/LibGfx/Font/TypefaceSkia.cpp b/Libraries/LibGfx/Font/TypefaceSkia.cppdeleted file mode 100644index d3cc2f3f94c..00000000000--- a/Libraries/LibGfx/Font/TypefaceSkia.cpp+++ /dev/null@@ -1,140 +0,0 @@-/*- * Copyright (c) 2024, Aliaksandr Kalenik <[email protected]>- * Copyright (c) 2026, Tim Ledbetter <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#include <AK/Atomic.h>-#include <AK/ByteString.h>-#include <AK/Diagnostics.h>-#include <AK/LsanSuppressions.h>-#include <AK/Math.h>-#include <AK/NeverDestroyed.h>-#include <AK/NumericLimits.h>-#include <AK/ScopeGuard.h>-#include <AK/Vector.h>-#include <LibGfx/Font/FontDatabase.h>-#include <LibGfx/Font/TypefaceSkia.h>-#include <LibIPC/Decoder.h>-#include <LibIPC/Encoder.h>--#include <core/SkData.h>-#include <core/SkFontArguments.h>-#include <core/SkFontMgr.h>-#include <core/SkStream.h>-#include <core/SkString.h>-#include <core/SkTypeface.h>-#include <harfbuzz/hb-ot.h>-#include <harfbuzz/hb.h>-#include <ports/SkFontScanner_FreeType.h>-#if defined(AK_OS_ANDROID)-# include <ports/SkFontMgr_android.h>-#elif defined(AK_OS_WINDOWS)-# include <ports/SkFontMgr_empty.h>-# include <ports/SkTypeface_win.h>-# include <src/ports/SkTypeface_win_dw.h>-#else-# include <ports/SkFontMgr_fontconfig.h>-#endif--#ifdef AK_OS_MACOS-# include <CoreText/CoreText.h>-# include <LibGfx/Font/TypefaceCoreText.h>-# include <ports/SkFontMgr_mac_ct.h>-# include <ports/SkTypeface_mac.h>-# include <src/ports/SkTypeface_mac_ct.h>-#endif--namespace Gfx {--struct TypefaceSkia::Impl {- AK_ALLOC_WITH_KMALLOC;-- explicit Impl(sk_sp<SkTypeface> skia_typeface)- : skia_typeface(move(skia_typeface))- {- }-- sk_sp<SkTypeface> skia_typeface;-};--// Text is shaped with HarfBuzz, so font data is of no use if HarfBuzz cannot read the face in it, even if Skia can-// draw it. FreeType also reads font formats such as WOFF, which have to be decoded first.-static bool harfbuzz_can_read_face(ReadonlyBytes buffer, u32 ttc_index)-{- if (buffer.size() > NumericLimits<unsigned>::max())- return false;- auto* blob = hb_blob_create(reinterpret_cast<char const*>(buffer.data()), buffer.size(), HB_MEMORY_MODE_READONLY, nullptr, nullptr);- ScopeGuard destroy_blob = [&] { hb_blob_destroy(blob); };- // The upper 16 bits of the index select a named instance of a variable face.- return (ttc_index & 0xFFFF) < hb_face_count(blob);-}--ErrorOr<NonnullRefPtr<TypefaceSkia>> TypefaceSkia::load_from_buffer(AK::ReadonlyBytes buffer, u32 ttc_index, NonnullRefPtr<FontDataBacking> backing)-{- if (!harfbuzz_can_read_face(buffer, ttc_index))- return Error::from_string_literal("Font data has no face that HarfBuzz can read");-- // NB: Skia can retain the typeface in text blobs and glyph caches after our Typeface is destroyed.- // Keep the backing alive through SkData.- backing->ref();-- sk_sp<SkData> data = SkData::MakeWithProc(buffer.data(), buffer.size(), [](void const*, void* context) { static_cast<FontDataBacking*>(context)->unref(); }, backing.ptr());-- sk_sp<SkTypeface> skia_typeface;-#ifdef AK_OS_MACOS- // NB: Skia's CoreText stream loader only supports collection index zero.- if (ttc_index != 0 && !FontDatabase::the().force_freetype_rasterization()) {- auto ct_font = create_core_text_font_from_data(backing, buffer, ttc_index);- if (!ct_font)- return Error::from_string_literal("CoreText did not load the collection face");- ScopeGuard release_font = [&] { CFRelease(ct_font); };- skia_typeface = SkMakeTypefaceFromCTFont(ct_font);- } else-#endif- {- // https://learn.microsoft.com/en-us/typography/opentype/spec/otff#ttc-header- // TrueType Collection files bundle multiple fonts (often different weights of the same- // family). We use SkFontArguments to specify which font to load from the collection.- SkFontArguments font_args;- font_args.setCollectionIndex(static_cast<int>(ttc_index));-- auto stream = std::make_unique<SkMemoryStream>(data);- // Loading supplied font data does not need a manager that discovers system fonts.-#if defined(AK_OS_WINDOWS)- if (!FontDatabase::the().force_freetype_rasterization()) {- skia_typeface = DWriteFontTypeface::MakeFromStream(std::move(stream), font_args);- } else-#elif defined(AK_OS_MACOS)- if (!FontDatabase::the().force_freetype_rasterization()) {- skia_typeface = SkTypeface_Mac::MakeFromStream(std::move(stream), font_args);- } else-#endif- {- skia_typeface = SkFontScanner_Make_FreeType()->MakeFromStream(std::move(stream), font_args);- }- }-- if (!skia_typeface) {- return Error::from_string_literal("Failed to load typeface from buffer");- }-- auto typeface = adopt_ref(*new TypefaceSkia { make<TypefaceSkia::Impl>(skia_typeface), buffer, ttc_index });- typeface->set_font_data(move(backing));- return typeface;-}--SkTypeface const* TypefaceSkia::sk_typeface() const-{- return impl().skia_typeface.get();-}--TypefaceSkia::TypefaceSkia(NonnullOwnPtr<Impl> impl, ReadonlyBytes buffer, u32 ttc_index)- : m_impl(move(impl))- , m_buffer(buffer)- , m_ttc_index(ttc_index)-{-}--}diff --git a/Libraries/LibGfx/Font/TypefaceSkia.h b/Libraries/LibGfx/Font/TypefaceSkia.hdeleted file mode 100644index 0d610b29d17..00000000000--- a/Libraries/LibGfx/Font/TypefaceSkia.h+++ /dev/null@@ -1,40 +0,0 @@-/*- * Copyright (c) 2024, Aliaksandr Kalenik <[email protected]>- *- * SPDX-License-Identifier: BSD-2-Clause- */--#pragma once--#include <LibGfx/Font/Typeface.h>--namespace Gfx {--class TypefaceSkia : public Gfx::Typeface {- AK_MAKE_NONCOPYABLE(TypefaceSkia);--public:- static ErrorOr<NonnullRefPtr<TypefaceSkia>> load_from_buffer(ReadonlyBytes, u32 ttc_index, NonnullRefPtr<FontDataBacking>);-- virtual ReadonlyBytes buffer() const LIFETIME_BOUND override { return m_buffer; }- virtual u32 ttc_index() const override { return m_ttc_index; }-- SkTypeface const* sk_typeface() const;--private:- struct Impl;- Impl& impl() const { return *m_impl; }- NonnullOwnPtr<Impl> m_impl;-- TypefaceSkia(NonnullOwnPtr<Impl>, ReadonlyBytes, u32 ttc_index = 0);-- virtual bool is_skia() const override { return true; }-- ReadonlyBytes m_buffer;- u32 m_ttc_index { 0 };-};--template<>-inline bool Typeface::fast_is<TypefaceSkia>() const { return is_skia(); }--}diff --git a/Libraries/LibWebView/FontService.cpp b/Libraries/LibWebView/FontService.cppindex 6fed5c7ed32..d089e3c1c9c 100644--- a/Libraries/LibWebView/FontService.cpp+++ b/Libraries/LibWebView/FontService.cpp@@ -13,6 +13,10 @@#include <LibGfx/Font/PathFontProvider.h>#include <LibWebView/FontService.h>+#ifdef AK_OS_MACOS+# include <LibGfx/Font/TypefaceCoreText.h>+#endif+#include <fcntl.h>#if !defined(AK_OS_WINDOWS)@@ -90,6 +94,12 @@ ErrorOr<void> FontService::build_catalog()// A face without a family name cannot be matched.if (typeface->family().is_empty())return;+#ifdef AK_OS_MACOS+ // The compositor draws the faces of installed fonts with CoreText, which does not load every face that+ // HarfBuzz reads, such as one with hvgl outlines.+ if (!Gfx::core_text_accepts_font_data(*typeface->font_data_backing(), ttc_index))+ return;+#endifauto face_id = next_face_id++;auto names = typeface->local_font_names();if (names.is_error()) {diff --git a/Libraries/LibWebView/HelperProcess.cpp b/Libraries/LibWebView/HelperProcess.cppindex 44502de8fd6..c8ac20ba801 100644--- a/Libraries/LibWebView/HelperProcess.cpp+++ b/Libraries/LibWebView/HelperProcess.cpp@@ -330,8 +330,6 @@ ErrorOr<NonnullRefPtr<WebView::WebContentClient>> launch_web_content_process(IsParguments.append("--expose-experimental-interfaces"sv);if (web_content_options.expose_internals_object == WebView::ExposeInternalsObject::Yes)arguments.append("--expose-internals-object"sv);- if (web_content_options.force_fontconfig == WebView::ForceFontconfig::Yes)- arguments.append("--force-fontconfig"sv);if (web_content_options.collect_garbage_on_every_allocation == WebView::CollectGarbageOnEveryAllocation::Yes)arguments.append("--collect-garbage-on-every-allocation"sv);if (web_content_options.paint_viewport_scrollbars == PaintViewportScrollbars::No)diff --git a/Services/Compositor/FontSkia.cpp b/Services/Compositor/FontSkia.cppindex 189d5d773e8..30f1fa76f93 100644--- a/Services/Compositor/FontSkia.cpp+++ b/Services/Compositor/FontSkia.cpp@@ -10,7 +10,6 @@#include <AK/TypeCasts.h>#include <Compositor/FontSkia.h>#include <LibGfx/Font/Font.h>-#include <LibGfx/Font/FontDatabase.h>#include <LibGfx/Font/RasterizerData.h>#include <core/SkData.h>@@ -38,6 +37,8 @@namespace Compositor {+static FontRasterizer s_font_rasterizer { FontRasterizer::Platform };+#if defined(USE_FONTCONFIG)static bool s_force_hinting_for_testing { false };@@ -85,7 +86,7 @@ struct TypefaceData final : public Gfx::RasterizerData {// 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){- if (Gfx::FontDatabase::the().force_freetype_rasterization())+ if (s_font_rasterizer == FontRasterizer::FreeType)return nullptr;#if defined(AK_OS_MACOS)// The CoreText typeface loader of Skia loads only the first face of font data.@@ -198,6 +199,11 @@ Gfx::FontHintingOptions hinting_options(Gfx::Font const& font, float scale)}+void set_font_rasterizer(FontRasterizer rasterizer)+{+ s_font_rasterizer = rasterizer;+}+void force_font_hinting_for_testing(){#if defined(USE_FONTCONFIG)diff --git a/Services/Compositor/FontSkia.h b/Services/Compositor/FontSkia.hindex 5cf501c66fd..63926aca10f 100644--- a/Services/Compositor/FontSkia.h+++ b/Services/Compositor/FontSkia.h@@ -13,6 +13,15 @@ class SkFont;namespace Compositor {+enum class FontRasterizer {+ // The font manager of the platform: CoreText on macOS, DirectWrite on Windows, and FreeType elsewhere.+ Platform,+ FreeType,+};++// Call this before any text is drawn.+void set_font_rasterizer(FontRasterizer);+// Makes every font use normal hinting, so that text looks the same on every machine.void force_font_hinting_for_testing();diff --git a/Services/Compositor/main.cpp b/Services/Compositor/main.cppindex adb2064cdba..4619a7a5fda 100644--- a/Services/Compositor/main.cpp+++ b/Services/Compositor/main.cpp@@ -16,7 +16,6 @@#include <LibCore/ResourceImplementationFile.h>#include <LibCore/StatsLog.h>#include <LibGfx/Font/Font.h>-#include <LibGfx/Font/FontDatabase.h>#include <LibGfx/GpuDeviceIdentity.h>#include <LibGfx/SkiaBackendContext.h>#include <LibIPC/SingleServer.h>@@ -73,7 +72,7 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)Core::ResourceImplementation::install(make<Core::ResourceImplementationFile>(TRY(String::from_utf8(resource_root))));if (force_fontconfig)- Gfx::FontDatabase::the().set_force_freetype_rasterization(true);+ Compositor::set_font_rasterizer(Compositor::FontRasterizer::FreeType);#if defined(AK_OS_LINUX)// NB: The GPU driver starts threads, and Landlock does not confine a thread that already runs. Seccomp covers everydiff --git a/Services/WebContent/main.cpp b/Services/WebContent/main.cppindex ce8e0bb79cc..6e1052f15f9 100644--- a/Services/WebContent/main.cpp+++ b/Services/WebContent/main.cpp@@ -20,7 +20,6 @@#include <LibCore/TimeZone.h>#include <LibCrypto/OpenSSL.h>#include <LibCrypto/OpenSSLForward.h>-#include <LibGfx/Font/FontDatabase.h>#include <LibGfx/Font/SystemFallbackFonts.h>#include <LibIPC/ConnectionFromClient.h>#include <LibIPC/TransportHandle.h>@@ -182,7 +181,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)bool wait_for_debugger = false;bool log_all_js_exceptions = false;bool enable_http_memory_cache = false;- bool force_fontconfig = false;bool collect_garbage_on_every_allocation = false;bool is_headless = false;bool disable_scrollbar_painting = false;@@ -203,7 +201,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)args_parser.add_option(mach_server_name, "Mach server name", "mach-server-name", 0, "mach_server_name");args_parser.add_option(log_all_js_exceptions, "Log all JavaScript exceptions", "log-all-js-exceptions");args_parser.add_option(enable_http_memory_cache, "Enable HTTP cache", "enable-http-memory-cache");- args_parser.add_option(force_fontconfig, "Force using fontconfig for font loading", "force-fontconfig");args_parser.add_option(collect_garbage_on_every_allocation, "Collect garbage after every JS heap allocation", "collect-garbage-on-every-allocation");args_parser.add_option(disable_scrollbar_painting, "Don't paint horizontal or vertical viewport scrollbars", "disable-scrollbar-painting");args_parser.add_option(disable_sandbox, "Disable process sandboxing", "disable-sandbox");@@ -252,9 +249,6 @@ ErrorOr<int> ladybird_main(Main::Arguments arguments)if (file_origins_are_tuple_origins)URL::set_file_scheme_urls_have_tuple_origins();- if (force_fontconfig)- Gfx::FontDatabase::the().set_force_freetype_rasterization(true);-WebContent::PageClient::set_is_headless(is_headless);if (enable_http_memory_cache)diff --git a/Tests/LibGfx/CMakeLists.txt b/Tests/LibGfx/CMakeLists.txtindex 0ca06e0d288..0d4bf6f10e3 100644--- a/Tests/LibGfx/CMakeLists.txt+++ b/Tests/LibGfx/CMakeLists.txt@@ -21,7 +21,7 @@ endforeach()target_link_libraries(BenchmarkJPEGLoader PRIVATE LibImageDecoders)target_link_libraries(TestImageDecoder PRIVATE LibImageDecoders)target_link_libraries(TestImageWriter PRIVATE LibImageDecoders)-target_link_libraries(TestFont PRIVATE harfbuzz LibIPC LibThreading skia)+target_link_libraries(TestFont PRIVATE harfbuzz LibIPC LibThreading)target_link_libraries(TestSharedFontProvider PRIVATE LibIPC)target_compile_definitions(TestSharedFontProvider PRIVATE LADYBIRD_SOURCE_DIR="${LADYBIRD_SOURCE_DIR}")diff --git a/Tests/LibGfx/TestFont.cpp b/Tests/LibGfx/TestFont.cppindex 9c014d570d8..6621728f842 100644--- a/Tests/LibGfx/TestFont.cpp+++ b/Tests/LibGfx/TestFont.cpp@@ -13,7 +13,6 @@#include <LibGfx/Font/PathFontProvider.h>#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@@ -23,8 +22,6 @@#include <LibIPC/Encoder.h>#include <LibTest/TestCase.h>#include <LibThreading/Thread.h>-#include <core/SkStream.h>-#include <core/SkTypeface.h>#include <harfbuzz/hb.h>#define TEST_INPUT(x) ("test-inputs/" x)@@ -82,56 +79,6 @@ TEST_CASE(text_font_is_not_emoji_font)EXPECT(!font_is_emoji(TEST_INPUT("fonts/text.ttf"sv)));}-static void check_skia_font_data_lifetime(Function<NonnullRefPtr<Gfx::Typeface>()> load_typeface)-{- sk_sp<SkTypeface const> skia_typeface;- ByteBuffer expected_table;- ByteBuffer expected_font_data;- constexpr auto cmap_tag = SkSetFourByteTag('c', 'm', 'a', 'p');- {- auto typeface = load_typeface();- skia_typeface = sk_ref_sp(static_cast<Gfx::TypefaceSkia const&>(*typeface).sk_typeface());- int collection_index = 0;- auto stream = skia_typeface->openStream(&collection_index);- EXPECT(stream);- if (stream) {- EXPECT_EQ(stream->getMemoryBase(), typeface->font_data().data());- EXPECT_EQ(stream->getLength(), typeface->font_data().size());- }- expected_font_data = MUST(ByteBuffer::copy(typeface->font_data()));- expected_table = MUST(ByteBuffer::create_uninitialized(skia_typeface->getTableSize(cmap_tag)));- EXPECT(!expected_table.is_empty());- EXPECT_EQ(skia_typeface->getTableData(cmap_tag, 0, expected_table.size(), expected_table.data()), expected_table.size());- }- auto actual_table = MUST(ByteBuffer::create_uninitialized(expected_table.size()));- EXPECT_EQ(skia_typeface->getTableData(cmap_tag, 0, actual_table.size(), actual_table.data()), actual_table.size());- EXPECT_EQ(actual_table, expected_table);- int collection_index = 0;- auto stream = skia_typeface->openStream(&collection_index);- EXPECT(stream);- if (stream) {- auto actual_font_data = MUST(ByteBuffer::create_uninitialized(expected_font_data.size()));- EXPECT_EQ(stream->read(actual_font_data.data(), actual_font_data.size()), actual_font_data.size());- EXPECT_EQ(actual_font_data, expected_font_data);- }-}--TEST_CASE(skia_typeface_retains_font_data_after_ladybird_typeface_is_destroyed)-{- check_skia_font_data_lifetime([] {- auto file = MUST(Core::MappedFile::map(TEST_INPUT("fonts/text.ttf"sv)));- return MUST(Gfx::Typeface::try_load_from_temporary_memory(file->bytes()));- });-}--TEST_CASE(skia_typeface_retains_mapped_font_data_after_ladybird_typeface_is_destroyed)-{- check_skia_font_data_lifetime([] {- auto file = MUST(Core::MappedFile::map(TEST_INPUT("fonts/text.ttf"sv)));- return MUST(Gfx::Typeface::try_load_from_mapped_file(move(file)));- });-}-static NonnullRefPtr<Gfx::Font> load_text_font(float point_size){auto file = MUST(Core::MappedFile::map(TEST_INPUT("fonts/text.ttf"sv)));@@ -547,7 +494,7 @@ TEST_CASE(font_collection_preserves_each_face_style){auto file = MUST(Core::MappedFile::map(TEST_INPUT("fonts/styles.ttc"sv)));for (u32 index = 0; index < 3; ++index) {- auto result = Gfx::TypefaceSkia::try_load_from_temporary_memory(file->bytes(), index);+ auto result = Gfx::Typeface::try_load_from_temporary_memory(file->bytes(), index);EXPECT(!result.is_error());if (result.is_error())continue;@@ -566,31 +513,7 @@ TEST_CASE(font_collection_preserves_each_face_style)EXPECT_EQ(font->slope(), expected_slope);EXPECT_NE(font->glyph_id_for_code_point('a'), 0u);}- EXPECT(Gfx::TypefaceSkia::try_load_from_temporary_memory(file->bytes(), 3).is_error());-}--TEST_CASE(font_collection_retains_shared_backing_for_skia)-{- auto mapping = MUST(Core::MappedFile::map(TEST_INPUT("fonts/styles.ttc"sv)));- auto backing = make_ref_counted<Gfx::Typeface::FontDataBacking>(move(mapping));- sk_sp<SkTypeface const> skia_typeface;- ByteBuffer expected_table;- constexpr auto cmap_tag = SkSetFourByteTag('c', 'm', 'a', 'p');- {- auto const& mapping = backing->storage.get<NonnullOwnPtr<Core::MappedFile>>();-- auto typeface = MUST(Gfx::TypefaceSkia::load_from_buffer(mapping->bytes(), 1, backing));- EXPECT_EQ(typeface->buffer().data(), mapping->bytes().data());- skia_typeface = sk_ref_sp(typeface->sk_typeface());- expected_table = MUST(ByteBuffer::create_uninitialized(skia_typeface->getTableSize(cmap_tag)));- EXPECT(!expected_table.is_empty());- EXPECT_EQ(skia_typeface->getTableData(cmap_tag, 0, expected_table.size(), expected_table.data()), expected_table.size());- }- // Skia still uses the original mapping after the Ladybird typeface has gone away.- EXPECT(backing->ref_count() > 1);- auto actual_table = MUST(ByteBuffer::create_uninitialized(expected_table.size()));- EXPECT_EQ(skia_typeface->getTableData(cmap_tag, 0, actual_table.size(), actual_table.data()), actual_table.size());- EXPECT_EQ(actual_table, expected_table);+ EXPECT(Gfx::Typeface::try_load_from_temporary_memory(file->bytes(), 3).is_error());}// The answer depends on the installed font set alone, so every thread must reach the same one and