blob: fceb98041d59f2fe7f07e2e806fc926f3b201323 [file] [log] [blame]
Anish Athalye88b5b0b2014-06-24 14:39:43 -07001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "StaticLayout"
18
19#include "ScopedIcuLocale.h"
20#include "unicode/locid.h"
21#include "unicode/brkiter.h"
22#include "utils/misc.h"
23#include "utils/Log.h"
24#include "ScopedPrimitiveArray.h"
25#include "JNIHelp.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080026#include "core_jni_helpers.h"
Anish Athalye88b5b0b2014-06-24 14:39:43 -070027#include <vector>
28
29namespace android {
30
31class ScopedBreakIterator {
32 public:
Elliott Hughes4c5806b2015-03-07 11:00:50 -080033 ScopedBreakIterator(JNIEnv* env, icu::BreakIterator* breakIterator, jcharArray inputText,
Anish Athalye88b5b0b2014-06-24 14:39:43 -070034 jint length) : mBreakIterator(breakIterator), mChars(env, inputText) {
35 UErrorCode status = U_ZERO_ERROR;
36 mUText = utext_openUChars(NULL, mChars.get(), length, &status);
37 if (mUText == NULL) {
38 return;
39 }
40
41 mBreakIterator->setText(mUText, status);
42 }
43
Elliott Hughes4c5806b2015-03-07 11:00:50 -080044 inline icu::BreakIterator* operator->() {
Anish Athalye88b5b0b2014-06-24 14:39:43 -070045 return mBreakIterator;
46 }
47
48 ~ScopedBreakIterator() {
49 utext_close(mUText);
50 delete mBreakIterator;
51 }
52 private:
Elliott Hughes4c5806b2015-03-07 11:00:50 -080053 icu::BreakIterator* mBreakIterator;
Anish Athalye88b5b0b2014-06-24 14:39:43 -070054 ScopedCharArrayRO mChars;
55 UText* mUText;
56
57 // disable copying and assignment
58 ScopedBreakIterator(const ScopedBreakIterator&);
59 void operator=(const ScopedBreakIterator&);
60};
61
62static jintArray nLineBreakOpportunities(JNIEnv* env, jclass, jstring javaLocaleName,
63 jcharArray inputText, jint length,
64 jintArray recycle) {
65 jintArray ret;
Anish Athalye9b727002014-07-08 13:27:14 -070066 std::vector<jint> breaks;
Anish Athalye88b5b0b2014-06-24 14:39:43 -070067
68 ScopedIcuLocale icuLocale(env, javaLocaleName);
69 if (icuLocale.valid()) {
70 UErrorCode status = U_ZERO_ERROR;
Elliott Hughes4c5806b2015-03-07 11:00:50 -080071 icu::BreakIterator* it = icu::BreakIterator::createLineInstance(icuLocale.locale(), status);
Anish Athalye88b5b0b2014-06-24 14:39:43 -070072 if (!U_SUCCESS(status) || it == NULL) {
73 if (it) {
74 delete it;
75 }
76 } else {
77 ScopedBreakIterator breakIterator(env, it, inputText, length);
Elliott Hughes4c5806b2015-03-07 11:00:50 -080078 for (int loc = breakIterator->first(); loc != icu::BreakIterator::DONE;
Anish Athalye88b5b0b2014-06-24 14:39:43 -070079 loc = breakIterator->next()) {
80 breaks.push_back(loc);
81 }
82 }
83 }
84
85 breaks.push_back(-1); // sentinel terminal value
86
Anish Athalye9b727002014-07-08 13:27:14 -070087 if (recycle != NULL && static_cast<size_t>(env->GetArrayLength(recycle)) >= breaks.size()) {
Anish Athalye88b5b0b2014-06-24 14:39:43 -070088 ret = recycle;
89 } else {
90 ret = env->NewIntArray(breaks.size());
91 }
92
93 if (ret != NULL) {
94 env->SetIntArrayRegion(ret, 0, breaks.size(), &breaks.front());
95 }
96
97 return ret;
98}
99
100static JNINativeMethod gMethods[] = {
101 {"nLineBreakOpportunities", "(Ljava/lang/String;[CI[I)[I", (void*) nLineBreakOpportunities}
102};
103
104int register_android_text_StaticLayout(JNIEnv* env)
105{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800106 return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods));
Anish Athalye88b5b0b2014-06-24 14:39:43 -0700107}
108
109}