From 136762f6a2829161bf9218c4bac0b37cbd5d338e Mon Sep 17 00:00:00 2001
From: jdgleaver <james@leaver.myzen.co.uk>
Date: Tue, 9 Apr 2019 15:51:33 +0100
Subject: [PATCH] Fix text display issues when using Japanese (and other
 unicode-dependent language) text with stb_unicode

---
 gfx/drivers_font_renderer/stb_unicode.c | 19 +++++++++++++++----
 1 file changed, 15 insertions(+), 4 deletions(-)

diff --git a/gfx/drivers_font_renderer/stb_unicode.c b/gfx/drivers_font_renderer/stb_unicode.c
index aa74e1b8f5..075490f577 100644
--- a/gfx/drivers_font_renderer/stb_unicode.c
+++ b/gfx/drivers_font_renderer/stb_unicode.c
@@ -152,11 +152,22 @@ static const struct font_glyph *font_renderer_stb_unicode_get_glyph(
    dst = (uint8_t*)self->atlas.buffer + atlas_slot->glyph.atlas_offset_x
          + atlas_slot->glyph.atlas_offset_y * self->atlas.width;
 
-   stbtt_MakeGlyphBitmap(&self->info, dst, self->max_glyph_width, self->max_glyph_height,
-         self->atlas.width, self->scale_factor, self->scale_factor, glyph_index);
-
    stbtt_GetGlyphHMetrics(&self->info, glyph_index, &advance_width, &left_side_bearing);
-   stbtt_GetGlyphBox(&self->info, glyph_index, &x0, NULL, NULL, &y1);
+   if (stbtt_GetGlyphBox(&self->info, glyph_index, &x0, NULL, NULL, &y1))
+   {
+      stbtt_MakeGlyphBitmap(&self->info, dst, self->max_glyph_width, self->max_glyph_height,
+            self->atlas.width, self->scale_factor, self->scale_factor, glyph_index);
+   }
+   else
+   {
+      /* This means the glyph is empty. In this case, stbtt_MakeGlyphBitmap()
+       * fills the corresponding region of the atlas buffer with garbage,
+       * so just zero it */
+      unsigned x, y;
+      for (x = 0; x < self->max_glyph_width; x++)
+         for (y = 0; y < self->max_glyph_height; y++)
+            dst[x + (y * self->atlas.width)] = 0;
+   }
 
    atlas_slot->glyph.width          = self->max_glyph_width;
    atlas_slot->glyph.height         = self->max_glyph_height;