From eb2c61cd04094494877848a2b065c94d34c31f9d Mon Sep 17 00:00:00 2001 From: Layla Marchant Date: Mon, 27 Jul 2020 10:12:47 -0400 Subject: [PATCH] Added a check for if the breakable byte is the start of char. Fixes #9 Thanks to https://gamedev.net/forums/topic/707591-could-anyone-help-with-utf-8-line-breaking-in-c/5429529/ for helping with how to do this. --- src/gfx.c | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/src/gfx.c b/src/gfx.c index ccae18c..0c48503 100644 --- a/src/gfx.c +++ b/src/gfx.c @@ -272,6 +272,20 @@ int gfx_renderUnicode(const char *in, int x, int y, int fontColor, int wrap, SDL } #else +static int gfx_charIsUTF8Start(unsigned char c) +{ + // Top bit not set (single byte ASCII character) + if ((c & 0x80) == 0) + return 1; + + // Top two bits set (start of multi-byte character) + if ((c & 0x80) && (c & 0x40)) + return 1; + + // Top bit set, but second bit not set (somewhere in the middle) + return 0; +} + int gfx_unicodeWidth(const char *in) { int w; @@ -360,7 +374,8 @@ int gfx_renderUnicodeBase(const char *in, int x, int y, int real_x, int fontColo nBreakPoints = 0; for (i = 0; i < nLogAttrs; i++) { - if (logAttrs[i].is_line_break) + if (logAttrs[i].is_line_break + && gfx_charIsUTF8Start(remainingStr[i])) { breakPoints[nBreakPoints] = i; nBreakPoints++;