Truly independent web browser
LibGfx: Load only font data whose face HarfBuzz can read
Aliaksandr Kalenikd0c29f711319
diff --git a/Libraries/LibGfx/Font/TypefaceSkia.cpp b/Libraries/LibGfx/Font/TypefaceSkia.cppindex c6042929c83..ea7aa7e2537 100644--- a/Libraries/LibGfx/Font/TypefaceSkia.cpp+++ b/Libraries/LibGfx/Font/TypefaceSkia.cpp@@ -195,8 +195,23 @@ ErrorOr<RefPtr<TypefaceSkia>> TypefaceSkia::typeface_from_skia_typeface(sk_sp<Skttc_index });}+// 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();@@ -210,12 +225,8 @@ ErrorOr<NonnullRefPtr<TypefaceSkia>> TypefaceSkia::load_from_buffer(AK::Readonlyif (ttc_index != 0 && !FontDatabase::the().force_freetype_rasterization()) {if (buffer.size() > static_cast<size_t>(NumericLimits<CFIndex>::max()))return Error::from_string_literal("Font data is too large for CoreText");- if (buffer.size() > NumericLimits<unsigned>::max())- return Error::from_string_literal("Font data is too large for HarfBuzz");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); };- if (ttc_index >= hb_face_count(blob))- return Error::from_string_literal("Font collection index is out of range");auto* face = hb_face_create(blob, ttc_index);ScopeGuard destroy_face = [&] { hb_face_destroy(face); };unsigned entry_count = 0;diff --git a/Tests/LibWeb/TestConfig.ini b/Tests/LibWeb/TestConfig.iniindex eab0d9f3ed2..e79c5d36206 100644--- a/Tests/LibWeb/TestConfig.ini+++ b/Tests/LibWeb/TestConfig.ini@@ -94,6 +94,7 @@ Text/input/wpt-import/media-source/SourceBuffer-appendWindowEnd-rounding.htmlText/input/HTML/HTMLMediaElement-load-overrides-preload-none.htmlText/input/HTML/HTMLMediaElement-preload-none-does-not-fetch.htmlText/input/css/FontFace-arraybuffer-matching.html+Text/input/css/FontFace-arraybuffer-woff-glyphs.htmlText/input/wpt-import/mediacapture-streams/idlharness.https.window.htmlText/input/wpt-import/media-playback-quality/idlharness.window.htmlText/input/css/font-face-load-dedups-same-url.htmldiff --git a/Tests/LibWeb/Text/expected/css/FontFace-arraybuffer-woff-glyphs.txt b/Tests/LibWeb/Text/expected/css/FontFace-arraybuffer-woff-glyphs.txtnew file mode 100644index 00000000000..8feed1223ab--- /dev/null+++ b/Tests/LibWeb/Text/expected/css/FontFace-arraybuffer-woff-glyphs.txt@@ -0,0 +1 @@+width: 80diff --git a/Tests/LibWeb/Text/input/css/FontFace-arraybuffer-woff-glyphs.html b/Tests/LibWeb/Text/input/css/FontFace-arraybuffer-woff-glyphs.htmlnew file mode 100644index 00000000000..fd32be27303--- /dev/null+++ b/Tests/LibWeb/Text/input/css/FontFace-arraybuffer-woff-glyphs.html@@ -0,0 +1,18 @@+<!DOCTYPE html>+<script src="../include.js"></script>+<script>+ promiseTest(async () => {+ const fontData = await fetch("../../../Assets/HashSans.woff").then(response => response.arrayBuffer());+ const face = new FontFace("WoffFromArrayBuffer", fontData);+ document.fonts.add(face);+ await face.loaded;++ const element = document.createElement("span");+ element.style.font = "20px WoffFromArrayBuffer";+ element.textContent = "WWWWW";+ document.body.appendChild(element);++ // Every glyph of HashSans has an advance of 0.8em.+ println(`width: ${element.getBoundingClientRect().width}`);+ });+</script>