1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 05:17:35 +00:00

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).
This commit is contained in:
Nico Weber 2024-02-20 11:59:46 -05:00 committed by Andreas Kling
parent 0dee94ef40
commit cade76d240
3 changed files with 8 additions and 3 deletions

View file

@ -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())

View file

@ -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 };
};

View file

@ -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: