From 55634b5f79ffb723eebe4a2f6c773213011e6a61 Mon Sep 17 00:00:00 2001 From: Po Lu Date: Thu, 9 Mar 2023 10:50:03 +0800 Subject: [PATCH] Update Android port * src/android.c (android_build_string): Convert the text to UTF-16, and create the Java string using that. (android_build_jstring): Update comment. * src/androidmenu.c (android_init_emacs_context_menu): Add String argument to `addItem'. (android_menu_show): Correctly pass help strings in regular menu items. * src/sfnt.c (_sfnt_swap16, _sfnt_swap32): Avoid reserved names. --- src/android.c | 38 +++++++++++++++++++++++++++++--------- src/androidmenu.c | 17 +++++++++++++++-- src/sfnt.c | 8 ++++---- 3 files changed, 48 insertions(+), 15 deletions(-) diff --git a/src/android.c b/src/android.c index e8f076771b9..5420fbde9b9 100644 --- a/src/android.c +++ b/src/android.c @@ -5244,28 +5244,48 @@ android_build_string (Lisp_Object text) { Lisp_Object encoded; jstring string; + size_t nchars; + jchar *characters; + USE_SAFE_ALLOCA; - encoded = ENCODE_UTF_8 (text); + encoded = code_convert_string_norecord (text, Qutf_16le, + true); + nchars = (SBYTES (encoded) / sizeof (jchar)); - /* Note that Java expects this string to be in ``modified UTF - encoding'', which is actually UTF-8, except with NUL encoded as a - two-byte sequence. The only consequence of passing an actual - UTF-8 string is that NUL bytes cannot be represented, which is - not really of consequence. */ - string = (*android_java_env)->NewStringUTF (android_java_env, - SSDATA (encoded)); + /* Encode the string as UTF-16 prior to creating the string. + Copy the string to a separate buffer in order to preserve + alignment. */ + + characters = SAFE_ALLOCA (SBYTES (encoded)); + memcpy (characters, SDATA (encoded), SBYTES (encoded)); + + /* Create the string. */ + string + = (*android_java_env)->NewString (android_java_env, + characters, nchars); android_exception_check (); + SAFE_FREE (); return string; } -/* Do the same, except TEXT is constant string data. */ +/* Do the same, except TEXT is constant string data in ASCII or + UTF-8 containing no characters outside the Basic Multilingual + Plane. */ jstring android_build_jstring (const char *text) { jstring string; + /* Note that Java expects this string to be in ``modified UTF + encoding'', which is actually UTF-8, except with NUL + encoded as a two-byte sequence, and surrogate pairs encoded + in the three-byte extended encoding. The only consequence + of passing an actual UTF-8 string is that NUL bytes and + characters requiring surrogate pairs cannot be represented, + which is not really of consequence. */ + string = (*android_java_env)->NewStringUTF (android_java_env, text); android_exception_check (); diff --git a/src/androidmenu.c b/src/androidmenu.c index 2ba11aa1663..540b25cf602 100644 --- a/src/androidmenu.c +++ b/src/androidmenu.c @@ -98,7 +98,8 @@ android_init_emacs_context_menu (void) FIND_METHOD_STATIC (create_context_menu, "createContextMenu", "(Ljava/lang/String;)Lorg/gnu/emacs/EmacsContextMenu;"); - FIND_METHOD (add_item, "addItem", "(ILjava/lang/String;ZZZ)V"); + FIND_METHOD (add_item, "addItem", "(ILjava/lang/String;ZZZ" + "Ljava/lang/String;)V"); FIND_METHOD (add_submenu, "addSubmenu", "(Ljava/lang/String;" "Ljava/lang/String;Ljava/lang/String;)" "Lorg/gnu/emacs/EmacsContextMenu;"); @@ -411,6 +412,14 @@ android_menu_show (struct frame *f, int x, int y, int menuflags, title_string = (!NILP (item_name) ? android_build_string (item_name) : NULL); + help_string = NULL; + + /* Menu items can have tool tips on Android 26 and + later. In this case, set it to the help string. */ + + if (android_get_current_api_level () >= 26 + && STRINGP (help)) + help_string = android_build_string (help); /* Determine whether or not to display a check box. */ @@ -424,11 +433,15 @@ android_menu_show (struct frame *f, int x, int y, int menuflags, title_string, (jboolean) !NILP (enable), (jboolean) checkmark, - (jboolean) !NILP (selected)); + (jboolean) !NILP (selected), + help_string); android_exception_check (); if (title_string) ANDROID_DELETE_LOCAL_REF (title_string); + + if (help_string) + ANDROID_DELETE_LOCAL_REF (help_string); } i += MENU_ITEMS_ITEM_LENGTH; diff --git a/src/sfnt.c b/src/sfnt.c index b4f587a4690..5b219bf6369 100644 --- a/src/sfnt.c +++ b/src/sfnt.c @@ -155,7 +155,7 @@ static uint32_t sfnt_table_names[] = /* Swap values from TrueType to system byte order. */ static void -_sfnt_swap16 (uint16_t *value) +sfnt_swap16_1 (uint16_t *value) { #ifndef WORDS_BIGENDIAN *value = bswap_16 (*value); @@ -163,15 +163,15 @@ _sfnt_swap16 (uint16_t *value) } static void -_sfnt_swap32 (uint32_t *value) +sfnt_swap32_1 (uint32_t *value) { #ifndef WORDS_BIGENDIAN *value = bswap_32 (*value); #endif } -#define sfnt_swap16(what) (_sfnt_swap16 ((uint16_t *) (what))) -#define sfnt_swap32(what) (_sfnt_swap32 ((uint32_t *) (what))) +#define sfnt_swap16(what) (sfnt_swap16_1 ((uint16_t *) (what))) +#define sfnt_swap32(what) (sfnt_swap32_1 ((uint32_t *) (what))) /* Read the table directory from the file FD. FD must currently be at the start of the file (or an offset defined in the TTC header, if -- 2.39.5