diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp index 358e22024a..303aef9d08 100644 --- a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp @@ -45,17 +45,18 @@ PDFErrorOr SimpleFont::initialize(Document* document, NonnullRefPtr(m_widths.get(char_code).value_or(m_missing_width)) / 1000.0f; -} - PDFErrorOr SimpleFont::draw_string(Gfx::Painter& painter, Gfx::FloatPoint glyph_position, DeprecatedString const& string, Color const& paint_color, float font_size, float character_spacing, float horizontal_scaling) { auto so = make_object(string, true); for (auto char_code : string.bytes()) { - auto char_width = get_char_width(char_code); - auto glyph_width = char_width * font_size; + // Use the width specified in the font's dictionary if available, + // and use the default width for the given font otherwise. + float glyph_width; + if (auto width = m_widths.get(char_code); width.has_value()) + glyph_width = font_size * width.value() / 1000.0f; + else + glyph_width = get_glyph_width(char_code); + draw_glyph(painter, glyph_position, glyph_width, char_code, paint_color); auto tx = glyph_width; tx += character_spacing; diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.h b/Userland/Libraries/LibPDF/Fonts/SimpleFont.h index 61bc7b2eed..8137a2ef46 100644 --- a/Userland/Libraries/LibPDF/Fonts/SimpleFont.h +++ b/Userland/Libraries/LibPDF/Fonts/SimpleFont.h @@ -17,13 +17,12 @@ public: protected: PDFErrorOr initialize(Document* document, NonnullRefPtr const& dict, float font_size) override; + virtual float get_glyph_width(u8 char_code) const = 0; virtual void draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) = 0; RefPtr& encoding() { return m_encoding; } RefPtr const& encoding() const { return m_encoding; } private: - float get_char_width(u8 char_code) const; - RefPtr m_encoding; RefPtr m_to_unicode; HashMap m_widths; diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp index 6a943a08c4..a35680bd51 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp @@ -30,11 +30,13 @@ PDFErrorOr TrueTypeFont::initialize(Document* document, NonnullRefPtrglyph_width(char_code); +} + void TrueTypeFont::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float, u8 char_code, Color color) { - if (!m_font) - return; - // Account for the reversed font baseline auto position = point.translated(0, -m_font->baseline()); painter.draw_glyph(position, char_code, *m_font, color); diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h index 02fba97f5b..240b7da4fe 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h @@ -14,6 +14,7 @@ namespace PDF { class TrueTypeFont : public SimpleFont { public: + float get_glyph_width(u8 char_code) const override; void draw_glyph(Gfx::Painter&, Gfx::FloatPoint, float, u8, Color) override; Type type() const override { return PDFFont::Type::TrueType; } diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp index 49a828e3d8..c9cf6c2e05 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp @@ -49,6 +49,11 @@ PDFErrorOr Type1Font::initialize(Document* document, NonnullRefPtrglyph_width(char_code); +} + void Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) { if (!m_font_program) { diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.h b/Userland/Libraries/LibPDF/Fonts/Type1Font.h index c85df6d7b0..c780804323 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.h @@ -14,6 +14,7 @@ namespace PDF { class Type1Font : public SimpleFont { public: + float get_glyph_width(u8 char_code) const override; void draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) override; Type type() const override { return PDFFont::Type::Type1; }