Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 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 | |
Derek Sollenberger | 5368eda | 2019-10-25 11:20:03 -0400 | [diff] [blame] | 17 | #undef LOG_TAG |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 18 | #define LOG_TAG "Minikin" |
| 19 | |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 20 | #include "Font.h" |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 21 | #include "SkData.h" |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 22 | #include "SkFont.h" |
| 23 | #include "SkFontMetrics.h" |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 24 | #include "SkFontMgr.h" |
| 25 | #include "SkRefCnt.h" |
| 26 | #include "SkTypeface.h" |
| 27 | #include "GraphicsJNI.h" |
| 28 | #include <nativehelper/ScopedUtfChars.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 29 | #include "Utils.h" |
| 30 | #include "FontUtils.h" |
| 31 | |
| 32 | #include <hwui/MinikinSkia.h> |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 33 | #include <hwui/Paint.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 34 | #include <hwui/Typeface.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 35 | #include <minikin/FontFamily.h> |
Jagadeesh Pakaravoor | b624af3 | 2020-05-01 00:01:40 +0000 | [diff] [blame] | 36 | #include <ui/FatVector.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 37 | |
| 38 | #include <memory> |
| 39 | |
| 40 | namespace android { |
| 41 | |
| 42 | struct NativeFontBuilder { |
| 43 | std::vector<minikin::FontVariation> axes; |
| 44 | }; |
| 45 | |
| 46 | static inline NativeFontBuilder* toBuilder(jlong ptr) { |
| 47 | return reinterpret_cast<NativeFontBuilder*>(ptr); |
| 48 | } |
| 49 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 50 | static void releaseFont(jlong font) { |
| 51 | delete reinterpret_cast<FontWrapper*>(font); |
| 52 | } |
| 53 | |
| 54 | static void release_global_ref(const void* /*data*/, void* context) { |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 55 | JNIEnv* env = GraphicsJNI::getJNIEnv(); |
| 56 | bool needToAttach = (env == nullptr); |
| 57 | if (needToAttach) { |
| 58 | env = GraphicsJNI::attachJNIEnv("release_font_data"); |
| 59 | if (env == nullptr) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 60 | ALOGE("failed to attach to thread to release global ref."); |
| 61 | return; |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | jobject obj = reinterpret_cast<jobject>(context); |
| 66 | env->DeleteGlobalRef(obj); |
| 67 | } |
| 68 | |
| 69 | // Regular JNI |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 70 | static jlong Font_Builder_initBuilder(JNIEnv*, jobject) { |
| 71 | return reinterpret_cast<jlong>(new NativeFontBuilder()); |
| 72 | } |
| 73 | |
| 74 | // Critical Native |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 75 | static void Font_Builder_addAxis(CRITICAL_JNI_PARAMS_COMMA jlong builderPtr, jint tag, jfloat value) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 76 | toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value); |
| 77 | } |
| 78 | |
| 79 | // Regular JNI |
| 80 | static jlong Font_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr, jobject buffer, |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 81 | jstring filePath, jint weight, jboolean italic, jint ttcIndex) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 82 | NPE_CHECK_RETURN_ZERO(env, buffer); |
| 83 | std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr)); |
| 84 | const void* fontPtr = env->GetDirectBufferAddress(buffer); |
| 85 | if (fontPtr == nullptr) { |
| 86 | jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer"); |
| 87 | return 0; |
| 88 | } |
| 89 | jlong fontSize = env->GetDirectBufferCapacity(buffer); |
| 90 | if (fontSize <= 0) { |
| 91 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 92 | "buffer size must not be zero or negative"); |
| 93 | return 0; |
| 94 | } |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 95 | ScopedUtfChars fontPath(env, filePath); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 96 | jobject fontRef = MakeGlobalRefOrDie(env, buffer); |
| 97 | sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize, |
| 98 | release_global_ref, reinterpret_cast<void*>(fontRef))); |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 99 | std::shared_ptr<minikin::MinikinFont> minikinFont = fonts::createMinikinFontSkia( |
| 100 | std::move(data), std::string_view(fontPath.c_str(), fontPath.size()), |
| 101 | fontPtr, fontSize, ttcIndex, builder->axes); |
| 102 | if (minikinFont == nullptr) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 103 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 104 | "Failed to create internal object. maybe invalid font data."); |
| 105 | return 0; |
| 106 | } |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 107 | std::shared_ptr<minikin::Font> font = minikin::Font::Builder(minikinFont).setWeight(weight) |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 108 | .setSlant(static_cast<minikin::FontStyle::Slant>(italic)).build(); |
| 109 | return reinterpret_cast<jlong>(new FontWrapper(std::move(font))); |
| 110 | } |
| 111 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 112 | // Fast Native |
| 113 | static jlong Font_Builder_clone(JNIEnv* env, jobject clazz, jlong fontPtr, jlong builderPtr, |
| 114 | jint weight, jboolean italic, jint ttcIndex) { |
| 115 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 116 | MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get()); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 117 | std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr)); |
| 118 | |
| 119 | // Reconstruct SkTypeface with different arguments from existing SkTypeface. |
| 120 | FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation; |
| 121 | for (const auto& axis : builder->axes) { |
| 122 | skVariation.push_back({axis.axisTag, axis.value}); |
| 123 | } |
| 124 | SkFontArguments args; |
| 125 | args.setCollectionIndex(ttcIndex); |
| 126 | args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())}); |
| 127 | |
| 128 | sk_sp<SkTypeface> newTypeface = minikinSkia->GetSkTypeface()->makeClone(args); |
| 129 | |
| 130 | std::shared_ptr<minikin::MinikinFont> newMinikinFont = std::make_shared<MinikinFontSkia>( |
| 131 | std::move(newTypeface), |
| 132 | minikinSkia->GetFontData(), |
| 133 | minikinSkia->GetFontSize(), |
| 134 | minikinSkia->getFilePath(), |
| 135 | minikinSkia->GetFontIndex(), |
| 136 | builder->axes); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 137 | std::shared_ptr<minikin::Font> newFont = minikin::Font::Builder(newMinikinFont) |
| 138 | .setWeight(weight) |
| 139 | .setSlant(static_cast<minikin::FontStyle::Slant>(italic)) |
| 140 | .build(); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 141 | return reinterpret_cast<jlong>(new FontWrapper(std::move(newFont))); |
| 142 | } |
| 143 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 144 | // Critical Native |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 145 | static jlong Font_Builder_getReleaseNativeFont(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 146 | return reinterpret_cast<jlong>(releaseFont); |
| 147 | } |
| 148 | |
| 149 | /////////////////////////////////////////////////////////////////////////////// |
| 150 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 151 | // Fast Native |
| 152 | static jfloat Font_getGlyphBounds(JNIEnv* env, jobject, jlong fontHandle, jint glyphId, |
| 153 | jlong paintHandle, jobject rect) { |
| 154 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 155 | MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get()); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 156 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 157 | |
| 158 | SkFont* skFont = &paint->getSkFont(); |
| 159 | // We don't use populateSkFont since it is designed to be used for layout result with addressing |
| 160 | // auto fake-bolding. |
| 161 | skFont->setTypeface(minikinSkia->RefSkTypeface()); |
| 162 | |
| 163 | uint16_t glyph16 = glyphId; |
| 164 | SkRect skBounds; |
| 165 | SkScalar skWidth; |
| 166 | skFont->getWidthsBounds(&glyph16, 1, &skWidth, &skBounds, nullptr); |
| 167 | GraphicsJNI::rect_to_jrectf(skBounds, env, rect); |
| 168 | return SkScalarToFloat(skWidth); |
| 169 | } |
| 170 | |
| 171 | // Fast Native |
| 172 | static jfloat Font_getFontMetrics(JNIEnv* env, jobject, jlong fontHandle, jlong paintHandle, |
| 173 | jobject metricsObj) { |
| 174 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 175 | MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get()); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 176 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 177 | |
| 178 | SkFont* skFont = &paint->getSkFont(); |
| 179 | // We don't use populateSkFont since it is designed to be used for layout result with addressing |
| 180 | // auto fake-bolding. |
| 181 | skFont->setTypeface(minikinSkia->RefSkTypeface()); |
| 182 | |
| 183 | SkFontMetrics metrics; |
| 184 | SkScalar spacing = skFont->getMetrics(&metrics); |
| 185 | GraphicsJNI::set_metrics(env, metricsObj, metrics); |
| 186 | return spacing; |
| 187 | } |
| 188 | |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 189 | // Critical Native |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 190 | static jlong Font_getNativeFontPtr(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle) { |
| 191 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle); |
| 192 | return reinterpret_cast<jlong>(font->font.get()); |
| 193 | } |
| 194 | |
Seigo Nonaka | 31bf860 | 2020-10-14 15:04:01 -0700 | [diff] [blame] | 195 | // Critical Native |
Seigo Nonaka | bb68403 | 2020-11-03 21:47:25 -0800 | [diff] [blame] | 196 | static jlong Font_GetBufferAddress(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle) { |
| 197 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle); |
| 198 | const void* bufferPtr = font->font->typeface()->GetFontData(); |
| 199 | return reinterpret_cast<jlong>(bufferPtr); |
Seigo Nonaka | 31bf860 | 2020-10-14 15:04:01 -0700 | [diff] [blame] | 200 | } |
| 201 | |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 202 | /////////////////////////////////////////////////////////////////////////////// |
| 203 | |
| 204 | struct FontBufferWrapper { |
| 205 | FontBufferWrapper(const std::shared_ptr<minikin::MinikinFont>& font) : minikinFont(font) {} |
| 206 | // MinikinFont holds a shared pointer of SkTypeface which has reference to font data. |
| 207 | std::shared_ptr<minikin::MinikinFont> minikinFont; |
| 208 | }; |
| 209 | |
| 210 | static void unrefBuffer(jlong nativePtr) { |
| 211 | FontBufferWrapper* wrapper = reinterpret_cast<FontBufferWrapper*>(nativePtr); |
| 212 | delete wrapper; |
| 213 | } |
| 214 | |
| 215 | // Critical Native |
| 216 | static jlong FontBufferHelper_refFontBuffer(CRITICAL_JNI_PARAMS_COMMA jlong fontHandle) { |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 217 | const minikin::Font* font = reinterpret_cast<minikin::Font*>(fontHandle); |
| 218 | return reinterpret_cast<jlong>(new FontBufferWrapper(font->typeface())); |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 219 | } |
| 220 | |
| 221 | // Fast Native |
| 222 | static jobject FontBufferHelper_wrapByteBuffer(JNIEnv* env, jobject, jlong nativePtr) { |
| 223 | FontBufferWrapper* wrapper = reinterpret_cast<FontBufferWrapper*>(nativePtr); |
| 224 | return env->NewDirectByteBuffer( |
| 225 | const_cast<void*>(wrapper->minikinFont->GetFontData()), |
| 226 | wrapper->minikinFont->GetFontSize()); |
| 227 | } |
| 228 | |
| 229 | // Critical Native |
| 230 | static jlong FontBufferHelper_getReleaseFunc(CRITICAL_JNI_PARAMS) { |
| 231 | return reinterpret_cast<jlong>(unrefBuffer); |
| 232 | } |
| 233 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 234 | /////////////////////////////////////////////////////////////////////////////// |
| 235 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 236 | static const JNINativeMethod gFontBuilderMethods[] = { |
| 237 | { "nInitBuilder", "()J", (void*) Font_Builder_initBuilder }, |
| 238 | { "nAddAxis", "(JIF)V", (void*) Font_Builder_addAxis }, |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 239 | { "nBuild", "(JLjava/nio/ByteBuffer;Ljava/lang/String;IZI)J", (void*) Font_Builder_build }, |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 240 | { "nClone", "(JJIZI)J", (void*) Font_Builder_clone }, |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 241 | { "nGetReleaseNativeFont", "()J", (void*) Font_Builder_getReleaseNativeFont }, |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 242 | }; |
| 243 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 244 | static const JNINativeMethod gFontMethods[] = { |
| 245 | { "nGetGlyphBounds", "(JIJLandroid/graphics/RectF;)F", (void*) Font_getGlyphBounds }, |
| 246 | { "nGetFontMetrics", "(JJLandroid/graphics/Paint$FontMetrics;)F", (void*) Font_getFontMetrics }, |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 247 | { "nGetNativeFontPtr", "(J)J", (void*) Font_getNativeFontPtr }, |
Seigo Nonaka | bb68403 | 2020-11-03 21:47:25 -0800 | [diff] [blame] | 248 | { "nGetFontBufferAddress", "(J)J", (void*) Font_GetBufferAddress }, |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 249 | }; |
| 250 | |
| 251 | static const JNINativeMethod gFontBufferHelperMethods[] = { |
| 252 | { "nRefFontBuffer", "(J)J", (void*) FontBufferHelper_refFontBuffer }, |
| 253 | { "nWrapByteBuffer", "(J)Ljava/nio/ByteBuffer;", (void*) FontBufferHelper_wrapByteBuffer }, |
| 254 | { "nGetReleaseFunc", "()J", (void*) FontBufferHelper_getReleaseFunc }, |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 255 | }; |
| 256 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 257 | int register_android_graphics_fonts_Font(JNIEnv* env) { |
| 258 | return RegisterMethodsOrDie(env, "android/graphics/fonts/Font$Builder", gFontBuilderMethods, |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 259 | NELEM(gFontBuilderMethods)) + |
| 260 | RegisterMethodsOrDie(env, "android/graphics/fonts/Font", gFontMethods, |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 261 | NELEM(gFontMethods)) + |
| 262 | RegisterMethodsOrDie(env, "android/graphics/fonts/NativeFontBufferHelper", |
| 263 | gFontBufferHelperMethods, NELEM(gFontBufferHelperMethods)); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 264 | } |
| 265 | |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 266 | namespace fonts { |
| 267 | |
| 268 | std::shared_ptr<minikin::MinikinFont> createMinikinFontSkia( |
| 269 | sk_sp<SkData>&& data, std::string_view fontPath, const void *fontPtr, size_t fontSize, |
| 270 | int ttcIndex, const std::vector<minikin::FontVariation>& axes) { |
| 271 | FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation; |
| 272 | for (const auto& axis : axes) { |
| 273 | skVariation.push_back({axis.axisTag, axis.value}); |
| 274 | } |
| 275 | |
| 276 | std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data))); |
| 277 | |
| 278 | SkFontArguments args; |
| 279 | args.setCollectionIndex(ttcIndex); |
| 280 | args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())}); |
| 281 | |
| 282 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 283 | sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args)); |
| 284 | if (face == nullptr) { |
| 285 | return nullptr; |
| 286 | } |
| 287 | return std::make_shared<MinikinFontSkia>(std::move(face), fontPtr, fontSize, |
| 288 | fontPath, ttcIndex, axes); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 289 | } |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 290 | |
| 291 | } // namespace fonts |
| 292 | |
| 293 | } // namespace android |