blob: 996cdceed8a7ef29bff4630caab429a698ce5b31 [file] [log] [blame]
Seigo Nonakaa1c21c02018-07-20 15:57:39 -07001/*
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
Derek Sollenberger5368eda2019-10-25 11:20:03 -040017#undef LOG_TAG
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070018#define LOG_TAG "Minikin"
19
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070020#include "SkData.h"
21#include "SkFontMgr.h"
22#include "SkRefCnt.h"
23#include "SkTypeface.h"
24#include "GraphicsJNI.h"
25#include <nativehelper/ScopedUtfChars.h>
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070026#include "Utils.h"
27#include "FontUtils.h"
28
29#include <hwui/MinikinSkia.h>
30#include <hwui/Typeface.h>
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070031#include <minikin/FontFamily.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000032#include <ui/FatVector.h>
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070033
34#include <memory>
35
36namespace android {
37
38struct NativeFontBuilder {
39 std::vector<minikin::FontVariation> axes;
40};
41
42static inline NativeFontBuilder* toBuilder(jlong ptr) {
43 return reinterpret_cast<NativeFontBuilder*>(ptr);
44}
45
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070046static void releaseFont(jlong font) {
47 delete reinterpret_cast<FontWrapper*>(font);
48}
49
50static void release_global_ref(const void* /*data*/, void* context) {
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040051 JNIEnv* env = GraphicsJNI::getJNIEnv();
52 bool needToAttach = (env == nullptr);
53 if (needToAttach) {
54 env = GraphicsJNI::attachJNIEnv("release_font_data");
55 if (env == nullptr) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070056 ALOGE("failed to attach to thread to release global ref.");
57 return;
58 }
59 }
60
61 jobject obj = reinterpret_cast<jobject>(context);
62 env->DeleteGlobalRef(obj);
63}
64
65// Regular JNI
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070066static jlong Font_Builder_initBuilder(JNIEnv*, jobject) {
67 return reinterpret_cast<jlong>(new NativeFontBuilder());
68}
69
70// Critical Native
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010071static void Font_Builder_addAxis(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jint tag, jfloat value) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070072 toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value);
73}
74
75// Regular JNI
76static jlong Font_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr, jobject buffer,
Seigo Nonaka54c6a272018-10-25 15:44:32 -070077 jstring filePath, jint weight, jboolean italic, jint ttcIndex) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070078 NPE_CHECK_RETURN_ZERO(env, buffer);
79 std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr));
80 const void* fontPtr = env->GetDirectBufferAddress(buffer);
81 if (fontPtr == nullptr) {
82 jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer");
83 return 0;
84 }
85 jlong fontSize = env->GetDirectBufferCapacity(buffer);
86 if (fontSize <= 0) {
87 jniThrowException(env, "java/lang/IllegalArgumentException",
88 "buffer size must not be zero or negative");
89 return 0;
90 }
Seigo Nonaka54c6a272018-10-25 15:44:32 -070091 ScopedUtfChars fontPath(env, filePath);
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070092 jobject fontRef = MakeGlobalRefOrDie(env, buffer);
93 sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize,
94 release_global_ref, reinterpret_cast<void*>(fontRef)));
95
Ben Wagner78d58fd2020-07-14 16:52:13 +000096 FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation;
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070097 for (const auto& axis : builder->axes) {
Ben Wagner78d58fd2020-07-14 16:52:13 +000098 skVariation.push_back({axis.axisTag, axis.value});
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070099 }
100
101 std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data)));
102
Ben Wagner78d58fd2020-07-14 16:52:13 +0000103 SkFontArguments args;
104 args.setCollectionIndex(ttcIndex);
105 args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())});
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700106
107 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
Ben Wagner78d58fd2020-07-14 16:52:13 +0000108 sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args));
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700109 if (face == nullptr) {
110 jniThrowException(env, "java/lang/IllegalArgumentException",
111 "Failed to create internal object. maybe invalid font data.");
112 return 0;
113 }
114 std::shared_ptr<minikin::MinikinFont> minikinFont =
Seigo Nonaka54c6a272018-10-25 15:44:32 -0700115 std::make_shared<MinikinFontSkia>(std::move(face), fontPtr, fontSize,
116 std::string_view(fontPath.c_str(), fontPath.size()),
117 ttcIndex, builder->axes);
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700118 minikin::Font font = minikin::Font::Builder(minikinFont).setWeight(weight)
119 .setSlant(static_cast<minikin::FontStyle::Slant>(italic)).build();
120 return reinterpret_cast<jlong>(new FontWrapper(std::move(font)));
121}
122
123// Critical Native
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100124static jlong Font_Builder_getReleaseNativeFont(CRITICAL_JNI_PARAMS) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700125 return reinterpret_cast<jlong>(releaseFont);
126}
127
128///////////////////////////////////////////////////////////////////////////////
129
130static const JNINativeMethod gFontBuilderMethods[] = {
131 { "nInitBuilder", "()J", (void*) Font_Builder_initBuilder },
132 { "nAddAxis", "(JIF)V", (void*) Font_Builder_addAxis },
Seigo Nonaka54c6a272018-10-25 15:44:32 -0700133 { "nBuild", "(JLjava/nio/ByteBuffer;Ljava/lang/String;IZI)J", (void*) Font_Builder_build },
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700134 { "nGetReleaseNativeFont", "()J", (void*) Font_Builder_getReleaseNativeFont },
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700135};
136
137int register_android_graphics_fonts_Font(JNIEnv* env) {
138 return RegisterMethodsOrDie(env, "android/graphics/fonts/Font$Builder", gFontBuilderMethods,
139 NELEM(gFontBuilderMethods));
140}
141
142}