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 "ScopedStringChars.h"
25 #include "ScopedPrimitiveArray.h"
26 #include "JNIHelp.h"
27 #include "core_jni_helpers.h"
28 #include <cstdint>
29 #include <vector>
30 #include <list>
31 #include <algorithm>
32
33 #include "SkPaint.h"
34 #include "SkTypeface.h"
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>
41
42 namespace android {
43
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
53 // set text and set a number of parameters for creating a layout (width, tabstops, strategy,
54 // hyphenFrequency)
nSetupParagraph(JNIEnv * env,jclass,jlong nativePtr,jcharArray text,jint length,jfloat firstWidth,jint firstWidthLineLimit,jfloat restWidth,jintArray variableTabStops,jint defaultTabStop,jint strategy,jint hyphenFrequency,jboolean isJustified)55 static void nSetupParagraph(JNIEnv* env, jclass, jlong nativePtr, jcharArray text, jint length,
56 jfloat firstWidth, jint firstWidthLineLimit, jfloat restWidth,
57 jintArray variableTabStops, jint defaultTabStop, jint strategy, jint hyphenFrequency,
58 jboolean isJustified) {
59 minikin::LineBreaker* b = reinterpret_cast<minikin::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<minikin::BreakStrategy>(strategy));
71 b->setHyphenationFrequency(static_cast<minikin::HyphenationFrequency>(hyphenFrequency));
72 b->setJustified(isJustified);
73 }
74
recycleCopy(JNIEnv * env,jobject recycle,jintArray recycleBreaks,jfloatArray recycleWidths,jintArray recycleFlags,jint recycleLength,size_t nBreaks,const jint * breaks,const jfloat * widths,const jint * flags)75 static void recycleCopy(JNIEnv* env, jobject recycle, jintArray recycleBreaks,
76 jfloatArray recycleWidths, jintArray recycleFlags,
77 jint recycleLength, size_t nBreaks, const jint* breaks,
78 const jfloat* widths, const jint* flags) {
79 if ((size_t)recycleLength < nBreaks) {
80 // have to reallocate buffers
81 recycleBreaks = env->NewIntArray(nBreaks);
82 recycleWidths = env->NewFloatArray(nBreaks);
83 recycleFlags = env->NewIntArray(nBreaks);
84
85 env->SetObjectField(recycle, gLineBreaks_fieldID.breaks, recycleBreaks);
86 env->SetObjectField(recycle, gLineBreaks_fieldID.widths, recycleWidths);
87 env->SetObjectField(recycle, gLineBreaks_fieldID.flags, recycleFlags);
88 }
89 // copy data
90 env->SetIntArrayRegion(recycleBreaks, 0, nBreaks, breaks);
91 env->SetFloatArrayRegion(recycleWidths, 0, nBreaks, widths);
92 env->SetIntArrayRegion(recycleFlags, 0, nBreaks, flags);
93 }
94
nComputeLineBreaks(JNIEnv * env,jclass,jlong nativePtr,jobject recycle,jintArray recycleBreaks,jfloatArray recycleWidths,jintArray recycleFlags,jint recycleLength)95 static jint nComputeLineBreaks(JNIEnv* env, jclass, jlong nativePtr,
96 jobject recycle, jintArray recycleBreaks,
97 jfloatArray recycleWidths, jintArray recycleFlags,
98 jint recycleLength) {
99 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
100
101 size_t nBreaks = b->computeBreaks();
102
103 recycleCopy(env, recycle, recycleBreaks, recycleWidths, recycleFlags, recycleLength,
104 nBreaks, b->getBreaks(), b->getWidths(), b->getFlags());
105
106 b->finish();
107
108 return static_cast<jint>(nBreaks);
109 }
110
nNewBuilder(JNIEnv *,jclass)111 static jlong nNewBuilder(JNIEnv*, jclass) {
112 return reinterpret_cast<jlong>(new minikin::LineBreaker);
113 }
114
nFreeBuilder(JNIEnv *,jclass,jlong nativePtr)115 static void nFreeBuilder(JNIEnv*, jclass, jlong nativePtr) {
116 delete reinterpret_cast<minikin::LineBreaker*>(nativePtr);
117 }
118
nFinishBuilder(JNIEnv *,jclass,jlong nativePtr)119 static void nFinishBuilder(JNIEnv*, jclass, jlong nativePtr) {
120 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
121 b->finish();
122 }
123
nLoadHyphenator(JNIEnv * env,jclass,jobject buffer,jint offset,jint minPrefix,jint minSuffix)124 static jlong nLoadHyphenator(JNIEnv* env, jclass, jobject buffer, jint offset,
125 jint minPrefix, jint minSuffix) {
126 const uint8_t* bytebuf = nullptr;
127 if (buffer != nullptr) {
128 void* rawbuf = env->GetDirectBufferAddress(buffer);
129 if (rawbuf != nullptr) {
130 bytebuf = reinterpret_cast<const uint8_t*>(rawbuf) + offset;
131 } else {
132 ALOGE("failed to get direct buffer address");
133 }
134 }
135 minikin::Hyphenator* hyphenator = minikin::Hyphenator::loadBinary(
136 bytebuf, minPrefix, minSuffix);
137 return reinterpret_cast<jlong>(hyphenator);
138 }
139
nSetLocale(JNIEnv * env,jclass,jlong nativePtr,jstring javaLocaleName,jlong nativeHyphenator)140 static void nSetLocale(JNIEnv* env, jclass, jlong nativePtr, jstring javaLocaleName,
141 jlong nativeHyphenator) {
142 ScopedIcuLocale icuLocale(env, javaLocaleName);
143 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
144 minikin::Hyphenator* hyphenator = reinterpret_cast<minikin::Hyphenator*>(nativeHyphenator);
145
146 if (icuLocale.valid()) {
147 b->setLocale(icuLocale.locale(), hyphenator);
148 }
149 }
150
nSetIndents(JNIEnv * env,jclass,jlong nativePtr,jintArray indents)151 static void nSetIndents(JNIEnv* env, jclass, jlong nativePtr, jintArray indents) {
152 ScopedIntArrayRO indentArr(env, indents);
153 std::vector<float> indentVec(indentArr.get(), indentArr.get() + indentArr.size());
154 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
155 b->setIndents(indentVec);
156 }
157
158 // Basically similar to Paint.getTextRunAdvances but with C++ interface
nAddStyleRun(JNIEnv * env,jclass,jlong nativePtr,jlong nativePaint,jlong nativeTypeface,jint start,jint end,jboolean isRtl)159 static jfloat nAddStyleRun(JNIEnv* env, jclass, jlong nativePtr,
160 jlong nativePaint, jlong nativeTypeface, jint start, jint end, jboolean isRtl) {
161 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
162 Paint* paint = reinterpret_cast<Paint*>(nativePaint);
163 Typeface* typeface = reinterpret_cast<Typeface*>(nativeTypeface);
164 minikin::MinikinPaint minikinPaint;
165 Typeface* resolvedTypeface = Typeface::resolveDefault(typeface);
166 minikin::FontStyle style = MinikinUtils::prepareMinikinPaint(&minikinPaint, paint,
167 typeface);
168 return b->addStyleRun(&minikinPaint, resolvedTypeface->fFontCollection, style, start, end,
169 isRtl);
170 }
171
172 // Accept width measurements for the run, passed in from Java
nAddMeasuredRun(JNIEnv * env,jclass,jlong nativePtr,jint start,jint end,jfloatArray widths)173 static void nAddMeasuredRun(JNIEnv* env, jclass, jlong nativePtr,
174 jint start, jint end, jfloatArray widths) {
175 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
176 env->GetFloatArrayRegion(widths, start, end - start, b->charWidths() + start);
177 b->addStyleRun(nullptr, nullptr, minikin::FontStyle{}, start, end, false);
178 }
179
nAddReplacementRun(JNIEnv * env,jclass,jlong nativePtr,jint start,jint end,jfloat width)180 static void nAddReplacementRun(JNIEnv* env, jclass, jlong nativePtr,
181 jint start, jint end, jfloat width) {
182 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
183 b->addReplacement(start, end, width);
184 }
185
nGetWidths(JNIEnv * env,jclass,jlong nativePtr,jfloatArray widths)186 static void nGetWidths(JNIEnv* env, jclass, jlong nativePtr, jfloatArray widths) {
187 minikin::LineBreaker* b = reinterpret_cast<minikin::LineBreaker*>(nativePtr);
188 env->SetFloatArrayRegion(widths, 0, b->size(), b->charWidths());
189 }
190
191 static const JNINativeMethod gMethods[] = {
192 // TODO performance: many of these are candidates for fast jni, awaiting guidance
193 {"nNewBuilder", "()J", (void*) nNewBuilder},
194 {"nFreeBuilder", "(J)V", (void*) nFreeBuilder},
195 {"nFinishBuilder", "(J)V", (void*) nFinishBuilder},
196 {"nLoadHyphenator", "(Ljava/nio/ByteBuffer;III)J", (void*) nLoadHyphenator},
197 {"nSetLocale", "(JLjava/lang/String;J)V", (void*) nSetLocale},
198 {"nSetupParagraph", "(J[CIFIF[IIIIZ)V", (void*) nSetupParagraph},
199 {"nSetIndents", "(J[I)V", (void*) nSetIndents},
200 {"nAddStyleRun", "(JJJIIZ)F", (void*) nAddStyleRun},
201 {"nAddMeasuredRun", "(JII[F)V", (void*) nAddMeasuredRun},
202 {"nAddReplacementRun", "(JIIF)V", (void*) nAddReplacementRun},
203 {"nGetWidths", "(J[F)V", (void*) nGetWidths},
204 {"nComputeLineBreaks", "(JLandroid/text/StaticLayout$LineBreaks;[I[F[II)I",
205 (void*) nComputeLineBreaks}
206 };
207
register_android_text_StaticLayout(JNIEnv * env)208 int register_android_text_StaticLayout(JNIEnv* env)
209 {
210 gLineBreaks_class = MakeGlobalRefOrDie(env,
211 FindClassOrDie(env, "android/text/StaticLayout$LineBreaks"));
212
213 gLineBreaks_fieldID.breaks = GetFieldIDOrDie(env, gLineBreaks_class, "breaks", "[I");
214 gLineBreaks_fieldID.widths = GetFieldIDOrDie(env, gLineBreaks_class, "widths", "[F");
215 gLineBreaks_fieldID.flags = GetFieldIDOrDie(env, gLineBreaks_class, "flags", "[I");
216
217 return RegisterMethodsOrDie(env, "android/text/StaticLayout", gMethods, NELEM(gMethods));
218 }
219
220 }
221