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 | |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 19 | #include "utils/misc.h" |
| 20 | #include "utils/Log.h" |
Steven Moreland | 2279b25 | 2017-07-19 09:50:45 -0700 | [diff] [blame] | 21 | #include <nativehelper/ScopedStringChars.h> |
| 22 | #include <nativehelper/ScopedPrimitiveArray.h> |
| 23 | #include <nativehelper/JNIHelp.h> |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 24 | #include "core_jni_helpers.h" |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 25 | #include "scoped_nullable_primitive_array.h" |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 26 | #include <cstdint> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 27 | #include <vector> |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 28 | #include <list> |
| 29 | #include <algorithm> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 30 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 31 | #include "SkPaint.h" |
| 32 | #include "SkTypeface.h" |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 33 | #include <hwui/MinikinSkia.h> |
| 34 | #include <hwui/MinikinUtils.h> |
| 35 | #include <hwui/Paint.h> |
| 36 | #include <minikin/FontCollection.h> |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 37 | #include <minikin/AndroidLineBreakerHelper.h> |
sergeyv | dccca44 | 2016-03-21 15:38:21 -0700 | [diff] [blame] | 38 | #include <minikin/MinikinFont.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; |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 45 | jfieldID ascents; |
| 46 | jfieldID descents; |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 47 | jfieldID flags; |
| 48 | }; |
| 49 | |
| 50 | static jclass gLineBreaks_class; |
| 51 | static JLineBreaksID gLineBreaks_fieldID; |
| 52 | |
Roozbeh Pournader | 0bba742 | 2017-09-28 12:35:10 -0700 | [diff] [blame] | 53 | static inline std::vector<float> jintArrayToFloatVector(JNIEnv* env, jintArray javaArray) { |
| 54 | if (javaArray == nullptr) { |
| 55 | return std::vector<float>(); |
| 56 | } else { |
| 57 | ScopedIntArrayRO intArr(env, javaArray); |
| 58 | return std::vector<float>(intArr.get(), intArr.get() + intArr.size()); |
| 59 | } |
| 60 | } |
| 61 | |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 62 | static inline minikin::android::StaticLayoutNative* toNative(jlong ptr) { |
| 63 | return reinterpret_cast<minikin::android::StaticLayoutNative*>(ptr); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 64 | } |
| 65 | |
Roozbeh Pournader | 95c7a13 | 2015-05-12 12:01:06 -0700 | [diff] [blame] | 66 | // set text and set a number of parameters for creating a layout (width, tabstops, strategy, |
| 67 | // hyphenFrequency) |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 68 | static jlong nInit(JNIEnv* env, jclass /* unused */, |
| 69 | jint breakStrategy, jint hyphenationFrequency, jboolean isJustified, |
| 70 | jintArray indents, jintArray leftPaddings, jintArray rightPaddings) { |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 71 | return reinterpret_cast<jlong>(new minikin::android::StaticLayoutNative( |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 72 | static_cast<minikin::BreakStrategy>(breakStrategy), |
| 73 | static_cast<minikin::HyphenationFrequency>(hyphenationFrequency), |
| 74 | isJustified, |
| 75 | jintArrayToFloatVector(env, indents), |
| 76 | jintArrayToFloatVector(env, leftPaddings), |
| 77 | jintArrayToFloatVector(env, rightPaddings))); |
| 78 | } |
Seigo Nonaka | bafe197 | 2017-08-24 15:30:29 -0700 | [diff] [blame] | 79 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 80 | // CriticalNative |
| 81 | static void nFinish(jlong nativePtr) { |
| 82 | delete toNative(nativePtr); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 83 | } |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 84 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 85 | static void recycleCopy(JNIEnv* env, jobject recycle, jintArray recycleBreaks, |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 86 | jfloatArray recycleWidths, jfloatArray recycleAscents, |
| 87 | jfloatArray recycleDescents, jintArray recycleFlags, |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 88 | jint recycleLength, const minikin::LineBreakResult& result) { |
| 89 | const size_t nBreaks = result.breakPoints.size(); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 90 | if ((size_t)recycleLength < nBreaks) { |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 91 | // have to reallocate buffers |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame] | 92 | recycleBreaks = env->NewIntArray(nBreaks); |
| 93 | recycleWidths = env->NewFloatArray(nBreaks); |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 94 | recycleAscents = env->NewFloatArray(nBreaks); |
| 95 | recycleDescents = env->NewFloatArray(nBreaks); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 96 | recycleFlags = env->NewIntArray(nBreaks); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 97 | |
| 98 | env->SetObjectField(recycle, gLineBreaks_fieldID.breaks, recycleBreaks); |
| 99 | env->SetObjectField(recycle, gLineBreaks_fieldID.widths, recycleWidths); |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 100 | env->SetObjectField(recycle, gLineBreaks_fieldID.ascents, recycleAscents); |
| 101 | env->SetObjectField(recycle, gLineBreaks_fieldID.descents, recycleDescents); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 102 | env->SetObjectField(recycle, gLineBreaks_fieldID.flags, recycleFlags); |
| 103 | } |
| 104 | // copy data |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 105 | env->SetIntArrayRegion(recycleBreaks, 0, nBreaks, result.breakPoints.data()); |
| 106 | env->SetFloatArrayRegion(recycleWidths, 0, nBreaks, result.widths.data()); |
| 107 | env->SetFloatArrayRegion(recycleAscents, 0, nBreaks, result.ascents.data()); |
| 108 | env->SetFloatArrayRegion(recycleDescents, 0, nBreaks, result.descents.data()); |
| 109 | env->SetIntArrayRegion(recycleFlags, 0, nBreaks, result.flags.data()); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 110 | } |
| 111 | |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 112 | static jint nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 113 | // Inputs |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 114 | jcharArray javaText, |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 115 | jlong measuredTextPtr, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 116 | jint length, |
| 117 | jfloat firstWidth, |
| 118 | jint firstWidthLineCount, |
| 119 | jfloat restWidth, |
| 120 | jintArray variableTabStops, |
| 121 | jint defaultTabStop, |
| 122 | jint indentsOffset, |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 123 | |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 124 | // Outputs |
| 125 | jobject recycle, |
| 126 | jint recycleLength, |
| 127 | jintArray recycleBreaks, |
| 128 | jfloatArray recycleWidths, |
| 129 | jfloatArray recycleAscents, |
| 130 | jfloatArray recycleDescents, |
| 131 | jintArray recycleFlags, |
| 132 | jfloatArray charWidths) { |
| 133 | |
Seigo Nonaka | 3e7deec | 2017-11-15 12:06:24 -0800 | [diff] [blame] | 134 | minikin::android::StaticLayoutNative* builder = toNative(nativePtr); |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 135 | |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 136 | ScopedCharArrayRO text(env, javaText); |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 137 | ScopedNullableIntArrayRO tabStops(env, variableTabStops); |
Seigo Nonaka | ef94f49 | 2017-11-10 16:11:33 -0800 | [diff] [blame] | 138 | |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 139 | minikin::U16StringPiece u16Text(text.get(), length); |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 140 | minikin::MeasuredText* measuredText = reinterpret_cast<minikin::MeasuredText*>(measuredTextPtr); |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 141 | minikin::LineBreakResult result = builder->computeBreaks( |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 142 | u16Text, *measuredText, firstWidth, firstWidthLineCount, restWidth, indentsOffset, |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 143 | tabStops.get(), tabStops.size(), defaultTabStop); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 144 | |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 145 | recycleCopy(env, recycle, recycleBreaks, recycleWidths, recycleAscents, recycleDescents, |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 146 | recycleFlags, recycleLength, result); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 147 | |
Seigo Nonaka | 6f1868b | 2017-12-01 16:24:19 -0800 | [diff] [blame] | 148 | env->SetFloatArrayRegion(charWidths, 0, measuredText->widths.size(), |
| 149 | measuredText->widths.data()); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 150 | |
Seigo Nonaka | beb1640 | 2017-11-22 12:07:16 -0800 | [diff] [blame] | 151 | return static_cast<jint>(result.breakPoints.size()); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Daniel Micay | 76f6a86 | 2015-09-19 17:31:01 -0400 | [diff] [blame] | 154 | static const JNINativeMethod gMethods[] = { |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 155 | // Fast Natives |
| 156 | {"nInit", "(" |
| 157 | "I" // breakStrategy |
| 158 | "I" // hyphenationFrequency |
| 159 | "Z" // isJustified |
| 160 | "[I" // indents |
| 161 | "[I" // left paddings |
| 162 | "[I" // right paddings |
| 163 | ")J", (void*) nInit}, |
| 164 | |
| 165 | // Critical Natives |
| 166 | {"nFinish", "(J)V", (void*) nFinish}, |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 167 | |
| 168 | // Regular JNI |
| 169 | {"nComputeLineBreaks", "(" |
| 170 | "J" // nativePtr |
| 171 | |
| 172 | // Inputs |
| 173 | "[C" // text |
Seigo Nonaka | 9d3bd08 | 2018-01-11 10:02:12 -0800 | [diff] [blame] | 174 | "J" // MeasuredParagraph ptr. |
Seigo Nonaka | b90e08f | 2017-10-20 15:23:51 -0700 | [diff] [blame] | 175 | "I" // length |
| 176 | "F" // firstWidth |
| 177 | "I" // firstWidthLineCount |
| 178 | "F" // restWidth |
| 179 | "[I" // variableTabStops |
| 180 | "I" // defaultTabStop |
| 181 | "I" // indentsOffset |
| 182 | |
| 183 | // Outputs |
| 184 | "Landroid/text/StaticLayout$LineBreaks;" // recycle |
| 185 | "I" // recycleLength |
| 186 | "[I" // recycleBreaks |
| 187 | "[F" // recycleWidths |
| 188 | "[F" // recycleAscents |
| 189 | "[F" // recycleDescents |
| 190 | "[I" // recycleFlags |
| 191 | "[F" // charWidths |
| 192 | ")I", (void*) nComputeLineBreaks} |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 193 | }; |
| 194 | |
| 195 | int register_android_text_StaticLayout(JNIEnv* env) |
| 196 | { |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 197 | gLineBreaks_class = MakeGlobalRefOrDie(env, |
| 198 | FindClassOrDie(env, "android/text/StaticLayout$LineBreaks")); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 199 | |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 200 | gLineBreaks_fieldID.breaks = GetFieldIDOrDie(env, gLineBreaks_class, "breaks", "[I"); |
| 201 | gLineBreaks_fieldID.widths = GetFieldIDOrDie(env, gLineBreaks_class, "widths", "[F"); |
Roozbeh Pournader | 737dfea | 2017-08-10 11:32:24 -0700 | [diff] [blame] | 202 | gLineBreaks_fieldID.ascents = GetFieldIDOrDie(env, gLineBreaks_class, "ascents", "[F"); |
| 203 | gLineBreaks_fieldID.descents = GetFieldIDOrDie(env, gLineBreaks_class, "descents", "[F"); |
Raph Levien | 26d443a | 2015-03-30 14:18:32 -0700 | [diff] [blame] | 204 | gLineBreaks_fieldID.flags = GetFieldIDOrDie(env, gLineBreaks_class, "flags", "[I"); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 205 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 206 | return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods)); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 207 | } |
| 208 | |
| 209 | } |