Selaa lähdekoodia

LibPDF: Make SimpleFont::draw_glyph() fallible

Nico Weber 1 vuosi sitten
vanhempi
commit
6c1da5db54

+ 1 - 1
Userland/Libraries/LibPDF/Fonts/SimpleFont.cpp

@@ -58,7 +58,7 @@ PDFErrorOr<Gfx::FloatPoint> SimpleFont::draw_string(Gfx::Painter& painter, Gfx::
         else
             glyph_width = m_missing_width;
 
-        draw_glyph(painter, glyph_position, glyph_width, char_code, paint_color);
+        TRY(draw_glyph(painter, glyph_position, glyph_width, char_code, paint_color));
 
         auto tx = glyph_width;
         tx += character_spacing;

+ 1 - 1
Userland/Libraries/LibPDF/Fonts/SimpleFont.h

@@ -18,7 +18,7 @@ public:
 protected:
     PDFErrorOr<void> initialize(Document* document, NonnullRefPtr<DictObject> const& dict, float font_size) override;
     virtual Optional<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;
+    virtual PDFErrorOr<void> draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) = 0;
     RefPtr<Encoding>& encoding() { return m_encoding; }
     RefPtr<Encoding> const& encoding() const { return m_encoding; }
 

+ 2 - 1
Userland/Libraries/LibPDF/Fonts/TrueTypeFont.cpp

@@ -45,11 +45,12 @@ void TrueTypeFont::set_font_size(float font_size)
     m_font = m_font->with_size((font_size * POINTS_PER_INCH) / DEFAULT_DPI);
 }
 
-void TrueTypeFont::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float, u8 char_code, Color color)
+PDFErrorOr<void> TrueTypeFont::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float, u8 char_code, Color color)
 {
     // Account for the reversed font baseline
     auto position = point.translated(0, -m_font->baseline());
     painter.draw_glyph(position, char_code, *m_font, color);
+    return {};
 }
 
 }

+ 1 - 1
Userland/Libraries/LibPDF/Fonts/TrueTypeFont.h

@@ -16,7 +16,7 @@ class TrueTypeFont : public SimpleFont {
 public:
     Optional<float> 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;
+    PDFErrorOr<void> draw_glyph(Gfx::Painter&, Gfx::FloatPoint, float, u8, Color) override;
 
 protected:
     PDFErrorOr<void> initialize(Document*, NonnullRefPtr<DictObject> const&, float font_size) override;

+ 3 - 2
Userland/Libraries/LibPDF/Fonts/Type1Font.cpp

@@ -62,13 +62,13 @@ void Type1Font::set_font_size(float font_size)
         m_font = m_font->with_size((font_size * POINTS_PER_INCH) / DEFAULT_DPI);
 }
 
-void Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color)
+PDFErrorOr<void> Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color)
 {
     if (!m_font_program) {
         // Account for the reversed font baseline
         auto position = point.translated(0, -m_font->baseline());
         painter.draw_glyph(position, char_code, *m_font, color);
-        return;
+        return {};
     }
 
     auto effective_encoding = encoding();
@@ -95,5 +95,6 @@ void Type1Font::draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float w
     painter.blit_filtered(glyph_position.blit_position, *bitmap, bitmap->rect(), [color](Color pixel) -> Color {
         return pixel.multiply(color);
     });
+    return {};
 }
 }

+ 1 - 1
Userland/Libraries/LibPDF/Fonts/Type1Font.h

@@ -24,7 +24,7 @@ class Type1Font : public SimpleFont {
 public:
     Optional<float> 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;
+    PDFErrorOr<void> draw_glyph(Gfx::Painter& painter, Gfx::FloatPoint point, float width, u8 char_code, Color color) override;
 
 protected:
     PDFErrorOr<void> initialize(Document*, NonnullRefPtr<DictObject> const&, float font_size) override;