Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 1 | /* //device/libs/android_runtime/android_text_AndroidBidi.cpp |
| 2 | ** |
| 3 | ** Copyright 2010, The Android Open Source Project |
| 4 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 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 |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 8 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 9 | ** http://www.apache.org/licenses/LICENSE-2.0 |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 10 | ** |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 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 |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 15 | ** limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #define LOG_TAG "AndroidUnicode" |
| 19 | |
Jim Huang | 1fc4d88 | 2010-10-17 21:08:53 +0800 | [diff] [blame] | 20 | #include "JNIHelp.h" |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 21 | #include <android_runtime/AndroidRuntime.h> |
| 22 | #include "utils/misc.h" |
| 23 | #include "utils/Log.h" |
| 24 | #include "unicode/ubidi.h" |
| 25 | |
| 26 | namespace android { |
Elliott Hughes | dd66bcb | 2011-04-12 11:28:59 -0700 | [diff] [blame] | 27 | |
| 28 | static jint runBidi(JNIEnv* env, jobject obj, jint dir, jcharArray chsArray, |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 29 | jbyteArray infoArray, int n, jboolean haveInfo) |
| 30 | { |
| 31 | // Parameters are checked on java side |
| 32 | // Failures from GetXXXArrayElements indicate a serious out-of-memory condition |
| 33 | // that we don't bother to report, we're probably dead anyway. |
| 34 | jint result = 0; |
| 35 | jchar* chs = env->GetCharArrayElements(chsArray, NULL); |
| 36 | if (chs != NULL) { |
| 37 | jbyte* info = env->GetByteArrayElements(infoArray, NULL); |
| 38 | if (info != NULL) { |
| 39 | UErrorCode status = U_ZERO_ERROR; |
| 40 | UBiDi* bidi = ubidi_openSized(n, 0, &status); |
| 41 | ubidi_setPara(bidi, chs, n, dir, NULL, &status); |
| 42 | if (U_SUCCESS(status)) { |
| 43 | for (int i = 0; i < n; ++i) { |
| 44 | info[i] = ubidi_getLevelAt(bidi, i); |
| 45 | } |
| 46 | result = ubidi_getParaLevel(bidi); |
| 47 | } else { |
| 48 | jniThrowException(env, "java/lang/RuntimeException", NULL); |
| 49 | } |
| 50 | ubidi_close(bidi); |
| 51 | |
| 52 | env->ReleaseByteArrayElements(infoArray, info, 0); |
| 53 | } |
| 54 | env->ReleaseCharArrayElements(chsArray, chs, JNI_ABORT); |
| 55 | } |
| 56 | return result; |
| 57 | } |
| 58 | |
| 59 | static JNINativeMethod gMethods[] = { |
| 60 | { "runBidi", "(I[C[BIZ)I", |
| 61 | (void*) runBidi } |
| 62 | }; |
| 63 | |
| 64 | int register_android_text_AndroidBidi(JNIEnv* env) |
| 65 | { |
Doug Felt | dae8e94 | 2010-02-24 14:33:15 -0800 | [diff] [blame] | 66 | return AndroidRuntime::registerNativeMethods(env, "android/text/AndroidBidi", |
| 67 | gMethods, NELEM(gMethods)); |
| 68 | } |
| 69 | |
| 70 | } |