From 41d3c8f67e62fe307c46c91e5a42b71e39c59334 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 16 Sep 2016 07:49:21 +0000 Subject: Patch from Christian Werner, for evaluation --- generic/tkEntry.c | 16 +++++++- generic/tkFont.c | 41 ++++++++++++++++++++ generic/tkInt.h | 4 ++ generic/tkText.c | 8 ++++ generic/tkTextIndex.c | 12 +++++- generic/tkUtil.c | 102 +++++++++++++++++++++++++++++++++++++++++++++++++ generic/ttk/ttkEntry.c | 16 +++++++- unix/tkUnixFont.c | 6 ++- unix/tkUnixRFont.c | 12 +++++- win/tkWinFont.c | 37 ++++++++++++++---- win/tkWinKey.c | 3 +- 11 files changed, 241 insertions(+), 16 deletions(-) diff --git a/generic/tkEntry.c b/generic/tkEntry.c index a66cf18..5faf4ef 100644 --- a/generic/tkEntry.c +++ b/generic/tkEntry.c @@ -1926,7 +1926,6 @@ EntryComputeGeometry( */ if (entryPtr->showChar != NULL) { - Tcl_UniChar ch; char buf[4]; int size; @@ -1936,8 +1935,15 @@ EntryComputeGeometry( * characters might end up looking like one valid UTF character in the * resulting string. */ +#if TCL_UTF_MAX == 4 + int ch; + + TkUtfToUniChar32(entryPtr->showChar, &ch); +#else + Tcl_UniChar ch; Tcl_UtfToUniChar(entryPtr->showChar, &ch); +#endif size = Tcl_UniCharToUtf(ch, buf); entryPtr->numDisplayBytes = entryPtr->numChars * size; @@ -3414,7 +3420,11 @@ ExpandPercents( * list element. */ int number, length; register const char *string; +#if TCL_UTF_MAX == 4 + int ch; +#else Tcl_UniChar ch; +#endif char numStorage[2*TCL_INTEGER_SPACE]; while (1) { @@ -3447,7 +3457,11 @@ ExpandPercents( before++; /* skip over % */ if (*before != '\0') { +#if TCL_UTF_MAX == 4 + before += TkUtfToUniChar32(before, &ch); +#else before += Tcl_UtfToUniChar(before, &ch); +#endif } else { ch = '%'; } diff --git a/generic/tkFont.c b/generic/tkFont.c index 1ffac16..3088959 100644 --- a/generic/tkFont.c +++ b/generic/tkFont.c @@ -497,7 +497,11 @@ Tk_FontObjCmd( const char *s; Tk_Font tkfont; Tcl_Obj *optPtr, *charPtr, *resultPtr; +#if TCL_UTF_MAX == 4 int uniChar = 0; +#else + Tcl_UniChar uniChar = 0; +#endif const TkFontAttributes *faPtr; TkFontAttributes fa; @@ -562,6 +566,10 @@ Tk_FontObjCmd( */ if (charPtr != NULL) { +#if TCL_UTF_MAX == 4 + Tcl_UniChar *ucPtr; +#endif + if (Tcl_GetCharLength(charPtr) != 1) { resultPtr = Tcl_NewStringObj( "expected a single character but got \"", -1); @@ -572,7 +580,18 @@ Tk_FontObjCmd( Tcl_SetErrorCode(interp, "TK", "VALUE", "FONT_SAMPLE", NULL); return TCL_ERROR; } +#if TCL_UTF_MAX == 4 + ucPtr = Tcl_GetUnicodeFromObj(charPtr, NULL); + uniChar = *ucPtr; + if (((uniChar & 0xFC00) == 0xD800) && (ucPtr[1] != 0x000)) { + if ((ucPtr[1] & 0xFC00) == 0xDC00) { + uniChar = ((uniChar & 0x3FF) << 10) + (ucPtr[1] & 0x3FF); + uniChar += 0x10000; + } + } +#else uniChar = Tcl_GetUniChar(charPtr, 0); +#endif } /* @@ -1694,7 +1713,11 @@ Tk_PostscriptFontName( } else if (strcasecmp(family, "ZapfDingbats") == 0) { family = "ZapfDingbats"; } else { +#if TCL_UTF_MAX == 4 + int ch; +#else Tcl_UniChar ch; +#endif /* * Inline, capitalize the first letter of each word, lowercase the @@ -1712,7 +1735,11 @@ Tk_PostscriptFontName( src++; upper = 1; } +#if TCL_UTF_MAX == 4 + src += TkUtfToUniChar32(src, &ch); +#else src += Tcl_UtfToUniChar(src, &ch); +#endif if (upper) { ch = (Tcl_UniChar) Tcl_UniCharToUpper(ch); upper = 0; @@ -3249,7 +3276,11 @@ Tk_TextLayoutToPostscript( int i, j, len; const char *p, *glyphname; char uindex[5], c, *ps; +#if TCL_UTF_MAX == 4 + int ch; +#else Tcl_UniChar ch; +#endif Tcl_AppendToObj(psObj, "[(", -1); for (i = 0; i < layoutPtr->numChunks; i++, chunkPtr++) { @@ -3272,7 +3303,11 @@ Tk_TextLayoutToPostscript( * international postscript fonts. */ +#if TCL_UTF_MAX == 4 + p += TkUtfToUniChar32(p, &ch); +#else p += Tcl_UtfToUniChar(p, &ch); +#endif if ((ch == '(') || (ch == ')') || (ch == '\\') || (ch < 0x20)) { /* * Tricky point: the "03" is necessary in the sprintf below, @@ -3298,6 +3333,11 @@ Tk_TextLayoutToPostscript( * use the full glyph name. */ +#if TCL_UTF_MAX > 3 + if (ch > 0xffff) { + goto noMapping; + } +#endif sprintf(uindex, "%04X", ch); /* endianness? */ glyphname = Tcl_GetVar2(interp, "::tk::psglyphs", uindex, 0); if (glyphname) { @@ -3318,6 +3358,7 @@ Tk_TextLayoutToPostscript( * No known mapping for the character into the space of * PostScript glyphs. Ignore it. :-( */ +noMapping: ; #ifdef TK_DEBUG_POSTSCRIPT_OUTPUT fprintf(stderr, "Warning: no mapping to PostScript " diff --git a/generic/tkInt.h b/generic/tkInt.h index 0b502e4..367ef3a 100644 --- a/generic/tkInt.h +++ b/generic/tkInt.h @@ -1232,6 +1232,10 @@ MODULE_SCOPE Status TkParseColor (Display * display, MODULE_SCOPE void TkUnixSetXftClipRegion(TkRegion clipRegion); #endif +#if TCL_UTF_MAX == 4 +MODULE_SCOPE int TkUtfToUniChar32(const char *src, int *chPtr); +#endif + /* * Unsupported commands. */ diff --git a/generic/tkText.c b/generic/tkText.c index 1227e7b..834e842 100644 --- a/generic/tkText.c +++ b/generic/tkText.c @@ -4508,7 +4508,11 @@ TkTextGetTabs( Tcl_Obj **objv; TkTextTabArray *tabArrayPtr; TkTextTab *tabPtr; +#if TCL_UTF_MAX == 4 + int ch; +#else Tcl_UniChar ch; +#endif double prevStop, lastStop; /* * Map these strings to TkTextTabAlign values. @@ -4615,7 +4619,11 @@ TkTextGetTabs( * There may be a more efficient way of getting this. */ +#if TCL_UTF_MAX == 4 + TkUtfToUniChar32(Tcl_GetString(objv[i+1]), &ch); +#else Tcl_UtfToUniChar(Tcl_GetString(objv[i+1]), &ch); +#endif if (!Tcl_UniCharIsAlpha(ch)) { continue; } diff --git a/generic/tkTextIndex.c b/generic/tkTextIndex.c index 92ca03b..f64a6d2 100644 --- a/generic/tkTextIndex.c +++ b/generic/tkTextIndex.c @@ -2298,9 +2298,13 @@ StartEnd( int chSize = 1; if (segPtr->typePtr == &tkTextCharType) { +#if TCL_UTF_MAX == 4 + int ch; + chSize = TkUtfToUniChar32(segPtr->body.chars + offset, &ch); +#else Tcl_UniChar ch; - chSize = Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch); +#endif if (!Tcl_UniCharIsWordChar(ch)) { break; } @@ -2343,9 +2347,13 @@ StartEnd( int chSize = 1; if (segPtr->typePtr == &tkTextCharType) { +#if TCL_UTF_MAX == 4 + int ch; + TkUtfToUniChar32(segPtr->body.chars + offset, &ch); +#else Tcl_UniChar ch; - Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch); +#endif if (!Tcl_UniCharIsWordChar(ch)) { break; } diff --git a/generic/tkUtil.c b/generic/tkUtil.c index d4c4d2d..fb796fd 100644 --- a/generic/tkUtil.c +++ b/generic/tkUtil.c @@ -1192,6 +1192,108 @@ TkSendVirtualEvent( Tk_QueueWindowEvent(&event.general, TCL_QUEUE_TAIL); } + +#if TCL_UTF_MAX == 4 +/* + *--------------------------------------------------------------------------- + * + * TkUtfToUniChar32 -- + * + * Copied from Tcl_UtfToUniChar but using int instead of Tcl_UniChar! + * + * Extract the Tcl_UniChar represented by the UTF-8 string. Bad UTF-8 + * sequences are converted to valid Tcl_UniChars and processing + * continues. Equivalent to Plan 9 chartorune(). + * + * The caller must ensure that the source buffer is long enough that this + * routine does not run off the end and dereference non-existent memory + * looking for trail bytes. If the source buffer is known to be '\0' + * terminated, this cannot happen. Otherwise, the caller should call + * Tcl_UtfCharComplete() before calling this routine to ensure that + * enough bytes remain in the string. + * + * Results: + * *chPtr is filled with the Tcl_UniChar, and the return value is the + * number of bytes from the UTF-8 string that were consumed. + * + * Side effects: + * None. + * + *--------------------------------------------------------------------------- + */ + +int +TkUtfToUniChar32( + const char *src, /* The UTF-8 string. */ + int *chPtr) /* Filled with the Tcl_UniChar represented by + * the UTF-8 string. */ +{ + int byte; + + /* + * Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones. + */ + + byte = *((unsigned char *) src); + if (byte < 0xC0) { + /* + * Handles properly formed UTF-8 characters between 0x01 and 0x7F. + * Also treats \0 and naked trail bytes 0x80 to 0xBF as valid + * characters representing themselves. + */ + + *chPtr = byte; + return 1; + } else if (byte < 0xE0) { + if ((src[1] & 0xC0) == 0x80) { + /* + * Two-byte-character lead-byte followed by a trail-byte. + */ + + *chPtr = ((byte & 0x1F) << 6) | (src[1] & 0x3F); + return 2; + } + + /* + * A two-byte-character lead-byte not followed by trail-byte + * represents itself. + */ + } else if (byte < 0xF0) { + if (((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80)) { + /* + * Three-byte-character lead byte followed by two trail bytes. + */ + + *chPtr = ((byte & 0x0F) << 12) + | ((src[1] & 0x3F) << 6) | (src[2] & 0x3F); + return 3; + } + + /* + * A three-byte-character lead-byte not followed by two trail-bytes + * represents itself. + */ + } else if (byte < 0xF8) { + if (((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80) && ((src[3] & 0xC0) == 0x80)) { + /* + * Four-byte-character lead byte followed by three trail bytes. + */ + + *chPtr = ((byte & 0x0E) << 18) | ((src[1] & 0x3F) << 12) + | ((src[2] & 0x3F) << 6) | (src[3] & 0x3F); + return 4; + } + + /* + * A three-byte-character lead-byte not followed by two trail-bytes + * represents itself. + */ + } + + *chPtr = byte; + return 1; +} +#endif /* * Local Variables: * mode: c diff --git a/generic/ttk/ttkEntry.c b/generic/ttk/ttkEntry.c index d80e1fd..63ebc5f 100644 --- a/generic/ttk/ttkEntry.c +++ b/generic/ttk/ttkEntry.c @@ -282,10 +282,16 @@ static char *EntryDisplayString(const char *showChar, int numChars) { char *displayString, *p; int size; - Tcl_UniChar ch; char buf[4]; +#if TCL_UTF_MAX == 4 + int ch; + + TkUtfToUniChar32(showChar, &ch); +#else + Tcl_UniChar ch; Tcl_UtfToUniChar(showChar, &ch); +#endif size = Tcl_UniCharToUtf(ch, buf); p = displayString = ckalloc(numChars * size + 1); @@ -406,7 +412,11 @@ ExpandPercents( int number, length; const char *string; int stringLength; +#if TCL_UTF_MAX == 4 + int ch; +#else Tcl_UniChar ch; +#endif char numStorage[2*TCL_INTEGER_SPACE]; while (*template) { @@ -430,7 +440,11 @@ ExpandPercents( */ ++template; /* skip over % */ if (*template != '\0') { +#if TCL_UTF_MAX == 4 + template += TkUtfToUniChar32(template, &ch); +#else template += Tcl_UtfToUniChar(template, &ch); +#endif } else { ch = '%'; } diff --git a/unix/tkUnixFont.c b/unix/tkUnixFont.c index a6826b5..b10fddd 100644 --- a/unix/tkUnixFont.c +++ b/unix/tkUnixFont.c @@ -35,9 +35,13 @@ static const char *const encodingList[] = { * family": the foundry, face name, and charset. */ +#if TCL_UTF_MAX > 3 +#define FONTMAP_SHIFT 12 +#define FONTMAP_PAGES (1 << (21 - FONTMAP_SHIFT)) +#else #define FONTMAP_SHIFT 10 - #define FONTMAP_PAGES (1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT)) +#endif #define FONTMAP_BITSPERPAGE (1 << FONTMAP_SHIFT) typedef struct FontFamily { diff --git a/unix/tkUnixRFont.c b/unix/tkUnixRFont.c index 01bbb30..ee87657 100644 --- a/unix/tkUnixRFont.c +++ b/unix/tkUnixRFont.c @@ -54,6 +54,12 @@ typedef struct ThreadSpecificData { Region clipRegion; /* The clipping region, or None. */ } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; + +#if TCL_UTF_MAX == 4 +#define UtfToUniChar(src, chPtr) TkUtfToUniChar32(src, chPtr) +#else +#define UtfToUniChar(src, chPtr) Tcl_UtfToUniChar(src, chPtr) +#endif /* * Package initialization: @@ -668,9 +674,13 @@ Tk_MeasureChars( curByte = 0; sawNonSpace = 0; while (numBytes > 0) { +#if TCL_UTF_MAX == 4 + int unichar; +#else Tcl_UniChar unichar; +#endif - clen = Tcl_UtfToUniChar(source, &unichar); + clen = UtfToUniChar(source, &unichar); c = (FcChar32) unichar; if (clen <= 0) { diff --git a/win/tkWinFont.c b/win/tkWinFont.c index 10ea1b9..e413e7d 100644 --- a/win/tkWinFont.c +++ b/win/tkWinFont.c @@ -26,9 +26,13 @@ * Under Windows, a "font family" is uniquely identified by its face name. */ -#define FONTMAP_SHIFT 10 - -#define FONTMAP_PAGES (1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT)) +#if TCL_UTF_MAX > 3 +#define FONTMAP_SHIFT 12 +#define FONTMAP_PAGES (1 << (21 - FONTMAP_SHIFT)) +#else +#define FONTMAP_SHIFT 10 +#define FONTMAP_PAGES (1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT)) +#endif #define FONTMAP_BITSPERPAGE (1 << FONTMAP_SHIFT) typedef struct FontFamily { @@ -229,6 +233,11 @@ static inline HFONT SelectFont(HDC hdc, WinFont *fontPtr, SubFont *subFontPtr, double angle); static inline void SwapLong(PULONG p); static inline void SwapShort(USHORT *p); +#if TCL_UTF_MAX == 4 +#define UtfToUniChar(src, chPtr) TkUtfToUniChar32(src, chPtr) +#else +#define UtfToUniChar(src, chPtr) Tcl_UtfToUniChar(src, chPtr) +#endif static int CALLBACK WinFontCanUseProc(ENUMLOGFONT *lfPtr, NEWTEXTMETRIC *tmPtr, int fontType, LPARAM lParam); @@ -828,7 +837,11 @@ Tk_MeasureChars( HFONT oldFont; WinFont *fontPtr; int curX, moretomeasure; +#if TCL_UTF_MAX == 4 + int ch; +#else Tcl_UniChar ch; +#endif SIZE size; FontFamily *familyPtr; Tcl_DString runString; @@ -859,7 +872,7 @@ Tk_MeasureChars( start = source; end = start + numBytes; for (p = start; p < end; ) { - next = p + Tcl_UtfToUniChar(p, &ch); + next = p + UtfToUniChar(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); if (thisSubFontPtr != lastSubFontPtr) { familyPtr = lastSubFontPtr->familyPtr; @@ -921,7 +934,7 @@ Tk_MeasureChars( familyPtr = lastSubFontPtr->familyPtr; Tcl_DStringInit(&runString); for (p = start; p < end; ) { - next = p + Tcl_UtfToUniChar(p, &ch); + next = p + UtfToUniChar(p, &ch); Tcl_UtfToExternal(NULL, familyPtr->encoding, p, (int) (next - p), 0, NULL, buf, sizeof(buf), NULL, &dstWrote, NULL); @@ -970,13 +983,17 @@ Tk_MeasureChars( */ const char *lastWordBreak = NULL; +#if TCL_UTF_MAX == 4 + int ch2; +#else Tcl_UniChar ch2; +#endif end = p; p = source; ch = ' '; while (p < end) { - next = p + Tcl_UtfToUniChar(p, &ch2); + next = p + UtfToUniChar(p, &ch2); if ((ch != ' ') && (ch2 == ' ')) { lastWordBreak = p; } @@ -1443,7 +1460,11 @@ MultiFontTextOut( * string when drawing. */ double angle) { +#if TCL_UTF_MAX == 4 + int ch; +#else Tcl_UniChar ch; +#endif SIZE size; HFONT oldFont; FontFamily *familyPtr; @@ -1458,7 +1479,7 @@ MultiFontTextOut( end = source + numBytes; for (p = source; p < end; ) { - next = p + Tcl_UtfToUniChar(p, &ch); + next = p + UtfToUniChar(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); if (thisSubFontPtr != lastSubFontPtr) { if (p > source) { @@ -2188,7 +2209,7 @@ FontMapLoadPage( { FontFamily *familyPtr; Tcl_Encoding encoding; - char src[XMaxTransChars], buf[16]; + char src[TCL_UTF_MAX], buf[16]; USHORT *startCount, *endCount; int i, j, bitOffset, end, segCount; diff --git a/win/tkWinKey.c b/win/tkWinKey.c index 2698c4d..815ff3b 100644 --- a/win/tkWinKey.c +++ b/win/tkWinKey.c @@ -113,7 +113,6 @@ TkpGetString( Tcl_DStringAppend(dsPtr, buf, len); } else if (keyEv->send_event == -3) { - char buf[XMaxTransChars]; int len; @@ -122,7 +121,7 @@ TkpGetString( */ len = Tcl_UniCharToUtf(keyEv->keycode, buf); - if ((keyEv->keycode <= 0xffff) || (len == XMaxTransChars)) { + if ((keyEv->keycode <= 0xffff) || (len > 3)) { Tcl_DStringAppend(dsPtr, buf, len); } else { Tcl_UniCharToUtf(((keyEv->keycode - 0x10000) >> 10) | 0xd800, buf); -- cgit v0.12 From 7a5d64437977cc14b7b79e336b3b2a20785348e5 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Fri, 16 Sep 2016 12:17:59 +0000 Subject: Rename TkUtfToUniChar32 to TkUtfToUniChar2, and various simplifications needing less #ifdef's --- generic/tkEntry.c | 16 +------- generic/tkFont.c | 47 +++--------------------- generic/tkInt.h | 6 ++- generic/tkText.c | 10 +---- generic/tkTextDisp.c | 4 +- generic/tkTextIndex.c | 16 ++------ generic/tkUtil.c | 99 +++++++++++--------------------------------------- generic/ttk/ttkEntry.c | 16 +------- unix/tkUnixFont.c | 5 --- unix/tkUnixRFont.c | 12 +----- win/tkWinFont.c | 32 +++------------- win/tkWinKey.c | 3 +- 12 files changed, 49 insertions(+), 217 deletions(-) diff --git a/generic/tkEntry.c b/generic/tkEntry.c index 5faf4ef..a66cf18 100644 --- a/generic/tkEntry.c +++ b/generic/tkEntry.c @@ -1926,6 +1926,7 @@ EntryComputeGeometry( */ if (entryPtr->showChar != NULL) { + Tcl_UniChar ch; char buf[4]; int size; @@ -1935,15 +1936,8 @@ EntryComputeGeometry( * characters might end up looking like one valid UTF character in the * resulting string. */ -#if TCL_UTF_MAX == 4 - int ch; - - TkUtfToUniChar32(entryPtr->showChar, &ch); -#else - Tcl_UniChar ch; Tcl_UtfToUniChar(entryPtr->showChar, &ch); -#endif size = Tcl_UniCharToUtf(ch, buf); entryPtr->numDisplayBytes = entryPtr->numChars * size; @@ -3420,11 +3414,7 @@ ExpandPercents( * list element. */ int number, length; register const char *string; -#if TCL_UTF_MAX == 4 - int ch; -#else Tcl_UniChar ch; -#endif char numStorage[2*TCL_INTEGER_SPACE]; while (1) { @@ -3457,11 +3447,7 @@ ExpandPercents( before++; /* skip over % */ if (*before != '\0') { -#if TCL_UTF_MAX == 4 - before += TkUtfToUniChar32(before, &ch); -#else before += Tcl_UtfToUniChar(before, &ch); -#endif } else { ch = '%'; } diff --git a/generic/tkFont.c b/generic/tkFont.c index 3088959..4a45691 100644 --- a/generic/tkFont.c +++ b/generic/tkFont.c @@ -497,11 +497,7 @@ Tk_FontObjCmd( const char *s; Tk_Font tkfont; Tcl_Obj *optPtr, *charPtr, *resultPtr; -#if TCL_UTF_MAX == 4 int uniChar = 0; -#else - Tcl_UniChar uniChar = 0; -#endif const TkFontAttributes *faPtr; TkFontAttributes fa; @@ -566,32 +562,19 @@ Tk_FontObjCmd( */ if (charPtr != NULL) { -#if TCL_UTF_MAX == 4 - Tcl_UniChar *ucPtr; -#endif + const char *string = Tcl_GetString(charPtr); + int len = TkUtfToUniChar2(string, &uniChar); - if (Tcl_GetCharLength(charPtr) != 1) { + if (len != charPtr->length) { resultPtr = Tcl_NewStringObj( "expected a single character but got \"", -1); - Tcl_AppendLimitedToObj(resultPtr, Tcl_GetString(charPtr), + Tcl_AppendLimitedToObj(resultPtr, string, -1, 40, "..."); Tcl_AppendToObj(resultPtr, "\"", -1); Tcl_SetObjResult(interp, resultPtr); Tcl_SetErrorCode(interp, "TK", "VALUE", "FONT_SAMPLE", NULL); return TCL_ERROR; } -#if TCL_UTF_MAX == 4 - ucPtr = Tcl_GetUnicodeFromObj(charPtr, NULL); - uniChar = *ucPtr; - if (((uniChar & 0xFC00) == 0xD800) && (ucPtr[1] != 0x000)) { - if ((ucPtr[1] & 0xFC00) == 0xDC00) { - uniChar = ((uniChar & 0x3FF) << 10) + (ucPtr[1] & 0x3FF); - uniChar += 0x10000; - } - } -#else - uniChar = Tcl_GetUniChar(charPtr, 0); -#endif } /* @@ -1713,11 +1696,7 @@ Tk_PostscriptFontName( } else if (strcasecmp(family, "ZapfDingbats") == 0) { family = "ZapfDingbats"; } else { -#if TCL_UTF_MAX == 4 int ch; -#else - Tcl_UniChar ch; -#endif /* * Inline, capitalize the first letter of each word, lowercase the @@ -1735,11 +1714,7 @@ Tk_PostscriptFontName( src++; upper = 1; } -#if TCL_UTF_MAX == 4 - src += TkUtfToUniChar32(src, &ch); -#else - src += Tcl_UtfToUniChar(src, &ch); -#endif + src += TkUtfToUniChar2(src, &ch); if (upper) { ch = (Tcl_UniChar) Tcl_UniCharToUpper(ch); upper = 0; @@ -3276,11 +3251,7 @@ Tk_TextLayoutToPostscript( int i, j, len; const char *p, *glyphname; char uindex[5], c, *ps; -#if TCL_UTF_MAX == 4 int ch; -#else - Tcl_UniChar ch; -#endif Tcl_AppendToObj(psObj, "[(", -1); for (i = 0; i < layoutPtr->numChunks; i++, chunkPtr++) { @@ -3303,11 +3274,7 @@ Tk_TextLayoutToPostscript( * international postscript fonts. */ -#if TCL_UTF_MAX == 4 - p += TkUtfToUniChar32(p, &ch); -#else - p += Tcl_UtfToUniChar(p, &ch); -#endif + p += TkUtfToUniChar2(p, &ch); if ((ch == '(') || (ch == ')') || (ch == '\\') || (ch < 0x20)) { /* * Tricky point: the "03" is necessary in the sprintf below, @@ -3333,11 +3300,9 @@ Tk_TextLayoutToPostscript( * use the full glyph name. */ -#if TCL_UTF_MAX > 3 if (ch > 0xffff) { goto noMapping; } -#endif sprintf(uindex, "%04X", ch); /* endianness? */ glyphname = Tcl_GetVar2(interp, "::tk::psglyphs", uindex, 0); if (glyphname) { diff --git a/generic/tkInt.h b/generic/tkInt.h index 367ef3a..6d86e08 100644 --- a/generic/tkInt.h +++ b/generic/tkInt.h @@ -1232,8 +1232,10 @@ MODULE_SCOPE Status TkParseColor (Display * display, MODULE_SCOPE void TkUnixSetXftClipRegion(TkRegion clipRegion); #endif -#if TCL_UTF_MAX == 4 -MODULE_SCOPE int TkUtfToUniChar32(const char *src, int *chPtr); +#if TCL_UTF_MAX > 4 +# define TkUtfToUniChar2 Tcl_UtfToUniChar +#else + MODULE_SCOPE int TkUtfToUniChar2(const char *src, int *chPtr); #endif /* diff --git a/generic/tkText.c b/generic/tkText.c index 834e842..dacadbe 100644 --- a/generic/tkText.c +++ b/generic/tkText.c @@ -4508,11 +4508,7 @@ TkTextGetTabs( Tcl_Obj **objv; TkTextTabArray *tabArrayPtr; TkTextTab *tabPtr; -#if TCL_UTF_MAX == 4 int ch; -#else - Tcl_UniChar ch; -#endif double prevStop, lastStop; /* * Map these strings to TkTextTabAlign values. @@ -4619,11 +4615,7 @@ TkTextGetTabs( * There may be a more efficient way of getting this. */ -#if TCL_UTF_MAX == 4 - TkUtfToUniChar32(Tcl_GetString(objv[i+1]), &ch); -#else - Tcl_UtfToUniChar(Tcl_GetString(objv[i+1]), &ch); -#endif + TkUtfToUniChar2(Tcl_GetString(objv[i+1]), &ch); if (!Tcl_UniCharIsAlpha(ch)) { continue; } diff --git a/generic/tkTextDisp.c b/generic/tkTextDisp.c index 5faab36..026023e 100644 --- a/generic/tkTextDisp.c +++ b/generic/tkTextDisp.c @@ -7581,8 +7581,8 @@ TkTextCharLayoutProc( if (bytesThatFit < maxBytes) { if ((bytesThatFit == 0) && noCharsYet) { - Tcl_UniChar ch; - int chLen = Tcl_UtfToUniChar(p, &ch); + int ch; + int chLen = TkUtfToUniChar2(p, &ch); #if TK_LAYOUT_WITH_BASE_CHUNKS bytesThatFit = CharChunkMeasureChars(chunkPtr, line, diff --git a/generic/tkTextIndex.c b/generic/tkTextIndex.c index f64a6d2..b794cdb 100644 --- a/generic/tkTextIndex.c +++ b/generic/tkTextIndex.c @@ -2298,13 +2298,9 @@ StartEnd( int chSize = 1; if (segPtr->typePtr == &tkTextCharType) { -#if TCL_UTF_MAX == 4 int ch; - chSize = TkUtfToUniChar32(segPtr->body.chars + offset, &ch); -#else - Tcl_UniChar ch; - chSize = Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch); -#endif + + chSize = TkUtfToUniChar2(segPtr->body.chars + offset, &ch); if (!Tcl_UniCharIsWordChar(ch)) { break; } @@ -2347,13 +2343,9 @@ StartEnd( int chSize = 1; if (segPtr->typePtr == &tkTextCharType) { -#if TCL_UTF_MAX == 4 + int ch; - TkUtfToUniChar32(segPtr->body.chars + offset, &ch); -#else - Tcl_UniChar ch; - Tcl_UtfToUniChar(segPtr->body.chars + offset, &ch); -#endif + TkUtfToUniChar2(segPtr->body.chars + offset, &ch); if (!Tcl_UniCharIsWordChar(ch)) { break; } diff --git a/generic/tkUtil.c b/generic/tkUtil.c index fb796fd..a266cb3 100644 --- a/generic/tkUtil.c +++ b/generic/tkUtil.c @@ -1193,24 +1193,15 @@ TkSendVirtualEvent( Tk_QueueWindowEvent(&event.general, TCL_QUEUE_TAIL); } -#if TCL_UTF_MAX == 4 +#if TCL_UTF_MAX <= 4 /* *--------------------------------------------------------------------------- * - * TkUtfToUniChar32 -- + * TkUtfToUniChar2 -- * - * Copied from Tcl_UtfToUniChar but using int instead of Tcl_UniChar! - * - * Extract the Tcl_UniChar represented by the UTF-8 string. Bad UTF-8 - * sequences are converted to valid Tcl_UniChars and processing - * continues. Equivalent to Plan 9 chartorune(). - * - * The caller must ensure that the source buffer is long enough that this - * routine does not run off the end and dereference non-existent memory - * looking for trail bytes. If the source buffer is known to be '\0' - * terminated, this cannot happen. Otherwise, the caller should call - * Tcl_UtfCharComplete() before calling this routine to ensure that - * enough bytes remain in the string. + * Almost the same as Tcl_UtfToUniChar but using int instead of Tcl_UniChar. + * This function is capable of collapsing a upper/lower pair to a single + * unicode character. So, up to 6 bytes (two UTF-8 characters) might be read. * * Results: * *chPtr is filled with the Tcl_UniChar, and the return value is the @@ -1223,75 +1214,29 @@ TkSendVirtualEvent( */ int -TkUtfToUniChar32( +TkUtfToUniChar2( const char *src, /* The UTF-8 string. */ int *chPtr) /* Filled with the Tcl_UniChar represented by * the UTF-8 string. */ { - int byte; - - /* - * Unroll 1 to 3 byte UTF-8 sequences, use loop to handle longer ones. - */ - - byte = *((unsigned char *) src); - if (byte < 0xC0) { - /* - * Handles properly formed UTF-8 characters between 0x01 and 0x7F. - * Also treats \0 and naked trail bytes 0x80 to 0xBF as valid - * characters representing themselves. - */ - - *chPtr = byte; - return 1; - } else if (byte < 0xE0) { - if ((src[1] & 0xC0) == 0x80) { - /* - * Two-byte-character lead-byte followed by a trail-byte. - */ - - *chPtr = ((byte & 0x1F) << 6) | (src[1] & 0x3F); - return 2; + Tcl_UniChar uniChar = 0; + + int len = Tcl_UtfToUniChar(src, &uniChar); + if ((uniChar & 0xfc00) == 0xd800) { + Tcl_UniChar high = uniChar; + /* This can only happen when Tcl is compiled with TCL_UTF_MAX=4, + * or when a high surrogate character is detected */ + int len2 = Tcl_UtfToUniChar(src+len, &uniChar); + if ((uniChar & 0xfc00) == 0xdc00) { + *chPtr = ((high & 0x3ff) << 10) | (uniChar & 0x3ff) | 0x10000; + len += len2; + } else { + *chPtr = high; } - - /* - * A two-byte-character lead-byte not followed by trail-byte - * represents itself. - */ - } else if (byte < 0xF0) { - if (((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80)) { - /* - * Three-byte-character lead byte followed by two trail bytes. - */ - - *chPtr = ((byte & 0x0F) << 12) - | ((src[1] & 0x3F) << 6) | (src[2] & 0x3F); - return 3; - } - - /* - * A three-byte-character lead-byte not followed by two trail-bytes - * represents itself. - */ - } else if (byte < 0xF8) { - if (((src[1] & 0xC0) == 0x80) && ((src[2] & 0xC0) == 0x80) && ((src[3] & 0xC0) == 0x80)) { - /* - * Four-byte-character lead byte followed by three trail bytes. - */ - - *chPtr = ((byte & 0x0E) << 18) | ((src[1] & 0x3F) << 12) - | ((src[2] & 0x3F) << 6) | (src[3] & 0x3F); - return 4; - } - - /* - * A three-byte-character lead-byte not followed by two trail-bytes - * represents itself. - */ + } else { + *chPtr = uniChar; } - - *chPtr = byte; - return 1; + return len; } #endif /* diff --git a/generic/ttk/ttkEntry.c b/generic/ttk/ttkEntry.c index 63ebc5f..d80e1fd 100644 --- a/generic/ttk/ttkEntry.c +++ b/generic/ttk/ttkEntry.c @@ -282,16 +282,10 @@ static char *EntryDisplayString(const char *showChar, int numChars) { char *displayString, *p; int size; - char buf[4]; -#if TCL_UTF_MAX == 4 - int ch; - - TkUtfToUniChar32(showChar, &ch); -#else Tcl_UniChar ch; + char buf[4]; Tcl_UtfToUniChar(showChar, &ch); -#endif size = Tcl_UniCharToUtf(ch, buf); p = displayString = ckalloc(numChars * size + 1); @@ -412,11 +406,7 @@ ExpandPercents( int number, length; const char *string; int stringLength; -#if TCL_UTF_MAX == 4 - int ch; -#else Tcl_UniChar ch; -#endif char numStorage[2*TCL_INTEGER_SPACE]; while (*template) { @@ -440,11 +430,7 @@ ExpandPercents( */ ++template; /* skip over % */ if (*template != '\0') { -#if TCL_UTF_MAX == 4 - template += TkUtfToUniChar32(template, &ch); -#else template += Tcl_UtfToUniChar(template, &ch); -#endif } else { ch = '%'; } diff --git a/unix/tkUnixFont.c b/unix/tkUnixFont.c index b10fddd..681d1d1 100644 --- a/unix/tkUnixFont.c +++ b/unix/tkUnixFont.c @@ -35,13 +35,8 @@ static const char *const encodingList[] = { * family": the foundry, face name, and charset. */ -#if TCL_UTF_MAX > 3 #define FONTMAP_SHIFT 12 #define FONTMAP_PAGES (1 << (21 - FONTMAP_SHIFT)) -#else -#define FONTMAP_SHIFT 10 -#define FONTMAP_PAGES (1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT)) -#endif #define FONTMAP_BITSPERPAGE (1 << FONTMAP_SHIFT) typedef struct FontFamily { diff --git a/unix/tkUnixRFont.c b/unix/tkUnixRFont.c index ee87657..0ea1ec6 100644 --- a/unix/tkUnixRFont.c +++ b/unix/tkUnixRFont.c @@ -54,12 +54,6 @@ typedef struct ThreadSpecificData { Region clipRegion; /* The clipping region, or None. */ } ThreadSpecificData; static Tcl_ThreadDataKey dataKey; - -#if TCL_UTF_MAX == 4 -#define UtfToUniChar(src, chPtr) TkUtfToUniChar32(src, chPtr) -#else -#define UtfToUniChar(src, chPtr) Tcl_UtfToUniChar(src, chPtr) -#endif /* * Package initialization: @@ -674,13 +668,9 @@ Tk_MeasureChars( curByte = 0; sawNonSpace = 0; while (numBytes > 0) { -#if TCL_UTF_MAX == 4 int unichar; -#else - Tcl_UniChar unichar; -#endif - clen = UtfToUniChar(source, &unichar); + clen = TkUtfToUniChar2(source, &unichar); c = (FcChar32) unichar; if (clen <= 0) { diff --git a/win/tkWinFont.c b/win/tkWinFont.c index e413e7d..30638ca 100644 --- a/win/tkWinFont.c +++ b/win/tkWinFont.c @@ -26,13 +26,8 @@ * Under Windows, a "font family" is uniquely identified by its face name. */ -#if TCL_UTF_MAX > 3 #define FONTMAP_SHIFT 12 #define FONTMAP_PAGES (1 << (21 - FONTMAP_SHIFT)) -#else -#define FONTMAP_SHIFT 10 -#define FONTMAP_PAGES (1 << (sizeof(Tcl_UniChar)*8 - FONTMAP_SHIFT)) -#endif #define FONTMAP_BITSPERPAGE (1 << FONTMAP_SHIFT) typedef struct FontFamily { @@ -233,11 +228,6 @@ static inline HFONT SelectFont(HDC hdc, WinFont *fontPtr, SubFont *subFontPtr, double angle); static inline void SwapLong(PULONG p); static inline void SwapShort(USHORT *p); -#if TCL_UTF_MAX == 4 -#define UtfToUniChar(src, chPtr) TkUtfToUniChar32(src, chPtr) -#else -#define UtfToUniChar(src, chPtr) Tcl_UtfToUniChar(src, chPtr) -#endif static int CALLBACK WinFontCanUseProc(ENUMLOGFONT *lfPtr, NEWTEXTMETRIC *tmPtr, int fontType, LPARAM lParam); @@ -837,11 +827,7 @@ Tk_MeasureChars( HFONT oldFont; WinFont *fontPtr; int curX, moretomeasure; -#if TCL_UTF_MAX == 4 int ch; -#else - Tcl_UniChar ch; -#endif SIZE size; FontFamily *familyPtr; Tcl_DString runString; @@ -872,7 +858,7 @@ Tk_MeasureChars( start = source; end = start + numBytes; for (p = start; p < end; ) { - next = p + UtfToUniChar(p, &ch); + next = p + TkUtfToUniChar2(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); if (thisSubFontPtr != lastSubFontPtr) { familyPtr = lastSubFontPtr->familyPtr; @@ -934,7 +920,7 @@ Tk_MeasureChars( familyPtr = lastSubFontPtr->familyPtr; Tcl_DStringInit(&runString); for (p = start; p < end; ) { - next = p + UtfToUniChar(p, &ch); + next = p + TkUtfToUniChar2(p, &ch); Tcl_UtfToExternal(NULL, familyPtr->encoding, p, (int) (next - p), 0, NULL, buf, sizeof(buf), NULL, &dstWrote, NULL); @@ -983,17 +969,13 @@ Tk_MeasureChars( */ const char *lastWordBreak = NULL; -#if TCL_UTF_MAX == 4 int ch2; -#else - Tcl_UniChar ch2; -#endif end = p; p = source; ch = ' '; while (p < end) { - next = p + UtfToUniChar(p, &ch2); + next = p + TkUtfToUniChar2(p, &ch2); if ((ch != ' ') && (ch2 == ' ')) { lastWordBreak = p; } @@ -1460,11 +1442,7 @@ MultiFontTextOut( * string when drawing. */ double angle) { -#if TCL_UTF_MAX == 4 int ch; -#else - Tcl_UniChar ch; -#endif SIZE size; HFONT oldFont; FontFamily *familyPtr; @@ -1479,7 +1457,7 @@ MultiFontTextOut( end = source + numBytes; for (p = source; p < end; ) { - next = p + UtfToUniChar(p, &ch); + next = p + TkUtfToUniChar2(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); if (thisSubFontPtr != lastSubFontPtr) { if (p > source) { @@ -2209,7 +2187,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 815ff3b..2698c4d 100644 --- a/win/tkWinKey.c +++ b/win/tkWinKey.c @@ -113,6 +113,7 @@ TkpGetString( Tcl_DStringAppend(dsPtr, buf, len); } else if (keyEv->send_event == -3) { + char buf[XMaxTransChars]; int len; @@ -121,7 +122,7 @@ TkpGetString( */ len = Tcl_UniCharToUtf(keyEv->keycode, buf); - if ((keyEv->keycode <= 0xffff) || (len > 3)) { + if ((keyEv->keycode <= 0xffff) || (len == XMaxTransChars)) { Tcl_DStringAppend(dsPtr, buf, len); } else { Tcl_UniCharToUtf(((keyEv->keycode - 0x10000) >> 10) | 0xd800, buf); -- cgit v0.12 From bdb580aa2563588e6e1f5090bb9b89369e2f2d4f Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Mon, 19 Sep 2016 10:14:52 +0000 Subject: More simplifications --- generic/tkEntry.c | 8 ++++---- generic/tkFont.c | 6 +++--- generic/tkInt.h | 6 ++++-- generic/tkText.c | 6 +++--- generic/tkTextDisp.c | 2 +- generic/tkTextIndex.c | 4 ++-- generic/tkUtil.c | 37 +++++++++++++++++++++++++++++++++++-- generic/ttk/ttkEntry.c | 14 +++++++------- unix/tkUnixFont.c | 20 ++++++++++++++------ unix/tkUnixRFont.c | 2 +- win/tkWinFont.c | 8 ++++---- win/tkWinKey.c | 38 +++++--------------------------------- win/tkWinX.c | 13 ++++++------- 13 files changed, 89 insertions(+), 75 deletions(-) diff --git a/generic/tkEntry.c b/generic/tkEntry.c index a66cf18..34f11d2 100644 --- a/generic/tkEntry.c +++ b/generic/tkEntry.c @@ -1926,8 +1926,8 @@ EntryComputeGeometry( */ if (entryPtr->showChar != NULL) { - Tcl_UniChar ch; - char buf[4]; + int ch; + char buf[6]; int size; /* @@ -1937,8 +1937,8 @@ EntryComputeGeometry( * resulting string. */ - Tcl_UtfToUniChar(entryPtr->showChar, &ch); - size = Tcl_UniCharToUtf(ch, buf); + TkUtfToUniChar(entryPtr->showChar, &ch); + size = TkUniCharToUtf(ch, buf); entryPtr->numDisplayBytes = entryPtr->numChars * size; p = ckalloc(entryPtr->numDisplayBytes + 1); diff --git a/generic/tkFont.c b/generic/tkFont.c index 4a45691..1947666 100644 --- a/generic/tkFont.c +++ b/generic/tkFont.c @@ -563,7 +563,7 @@ Tk_FontObjCmd( if (charPtr != NULL) { const char *string = Tcl_GetString(charPtr); - int len = TkUtfToUniChar2(string, &uniChar); + int len = TkUtfToUniChar(string, &uniChar); if (len != charPtr->length) { resultPtr = Tcl_NewStringObj( @@ -1714,7 +1714,7 @@ Tk_PostscriptFontName( src++; upper = 1; } - src += TkUtfToUniChar2(src, &ch); + src += TkUtfToUniChar(src, &ch); if (upper) { ch = (Tcl_UniChar) Tcl_UniCharToUpper(ch); upper = 0; @@ -3274,7 +3274,7 @@ Tk_TextLayoutToPostscript( * international postscript fonts. */ - p += TkUtfToUniChar2(p, &ch); + p += TkUtfToUniChar(p, &ch); if ((ch == '(') || (ch == ')') || (ch == '\\') || (ch < 0x20)) { /* * Tricky point: the "03" is necessary in the sprintf below, diff --git a/generic/tkInt.h b/generic/tkInt.h index 6d86e08..1615a81 100644 --- a/generic/tkInt.h +++ b/generic/tkInt.h @@ -1233,9 +1233,11 @@ MODULE_SCOPE void TkUnixSetXftClipRegion(TkRegion clipRegion); #endif #if TCL_UTF_MAX > 4 -# define TkUtfToUniChar2 Tcl_UtfToUniChar +# define TkUtfToUniChar Tcl_UtfToUniChar +# define TkUniCharToUtf Tcl_UniCharToUtf #else - MODULE_SCOPE int TkUtfToUniChar2(const char *src, int *chPtr); + MODULE_SCOPE int TkUtfToUniChar(const char *, int *); + MODULE_SCOPE int TkUniCharToUtf(int, char *); #endif /* diff --git a/generic/tkText.c b/generic/tkText.c index dacadbe..8ae17a5 100644 --- a/generic/tkText.c +++ b/generic/tkText.c @@ -4615,7 +4615,7 @@ TkTextGetTabs( * There may be a more efficient way of getting this. */ - TkUtfToUniChar2(Tcl_GetString(objv[i+1]), &ch); + TkUtfToUniChar(Tcl_GetString(objv[i+1]), &ch); if (!Tcl_UniCharIsAlpha(ch)) { continue; } @@ -5966,7 +5966,7 @@ SearchCore( CLANG_ASSERT(pattern); do { - Tcl_UniChar ch; + int ch; const char *p; int lastFullLine = lastOffset; @@ -6196,7 +6196,7 @@ SearchCore( } } else { firstOffset = p - startOfLine + - Tcl_UtfToUniChar(startOfLine+matchOffset,&ch); + TkUtfToUniChar(startOfLine+matchOffset,&ch); } } } while (searchSpecPtr->all); diff --git a/generic/tkTextDisp.c b/generic/tkTextDisp.c index 026023e..eb917cf 100644 --- a/generic/tkTextDisp.c +++ b/generic/tkTextDisp.c @@ -7582,7 +7582,7 @@ TkTextCharLayoutProc( if (bytesThatFit < maxBytes) { if ((bytesThatFit == 0) && noCharsYet) { int ch; - int chLen = TkUtfToUniChar2(p, &ch); + int chLen = TkUtfToUniChar(p, &ch); #if TK_LAYOUT_WITH_BASE_CHUNKS bytesThatFit = CharChunkMeasureChars(chunkPtr, line, diff --git a/generic/tkTextIndex.c b/generic/tkTextIndex.c index b794cdb..7aebc84 100644 --- a/generic/tkTextIndex.c +++ b/generic/tkTextIndex.c @@ -2300,7 +2300,7 @@ StartEnd( if (segPtr->typePtr == &tkTextCharType) { int ch; - chSize = TkUtfToUniChar2(segPtr->body.chars + offset, &ch); + chSize = TkUtfToUniChar(segPtr->body.chars + offset, &ch); if (!Tcl_UniCharIsWordChar(ch)) { break; } @@ -2345,7 +2345,7 @@ StartEnd( if (segPtr->typePtr == &tkTextCharType) { int ch; - TkUtfToUniChar2(segPtr->body.chars + offset, &ch); + TkUtfToUniChar(segPtr->body.chars + offset, &ch); if (!Tcl_UniCharIsWordChar(ch)) { break; } diff --git a/generic/tkUtil.c b/generic/tkUtil.c index a266cb3..19b343e 100644 --- a/generic/tkUtil.c +++ b/generic/tkUtil.c @@ -1197,7 +1197,7 @@ TkSendVirtualEvent( /* *--------------------------------------------------------------------------- * - * TkUtfToUniChar2 -- + * TkUtfToUniChar -- * * Almost the same as Tcl_UtfToUniChar but using int instead of Tcl_UniChar. * This function is capable of collapsing a upper/lower pair to a single @@ -1214,7 +1214,7 @@ TkSendVirtualEvent( */ int -TkUtfToUniChar2( +TkUtfToUniChar( const char *src, /* The UTF-8 string. */ int *chPtr) /* Filled with the Tcl_UniChar represented by * the UTF-8 string. */ @@ -1238,6 +1238,39 @@ TkUtfToUniChar2( } return len; } + +/* + *--------------------------------------------------------------------------- + * + * TkUniCharToUtf -- + * + * Almost the same as Tcl_UniCharToUtf but producing surrogates if + * TCL_UTF_MAX==3. + * + * Results: + * *buf is filled with the UTF-8 string, and the return value is the + * number of bytes produced. + * + * Side effects: + * None. + * + *--------------------------------------------------------------------------- + */ + +int TkUniCharToUtf(int ch, char *buf) +{ + int size = Tcl_UniCharToUtf(ch, buf); + if ((ch > 0xffff) && (size < 4)) { + /* Hey, this is wrong, we must be running TCL_UTF_MAX==3 + * The best thing we can do is spit out 2 surrogates */ + ch -= 0x10000; + size = Tcl_UniCharToUtf(((ch >> 10) | 0xd800), buf); + size += Tcl_UniCharToUtf(((ch & 0x3ff) | 0xdc00), buf+size); + } + return size; +} + + #endif /* * Local Variables: diff --git a/generic/ttk/ttkEntry.c b/generic/ttk/ttkEntry.c index d80e1fd..a25574a 100644 --- a/generic/ttk/ttkEntry.c +++ b/generic/ttk/ttkEntry.c @@ -282,11 +282,11 @@ static char *EntryDisplayString(const char *showChar, int numChars) { char *displayString, *p; int size; - Tcl_UniChar ch; - char buf[4]; + int ch; + char buf[6]; - Tcl_UtfToUniChar(showChar, &ch); - size = Tcl_UniCharToUtf(ch, buf); + TkUtfToUniChar(showChar, &ch); + size = TkUniCharToUtf(ch, buf); p = displayString = ckalloc(numChars * size + 1); while (numChars--) { @@ -406,7 +406,7 @@ ExpandPercents( int number, length; const char *string; int stringLength; - Tcl_UniChar ch; + int ch; char numStorage[2*TCL_INTEGER_SPACE]; while (*template) { @@ -430,7 +430,7 @@ ExpandPercents( */ ++template; /* skip over % */ if (*template != '\0') { - template += Tcl_UtfToUniChar(template, &ch); + template += TkUtfToUniChar(template, &ch); } else { ch = '%'; } @@ -480,7 +480,7 @@ ExpandPercents( string = Tk_PathName(entryPtr->core.tkwin); break; default: - length = Tcl_UniCharToUtf(ch, numStorage); + length = TkUniCharToUtf(ch, numStorage); numStorage[length] = '\0'; string = numStorage; break; diff --git a/unix/tkUnixFont.c b/unix/tkUnixFont.c index 681d1d1..ce4eca9 100644 --- a/unix/tkUnixFont.c +++ b/unix/tkUnixFont.c @@ -405,7 +405,7 @@ ControlUtfProc( { const char *srcStart, *srcEnd; char *dstStart, *dstEnd; - Tcl_UniChar ch; + int ch; int result; static char hexChars[] = "0123456789abcdef"; static char mapChars[] = { @@ -426,9 +426,9 @@ ControlUtfProc( result = TCL_CONVERT_NOSPACE; break; } - src += Tcl_UtfToUniChar(src, &ch); + src += TkUtfToUniChar(src, &ch); dst[0] = '\\'; - if ((ch < sizeof(mapChars)) && (mapChars[ch] != 0)) { + if (((size_t) ch < sizeof(mapChars)) && (mapChars[ch] != 0)) { dst[1] = mapChars[ch]; dst += 2; } else if (ch < 256) { @@ -436,13 +436,21 @@ ControlUtfProc( dst[2] = hexChars[(ch >> 4) & 0xf]; dst[3] = hexChars[ch & 0xf]; dst += 4; - } else { + } else if (ch < 0x10000) { dst[1] = 'u'; dst[2] = hexChars[(ch >> 12) & 0xf]; dst[3] = hexChars[(ch >> 8) & 0xf]; dst[4] = hexChars[(ch >> 4) & 0xf]; dst[5] = hexChars[ch & 0xf]; dst += 6; + } else { + /* TODO we can do better here */ + dst[1] = 'u'; + dst[2] = 'f'; + dst[3] = 'f'; + dst[4] = 'f'; + dst[5] = 'd'; + dst += 6; } } *srcReadPtr = src - srcStart; @@ -1027,7 +1035,7 @@ Tk_MeasureChars( curByte = 0; } else if (maxLength < 0) { const char *p, *end, *next; - Tcl_UniChar ch; + int ch; SubFont *thisSubFontPtr; FontFamily *familyPtr; Tcl_DString runString; @@ -1043,7 +1051,7 @@ Tk_MeasureChars( curX = 0; end = source + numBytes; for (p = source; p < end; ) { - next = p + Tcl_UtfToUniChar(p, &ch); + next = p + TkUtfToUniChar(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); if (thisSubFontPtr != lastSubFontPtr) { familyPtr = lastSubFontPtr->familyPtr; diff --git a/unix/tkUnixRFont.c b/unix/tkUnixRFont.c index 0ea1ec6..cf4127d 100644 --- a/unix/tkUnixRFont.c +++ b/unix/tkUnixRFont.c @@ -670,7 +670,7 @@ Tk_MeasureChars( while (numBytes > 0) { int unichar; - clen = TkUtfToUniChar2(source, &unichar); + clen = TkUtfToUniChar(source, &unichar); c = (FcChar32) unichar; if (clen <= 0) { diff --git a/win/tkWinFont.c b/win/tkWinFont.c index 30638ca..7c6c0ba 100644 --- a/win/tkWinFont.c +++ b/win/tkWinFont.c @@ -858,7 +858,7 @@ Tk_MeasureChars( start = source; end = start + numBytes; for (p = start; p < end; ) { - next = p + TkUtfToUniChar2(p, &ch); + next = p + TkUtfToUniChar(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); if (thisSubFontPtr != lastSubFontPtr) { familyPtr = lastSubFontPtr->familyPtr; @@ -920,7 +920,7 @@ Tk_MeasureChars( familyPtr = lastSubFontPtr->familyPtr; Tcl_DStringInit(&runString); for (p = start; p < end; ) { - next = p + TkUtfToUniChar2(p, &ch); + next = p + TkUtfToUniChar(p, &ch); Tcl_UtfToExternal(NULL, familyPtr->encoding, p, (int) (next - p), 0, NULL, buf, sizeof(buf), NULL, &dstWrote, NULL); @@ -975,7 +975,7 @@ Tk_MeasureChars( p = source; ch = ' '; while (p < end) { - next = p + TkUtfToUniChar2(p, &ch2); + next = p + TkUtfToUniChar(p, &ch2); if ((ch != ' ') && (ch2 == ' ')) { lastWordBreak = p; } @@ -1457,7 +1457,7 @@ MultiFontTextOut( end = source + numBytes; for (p = source; p < end; ) { - next = p + TkUtfToUniChar2(p, &ch); + next = p + TkUtfToUniChar(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); if (thisSubFontPtr != lastSubFontPtr) { if (p > source) { diff --git a/win/tkWinKey.c b/win/tkWinKey.c index 2698c4d..7fee101 100644 --- a/win/tkWinKey.c +++ b/win/tkWinKey.c @@ -88,6 +88,8 @@ TkpGetString( * result. */ { XKeyEvent *keyEv = &eventPtr->xkey; + int len; + char buf[6]; Tcl_DStringInit(dsPtr); if (keyEv->send_event == -1) { @@ -95,41 +97,14 @@ TkpGetString( Tcl_ExternalToUtfDString(TkWinGetKeyInputEncoding(), keyEv->trans_chars, keyEv->nbytes, dsPtr); } - } else if (keyEv->send_event == -2) { - /* - * Special case for win2000 multi-lingal IME input. xkey.trans_chars[] - * already contains a UNICODE char. - */ - - int unichar; - char buf[XMaxTransChars]; - int len; - - unichar = keyEv->trans_chars[1] & 0xff; - unichar <<= 8; - unichar |= keyEv->trans_chars[0] & 0xff; - - len = Tcl_UniCharToUtf((Tcl_UniChar) unichar, buf); - - Tcl_DStringAppend(dsPtr, buf, len); } else if (keyEv->send_event == -3) { - char buf[XMaxTransChars]; - int len; - /* - * Special case for WM_UNICHAR. + * Special case for WM_UNICHAR and win2000 multi-lingal IME input */ - len = Tcl_UniCharToUtf(keyEv->keycode, buf); - if ((keyEv->keycode <= 0xffff) || (len == XMaxTransChars)) { - Tcl_DStringAppend(dsPtr, buf, len); - } else { - 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); - } + len = TkUniCharToUtf(keyEv->keycode, buf); + Tcl_DStringAppend(dsPtr, buf, len); } else { /* * This is an event generated from generic code. It has no nchars or @@ -140,9 +115,6 @@ TkpGetString( if (((keysym != NoSymbol) && (keysym > 0) && (keysym < 256)) || (keysym == XK_Return) || (keysym == XK_Tab)) { - char buf[XMaxTransChars]; - int len; - len = Tcl_UniCharToUtf((Tcl_UniChar) (keysym & 255), buf); Tcl_DStringAppend(dsPtr, buf, len); } diff --git a/win/tkWinX.c b/win/tkWinX.c index 6be54e2..ce73aac 100644 --- a/win/tkWinX.c +++ b/win/tkWinX.c @@ -1580,8 +1580,8 @@ HandleIMEComposition( /* * Set up the fields pertinent to key event. * - * We set send_event to the special value of -2, so that TkpGetString - * in tkWinKey.c knows that trans_chars[] already contains a UNICODE + * We set send_event to the special value of -3, so that TkpGetString + * in tkWinKey.c knows that keycode already contains a UNICODE * char and there's no need to do encoding conversion. * * Note that the event *must* be zeroed out first; Tk plays cunning @@ -1592,7 +1592,7 @@ HandleIMEComposition( memset(&event, 0, sizeof(XEvent)); event.xkey.serial = winPtr->display->request++; - event.xkey.send_event = -2; + event.xkey.send_event = -3; event.xkey.display = winPtr->display; event.xkey.window = winPtr->window; event.xkey.root = RootWindow(winPtr->display, winPtr->screenNum); @@ -1600,8 +1600,7 @@ HandleIMEComposition( event.xkey.state = TkWinGetModifierState(); event.xkey.time = TkpGetMS(); event.xkey.same_screen = True; - event.xkey.keycode = 0; - event.xkey.nbytes = 2; + event.xkey.nbytes = 0; for (i=0; i Date: Tue, 27 Sep 2016 15:44:50 +0000 Subject: clearify comment --- win/tkWinKey.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/win/tkWinKey.c b/win/tkWinKey.c index 89b5d29..31faea0 100644 --- a/win/tkWinKey.c +++ b/win/tkWinKey.c @@ -100,7 +100,7 @@ TkpGetString( } else if (keyEv->send_event == -3) { /* - * Special case for WM_UNICHAR. + * Special case for WM_UNICHAR and win2000 multi-lingal IME input */ len = TkUniCharToUtf(keyEv->keycode, buf); -- cgit v0.12