diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp index e5e144ed345..d44de2f968e 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp @@ -26,7 +26,7 @@ static bool is_standard_latin_font(FlyString const& font) "Courier-BoldOblique"); } -PDFErrorOr> Type1Font::create(Document* document, NonnullRefPtr dict) +PDFErrorOr Type1Font::parse_data(Document* document, NonnullRefPtr dict) { // FIXME: "Required except for the standard 14 fonts"... // "Beginning with PDF 1.5, the special treatment given to the standard 14 @@ -68,33 +68,36 @@ PDFErrorOr> Type1Font::create(Document* document, Nonnu if (descriptor->contains(CommonNames::MissingWidth)) missing_width = descriptor->get_value(CommonNames::MissingWidth).get(); - return adopt_ref(*new Type1Font(to_unicode, encoding.release_nonnull(), widths, missing_width)); + return Type1Font::Data { to_unicode, encoding.release_nonnull(), move(widths), missing_width }; } -Type1Font::Type1Font(RefPtr to_unicode, NonnullRefPtr encoding, HashMap const& widths, u16 missing_width) - : m_to_unicode(to_unicode) - , m_encoding(encoding) - , m_widths(widths) - , m_missing_width(missing_width) +PDFErrorOr> Type1Font::create(Document* document, NonnullRefPtr dict) +{ + auto data = TRY(Type1Font::parse_data(document, dict)); + return adopt_ref(*new Type1Font(data)); +} + +Type1Font::Type1Font(Data data) + : m_data(move(data)) { } u32 Type1Font::char_code_to_code_point(u16 char_code) const { - if (m_to_unicode) + if (m_data.to_unicode) TODO(); - auto descriptor = m_encoding->get_char_code_descriptor(char_code); + auto descriptor = m_data.encoding->get_char_code_descriptor(char_code); return descriptor.code_point; } float Type1Font::get_char_width(u16 char_code, float) const { u16 width; - if (auto char_code_width = m_widths.get(char_code); char_code_width.has_value()) { + if (auto char_code_width = m_data.widths.get(char_code); char_code_width.has_value()) { width = char_code_width.value(); } else { - width = m_missing_width; + width = m_data.missing_width; } return static_cast(width) / 1000.0f; diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.h b/Userland/Libraries/LibPDF/Fonts/Type1Font.h index 6bd3e98caa7..18608cb215f 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.h @@ -13,19 +13,26 @@ namespace PDF { class Type1Font : public PDFFont { public: + // Also used by TrueTypeFont, which is very similar to Type1 + struct Data { + RefPtr to_unicode; + NonnullRefPtr encoding; + HashMap widths; + u16 missing_width; + }; + + static PDFErrorOr parse_data(Document*, NonnullRefPtr font_dict); + static PDFErrorOr> create(Document*, NonnullRefPtr); - Type1Font(RefPtr to_unicode, NonnullRefPtr, HashMap const& m_widths, u16 missing_width); + Type1Font(Data); ~Type1Font() override = default; u32 char_code_to_code_point(u16 char_code) const override; float get_char_width(u16 char_code, float font_size) const override; private: - RefPtr m_to_unicode; - NonnullRefPtr m_encoding; - HashMap m_widths; - u16 m_missing_width; + Data m_data; }; }