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 <jni.h>
21 #include <android_runtime/AndroidRuntime.h>
22 #include "utils/misc.h"
23 #include "utils/AndroidUnicode.h"
24 #include "utils/Log.h"
25
26 namespace android {
27
jniThrowException(JNIEnv * env,const char * exc,const char * msg=NULL)28 static void jniThrowException(JNIEnv* env, const char* exc, const char* msg = NULL)
29 {
30 jclass excClazz = env->FindClass(exc);
31 LOG_ASSERT(excClazz, "Unable to find class %s", exc);
32
33 env->ThrowNew(excClazz, msg);
34 }
35
getDirectionalities(JNIEnv * env,jobject obj,jcharArray srcArray,jbyteArray destArray,int count)36 static void getDirectionalities(JNIEnv* env, jobject obj, jcharArray srcArray, jbyteArray destArray, int count)
37 {
38 jchar* src = env->GetCharArrayElements(srcArray, NULL);
39 jbyte* dest = env->GetByteArrayElements(destArray, NULL);
40 if (src == NULL || dest == NULL) {
41 jniThrowException(env, "java/lang/NullPointerException", NULL);
42 goto DIRECTION_END;
43 }
44
45 if (env->GetArrayLength(srcArray) < count || env->GetArrayLength(destArray) < count) {
46 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
47 goto DIRECTION_END;
48 }
49
50 for (int i = 0; i < count; i++) {
51 if (src[i] >= 0xD800 && src[i] <= 0xDBFF &&
52 i + 1 < count &&
53 src[i + 1] >= 0xDC00 && src[i + 1] <= 0xDFFF) {
54 int c = 0x00010000 + ((src[i] - 0xD800) << 10) +
55 (src[i + 1] & 0x3FF);
56 int dir = android::Unicode::getDirectionality(c);
57
58 dest[i++] = dir;
59 dest[i] = dir;
60 } else {
61 int c = src[i];
62 int dir = android::Unicode::getDirectionality(c);
63
64 dest[i] = dir;
65 }
66 }
67
68 DIRECTION_END:
69 env->ReleaseCharArrayElements(srcArray, src, JNI_ABORT);
70 env->ReleaseByteArrayElements(destArray, dest, JNI_ABORT);
71 }
72
mirror(JNIEnv * env,jobject obj,jcharArray charArray,int start,int count)73 static jboolean mirror(JNIEnv* env, jobject obj, jcharArray charArray, int start, int count)
74 {
75 jchar* data = env->GetCharArrayElements(charArray, NULL);
76 bool ret = false;
77
78 if (data == NULL) {
79 jniThrowException(env, "java/lang/NullPointerException", NULL);
80 goto MIRROR_END;
81 }
82
83 if (start > start + count || env->GetArrayLength(charArray) < count) {
84 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
85 goto MIRROR_END;
86 }
87
88 for (int i = start; i < start + count; i++) {
89 // XXX this thinks it knows that surrogates are never mirrored
90
91 int c1 = data[i];
92 int c2 = android::Unicode::toMirror(c1);
93
94 if (c1 != c2) {
95 data[i] = c2;
96 ret = true;
97 }
98 }
99
100 MIRROR_END:
101 env->ReleaseCharArrayElements(charArray, data, JNI_ABORT);
102 return ret;
103 }
104
getMirror(JNIEnv * env,jobject obj,jchar c)105 static jchar getMirror(JNIEnv* env, jobject obj, jchar c)
106 {
107 return android::Unicode::toMirror(c);
108 }
109
110 static JNINativeMethod gMethods[] = {
111 { "getDirectionalities", "([C[BI)V",
112 (void*) getDirectionalities },
113 { "mirror", "([CII)Z",
114 (void*) mirror },
115 { "getMirror", "(C)C",
116 (void*) getMirror }
117 };
118
register_android_text_AndroidCharacter(JNIEnv * env)119 int register_android_text_AndroidCharacter(JNIEnv* env)
120 {
121 jclass clazz = env->FindClass("android/text/AndroidCharacter");
122 LOG_ASSERT(clazz, "Cannot find android/text/AndroidCharacter");
123
124 return AndroidRuntime::registerNativeMethods(env, "android/text/AndroidCharacter",
125 gMethods, NELEM(gMethods));
126 }
127
128 }
129