• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /* //device/libs/android_runtime/android_text_AndroidCharacter.cpp
2 **
3 ** Copyright 2006, The Android Open Source Project
4 **
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
8 **
9 **     http://www.apache.org/licenses/LICENSE-2.0
10 **
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
15 ** limitations under the License.
16 */
17 
18 #define LOG_TAG "AndroidUnicode"
19 
20 #include <nativehelper/JNIHelp.h>
21 #include <nativehelper/ScopedPrimitiveArray.h>
22 #include "core_jni_helpers.h"
23 #include "utils/misc.h"
24 #include "utils/Log.h"
25 #include "unicode/uchar.h"
26 
27 #define PROPERTY_UNDEFINED (-1)
28 #define JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY 18
29 
30 // ICU => JDK mapping
31 static int directionality_map[JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY + 1] = {
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 };
52 
53 namespace android {
54 
getDirectionalities(JNIEnv * env,jobject obj,jcharArray srcArray,jbyteArray destArray,jint count)55 static void getDirectionalities(JNIEnv* env, jobject obj, jcharArray srcArray,
56                                 jbyteArray destArray, jint count)
57 {
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;
65     }
66 
67     if (env->GetArrayLength(srcArray) < count || env->GetArrayLength(destArray) < count) {
68         jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
69         return;
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);
78             int dir = u_charDirection(c);
79             if (dir < 0 || dir > JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY
80                     || u_charType(c) == U_UNASSIGNED)
81                 dir = PROPERTY_UNDEFINED;
82             else
83                 dir = directionality_map[dir];
84 
85             dest[i++] = dir;
86             dest[i] = dir;
87         } else {
88             int c = src[i];
89             int dir = u_charDirection(c);
90             if (dir < 0 || dir > JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY
91                     || u_charType(c) == U_UNASSIGNED)
92                 dest[i] = PROPERTY_UNDEFINED;
93             else
94                 dest[i] = directionality_map[dir];
95         }
96     }
97 }
98 
getEastAsianWidth(JNIEnv * env,jobject obj,jchar input)99 static jint getEastAsianWidth(JNIEnv* env, jobject obj, jchar input)
100 {
101     int width = u_getIntPropertyValue(input, UCHAR_EAST_ASIAN_WIDTH);
102     if (width < 0 || width > u_getIntPropertyMaxValue(UCHAR_EAST_ASIAN_WIDTH))
103         width = PROPERTY_UNDEFINED;
104 
105     return width;
106 }
107 
getEastAsianWidths(JNIEnv * env,jobject obj,jcharArray srcArray,jint start,jint count,jbyteArray destArray)108 static void getEastAsianWidths(JNIEnv* env, jobject obj, jcharArray srcArray,
109                                jint start, jint count, jbyteArray destArray)
110 {
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;
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);
124         return;
125     }
126 
127     int maxWidth = u_getIntPropertyMaxValue(UCHAR_EAST_ASIAN_WIDTH);
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);
136             if (width < 0 || width > maxWidth)
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);
144             if (width < 0 || width > maxWidth)
145                 width = PROPERTY_UNDEFINED;
146 
147             dest[i] = width;
148         }
149     }
150 }
151 
mirror(JNIEnv * env,jobject obj,jcharArray charArray,jint start,jint count)152 static jboolean mirror(JNIEnv* env, jobject obj, jcharArray charArray, jint start, jint count)
153 {
154     ScopedCharArrayRW data(env, charArray);
155     if (data.get() == NULL) {
156         return JNI_FALSE;
157     }
158 
159     if (start < 0 || start > start + count
160             || env->GetArrayLength(charArray) < start + count) {
161         jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
162         return JNI_FALSE;
163     }
164 
165     jboolean ret = JNI_FALSE;
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];
170         int c2 = u_charMirror(c1);
171 
172         if (c1 != c2) {
173             data[i] = c2;
174             ret = JNI_TRUE;
175         }
176     }
177     return ret;
178 }
179 
getMirror(JNIEnv * env,jobject obj,jchar c)180 static jchar getMirror(JNIEnv* env, jobject obj, jchar c)
181 {
182     return u_charMirror(c);
183 }
184 
185 static const JNINativeMethod gMethods[] = {
186 	{ "getDirectionalities", "([C[BI)V",
187         (void*) getDirectionalities },
188 	{ "getEastAsianWidth", "(C)I",
189         (void*) getEastAsianWidth },
190 	{ "getEastAsianWidths", "([CII[B)V",
191         (void*) getEastAsianWidths },
192 	{ "mirror", "([CII)Z",
193         (void*) mirror },
194 	{ "getMirror", "(C)C",
195         (void*) getMirror }
196 };
197 
register_android_text_AndroidCharacter(JNIEnv * env)198 int register_android_text_AndroidCharacter(JNIEnv* env)
199 {
200     return RegisterMethodsOrDie(env, "android/text/AndroidCharacter", gMethods, NELEM(gMethods));
201 }
202 
203 }
204