diff options
author | culler <culler> | 2020-05-17 22:08:09 (GMT) |
---|---|---|
committer | culler <culler> | 2020-05-17 22:08:09 (GMT) |
commit | 8fe8aab16504c4e9b91e295e0ee2a537797d0316 (patch) | |
tree | 833c96abacc4d8a988e5ea3b8172bdabb3e60fdc /macosx/tkMacOSXFont.c | |
parent | 5fbfe2a7ea51ec68286bee8b30c89059022279ba (diff) | |
download | tk-8fe8aab16504c4e9b91e295e0ee2a537797d0316.zip tk-8fe8aab16504c4e9b91e295e0ee2a537797d0316.tar.gz tk-8fe8aab16504c4e9b91e295e0ee2a537797d0316.tar.bz2 |
Simplify Aqua text handling by moving encoding/decoding into the TKNSString class.
Diffstat (limited to 'macosx/tkMacOSXFont.c')
-rw-r--r-- | macosx/tkMacOSXFont.c | 144 |
1 files changed, 53 insertions, 91 deletions
diff --git a/macosx/tkMacOSXFont.c b/macosx/tkMacOSXFont.c index 6bd5c55..7fc0113 100644 --- a/macosx/tkMacOSXFont.c +++ b/macosx/tkMacOSXFont.c @@ -103,7 +103,9 @@ static void DrawCharsInContext(Display *display, Drawable drawable, /* * To avoid an extra copy, a TKNSString object wraps a Tcl_DString with an - * NSString that uses the DString's buffer as its character buffer. + * NSString that uses the DString's buffer as its character buffer. It can be + * constructed from a Tcl_DString and it has a DString property that handles + * converting from an NSString to a Tcl_DString */ @implementation TKNSString @@ -124,6 +126,17 @@ static void DrawCharsInContext(Display *display, Drawable drawable, return self; } +- (instancetype)initWithString:(NSString *)aString +{ + self = [self init]; + if (self) { + _string = [[NSString alloc] initWithString:aString]; + self.UTF8String = _string.UTF8String; + } + printf("Initialized with string %s\n", self.UTF8String); + return self; +} + - (void)dealloc { Tcl_DStringFree(&_ds); @@ -140,104 +153,53 @@ static void DrawCharsInContext(Display *display, Drawable drawable, { return [_string characterAtIndex:index]; } -#ifndef __clang__ -@synthesize UTF8String = _UTF8String; -#endif -@end -/* - *--------------------------------------------------------------------------- - * - * TkUtfAtIndex -- - * - * Write a sequence of bytes up to length 6 which is an encoding of a UTF-16 - * character in an NSString. Also record the unicode code point of the character. - * this may be a non-BMP character constructed by reading two surrogates from - * the NSString. See the documentation for TKNSString in tkMacOSXPrivate.h. - * - * Results: - * Returns the number of bytes written. - * - * Side effects: - * Bytes are written to the char array referenced by the pointer uni and - * the unicode code point is written to the integer referenced by the - * pointer code. - * - */ +# ifndef __clang__ +@synthesize DString = _ds; +#endif -MODULE_SCOPE int -TkUtfAtIndex( - NSString *string, - int index, - char *uni, - unsigned int *code) +- (Tcl_DString)DString { - char *ptr = uni; - UniChar uniChar = [string characterAtIndex: index]; - if (CFStringIsSurrogateHighCharacter(uniChar)) { - UniChar lowChar = [string characterAtIndex: ++index]; - *code = CFStringGetLongCharacterForSurrogatePair( - uniChar, lowChar); - ptr += Tcl_UniCharToUtf(uniChar, ptr); - ptr += Tcl_UniCharToUtf(lowChar, ptr); - return ptr - uni; - } else { - *code = (int) uniChar; - [[string substringWithRange: NSMakeRange(index, 1)] - getCString: uni - maxLength: 7 - encoding: NSUTF8StringEncoding]; - return strlen(uni); - } -} - -/* - *--------------------------------------------------------------------------- - * - * TkNSStringToUtf -- - * - * Encodes the unicode string represented by an NSString object with the - * internal encoding that Tcl uses when TCL_UTF_MAX = 3. This encoding - * is similar to UTF-8 except that non-BMP characters are encoded as two - * successive 3-byte sequences which are constructed from UTF-16 surrogates - * by applying the UTF-8 algorithm. Even though the UTF-8 encoding does not - * allow encoding surrogates, the algorithm does produce a well-defined - * 3-byte sequence. - * - * Results: - * Returns a pointer to a null-terminated byte array which encodes the - * NSString. - * - * Side effects: - * Memory is allocated to hold the byte array, which must be freed with - * ckalloc. If the pointer numBytes is not NULL the number of non-null - * bytes written to the array is stored in the integer it references. - */ + if ( _ds.string == NULL) { -MODULE_SCOPE char* -TkNSStringToUtf( - NSString *string, - int *numBytes) -{ - unsigned int code; - int i; - char *ptr, *bytes = ckalloc(6*[string length] + 1); - - ptr = bytes; - if (ptr) { - for (i = 0; i < [string length]; i++) { - ptr += TkUtfAtIndex(string, i, ptr, &code); - if (code > 0xffff){ - i++; + /* + * The DString has not been initialized. Construct it from + * our string's unicode characters. + */ + + char buffer[2*TCL_UTF_MAX]; + unsigned int index, length, ch; + + Tcl_DStringInit(&_ds); +#if TCL_UTF_MAX == 3 + for (index = 0; index < [_string length]; index++) { + UniChar uni = [_string characterAtIndex: index]; + + if (CFStringIsSurrogateHighCharacter(uni)) { + UniChar low = [_string characterAtIndex: ++index]; + ch = CFStringGetLongCharacterForSurrogatePair(uni, low); + } else { + ch = uni; } + length = TkUniCharToUtf(ch, buffer); + Tcl_DStringAppend(&_ds, buffer, length); } - *ptr = '\0'; - } - if (numBytes) { - *numBytes = ptr - bytes; +#else + for (index = 0; index < [_string length]; index++) { + ch = (int) [_string characterAtIndex: index]; + length = Tcl_UniCharToUtf(ch, buffer); + Tcl_DStringAppend(&_ds, buffer, length); + } + +#endif } - return bytes; + return _ds; } + +#ifndef __clang__ +@synthesize UTF8String = _UTF8String; +#endif +@end #define GetNSFontTraitsFromTkFontAttributes(faPtr) \ ((faPtr)->weight == TK_FW_BOLD ? NSBoldFontMask : NSUnboldFontMask) | \ |