The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_text_AndroidCharacter.cpp |
| 2 | ** |
| 3 | ** Copyright 2006, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 5 | ** Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | ** you may not use this file except in compliance with the License. |
| 7 | ** You may obtain a copy of the License at |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 11 | ** Unless required by applicable law or agreed to in writing, software |
| 12 | ** distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | ** See the License for the specific language governing permissions and |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "AndroidUnicode" |
| 19 | |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 20 | #include <nativehelper/JNIHelp.h> |
| 21 | #include <nativehelper/ScopedPrimitiveArray.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 22 | #include "core_jni_helpers.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 23 | #include "utils/misc.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 24 | #include "utils/Log.h" |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 25 | #include "unicode/uchar.h" |
| 26 | |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 27 | #define PROPERTY_UNDEFINED (-1) |
Victor Chang | c1463ec | 2018-11-27 12:40:23 +0000 | [diff] [blame] | 28 | #define JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY 18 |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 29 | |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 30 | // ICU => JDK mapping |
Victor Chang | c1463ec | 2018-11-27 12:40:23 +0000 | [diff] [blame] | 31 | static int directionality_map[JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY + 1] = { |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 32 | 0, // U_LEFT_TO_RIGHT (0) => DIRECTIONALITY_LEFT_TO_RIGHT (0) |
| 33 | 1, // U_RIGHT_TO_LEFT (1) => DIRECTIONALITY_RIGHT_TO_LEFT (1) |
| 34 | 3, // U_EUROPEAN_NUMBER (2) => DIRECTIONALITY_EUROPEAN_NUMBER (3) |
| 35 | 4, // U_EUROPEAN_NUMBER_SEPARATOR (3) => DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR (4) |
| 36 | 5, // U_EUROPEAN_NUMBER_TERMINATOR (4) => DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR (5) |
| 37 | 6, // U_ARABIC_NUMBER (5) => DIRECTIONALITY_ARABIC_NUMBER (6) |
| 38 | 7, // U_COMMON_NUMBER_SEPARATOR (6) => DIRECTIONALITY_COMMON_NUMBER_SEPARATOR (7) |
| 39 | 10, // U_BLOCK_SEPARATOR (7) => DIRECTIONALITY_PARAGRAPH_SEPARATOR (10) |
| 40 | 11, // U_SEGMENT_SEPARATOR (8) => DIRECTIONALITY_SEGMENT_SEPARATOR (11) |
| 41 | 12, // U_WHITE_SPACE_NEUTRAL (9) => DIRECTIONALITY_WHITESPACE (12) |
| 42 | 13, // U_OTHER_NEUTRAL (10) => DIRECTIONALITY_OTHER_NEUTRALS (13) |
| 43 | 14, // U_LEFT_TO_RIGHT_EMBEDDING (11) => DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING (14) |
| 44 | 15, // U_LEFT_TO_RIGHT_OVERRIDE (12) => DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE (15) |
| 45 | 2, // U_RIGHT_TO_LEFT_ARABIC (13) => DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC (2) |
| 46 | 16, // U_RIGHT_TO_LEFT_EMBEDDING (14) => DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING (16) |
| 47 | 17, // U_RIGHT_TO_LEFT_OVERRIDE (15) => DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE (17) |
| 48 | 18, // U_POP_DIRECTIONAL_FORMAT (16) => DIRECTIONALITY_POP_DIRECTIONAL_FORMAT (18) |
| 49 | 8, // U_DIR_NON_SPACING_MARK (17) => DIRECTIONALITY_NONSPACING_MARK (8) |
| 50 | 9, // U_BOUNDARY_NEUTRAL (18) => DIRECTIONALITY_BOUNDARY_NEUTRAL (9) |
| 51 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 52 | |
| 53 | namespace android { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 54 | |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 55 | static void getDirectionalities(JNIEnv* env, jobject obj, jcharArray srcArray, |
| 56 | jbyteArray destArray, jint count) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 57 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 58 | ScopedCharArrayRO src(env, srcArray); |
| 59 | if (src.get() == NULL) { |
| 60 | return; |
| 61 | } |
| 62 | ScopedByteArrayRW dest(env, destArray); |
| 63 | if (dest.get() == NULL) { |
| 64 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 65 | } |
| 66 | |
| 67 | if (env->GetArrayLength(srcArray) < count || env->GetArrayLength(destArray) < count) { |
| 68 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 69 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 70 | } |
| 71 | |
| 72 | for (int i = 0; i < count; i++) { |
| 73 | if (src[i] >= 0xD800 && src[i] <= 0xDBFF && |
| 74 | i + 1 < count && |
| 75 | src[i + 1] >= 0xDC00 && src[i + 1] <= 0xDFFF) { |
| 76 | int c = 0x00010000 + ((src[i] - 0xD800) << 10) + |
| 77 | (src[i + 1] & 0x3FF); |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 78 | int dir = u_charDirection(c); |
Victor Chang | 749d184 | 2018-11-27 16:43:20 +0000 | [diff] [blame] | 79 | if (dir < 0 || dir > JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY |
| 80 | || u_charType(c) == U_UNASSIGNED) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 81 | dir = PROPERTY_UNDEFINED; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 82 | else |
| 83 | dir = directionality_map[dir]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 84 | |
| 85 | dest[i++] = dir; |
| 86 | dest[i] = dir; |
| 87 | } else { |
| 88 | int c = src[i]; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 89 | int dir = u_charDirection(c); |
Victor Chang | 749d184 | 2018-11-27 16:43:20 +0000 | [diff] [blame] | 90 | if (dir < 0 || dir > JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY |
| 91 | || u_charType(c) == U_UNASSIGNED) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 92 | dest[i] = PROPERTY_UNDEFINED; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 93 | else |
| 94 | dest[i] = directionality_map[dir]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 95 | } |
| 96 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 99 | static jint getEastAsianWidth(JNIEnv* env, jobject obj, jchar input) |
| 100 | { |
| 101 | int width = u_getIntPropertyValue(input, UCHAR_EAST_ASIAN_WIDTH); |
Victor Chang | c1463ec | 2018-11-27 12:40:23 +0000 | [diff] [blame] | 102 | if (width < 0 || width > u_getIntPropertyMaxValue(UCHAR_EAST_ASIAN_WIDTH)) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 103 | width = PROPERTY_UNDEFINED; |
| 104 | |
| 105 | return width; |
| 106 | } |
| 107 | |
| 108 | static void getEastAsianWidths(JNIEnv* env, jobject obj, jcharArray srcArray, |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 109 | jint start, jint count, jbyteArray destArray) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 110 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 111 | ScopedCharArrayRO src(env, srcArray); |
| 112 | if (src.get() == NULL) { |
| 113 | return; |
| 114 | } |
| 115 | ScopedByteArrayRW dest(env, destArray); |
| 116 | if (dest.get() == NULL) { |
| 117 | return; |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | if (start < 0 || start > start + count |
| 121 | || env->GetArrayLength(srcArray) < (start + count) |
| 122 | || env->GetArrayLength(destArray) < count) { |
| 123 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 124 | return; |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 125 | } |
| 126 | |
Victor Chang | c1463ec | 2018-11-27 12:40:23 +0000 | [diff] [blame] | 127 | int maxWidth = u_getIntPropertyMaxValue(UCHAR_EAST_ASIAN_WIDTH); |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 128 | for (int i = 0; i < count; i++) { |
| 129 | const int srci = start + i; |
| 130 | if (src[srci] >= 0xD800 && src[srci] <= 0xDBFF && |
| 131 | i + 1 < count && |
| 132 | src[srci + 1] >= 0xDC00 && src[srci + 1] <= 0xDFFF) { |
| 133 | int c = 0x00010000 + ((src[srci] - 0xD800) << 10) + |
| 134 | (src[srci + 1] & 0x3FF); |
| 135 | int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH); |
Victor Chang | c1463ec | 2018-11-27 12:40:23 +0000 | [diff] [blame] | 136 | if (width < 0 || width > maxWidth) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 137 | width = PROPERTY_UNDEFINED; |
| 138 | |
| 139 | dest[i++] = width; |
| 140 | dest[i] = width; |
| 141 | } else { |
| 142 | int c = src[srci]; |
| 143 | int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH); |
Victor Chang | c1463ec | 2018-11-27 12:40:23 +0000 | [diff] [blame] | 144 | if (width < 0 || width > maxWidth) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 145 | width = PROPERTY_UNDEFINED; |
| 146 | |
| 147 | dest[i] = width; |
| 148 | } |
| 149 | } |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 150 | } |
| 151 | |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 152 | static jboolean mirror(JNIEnv* env, jobject obj, jcharArray charArray, jint start, jint count) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 154 | ScopedCharArrayRW data(env, charArray); |
| 155 | if (data.get() == NULL) { |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 156 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Kenny Root | 073a3d5 | 2010-02-17 08:25:47 -0800 | [diff] [blame] | 159 | if (start < 0 || start > start + count |
| 160 | || env->GetArrayLength(charArray) < start + count) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 161 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 162 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 163 | } |
| 164 | |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 165 | jboolean ret = JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 166 | for (int i = start; i < start + count; i++) { |
| 167 | // XXX this thinks it knows that surrogates are never mirrored |
| 168 | |
| 169 | int c1 = data[i]; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 170 | int c2 = u_charMirror(c1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | |
| 172 | if (c1 != c2) { |
| 173 | data[i] = c2; |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 174 | ret = JNI_TRUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 175 | } |
| 176 | } |
Raph Levien | e250716 | 2012-06-18 16:17:30 -0700 | [diff] [blame] | 177 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | static jchar getMirror(JNIEnv* env, jobject obj, jchar c) |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 181 | { |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 182 | return u_charMirror(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 183 | } |
| 184 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 185 | static const JNINativeMethod gMethods[] = { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 186 | { "getDirectionalities", "([C[BI)V", |
| 187 | (void*) getDirectionalities }, |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 188 | { "getEastAsianWidth", "(C)I", |
| 189 | (void*) getEastAsianWidth }, |
| 190 | { "getEastAsianWidths", "([CII[B)V", |
| 191 | (void*) getEastAsianWidths }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 192 | { "mirror", "([CII)Z", |
| 193 | (void*) mirror }, |
| 194 | { "getMirror", "(C)C", |
| 195 | (void*) getMirror } |
| 196 | }; |
| 197 | |
| 198 | int register_android_text_AndroidCharacter(JNIEnv* env) |
| 199 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 200 | return RegisterMethodsOrDie(env, "android/text/AndroidCharacter", gMethods, NELEM(gMethods)); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | } |