From 68c977ee4d5cb3964d8f0fb216a1139ac22bf9bf Mon Sep 17 00:00:00 2001 From: Daijiro Fukuda Date: Mon, 23 May 2022 16:33:52 +0900 Subject: [PATCH] macOS: Use UTF-32 for preedit text encoding The char stream of GLFW API must be UTF-32. --- src/cocoa_window.m | 29 +++++++++++++++++++++++------ 1 file changed, 23 insertions(+), 6 deletions(-) diff --git a/src/cocoa_window.m b/src/cocoa_window.m index 44f33a5e..1ac8990c 100644 --- a/src/cocoa_window.m +++ b/src/cocoa_window.m @@ -696,7 +696,7 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; NSString* markedTextString = markedText.string; - NSUInteger i, length = [markedTextString length]; + NSUInteger length = [markedTextString length]; int ctext = window->ctext; while (ctext < length + 1) { @@ -711,13 +711,30 @@ static const NSRange kEmptyRange = { NSNotFound, 0 }; window->preeditText = preeditText; window->ctext = ctext; } - window->ntext = length; - window->preeditText[length] = 0; - for (i = 0; i < length; i++) + + NSInteger preeditTextLength = 0; + NSRange range = NSMakeRange(0, length); + while (range.length) { - const unichar codepoint = [markedTextString characterAtIndex:i]; - window->preeditText[i] = codepoint; + uint32_t codepoint = 0; + + if ([markedTextString getBytes:&codepoint + maxLength:sizeof(codepoint) + usedLength:NULL + encoding:NSUTF32StringEncoding + options:0 + range:range + remainingRange:&range]) + { + if (codepoint >= 0xf700 && codepoint <= 0xf7ff) + continue; + + window->preeditText[preeditTextLength++] = codepoint; + } } + window->ntext = preeditTextLength; + window->preeditText[preeditTextLength] = 0; + int focusedBlock = 0; NSInteger offset = 0; window->nblocks = 0;