From 8166b9bf7de9b6fa88032ae6539431560b86421d Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Tue, 30 Aug 2016 10:24:09 +0000 Subject: Enhance TkpGetString() to handle Unicode characters, even when TCL_UTF_MAX==3. --- win/tkWinFont.c | 2 +- win/tkWinKey.c | 16 ++++++++++++---- win/tkWinX.c | 10 ++++++++-- 3 files changed, 21 insertions(+), 7 deletions(-) diff --git a/win/tkWinFont.c b/win/tkWinFont.c index 9172b00..940bc10 100644 --- a/win/tkWinFont.c +++ b/win/tkWinFont.c @@ -2188,7 +2188,7 @@ FontMapLoadPage( { FontFamily *familyPtr; Tcl_Encoding encoding; - char src[TCL_UTF_MAX], buf[16]; + char src[XMaxTransChars], buf[16]; USHORT *startCount, *endCount; int i, j, bitOffset, end, segCount; diff --git a/win/tkWinKey.c b/win/tkWinKey.c index ed546f7..a49898f 100644 --- a/win/tkWinKey.c +++ b/win/tkWinKey.c @@ -102,7 +102,7 @@ TkpGetString( */ int unichar; - char buf[TCL_UTF_MAX]; + char buf[XMaxTransChars]; int len; unichar = keyEv->trans_chars[1] & 0xff; @@ -115,10 +115,18 @@ TkpGetString( } else if (keyEv->send_event == -3) { /* * Special case for WM_UNICHAR. xkey.trans_chars[] already contains a - * UTF-8 char. + * UTF-8 char, except when nbytes == 0 (then it didn't fit there). */ - Tcl_DStringAppend(dsPtr, keyEv->trans_chars, keyEv->nbytes); + if (keyEv->nbytes) { + Tcl_DStringAppend(dsPtr, keyEv->trans_chars, keyEv->nbytes); + } else if (keyEv->keycode > 0xffff) { + char buf[XMaxTransChars]; + Tcl_UniCharToUtf(((keyEv->keycode - 0x10000) >> 10) | 0xd800, buf); + Tcl_DStringAppend(dsPtr, buf, 3); + Tcl_UniCharToUtf(((keyEv->keycode - 0x10000) & 0x3ff) | 0xdc00, buf); + Tcl_DStringAppend(dsPtr, buf, 3); + } } else { /* * This is an event generated from generic code. It has no nchars or @@ -129,7 +137,7 @@ TkpGetString( if (((keysym != NoSymbol) && (keysym > 0) && (keysym < 256)) || (keysym == XK_Return) || (keysym == XK_Tab)) { - char buf[TCL_UTF_MAX]; + char buf[XMaxTransChars]; int len; len = Tcl_UniCharToUtf((Tcl_UniChar) (keysym & 255), buf); diff --git a/win/tkWinX.c b/win/tkWinX.c index 2180f52..6b8bdff 100644 --- a/win/tkWinX.c +++ b/win/tkWinX.c @@ -1252,8 +1252,14 @@ GenerateXEvent( event.xany.send_event = -3; event.xkey.keycode = wParam; event.xkey.nbytes = Tcl_UniCharToUtf((int)wParam, buffer); - for (i=0; i 0xffff) && (event.xkey.nbytes < 4)) { + /* trans_chars buffer is not big enough to hold 2 surrogate + characters, so don't store anything */ + event.xkey.nbytes = 0; + } else { + for (i=0; i