Browse Source

LibPDF+LibGfx: Do not try to read "OS/2" table for PDFs

It is sometimes truncated in fonts embedded in PDFs, and the data
is not needed to render PDFs. 2 of my 1000 test PDFs used to
complain "Could not load OS2 v1: Not enough data" and 1
"Could not load OS2 v2: Not enough data" before.

Increases number of PDFs that render without diagnostics from
764 to 765 (and decreases the number of distinct error messages
from 27 to 25).
Nico Weber 1 năm trước cách đây
mục cha
commit
cade76d240

+ 4 - 2
Userland/Libraries/LibGfx/Font/OpenType/Font.cpp

@@ -290,8 +290,10 @@ ErrorOr<NonnullRefPtr<Font>> Font::try_load_from_offset(ReadonlyBytes buffer, u3
     }
 
     Optional<OS2> os2;
-    if (opt_os2_slice.has_value())
-        os2 = TRY(OS2::from_slice(opt_os2_slice.value()));
+    if (!(options.skip_tables & Options::SkipTables::OS2)) {
+        if (opt_os2_slice.has_value())
+            os2 = TRY(OS2::from_slice(opt_os2_slice.value()));
+    }
 
     Optional<Kern> kern {};
     if (opt_kern_slice.has_value())

+ 3 - 0
Userland/Libraries/LibGfx/Font/OpenType/Font.h

@@ -37,6 +37,9 @@ struct FontOptions {
 
         // If set, do not try to read the 'hmtx' table. This will make glyph_metrics() return 0 for everyting and is_fixed_width() return true.
         Hmtx = 1 << 1,
+
+        // If set, do not try to read the 'OS/2' table. metrics(), resolve_ascender_and_descender(), weight(), width(), and slope() will return different values.
+        OS2 = 1 << 2,
     };
     u32 skip_tables { 0 };
 };

+ 1 - 1
Userland/Libraries/LibPDF/Fonts/PDFFont.h

@@ -19,7 +19,7 @@ class Renderer;
 
 // PDF files don't need most of the data in OpenType fonts, and even contain invalid data for
 // these tables in some cases. Skip reading these tables.
-constexpr u32 pdf_skipped_opentype_tables = OpenType::FontOptions::SkipTables::Name | OpenType::FontOptions::SkipTables::Hmtx;
+constexpr u32 pdf_skipped_opentype_tables = OpenType::FontOptions::SkipTables::Name | OpenType::FontOptions::SkipTables::Hmtx | OpenType::FontOptions::SkipTables::OS2;
 
 class PDFFont : public RefCounted<PDFFont> {
 public: