From 19db5e1b4acafe4553236f9e5a0a481431801f26 Mon Sep 17 00:00:00 2001 From: Bill Kendrick Date: Wed, 3 Nov 2021 22:16:24 -0700 Subject: [PATCH] Avoid compile-time warnings re: wchar_t comparison When assembling UTF-8 in render_text_w() (to send to SDL_Pango), we assumed `wchar_t` could encompass the entirety of Unicode (up to U+10FFFF); however, on Windows `wchar_t` is only 16-bits, meaning the test for characters <= U+FFFF was always true. This reworks the if/elseif/else block, via some "#ifndef WIN32" tests, to avoid this warning. In the end, we need to replace our internal use of `wchar_t` with something 32-bit, to allow for higher Unicode code points (e.g., to support Emoji via the Text and Label tools). Additonally, some work will need to be done to ensure that text stored as Labels within saved Tux Paint images (PNGs) continued to work correctly, and could be traded between platforms. (Untested, but right now I assume a picture drawn on Linux where `wchar_t` is 32-bit would break if you attempt to load it on Windows, and possibly vice-versa?) See https://sourceforge.net/p/tuxpaint/feature-requests/210/ --- src/tuxpaint.c | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/src/tuxpaint.c b/src/tuxpaint.c index 05d0b8396..90e78cb5b 100644 --- a/src/tuxpaint.c +++ b/src/tuxpaint.c @@ -1686,7 +1686,14 @@ static SDL_Surface *render_text_w(TuxPaint_Font * restrict font, const wchar_t * utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --fghijk to --fghijk */ (0x80)); /* add 10------ */ } +#ifndef WIN32 else if (str[i] <= 0x0000FFFF) +#else + /* str[i] is a wchar_t, which is only 16-bit on Windows, so + avoiding a "comparison is always true due to limited range + of data type" compile-time warning */ + else +#endif { /* Range: 0x00000800 - 0x0000FFFF: @@ -1700,12 +1707,14 @@ static SDL_Surface *render_text_w(TuxPaint_Font * restrict font, const wchar_t * utfstr[j++] = (((str[i] & 0x003F)) | /* -------- --klmnop to --klmnop */ (0x80)); /* add 10------ */ } +#ifndef WIN32 else { /* Range: 0x00010000 - 0x001FFFFF: In: 000abcde fghijklm nopqrstu - Out: 11110abc 10defghi 10jklmno 10pqrstu */ + Out: 11110abc 10defghi 10jklmno 10pqrstu + */ utfstr[j++] = (((str[i] & 0x1C0000) >> 18) | /* ---abc-- -------- -------- to -----abc */ (0xF0)); /* add 11110000 */ @@ -1718,6 +1727,7 @@ static SDL_Surface *render_text_w(TuxPaint_Font * restrict font, const wchar_t * utfstr[j++] = ((str[i] & 0x00003F) | /* -------- -------- --pqrstu to --prqstu */ (0x80)); /* add 10------ */ } +#endif } utfstr[j] = '\0';