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" 26 #include <android_runtime/AndroidRuntime.h> 27 #include <vector> 28 29 namespace android { 30 31 class ScopedBreakIterator { 32 public: 33 ScopedBreakIterator(JNIEnv* env, BreakIterator* breakIterator, jcharArray inputText, 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 44 inline BreakIterator* operator->() { 45 return mBreakIterator; 46 } 47 48 ~ScopedBreakIterator() { 49 utext_close(mUText); 50 delete mBreakIterator; 51 } 52 private: 53 BreakIterator* mBreakIterator; 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; 66 std::vector<jint> breaks; 67 68 ScopedIcuLocale icuLocale(env, javaLocaleName); 69 if (icuLocale.valid()) { 70 UErrorCode status = U_ZERO_ERROR; 71 BreakIterator* it = BreakIterator::createLineInstance(icuLocale.locale(), status); 72 if (!U_SUCCESS(status) || it == NULL) { 73 if (it) { 74 delete it; 75 } 76 } else { 77 ScopedBreakIterator breakIterator(env, it, inputText, length); 78 for (int loc = breakIterator->first(); loc != BreakIterator::DONE; 79 loc = breakIterator->next()) { 80 breaks.push_back(loc); 81 } 82 } 83 } 84 85 breaks.push_back(-1); // sentinel terminal value 86 87 if (recycle != NULL && static_cast<size_t>(env->GetArrayLength(recycle)) >= breaks.size()) { 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 { 106 return AndroidRuntime::registerNativeMethods(env, "android/text/StaticLayout", 107 gMethods, NELEM(gMethods)); 108 } 109 110 } 111