From 4e8c59e74d4386204c4d53b5e8dfb2cbe9cd770c Mon Sep 17 00:00:00 2001 From: "jan.nijtmans" Date: Thu, 30 Nov 2023 16:17:52 +0000 Subject: Entry widget on MacOS works fine now --- generic/tkIcu.c | 10 +++++----- macosx/tkMacOSXFont.c | 39 +++++++++++++++++++++++++++------------ 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/generic/tkIcu.c b/generic/tkIcu.c index 3ccb7f6..1e24698 100644 --- a/generic/tkIcu.c +++ b/generic/tkIcu.c @@ -114,9 +114,9 @@ startEndOfCmd( } if (idx > 0 && len != ulen) { /* The string contains codepoints > \uFFFF. Determine UTF-16 index */ - Tcl_Size newIdx = 1; - for (Tcl_Size i = 1; i < idx; i++) { - newIdx += 1 + (((ustr[newIdx-1]&0xFFC0) == 0xD800) && ((ustr[newIdx]&0xFFC0) == 0xDC00)); + Tcl_Size newIdx = 0; + for (Tcl_Size i = 0; i < index; i++) { + newIdx += 1 + (((newIdx < (Tcl_Size)len-1) && (ustr[newIdx]&0xFC00) == 0xD800) && ((ustr[newIdx+1]&0xFC00) == 0xDC00)); } idx = newIdx; } @@ -130,7 +130,7 @@ startEndOfCmd( } } else if (idx > 0) { if (!(flags & FLAG_WORD)) { - idx += 1 + (((ustr[idx]&0xFFC0) == 0xD800) && ((ustr[idx+1]&0xFFC0) == 0xDC00)); + idx += 1 + (((ustr[idx]&0xFC00) == 0xD800) && ((ustr[idx+1]&0xFC00) == 0xDC00)); } idx = icu_preceding(it, idx); if (idx == 0 && (flags & FLAG_WORD)) { @@ -158,7 +158,7 @@ startEndOfCmd( /* The string contains codepoints > \uFFFF. Determine UTF-32 index */ Tcl_Size newIdx = 1; for (Tcl_Size i = 1; i < idx; i++) { - if (((ustr[i-1]&0xFFC0) != 0xD800) || ((ustr[i]&0xFFC0) != 0xDC00)) newIdx++; + if (((ustr[i-1]&0xFC00) != 0xD800) || ((ustr[i]&0xFC00) != 0xDC00)) newIdx++; } idx = newIdx; } diff --git a/macosx/tkMacOSXFont.c b/macosx/tkMacOSXFont.c index 8a2e0a8..36bae03 100644 --- a/macosx/tkMacOSXFont.c +++ b/macosx/tkMacOSXFont.c @@ -27,6 +27,21 @@ */ /* + * TclNumUtfChars() is the same as Tcl_NumUtfChars(), but counting + * in UTF-16 in stead of UTF-32. For Tcl 8.7 it's a little bit + * tricky to get this function, because we are compiling with TCL_UTF_MAX=4. + */ +#if TCL_MAJOR_VERSION < 9 +# undef TclNumUtfChars +# ifdef USE_TCL_STUBS +# define TclNumUtfChars \ + (tclStubsPtr->tcl_NumUtfChars) /* 312 */ +# else +# define TclNumUtfChars Tcl_NumUtfChars +# endif +#endif + +/* * The following structure represents our Macintosh-specific implementation * of a font object. */ @@ -471,9 +486,9 @@ startOfClusterObjCmd( } if (index > 0 && (Tcl_Size)[S length] != ulen) { /* The string contains codepoints > \uFFFF. Determine UTF-16 index */ - Tcl_Size newIdx = 1; - for (Tcl_Size i = 1; i < index; i++) { - newIdx += 1 + ((([S characterAtIndex:newIdx-1]&0xFFC0) == 0xD800) && (([S characterAtIndex:newIdx]&0xFFC0) == 0xDC00)); + Tcl_Size newIdx = 0; + for (Tcl_Size i = 0; i < index; i++) { + newIdx += 1 + (((newIdx < (Tcl_Size)[S length]-1) && ([S characterAtIndex:newIdx]&0xFC00) == 0xD800) && (([S characterAtIndex:newIdx+1]&0xFC00) == 0xDC00)); } index = newIdx; } @@ -488,7 +503,7 @@ startOfClusterObjCmd( /* The string contains codepoints > \uFFFF. Determine UTF-32 index */ Tcl_Size newIdx = 1; for (Tcl_Size i = 1; i < index; i++) { - if ((([S characterAtIndex:i-1]&0xFFC0) != 0xD800) || (([S characterAtIndex:i]&0xFFC0) != 0xDC00)) newIdx++; + if ((([S characterAtIndex:i-1]&0xFC00) != 0xD800) || (([S characterAtIndex:i]&0xFC00) != 0xDC00)) newIdx++; } index = newIdx; } @@ -527,9 +542,9 @@ endOfClusterObjCmd( } if (index > 0 && (Tcl_Size)[S length] != ulen) { /* The string contains codepoints > \uFFFF. Determine UTF-16 index */ - Tcl_Size newIdx = 1; - for (Tcl_Size i = 1; i < index; i++) { - newIdx += 1 + ((([S characterAtIndex:newIdx-1]&0xFFC0) == 0xD800) && (([S characterAtIndex:newIdx]&0xFFC0) == 0xDC00)); + Tcl_Size newIdx = 0; + for (Tcl_Size i = 0; i < index; i++) { + newIdx += 1 + (((newIdx < (Tcl_Size)[S length]-1) && ([S characterAtIndex:newIdx]&0xFC00) == 0xD800) && (([S characterAtIndex:newIdx+1]&0xFC00) == 0xDC00)); } index = newIdx; } @@ -544,7 +559,7 @@ endOfClusterObjCmd( /* The string contains codepoints > \uFFFF. Determine UTF-32 index */ Tcl_Size newIdx = 1; for (Tcl_Size i = 1; i < index; i++) { - if ((([S characterAtIndex:i-1]&0xFFC0) != 0xD800) || (([S characterAtIndex:i]&0xFFC0) != 0xDC00)) newIdx++; + if ((([S characterAtIndex:i-1]&0xFC00) != 0xD800) || (([S characterAtIndex:i]&0xFC00) != 0xDC00)) newIdx++; } index = newIdx; } @@ -1064,8 +1079,8 @@ TkpMeasureCharsInContext( attributes:fontPtr->nsAttributes]; typesetter = CTTypesetterCreateWithAttributedString( (CFAttributedStringRef)attributedString); - start = Tcl_NumUtfChars(source, rangeStart); - len = Tcl_NumUtfChars(source + rangeStart, rangeLength); + start = TclNumUtfChars(source, rangeStart); + len = TclNumUtfChars(source + rangeStart, rangeLength); if (start > 0) { range.length = start; line = CTTypesetterCreateLine(typesetter, range); @@ -1365,8 +1380,8 @@ TkpDrawAngledCharsInContext( -textX, -textY); } CGContextConcatCTM(context, t); - start = Tcl_NumUtfChars(source, rangeStart); - length = Tcl_NumUtfChars(source, rangeStart + rangeLength) - start; + start = TclNumUtfChars(source, rangeStart); + length = TclNumUtfChars(source, rangeStart + rangeLength) - start; line = CTTypesetterCreateLine(typesetter, CFRangeMake(start, length)); if (start > 0) { -- cgit v0.12