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 FakedFontKey {
operator ()android::FakedFontKey35 uint32_t operator()(const minikin::FakedFont& fakedFont) const {
36 return minikin::Hasher()
37 .update(reinterpret_cast<uintptr_t>(fakedFont.font.get()))
38 .update(fakedFont.fakery.bits())
39 .update(fakedFont.fakery.variationSettings())
40 .hash();
41 }
42 };
43
44 struct LayoutWrapper {
LayoutWrapperandroid::LayoutWrapper45 LayoutWrapper(minikin::Layout&& layout, float ascent, float descent)
46 : layout(std::move(layout)), ascent(ascent), descent(descent) {}
47
LayoutWrapperandroid::LayoutWrapper48 LayoutWrapper(minikin::Layout&& layout, float ascent, float descent, std::vector<jlong>&& fonts,
49 std::vector<uint32_t>&& fontIds)
50 : layout(std::move(layout))
51 , ascent(ascent)
52 , descent(descent)
53 , fonts(std::move(fonts))
54 , fontIds(std::move(fontIds)) {}
55
56 minikin::Layout layout;
57 float ascent;
58 float descent;
59
60 std::vector<jlong> fonts;
61 std::vector<uint32_t> fontIds; // per glyph
62 };
63
releaseLayout(jlong ptr)64 static void releaseLayout(jlong ptr) {
65 delete reinterpret_cast<LayoutWrapper*>(ptr);
66 }
67
shapeTextRun(const uint16_t * text,int textSize,int start,int count,int contextStart,int contextCount,minikin::Bidi bidiFlags,const Paint & paint,const Typeface * typeface)68 static jlong shapeTextRun(const uint16_t* text, int textSize, int start, int count,
69 int contextStart, int contextCount, minikin::Bidi bidiFlags,
70 const Paint& paint, const Typeface* typeface) {
71 const Typeface* resolvedFace = Typeface::resolveDefault(typeface);
72 minikin::MinikinPaint minikinPaint = MinikinUtils::prepareMinikinPaint(&paint, typeface);
73
74 minikin::Layout layout = MinikinUtils::doLayout(&paint, bidiFlags, typeface,
75 text, textSize, start, count, contextStart, contextCount, nullptr);
76
77 std::set<const minikin::Font*> seenFonts;
78 float overallAscent = 0;
79 float overallDescent = 0;
80 for (int i = 0; i < layout.nGlyphs(); ++i) {
81 const minikin::Font* font = layout.getFont(i);
82 if (seenFonts.find(font) != seenFonts.end()) continue;
83 minikin::MinikinExtent extent = {};
84 layout.typeface(i)->GetFontExtent(&extent, minikinPaint, layout.getFakery(i));
85 overallAscent = std::min(overallAscent, extent.ascent);
86 overallDescent = std::max(overallDescent, extent.descent);
87 }
88
89 if (text_feature::typeface_redesign_readonly()) {
90 uint32_t runCount = layout.getFontRunCount();
91
92 std::unordered_map<minikin::FakedFont, uint32_t, FakedFontKey> fakedToFontIds;
93 std::vector<jlong> fonts;
94 std::vector<uint32_t> fontIds;
95
96 fontIds.resize(layout.nGlyphs());
97 for (uint32_t ri = 0; ri < runCount; ++ri) {
98 const minikin::FakedFont& fakedFont = layout.getFontRunFont(ri);
99
100 auto it = fakedToFontIds.find(fakedFont);
101 uint32_t fontId;
102 if (it != fakedToFontIds.end()) {
103 fontId = it->second; // We've seen it.
104 } else {
105 fontId = fonts.size(); // This is new to us. Create new one.
106 std::shared_ptr<minikin::Font> font;
107 if (resolvedFace->fIsVariationInstance) {
108 // The optimization for target SDK 35 or before because the variation instance
109 // is already created and no runtime variation resolution happens on such
110 // environment.
111 font = fakedFont.font;
112 } else {
113 font = std::make_shared<minikin::Font>(fakedFont.font,
114 fakedFont.fakery.variationSettings());
115 }
116 fonts.push_back(reinterpret_cast<jlong>(new FontWrapper(std::move(font))));
117 fakedToFontIds.insert(std::make_pair(fakedFont, fontId));
118 }
119
120 const uint32_t runStart = layout.getFontRunStart(ri);
121 const uint32_t runEnd = layout.getFontRunEnd(ri);
122 for (uint32_t i = runStart; i < runEnd; ++i) {
123 fontIds[i] = fontId;
124 }
125 }
126
127 std::unique_ptr<LayoutWrapper> ptr =
128 std::make_unique<LayoutWrapper>(std::move(layout), overallAscent, overallDescent,
129 std::move(fonts), std::move(fontIds));
130
131 return reinterpret_cast<jlong>(ptr.release());
132 }
133
134 std::unique_ptr<LayoutWrapper> ptr = std::make_unique<LayoutWrapper>(
135 std::move(layout), overallAscent, overallDescent
136 );
137
138 return reinterpret_cast<jlong>(ptr.release());
139 }
140
TextShaper_shapeTextRunChars(JNIEnv * env,jobject,jcharArray charArray,jint start,jint count,jint contextStart,jint contextCount,jboolean isRtl,jlong paintPtr)141 static jlong TextShaper_shapeTextRunChars(JNIEnv *env, jobject, jcharArray charArray,
142 jint start, jint count, jint contextStart, jint contextCount, jboolean isRtl,
143 jlong paintPtr) {
144 ScopedCharArrayRO text(env, charArray);
145 Paint* paint = reinterpret_cast<Paint*>(paintPtr);
146 const Typeface* typeface = paint->getAndroidTypeface();
147 const minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
148 return shapeTextRun(
149 text.get(), text.size(),
150 start, count,
151 contextStart, contextCount,
152 bidiFlags,
153 *paint, typeface);
154
155 }
156
TextShaper_shapeTextRunString(JNIEnv * env,jobject,jstring string,jint start,jint count,jint contextStart,jint contextCount,jboolean isRtl,jlong paintPtr)157 static jlong TextShaper_shapeTextRunString(JNIEnv *env, jobject, jstring string,
158 jint start, jint count, jint contextStart, jint contextCount, jboolean isRtl,
159 jlong paintPtr) {
160 ScopedStringChars text(env, string);
161 Paint* paint = reinterpret_cast<Paint*>(paintPtr);
162 const Typeface* typeface = paint->getAndroidTypeface();
163 const minikin::Bidi bidiFlags = isRtl ? minikin::Bidi::FORCE_RTL : minikin::Bidi::FORCE_LTR;
164 return shapeTextRun(
165 text.get(), text.size(),
166 start, count,
167 contextStart, contextCount,
168 bidiFlags,
169 *paint, typeface);
170 }
171
172 // CriticalNative
TextShaper_Result_getGlyphCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr)173 static jint TextShaper_Result_getGlyphCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
174 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
175 return layout->layout.nGlyphs();
176 }
177
178 // CriticalNative
TextShaper_Result_getTotalAdvance(CRITICAL_JNI_PARAMS_COMMA jlong ptr)179 static jfloat TextShaper_Result_getTotalAdvance(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
180 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
181 return layout->layout.getAdvance();
182 }
183
184 // CriticalNative
TextShaper_Result_getAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr)185 static jfloat TextShaper_Result_getAscent(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
186 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
187 return layout->ascent;
188 }
189
190 // CriticalNative
TextShaper_Result_getDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr)191 static jfloat TextShaper_Result_getDescent(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
192 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
193 return layout->descent;
194 }
195
196 // CriticalNative
TextShaper_Result_getGlyphId(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)197 static jint TextShaper_Result_getGlyphId(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
198 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
199 return layout->layout.getGlyphId(i);
200 }
201
202 // CriticalNative
TextShaper_Result_getX(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)203 static jfloat TextShaper_Result_getX(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
204 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
205 return layout->layout.getX(i);
206 }
207
208 // CriticalNative
TextShaper_Result_getY(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)209 static jfloat TextShaper_Result_getY(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
210 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
211 return layout->layout.getY(i);
212 }
213
214 // CriticalNative
TextShaper_Result_getFakeBold(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)215 static jboolean TextShaper_Result_getFakeBold(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
216 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
217 return layout->layout.getFakery(i).isFakeBold();
218 }
219
220 // CriticalNative
TextShaper_Result_getFakeItalic(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)221 static jboolean TextShaper_Result_getFakeItalic(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
222 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
223 return layout->layout.getFakery(i).isFakeItalic();
224 }
225
226 constexpr float NO_OVERRIDE = -1;
227
findValueFromVariationSettings(const minikin::VariationSettings & axes,minikin::AxisTag tag)228 float findValueFromVariationSettings(const minikin::VariationSettings& axes, minikin::AxisTag tag) {
229 for (const minikin::FontVariation& fv : axes) {
230 if (fv.axisTag == tag) {
231 return fv.value;
232 }
233 }
234 return std::numeric_limits<float>::quiet_NaN();
235 }
236
237 // CriticalNative
TextShaper_Result_getWeightOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)238 static jfloat TextShaper_Result_getWeightOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
239 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
240 if (text_feature::typeface_redesign_readonly()) {
241 float value = findValueFromVariationSettings(layout->layout.typeface(i)->GetAxes(),
242 minikin::TAG_wght);
243 return std::isnan(value) ? NO_OVERRIDE : value;
244 } else {
245 return layout->layout.getFakery(i).wghtAdjustment();
246 }
247 }
248
249 // CriticalNative
TextShaper_Result_getItalicOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)250 static jfloat TextShaper_Result_getItalicOverride(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
251 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
252 if (text_feature::typeface_redesign_readonly()) {
253 float value = findValueFromVariationSettings(layout->layout.typeface(i)->GetAxes(),
254 minikin::TAG_ital);
255 return std::isnan(value) ? NO_OVERRIDE : value;
256 } else {
257 return layout->layout.getFakery(i).italAdjustment();
258 }
259 }
260
261 // CriticalNative
TextShaper_Result_getFont(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint i)262 static jlong TextShaper_Result_getFont(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint i) {
263 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
264 std::shared_ptr<minikin::Font> fontRef = layout->layout.getFontRef(i);
265 return reinterpret_cast<jlong>(new FontWrapper(std::move(fontRef)));
266 }
267
268 // CriticalNative
TextShaper_Result_getFontCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr)269 static jint TextShaper_Result_getFontCount(CRITICAL_JNI_PARAMS_COMMA jlong ptr) {
270 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
271 return layout->fonts.size();
272 }
273
274 // CriticalNative
TextShaper_Result_getFontRef(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint fontId)275 static jlong TextShaper_Result_getFontRef(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint fontId) {
276 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
277 return layout->fonts[fontId];
278 }
279
280 // CriticalNative
TextShaper_Result_getFontId(CRITICAL_JNI_PARAMS_COMMA jlong ptr,jint glyphIdx)281 static jint TextShaper_Result_getFontId(CRITICAL_JNI_PARAMS_COMMA jlong ptr, jint glyphIdx) {
282 const LayoutWrapper* layout = reinterpret_cast<LayoutWrapper*>(ptr);
283 return layout->fontIds[glyphIdx];
284 }
285
286 // CriticalNative
TextShaper_Result_nReleaseFunc(CRITICAL_JNI_PARAMS)287 static jlong TextShaper_Result_nReleaseFunc(CRITICAL_JNI_PARAMS) {
288 return reinterpret_cast<jlong>(releaseLayout);
289 }
290
291 static const JNINativeMethod gMethods[] = {
292 {"nativeShapeTextRun", "("
293 "[C" // text
294 "I" // start
295 "I" // count
296 "I" // contextStart
297 "I" // contextCount
298 "Z" // isRtl
299 "J)" // paint
300 "J", // LayoutPtr
301 (void*) TextShaper_shapeTextRunChars},
302
303 {"nativeShapeTextRun", "("
304 "Ljava/lang/String;" // text
305 "I" // start
306 "I" // count
307 "I" // contextStart
308 "I" // contextCount
309 "Z" // isRtl
310 "J)" // paint
311 "J", // LayoutPtr
312 (void*) TextShaper_shapeTextRunString},
313
314 };
315
316 static const JNINativeMethod gResultMethods[] = {
317 {"nGetGlyphCount", "(J)I", (void*)TextShaper_Result_getGlyphCount},
318 {"nGetTotalAdvance", "(J)F", (void*)TextShaper_Result_getTotalAdvance},
319 {"nGetAscent", "(J)F", (void*)TextShaper_Result_getAscent},
320 {"nGetDescent", "(J)F", (void*)TextShaper_Result_getDescent},
321 {"nGetGlyphId", "(JI)I", (void*)TextShaper_Result_getGlyphId},
322 {"nGetX", "(JI)F", (void*)TextShaper_Result_getX},
323 {"nGetY", "(JI)F", (void*)TextShaper_Result_getY},
324 {"nGetFont", "(JI)J", (void*)TextShaper_Result_getFont},
325 {"nGetFakeBold", "(JI)Z", (void*)TextShaper_Result_getFakeBold},
326 {"nGetFakeItalic", "(JI)Z", (void*)TextShaper_Result_getFakeItalic},
327 {"nGetWeightOverride", "(JI)F", (void*)TextShaper_Result_getWeightOverride},
328 {"nGetItalicOverride", "(JI)F", (void*)TextShaper_Result_getItalicOverride},
329 {"nReleaseFunc", "()J", (void*)TextShaper_Result_nReleaseFunc},
330
331 {"nGetFontCount", "(J)I", (void*)TextShaper_Result_getFontCount},
332 {"nGetFontRef", "(JI)J", (void*)TextShaper_Result_getFontRef},
333 {"nGetFontId", "(JI)I", (void*)TextShaper_Result_getFontId},
334 };
335
register_android_graphics_text_TextShaper(JNIEnv * env)336 int register_android_graphics_text_TextShaper(JNIEnv* env) {
337 return RegisterMethodsOrDie(env, "android/graphics/text/TextRunShaper", gMethods,
338 NELEM(gMethods))
339 + RegisterMethodsOrDie(env, "android/graphics/text/PositionedGlyphs",
340 gResultMethods, NELEM(gResultMethods));
341 }
342
343 }
344
345