1 /*
2 * Copyright (C) 2017 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 #undef LOG_TAG
18 #define LOG_TAG "MeasuredText"
19
20 #include "GraphicsJNI.h"
21 #include "utils/misc.h"
22 #include "utils/Log.h"
23 #include <nativehelper/ScopedStringChars.h>
24 #include <nativehelper/ScopedPrimitiveArray.h>
25 #include <cstdint>
26 #include <vector>
27 #include <list>
28 #include <algorithm>
29
30 #include "SkPaint.h"
31 #include "SkTypeface.h"
32 #include <hwui/MinikinSkia.h>
33 #include <hwui/MinikinUtils.h>
34 #include <hwui/Paint.h>
35 #include <minikin/FontCollection.h>
36 #include <minikin/AndroidLineBreakerHelper.h>
37 #include <minikin/MinikinFont.h>
38
39 namespace android {
40
toBuilder(jlong ptr)41 static inline minikin::MeasuredTextBuilder* toBuilder(jlong ptr) {
42 return reinterpret_cast<minikin::MeasuredTextBuilder*>(ptr);
43 }
44
toPaint(jlong ptr)45 static inline Paint* toPaint(jlong ptr) {
46 return reinterpret_cast<Paint*>(ptr);
47 }
48
toMeasuredParagraph(jlong ptr)49 static inline minikin::MeasuredText* toMeasuredParagraph(jlong ptr) {
50 return reinterpret_cast<minikin::MeasuredText*>(ptr);
51 }
52
toJLong(Ptr ptr)53 template<typename Ptr> static inline jlong toJLong(Ptr ptr) {
54 return reinterpret_cast<jlong>(ptr);
55 }
56
releaseMeasuredParagraph(jlong measuredTextPtr)57 static void releaseMeasuredParagraph(jlong measuredTextPtr) {
58 delete toMeasuredParagraph(measuredTextPtr);
59 }
60
61 // Regular JNI
nInitBuilder(CRITICAL_JNI_PARAMS)62 static jlong nInitBuilder(CRITICAL_JNI_PARAMS) {
63 return toJLong(new minikin::MeasuredTextBuilder());
64 }
65
66 // Regular JNI
nAddStyleRun(JNIEnv *,jclass,jlong builderPtr,jlong paintPtr,jint lbStyle,jint lbWordStyle,jint start,jint end,jboolean isRtl)67 static void nAddStyleRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr,
68 jlong paintPtr, jint lbStyle, jint lbWordStyle, jint start, jint end,
69 jboolean isRtl) {
70 Paint* paint = toPaint(paintPtr);
71 const Typeface* typeface = Typeface::resolveDefault(paint->getAndroidTypeface());
72 minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(paint, typeface);
73 toBuilder(builderPtr)
74 ->addStyleRun(start, end, std::move(minikinPaint), lbStyle, lbWordStyle, isRtl);
75 }
76
77 // Regular JNI
nAddReplacementRun(JNIEnv *,jclass,jlong builderPtr,jlong paintPtr,jint start,jint end,jfloat width)78 static void nAddReplacementRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr,
79 jlong paintPtr, jint start, jint end, jfloat width) {
80 toBuilder(builderPtr)->addReplacementRun(start, end, width,
81 toPaint(paintPtr)->getMinikinLocaleListId());
82 }
83
84 // Regular JNI
nBuildMeasuredText(JNIEnv * env,jclass,jlong builderPtr,jlong hintPtr,jcharArray javaText,jboolean computeHyphenation,jboolean computeLayout,jboolean fastHyphenationMode)85 static jlong nBuildMeasuredText(JNIEnv* env, jclass /* unused */, jlong builderPtr, jlong hintPtr,
86 jcharArray javaText, jboolean computeHyphenation,
87 jboolean computeLayout, jboolean fastHyphenationMode) {
88 ScopedCharArrayRO text(env, javaText);
89 const minikin::U16StringPiece textBuffer(text.get(), text.size());
90
91 // Pass the ownership to Java.
92 return toJLong(toBuilder(builderPtr)
93 ->build(textBuffer, computeHyphenation, computeLayout,
94 fastHyphenationMode, toMeasuredParagraph(hintPtr))
95 .release());
96 }
97
98 // Regular JNI
nFreeBuilder(JNIEnv * env,jclass,jlong builderPtr)99 static void nFreeBuilder(JNIEnv* env, jclass /* unused */, jlong builderPtr) {
100 delete toBuilder(builderPtr);
101 }
102
103 // CriticalNative
nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint start,jint end)104 static jfloat nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint start, jint end) {
105 minikin::MeasuredText* mt = toMeasuredParagraph(ptr);
106 float r = 0.0f;
107 for (int i = start; i < end; ++i) {
108 r += mt->widths[i];
109 }
110 return r;
111 }
112
nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint offset)113 static jfloat nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint offset) {
114 return toMeasuredParagraph(ptr)->widths[offset];
115 }
116
117 // Regular JNI
nGetBounds(JNIEnv * env,jobject,jlong ptr,jcharArray javaText,jint start,jint end,jobject bounds)118 static void nGetBounds(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, jint end,
119 jobject bounds) {
120 ScopedCharArrayRO text(env, javaText);
121 const minikin::U16StringPiece textBuffer(text.get(), text.size());
122 const minikin::Range range(start, end);
123
124 minikin::MinikinRect rect = toMeasuredParagraph(ptr)->getBounds(textBuffer, range);
125
126 SkRect r;
127 r.fLeft = rect.mLeft;
128 r.fTop = rect.mTop;
129 r.fRight = rect.mRight;
130 r.fBottom = rect.mBottom;
131
132 SkIRect ir;
133 r.roundOut(&ir);
134 GraphicsJNI::irect_to_jrect(ir, env, bounds);
135 }
136
137 // Regular JNI
nGetExtent(JNIEnv * env,jobject,jlong ptr,jcharArray javaText,jint start,jint end)138 static jlong nGetExtent(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start,
139 jint end) {
140 ScopedCharArrayRO text(env, javaText);
141 const minikin::U16StringPiece textBuffer(text.get(), text.size());
142 const minikin::Range range(start, end);
143
144 minikin::MinikinExtent extent = toMeasuredParagraph(ptr)->getExtent(textBuffer, range);
145
146 int32_t ascent = SkScalarRoundToInt(extent.ascent);
147 int32_t descent = SkScalarRoundToInt(extent.descent);
148
149 return (((jlong)(ascent)) << 32) | ((jlong)descent);
150 }
151
152 // CriticalNative
nGetReleaseFunc(CRITICAL_JNI_PARAMS)153 static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) {
154 return toJLong(&releaseMeasuredParagraph);
155 }
156
nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr)157 static jint nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
158 return static_cast<jint>(toMeasuredParagraph(ptr)->getMemoryUsage());
159 }
160
161 static const JNINativeMethod gMTBuilderMethods[] = {
162 // MeasuredParagraphBuilder native functions.
163 {"nInitBuilder", "()J", (void*)nInitBuilder},
164 {"nAddStyleRun", "(JJIIIIZ)V", (void*)nAddStyleRun},
165 {"nAddReplacementRun", "(JJIIF)V", (void*)nAddReplacementRun},
166 {"nBuildMeasuredText", "(JJ[CZZZ)J", (void*)nBuildMeasuredText},
167 {"nFreeBuilder", "(J)V", (void*)nFreeBuilder},
168 };
169
170 static const JNINativeMethod gMTMethods[] = {
171 // MeasuredParagraph native functions.
172 {"nGetWidth", "(JII)F", (void*)nGetWidth}, // Critical Natives
173 {"nGetBounds", "(J[CIILandroid/graphics/Rect;)V", (void*)nGetBounds}, // Regular JNI
174 {"nGetExtent", "(J[CII)J", (void*)nGetExtent}, // Regular JNI
175 {"nGetReleaseFunc", "()J", (void*)nGetReleaseFunc}, // Critical Natives
176 {"nGetMemoryUsage", "(J)I", (void*)nGetMemoryUsage}, // Critical Native
177 {"nGetCharWidthAt", "(JI)F", (void*)nGetCharWidthAt}, // Critical Native
178 };
179
register_android_graphics_text_MeasuredText(JNIEnv * env)180 int register_android_graphics_text_MeasuredText(JNIEnv* env) {
181 return RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText",
182 gMTMethods, NELEM(gMTMethods))
183 + RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText$Builder",
184 gMTBuilderMethods, NELEM(gMTBuilderMethods));
185 }
186
187 }
188