Parcourir la source

LibGfx: draw_glyph_or_emoji fix check for available glyph

This would cause question marks to be rendered when a ttf with fewer
glyphs than the value of the code_point was used.
Stephan Unverwerth il y a 4 ans
Parent
commit
3b67b55c84

+ 1 - 0
Userland/Libraries/LibGfx/BitmapFont.h

@@ -58,6 +58,7 @@ public:
     void set_weight(u16 weight) { m_weight = weight; }
 
     Glyph glyph(u32 code_point) const;
+    bool contains_glyph(u32 code_point) const { return code_point < (u32)glyph_count(); }
 
     u8 glyph_width(size_t ch) const { return m_fixed_width ? m_glyph_width : m_glyph_widths[ch]; }
     int glyph_or_emoji_width(u32 code_point) const;

+ 1 - 0
Userland/Libraries/LibGfx/Font.h

@@ -110,6 +110,7 @@ public:
 
     virtual u16 weight() const = 0;
     virtual Glyph glyph(u32 code_point) const = 0;
+    virtual bool contains_glyph(u32 code_point) const = 0;
 
     virtual u8 glyph_width(size_t ch) const = 0;
     virtual int glyph_or_emoji_width(u32 code_point) const = 0;

+ 2 - 3
Userland/Libraries/LibGfx/Painter.cpp

@@ -929,9 +929,8 @@ void Painter::draw_emoji(const IntPoint& point, const Gfx::Bitmap& emoji, const
 
 void Painter::draw_glyph_or_emoji(const IntPoint& point, u32 code_point, const Font& font, Color color)
 {
-    if (code_point < (u32)font.glyph_count()) {
-        // This looks like a regular character.
-        draw_glyph(point, (size_t)code_point, font, color);
+    if (font.contains_glyph(code_point)) {
+        draw_glyph(point, code_point, font, color);
         return;
     }
 

+ 1 - 0
Userland/Libraries/LibTTF/Font.h

@@ -140,6 +140,7 @@ public:
     virtual u8 presentation_size() const override { return m_point_height; }
     virtual u16 weight() const override { return m_font->weight(); }
     virtual Gfx::Glyph glyph(u32 code_point) const override;
+    virtual bool contains_glyph(u32 code_point) const override { return m_font->glyph_id_for_codepoint(code_point) > 0; }
     virtual u8 glyph_width(size_t ch) const override;
     virtual int glyph_or_emoji_width(u32 code_point) const override;
     virtual u8 glyph_height() const override { return m_point_height; }    /* TODO */