Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 1 | /* |
| 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 Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 26 | #include "core_jni_helpers.h" |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 27 | #include <vector> |
| 28 | |
| 29 | namespace android { |
| 30 | |
| 31 | class ScopedBreakIterator { |
| 32 | public: |
Elliott Hughes | 4c5806b | 2015-03-07 11:00:50 -0800 | [diff] [blame^] | 33 | ScopedBreakIterator(JNIEnv* env, icu::BreakIterator* breakIterator, jcharArray inputText, |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 34 | 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 Hughes | 4c5806b | 2015-03-07 11:00:50 -0800 | [diff] [blame^] | 44 | inline icu::BreakIterator* operator->() { |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 45 | return mBreakIterator; |
| 46 | } |
| 47 | |
| 48 | ~ScopedBreakIterator() { |
| 49 | utext_close(mUText); |
| 50 | delete mBreakIterator; |
| 51 | } |
| 52 | private: |
Elliott Hughes | 4c5806b | 2015-03-07 11:00:50 -0800 | [diff] [blame^] | 53 | icu::BreakIterator* mBreakIterator; |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 54 | ScopedCharArrayRO mChars; |
| 55 | UText* mUText; |
| 56 | |
| 57 | // disable copying and assignment |
| 58 | ScopedBreakIterator(const ScopedBreakIterator&); |
| 59 | void operator=(const ScopedBreakIterator&); |
| 60 | }; |
| 61 | |
| 62 | static jintArray nLineBreakOpportunities(JNIEnv* env, jclass, jstring javaLocaleName, |
| 63 | jcharArray inputText, jint length, |
| 64 | jintArray recycle) { |
| 65 | jintArray ret; |
Anish Athalye | 9b72700 | 2014-07-08 13:27:14 -0700 | [diff] [blame] | 66 | std::vector<jint> breaks; |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 67 | |
| 68 | ScopedIcuLocale icuLocale(env, javaLocaleName); |
| 69 | if (icuLocale.valid()) { |
| 70 | UErrorCode status = U_ZERO_ERROR; |
Elliott Hughes | 4c5806b | 2015-03-07 11:00:50 -0800 | [diff] [blame^] | 71 | icu::BreakIterator* it = icu::BreakIterator::createLineInstance(icuLocale.locale(), status); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 72 | if (!U_SUCCESS(status) || it == NULL) { |
| 73 | if (it) { |
| 74 | delete it; |
| 75 | } |
| 76 | } else { |
| 77 | ScopedBreakIterator breakIterator(env, it, inputText, length); |
Elliott Hughes | 4c5806b | 2015-03-07 11:00:50 -0800 | [diff] [blame^] | 78 | for (int loc = breakIterator->first(); loc != icu::BreakIterator::DONE; |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 79 | loc = breakIterator->next()) { |
| 80 | breaks.push_back(loc); |
| 81 | } |
| 82 | } |
| 83 | } |
| 84 | |
| 85 | breaks.push_back(-1); // sentinel terminal value |
| 86 | |
Anish Athalye | 9b72700 | 2014-07-08 13:27:14 -0700 | [diff] [blame] | 87 | if (recycle != NULL && static_cast<size_t>(env->GetArrayLength(recycle)) >= breaks.size()) { |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 88 | 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 | |
| 100 | static JNINativeMethod gMethods[] = { |
| 101 | {"nLineBreakOpportunities", "(Ljava/lang/String;[CI[I)[I", (void*) nLineBreakOpportunities} |
| 102 | }; |
| 103 | |
| 104 | int register_android_text_StaticLayout(JNIEnv* env) |
| 105 | { |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 106 | return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods)); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 107 | } |
| 108 | |
| 109 | } |