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 start,jint end,jboolean isRtl)67 static void nAddStyleRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr,
68                          jlong paintPtr, jint start, jint end, jboolean isRtl) {
69     Paint* paint = toPaint(paintPtr);
70     const Typeface* typeface = Typeface::resolveDefault(paint->getAndroidTypeface());
71     minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(paint, typeface);
72     toBuilder(builderPtr)->addStyleRun(start, end, std::move(minikinPaint), isRtl);
73 }
74 
75 // Regular JNI
nAddReplacementRun(JNIEnv *,jclass,jlong builderPtr,jlong paintPtr,jint start,jint end,jfloat width)76 static void nAddReplacementRun(JNIEnv* /* unused */, jclass /* unused */, jlong builderPtr,
77                                jlong paintPtr, jint start, jint end, jfloat width) {
78     toBuilder(builderPtr)->addReplacementRun(start, end, width,
79                                              toPaint(paintPtr)->getMinikinLocaleListId());
80 }
81 
82 // Regular JNI
nBuildMeasuredText(JNIEnv * env,jclass,jlong builderPtr,jlong hintPtr,jcharArray javaText,jboolean computeHyphenation,jboolean computeLayout)83 static jlong nBuildMeasuredText(JNIEnv* env, jclass /* unused */, jlong builderPtr,
84                                 jlong hintPtr, jcharArray javaText, jboolean computeHyphenation,
85                                 jboolean computeLayout) {
86     ScopedCharArrayRO text(env, javaText);
87     const minikin::U16StringPiece textBuffer(text.get(), text.size());
88 
89     // Pass the ownership to Java.
90     return toJLong(toBuilder(builderPtr)->build(textBuffer, computeHyphenation, computeLayout,
91                                                 toMeasuredParagraph(hintPtr)).release());
92 }
93 
94 // Regular JNI
nFreeBuilder(JNIEnv * env,jclass,jlong builderPtr)95 static void nFreeBuilder(JNIEnv* env, jclass /* unused */, jlong builderPtr) {
96     delete toBuilder(builderPtr);
97 }
98 
99 // CriticalNative
nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint start,jint end)100 static jfloat nGetWidth(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint start, jint end) {
101     minikin::MeasuredText* mt = toMeasuredParagraph(ptr);
102     float r = 0.0f;
103     for (int i = start; i < end; ++i) {
104         r += mt->widths[i];
105     }
106     return r;
107 }
108 
nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint offset)109 static jfloat nGetCharWidthAt(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint offset) {
110     return toMeasuredParagraph(ptr)->widths[offset];
111 }
112 
113 // Regular JNI
nGetBounds(JNIEnv * env,jobject,jlong ptr,jcharArray javaText,jint start,jint end,jobject bounds)114 static void nGetBounds(JNIEnv* env, jobject, jlong ptr, jcharArray javaText, jint start, jint end,
115                        jobject bounds) {
116     ScopedCharArrayRO text(env, javaText);
117     const minikin::U16StringPiece textBuffer(text.get(), text.size());
118     const minikin::Range range(start, end);
119 
120     minikin::MinikinRect rect = toMeasuredParagraph(ptr)->getBounds(textBuffer, range);
121 
122     SkRect r;
123     r.fLeft = rect.mLeft;
124     r.fTop = rect.mTop;
125     r.fRight = rect.mRight;
126     r.fBottom = rect.mBottom;
127 
128     SkIRect ir;
129     r.roundOut(&ir);
130     GraphicsJNI::irect_to_jrect(ir, env, bounds);
131 }
132 
133 // CriticalNative
nGetReleaseFunc(CRITICAL_JNI_PARAMS)134 static jlong nGetReleaseFunc(CRITICAL_JNI_PARAMS) {
135     return toJLong(&releaseMeasuredParagraph);
136 }
137 
nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr)138 static jint nGetMemoryUsage(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
139     return static_cast<jint>(toMeasuredParagraph(ptr)->getMemoryUsage());
140 }
141 
142 static const JNINativeMethod gMTBuilderMethods[] = {
143     // MeasuredParagraphBuilder native functions.
144     {"nInitBuilder", "()J", (void*) nInitBuilder},
145     {"nAddStyleRun", "(JJIIZ)V", (void*) nAddStyleRun},
146     {"nAddReplacementRun", "(JJIIF)V", (void*) nAddReplacementRun},
147     {"nBuildMeasuredText", "(JJ[CZZ)J", (void*) nBuildMeasuredText},
148     {"nFreeBuilder", "(J)V", (void*) nFreeBuilder},
149 };
150 
151 static const JNINativeMethod gMTMethods[] = {
152     // MeasuredParagraph native functions.
153     {"nGetWidth", "(JII)F", (void*) nGetWidth},  // Critical Natives
154     {"nGetBounds", "(J[CIILandroid/graphics/Rect;)V", (void*) nGetBounds},  // Regular JNI
155     {"nGetReleaseFunc", "()J", (void*) nGetReleaseFunc},  // Critical Natives
156     {"nGetMemoryUsage", "(J)I", (void*) nGetMemoryUsage},  // Critical Native
157     {"nGetCharWidthAt", "(JI)F", (void*) nGetCharWidthAt},  // Critical Native
158 };
159 
register_android_graphics_text_MeasuredText(JNIEnv * env)160 int register_android_graphics_text_MeasuredText(JNIEnv* env) {
161     return RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText",
162             gMTMethods, NELEM(gMTMethods))
163         + RegisterMethodsOrDie(env, "android/graphics/text/MeasuredText$Builder",
164             gMTBuilderMethods, NELEM(gMTBuilderMethods));
165 }
166 
167 }
168