Skip to content

Commit

Permalink
Better approach, using "Tk" prefix for UTF-32 versions of API
Browse files Browse the repository at this point in the history
  • Loading branch information
jan.nijtmans committed Mar 14, 2024
1 parent 6eb17d2 commit fa083c8
Show file tree
Hide file tree
Showing 13 changed files with 76 additions and 94 deletions.
12 changes: 6 additions & 6 deletions generic/tkCanvText.c
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ ConfigureText(
*/

textPtr->numBytes = strlen(textPtr->text);
textPtr->numChars = Tcl_NumUtfChars(textPtr->text, textPtr->numBytes);
textPtr->numChars = TkNumUtfChars(textPtr->text, textPtr->numBytes);
if (textInfoPtr->selItemPtr == itemPtr) {

if (textInfoPtr->selectFirst >= textPtr->numChars) {
Expand Down Expand Up @@ -1119,7 +1119,7 @@ TextInsert(

ckfree(text);
textPtr->text = newStr;
charsAdded = Tcl_NumUtfChars(string, byteCount);
charsAdded = TkNumUtfChars(string, byteCount);
textPtr->numChars += charsAdded;
textPtr->numBytes += byteCount;

Expand Down Expand Up @@ -1189,8 +1189,8 @@ TextDeleteChars(
}
charsRemoved = last + 1 - first;

byteIndex = Tcl_UtfAtIndex(text, first) - text;
byteCount = Tcl_UtfAtIndex(text + byteIndex, charsRemoved)
byteIndex = TkUtfAtIndex(text, first) - text;
byteCount = TkUtfAtIndex(text + byteIndex, charsRemoved)
- (text + byteIndex);

newStr = (char *)ckalloc(textPtr->numBytes + 1 - byteCount);
Expand Down Expand Up @@ -1612,8 +1612,8 @@ GetSelText(
return 0;
}
text = textPtr->text;
selStart = Tcl_UtfAtIndex(text, textInfoPtr->selectFirst);
selEnd = Tcl_UtfAtIndex(selStart,
selStart = TkUtfAtIndex(text, textInfoPtr->selectFirst);
selEnd = TkUtfAtIndex(selStart,
textInfoPtr->selectLast + 1 - textInfoPtr->selectFirst);
if (selEnd <= selStart + offset) {
return 0;
Expand Down
14 changes: 7 additions & 7 deletions generic/tkEntry.c
Original file line number Diff line number Diff line change
Expand Up @@ -2164,7 +2164,7 @@ InsertChars(
char *newStr;

string = entryPtr->string;
byteIndex = Tcl_UtfAtIndex(string, index) - string;
byteIndex = TkUtfAtIndex(string, index) - string;
byteCount = strlen(value);
if (byteCount == 0) {
return TCL_OK;
Expand Down Expand Up @@ -2197,7 +2197,7 @@ InsertChars(
*/

oldChars = entryPtr->numChars;
entryPtr->numChars = Tcl_NumUtfChars(newStr, TCL_INDEX_NONE);
entryPtr->numChars = TkNumUtfChars(newStr, TCL_INDEX_NONE);
charsAdded = entryPtr->numChars - oldChars;
entryPtr->numBytes += byteCount;

Expand Down Expand Up @@ -2268,8 +2268,8 @@ DeleteChars(
}

string = entryPtr->string;
byteIndex = Tcl_UtfAtIndex(string, index) - string;
byteCount = Tcl_UtfAtIndex(string + byteIndex, count) - (string+byteIndex);
byteIndex = TkUtfAtIndex(string, index) - string;
byteCount = TkUtfAtIndex(string + byteIndex, count) - (string+byteIndex);

newByteCount = entryPtr->numBytes + 1 - byteCount;
newStr = (char *)ckalloc(newByteCount);
Expand Down Expand Up @@ -2497,7 +2497,7 @@ EntrySetValue(
entryPtr->string = tmp;
}
entryPtr->numBytes = valueLen;
entryPtr->numChars = Tcl_NumUtfChars(value, valueLen);
entryPtr->numChars = TkNumUtfChars(value, valueLen);

if (entryPtr->displayString == oldSource) {
entryPtr->displayString = entryPtr->string;
Expand Down Expand Up @@ -2927,8 +2927,8 @@ EntryFetchSelection(
return -1;
}
string = entryPtr->displayString;
selStart = Tcl_UtfAtIndex(string, entryPtr->selectFirst);
selEnd = Tcl_UtfAtIndex(selStart,
selStart = TkUtfAtIndex(string, entryPtr->selectFirst);
selEnd = TkUtfAtIndex(selStart,
entryPtr->selectLast - entryPtr->selectFirst);
if (selEnd <= selStart + offset) {
return 0;
Expand Down
20 changes: 10 additions & 10 deletions generic/tkFont.c
Original file line number Diff line number Diff line change
Expand Up @@ -2013,7 +2013,7 @@ Tk_ComputeTextLayout(
height = fmPtr->ascent + fmPtr->descent;

if (numChars < 0) {
numChars = Tcl_NumUtfChars(string, TCL_INDEX_NONE);
numChars = TkNumUtfChars(string, TCL_INDEX_NONE);
}
if (wrapLength == 0) {
wrapLength = -1;
Expand All @@ -2036,7 +2036,7 @@ Tk_ComputeTextLayout(

curX = 0;

endp = Tcl_UtfAtIndex(string, numChars);
endp = TkUtfAtIndex(string, numChars);
special = string;

flags &= TK_IGNORE_TABS | TK_IGNORE_NEWLINES;
Expand Down Expand Up @@ -2153,7 +2153,7 @@ Tk_ComputeTextLayout(
bytesThisChunk = Tk_MeasureChars(tkfont, end, bytesThisChunk,
-1, 0, &chunkPtr->totalWidth);
chunkPtr->numBytes += bytesThisChunk;
chunkPtr->numChars += Tcl_NumUtfChars(end, bytesThisChunk);
chunkPtr->numChars += TkNumUtfChars(end, bytesThisChunk);
chunkPtr->totalWidth += curX;
}
}
Expand Down Expand Up @@ -2345,14 +2345,14 @@ Tk_DrawTextLayout(
firstChar = 0;
firstByte = chunkPtr->start;
} else {
firstByte = Tcl_UtfAtIndex(chunkPtr->start, firstChar);
firstByte = TkUtfAtIndex(chunkPtr->start, firstChar);
Tk_MeasureChars(layoutPtr->tkfont, chunkPtr->start,
firstByte - chunkPtr->start, -1, 0, &drawX);
}
if (lastChar < numDisplayChars) {
numDisplayChars = lastChar;
}
lastByte = Tcl_UtfAtIndex(chunkPtr->start, numDisplayChars);
lastByte = TkUtfAtIndex(chunkPtr->start, numDisplayChars);
#ifdef TK_DRAW_IN_CONTEXT
TkpDrawCharsInContext(display, drawable, gc, layoutPtr->tkfont,
chunkPtr->start, chunkPtr->numBytes,
Expand Down Expand Up @@ -2415,14 +2415,14 @@ TkDrawAngledTextLayout(
firstChar = 0;
firstByte = chunkPtr->start;
} else {
firstByte = Tcl_UtfAtIndex(chunkPtr->start, firstChar);
firstByte = TkUtfAtIndex(chunkPtr->start, firstChar);
Tk_MeasureChars(layoutPtr->tkfont, chunkPtr->start,
firstByte - chunkPtr->start, -1, 0, &drawX);
}
if (lastChar < numDisplayChars) {
numDisplayChars = lastChar;
}
lastByte = Tcl_UtfAtIndex(chunkPtr->start, numDisplayChars);
lastByte = TkUtfAtIndex(chunkPtr->start, numDisplayChars);
#ifdef TK_DRAW_IN_CONTEXT
dx = cosA * (chunkPtr->x) + sinA * (chunkPtr->y);
dy = -sinA * (chunkPtr->x) + cosA * (chunkPtr->y);
Expand Down Expand Up @@ -2675,7 +2675,7 @@ Tk_PointToChar(
}
n = Tk_MeasureChars((Tk_Font) fontPtr, chunkPtr->start,
chunkPtr->numBytes, x - chunkPtr->x, 0, &dummy);
return numChars + Tcl_NumUtfChars(chunkPtr->start, n);
return numChars + TkNumUtfChars(chunkPtr->start, n);
}
numChars += chunkPtr->numChars;
lastPtr = chunkPtr;
Expand Down Expand Up @@ -2784,7 +2784,7 @@ Tk_CharBbox(
goto check;
}
} else if (index < chunkPtr->numChars) {
end = Tcl_UtfAtIndex(chunkPtr->start, index);
end = TkUtfAtIndex(chunkPtr->start, index);
if (xPtr != NULL) {
Tk_MeasureChars(tkfont, chunkPtr->start,
end - chunkPtr->start, -1, 0, &x);
Expand Down Expand Up @@ -3859,7 +3859,7 @@ NewChunk(
*layoutPtrPtr = layoutPtr;
*maxPtr = maxChunks;
}
numChars = Tcl_NumUtfChars(start, numBytes);
numChars = TkNumUtfChars(start, numBytes);
chunkPtr = &layoutPtr->chunks[layoutPtr->numChunks];
chunkPtr->start = start;
chunkPtr->numBytes = numBytes;
Expand Down
2 changes: 1 addition & 1 deletion generic/tkIcu.c
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,7 @@ startEndOfCmd(
str = Tcl_GetStringFromObj(objv[1], &len);
Tcl_UtfToChar16DString(str, len, &ds);
len = Tcl_DStringLength(&ds)/2;
Tcl_Size ulen = Tcl_GetCharLength(objv[1]);
Tcl_Size ulen = TkGetCharLength(objv[1]);
if (TkGetIntForIndex(objv[2], ulen-1, 0, &idx) != TCL_OK) {
Tcl_DStringFree(&ds);
Tcl_SetObjResult(interp, Tcl_ObjPrintf("bad index \"%s\": must be integer?[+-]integer?, end?[+-]integer?, or \"\"", Tcl_GetString(objv[2])));
Expand Down
29 changes: 7 additions & 22 deletions generic/tkInt.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,34 +83,19 @@
# define TCL_COMBINE 0
#endif

#undef TclNumUtfChars
#undef TclUtfAtIndex
#undef Tcl_NumUtfChars
#undef Tcl_GetCharLength
#undef Tcl_UtfAtIndex
/* Make available UTF-32 versions of the API, even though we compile with TCL_UTF_MAX=3 */
#if TCL_MAJOR_VERSION > 8
# define TclNumUtfChars \
(tclStubsPtr->tclNumUtfChars) /* 312 */
# define TclUtfAtIndex \
(tclStubsPtr->tclUtfAtIndex) /* 325 */
# define Tcl_NumUtfChars \
(tclStubsPtr->tcl_NumUtfChars) /* 669 */
# define Tcl_GetCharLength \
(tclStubsPtr->tcl_GetCharLength) /* 670 */
# define Tcl_UtfAtIndex \
(tclStubsPtr->tcl_UtfAtIndex) /* 671 */
# define TkNumUtfChars (tclStubsPtr->tcl_NumUtfChars) /* 669 */
# define TkGetCharLength (tclStubsPtr->tcl_GetCharLength) /* 670 */
# define TkUtfAtIndex (tclStubsPtr->tcl_UtfAtIndex) /* 671 */
#else
# define TclNumUtfChars \
(tclStubsPtr->tcl_NumUtfChars) /* 312 */
# define TclUtfAtIndex \
(tclStubsPtr->tcl_UtfAtIndex) /* 325 */
# define Tcl_NumUtfChars (((&tclStubsPtr->tcl_PkgProvideEx)[631]) ? \
# define TkNumUtfChars (((&tclStubsPtr->tcl_PkgProvideEx)[631]) ? \
((Tcl_Size (*)(const char *, Tcl_Size))(void *)((&tclStubsPtr->tcl_PkgProvideEx)[669])) \
: (tclStubsPtr->tcl_NumUtfChars) /* 312 */)
# define Tcl_GetCharLength (((&tclStubsPtr->tcl_PkgProvideEx)[631]) ? \
# define TkGetCharLength (((&tclStubsPtr->tcl_PkgProvideEx)[631]) ? \
((Tcl_Size (*)(Tcl_Obj *))(void *)((&tclStubsPtr->tcl_PkgProvideEx)[670])) \
: (tclStubsPtr->tcl_GetCharLength) /* 380 */)
# define Tcl_UtfAtIndex (((&tclStubsPtr->tcl_PkgProvideEx)[631]) ? \
# define TkUtfAtIndex (((&tclStubsPtr->tcl_PkgProvideEx)[631]) ? \
((const char *(*)(const char *, Tcl_Size))(void *)((&tclStubsPtr->tcl_PkgProvideEx)[671])) \
: (tclStubsPtr->tcl_UtfAtIndex) /* 325 */)
#endif
Expand Down
4 changes: 2 additions & 2 deletions generic/tkMessage.c
Original file line number Diff line number Diff line change
Expand Up @@ -501,7 +501,7 @@ ConfigureMessage(
* be specified to Tk_ConfigureWidget.
*/

msgPtr->numChars = Tcl_NumUtfChars(msgPtr->string, TCL_INDEX_NONE);
msgPtr->numChars = TkNumUtfChars(msgPtr->string, TCL_INDEX_NONE);

if (msgPtr->highlightWidth < 0) {
msgPtr->highlightWidth = 0;
Expand Down Expand Up @@ -902,7 +902,7 @@ MessageTextVarProc(
if (msgPtr->string != NULL) {
ckfree(msgPtr->string);
}
msgPtr->numChars = Tcl_NumUtfChars(value, TCL_INDEX_NONE);
msgPtr->numChars = TkNumUtfChars(value, TCL_INDEX_NONE);
msgPtr->string = (char *)ckalloc(strlen(value) + 1);
strcpy(msgPtr->string, value);
ComputeMessageGeometry(msgPtr);
Expand Down
6 changes: 3 additions & 3 deletions generic/tkSelect.c
Original file line number Diff line number Diff line change
Expand Up @@ -1401,15 +1401,15 @@ HandleTclCommand(

if (cmdInfoPtr->interp != NULL) {
if (length <= maxBytes) {
cmdInfoPtr->charOffset += Tcl_NumUtfChars(string, TCL_INDEX_NONE);
cmdInfoPtr->charOffset += TkNumUtfChars(string, TCL_INDEX_NONE);
cmdInfoPtr->buffer[0] = '\0';
} else {
Tcl_UniChar ch = 0;
int ch = 0;
p = string;
string += count;
numChars = 0;
while (p < string) {
p += Tcl_UtfToUniChar(p, &ch);
p += TkUtfToUniChar(p, &ch);
numChars++;
}
cmdInfoPtr->charOffset += numChars;
Expand Down
18 changes: 9 additions & 9 deletions generic/tkText.c
Original file line number Diff line number Diff line change
Expand Up @@ -1461,7 +1461,7 @@ TextWidgetObjCmd(

insertLength = 0;
for (j = 4; j < objc; j += 2) {
insertLength += Tcl_GetCharLength(objv[j]);
insertLength += TkGetCharLength(objv[j]);
}

/*
Expand Down Expand Up @@ -4092,12 +4092,12 @@ TextSearchIndexInLine(
if (searchSpecPtr->exact) {
index += leftToScan;
} else {
index += Tcl_NumUtfChars(segPtr->body.chars, leftToScan);
index += TkNumUtfChars(segPtr->body.chars, leftToScan);
}
} else if (searchSpecPtr->exact) {
index += segPtr->size;
} else {
index += Tcl_NumUtfChars(segPtr->body.chars, -1);
index += TkNumUtfChars(segPtr->body.chars, -1);
}
}
leftToScan -= segPtr->size;
Expand Down Expand Up @@ -4222,7 +4222,7 @@ TextSearchAddNextLine(
Tcl_GetString(theLine);
*lenPtr = theLine->length;
} else {
*lenPtr = Tcl_GetCharLength(theLine);
*lenPtr = TkGetCharLength(theLine);
}
}
return linePtr;
Expand Down Expand Up @@ -4292,7 +4292,7 @@ TextSearchFoundMatch(
if (searchSpecPtr->exact) {
const char *startOfLine = Tcl_GetString(theLine);

numChars = Tcl_NumUtfChars(startOfLine + matchOffset, matchLength);
numChars = TkNumUtfChars(startOfLine + matchOffset, matchLength);
} else {
numChars = matchLength;
}
Expand Down Expand Up @@ -4351,13 +4351,13 @@ TextSearchFoundMatch(
if (searchSpecPtr->exact) {
matchOffset += segPtr->size;
} else {
matchOffset += Tcl_NumUtfChars(segPtr->body.chars, -1);
matchOffset += TkNumUtfChars(segPtr->body.chars, -1);
}
} else {
if (searchSpecPtr->exact) {
leftToScan -= (int)segPtr->size;
} else {
leftToScan -= Tcl_NumUtfChars(segPtr->body.chars, -1);
leftToScan -= TkNumUtfChars(segPtr->body.chars, -1);
}
}
curIndex.byteIndex += segPtr->size;
Expand Down Expand Up @@ -4442,13 +4442,13 @@ TextSearchFoundMatch(
continue;
} else if (!searchSpecPtr->searchElide
&& TkTextIsElided(textPtr, &curIndex, NULL)) {
numChars += Tcl_NumUtfChars(segPtr->body.chars, -1);
numChars += TkNumUtfChars(segPtr->body.chars, -1);
continue;
}
if (searchSpecPtr->exact) {
leftToScan -= segPtr->size;
} else {
leftToScan -= Tcl_NumUtfChars(segPtr->body.chars, -1);
leftToScan -= TkNumUtfChars(segPtr->body.chars, -1);
}
}

Expand Down
13 changes: 5 additions & 8 deletions generic/tkTextIndex.c
Original file line number Diff line number Diff line change
Expand Up @@ -494,7 +494,7 @@ TkTextMakeCharIndex(
TkTextSegment *segPtr;
char *p, *start, *end;
int index, offset;
Tcl_UniChar ch = 0;
int ch = 0;

indexPtr->tree = tree;
if (lineIndex < 0) {
Expand Down Expand Up @@ -541,7 +541,7 @@ TkTextMakeCharIndex(
return indexPtr;
}
charIndex--;
offset = Tcl_UtfToUniChar(p, &ch);
offset = TkUtfToUniChar(p, &ch);
index += offset;
}
} else {
Expand Down Expand Up @@ -1129,15 +1129,15 @@ TkTextPrintIndex(
break;
}
if (segPtr->typePtr == &tkTextCharType) {
charIndex += Tcl_NumUtfChars(segPtr->body.chars, segPtr->size);
charIndex += TkNumUtfChars(segPtr->body.chars, segPtr->size);
} else {
charIndex += segPtr->size;
}
numBytes -= segPtr->size;
}

if (segPtr->typePtr == &tkTextCharType) {
charIndex += Tcl_NumUtfChars(segPtr->body.chars, numBytes);
charIndex += TkNumUtfChars(segPtr->body.chars, numBytes);
} else {
charIndex += numBytes;
}
Expand Down Expand Up @@ -1948,7 +1948,7 @@ TkTextIndexCount(
}
count += byteLen - i;
if (i) {
count += Tcl_NumUtfChars(segPtr->body.chars + byteOffset
count += TkNumUtfChars(segPtr->body.chars + byteOffset
+ (byteLen - i), i);
}
} else {
Expand Down Expand Up @@ -2210,9 +2210,6 @@ TkTextIndexBackChars(
if (p == start) {
break;
}
if ((sizeof(Tcl_UniChar) == 2) && (unsigned)(UCHAR(*p) - 0xF0) <= 5) {
charCount--; /* Characters > U+FFFF count as 2 here */
}
if (charCount != 0) {
charCount--;
}
Expand Down
Loading

0 comments on commit fa083c8

Please sign in to comment.