Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 3a507b8

Browse filesBrowse files
livecodefraserlivecodeali
authored andcommitted
Fix iOS text input for non-BMP codepoints
Only one codeunit was being sent to fields, causing corruption of any non-BMP codepoints entered using the iOS keyboard (in particular, emoji).
1 parent 871341e commit 3a507b8
Copy full SHA for 3a507b8

File tree

Expand file treeCollapse file tree

2 files changed

+57
-22
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+57
-22
lines changed

‎engine/src/eventqueue.cpp

Copy file name to clipboardExpand all lines: engine/src/eventqueue.cpp
+6-4Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -496,13 +496,15 @@ static void MCEventQueueDispatchEvent(MCEvent *p_event)
496496
}
497497

498498
// Otherwise 'char_code' is the unicode codepoint, so first map to
499-
// UTF-16 (not done properly yet...)
500-
unichar_t t_unichar;
501-
t_unichar = (unichar_t)t_event -> key . press . char_code;
499+
// UTF-16 codeunits
500+
unichar_t t_unichars[2];
501+
uindex_t t_length = 1;
502+
if (MCUnicodeCodepointToSurrogates(t_event->key.press.char_code, t_unichars))
503+
t_length = 2;
502504

503505
// Now the string is created with the appropriate unicode-capable function
504506
MCAutoStringRef t_buffer;
505-
MCStringCreateWithChars(&t_unichar, 1, &t_buffer);
507+
MCStringCreateWithChars(t_unichars, t_length, &t_buffer);
506508
t_target -> kdown(*t_buffer, t_event -> key . press . key_code);
507509
t_target -> kup(*t_buffer, t_event -> key . press . key_code);
508510
}

‎engine/src/mbliphoneappview.mm

Copy file name to clipboardExpand all lines: engine/src/mbliphoneappview.mm
+51-18Lines changed: 51 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -254,26 +254,59 @@ - (void)textViewDidEndEditing: (UITextView *)textView
254254

255255
- (BOOL)textView: (UITextView *)textView shouldChangeTextInRange: (NSRange)range replacementText: (NSString*)text
256256
{
257-
uint32_t t_key_code, t_char_code;
258-
// PM-2015-01-16: Prevent crash when deleting accented characters
259-
if (range . location == 0 || [text length] == 0)
257+
// Handle deletion
258+
if (range . location == 0 || [text length] == 0)
260259
{
261-
t_key_code = 0xff08;
262-
t_char_code = 0;
260+
MCIPhoneHandleProcessTextInput(0, 0xff08);
261+
return NO;
263262
}
264-
else
265-
{
266-
t_key_code = 0;
267-
t_char_code = [text characterAtIndex: 0];
268-
}
269-
270-
if (t_char_code == 10)
271-
{
272-
t_key_code = 0xff0d;
273-
t_char_code = 0;
274-
}
275-
276-
MCIPhoneHandleProcessTextInput(t_char_code, t_key_code);
263+
264+
// Handle newlines
265+
if ([text characterAtIndex:0] == 0x0a)
266+
{
267+
MCIPhoneHandleProcessTextInput(0, 0xff0d);
268+
return NO;
269+
}
270+
271+
// Loop over the UTF-16 codeunits and insert them as codepoints
272+
bool t_is_surrogate = false;
273+
unichar_t t_surrogate = 0;
274+
for (NSUInteger i = 0; i < [text length]; i++)
275+
{
276+
// Get the next character
277+
unichar_t t_char = [text characterAtIndex:i];
278+
279+
// Process based on whether we are dealing with a surrogate or not
280+
codepoint_t t_codepoint;
281+
if (t_is_surrogate)
282+
{
283+
// We're expecting a trailing surrogate
284+
if (!MCUnicodeCodepointIsTrailingSurrogate(t_char))
285+
{
286+
// Something has gone wrong... abort processing of this text
287+
return NO;
288+
}
289+
290+
// Re-combine the surrogate
291+
t_codepoint = MCUnicodeCombineSurrogates(t_surrogate, t_char);
292+
t_is_surrogate = false;
293+
294+
}
295+
else if (MCUnicodeCodepointIsLeadingSurrogate(t_char))
296+
{
297+
// Wait for the second half of the pair
298+
t_is_surrogate = true;
299+
t_surrogate = t_char;
300+
continue;
301+
}
302+
else
303+
{
304+
// Not a surrogate - use directly as a codepoint
305+
t_codepoint = t_char;
306+
}
307+
308+
MCIPhoneHandleProcessTextInput(t_codepoint, 0);
309+
}
277310

278311
return NO;
279312
}

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.