blob: 0eb409557718988fb2abaef43698f8f1e75266cf [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"
Seigo Nonaka1ed4f642020-09-10 17:19:34 -070021#include "SkFont.h"
22#include "SkFontMetrics.h"
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070023#include "SkFontMgr.h"
24#include "SkRefCnt.h"
25#include "SkTypeface.h"
26#include "GraphicsJNI.h"
27#include <nativehelper/ScopedUtfChars.h>
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070028#include "Utils.h"
29#include "FontUtils.h"
30
31#include <hwui/MinikinSkia.h>
Seigo Nonaka1ed4f642020-09-10 17:19:34 -070032#include <hwui/Paint.h>
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070033#include <hwui/Typeface.h>
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070034#include <minikin/FontFamily.h>
Jagadeesh Pakaravoorb624af32020-05-01 00:01:40 +000035#include <ui/FatVector.h>
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070036
37#include <memory>
38
39namespace android {
40
41struct NativeFontBuilder {
42 std::vector<minikin::FontVariation> axes;
43};
44
45static inline NativeFontBuilder* toBuilder(jlong ptr) {
46 return reinterpret_cast<NativeFontBuilder*>(ptr);
47}
48
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070049static void releaseFont(jlong font) {
50 delete reinterpret_cast<FontWrapper*>(font);
51}
52
53static void release_global_ref(const void* /*data*/, void* context) {
Derek Sollenbergerc5882c42019-10-25 11:11:32 -040054 JNIEnv* env = GraphicsJNI::getJNIEnv();
55 bool needToAttach = (env == nullptr);
56 if (needToAttach) {
57 env = GraphicsJNI::attachJNIEnv("release_font_data");
58 if (env == nullptr) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070059 ALOGE("failed to attach to thread to release global ref.");
60 return;
61 }
62 }
63
64 jobject obj = reinterpret_cast<jobject>(context);
65 env->DeleteGlobalRef(obj);
66}
67
68// Regular JNI
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070069static jlong Font_Builder_initBuilder(JNIEnv*, jobject) {
70 return reinterpret_cast<jlong>(new NativeFontBuilder());
71}
72
73// Critical Native
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +010074static void Font_Builder_addAxis(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jint tag, jfloat value) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070075 toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value);
76}
77
78// Regular JNI
79static jlong Font_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr, jobject buffer,
Seigo Nonaka54c6a272018-10-25 15:44:32 -070080 jstring filePath, jint weight, jboolean italic, jint ttcIndex) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070081 NPE_CHECK_RETURN_ZERO(env, buffer);
82 std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr));
83 const void* fontPtr = env->GetDirectBufferAddress(buffer);
84 if (fontPtr == nullptr) {
85 jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer");
86 return 0;
87 }
88 jlong fontSize = env->GetDirectBufferCapacity(buffer);
89 if (fontSize <= 0) {
90 jniThrowException(env, "java/lang/IllegalArgumentException",
91 "buffer size must not be zero or negative");
92 return 0;
93 }
Seigo Nonaka54c6a272018-10-25 15:44:32 -070094 ScopedUtfChars fontPath(env, filePath);
Seigo Nonakaa1c21c02018-07-20 15:57:39 -070095 jobject fontRef = MakeGlobalRefOrDie(env, buffer);
96 sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize,
97 release_global_ref, reinterpret_cast<void*>(fontRef)));
98
Ben Wagner78d58fd2020-07-14 16:52:13 +000099 FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation;
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700100 for (const auto& axis : builder->axes) {
Ben Wagner78d58fd2020-07-14 16:52:13 +0000101 skVariation.push_back({axis.axisTag, axis.value});
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700102 }
103
104 std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data)));
105
Ben Wagner78d58fd2020-07-14 16:52:13 +0000106 SkFontArguments args;
107 args.setCollectionIndex(ttcIndex);
108 args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())});
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700109
110 sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault());
Ben Wagner78d58fd2020-07-14 16:52:13 +0000111 sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args));
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700112 if (face == nullptr) {
113 jniThrowException(env, "java/lang/IllegalArgumentException",
114 "Failed to create internal object. maybe invalid font data.");
115 return 0;
116 }
117 std::shared_ptr<minikin::MinikinFont> minikinFont =
Seigo Nonaka54c6a272018-10-25 15:44:32 -0700118 std::make_shared<MinikinFontSkia>(std::move(face), fontPtr, fontSize,
119 std::string_view(fontPath.c_str(), fontPath.size()),
120 ttcIndex, builder->axes);
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700121 minikin::Font font = minikin::Font::Builder(minikinFont).setWeight(weight)
122 .setSlant(static_cast<minikin::FontStyle::Slant>(italic)).build();
123 return reinterpret_cast<jlong>(new FontWrapper(std::move(font)));
124}
125
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700126// Fast Native
127static jlong Font_Builder_clone(JNIEnv* env, jobject clazz, jlong fontPtr, jlong builderPtr,
128 jint weight, jboolean italic, jint ttcIndex) {
129 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr);
130 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font.typeface().get());
131 std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr));
132
133 // Reconstruct SkTypeface with different arguments from existing SkTypeface.
134 FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation;
135 for (const auto& axis : builder->axes) {
136 skVariation.push_back({axis.axisTag, axis.value});
137 }
138 SkFontArguments args;
139 args.setCollectionIndex(ttcIndex);
140 args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())});
141
142 sk_sp<SkTypeface> newTypeface = minikinSkia->GetSkTypeface()->makeClone(args);
143
144 std::shared_ptr<minikin::MinikinFont> newMinikinFont = std::make_shared<MinikinFontSkia>(
145 std::move(newTypeface),
146 minikinSkia->GetFontData(),
147 minikinSkia->GetFontSize(),
148 minikinSkia->getFilePath(),
149 minikinSkia->GetFontIndex(),
150 builder->axes);
151 minikin::Font newFont = minikin::Font::Builder(newMinikinFont).setWeight(weight)
152 .setSlant(static_cast<minikin::FontStyle::Slant>(italic)).build();
153 return reinterpret_cast<jlong>(new FontWrapper(std::move(newFont)));
154}
155
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700156// Critical Native
Jerome Gaillard21e7e2d2019-05-14 14:34:46 +0100157static jlong Font_Builder_getReleaseNativeFont(CRITICAL_JNI_PARAMS) {
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700158 return reinterpret_cast<jlong>(releaseFont);
159}
160
161///////////////////////////////////////////////////////////////////////////////
162
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700163// Fast Native
164static jfloat Font_getGlyphBounds(JNIEnv* env, jobject, jlong fontHandle, jint glyphId,
165 jlong paintHandle, jobject rect) {
166 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
167 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font.typeface().get());
168 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
169
170 SkFont* skFont = &paint->getSkFont();
171 // We don't use populateSkFont since it is designed to be used for layout result with addressing
172 // auto fake-bolding.
173 skFont->setTypeface(minikinSkia->RefSkTypeface());
174
175 uint16_t glyph16 = glyphId;
176 SkRect skBounds;
177 SkScalar skWidth;
178 skFont->getWidthsBounds(&glyph16, 1, &skWidth, &skBounds, nullptr);
179 GraphicsJNI::rect_to_jrectf(skBounds, env, rect);
180 return SkScalarToFloat(skWidth);
181}
182
183// Fast Native
184static jfloat Font_getFontMetrics(JNIEnv* env, jobject, jlong fontHandle, jlong paintHandle,
185 jobject metricsObj) {
186 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
187 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font.typeface().get());
188 Paint* paint = reinterpret_cast<Paint*>(paintHandle);
189
190 SkFont* skFont = &paint->getSkFont();
191 // We don't use populateSkFont since it is designed to be used for layout result with addressing
192 // auto fake-bolding.
193 skFont->setTypeface(minikinSkia->RefSkTypeface());
194
195 SkFontMetrics metrics;
196 SkScalar spacing = skFont->getMetrics(&metrics);
197 GraphicsJNI::set_metrics(env, metricsObj, metrics);
198 return spacing;
199}
200
Seigo Nonakaf3a19152020-09-14 15:29:42 -0700201// Critical Native
202static jlong Font_getFontInfo(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle) {
203 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
204 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font.typeface().get());
205
206 uint64_t result = font->font.style().weight();
207 result |= font->font.style().slant() == minikin::FontStyle::Slant::ITALIC ? 0x10000 : 0x00000;
208 result |= ((static_cast<uint64_t>(minikinSkia->GetFontIndex())) << 32);
209 result |= ((static_cast<uint64_t>(minikinSkia->GetAxes().size())) << 48);
210 return result;
211}
212
213// Critical Native
214static jlong Font_getAxisInfo(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle, jint index) {
215 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
216 MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font.typeface().get());
217 const minikin::FontVariation& var = minikinSkia->GetAxes().at(index);
218 uint32_t floatBinary = *reinterpret_cast<const uint32_t*>(&var.value);
219 return (static_cast<uint64_t>(var.axisTag) << 32) | static_cast<uint64_t>(floatBinary);
220}
221
222///////////////////////////////////////////////////////////////////////////////
223
224struct FontBufferWrapper {
225 FontBufferWrapper(const std::shared_ptr<minikin::MinikinFont>& font) : minikinFont(font) {}
226 // MinikinFont holds a shared pointer of SkTypeface which has reference to font data.
227 std::shared_ptr<minikin::MinikinFont> minikinFont;
228};
229
230static void unrefBuffer(jlong nativePtr) {
231 FontBufferWrapper* wrapper = reinterpret_cast<FontBufferWrapper*>(nativePtr);
232 delete wrapper;
233}
234
235// Critical Native
236static jlong FontBufferHelper_refFontBuffer(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle) {
237 FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle);
238 return reinterpret_cast<jlong>(new FontBufferWrapper(font->font.typeface()));
239}
240
241// Fast Native
242static jobject FontBufferHelper_wrapByteBuffer(JNIEnv* env, jobject, jlong nativePtr) {
243 FontBufferWrapper* wrapper = reinterpret_cast<FontBufferWrapper*>(nativePtr);
244 return env->NewDirectByteBuffer(
245 const_cast<void*>(wrapper->minikinFont->GetFontData()),
246 wrapper->minikinFont->GetFontSize());
247}
248
249// Critical Native
250static jlong FontBufferHelper_getReleaseFunc(CRITICAL_JNI_PARAMS) {
251 return reinterpret_cast<jlong>(unrefBuffer);
252}
253
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700254///////////////////////////////////////////////////////////////////////////////
255
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700256static const JNINativeMethod gFontBuilderMethods[] = {
257 { "nInitBuilder", "()J", (void*) Font_Builder_initBuilder },
258 { "nAddAxis", "(JIF)V", (void*) Font_Builder_addAxis },
Seigo Nonaka54c6a272018-10-25 15:44:32 -0700259 { "nBuild", "(JLjava/nio/ByteBuffer;Ljava/lang/String;IZI)J", (void*) Font_Builder_build },
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700260 { "nClone", "(JJIZI)J", (void*) Font_Builder_clone },
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700261 { "nGetReleaseNativeFont", "()J", (void*) Font_Builder_getReleaseNativeFont },
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700262};
263
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700264static const JNINativeMethod gFontMethods[] = {
265 { "nGetGlyphBounds", "(JIJLandroid/graphics/RectF;)F", (void*) Font_getGlyphBounds },
266 { "nGetFontMetrics", "(JJLandroid/graphics/Paint$FontMetrics;)F", (void*) Font_getFontMetrics },
Seigo Nonakaf3a19152020-09-14 15:29:42 -0700267 { "nGetFontInfo", "(J)J", (void*) Font_getFontInfo },
268 { "nGetAxisInfo", "(JI)J", (void*) Font_getAxisInfo },
269};
270
271static const JNINativeMethod gFontBufferHelperMethods[] = {
272 { "nRefFontBuffer", "(J)J", (void*) FontBufferHelper_refFontBuffer },
273 { "nWrapByteBuffer", "(J)Ljava/nio/ByteBuffer;", (void*) FontBufferHelper_wrapByteBuffer },
274 { "nGetReleaseFunc", "()J", (void*) FontBufferHelper_getReleaseFunc },
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700275};
276
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700277int register_android_graphics_fonts_Font(JNIEnv* env) {
278 return RegisterMethodsOrDie(env, "android/graphics/fonts/Font$Builder", gFontBuilderMethods,
Seigo Nonaka1ed4f642020-09-10 17:19:34 -0700279 NELEM(gFontBuilderMethods)) +
280 RegisterMethodsOrDie(env, "android/graphics/fonts/Font", gFontMethods,
Seigo Nonakaf3a19152020-09-14 15:29:42 -0700281 NELEM(gFontMethods)) +
282 RegisterMethodsOrDie(env, "android/graphics/fonts/NativeFontBufferHelper",
283 gFontBufferHelperMethods, NELEM(gFontBufferHelperMethods));
Seigo Nonakaa1c21c02018-07-20 15:57:39 -0700284}
285
286}