From be8b5b794fd8856cfc8cbee21b2894d015cd1953 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Wed, 7 Feb 2024 20:22:50 -0500 Subject: [PATCH] LibGfx/OpenType: Error on fonts without a supported subtable This would've saved me some debugging on #23103. We now return an error instead of a font that draws squares for all characters. That seems preferable since it makes these cases easy to find. This fires for three files in my 1000-file PDF test set, so it's not exceedingly common (...but I wasn't aware that three files were rendering boxes for this reason, and now I am and can just make them work in the future). --- Userland/Libraries/LibGfx/Font/OpenType/Font.cpp | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp index 039491cb2c..5821518a59 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp @@ -223,6 +223,7 @@ ErrorOr> Font::try_load_from_offset(ReadonlyBytes buffer, u3 // Select cmap table. FIXME: Do this better. Right now, just looks for platform "Windows" // and corresponding encoding "Unicode full repertoire", or failing that, "Unicode BMP" + Optional active_cmap_index; for (u32 i = 0; i < cmap.num_subtables(); i++) { auto opt_subtable = cmap.subtable(i); if (!opt_subtable.has_value()) { @@ -239,28 +240,31 @@ ErrorOr> Font::try_load_from_offset(ReadonlyBytes buffer, u3 if (platform.value() == Cmap::Subtable::Platform::Unicode) { if (subtable.encoding_id() == (u16)Cmap::Subtable::UnicodeEncoding::Unicode2_0_FullRepertoire) { // "Encoding ID 3 should be used in conjunction with 'cmap' subtable formats 4 or 6." - cmap.set_active_index(i); + active_cmap_index = i; break; } if (subtable.encoding_id() == (u16)Cmap::Subtable::UnicodeEncoding::Unicode2_0_BMP_Only) { // "Encoding ID 4 should be used in conjunction with subtable formats 10 or 12." - cmap.set_active_index(i); + active_cmap_index = i; break; } } else if (platform.value() == Cmap::Subtable::Platform::Windows) { if (subtable.encoding_id() == (u16)Cmap::Subtable::WindowsEncoding::UnicodeFullRepertoire) { - cmap.set_active_index(i); + active_cmap_index = i; break; } if (subtable.encoding_id() == (u16)Cmap::Subtable::WindowsEncoding::UnicodeBMP) { - cmap.set_active_index(i); + active_cmap_index = i; break; } } else if (platform.value() == Cmap::Subtable::Platform::Macintosh) { - cmap.set_active_index(i); + active_cmap_index = i; // Intentionally no `break` so that Windows (value 3) wins over Macintosh (value 1). } } + if (!active_cmap_index.has_value()) + return Error::from_string_literal("No suitable cmap subtable found"); + cmap.set_active_index(active_cmap_index.value()); return adopt_ref(*new Font( move(head),