android_text_AndroidCharacter.cpp revision 1fc4d880909362127af9873dbacae1d00eb39d8b
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 "JNIHelp.h"
21#include <android_runtime/AndroidRuntime.h>
22#include "utils/misc.h"
23#include "utils/Log.h"
24#include "unicode/uchar.h"
25
26#define PROPERTY_UNDEFINED (-1)
27
28// ICU => JDK mapping
29static int directionality_map[U_CHAR_DIRECTION_COUNT] = {
30    0, // U_LEFT_TO_RIGHT (0) => DIRECTIONALITY_LEFT_TO_RIGHT (0)
31    1, // U_RIGHT_TO_LEFT (1) => DIRECTIONALITY_RIGHT_TO_LEFT (1)
32    3, // U_EUROPEAN_NUMBER (2) => DIRECTIONALITY_EUROPEAN_NUMBER (3)
33    4, // U_EUROPEAN_NUMBER_SEPARATOR (3) => DIRECTIONALITY_EUROPEAN_NUMBER_SEPARATOR (4)
34    5, // U_EUROPEAN_NUMBER_TERMINATOR (4) => DIRECTIONALITY_EUROPEAN_NUMBER_TERMINATOR (5)
35    6, // U_ARABIC_NUMBER (5) => DIRECTIONALITY_ARABIC_NUMBER (6)
36    7, // U_COMMON_NUMBER_SEPARATOR (6) => DIRECTIONALITY_COMMON_NUMBER_SEPARATOR (7)
37    10, // U_BLOCK_SEPARATOR (7) => DIRECTIONALITY_PARAGRAPH_SEPARATOR (10)
38    11, // U_SEGMENT_SEPARATOR (8) => DIRECTIONALITY_SEGMENT_SEPARATOR (11)
39    12, // U_WHITE_SPACE_NEUTRAL (9) => DIRECTIONALITY_WHITESPACE (12)
40    13, // U_OTHER_NEUTRAL (10) => DIRECTIONALITY_OTHER_NEUTRALS (13)
41    14, // U_LEFT_TO_RIGHT_EMBEDDING (11) => DIRECTIONALITY_LEFT_TO_RIGHT_EMBEDDING (14)
42    15, // U_LEFT_TO_RIGHT_OVERRIDE (12) => DIRECTIONALITY_LEFT_TO_RIGHT_OVERRIDE (15)
43    2, // U_RIGHT_TO_LEFT_ARABIC (13) => DIRECTIONALITY_RIGHT_TO_LEFT_ARABIC (2)
44    16, // U_RIGHT_TO_LEFT_EMBEDDING (14) => DIRECTIONALITY_RIGHT_TO_LEFT_EMBEDDING (16)
45    17, // U_RIGHT_TO_LEFT_OVERRIDE (15) => DIRECTIONALITY_RIGHT_TO_LEFT_OVERRIDE (17)
46    18, // U_POP_DIRECTIONAL_FORMAT (16) => DIRECTIONALITY_POP_DIRECTIONAL_FORMAT (18)
47    8, // U_DIR_NON_SPACING_MARK (17) => DIRECTIONALITY_NONSPACING_MARK (8)
48    9, // U_BOUNDARY_NEUTRAL (18) => DIRECTIONALITY_BOUNDARY_NEUTRAL (9)
49};
50
51namespace android {
52
53static void getDirectionalities(JNIEnv* env, jobject obj, jcharArray srcArray, jbyteArray destArray, int count)
54{
55    jchar* src = env->GetCharArrayElements(srcArray, NULL);
56    jbyte* dest = env->GetByteArrayElements(destArray, NULL);
57    if (src == NULL || dest == NULL) {
58        jniThrowException(env, "java/lang/NullPointerException", NULL);
59        goto DIRECTION_END;
60    }
61
62    if (env->GetArrayLength(srcArray) < count || env->GetArrayLength(destArray) < count) {
63        jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
64        goto DIRECTION_END;
65    }
66
67    for (int i = 0; i < count; i++) {
68        if (src[i] >= 0xD800 && src[i] <= 0xDBFF &&
69            i + 1 < count &&
70            src[i + 1] >= 0xDC00 && src[i + 1] <= 0xDFFF) {
71            int c = 0x00010000 + ((src[i] - 0xD800) << 10) +
72                                 (src[i + 1] & 0x3FF);
73            int dir = u_charDirection(c);
74            if (dir < 0 || dir >= U_CHAR_DIRECTION_COUNT)
75                dir = PROPERTY_UNDEFINED;
76            else
77                dir = directionality_map[dir];
78
79            dest[i++] = dir;
80            dest[i] = dir;
81        } else {
82            int c = src[i];
83            int dir = u_charDirection(c);
84            if (dir < 0 || dir >= U_CHAR_DIRECTION_COUNT)
85                dest[i] = PROPERTY_UNDEFINED;
86            else
87                dest[i] = directionality_map[dir];
88        }
89    }
90
91DIRECTION_END:
92    env->ReleaseCharArrayElements(srcArray, src, JNI_ABORT);
93    env->ReleaseByteArrayElements(destArray, dest, JNI_ABORT);
94}
95
96static 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
105static void getEastAsianWidths(JNIEnv* env, jobject obj, jcharArray srcArray,
106                               int start, int count, jbyteArray destArray)
107{
108    jchar* src = env->GetCharArrayElements(srcArray, NULL);
109    jbyte* dest = env->GetByteArrayElements(destArray, NULL);
110    if (src == NULL || dest == NULL) {
111        jniThrowException(env, "java/lang/NullPointerException", NULL);
112        goto EA_END;
113    }
114
115    if (start < 0 || start > start + count
116            || env->GetArrayLength(srcArray) < (start + count)
117            || env->GetArrayLength(destArray) < count) {
118        jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
119        goto EA_END;
120    }
121
122    for (int i = 0; i < count; i++) {
123        const int srci = start + i;
124        if (src[srci] >= 0xD800 && src[srci] <= 0xDBFF &&
125            i + 1 < count &&
126            src[srci + 1] >= 0xDC00 && src[srci + 1] <= 0xDFFF) {
127            int c = 0x00010000 + ((src[srci] - 0xD800) << 10) +
128                                 (src[srci + 1] & 0x3FF);
129            int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH);
130            if (width < 0 || width >= U_EA_COUNT)
131                width = PROPERTY_UNDEFINED;
132
133            dest[i++] = width;
134            dest[i] = width;
135        } else {
136            int c = src[srci];
137            int width = u_getIntPropertyValue(c, UCHAR_EAST_ASIAN_WIDTH);
138            if (width < 0 || width >= U_EA_COUNT)
139                width = PROPERTY_UNDEFINED;
140
141            dest[i] = width;
142        }
143    }
144
145EA_END:
146    env->ReleaseCharArrayElements(srcArray, src, JNI_ABORT);
147    env->ReleaseByteArrayElements(destArray, dest, JNI_ABORT);
148}
149
150static jboolean mirror(JNIEnv* env, jobject obj, jcharArray charArray, int start, int count)
151{
152    jchar* data = env->GetCharArrayElements(charArray, NULL);
153    bool ret = false;
154
155    if (data == NULL) {
156        jniThrowException(env, "java/lang/NullPointerException", NULL);
157        goto MIRROR_END;
158    }
159
160    if (start < 0 || start > start + count
161            || env->GetArrayLength(charArray) < start + count) {
162        jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
163        goto MIRROR_END;
164    }
165
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 = true;
175        }
176    }
177
178MIRROR_END:
179    env->ReleaseCharArrayElements(charArray, data, JNI_ABORT);
180	return ret;
181}
182
183static jchar getMirror(JNIEnv* env, jobject obj, jchar c)
184{
185    return u_charMirror(c);
186}
187
188static JNINativeMethod gMethods[] = {
189	{ "getDirectionalities", "([C[BI)V",
190        (void*) getDirectionalities },
191	{ "getEastAsianWidth", "(C)I",
192        (void*) getEastAsianWidth },
193	{ "getEastAsianWidths", "([CII[B)V",
194        (void*) getEastAsianWidths },
195	{ "mirror", "([CII)Z",
196        (void*) mirror },
197	{ "getMirror", "(C)C",
198        (void*) getMirror }
199};
200
201int register_android_text_AndroidCharacter(JNIEnv* env)
202{
203    jclass clazz = env->FindClass("android/text/AndroidCharacter");
204    LOG_ASSERT(clazz, "Cannot find android/text/AndroidCharacter");
205
206    return AndroidRuntime::registerNativeMethods(env, "android/text/AndroidCharacter",
207            gMethods, NELEM(gMethods));
208}
209
210}
211