1 /*
2  * Copyright (C) 2020 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 #include "graphics_jni_helpers.h"
18 #include <nativehelper/ScopedStringChars.h>
19 #include <nativehelper/ScopedPrimitiveArray.h>
20 #include <set>
21 #include <algorithm>
22 
23 #include <hwui/MinikinSkia.h>
24 #include <hwui/MinikinUtils.h>
25 #include <hwui/Paint.h>
26 #include <minikin/MinikinFont.h>
27 #include <minikin/MinikinPaint.h>
28 #include "FontUtils.h"
29 #include "SkPaint.h"
30 #include "SkTypeface.h"
31 
32 namespace android {
33 
34 struct LayoutWrapper {
LayoutWrapperandroid::LayoutWrapper35     LayoutWrapper(minikin::Layout&& layout, float ascent, float descent)
36         : layout(std::move(layout)), ascent(ascent), descent(descent)  {}
37     minikin::Layout layout;
38     float ascent;
39     float descent;
40 };
41 
releaseLayout(jlong ptr)42 static void releaseLayout(jlong ptr) {
43     delete reinterpret_cast<LayoutWrapper*>(ptr);
44 }
45 
shapeTextRun(const uint16_t * text,int textSize,int start,int count,int contextStart,int contextCount,minikin::Bidi bidiFlags,const Paint & paint,const Typeface * typeface)46 static jlong shapeTextRun(const uint16_t* text, int textSize, int start, int count,
47     int contextStart, int contextCount, minikin::Bidi bidiFlags,
48     const Paint& paint, const Typeface* typeface) {
49 
50     minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(&paint, typeface);
51 
52     minikin::Layout layout = MinikinUtils::doLayout(&paint, bidiFlags, typeface,
53         text, textSize, start, count, contextStart, contextCount, nullptr);
54 
55     std::set<const minikin::Font*> seenFonts;
56     float overallAscent = 0;
57     float overallDescent = 0;
58     for (int i = 0; i < layout.nGlyphs(); ++i) {
59         const minikin::Font* font = layout.getFont(i);
60         if (seenFonts.find(font) != seenFonts.end()) continue;
61         minikin::MinikinExtent extent = {};
62         layout.typeface(i)->GetFontExtent(&extent, minikinPaint, layout.getFakery(i));
63         overallAscent = std::min(overallAscent, extent.ascent);
64         overallDescent = std::max(overallDescent, extent.descent);
65     }
66 
67     std::unique_ptr<LayoutWrapper> ptr = std::make_unique<LayoutWrapper>(
68         std::move(layout), overallAscent, overallDescent
69     );
70 
71     return reinterpret_cast<jlong>(ptr.release());
72 }
73 
TextShaper_shapeTextRunChars(JNIEnv * env,jobject,jcharArray charArray,jint start,jint count,jint contextStart,jint contextCount,jboolean isRtl,jlong paintPtr)74 static jlong TextShaper_shapeTextRunChars(JNIEnv *env, jobject, jcharArray charArray,
75     jint start, jint count, jint contextStart, jint contextCount, jboolean isRtl,
76     jlong paintPtr) {
77     ScopedCharArrayRO text(env, charArray);
78     Paint* paint = reinterpret_cast<Paint*>(paintPtr);
79     const Typeface* typeface = paint->getAndroidTypeface();
80     const minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
81     return shapeTextRun(
82         text.get(), text.size(),
83         start, count,
84         contextStart, contextCount,
85         bidiFlags,
86         *paint, typeface);
87 
88 }
89 
TextShaper_shapeTextRunString(JNIEnv * env,jobject,jstring string,jint start,jint count,jint contextStart,jint contextCount,jboolean isRtl,jlong paintPtr)90 static jlong TextShaper_shapeTextRunString(JNIEnv *env, jobject, jstring string,
91     jint start, jint count, jint contextStart, jint contextCount, jboolean isRtl,
92     jlong paintPtr) {
93     ScopedStringChars text(env, string);
94     Paint* paint = reinterpret_cast<Paint*>(paintPtr);
95     const Typeface* typeface = paint->getAndroidTypeface();
96     const minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
97     return shapeTextRun(
98         text.get(), text.size(),
99         start, count,
100         contextStart, contextCount,
101         bidiFlags,
102         *paint, typeface);
103 }
104 
105 // CriticalNative
TextShaper_Result_getGlyphCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr)106 static jint TextShaper_Result_getGlyphCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
107     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
108     return layout->layout.nGlyphs();
109 }
110 
111 // CriticalNative
TextShaper_Result_getTotalAdvance(CRITICAL_JNI_PARAMS_COMMA jlong ptr)112 static jfloat TextShaper_Result_getTotalAdvance(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
113     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
114     return layout->layout.getAdvance();
115 }
116 
117 // CriticalNative
TextShaper_Result_getAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr)118 static jfloat TextShaper_Result_getAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
119     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
120     return layout->ascent;
121 }
122 
123 // CriticalNative
TextShaper_Result_getDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr)124 static jfloat TextShaper_Result_getDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
125     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
126     return layout->descent;
127 }
128 
129 // CriticalNative
TextShaper_Result_getGlyphId(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)130 static jint TextShaper_Result_getGlyphId(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
131     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
132     return layout->layout.getGlyphId(i);
133 }
134 
135 // CriticalNative
TextShaper_Result_getX(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)136 static jfloat TextShaper_Result_getX(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
137     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
138     return layout->layout.getX(i);
139 }
140 
141 // CriticalNative
TextShaper_Result_getY(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)142 static jfloat TextShaper_Result_getY(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
143     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
144     return layout->layout.getY(i);
145 }
146 
147 // CriticalNative
TextShaper_Result_getFakeBold(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)148 static jboolean TextShaper_Result_getFakeBold(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
149     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
150     return layout->layout.getFakery(i).isFakeBold();
151 }
152 
153 // CriticalNative
TextShaper_Result_getFakeItalic(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)154 static jboolean TextShaper_Result_getFakeItalic(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
155     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
156     return layout->layout.getFakery(i).isFakeItalic();
157 }
158 
159 // CriticalNative
TextShaper_Result_getWeightOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)160 static jfloat TextShaper_Result_getWeightOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
161     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
162     return layout->layout.getFakery(i).wghtAdjustment();
163 }
164 
165 // CriticalNative
TextShaper_Result_getItalicOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)166 static jfloat TextShaper_Result_getItalicOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
167     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
168     return layout->layout.getFakery(i).italAdjustment();
169 }
170 
171 // CriticalNative
TextShaper_Result_getFont(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)172 static jlong TextShaper_Result_getFont(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
173     const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
174     std::shared_ptr<minikin::Font> fontRef = layout->layout.getFontRef(i);
175     return reinterpret_cast<jlong>(new FontWrapper(std::move(fontRef)));
176 }
177 
178 // CriticalNative
TextShaper_Result_nReleaseFunc(CRITICAL_JNI_PARAMS)179 static jlong TextShaper_Result_nReleaseFunc(CRITICAL_JNI_PARAMS) {
180     return reinterpret_cast<jlong>(releaseLayout);
181 }
182 
183 static const JNINativeMethod gMethods[] = {
184     {"nativeShapeTextRun", "("
185         "[C"  // text
186         "I"  // start
187         "I"  // count
188         "I"  // contextStart
189         "I"  // contextCount
190         "Z"  // isRtl
191         "J)"  // paint
192         "J",  // LayoutPtr
193         (void*) TextShaper_shapeTextRunChars},
194 
195     {"nativeShapeTextRun", "("
196         "Ljava/lang/String;"  // text
197         "I"  // start
198         "I"  // count
199         "I"  // contextStart
200         "I"  // contextCount
201         "Z"  // isRtl
202         "J)"  // paint
203         "J",  // LayoutPtr
204         (void*) TextShaper_shapeTextRunString},
205 
206 };
207 
208 static const JNINativeMethod gResultMethods[] = {
209         {"nGetGlyphCount", "(J)I", (void*)TextShaper_Result_getGlyphCount},
210         {"nGetTotalAdvance", "(J)F", (void*)TextShaper_Result_getTotalAdvance},
211         {"nGetAscent", "(J)F", (void*)TextShaper_Result_getAscent},
212         {"nGetDescent", "(J)F", (void*)TextShaper_Result_getDescent},
213         {"nGetGlyphId", "(JI)I", (void*)TextShaper_Result_getGlyphId},
214         {"nGetX", "(JI)F", (void*)TextShaper_Result_getX},
215         {"nGetY", "(JI)F", (void*)TextShaper_Result_getY},
216         {"nGetFont", "(JI)J", (void*)TextShaper_Result_getFont},
217         {"nGetFakeBold", "(JI)Z", (void*)TextShaper_Result_getFakeBold},
218         {"nGetFakeItalic", "(JI)Z", (void*)TextShaper_Result_getFakeItalic},
219         {"nGetWeightOverride", "(JI)F", (void*)TextShaper_Result_getWeightOverride},
220         {"nGetItalicOverride", "(JI)F", (void*)TextShaper_Result_getItalicOverride},
221         {"nReleaseFunc", "()J", (void*)TextShaper_Result_nReleaseFunc},
222 };
223 
register_android_graphics_text_TextShaper(JNIEnv * env)224 int register_android_graphics_text_TextShaper(JNIEnv* env) {
225     return RegisterMethodsOrDie(env, "android/graphics/text/TextRunShaper", gMethods,
226                                 NELEM(gMethods))
227         + RegisterMethodsOrDie(env, "android/graphics/text/PositionedGlyphs",
228             gResultMethods, NELEM(gResultMethods));
229 }
230 
231 }
232 
233