From 35b73ba95442b7ce47b964505c2368ce75fda9aa Mon Sep 17 00:00:00 2001 From: Sylvain PILLOT Date: Sat, 24 Feb 2024 10:49:28 +0100 Subject: [PATCH] added better (ie. cleaner) support for unicode characters in Kandinsky module --- ports/sh/mpconfigport.h | 4 ---- ports/sh/numworks/modkandinsky.c | 34 +++++++++++++++++++++----------- 2 files changed, 22 insertions(+), 16 deletions(-) diff --git a/ports/sh/mpconfigport.h b/ports/sh/mpconfigport.h index 539a1330c..4d2058993 100644 --- a/ports/sh/mpconfigport.h +++ b/ports/sh/mpconfigport.h @@ -127,7 +127,3 @@ typedef long mp_off_t; #define MP_STATE_PORT MP_STATE_VM -/* Activate support of unicode string in MicroPython */ -#define MICROPY_PY_BUILTINS_STR_UNICODE (1) - - diff --git a/ports/sh/numworks/modkandinsky.c b/ports/sh/numworks/modkandinsky.c index 443493c69..169745ad9 100644 --- a/ports/sh/numworks/modkandinsky.c +++ b/ports/sh/numworks/modkandinsky.c @@ -228,21 +228,31 @@ static mp_obj_t Kandinsky_draw_string(size_t n, mp_obj_t const *args) { u = 0; v += 16; } else { - /* The following test is for support of unicode characters that are encoded on 2 chars */ - /* This gives text[l]<0 and we need to pass 2 chars to dtext to take care of unicode encoding */ - if(text[l]>=0){ + /* The following test is for support of unicode characters that are encoded on 1 char or more */ + /* we need to pass multiple chars to dtext to take care of unicode encoding */ + if(((unsigned char) text[l]) >= 0x00 && ((unsigned char) text[l]) <= 0x7F){ drect(x + u - 1, y + v - 1, x + u + 9, y + v + 15, colorback); - dtext_opt(x + u, y + v, colortext, C_NONE, DTEXT_LEFT, DTEXT_TOP, - &text[l], 1); + dtext_opt(x + u, y + v, colortext, C_NONE, DTEXT_LEFT, DTEXT_TOP, &text[l], 1); u += 10; - } else { - drect(x + u - 1, y + v - 1, x + u + 9, y + v + 15, colorback); - dtext_opt(x + u, y + v, colortext, C_NONE, DTEXT_LEFT, DTEXT_TOP, - &text[l], 2); - u += 10; - l++; } - + else if(((unsigned char) text[l]) >= 0x80 && ((unsigned char) text[l]) <= 0xDF){ + drect(x + u - 1, y + v - 1, x + u + 9, y + v + 15, colorback); + dtext_opt(x + u, y + v, colortext, C_NONE, DTEXT_LEFT, DTEXT_TOP, &text[l], 2); + u += 10; + l+=1; + } + else if(((unsigned char) text[l]) >= 0xE0 && ((unsigned char) text[l]) <= 0xEF){ + drect(x + u - 1, y + v - 1, x + u + 9, y + v + 15, colorback); + dtext_opt(x + u, y + v, colortext, C_NONE, DTEXT_LEFT, DTEXT_TOP, &text[l], 3); + u += 10; + l+=2; + } + else if(((unsigned char) text[l]) >= 0xF0 && ((unsigned char) text[l]) <= 0xF7){ + drect(x + u - 1, y + v - 1, x + u + 9, y + v + 15, colorback); + dtext_opt(x + u, y + v, colortext, C_NONE, DTEXT_LEFT, DTEXT_TOP, &text[l], 4); + u += 10; + l+=3; + } } }