Debian Codegraph @

libs/hwui/jni/fonts/Font.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2018 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 "Minikin"
19
20 #include "Font.h"
21 #include "SkData.h"
22 #include "SkFont.h"
23 #include "SkFontMetrics.h"
24 #include "SkFontMgr.h"
25 #include "SkRefCnt.h"
26 #include "SkTypeface.h"
27 #include "GraphicsJNI.h"
28 #include <nativehelper/ScopedUtfChars.h>
29 #include "Utils.h"
30 #include "FontUtils.h"
31
32 #include <hwui/MinikinSkia.h>
33 #include <hwui/Paint.h>
34 #include <hwui/Typeface.h>
35 #include <minikin/FontFamily.h>
36 #include <minikin/FontFileParser.h>
37 #include <minikin/LocaleList.h>
38 #include <minikin/SystemFonts.h>
39 #include <ui/FatVector.h>
40
41 #include <memory>
42
43 namespace android {
44
45 struct NativeFontBuilder {
46 std::vector<minikin::FontVariation> axes;
47 };
48
49 static inline NativeFontBuilder* toBuilder(jlong ptr) {
50 return reinterpret_cast<NativeFontBuilder*>(ptr);
51 }
52
53 static void releaseFont(jlong font) {
54 delete reinterpret_cast<FontWrapper*>(font);
55 }
56
57 static void release_global_ref(const void* /*data*/, void* context) {
58 JNIEnv* env = GraphicsJNI::getJNIEnv();
59 bool needToAttach = (env == nullptr);
60 if (needToAttach) {
61 env = GraphicsJNI::attachJNIEnv("release_font_data");
62 if (env == nullptr) {
63 ALOGE("failed to attach to thread to release global ref.");
64 return;
65 }
66 }
67
68 jobject obj = reinterpret_cast<jobject>(context);
69 env->DeleteGlobalRef(obj);
70 }
71
72 // Regular JNI
73 static jlong Font_Builder_initBuilder(JNIEnv*, jobject) {
74 return reinterpret_cast<jlong>(new NativeFontBuilder());
75 }
76
77 // Critical Native
78 static void Font_Builder_addAxis(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jint tag, jfloat value) {
79 toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value);
80 }
81
82 // Regular JNI
83 static jlong Font_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr, jobject buffer,
84 jstring filePath, jstring langTags, jint weight, jboolean italic,
85 jint ttcIndex) {
86 NPE_CHECK_RETURN_ZERO(env, buffer);
87 std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr));
88 const void* fontPtr = env->GetDirectBufferAddress(buffer);
89 if (fontPtr == nullptr) {
90 jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer");
91 return 0;
92 }
93 jlong fontSize = env->GetDirectBufferCapacity(buffer);
94 if (fontSize <= 0) {
95 jniThrowException(env, "java/lang/IllegalArgumentException",
96 "buffer size must not be zero or negative");
97 return 0;
98 }
99 ScopedUtfChars fontPath(env, filePath);
100 ScopedUtfChars langTagStr(env, langTags);
101 jobject fontRef = MakeGlobalRefOrDie(env, buffer);
102 sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize,
103 release_global_ref, reinterpret_cast<void*>(fontRef)));
104 std::shared_ptr<minikin::MinikinFont> minikinFont = fonts::createMinikinFontSkia(
105 std::move(data), std::string_view(fontPath.c_str(), fontPath.size()),
106 fontPtr, fontSize, ttcIndex, builder->axes);
107 if (minikinFont == nullptr) {
108 jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException",
109 "Failed to create internal object. maybe invalid font data. filePath %s",
110 fontPath.c_str());
111 return 0;
112 }
113 uint32_t localeListId = minikin::registerLocaleList(langTagStr.c_str());
114 std::shared_ptr<minikin::Font> font =
115 minikin::Font::Builder(minikinFont)
116 .setWeight(weight)
117 .setSlant(static_cast<minikin::FontStyle::Slant>(italic))
118 .setLocaleListId(localeListId)
119 .build();
120 return reinterpret_cast<jlong>(new FontWrapper(std::move(font)));
121 }
122
123 // Fast Native
124 static jlong Font_Builder_clone(JNIEnv* env, jobject clazz, jlong fontPtr, jlong builderPtr,
125 jint weight, jboolean italic, jint ttcIndex) {
126 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
127 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get());
128 std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr));
129
130 // Reconstruct SkTypeface with different arguments from existing SkTypeface.
131 FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation;
132 for (const auto& axis : builder->axes) {
133 skVariation.push_back({axis.axisTag, axis.value});
134 }
135 SkFontArguments args;
136 args.setCollectionIndex(ttcIndex);
137 args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())});
138
139 sk_sp<SkTypeface> newTypeface = minikinSkia->GetSkTypeface()->makeClone(args);
140
141 std::shared_ptr<minikin::MinikinFont> newMinikinFont = std::make_shared<MinikinFontSkia>(
142 std::move(newTypeface), minikinSkia->GetSourceId(), minikinSkia->GetFontData(),
143 minikinSkia->GetFontSize(), minikinSkia->getFilePath(), minikinSkia->GetFontIndex(),
144 builder->axes);
145 std::shared_ptr<minikin::Font> newFont = minikin::Font::Builder(newMinikinFont)
146 .setWeight(weight)
147 .setSlant(static_cast<minikin::FontStyle::Slant>(italic))
148 .build();
149 return reinterpret_cast<jlong>(new FontWrapper(std::move(newFont)));
150 }
151
152 ///////////////////////////////////////////////////////////////////////////////
153 // Font JNI functions
154
155 // Fast Native
156 static jfloat Font_getGlyphBounds(JNIEnv* env, jobject, jlong fontHandle, jint glyphId,
157 jlong paintHandle, jobject rect) {
158 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
159 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get());
160 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
161
162 SkFont* skFont = &paint->getSkFont();
163 // We don't use populateSkFont since it is designed to be used for layout result with addressing
164 // auto fake-bolding.
165 skFont->setTypeface(minikinSkia->RefSkTypeface());
166
167 uint16_t glyph16 = glyphId;
168 SkRect skBounds;
169 SkScalar skWidth;
170 skFont->getWidthsBounds(&glyph16, 1, &skWidth, &skBounds, nullptr);
171 GraphicsJNI::rect_to_jrectf(skBounds, env, rect);
172 return SkScalarToFloat(skWidth);
173 }
174
175 // Fast Native
176 static jfloat Font_getFontMetrics(JNIEnv* env, jobject, jlong fontHandle, jlong paintHandle,
177 jobject metricsObj) {
178 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
179 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get());
180 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
181
182 SkFont* skFont = &paint->getSkFont();
183 // We don't use populateSkFont since it is designed to be used for layout result with addressing
184 // auto fake-bolding.
185 skFont->setTypeface(minikinSkia->RefSkTypeface());
186
187 SkFontMetrics metrics;
188 SkScalar spacing = skFont->getMetrics(&metrics);
189 GraphicsJNI::set_metrics(env, metricsObj, metrics);
190 return spacing;
191 }
192
193 // Critical Native
194 static jlong Font_getMinikinFontPtr(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
195 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
196 return reinterpret_cast<jlong>(font->font.get());
197 }
198
199 // Critical Native
200 static jlong Font_cloneFont(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
201 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
202 std::shared_ptr<minikin::Font> ref = font->font;
203 return reinterpret_cast<jlong>(new FontWrapper(std::move(ref)));
204 }
205
206 // Fast Native
207 static jobject Font_newByteBuffer(JNIEnv* env, jobject, jlong fontPtr) {
208 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
209 const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface();
210 return env->NewDirectByteBuffer(const_cast<void*>(minikinFont->GetFontData()),
211 minikinFont->GetFontSize());
212 }
213
214 // Critical Native
215 static jlong Font_getBufferAddress(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
216 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
217 return reinterpret_cast<jlong>(font->font->typeface()->GetFontData());
218 }
219
220 // Critical Native
221 static jlong Font_getReleaseNativeFontFunc(CRITICAL_JNI_PARAMS) {
222 return reinterpret_cast<jlong>(releaseFont);
223 }
224
225 // Fast Native
226 static jstring Font_getFontPath(JNIEnv* env, jobject, jlong fontPtr) {
227 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
228 minikin::BufferReader reader = font->font->typefaceMetadataReader();
229 if (reader.data() != nullptr) {
230 std::string path = std::string(reader.readString());
231 if (path.empty()) {
232 return nullptr;
233 }
234 return env->NewStringUTF(path.c_str());
235 } else {
236 const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface();
237 const std::string& path = minikinFont->GetFontPath();
238 if (path.empty()) {
239 return nullptr;
240 }
241 return env->NewStringUTF(path.c_str());
242 }
243 }
244
245 // Fast Native
246 static jstring Font_getLocaleList(JNIEnv* env, jobject, jlong fontPtr) {
247 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
248 uint32_t localeListId = font->font->getLocaleListId();
249 if (localeListId == 0) {
250 return nullptr;
251 }
252 std::string langTags = minikin::getLocaleString(localeListId);
253 if (langTags.empty()) {
254 return nullptr;
255 }
256 return env->NewStringUTF(langTags.c_str());
257 }
258
259 // Critical Native
260 static jint Font_getPackedStyle(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
261 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
262 uint32_t weight = font->font->style().weight();
263 uint32_t isItalic = font->font->style().slant() == minikin::FontStyle::Slant::ITALIC ? 1 : 0;
264 return (isItalic << 16) | weight;
265 }
266
267 // Critical Native
268 static jint Font_getIndex(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
269 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
270 minikin::BufferReader reader = font->font->typefaceMetadataReader();
271 if (reader.data() != nullptr) {
272 reader.skipString(); // fontPath
273 return reader.read<int>();
274 } else {
275 const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface();
276 return minikinFont->GetFontIndex();
277 }
278 }
279
280 // Critical Native
281 static jint Font_getAxisCount(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
282 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
283 minikin::BufferReader reader = font->font->typefaceMetadataReader();
284 if (reader.data() != nullptr) {
285 reader.skipString(); // fontPath
286 reader.skip<int>(); // fontIndex
287 return reader.readArray<minikin::FontVariation>().second;
288 } else {
289 const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface();
290 return minikinFont->GetAxes().size();
291 }
292 }
293
294 // Critical Native
295 static jlong Font_getAxisInfo(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr, jint index) {
296 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
297 minikin::BufferReader reader = font->font->typefaceMetadataReader();
298 minikin::FontVariation var;
299 if (reader.data() != nullptr) {
300 reader.skipString(); // fontPath
301 reader.skip<int>(); // fontIndex
302 var = reader.readArray<minikin::FontVariation>().first[index];
303 } else {
304 const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface();
305 var = minikinFont->GetAxes().at(index);
306 }
307 uint32_t floatBinary = *reinterpret_cast<const uint32_t*>(&var.value);
308 return (static_cast<uint64_t>(var.axisTag) << 32) | static_cast<uint64_t>(floatBinary);
309 }
310
311 // Critical Native
312 static jint Font_getSourceId(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) {
313 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
314 return font->font->typeface()->GetSourceId();
315 }
316
317 static jlongArray Font_getAvailableFontSet(JNIEnv* env, jobject) {
318 std::vector<jlong> refArray;
319 minikin::SystemFonts::getFontSet(
320 [&refArray](const std::vector<std::shared_ptr<minikin::Font>>& fontSet) {
321 refArray.reserve(fontSet.size());
322 for (const auto& font : fontSet) {
323 std::shared_ptr<minikin::Font> fontRef = font;
324 refArray.push_back(
325 reinterpret_cast<jlong>(new FontWrapper(std::move(fontRef))));
326 }
327 });
328 jlongArray r = env->NewLongArray(refArray.size());
329 env->SetLongArrayRegion(r, 0, refArray.size(), refArray.data());
330 return r;
331 }
332
333 // Fast Native
334 static jlong FontFileUtil_getFontRevision(JNIEnv* env, jobject, jobject buffer, jint index) {
335 NPE_CHECK_RETURN_ZERO(env, buffer);
336 const void* fontPtr = env->GetDirectBufferAddress(buffer);
337 if (fontPtr == nullptr) {
338 jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer");
339 return 0;
340 }
341 jlong fontSize = env->GetDirectBufferCapacity(buffer);
342 if (fontSize <= 0) {
343 jniThrowException(env, "java/lang/IllegalArgumentException",
344 "buffer size must not be zero or negative");
345 return 0;
346 }
347 minikin::FontFileParser parser(fontPtr, fontSize, index);
348 std::optional<uint32_t> revision = parser.getFontRevision();
349 if (!revision.has_value()) {
350 return -1L;
351 }
352 return revision.value();
353 }
354
355 static jstring FontFileUtil_getFontPostScriptName(JNIEnv* env, jobject, jobject buffer,
356 jint index) {
357 NPE_CHECK_RETURN_ZERO(env, buffer);
358 const void* fontPtr = env->GetDirectBufferAddress(buffer);
359 if (fontPtr == nullptr) {
360 jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer");
361 return nullptr;
362 }
363 jlong fontSize = env->GetDirectBufferCapacity(buffer);
364 if (fontSize <= 0) {
365 jniThrowException(env, "java/lang/IllegalArgumentException",
366 "buffer size must not be zero or negative");
367 return nullptr;
368 }
369 minikin::FontFileParser parser(fontPtr, fontSize, index);
370 std::optional<std::string> psName = parser.getPostScriptName();
371 if (!psName.has_value()) {
372 return nullptr; // null
373 }
374 return env->NewStringUTF(psName->c_str());
375 }
376
377 static jint FontFileUtil_isPostScriptType1Font(JNIEnv* env, jobject, jobject buffer, jint index) {
378 NPE_CHECK_RETURN_ZERO(env, buffer);
379 const void* fontPtr = env->GetDirectBufferAddress(buffer);
380 if (fontPtr == nullptr) {
381 jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer");
382 return -1;
383 }
384 jlong fontSize = env->GetDirectBufferCapacity(buffer);
385 if (fontSize <= 0) {
386 jniThrowException(env, "java/lang/IllegalArgumentException",
387 "buffer size must not be zero or negative");
388 return -1;
389 }
390 minikin::FontFileParser parser(fontPtr, fontSize, index);
391 std::optional<bool> isType1 = parser.isPostScriptType1Font();
392 if (!isType1.has_value()) {
393 return -1; // not an OpenType font. HarfBuzz failed to parse it.
394 }
395 return isType1.value();
396 }
397
398 ///////////////////////////////////////////////////////////////////////////////
399
400 static const JNINativeMethod gFontBuilderMethods[] = {
401 {"nInitBuilder", "()J", (void*)Font_Builder_initBuilder},
402 {"nAddAxis", "(JIF)V", (void*)Font_Builder_addAxis},
403 {"nBuild", "(JLjava/nio/ByteBuffer;Ljava/lang/String;Ljava/lang/String;IZI)J",
404 (void*)Font_Builder_build},
405 {"nClone", "(JJIZI)J", (void*)Font_Builder_clone},
406 };
407
408 static const JNINativeMethod gFontMethods[] = {
409 {"nGetMinikinFontPtr", "(J)J", (void*)Font_getMinikinFontPtr},
410 {"nCloneFont", "(J)J", (void*)Font_cloneFont},
411 {"nNewByteBuffer", "(J)Ljava/nio/ByteBuffer;", (void*)Font_newByteBuffer},
412 {"nGetBufferAddress", "(J)J", (void*)Font_getBufferAddress},
413 {"nGetReleaseNativeFont", "()J", (void*)Font_getReleaseNativeFontFunc},
414 {"nGetGlyphBounds", "(JIJLandroid/graphics/RectF;)F", (void*)Font_getGlyphBounds},
415 {"nGetFontMetrics", "(JJLandroid/graphics/Paint$FontMetrics;)F",
416 (void*)Font_getFontMetrics},
417 {"nGetFontPath", "(J)Ljava/lang/String;", (void*)Font_getFontPath},
418 {"nGetLocaleList", "(J)Ljava/lang/String;", (void*)Font_getLocaleList},
419 {"nGetPackedStyle", "(J)I", (void*)Font_getPackedStyle},
420 {"nGetIndex", "(J)I", (void*)Font_getIndex},
421 {"nGetAxisCount", "(J)I", (void*)Font_getAxisCount},
422 {"nGetAxisInfo", "(JI)J", (void*)Font_getAxisInfo},
423 {"nGetSourceId", "(J)I", (void*)Font_getSourceId},
424
425 // System font accessors
426 {"nGetAvailableFontSet", "()[J", (void*)Font_getAvailableFontSet},
427 };
428
429 static const JNINativeMethod gFontFileUtilMethods[] = {
430 { "nGetFontRevision", "(Ljava/nio/ByteBuffer;I)J", (void*) FontFileUtil_getFontRevision },
431 { "nGetFontPostScriptName", "(Ljava/nio/ByteBuffer;I)Ljava/lang/String;",
432 (void*) FontFileUtil_getFontPostScriptName },
433 { "nIsPostScriptType1Font", "(Ljava/nio/ByteBuffer;I)I",
434 (void*) FontFileUtil_isPostScriptType1Font },
435 };
436
437 int register_android_graphics_fonts_Font(JNIEnv* env) {
438 return RegisterMethodsOrDie(env, "android/graphics/fonts/Font$Builder", gFontBuilderMethods,
439 NELEM(gFontBuilderMethods)) +
440 RegisterMethodsOrDie(env, "android/graphics/fonts/Font", gFontMethods,
441 NELEM(gFontMethods)) +
442 RegisterMethodsOrDie(env, "android/graphics/fonts/FontFileUtil", gFontFileUtilMethods,
443 NELEM(gFontFileUtilMethods));
444 }
445
446 namespace fonts {
447
448 std::shared_ptr<minikin::MinikinFont> createMinikinFontSkia(
449 sk_sp<SkData>&& data, std::string_view fontPath, const void *fontPtr, size_t fontSize,
450 int ttcIndex, const std::vector<minikin::FontVariation>& axes) {
451 FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation;
452 for (const auto& axis : axes) {
453 skVariation.push_back({axis.axisTag, axis.value});
454 }
455
456 std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data)));
457
458 SkFontArguments args;
459 args.setCollectionIndex(ttcIndex);
460 args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())});
461
462 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
463 sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args));
464 if (face == nullptr) {
465 return nullptr;
466 }
467 return std::make_shared<MinikinFontSkia>(std::move(face), getNewSourceId(), fontPtr, fontSize,
468 fontPath, ttcIndex, axes);
469 }
470
471 int getNewSourceId() {
472 static std::atomic<int> sSourceId = {0};
473 return sSourceId++;
474 }
475
476 } // namespace fonts
477
478 } // namespace android