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 | |
Jim Huang | 1fc4d88 | 2010-10-17 21:08:53 +0800 | [diff] [blame] | 20 | #include "JNIHelp.h" |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 21 | #include "ScopedPrimitiveArray.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 22 | #include <android_runtime/AndroidRuntime.h> |
| 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) |
| 28 | |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 29 | // ICU => JDK mapping |
| 30 | static int directionality_map[U_CHAR_DIRECTION_COUNT] = { |
| 31 | 0, // U_LEFT_TO_RIGHT (0) => DIRECTIONALITY_LEFT_TO_RIGHT (0) |
| 32 | 1, // U_RIGHT_TO_LEFT (1) => DIRECTIONALITY_RIGHT_TO_LEFT (1) |
| 33 | 3, // U_EUROPEAN_NUMBER (2) => DIRECTIONALITY_EUROPEAN_NUMBER (3) |
| 34 | 4, // U_EUROPEAN_NUMBER_SEPARATOR (3) => DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR (4) |
| 35 | 5, // U_EUROPEAN_NUMBER_TERMINATOR (4) => DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR (5) |
| 36 | 6, // U_ARABIC_NUMBER (5) => DIRECTIONALITY_ARABIC_NUMBER (6) |
| 37 | 7, // U_COMMON_NUMBER_SEPARATOR (6) => DIRECTIONALITY_COMMON_NUMBER_SEPARATOR (7) |
| 38 | 10, // U_BLOCK_SEPARATOR (7) => DIRECTIONALITY_PARAGRAPH_SEPARATOR (10) |
| 39 | 11, // U_SEGMENT_SEPARATOR (8) => DIRECTIONALITY_SEGMENT_SEPARATOR (11) |
| 40 | 12, // U_WHITE_SPACE_NEUTRAL (9) => DIRECTIONALITY_WHITESPACE (12) |
| 41 | 13, // U_OTHER_NEUTRAL (10) => DIRECTIONALITY_OTHER_NEUTRALS (13) |
| 42 | 14, // U_LEFT_TO_RIGHT_EMBEDDING (11) => DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING (14) |
| 43 | 15, // U_LEFT_TO_RIGHT_OVERRIDE (12) => DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE (15) |
| 44 | 2, // U_RIGHT_TO_LEFT_ARABIC (13) => DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC (2) |
| 45 | 16, // U_RIGHT_TO_LEFT_EMBEDDING (14) => DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING (16) |
| 46 | 17, // U_RIGHT_TO_LEFT_OVERRIDE (15) => DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE (17) |
| 47 | 18, // U_POP_DIRECTIONAL_FORMAT (16) => DIRECTIONALITY_POP_DIRECTIONAL_FORMAT (18) |
| 48 | 8, // U_DIR_NON_SPACING_MARK (17) => DIRECTIONALITY_NONSPACING_MARK (8) |
| 49 | 9, // U_BOUNDARY_NEUTRAL (18) => DIRECTIONALITY_BOUNDARY_NEUTRAL (9) |
| 50 | }; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 51 | |
| 52 | namespace android { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 53 | |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 54 | static void getDirectionalities(JNIEnv* env, jobject obj, jcharArray srcArray, |
| 55 | jbyteArray destArray, jint count) |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 56 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 57 | ScopedCharArrayRO src(env, srcArray); |
| 58 | if (src.get() == NULL) { |
| 59 | return; |
| 60 | } |
| 61 | ScopedByteArrayRW dest(env, destArray); |
| 62 | if (dest.get() == NULL) { |
| 63 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | if (env->GetArrayLength(srcArray) < count || env->GetArrayLength(destArray) < count) { |
| 67 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 68 | return; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | for (int i = 0; i < count; i++) { |
| 72 | if (src[i] >= 0xD800 && src[i] <= 0xDBFF && |
| 73 | i + 1 < count && |
| 74 | src[i + 1] >= 0xDC00 && src[i + 1] <= 0xDFFF) { |
| 75 | int c = 0x00010000 + ((src[i] - 0xD800) << 10) + |
| 76 | (src[i + 1] & 0x3FF); |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 77 | int dir = u_charDirection(c); |
| 78 | if (dir < 0 || dir >= U_CHAR_DIRECTION_COUNT) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 79 | dir = PROPERTY_UNDEFINED; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 80 | else |
| 81 | dir = directionality_map[dir]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 82 | |
| 83 | dest[i++] = dir; |
| 84 | dest[i] = dir; |
| 85 | } else { |
| 86 | int c = src[i]; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 87 | int dir = u_charDirection(c); |
| 88 | if (dir < 0 || dir >= U_CHAR_DIRECTION_COUNT) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 89 | dest[i] = PROPERTY_UNDEFINED; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 90 | else |
| 91 | dest[i] = directionality_map[dir]; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 94 | } |
| 95 | |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 96 | static jint getEastAsianWidth(JNIEnv* env, jobject obj, jchar input) |
| 97 | { |
| 98 | int width = u_getIntPropertyValue(input, UCHAR_EAST_ASIAN_WIDTH); |
| 99 | if (width < 0 || width >= U_EA_COUNT) |
| 100 | width = PROPERTY_UNDEFINED; |
| 101 | |
| 102 | return width; |
| 103 | } |
| 104 | |
| 105 | static void getEastAsianWidths(JNIEnv* env, jobject obj, jcharArray srcArray, |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 106 | jint start, jint count, jbyteArray destArray) |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 107 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 108 | ScopedCharArrayRO src(env, srcArray); |
| 109 | if (src.get() == NULL) { |
| 110 | return; |
| 111 | } |
| 112 | ScopedByteArrayRW dest(env, destArray); |
| 113 | if (dest.get() == NULL) { |
| 114 | return; |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | if (start < 0 || start > start + count |
| 118 | || env->GetArrayLength(srcArray) < (start + count) |
| 119 | || env->GetArrayLength(destArray) < count) { |
| 120 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 121 | return; |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 122 | } |
| 123 | |
| 124 | for (int i = 0; i < count; i++) { |
| 125 | const int srci = start + i; |
| 126 | if (src[srci] >= 0xD800 && src[srci] <= 0xDBFF && |
| 127 | i + 1 < count && |
| 128 | src[srci + 1] >= 0xDC00 && src[srci + 1] <= 0xDFFF) { |
| 129 | int c = 0x00010000 + ((src[srci] - 0xD800) << 10) + |
| 130 | (src[srci + 1] & 0x3FF); |
| 131 | int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH); |
| 132 | if (width < 0 || width >= U_EA_COUNT) |
| 133 | width = PROPERTY_UNDEFINED; |
| 134 | |
| 135 | dest[i++] = width; |
| 136 | dest[i] = width; |
| 137 | } else { |
| 138 | int c = src[srci]; |
| 139 | int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH); |
| 140 | if (width < 0 || width >= U_EA_COUNT) |
| 141 | width = PROPERTY_UNDEFINED; |
| 142 | |
| 143 | dest[i] = width; |
| 144 | } |
| 145 | } |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 146 | } |
| 147 | |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 148 | 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] | 149 | { |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 150 | ScopedCharArrayRW data(env, charArray); |
| 151 | if (data.get() == NULL) { |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 152 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Kenny Root | 073a3d5 | 2010-02-17 08:25:47 -0800 | [diff] [blame] | 155 | if (start < 0 || start > start + count |
| 156 | || env->GetArrayLength(charArray) < start + count) { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 157 | jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL); |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 158 | return JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 161 | jboolean ret = JNI_FALSE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 162 | for (int i = start; i < start + count; i++) { |
| 163 | // XXX this thinks it knows that surrogates are never mirrored |
| 164 | |
| 165 | int c1 = data[i]; |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 166 | int c2 = u_charMirror(c1); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 167 | |
| 168 | if (c1 != c2) { |
| 169 | data[i] = c2; |
Ashok Bhat | f5df700 | 2014-03-25 20:51:35 +0000 | [diff] [blame] | 170 | ret = JNI_TRUE; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 171 | } |
| 172 | } |
Raph Levien | e250716 | 2012-06-18 16:17:30 -0700 | [diff] [blame] | 173 | return ret; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | static jchar getMirror(JNIEnv* env, jobject obj, jchar c) |
Elliott Hughes | 69a017b | 2011-04-08 14:10:28 -0700 | [diff] [blame] | 177 | { |
Kenny Root | a9886c5 | 2010-02-12 14:09:24 -0800 | [diff] [blame] | 178 | return u_charMirror(c); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 179 | } |
| 180 | |
| 181 | static JNINativeMethod gMethods[] = { |
| 182 | { "getDirectionalities", "([C[BI)V", |
| 183 | (void*) getDirectionalities }, |
Kenny Root | bb9a517 | 2010-02-13 00:07:38 -0800 | [diff] [blame] | 184 | { "getEastAsianWidth", "(C)I", |
| 185 | (void*) getEastAsianWidth }, |
| 186 | { "getEastAsianWidths", "([CII[B)V", |
| 187 | (void*) getEastAsianWidths }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 188 | { "mirror", "([CII)Z", |
| 189 | (void*) mirror }, |
| 190 | { "getMirror", "(C)C", |
| 191 | (void*) getMirror } |
| 192 | }; |
| 193 | |
| 194 | int register_android_text_AndroidCharacter(JNIEnv* env) |
| 195 | { |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 196 | return AndroidRuntime::registerNativeMethods(env, "android/text/AndroidCharacter", |
| 197 | gMethods, NELEM(gMethods)); |
| 198 | } |
| 199 | |
| 200 | } |