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