From cade76d2409c58a49eda2cdce46ce4c8dc6a080a Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 20 Feb 2024 11:59:46 -0500 Subject: [PATCH] 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). --- Userland/Libraries/LibGfx/Font/OpenType/Font.cpp | 6 ++++-- Userland/Libraries/LibGfx/Font/OpenType/Font.h | 3 +++ Userland/Libraries/LibPDF/Fonts/PDFFont.h | 2 +- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp index 51c231b5f2..b4aac6ae17 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp +++ b/Userland/Libraries/LibGfx/Font/OpenType/Font.cpp @@ -290,8 +290,10 @@ ErrorOr> Font::try_load_from_offset(ReadonlyBytes buffer, u3 } Optional 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 {}; if (opt_kern_slice.has_value()) diff --git a/Userland/Libraries/LibGfx/Font/OpenType/Font.h b/Userland/Libraries/LibGfx/Font/OpenType/Font.h index 823d01b342..f792e78dc7 100644 --- a/Userland/Libraries/LibGfx/Font/OpenType/Font.h +++ b/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 }; }; diff --git a/Userland/Libraries/LibPDF/Fonts/PDFFont.h b/Userland/Libraries/LibPDF/Fonts/PDFFont.h index 8688bfdb18..727c0c2778 100644 --- a/Userland/Libraries/LibPDF/Fonts/PDFFont.h +++ b/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 { public: