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" |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 24 | #include "ScopedStringChars.h" |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 25 | #include "ScopedPrimitiveArray.h" |
| 26 | #include "JNIHelp.h" |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 27 | #include "core_jni_helpers.h" |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 28 | #include <cstdint> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 29 | #include <vector> |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 30 | #include <list> |
| 31 | #include <algorithm> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 32 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 33 | #include "SkPaint.h" |
| 34 | #include "SkTypeface.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 35 | #include <hwui/MinikinSkia.h> |
| 36 | #include <hwui/MinikinUtils.h> |
| 37 | #include <hwui/Paint.h> |
| 38 | #include <minikin/FontCollection.h> |
| 39 | #include <minikin/LineBreaker.h> |
| 40 | #include <minikin/MinikinFont.h> |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 41 | |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 42 | namespace android { |
| 43 | |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 44 | struct JLineBreaksID { |
| 45 | jfieldID breaks; |
| 46 | jfieldID widths; |
| 47 | jfieldID flags; |
| 48 | }; |
| 49 | |
| 50 | static jclass gLineBreaks_class; |
| 51 | static JLineBreaksID gLineBreaks_fieldID; |
| 52 | |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 53 | // set text and set a number of parameters for creating a layout (width, tabstops, strategy, |
| 54 | // hyphenFrequency) |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 55 | static void nSetupParagraph(JNIEnv* env, jclass, jlong nativePtr, jcharArray text, jint length, |
| 56 | jfloat firstWidth, jint firstWidthLineLimit, jfloat restWidth, |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 57 | jintArray variableTabStops, jint defaultTabStop, jint strategy, jint hyphenFrequency) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 58 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 59 | b->resize(length); |
| 60 | env->GetCharArrayRegion(text, 0, length, b->buffer()); |
| 61 | b->setText(); |
| 62 | b->setLineWidths(firstWidth, firstWidthLineLimit, restWidth); |
| 63 | if (variableTabStops == nullptr) { |
| 64 | b->setTabStops(nullptr, 0, defaultTabStop); |
| 65 | } else { |
| 66 | ScopedIntArrayRO stops(env, variableTabStops); |
| 67 | b->setTabStops(stops.get(), stops.size(), defaultTabStop); |
| 68 | } |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 69 | b->setStrategy(static_cast<minikin::BreakStrategy>(strategy)); |
| 70 | b->setHyphenationFrequency(static_cast<minikin::HyphenationFrequency>(hyphenFrequency)); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 71 | } |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 72 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 73 | static void recycleCopy(JNIEnv* env, jobject recycle, jintArray recycleBreaks, |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 74 | jfloatArray recycleWidths, jintArray recycleFlags, |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 75 | jint recycleLength, size_t nBreaks, const jint* breaks, |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 76 | const jfloat* widths, const jint* flags) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 77 | if ((size_t)recycleLength < nBreaks) { |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 78 | // have to reallocate buffers |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 79 | recycleBreaks = env->NewIntArray(nBreaks); |
| 80 | recycleWidths = env->NewFloatArray(nBreaks); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 81 | recycleFlags = env->NewIntArray(nBreaks); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 82 | |
| 83 | env->SetObjectField(recycle, gLineBreaks_fieldID.breaks, recycleBreaks); |
| 84 | env->SetObjectField(recycle, gLineBreaks_fieldID.widths, recycleWidths); |
| 85 | env->SetObjectField(recycle, gLineBreaks_fieldID.flags, recycleFlags); |
| 86 | } |
| 87 | // copy data |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 88 | env->SetIntArrayRegion(recycleBreaks, 0, nBreaks, breaks); |
| 89 | env->SetFloatArrayRegion(recycleWidths, 0, nBreaks, widths); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 90 | env->SetIntArrayRegion(recycleFlags, 0, nBreaks, flags); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 91 | } |
| 92 | |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 93 | static jint nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr, |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 94 | jobject recycle, jintArray recycleBreaks, |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 95 | jfloatArray recycleWidths, jintArray recycleFlags, |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 96 | jint recycleLength) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 97 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 98 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 99 | size_t nBreaks = b->computeBreaks(); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 100 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 101 | recycleCopy(env, recycle, recycleBreaks, recycleWidths, recycleFlags, recycleLength, |
| 102 | nBreaks, b->getBreaks(), b->getWidths(), b->getFlags()); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 103 | |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 104 | b->finish(); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 105 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 106 | return static_cast<jint>(nBreaks); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 109 | static jlong nNewBuilder(JNIEnv*, jclass) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 110 | return reinterpret_cast<jlong>(new minikin::LineBreaker); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void nFreeBuilder(JNIEnv*, jclass, jlong nativePtr) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 114 | delete reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void nFinishBuilder(JNIEnv*, jclass, jlong nativePtr) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 118 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 119 | b->finish(); |
| 120 | } |
| 121 | |
Raph Levien | 091dba2 | 2015-08-31 16:21:20 -0700 | [diff] [blame] | 122 | static jlong nLoadHyphenator(JNIEnv* env, jclass, jobject buffer, jint offset) { |
| 123 | const uint8_t* bytebuf = nullptr; |
| 124 | if (buffer != nullptr) { |
| 125 | void* rawbuf = env->GetDirectBufferAddress(buffer); |
| 126 | if (rawbuf != nullptr) { |
| 127 | bytebuf = reinterpret_cast<const uint8_t*>(rawbuf) + offset; |
| 128 | } else { |
| 129 | ALOGE("failed to get direct buffer address"); |
| 130 | } |
| 131 | } |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 132 | minikin::Hyphenator* hyphenator = minikin::Hyphenator::loadBinary(bytebuf); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 133 | return reinterpret_cast<jlong>(hyphenator); |
| 134 | } |
| 135 | |
| 136 | static void nSetLocale(JNIEnv* env, jclass, jlong nativePtr, jstring javaLocaleName, |
| 137 | jlong nativeHyphenator) { |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 138 | ScopedIcuLocale icuLocale(env, javaLocaleName); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 139 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
| 140 | minikin::Hyphenator* hyphenator = reinterpret_cast<minikin::Hyphenator*>(nativeHyphenator); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 141 | |
| 142 | if (icuLocale.valid()) { |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 143 | b->setLocale(icuLocale.locale(), hyphenator); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 144 | } |
| 145 | } |
| 146 | |
Raph Levien | e319d5a | 2015-04-14 23:51:07 -0700 | [diff] [blame] | 147 | static void nSetIndents(JNIEnv* env, jclass, jlong nativePtr, jintArray indents) { |
| 148 | ScopedIntArrayRO indentArr(env, indents); |
| 149 | std::vector<float> indentVec(indentArr.get(), indentArr.get() + indentArr.size()); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 150 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | e319d5a | 2015-04-14 23:51:07 -0700 | [diff] [blame] | 151 | b->setIndents(indentVec); |
| 152 | } |
| 153 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 154 | // Basically similar to Paint.getTextRunAdvances but with C++ interface |
| 155 | static jfloat nAddStyleRun(JNIEnv* env, jclass, jlong nativePtr, |
| 156 | jlong nativePaint, jlong nativeTypeface, jint start, jint end, jboolean isRtl) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 157 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 158 | Paint* paint = reinterpret_cast<Paint*>(nativePaint); |
sergeyv | bad9918 | 2016-03-17 11:24:22 -0700 | [diff] [blame] | 159 | Typeface* typeface = reinterpret_cast<Typeface*>(nativeTypeface); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 160 | minikin::FontCollection *font; |
| 161 | minikin::MinikinPaint minikinPaint; |
| 162 | minikin::FontStyle style = MinikinUtils::prepareMinikinPaint(&minikinPaint, &font, paint, |
| 163 | typeface); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 164 | return b->addStyleRun(&minikinPaint, font, style, start, end, isRtl); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 165 | } |
| 166 | |
| 167 | // Accept width measurements for the run, passed in from Java |
| 168 | static void nAddMeasuredRun(JNIEnv* env, jclass, jlong nativePtr, |
| 169 | jint start, jint end, jfloatArray widths) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 170 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 171 | env->GetFloatArrayRegion(widths, start, end - start, b->charWidths() + start); |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 172 | b->addStyleRun(nullptr, nullptr, minikin::FontStyle{}, start, end, false); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | static void nAddReplacementRun(JNIEnv* env, jclass, jlong nativePtr, |
| 176 | jint start, jint end, jfloat width) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 177 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 178 | b->addReplacement(start, end, width); |
| 179 | } |
| 180 | |
| 181 | static void nGetWidths(JNIEnv* env, jclass, jlong nativePtr, jfloatArray widths) { |
Seigo Nonaka | ae1aa85 | 2016-06-09 19:42:51 +0900 | [diff] [blame^] | 182 | minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 183 | env->SetFloatArrayRegion(widths, 0, b->size(), b->charWidths()); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 184 | } |
| 185 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 186 | static const JNINativeMethod gMethods[] = { |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 187 | // TODO performance: many of these are candidates for fast jni, awaiting guidance |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 188 | {"nNewBuilder", "()J", (void*) nNewBuilder}, |
| 189 | {"nFreeBuilder", "(J)V", (void*) nFreeBuilder}, |
| 190 | {"nFinishBuilder", "(J)V", (void*) nFinishBuilder}, |
Raph Levien | 091dba2 | 2015-08-31 16:21:20 -0700 | [diff] [blame] | 191 | {"nLoadHyphenator", "(Ljava/nio/ByteBuffer;I)J", (void*) nLoadHyphenator}, |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 192 | {"nSetLocale", "(JLjava/lang/String;J)V", (void*) nSetLocale}, |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 193 | {"nSetupParagraph", "(J[CIFIF[IIII)V", (void*) nSetupParagraph}, |
Raph Levien | e319d5a | 2015-04-14 23:51:07 -0700 | [diff] [blame] | 194 | {"nSetIndents", "(J[I)V", (void*) nSetIndents}, |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 195 | {"nAddStyleRun", "(JJJIIZ)F", (void*) nAddStyleRun}, |
| 196 | {"nAddMeasuredRun", "(JII[F)V", (void*) nAddMeasuredRun}, |
| 197 | {"nAddReplacementRun", "(JIIF)V", (void*) nAddReplacementRun}, |
| 198 | {"nGetWidths", "(J[F)V", (void*) nGetWidths}, |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 199 | {"nComputeLineBreaks", "(JLandroid/text/StaticLayout$LineBreaks;[I[F[II)I", |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 200 | (void*) nComputeLineBreaks} |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 201 | }; |
| 202 | |
| 203 | int register_android_text_StaticLayout(JNIEnv* env) |
| 204 | { |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 205 | gLineBreaks_class = MakeGlobalRefOrDie(env, |
| 206 | FindClassOrDie(env, "android/text/StaticLayout$LineBreaks")); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 207 | |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 208 | gLineBreaks_fieldID.breaks = GetFieldIDOrDie(env, gLineBreaks_class, "breaks", "[I"); |
| 209 | gLineBreaks_fieldID.widths = GetFieldIDOrDie(env, gLineBreaks_class, "widths", "[F"); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 210 | gLineBreaks_fieldID.flags = GetFieldIDOrDie(env, gLineBreaks_class, "flags", "[I"); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 211 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 212 | return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods)); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 213 | } |
| 214 | |
| 215 | } |