From 7f999b1ff548995d763d2c4b8b65c0e8a69e12e0 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 14 Nov 2023 08:37:53 -0500 Subject: [PATCH] LibPDF: Sink m_base_font_name from PDFFont into subclasses /BaseFont is a required key for type 0, type 1, and truetype font dictionaries, but not for type 3 font dictionaries. This is mechanical; type 0 fonts don't even use this yet (but probably should). PDFFont::initialize() is now empty and could be removed, but maybe we'll put stuff there again later, so I'm leaving it around for a bit longer. --- Userland/Libraries/LibPDF/Fonts/PDFFont.cpp | 3 +-- Userland/Libraries/LibPDF/Fonts/PDFFont.h | 5 ----- Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp | 2 ++ Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h | 3 +++ Userland/Libraries/LibPDF/Fonts/Type0Font.cpp | 2 ++ Userland/Libraries/LibPDF/Fonts/Type0Font.h | 3 +++ Userland/Libraries/LibPDF/Fonts/Type1Font.cpp | 2 ++ Userland/Libraries/LibPDF/Fonts/Type1Font.h | 3 +++ 8 files changed, 16 insertions(+), 7 deletions(-) diff --git a/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp b/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp index 2b58638239..2bfc27b1d9 100644 --- a/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/PDFFont.cpp @@ -54,9 +54,8 @@ PDFErrorOr> PDFFont::create(Document* document, NonnullRe return font.release_nonnull(); } -PDFErrorOr PDFFont::initialize(Document* document, NonnullRefPtr const& dict, float) +PDFErrorOr PDFFont::initialize(Document*, NonnullRefPtr const&, float) { - m_base_font_name = TRY(dict->get_name(document, CommonNames::BaseFont))->name(); return {}; } diff --git a/Userland/Libraries/LibPDF/Fonts/PDFFont.h b/Userland/Libraries/LibPDF/Fonts/PDFFont.h index 508de7910b..d322f74562 100644 --- a/Userland/Libraries/LibPDF/Fonts/PDFFont.h +++ b/Userland/Libraries/LibPDF/Fonts/PDFFont.h @@ -23,14 +23,9 @@ public: virtual void set_font_size(float font_size) = 0; virtual PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint, DeprecatedString const&, Color const&, float font_size, float character_spacing, float word_spacing, float horizontal_scaling) = 0; - DeprecatedFlyString base_font_name() const { return m_base_font_name; } - protected: virtual PDFErrorOr initialize(Document* document, NonnullRefPtr const& dict, float font_size); static PDFErrorOr> replacement_for(StringView name, float font_size); - -private: - DeprecatedFlyString m_base_font_name; }; } diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp index d4a7c5fbd1..974956865a 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp @@ -17,6 +17,8 @@ PDFErrorOr TrueTypeFont::initialize(Document* document, NonnullRefPtrget_name(document, CommonNames::BaseFont))->name(); + // If there's an embedded font program we use that; otherwise we try to find a replacement font if (dict->contains(CommonNames::FontDescriptor)) { auto descriptor = MUST(dict->get_dict(document, CommonNames::FontDescriptor)); diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h index aecea8e4de..37f03d12c6 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h @@ -18,10 +18,13 @@ public: void set_font_size(float font_size) override; PDFErrorOr draw_glyph(Gfx::Painter&, Gfx::FloatPoint, float, u8, Color) override; + DeprecatedFlyString base_font_name() const { return m_base_font_name; } + protected: PDFErrorOr initialize(Document*, NonnullRefPtr const&, float font_size) override; private: + DeprecatedFlyString m_base_font_name; RefPtr m_font; }; diff --git a/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp index b417d85b9f..184b0999f1 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type0Font.cpp @@ -95,6 +95,8 @@ PDFErrorOr Type0Font::initialize(Document* document, NonnullRefPtrget_name(document, CommonNames::BaseFont))->name(); + // FIXME: Support arbitrary CMaps auto cmap_value = TRY(dict->get_object(document, CommonNames::Encoding)); if (!cmap_value->is() || cmap_value->cast()->name() != CommonNames::IdentityH) diff --git a/Userland/Libraries/LibPDF/Fonts/Type0Font.h b/Userland/Libraries/LibPDF/Fonts/Type0Font.h index 9edbf705d1..2db8472a4a 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type0Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type0Font.h @@ -28,12 +28,15 @@ public: void set_font_size(float font_size) override; PDFErrorOr draw_string(Gfx::Painter&, Gfx::FloatPoint pos, DeprecatedString const&, Color const&, float, float, float, float) override; + DeprecatedFlyString base_font_name() const { return m_base_font_name; } + protected: PDFErrorOr initialize(Document*, NonnullRefPtr const&, float) override; private: float get_char_width(u16 char_code) const; + DeprecatedFlyString m_base_font_name; CIDSystemInfo m_system_info; HashMap m_widths; u16 m_missing_width; diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp index 78ae70a9da..e571e3f0b4 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp @@ -17,6 +17,8 @@ PDFErrorOr Type1Font::initialize(Document* document, NonnullRefPtrget_name(document, CommonNames::BaseFont))->name(); + // auto is_standard_font = is_standard_latin_font(font->base_font_name()); // If there's an embedded font program we use that; otherwise we try to find a replacement font diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.h b/Userland/Libraries/LibPDF/Fonts/Type1Font.h index 20ca5ec078..cbe8c2c645 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.h @@ -26,10 +26,13 @@ public: void set_font_size(float font_size) override; PDFErrorOr draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) override; + DeprecatedFlyString base_font_name() const { return m_base_font_name; } + protected: PDFErrorOr initialize(Document*, NonnullRefPtr const&, float font_size) override; private: + DeprecatedFlyString m_base_font_name; RefPtr m_font_program; RefPtr m_font; HashMap> m_glyph_cache;