• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "SkData.h"
21 #include "SkFontMgr.h"
22 #include "SkRefCnt.h"
23 #include "SkTypeface.h"
24 #include "GraphicsJNI.h"
25 #include <nativehelper/ScopedUtfChars.h>
26 #include "Utils.h"
27 #include "FontUtils.h"
28 
29 #include <hwui/MinikinSkia.h>
30 #include <hwui/Typeface.h>
31 #include <minikin/FontFamily.h>
32 #include <ui/FatVector.h>
33 
34 #include <memory>
35 
36 namespace android {
37 
38 struct NativeFontBuilder {
39     std::vector<minikin::FontVariation> axes;
40 };
41 
toBuilder(jlong ptr)42 static inline NativeFontBuilder* toBuilder(jlong ptr) {
43     return reinterpret_cast<NativeFontBuilder*>(ptr);
44 }
45 
releaseFont(jlong font)46 static void releaseFont(jlong font) {
47     delete reinterpret_cast<FontWrapper*>(font);
48 }
49 
release_global_ref(const void *,void * context)50 static void release_global_ref(const void* /*data*/, void* context) {
51     JNIEnv* env = GraphicsJNI::getJNIEnv();
52     bool needToAttach = (env == nullptr);
53     if (needToAttach) {
54         env = GraphicsJNI::attachJNIEnv("release_font_data");
55         if (env == nullptr) {
56             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
Font_Builder_initBuilder(JNIEnv *,jobject)66 static jlong Font_Builder_initBuilder(JNIEnv*, jobject) {
67     return reinterpret_cast<jlong>(new NativeFontBuilder());
68 }
69 
70 // Critical Native
Font_Builder_addAxis(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr,jint tag,jfloat value)71 static void Font_Builder_addAxis(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jint tag, jfloat value) {
72     toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value);
73 }
74 
75 // Regular JNI
Font_Builder_build(JNIEnv * env,jobject clazz,jlong builderPtr,jobject buffer,jstring filePath,jint weight,jboolean italic,jint ttcIndex)76 static jlong Font_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr, jobject buffer,
77         jstring filePath, jint weight, jboolean italic, jint ttcIndex) {
78     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     }
91     ScopedUtfChars fontPath(env, filePath);
92     jobject fontRef = MakeGlobalRefOrDie(env, buffer);
93     sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize,
94             release_global_ref, reinterpret_cast<void*>(fontRef)));
95 
96     FatVector<SkFontArguments::Axis, 2> skiaAxes;
97     for (const auto& axis : builder->axes) {
98         skiaAxes.emplace_back(SkFontArguments::Axis{axis.axisTag, axis.value});
99     }
100 
101     std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data)));
102 
103     SkFontArguments params;
104     params.setCollectionIndex(ttcIndex);
105     params.setAxes(skiaAxes.data(), skiaAxes.size());
106 
107     sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
108     sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), params));
109     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 =
115             std::make_shared<MinikinFontSkia>(std::move(face), fontPtr, fontSize,
116                                               std::string_view(fontPath.c_str(), fontPath.size()),
117                                               ttcIndex, builder->axes);
118     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
Font_Builder_getReleaseNativeFont(CRITICAL_JNI_PARAMS)124 static jlong Font_Builder_getReleaseNativeFont(CRITICAL_JNI_PARAMS) {
125     return reinterpret_cast<jlong>(releaseFont);
126 }
127 
128 ///////////////////////////////////////////////////////////////////////////////
129 
130 static const JNINativeMethod gFontBuilderMethods[] = {
131     { "nInitBuilder", "()J", (void*) Font_Builder_initBuilder },
132     { "nAddAxis", "(JIF)V", (void*) Font_Builder_addAxis },
133     { "nBuild", "(JLjava/nio/ByteBuffer;Ljava/lang/String;IZI)J", (void*) Font_Builder_build },
134     { "nGetReleaseNativeFont", "()J", (void*) Font_Builder_getReleaseNativeFont },
135 };
136 
register_android_graphics_fonts_Font(JNIEnv * env)137 int register_android_graphics_fonts_Font(JNIEnv* env) {
138     return RegisterMethodsOrDie(env, "android/graphics/fonts/Font$Builder", gFontBuilderMethods,
139             NELEM(gFontBuilderMethods));
140 }
141 
142 }
143