From 448eaa29661e50c6b1b971cdee6010dba1ff5d07 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Sun, 25 Feb 2024 13:19:43 -0500 Subject: [PATCH] LibPDF: Let Type1Font use TrueTypePainter for standard fonts ...and for fallback fonts too. We use Liberation Sans (a truetype font) for standard and fallback fonts. So we should use the standard PDF algorithm for mapping bytes to truetype glyphs. TrueTypePainter knows how to do this. Makes the "fi" ligature in the title on page 1 of 5014.CIDFont_Spec.pdf or the dotless-i in the title of page 2 of ThinkingInPostScript.pdf show up. They use Helvetica and TImes, and Helvetica and Symbol respecitively (with -Bold variants). --- Userland/Libraries/LibPDF/Fonts/Type1Font.cpp | 34 ++++++++----------- Userland/Libraries/LibPDF/Fonts/Type1Font.h | 3 +- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp index 5c1890ea22..671047d449 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp @@ -49,43 +49,39 @@ PDFErrorOr Type1Font::initialize(Document* document, NonnullRefPtr Type1Font::get_glyph_width(u8 char_code) const { - if (m_font) - return m_font->glyph_width(char_code); + if (m_fallback_font_painter) + return m_fallback_font_painter->get_glyph_width(char_code); return OptionalNone {}; } void Type1Font::set_font_size(float font_size) { - if (m_font) - m_font = m_font->with_size((font_size * POINTS_PER_INCH) / DEFAULT_DPI); + if (m_fallback_font_painter) + m_fallback_font_painter->set_font_size(font_size); } PDFErrorOr Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Renderer const& renderer) { auto style = renderer.state().paint_style; - if (!m_font_program) { - // Undo shift in Glyf::Glyph::append_simple_path() via OpenType::Font::rasterize_glyph(). - auto position = point.translated(0, -m_font->pixel_metrics().ascent); - // FIXME: Bounding box and sample point look to be pretty wrong - if (style.has()) { - painter.draw_glyph(position, char_code, *m_font, style.get()); - } else { - style.get>()->paint(Gfx::IntRect(position.x(), position.y(), width, 0), [&](auto sample) { - painter.draw_glyph(position, char_code, *m_font, sample(Gfx::IntPoint(position.x(), position.y()))); - }); - } - return {}; - } + if (!m_font_program) + return m_fallback_font_painter->draw_glyph(painter, point, width, char_code, renderer); auto effective_encoding = encoding(); if (!effective_encoding) diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.h b/Userland/Libraries/LibPDF/Fonts/Type1Font.h index 454905e22d..d327f96a00 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.h @@ -8,6 +8,7 @@ #include #include +#include #include namespace PDF { @@ -34,7 +35,7 @@ protected: private: DeprecatedFlyString m_base_font_name; RefPtr m_font_program; - RefPtr m_font; + OwnPtr m_fallback_font_painter; HashMap> m_glyph_cache; };