From a4b5a1d051cb3b012a5845112f42674604c7588b Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 14 Mar 2024 13:45:48 +0000 Subject: [PATCH] Handle Tcl_UtfToUniChar/Tcl_UniCharToUtf correctly as well --- generic/tkInt.h | 17 ++++++----------- generic/tkUtil.c | 20 +++++++++----------- generic/tkWindow.c | 3 --- macosx/tkMacOSXPrivate.h | 4 ++-- 4 files changed, 17 insertions(+), 27 deletions(-) diff --git a/generic/tkInt.h b/generic/tkInt.h index b96edba8a..6f975b917 100644 --- a/generic/tkInt.h +++ b/generic/tkInt.h @@ -76,8 +76,8 @@ #endif #if (TCL_MAJOR_VERSION == 8) && (TCL_MINOR_VERSION < 7) -# define Tcl_WCharToUtfDString ((char * (*)(const WCHAR *, int len, Tcl_DString *))Tcl_UniCharToUtfDString) -# define Tcl_UtfToWCharDString ((WCHAR * (*)(const char *, int len, Tcl_DString *))Tcl_UtfToUniCharDString) +# define Tcl_WCharToUtfDString ((char * (*)(const WCHAR *, int, Tcl_DString *))Tcl_UniCharToUtfDString) +# define Tcl_UtfToWCharDString ((WCHAR * (*)(const char *, int, Tcl_DString *))Tcl_UtfToUniCharDString) # define Tcl_Char16ToUtfDString Tcl_UniCharToUtfDString # define Tcl_UtfToChar16DString Tcl_UtfToUniCharDString # define TCL_COMBINE 0 @@ -85,10 +85,14 @@ /* Make available UTF-32 versions of the API, even though we compile with TCL_UTF_MAX=3 */ #if TCL_MAJOR_VERSION > 8 +# define TkUtfToUniChar (tclStubsPtr->tcl_UtfToUniChar) /* 646 */ +# define TkUniCharToUtf (tclStubsPtr->tcl_UniCharToUtf) /* 324 (without TCL_COMBINE) */ # define TkNumUtfChars (tclStubsPtr->tcl_NumUtfChars) /* 669 */ # define TkGetCharLength (tclStubsPtr->tcl_GetCharLength) /* 670 */ # define TkUtfAtIndex (tclStubsPtr->tcl_UtfAtIndex) /* 671 */ #else + MODULE_SCOPE Tcl_Size TkUtfToUniChar(const char *, int *); + MODULE_SCOPE Tcl_Size TkUniCharToUtf(int, char *); # define TkNumUtfChars (((&tclStubsPtr->tcl_PkgProvideEx)[631]) ? \ ((Tcl_Size (*)(const char *, Tcl_Size))(void *)((&tclStubsPtr->tcl_PkgProvideEx)[669])) \ : (tclStubsPtr->tcl_NumUtfChars) /* 312 */) @@ -1394,15 +1398,6 @@ MODULE_SCOPE void TkpCopyRegion(TkRegion dst, TkRegion src); # define c_class class #endif -/* Tcl 8.6 has a different definition of Tcl_UniChar than other Tcl versions for TCL_UTF_MAX > 3 */ -#if TCL_UTF_MAX > (3 + (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION == 6)) -# define TkUtfToUniChar(src, ch) (size_t)(((int (*)(const char *, int *))Tcl_UtfToUniChar)(src, ch)) -# define TkUniCharToUtf(ch, src) (size_t)(((int (*)(int, char *))Tcl_UniCharToUtf)(ch, src)) -#else - MODULE_SCOPE size_t TkUtfToUniChar(const char *, int *); - MODULE_SCOPE size_t TkUniCharToUtf(int, char *); -#endif - #if defined(_WIN32) && !defined(STATIC_BUILD) && TCL_MAJOR_VERSION < 9 # define tcl_CreateFileHandler reserved9 #endif diff --git a/generic/tkUtil.c b/generic/tkUtil.c index 13b91d39c..ef65880ea 100644 --- a/generic/tkUtil.c +++ b/generic/tkUtil.c @@ -1246,8 +1246,7 @@ Tk_SendVirtualEvent( Tk_QueueWindowEvent(&event.general, TCL_QUEUE_TAIL); } -/* Tcl 8.6 has a different definition of Tcl_UniChar than other Tcl versions for TCL_UTF_MAX > 3 */ -#if TCL_UTF_MAX <= (3 + (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION == 6)) +#if TCL_MAJOR_VERSION < 9 /* *--------------------------------------------------------------------------- * @@ -1267,7 +1266,7 @@ Tk_SendVirtualEvent( *--------------------------------------------------------------------------- */ -size_t +Tcl_Size TkUtfToUniChar( const char *src, /* The UTF-8 string. */ int *chPtr) /* Filled with the Unicode value represented by @@ -1275,12 +1274,11 @@ TkUtfToUniChar( { Tcl_UniChar uniChar = 0; - size_t len = Tcl_UtfToUniChar(src, &uniChar); + Tcl_Size len = Tcl_UtfToUniChar(src, &uniChar); if ((uniChar & 0xFC00) == 0xD800) { Tcl_UniChar low = uniChar; - /* This can only happen if sizeof(Tcl_UniChar)== 2 and src points - * to a character > U+FFFF */ - size_t len2 = Tcl_UtfToUniChar(src+len, &low); + /* This can only happen if src points to a character > U+FFFF */ + Tcl_Size len2 = Tcl_UtfToUniChar(src+len, &low); if ((low & 0xFC00) == 0xDC00) { *chPtr = (((uniChar & 0x3FF) << 10) | (low & 0x3FF)) + 0x10000; return len + len2; @@ -1309,12 +1307,12 @@ TkUtfToUniChar( *--------------------------------------------------------------------------- */ -size_t TkUniCharToUtf(int ch, char *buf) +Tcl_Size TkUniCharToUtf(int ch, char *buf) { if ((unsigned)(ch - 0x10000) <= 0xFFFFF) { - /* Spit out a 4-byte UTF-8 character or 2 x 3-byte UTF-8 characters, depending on Tcl - * version and/or TCL_UTF_MAX build value */ - int len = Tcl_UniCharToUtf(0xD800 | ((ch - 0x10000) >> 10), buf); + /* Spit out a 4-byte UTF-8 character (Tcl 8.7+) or + * 2 x 3-byte UTF-8 characters (Tcl 8.6) */ + Tcl_Size len = Tcl_UniCharToUtf(0xD800 | ((ch - 0x10000) >> 10), buf); return len + Tcl_UniCharToUtf(0xDC00 | (ch & 0x7FF), buf + len); } return Tcl_UniCharToUtf(ch, buf); diff --git a/generic/tkWindow.c b/generic/tkWindow.c index 37cda60ed..53f6936ed 100644 --- a/generic/tkWindow.c +++ b/generic/tkWindow.c @@ -1028,9 +1028,6 @@ TkCreateMainWindow( #ifdef STATIC_BUILD ".static" #endif -#if TCL_UTF_MAX <= (3 + (TCL_MAJOR_VERSION == 8 && TCL_MINOR_VERSION == 6)) - ".utf-16" -#endif #if defined(_WIN32) ".win32" #endif diff --git a/macosx/tkMacOSXPrivate.h b/macosx/tkMacOSXPrivate.h index 51901c93c..de230da41 100644 --- a/macosx/tkMacOSXPrivate.h +++ b/macosx/tkMacOSXPrivate.h @@ -532,8 +532,8 @@ VISIBILITY_HIDDEN * NSString class does not provide a constructor which accepts a CESU-8 encoded * byte sequence as initial data. So we add a new class which does provide * such a constructor. It also has a DString property which is a DString whose - * string pointer is a byte sequence encoding the NSString with the current Tk - * encoding, namely UTF-8 if TCL_UTF_MAX >= 4 or CESU-8 if TCL_UTF_MAX = 3. + * string pointer is a byte sequence encoding the NSString with the current Tcl + * internal encoding, namely UTF-8 for Tcl8.7+ or CESU-8 otherwise. * *--------------------------------------------------------------------------- */