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 | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 27 | #include <cstdint> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 28 | #include <vector> |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 29 | #include <list> |
| 30 | #include <algorithm> |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 31 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 32 | #include "SkPaint.h" |
| 33 | #include "SkTypeface.h" |
| 34 | #include "MinikinSkia.h" |
| 35 | #include "MinikinUtils.h" |
| 36 | #include "Paint.h" |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 37 | #include "minikin/LineBreaker.h" |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 38 | |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 39 | namespace android { |
| 40 | |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 41 | struct JLineBreaksID { |
| 42 | jfieldID breaks; |
| 43 | jfieldID widths; |
| 44 | jfieldID flags; |
| 45 | }; |
| 46 | |
| 47 | static jclass gLineBreaks_class; |
| 48 | static JLineBreaksID gLineBreaks_fieldID; |
| 49 | |
| 50 | static const int CHAR_SPACE = 0x20; |
| 51 | static const int CHAR_TAB = 0x09; |
| 52 | static const int CHAR_NEWLINE = 0x0a; |
| 53 | static const int CHAR_ZWSP = 0x200b; |
| 54 | |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 55 | // set text and set a number of parameters for creating a layout (width, tabstops, strategy) |
| 56 | static void nSetupParagraph(JNIEnv* env, jclass, jlong nativePtr, jcharArray text, jint length, |
| 57 | jfloat firstWidth, jint firstWidthLineLimit, jfloat restWidth, |
| 58 | jintArray variableTabStops, jint defaultTabStop, jint strategy) { |
| 59 | LineBreaker* b = reinterpret_cast<LineBreaker*>(nativePtr); |
| 60 | b->resize(length); |
| 61 | env->GetCharArrayRegion(text, 0, length, b->buffer()); |
| 62 | b->setText(); |
| 63 | b->setLineWidths(firstWidth, firstWidthLineLimit, restWidth); |
| 64 | if (variableTabStops == nullptr) { |
| 65 | b->setTabStops(nullptr, 0, defaultTabStop); |
| 66 | } else { |
| 67 | ScopedIntArrayRO stops(env, variableTabStops); |
| 68 | b->setTabStops(stops.get(), stops.size(), defaultTabStop); |
| 69 | } |
| 70 | b->setStrategy(static_cast<BreakStrategy>(strategy)); |
| 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, |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 74 | jfloatArray recycleWidths, jbooleanArray recycleFlags, |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 75 | jint recycleLength, size_t nBreaks, const jint* breaks, |
| 76 | const jfloat* widths, const jboolean* flags) { |
| 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); |
| 81 | recycleFlags = env->NewBooleanArray(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); |
| 90 | env->SetBooleanArrayRegion(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, |
| 95 | jfloatArray recycleWidths, jbooleanArray recycleFlags, |
| 96 | jint recycleLength) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 97 | LineBreaker* b = reinterpret_cast<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) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 110 | return reinterpret_cast<jlong>(new LineBreaker); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static void nFreeBuilder(JNIEnv*, jclass, jlong nativePtr) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 114 | delete reinterpret_cast<LineBreaker*>(nativePtr); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 115 | } |
| 116 | |
| 117 | static void nFinishBuilder(JNIEnv*, jclass, jlong nativePtr) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 118 | LineBreaker* b = reinterpret_cast<LineBreaker*>(nativePtr); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 119 | b->finish(); |
| 120 | } |
| 121 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 122 | static void nSetLocale(JNIEnv* env, jclass, jlong nativePtr, jstring javaLocaleName) { |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 123 | ScopedIcuLocale icuLocale(env, javaLocaleName); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 124 | LineBreaker* b = reinterpret_cast<LineBreaker*>(nativePtr); |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 125 | |
| 126 | if (icuLocale.valid()) { |
| 127 | b->setLocale(icuLocale.locale()); |
| 128 | } |
| 129 | } |
| 130 | |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 131 | // Basically similar to Paint.getTextRunAdvances but with C++ interface |
| 132 | static jfloat nAddStyleRun(JNIEnv* env, jclass, jlong nativePtr, |
| 133 | jlong nativePaint, jlong nativeTypeface, jint start, jint end, jboolean isRtl) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 134 | LineBreaker* b = reinterpret_cast<LineBreaker*>(nativePtr); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 135 | Paint* paint = reinterpret_cast<Paint*>(nativePaint); |
| 136 | TypefaceImpl* typeface = reinterpret_cast<TypefaceImpl*>(nativeTypeface); |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 137 | FontCollection *font; |
| 138 | MinikinPaint minikinPaint; |
| 139 | FontStyle style = MinikinUtils::prepareMinikinPaint(&minikinPaint, &font, paint, typeface); |
| 140 | return b->addStyleRun(&minikinPaint, font, style, start, end, isRtl); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 141 | } |
| 142 | |
| 143 | // Accept width measurements for the run, passed in from Java |
| 144 | static void nAddMeasuredRun(JNIEnv* env, jclass, jlong nativePtr, |
| 145 | jint start, jint end, jfloatArray widths) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 146 | LineBreaker* b = reinterpret_cast<LineBreaker*>(nativePtr); |
| 147 | env->GetFloatArrayRegion(widths, start, end - start, b->charWidths() + start); |
| 148 | b->addStyleRun(nullptr, nullptr, FontStyle{}, start, end, false); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | static void nAddReplacementRun(JNIEnv* env, jclass, jlong nativePtr, |
| 152 | jint start, jint end, jfloat width) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 153 | LineBreaker* b = reinterpret_cast<LineBreaker*>(nativePtr); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 154 | b->addReplacement(start, end, width); |
| 155 | } |
| 156 | |
| 157 | static void nGetWidths(JNIEnv* env, jclass, jlong nativePtr, jfloatArray widths) { |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 158 | LineBreaker* b = reinterpret_cast<LineBreaker*>(nativePtr); |
| 159 | env->SetFloatArrayRegion(widths, 0, b->size(), b->charWidths()); |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 162 | static JNINativeMethod gMethods[] = { |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 163 | // TODO performance: many of these are candidates for fast jni, awaiting guidance |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 164 | {"nNewBuilder", "()J", (void*) nNewBuilder}, |
| 165 | {"nFreeBuilder", "(J)V", (void*) nFreeBuilder}, |
| 166 | {"nFinishBuilder", "(J)V", (void*) nFinishBuilder}, |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 167 | {"nSetLocale", "(JLjava/lang/String;)V", (void*) nSetLocale}, |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 168 | {"nSetupParagraph", "(J[CIFIF[III)V", (void*) nSetupParagraph}, |
Raph Levien | 70616ec | 2015-03-04 10:41:30 -0800 | [diff] [blame] | 169 | {"nAddStyleRun", "(JJJIIZ)F", (void*) nAddStyleRun}, |
| 170 | {"nAddMeasuredRun", "(JII[F)V", (void*) nAddMeasuredRun}, |
| 171 | {"nAddReplacementRun", "(JIIF)V", (void*) nAddReplacementRun}, |
| 172 | {"nGetWidths", "(J[F)V", (void*) nGetWidths}, |
Raph Levien | c94f742 | 2015-03-06 19:19:48 -0800 | [diff] [blame^] | 173 | {"nComputeLineBreaks", "(JLandroid/text/StaticLayout$LineBreaks;[I[F[ZI)I", |
Raph Levien | 4c1f12e | 2015-03-02 16:29:23 -0800 | [diff] [blame] | 174 | (void*) nComputeLineBreaks} |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 175 | }; |
| 176 | |
| 177 | int register_android_text_StaticLayout(JNIEnv* env) |
| 178 | { |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 179 | gLineBreaks_class = MakeGlobalRefOrDie(env, |
| 180 | FindClassOrDie(env, "android/text/StaticLayout$LineBreaks")); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 181 | |
Andreas Gampe | 158b6c9 | 2014-11-21 14:03:34 -0800 | [diff] [blame] | 182 | gLineBreaks_fieldID.breaks = GetFieldIDOrDie(env, gLineBreaks_class, "breaks", "[I"); |
| 183 | gLineBreaks_fieldID.widths = GetFieldIDOrDie(env, gLineBreaks_class, "widths", "[F"); |
| 184 | gLineBreaks_fieldID.flags = GetFieldIDOrDie(env, gLineBreaks_class, "flags", "[Z"); |
Anish Athalye | c8f9e62 | 2014-07-21 15:26:34 -0700 | [diff] [blame] | 185 | |
Andreas Gampe | ed6b9df | 2014-11-20 22:02:20 -0800 | [diff] [blame] | 186 | return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods)); |
Anish Athalye | 88b5b0b | 2014-06-24 14:39:43 -0700 | [diff] [blame] | 187 | } |
| 188 | |
| 189 | } |