blob: dacbe41c3a964d734670e91a85a39cbb4b98cfdc [file] [log] [blame]
The Android Open Source Project9066cfe2009-03-03 19:31:44 -08001/* //device/libs/android_runtime/android_text_AndroidCharacter.cpp
2**
3** Copyright 2006, The Android Open Source Project
4**
Elliott Hughes69a017b2011-04-08 14:10:28 -07005** 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 Project9066cfe2009-03-03 19:31:44 -08008**
Elliott Hughes69a017b2011-04-08 14:10:28 -07009** http://www.apache.org/licenses/LICENSE-2.0
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080010**
Elliott Hughes69a017b2011-04-08 14:10:28 -070011** 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 Project9066cfe2009-03-03 19:31:44 -080015** limitations under the License.
16*/
17
18#define LOG_TAG "AndroidUnicode"
19
Jim Huang1fc4d882010-10-17 21:08:53 +080020#include "JNIHelp.h"
Elliott Hughes69a017b2011-04-08 14:10:28 -070021#include "ScopedPrimitiveArray.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080022#include <android_runtime/AndroidRuntime.h>
23#include "utils/misc.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080024#include "utils/Log.h"
Kenny Roota9886c52010-02-12 14:09:24 -080025#include "unicode/uchar.h"
26
Kenny Rootbb9a5172010-02-13 00:07:38 -080027#define PROPERTY_UNDEFINED (-1)
28
Kenny Roota9886c52010-02-12 14:09:24 -080029// ICU => JDK mapping
30static 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 Project9066cfe2009-03-03 19:31:44 -080051
52namespace android {
Elliott Hughes69a017b2011-04-08 14:10:28 -070053
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080054static void getDirectionalities(JNIEnv* env, jobject obj, jcharArray srcArray, jbyteArray destArray, int count)
55{
Elliott Hughes69a017b2011-04-08 14:10:28 -070056 ScopedCharArrayRO src(env, srcArray);
57 if (src.get() == NULL) {
58 return;
59 }
60 ScopedByteArrayRW dest(env, destArray);
61 if (dest.get() == NULL) {
62 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080063 }
64
65 if (env->GetArrayLength(srcArray) < count || env->GetArrayLength(destArray) < count) {
66 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
Elliott Hughes69a017b2011-04-08 14:10:28 -070067 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080068 }
69
70 for (int i = 0; i < count; i++) {
71 if (src[i] >= 0xD800 && src[i] <= 0xDBFF &&
72 i + 1 < count &&
73 src[i + 1] >= 0xDC00 && src[i + 1] <= 0xDFFF) {
74 int c = 0x00010000 + ((src[i] - 0xD800) << 10) +
75 (src[i + 1] & 0x3FF);
Kenny Roota9886c52010-02-12 14:09:24 -080076 int dir = u_charDirection(c);
77 if (dir < 0 || dir >= U_CHAR_DIRECTION_COUNT)
Kenny Rootbb9a5172010-02-13 00:07:38 -080078 dir = PROPERTY_UNDEFINED;
Kenny Roota9886c52010-02-12 14:09:24 -080079 else
80 dir = directionality_map[dir];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080081
82 dest[i++] = dir;
83 dest[i] = dir;
84 } else {
85 int c = src[i];
Kenny Roota9886c52010-02-12 14:09:24 -080086 int dir = u_charDirection(c);
87 if (dir < 0 || dir >= U_CHAR_DIRECTION_COUNT)
Kenny Rootbb9a5172010-02-13 00:07:38 -080088 dest[i] = PROPERTY_UNDEFINED;
Kenny Roota9886c52010-02-12 14:09:24 -080089 else
90 dest[i] = directionality_map[dir];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080091 }
92 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080093}
94
Kenny Rootbb9a5172010-02-13 00:07:38 -080095static jint getEastAsianWidth(JNIEnv* env, jobject obj, jchar input)
96{
97 int width = u_getIntPropertyValue(input, UCHAR_EAST_ASIAN_WIDTH);
98 if (width < 0 || width >= U_EA_COUNT)
99 width = PROPERTY_UNDEFINED;
100
101 return width;
102}
103
104static void getEastAsianWidths(JNIEnv* env, jobject obj, jcharArray srcArray,
105 int start, int count, jbyteArray destArray)
106{
Elliott Hughes69a017b2011-04-08 14:10:28 -0700107 ScopedCharArrayRO src(env, srcArray);
108 if (src.get() == NULL) {
109 return;
110 }
111 ScopedByteArrayRW dest(env, destArray);
112 if (dest.get() == NULL) {
113 return;
Kenny Rootbb9a5172010-02-13 00:07:38 -0800114 }
115
116 if (start < 0 || start > start + count
117 || env->GetArrayLength(srcArray) < (start + count)
118 || env->GetArrayLength(destArray) < count) {
119 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700120 return;
Kenny Rootbb9a5172010-02-13 00:07:38 -0800121 }
122
123 for (int i = 0; i < count; i++) {
124 const int srci = start + i;
125 if (src[srci] >= 0xD800 && src[srci] <= 0xDBFF &&
126 i + 1 < count &&
127 src[srci + 1] >= 0xDC00 && src[srci + 1] <= 0xDFFF) {
128 int c = 0x00010000 + ((src[srci] - 0xD800) << 10) +
129 (src[srci + 1] & 0x3FF);
130 int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH);
131 if (width < 0 || width >= U_EA_COUNT)
132 width = PROPERTY_UNDEFINED;
133
134 dest[i++] = width;
135 dest[i] = width;
136 } else {
137 int c = src[srci];
138 int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH);
139 if (width < 0 || width >= U_EA_COUNT)
140 width = PROPERTY_UNDEFINED;
141
142 dest[i] = width;
143 }
144 }
Kenny Rootbb9a5172010-02-13 00:07:38 -0800145}
146
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800147static jboolean mirror(JNIEnv* env, jobject obj, jcharArray charArray, int start, int count)
148{
Elliott Hughes69a017b2011-04-08 14:10:28 -0700149 ScopedCharArrayRW data(env, charArray);
150 if (data.get() == NULL) {
151 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800152 }
153
Kenny Root073a3d52010-02-17 08:25:47 -0800154 if (start < 0 || start > start + count
155 || env->GetArrayLength(charArray) < start + count) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800156 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700157 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800158 }
159
160 for (int i = start; i < start + count; i++) {
161 // XXX this thinks it knows that surrogates are never mirrored
162
163 int c1 = data[i];
Kenny Roota9886c52010-02-12 14:09:24 -0800164 int c2 = u_charMirror(c1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800165
166 if (c1 != c2) {
167 data[i] = c2;
Elliott Hughes69a017b2011-04-08 14:10:28 -0700168 return true;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800169 }
170 }
Elliott Hughes69a017b2011-04-08 14:10:28 -0700171 return false;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800172}
173
174static jchar getMirror(JNIEnv* env, jobject obj, jchar c)
Elliott Hughes69a017b2011-04-08 14:10:28 -0700175{
Kenny Roota9886c52010-02-12 14:09:24 -0800176 return u_charMirror(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800177}
178
179static JNINativeMethod gMethods[] = {
180 { "getDirectionalities", "([C[BI)V",
181 (void*) getDirectionalities },
Kenny Rootbb9a5172010-02-13 00:07:38 -0800182 { "getEastAsianWidth", "(C)I",
183 (void*) getEastAsianWidth },
184 { "getEastAsianWidths", "([CII[B)V",
185 (void*) getEastAsianWidths },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 { "mirror", "([CII)Z",
187 (void*) mirror },
188 { "getMirror", "(C)C",
189 (void*) getMirror }
190};
191
192int register_android_text_AndroidCharacter(JNIEnv* env)
193{
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800194 return AndroidRuntime::registerNativeMethods(env, "android/text/AndroidCharacter",
195 gMethods, NELEM(gMethods));
196}
197
198}