From 814e5ea011ddebadc0a4ee059e17c73a04ea5359 Mon Sep 17 00:00:00 2001 From: NishiOwO Date: Thu, 9 Oct 2025 11:21:39 +0000 Subject: [PATCH] renders git-svn-id: http://svn2.nishi.boats/svn/milsko/trunk@242 b9cfdab3-6d41-4d17-bbe4-086880011989 --- include/Mw/Unicode.h | 11 +++++++++++ src/draw.c | 9 +++++++-- src/unicode.c | 20 ++++++++++++++++++++ src/widget/entry.c | 9 ++++----- 4 files changed, 42 insertions(+), 7 deletions(-) diff --git a/include/Mw/Unicode.h b/include/Mw/Unicode.h index 23893a1..0450bf1 100644 --- a/include/Mw/Unicode.h +++ b/include/Mw/Unicode.h @@ -28,6 +28,17 @@ MWDECL int MwUTF8ToUTF32(const char* input, int* output); */ MWDECL int MwUTF8Length(const char* input); +/*! + * %brief Copies UTF-8 string to other string + * %brief src Source + * %brief srcskip Length to be skipped + * %brief dst Destination + * %brief dstskip Length to be skipped + * %brief len Length + * %return Copied length in bytes + */ +MWDECL int MwUTF8Copy(const char* src, int srcskip, char* dst, int dstskip, int len); + #ifdef __cplusplus } #endif diff --git a/src/draw.c b/src/draw.c index 76f7942..e227aa6 100644 --- a/src/draw.c +++ b/src/draw.c @@ -372,7 +372,12 @@ void MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int sx -= strlen(text) * FontWidth * FontScale; } - for(i = 0; text[i] != 0; i++) { + while(text[i] != 0) { + int out; + i += MwUTF8ToUTF32(text, &out); + + if(out >= 0x80) out = 0; + for(y = 0; y < FontHeight; y++) { for(x = 0; x < FontWidth; x++) { r.x = sx + x * FontScale; @@ -380,7 +385,7 @@ void MwDrawText(MwWidget handle, MwPoint* point, const char* text, int bold, int r.width = FontScale; r.height = FontScale; - if((bold ? MwBoldFontData : MwFontData)[(unsigned char)text[i]].data[y] & (1 << ((FontWidth - 1) - x))) { + if((bold ? MwBoldFontData : MwFontData)[out].data[y] & (1 << ((FontWidth - 1) - x))) { MwDrawRect(handle, &r, color); } } diff --git a/src/unicode.c b/src/unicode.c index 04b2a34..06fbd88 100644 --- a/src/unicode.c +++ b/src/unicode.c @@ -74,3 +74,23 @@ int MwUTF8Length(const char* input) { return len; } + +int MwUTF8Copy(const char* src, int srcskip, char* dst, int dstskip, int len){ + int i; + int out; + int total = 0; + for(i = 0; i < srcskip; i++) src += MwUTF8ToUTF32(src, &out); + for(i = 0; i < dstskip; i++) dst += MwUTF8ToUTF32(dst, &out); + for(i = 0; i < len; i++){ + int len = MwUTF8ToUTF32(src, &out); + + memcpy(dst, src, len); + + src += len; + dst += len; + total += len; + } + dst[total] = 0; + + return total; +} diff --git a/src/widget/entry.c b/src/widget/entry.c index 8f77a31..994b831 100644 --- a/src/widget/entry.c +++ b/src/widget/entry.c @@ -13,7 +13,7 @@ static int create(MwWidget handle) { MwSetDefault(handle); - MwSetText(handle, MwNtext, "こんにちは、世界"); + MwSetText(handle, MwNtext, "こんにちは、世界 Hello, World"); MwLLSetCursor(handle->lowlevel, &MwCursorText, &MwCursorTextMask); return 0; @@ -111,12 +111,11 @@ static void key(MwWidget handle, int code) { if(t->cursor == (int)strlen(str)) return; t->cursor++; } else { - int i, incr = 0; + int incr = 0; out = malloc(strlen(str) + 1 + 1); - for(i = 0; i < t->cursor; i++) out[incr++] = str[i]; + incr += MwUTF8Copy(str, 0, out, 0, t->cursor); out[incr++] = code; - for(i = t->cursor; i < (int)strlen(str); i++) out[incr++] = str[i]; - out[incr++] = 0; + MwUTF8Copy(str, t->cursor, out, t->cursor + 1, MwUTF8Length(str) - t->cursor); t->cursor++;