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" |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame^] | 21 | #include "SkFont.h" |
| 22 | #include "SkFontMetrics.h" |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 23 | #include "SkFontMgr.h" |
| 24 | #include "SkRefCnt.h" |
| 25 | #include "SkTypeface.h" |
| 26 | #include "GraphicsJNI.h" |
| 27 | #include <nativehelper/ScopedUtfChars.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 28 | #include "Utils.h" |
| 29 | #include "FontUtils.h" |
| 30 | |
| 31 | #include <hwui/MinikinSkia.h> |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame^] | 32 | #include <hwui/Paint.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 33 | #include <hwui/Typeface.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 34 | #include <minikin/FontFamily.h> |
Jagadeesh Pakaravoor | b624af3 | 2020-05-01 00:01:40 +0000 | [diff] [blame] | 35 | #include <ui/FatVector.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 36 | |
| 37 | #include <memory> |
| 38 | |
| 39 | namespace android { |
| 40 | |
| 41 | struct NativeFontBuilder { |
| 42 | std::vector<minikin::FontVariation> axes; |
| 43 | }; |
| 44 | |
| 45 | static inline NativeFontBuilder* toBuilder(jlong ptr) { |
| 46 | return reinterpret_cast<NativeFontBuilder*>(ptr); |
| 47 | } |
| 48 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 49 | static void releaseFont(jlong font) { |
| 50 | delete reinterpret_cast<FontWrapper*>(font); |
| 51 | } |
| 52 | |
| 53 | static void release_global_ref(const void* /*data*/, void* context) { |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 54 | JNIEnv* env = GraphicsJNI::getJNIEnv(); |
| 55 | bool needToAttach = (env == nullptr); |
| 56 | if (needToAttach) { |
| 57 | env = GraphicsJNI::attachJNIEnv("release_font_data"); |
| 58 | if (env == nullptr) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 59 | 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 Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 69 | static jlong Font_Builder_initBuilder(JNIEnv*, jobject) { |
| 70 | return reinterpret_cast<jlong>(new NativeFontBuilder()); |
| 71 | } |
| 72 | |
| 73 | // Critical Native |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 74 | 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] | 75 | toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value); |
| 76 | } |
| 77 | |
| 78 | // Regular JNI |
| 79 | 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] | 80 | jstring filePath, jint weight, jboolean italic, jint ttcIndex) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 81 | 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 Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 94 | ScopedUtfChars fontPath(env, filePath); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 95 | jobject fontRef = MakeGlobalRefOrDie(env, buffer); |
| 96 | sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize, |
| 97 | release_global_ref, reinterpret_cast<void*>(fontRef))); |
| 98 | |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 99 | FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation; |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 100 | for (const auto& axis : builder->axes) { |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 101 | skVariation.push_back({axis.axisTag, axis.value}); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data))); |
| 105 | |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 106 | SkFontArguments args; |
| 107 | args.setCollectionIndex(ttcIndex); |
| 108 | args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())}); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 109 | |
| 110 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
Ben Wagner | 78d58fd | 2020-07-14 16:52:13 +0000 | [diff] [blame] | 111 | sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args)); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 112 | 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 Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 118 | std::make_shared<MinikinFontSkia>(std::move(face), fontPtr, fontSize, |
| 119 | std::string_view(fontPath.c_str(), fontPath.size()), |
| 120 | ttcIndex, builder->axes); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 121 | 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 Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame^] | 126 | // Fast Native |
| 127 | static 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 Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 156 | // Critical Native |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 157 | static jlong Font_Builder_getReleaseNativeFont(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 158 | return reinterpret_cast<jlong>(releaseFont); |
| 159 | } |
| 160 | |
| 161 | /////////////////////////////////////////////////////////////////////////////// |
| 162 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame^] | 163 | // Fast Native |
| 164 | static 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 |
| 184 | static 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 | |
| 201 | /////////////////////////////////////////////////////////////////////////////// |
| 202 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 203 | static const JNINativeMethod gFontBuilderMethods[] = { |
| 204 | { "nInitBuilder", "()J", (void*) Font_Builder_initBuilder }, |
| 205 | { "nAddAxis", "(JIF)V", (void*) Font_Builder_addAxis }, |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 206 | { "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^] | 207 | { "nClone", "(JJIZI)J", (void*) Font_Builder_clone }, |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 208 | { "nGetReleaseNativeFont", "()J", (void*) Font_Builder_getReleaseNativeFont }, |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 209 | }; |
| 210 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame^] | 211 | static const JNINativeMethod gFontMethods[] = { |
| 212 | { "nGetGlyphBounds", "(JIJLandroid/graphics/RectF;)F", (void*) Font_getGlyphBounds }, |
| 213 | { "nGetFontMetrics", "(JJLandroid/graphics/Paint$FontMetrics;)F", (void*) Font_getFontMetrics }, |
| 214 | }; |
| 215 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 216 | int register_android_graphics_fonts_Font(JNIEnv* env) { |
| 217 | return RegisterMethodsOrDie(env, "android/graphics/fonts/Font$Builder", gFontBuilderMethods, |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame^] | 218 | NELEM(gFontBuilderMethods)) + |
| 219 | RegisterMethodsOrDie(env, "android/graphics/fonts/Font", gFontMethods, |
| 220 | NELEM(gFontMethods)); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | } |