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 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 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> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 26 | #include "Utils.h" |
| 27 | #include "FontUtils.h" |
| 28 | |
| 29 | #include <hwui/MinikinSkia.h> |
| 30 | #include <hwui/Typeface.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 31 | #include <minikin/FontFamily.h> |
Jagadeesh Pakaravoor | b624af3 | 2020-05-01 00:01:40 +0000 | [diff] [blame] | 32 | #include <ui/FatVector.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 33 | |
| 34 | #include <memory> |
| 35 | |
| 36 | namespace android { |
| 37 | |
| 38 | struct NativeFontBuilder { |
| 39 | std::vector<minikin::FontVariation> axes; |
| 40 | }; |
| 41 | |
| 42 | static inline NativeFontBuilder* toBuilder(jlong ptr) { |
| 43 | return reinterpret_cast<NativeFontBuilder*>(ptr); |
| 44 | } |
| 45 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 46 | static void releaseFont(jlong font) { |
| 47 | delete reinterpret_cast<FontWrapper*>(font); |
| 48 | } |
| 49 | |
| 50 | static void release_global_ref(const void* /*data*/, void* context) { |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 51 | JNIEnv* env = GraphicsJNI::getJNIEnv(); |
| 52 | bool needToAttach = (env == nullptr); |
| 53 | if (needToAttach) { |
| 54 | env = GraphicsJNI::attachJNIEnv("release_font_data"); |
| 55 | if (env == nullptr) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 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 |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 66 | static jlong Font_Builder_initBuilder(JNIEnv*, jobject) { |
| 67 | return reinterpret_cast<jlong>(new NativeFontBuilder()); |
| 68 | } |
| 69 | |
| 70 | // Critical Native |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 71 | 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] | 72 | toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value); |
| 73 | } |
| 74 | |
| 75 | // Regular JNI |
| 76 | 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] | 77 | jstring filePath, jint weight, jboolean italic, jint ttcIndex) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 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 | } |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 91 | ScopedUtfChars fontPath(env, filePath); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 92 | jobject fontRef = MakeGlobalRefOrDie(env, buffer); |
| 93 | sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize, |
| 94 | release_global_ref, reinterpret_cast<void*>(fontRef))); |
| 95 | |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 96 | FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation; |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 97 | for (const auto& axis : builder->axes) { |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 98 | skVariation.push_back({axis.axisTag, axis.value}); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 99 | } |
| 100 | |
| 101 | std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data))); |
| 102 | |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 103 | SkFontArguments args; |
| 104 | args.setCollectionIndex(ttcIndex); |
| 105 | args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())}); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 106 | |
| 107 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 108 | sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args)); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 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 = |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 115 | std::make_shared<MinikinFontSkia>(std::move(face), fontPtr, fontSize, |
| 116 | std::string_view(fontPath.c_str(), fontPath.size()), |
| 117 | ttcIndex, builder->axes); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 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 |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 124 | static jlong Font_Builder_getReleaseNativeFont(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 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 }, |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 133 | { "nBuild", "(JLjava/nio/ByteBuffer;Ljava/lang/String;IZI)J", (void*) Font_Builder_build }, |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 134 | { "nGetReleaseNativeFont", "()J", (void*) Font_Builder_getReleaseNativeFont }, |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 135 | }; |
| 136 | |
| 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 | } |