From f374ad50a122a7d3218645f305727ce71725e991 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Fri, 1 Mar 2024 08:10:11 -0500 Subject: [PATCH] LibPDF: Give TrueTypePainter a ScaledFont instead of a Font This will allow us to get at the font's glyphs as paths, which will eventually enable us to implement glyph rotation. We'll have to do our own caching then, but we can then hopefully share the caching across the Type0 / Type1 / TrueType codepaths. It also gives us access to a font's glyphs by glyph id, which will help us implementing looking up glyph ids by postscript name. (Else we'd have to plumb through a whole Painter::draw_glyph_by_postscript_name() API just for LibPDF.) No behavior change. --- Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp | 9 ++++----- Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp index 7bc4d3d673..8a13a7afc5 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp @@ -15,7 +15,7 @@ namespace PDF { -TrueTypePainter::TrueTypePainter(AK::NonnullRefPtr font, NonnullRefPtr encoding, bool encoding_is_mac_roman_or_win_ansi, bool is_nonsymbolic, Optional high_byte, bool is_zapf_dingbats) +TrueTypePainter::TrueTypePainter(AK::NonnullRefPtr font, NonnullRefPtr encoding, bool encoding_is_mac_roman_or_win_ansi, bool is_nonsymbolic, Optional high_byte, bool is_zapf_dingbats) : m_font(move(font)) , m_encoding(move(encoding)) , m_encoding_is_mac_roman_or_win_ansi(encoding_is_mac_roman_or_win_ansi) @@ -25,7 +25,7 @@ TrueTypePainter::TrueTypePainter(AK::NonnullRefPtr font, NonnullRefPt { } -NonnullOwnPtr TrueTypePainter::create(Document* document, NonnullRefPtr const& dict, SimpleFont const& containing_pdf_font, AK::NonnullRefPtr font, NonnullRefPtr encoding, bool is_zapf_dingbats) +NonnullOwnPtr TrueTypePainter::create(Document* document, NonnullRefPtr const& dict, SimpleFont const& containing_pdf_font, AK::NonnullRefPtr font, NonnullRefPtr encoding, bool is_zapf_dingbats) { bool encoding_is_mac_roman_or_win_ansi = false; if (dict->contains(CommonNames::Encoding)) { @@ -150,8 +150,7 @@ Optional TrueTypePainter::get_glyph_width(u8 char_code) const void TrueTypePainter::set_font_size(float font_size) { - // Guaranteed non-null for ScaledFonts. - m_font = *m_font->with_size((font_size * POINTS_PER_INCH) / DEFAULT_DPI); + m_font = m_font->scaled_with_size((font_size * POINTS_PER_INCH) / DEFAULT_DPI); } PDFErrorOr TrueTypeFont::initialize(Document* document, NonnullRefPtr const& dict, float font_size) @@ -161,7 +160,7 @@ 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 - RefPtr font; + RefPtr font; if (dict->contains(CommonNames::FontDescriptor)) { auto descriptor = MUST(dict->get_dict(document, CommonNames::FontDescriptor)); if (descriptor->contains(CommonNames::FontFile2)) { diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h index e8a3ab6080..ecd94a2d29 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h @@ -14,16 +14,16 @@ namespace PDF { class TrueTypePainter { public: - static NonnullOwnPtr create(Document*, NonnullRefPtr const&, SimpleFont const& containing_pdf_font, AK::NonnullRefPtr, NonnullRefPtr, bool is_zapf_dingbats); + static NonnullOwnPtr create(Document*, NonnullRefPtr const&, SimpleFont const& containing_pdf_font, AK::NonnullRefPtr, NonnullRefPtr, bool is_zapf_dingbats); PDFErrorOr draw_glyph(Gfx::Painter&, Gfx::FloatPoint, float width, u8 char_code, Renderer const&); Optional get_glyph_width(u8 char_code) const; void set_font_size(float font_size); private: - TrueTypePainter(AK::NonnullRefPtr, NonnullRefPtr, bool encoding_is_mac_roman_or_win_ansi, bool is_nonsymbolic, Optional high_byte, bool is_zapf_dingbats); + TrueTypePainter(AK::NonnullRefPtr, NonnullRefPtr, bool encoding_is_mac_roman_or_win_ansi, bool is_nonsymbolic, Optional high_byte, bool is_zapf_dingbats); - NonnullRefPtr m_font; + NonnullRefPtr m_font; NonnullRefPtr m_encoding; bool m_encoding_is_mac_roman_or_win_ansi { false }; bool m_is_nonsymbolic { false };