diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp index f54e92a7ea..b9106c06e4 100644 --- a/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp @@ -53,8 +53,10 @@ PDFErrorOr SimpleFont::draw_string(Gfx::Painter& painter, Gfx:: float glyph_width; if (auto width = m_widths.get(char_code); width.has_value()) glyph_width = font_size * width.value() / 1000.0f; + else if (auto width = get_glyph_width(char_code); width.has_value()) + glyph_width = width.value(); else - glyph_width = get_glyph_width(char_code); + glyph_width = m_missing_width; draw_glyph(painter, glyph_position, glyph_width, char_code, paint_color); diff --git a/Userland/Libraries/LibPDF/Fonts/SimpleFont.h b/Userland/Libraries/LibPDF/Fonts/SimpleFont.h index 3c8745293a..12c60cac53 100644 --- a/Userland/Libraries/LibPDF/Fonts/SimpleFont.h +++ b/Userland/Libraries/LibPDF/Fonts/SimpleFont.h @@ -17,7 +17,7 @@ public: protected: PDFErrorOr initialize(Document* document, NonnullRefPtr const& dict, float font_size) override; - virtual float get_glyph_width(u8 char_code) const = 0; + virtual Optional 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; } @@ -26,7 +26,7 @@ private: RefPtr m_encoding; RefPtr m_to_unicode; HashMap m_widths; - u16 m_missing_width; + u16 m_missing_width { 0 }; }; } diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp index 89e8baeb03..08b30a895c 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp @@ -35,7 +35,7 @@ PDFErrorOr TrueTypeFont::initialize(Document* document, NonnullRefPtr TrueTypeFont::get_glyph_width(u8 char_code) const { return m_font->glyph_width(char_code); } diff --git a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h index 6abcd6e4af..5a732c5e90 100644 --- a/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h +++ b/Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h @@ -14,7 +14,7 @@ namespace PDF { class TrueTypeFont : public SimpleFont { public: - float get_glyph_width(u8 char_code) const override; + Optional get_glyph_width(u8 char_code) const override; void set_font_size(float font_size) 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 b9c4e94494..4fc379284c 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.cpp @@ -49,9 +49,11 @@ PDFErrorOr Type1Font::initialize(Document* document, NonnullRefPtr Type1Font::get_glyph_width(u8 char_code) const { - return m_font->glyph_width(char_code); + if (m_font) + return m_font->glyph_width(char_code); + return OptionalNone {}; } void Type1Font::set_font_size(float font_size) diff --git a/Userland/Libraries/LibPDF/Fonts/Type1Font.h b/Userland/Libraries/LibPDF/Fonts/Type1Font.h index 911626f6e1..7faa3cf8b9 100644 --- a/Userland/Libraries/LibPDF/Fonts/Type1Font.h +++ b/Userland/Libraries/LibPDF/Fonts/Type1Font.h @@ -22,7 +22,7 @@ struct Type1GlyphCacheKey { class Type1Font : public SimpleFont { public: - float get_glyph_width(u8 char_code) const override; + Optional get_glyph_width(u8 char_code) const override; void set_font_size(float font_size) 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; }