From a1c49d881fad7ee0638c54eab49208dcb9b2f1a8 Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Wed, 11 Apr 2018 21:08:00 +0000 Subject: Improved surrogate handling for TCL_UTF_MAX=3|4. (backported from androwish). Slight optimizations. --- generic/tkTextIndex.c | 12 ++++++------ generic/tkUtil.c | 2 +- unix/tkUnixFont.c | 38 +++++++++++++++++++------------------- 3 files changed, 26 insertions(+), 26 deletions(-) diff --git a/generic/tkTextIndex.c b/generic/tkTextIndex.c index faa1afd..2b5b569 100644 --- a/generic/tkTextIndex.c +++ b/generic/tkTextIndex.c @@ -387,7 +387,7 @@ TkTextMakeByteIndex( TkTextSegment *segPtr; int index; const char *p, *start; - Tcl_UniChar ch; + int ch; indexPtr->tree = tree; if (lineIndex < 0) { @@ -437,7 +437,7 @@ TkTextMakeByteIndex( start = segPtr->body.chars + (byteIndex - index); p = Tcl_UtfPrev(start, segPtr->body.chars); - p += Tcl_UtfToUniChar(p, &ch); + p += TkUtfToUniChar(p, &ch); indexPtr->byteIndex += p - start; } break; @@ -480,7 +480,7 @@ TkTextMakeCharIndex( register TkTextSegment *segPtr; char *p, *start, *end; int index, offset; - Tcl_UniChar ch; + int ch; indexPtr->tree = tree; if (lineIndex < 0) { @@ -527,7 +527,7 @@ TkTextMakeCharIndex( return indexPtr; } charIndex--; - offset = Tcl_UtfToUniChar(p, &ch); + offset = TkUtfToUniChar(p, &ch); index += offset; } } else { @@ -1475,7 +1475,7 @@ TkTextIndexForwChars( TkTextElideInfo *infoPtr = NULL; int byteOffset; char *start, *end, *p; - Tcl_UniChar ch; + int ch; int elide = 0; int checkElided = (type & COUNT_DISPLAY); @@ -1574,7 +1574,7 @@ TkTextIndexForwChars( if (segPtr->typePtr == &tkTextCharType) { start = segPtr->body.chars + byteOffset; end = segPtr->body.chars + segPtr->size; - for (p = start; p < end; p += Tcl_UtfToUniChar(p, &ch)) { + for (p = start; p < end; p += TkUtfToUniChar(p, &ch)) { if (charCount == 0) { dstPtr->byteIndex += (p - start); goto forwardCharDone; diff --git a/generic/tkUtil.c b/generic/tkUtil.c index e686826..ff5bd98 100644 --- a/generic/tkUtil.c +++ b/generic/tkUtil.c @@ -1260,7 +1260,7 @@ TkUtfToUniChar( int TkUniCharToUtf(int ch, char *buf) { int size = Tcl_UniCharToUtf(ch, buf); - if ((ch > 0xffff) && (ch <= 0x10ffff) && (size < 4)) { + if ((((unsigned)(ch - 0x10000) <= 0xFFFFF)) && (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; diff --git a/unix/tkUnixFont.c b/unix/tkUnixFont.c index b361e83..96635b4 100644 --- a/unix/tkUnixFont.c +++ b/unix/tkUnixFont.c @@ -406,8 +406,7 @@ ControlUtfProc( { const char *srcStart, *srcEnd; char *dstStart, *dstEnd; - int ch; - int result; + int ch, result; static char hexChars[] = "0123456789abcdef"; static char mapChars[] = { 0, 0, 0, 0, 0, 0, 0, @@ -954,7 +953,7 @@ void TkpGetFontAttrsForChar( Tk_Window tkwin, /* Window on the font's display */ Tk_Font tkfont, /* Font to query */ - int c, /* Character of interest */ + int c, /* Character of interest */ TkFontAttributes *faPtr) /* Output: Font attributes */ { FontAttributes atts; @@ -1018,7 +1017,7 @@ Tk_MeasureChars( { UnixFont *fontPtr; SubFont *lastSubFontPtr; - int curX, curByte; + int curX, curByte, ch; /* * Unix does not use kerning or fractional character widths when @@ -1036,7 +1035,6 @@ Tk_MeasureChars( curByte = 0; } else if (maxLength < 0) { const char *p, *end, *next; - int ch; SubFont *thisSubFontPtr; FontFamily *familyPtr; Tcl_DString runString; @@ -1090,7 +1088,6 @@ Tk_MeasureChars( } else { const char *p, *end, *next, *term; int newX, termX, sawNonSpace, dstWrote; - Tcl_UniChar ch; FontFamily *familyPtr; XChar2b buf[8]; @@ -1100,7 +1097,7 @@ Tk_MeasureChars( * individually. */ - next = source + Tcl_UtfToUniChar(source, &ch); + next = source + TkUtfToUniChar(source, &ch); newX = curX = termX = 0; term = source; @@ -1135,7 +1132,7 @@ Tk_MeasureChars( break; } - next += Tcl_UtfToUniChar(next, &ch); + next += TkUtfToUniChar(next, &ch); if ((ch < 256) && isspace(ch)) { if (sawNonSpace) { term = p; @@ -1160,13 +1157,13 @@ Tk_MeasureChars( */ curX = newX; - p += Tcl_UtfToUniChar(p, &ch); + p += TkUtfToUniChar(p, &ch); } if ((flags & TK_AT_LEAST_ONE) && (term == source) && (p < end)) { term = p; termX = curX; if (term == source) { - term += Tcl_UtfToUniChar(term, &ch); + term += TkUtfToUniChar(term, &ch); termX = newX; } } else if ((p >= end) || !(flags & TK_WHOLE_WORDS)) { @@ -1278,8 +1275,7 @@ Tk_DrawChars( SubFont *thisSubFontPtr, *lastSubFontPtr; Tcl_DString runString; const char *p, *end, *next; - int xStart, needWidth, window_width, do_width; - Tcl_UniChar ch; + int xStart, needWidth, window_width, do_width, ch; FontFamily *familyPtr; #ifdef TK_DRAW_CHAR_XWINDOW_CHECK int rx, ry; @@ -1314,7 +1310,7 @@ Tk_DrawChars( needWidth = fontPtr->font.fa.underline + fontPtr->font.fa.overstrike; for (p = source; p <= end; ) { if (p < end) { - next = p + Tcl_UtfToUniChar(p, &ch); + next = p + TkUtfToUniChar(p, &ch); thisSubFontPtr = FindSubFontForChar(fontPtr, ch, &lastSubFontPtr); } else { next = p + 1; @@ -1849,7 +1845,9 @@ AllocFontFamily( if ((familyPtr->faceName == fa.fa.family) && (familyPtr->foundry == fa.xa.foundry) && (familyPtr->encoding == encoding)) { - Tcl_FreeEncoding(encoding); + if (encoding) { + Tcl_FreeEncoding(encoding); + } familyPtr->refCount++; return familyPtr; } @@ -1923,7 +1921,9 @@ FreeFontFamily( if (familyPtr->refCount > 0) { return; } - Tcl_FreeEncoding(familyPtr->encoding); + if (familyPtr->encoding) { + Tcl_FreeEncoding(familyPtr->encoding); + } for (i = 0; i < FONTMAP_PAGES; i++) { if (familyPtr->fontMap[i] != NULL) { ckfree(familyPtr->fontMap[i]); @@ -2213,7 +2213,7 @@ FontMapLoadPage( int row) /* Index of the page to be loaded into the * cache. */ { - char buf[16], src[TCL_UTF_MAX]; + char buf[16], src[6]; int minHi, maxHi, minLo, maxLo, scale, checkLo; int i, end, bitOffset, isTwoByteFont, n; Tcl_Encoding encoding; @@ -2251,7 +2251,7 @@ FontMapLoadPage( for (i = row << FONTMAP_SHIFT; i < end; i++) { int hi, lo; - if (Tcl_UtfToExternal(NULL, encoding, src, Tcl_UniCharToUtf(i, src), + if (Tcl_UtfToExternal(NULL, encoding, src, TkUniCharToUtf(i, src), TCL_ENCODING_STOPONERROR, NULL, buf, sizeof(buf), NULL, NULL, NULL) != TCL_OK) { continue; @@ -2417,7 +2417,7 @@ CanUseFallback( unsigned bestScore[2]; char **nameList; char **nameListOrig; - char src[TCL_UTF_MAX]; + char src[6]; FontAttributes want, got; Display *display; SubFont subFont; @@ -2447,7 +2447,7 @@ CanUseFallback( } nameListOrig = nameList; - srcLen = Tcl_UniCharToUtf(ch, src); + srcLen = TkUniCharToUtf(ch, src); want.fa = fontPtr->font.fa; want.xa = fontPtr->xa; -- cgit v0.12