blob: c6ea4e10f63e49814659ac16b71dd68b8dfd8c99 [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
Steven Moreland2279b252017-07-19 09:50:45 -070020#include <nativehelper/JNIHelp.h>
21#include <nativehelper/ScopedPrimitiveArray.h>
Andreas Gampeed6b9df2014-11-20 22:02:20 -080022#include "core_jni_helpers.h"
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023#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)
Victor Changc1463ec2018-11-27 12:40:23 +000028#define JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY 18
Kenny Rootbb9a5172010-02-13 00:07:38 -080029
Kenny Roota9886c52010-02-12 14:09:24 -080030// ICU => JDK mapping
Victor Changc1463ec2018-11-27 12:40:23 +000031static int directionality_map[JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY + 1] = {
Kenny Roota9886c52010-02-12 14:09:24 -080032 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};
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080052
53namespace android {
Elliott Hughes69a017b2011-04-08 14:10:28 -070054
Ashok Bhatf5df7002014-03-25 20:51:35 +000055static void getDirectionalities(JNIEnv* env, jobject obj, jcharArray srcArray,
56 jbyteArray destArray, jint count)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080057{
Elliott Hughes69a017b2011-04-08 14:10:28 -070058 ScopedCharArrayRO src(env, srcArray);
59 if (src.get() == NULL) {
60 return;
61 }
62 ScopedByteArrayRW dest(env, destArray);
63 if (dest.get() == NULL) {
64 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080065 }
66
67 if (env->GetArrayLength(srcArray) < count || env->GetArrayLength(destArray) < count) {
68 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
Elliott Hughes69a017b2011-04-08 14:10:28 -070069 return;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080070 }
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);
Kenny Roota9886c52010-02-12 14:09:24 -080078 int dir = u_charDirection(c);
Victor Chang749d1842018-11-27 16:43:20 +000079 if (dir < 0 || dir > JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY
80 || u_charType(c) == U_UNASSIGNED)
Kenny Rootbb9a5172010-02-13 00:07:38 -080081 dir = PROPERTY_UNDEFINED;
Kenny Roota9886c52010-02-12 14:09:24 -080082 else
83 dir = directionality_map[dir];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080084
85 dest[i++] = dir;
86 dest[i] = dir;
87 } else {
88 int c = src[i];
Kenny Roota9886c52010-02-12 14:09:24 -080089 int dir = u_charDirection(c);
Victor Chang749d1842018-11-27 16:43:20 +000090 if (dir < 0 || dir > JAVA_LANG_CHARACTER_MAX_DIRECTIONALITY
91 || u_charType(c) == U_UNASSIGNED)
Kenny Rootbb9a5172010-02-13 00:07:38 -080092 dest[i] = PROPERTY_UNDEFINED;
Kenny Roota9886c52010-02-12 14:09:24 -080093 else
94 dest[i] = directionality_map[dir];
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080095 }
96 }
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080097}
98
Kenny Rootbb9a5172010-02-13 00:07:38 -080099static jint getEastAsianWidth(JNIEnv* env, jobject obj, jchar input)
100{
101 int width = u_getIntPropertyValue(input, UCHAR_EAST_ASIAN_WIDTH);
Victor Changc1463ec2018-11-27 12:40:23 +0000102 if (width < 0 || width > u_getIntPropertyMaxValue(UCHAR_EAST_ASIAN_WIDTH))
Kenny Rootbb9a5172010-02-13 00:07:38 -0800103 width = PROPERTY_UNDEFINED;
104
105 return width;
106}
107
108static void getEastAsianWidths(JNIEnv* env, jobject obj, jcharArray srcArray,
Ashok Bhatf5df7002014-03-25 20:51:35 +0000109 jint start, jint count, jbyteArray destArray)
Kenny Rootbb9a5172010-02-13 00:07:38 -0800110{
Elliott Hughes69a017b2011-04-08 14:10:28 -0700111 ScopedCharArrayRO src(env, srcArray);
112 if (src.get() == NULL) {
113 return;
114 }
115 ScopedByteArrayRW dest(env, destArray);
116 if (dest.get() == NULL) {
117 return;
Kenny Rootbb9a5172010-02-13 00:07:38 -0800118 }
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);
Elliott Hughes69a017b2011-04-08 14:10:28 -0700124 return;
Kenny Rootbb9a5172010-02-13 00:07:38 -0800125 }
126
Victor Changc1463ec2018-11-27 12:40:23 +0000127 int maxWidth = u_getIntPropertyMaxValue(UCHAR_EAST_ASIAN_WIDTH);
Kenny Rootbb9a5172010-02-13 00:07:38 -0800128 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);
Victor Changc1463ec2018-11-27 12:40:23 +0000136 if (width < 0 || width > maxWidth)
Kenny Rootbb9a5172010-02-13 00:07:38 -0800137 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);
Victor Changc1463ec2018-11-27 12:40:23 +0000144 if (width < 0 || width > maxWidth)
Kenny Rootbb9a5172010-02-13 00:07:38 -0800145 width = PROPERTY_UNDEFINED;
146
147 dest[i] = width;
148 }
149 }
Kenny Rootbb9a5172010-02-13 00:07:38 -0800150}
151
Ashok Bhatf5df7002014-03-25 20:51:35 +0000152static jboolean mirror(JNIEnv* env, jobject obj, jcharArray charArray, jint start, jint count)
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800153{
Elliott Hughes69a017b2011-04-08 14:10:28 -0700154 ScopedCharArrayRW data(env, charArray);
155 if (data.get() == NULL) {
Ashok Bhatf5df7002014-03-25 20:51:35 +0000156 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800157 }
158
Kenny Root073a3d52010-02-17 08:25:47 -0800159 if (start < 0 || start > start + count
160 || env->GetArrayLength(charArray) < start + count) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800161 jniThrowException(env, "java/lang/ArrayIndexOutOfBoundsException", NULL);
Ashok Bhatf5df7002014-03-25 20:51:35 +0000162 return JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800163 }
164
Ashok Bhatf5df7002014-03-25 20:51:35 +0000165 jboolean ret = JNI_FALSE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800166 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];
Kenny Roota9886c52010-02-12 14:09:24 -0800170 int c2 = u_charMirror(c1);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800171
172 if (c1 != c2) {
173 data[i] = c2;
Ashok Bhatf5df7002014-03-25 20:51:35 +0000174 ret = JNI_TRUE;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800175 }
176 }
Raph Leviene2507162012-06-18 16:17:30 -0700177 return ret;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800178}
179
180static jchar getMirror(JNIEnv* env, jobject obj, jchar c)
Elliott Hughes69a017b2011-04-08 14:10:28 -0700181{
Kenny Roota9886c52010-02-12 14:09:24 -0800182 return u_charMirror(c);
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800183}
184
Daniel Micay76f6a862015-09-19 17:31:01 -0400185static const JNINativeMethod gMethods[] = {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800186 { "getDirectionalities", "([C[BI)V",
187 (void*) getDirectionalities },
Kenny Rootbb9a5172010-02-13 00:07:38 -0800188 { "getEastAsianWidth", "(C)I",
189 (void*) getEastAsianWidth },
190 { "getEastAsianWidths", "([CII[B)V",
191 (void*) getEastAsianWidths },
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800192 { "mirror", "([CII)Z",
193 (void*) mirror },
194 { "getMirror", "(C)C",
195 (void*) getMirror }
196};
197
198int register_android_text_AndroidCharacter(JNIEnv* env)
199{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800200 return RegisterMethodsOrDie(env, "android/text/AndroidCharacter", gMethods, NELEM(gMethods));
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800201}
202
203}