1dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt/* //device/libs/android_runtime/android_text_AndroidBidi.cpp
2dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt**
3dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt** Copyright 2010, The Android Open Source Project
4dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt**
5dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes** Licensed under the Apache License, Version 2.0 (the "License");
6dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes** you may not use this file except in compliance with the License.
7dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes** You may obtain a copy of the License at
8dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt**
9dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes**     http://www.apache.org/licenses/LICENSE-2.0
10dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt**
11dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes** Unless required by applicable law or agreed to in writing, software
12dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes** distributed under the License is distributed on an "AS IS" BASIS,
13dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes** See the License for the specific language governing permissions and
15dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt** limitations under the License.
16dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt*/
17dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt
18dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt#define LOG_TAG "AndroidUnicode"
19dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt
201fc4d880909362127af9873dbacae1d00eb39d8bJim Huang#include "JNIHelp.h"
21dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt#include <android_runtime/AndroidRuntime.h>
22dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt#include "utils/misc.h"
23dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt#include "utils/Log.h"
24dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt#include "unicode/ubidi.h"
25dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt
26dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Feltnamespace android {
27dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughes
28dd66bcbf9d6ef0c50a18d9c4b1b39ce7ef7afcc4Elliott Hughesstatic jint runBidi(JNIEnv* env, jobject obj, jint dir, jcharArray chsArray,
29dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt                    jbyteArray infoArray, int n, jboolean haveInfo)
30dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt{
31dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    // Parameters are checked on java side
32dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    // Failures from GetXXXArrayElements indicate a serious out-of-memory condition
33dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    // that we don't bother to report, we're probably dead anyway.
34dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    jint result = 0;
35dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    jchar* chs = env->GetCharArrayElements(chsArray, NULL);
36dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    if (chs != NULL) {
37dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt        jbyte* info = env->GetByteArrayElements(infoArray, NULL);
38dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt        if (info != NULL) {
39dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            UErrorCode status = U_ZERO_ERROR;
40dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            UBiDi* bidi = ubidi_openSized(n, 0, &status);
41dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            ubidi_setPara(bidi, chs, n, dir, NULL, &status);
42dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            if (U_SUCCESS(status)) {
43dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt                for (int i = 0; i < n; ++i) {
44dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt                  info[i] = ubidi_getLevelAt(bidi, i);
45dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt                }
46dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt                result = ubidi_getParaLevel(bidi);
47dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            } else {
48dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt                jniThrowException(env, "java/lang/RuntimeException", NULL);
49dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            }
50dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            ubidi_close(bidi);
51dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt
52dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            env->ReleaseByteArrayElements(infoArray, info, 0);
53dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt        }
54dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt        env->ReleaseCharArrayElements(chsArray, chs, JNI_ABORT);
55dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    }
56dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    return result;
57dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt}
58dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt
59dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Feltstatic JNINativeMethod gMethods[] = {
60dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt        { "runBidi", "(I[C[BIZ)I",
61dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt        (void*) runBidi }
62dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt};
63dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt
64dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Feltint register_android_text_AndroidBidi(JNIEnv* env)
65dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt{
66dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt    return AndroidRuntime::registerNativeMethods(env, "android/text/AndroidBidi",
67dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt            gMethods, NELEM(gMethods));
68dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt}
69dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt
70dae8e94cce0881f3e10ef5e34b881f512bb52a75Doug Felt}
71