1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 18:37:34 +00:00

LibPDF+LibGfx: Do not try to read "name" table for PDFs

It is often missing in fonts embedded in PDFs. 75 of my 1000 test
files complained "Font is missing Name" when trying to read fonts
before.

Increases number of PDFs that render without diagnostics from
682 to 743.
This commit is contained in:
Nico Weber 2024-02-20 11:31:33 -05:00 committed by Andreas Kling
parent 41eca52b50
commit 5efe80af7f
5 changed files with 31 additions and 11 deletions

View file

@ -8,6 +8,7 @@
#include <AK/HashMap.h>
#include <AK/Tuple.h>
#include <LibGfx/Font/OpenType/Font.h>
#include <LibGfx/Forward.h>
#include <LibPDF/Document.h>
#include <LibPDF/Encoding.h>
@ -16,6 +17,10 @@ namespace PDF {
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;
class PDFFont : public RefCounted<PDFFont> {
public:
static PDFErrorOr<NonnullRefPtr<PDFFont>> create(Document*, NonnullRefPtr<DictObject> const&, float font_size);

View file

@ -25,7 +25,7 @@ PDFErrorOr<void> TrueTypeFont::initialize(Document* document, NonnullRefPtr<Dict
auto descriptor = MUST(dict->get_dict(document, CommonNames::FontDescriptor));
if (descriptor->contains(CommonNames::FontFile2)) {
auto font_file_stream = TRY(descriptor->get_stream(document, CommonNames::FontFile2));
auto ttf_font = TRY(OpenType::Font::try_load_from_externally_owned_memory(font_file_stream->bytes()));
auto ttf_font = TRY(OpenType::Font::try_load_from_externally_owned_memory(font_file_stream->bytes(), { .skip_tables = pdf_skipped_opentype_tables }));
float point_size = (font_size * POINTS_PER_INCH) / DEFAULT_DPI;
m_font = adopt_ref(*new Gfx::ScaledFont(*ttf_font, point_size, point_size));
}

View file

@ -183,7 +183,7 @@ PDFErrorOr<NonnullOwnPtr<CIDFontType2>> CIDFontType2::create(Document* document,
if (descriptor->contains(CommonNames::FontFile2)) {
auto font_file_stream = TRY(descriptor->get_stream(document, CommonNames::FontFile2));
float point_size = (font_size * POINTS_PER_INCH) / DEFAULT_DPI;
auto ttf_font = TRY(OpenType::Font::try_load_from_externally_owned_memory(font_file_stream->bytes(), { .external_cmap = move(cid_to_gid_map) }));
auto ttf_font = TRY(OpenType::Font::try_load_from_externally_owned_memory(font_file_stream->bytes(), { .external_cmap = move(cid_to_gid_map), .skip_tables = pdf_skipped_opentype_tables }));
font = adopt_ref(*new Gfx::ScaledFont(*ttf_font, point_size, point_size));
}