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> |
Seigo Nonaka | 47b6c1b | 2021-01-13 15:02:47 -0800 | [diff] [blame] | 36 | #include <minikin/FontFileParser.h> |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 37 | #include <minikin/LocaleList.h> |
Seigo Nonaka | d5ecaba | 2021-05-27 15:49:37 -0700 | [diff] [blame] | 38 | #include <minikin/SystemFonts.h> |
Jagadeesh Pakaravoor | b624af3 | 2020-05-01 00:01:40 +0000 | [diff] [blame] | 39 | #include <ui/FatVector.h> |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 40 | |
| 41 | #include <memory> |
| 42 | |
| 43 | namespace android { |
| 44 | |
| 45 | struct NativeFontBuilder { |
| 46 | std::vector<minikin::FontVariation> axes; |
| 47 | }; |
| 48 | |
| 49 | static inline NativeFontBuilder* toBuilder(jlong ptr) { |
| 50 | return reinterpret_cast<NativeFontBuilder*>(ptr); |
| 51 | } |
| 52 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 53 | static void releaseFont(jlong font) { |
| 54 | delete reinterpret_cast<FontWrapper*>(font); |
| 55 | } |
| 56 | |
| 57 | static void release_global_ref(const void* /*data*/, void* context) { |
Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame] | 58 | JNIEnv* env = GraphicsJNI::getJNIEnv(); |
| 59 | bool needToAttach = (env == nullptr); |
| 60 | if (needToAttach) { |
| 61 | env = GraphicsJNI::attachJNIEnv("release_font_data"); |
| 62 | if (env == nullptr) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 63 | ALOGE("failed to attach to thread to release global ref."); |
| 64 | return; |
| 65 | } |
| 66 | } |
| 67 | |
| 68 | jobject obj = reinterpret_cast<jobject>(context); |
| 69 | env->DeleteGlobalRef(obj); |
| 70 | } |
| 71 | |
| 72 | // Regular JNI |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 73 | static jlong Font_Builder_initBuilder(JNIEnv*, jobject) { |
| 74 | return reinterpret_cast<jlong>(new NativeFontBuilder()); |
| 75 | } |
| 76 | |
| 77 | // Critical Native |
Jerome Gaillard | 21e7e2d | 2019-05-14 14:34:46 +0100 | [diff] [blame] | 78 | 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] | 79 | toBuilder(builderPtr)->axes.emplace_back(static_cast<minikin::AxisTag>(tag), value); |
| 80 | } |
| 81 | |
| 82 | // Regular JNI |
| 83 | static jlong Font_Builder_build(JNIEnv* env, jobject clazz, jlong builderPtr, jobject buffer, |
Seigo Nonaka | 99c0756 | 2021-02-03 21:37:57 -0800 | [diff] [blame] | 84 | jstring filePath, jstring langTags, jint weight, jboolean italic, |
| 85 | jint ttcIndex) { |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 86 | NPE_CHECK_RETURN_ZERO(env, buffer); |
| 87 | std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr)); |
| 88 | const void* fontPtr = env->GetDirectBufferAddress(buffer); |
| 89 | if (fontPtr == nullptr) { |
| 90 | jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer"); |
| 91 | return 0; |
| 92 | } |
| 93 | jlong fontSize = env->GetDirectBufferCapacity(buffer); |
| 94 | if (fontSize <= 0) { |
| 95 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 96 | "buffer size must not be zero or negative"); |
| 97 | return 0; |
| 98 | } |
Seigo Nonaka | 54c6a27 | 2018-10-25 15:44:32 -0700 | [diff] [blame] | 99 | ScopedUtfChars fontPath(env, filePath); |
Seigo Nonaka | 99c0756 | 2021-02-03 21:37:57 -0800 | [diff] [blame] | 100 | ScopedUtfChars langTagStr(env, langTags); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 101 | jobject fontRef = MakeGlobalRefOrDie(env, buffer); |
| 102 | sk_sp<SkData> data(SkData::MakeWithProc(fontPtr, fontSize, |
| 103 | release_global_ref, reinterpret_cast<void*>(fontRef))); |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 104 | std::shared_ptr<minikin::MinikinFont> minikinFont = fonts::createMinikinFontSkia( |
| 105 | std::move(data), std::string_view(fontPath.c_str(), fontPath.size()), |
| 106 | fontPtr, fontSize, ttcIndex, builder->axes); |
| 107 | if (minikinFont == nullptr) { |
xi yu | 61d6a90d | 2023-02-17 03:47:43 +0000 | [diff] [blame^] | 108 | jniThrowExceptionFmt(env, "java/lang/IllegalArgumentException", |
| 109 | "Failed to create internal object. maybe invalid font data. filePath %s", |
| 110 | fontPath.c_str()); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 111 | return 0; |
| 112 | } |
Seigo Nonaka | 99c0756 | 2021-02-03 21:37:57 -0800 | [diff] [blame] | 113 | uint32_t localeListId = minikin::registerLocaleList(langTagStr.c_str()); |
| 114 | std::shared_ptr<minikin::Font> font = |
| 115 | minikin::Font::Builder(minikinFont) |
| 116 | .setWeight(weight) |
| 117 | .setSlant(static_cast<minikin::FontStyle::Slant>(italic)) |
| 118 | .setLocaleListId(localeListId) |
| 119 | .build(); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 120 | return reinterpret_cast<jlong>(new FontWrapper(std::move(font))); |
| 121 | } |
| 122 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 123 | // Fast Native |
| 124 | static jlong Font_Builder_clone(JNIEnv* env, jobject clazz, jlong fontPtr, jlong builderPtr, |
| 125 | jint weight, jboolean italic, jint ttcIndex) { |
| 126 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 127 | MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get()); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 128 | std::unique_ptr<NativeFontBuilder> builder(toBuilder(builderPtr)); |
| 129 | |
| 130 | // Reconstruct SkTypeface with different arguments from existing SkTypeface. |
| 131 | FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation; |
| 132 | for (const auto& axis : builder->axes) { |
| 133 | skVariation.push_back({axis.axisTag, axis.value}); |
| 134 | } |
| 135 | SkFontArguments args; |
| 136 | args.setCollectionIndex(ttcIndex); |
| 137 | args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())}); |
| 138 | |
| 139 | sk_sp<SkTypeface> newTypeface = minikinSkia->GetSkTypeface()->makeClone(args); |
| 140 | |
| 141 | std::shared_ptr<minikin::MinikinFont> newMinikinFont = std::make_shared<MinikinFontSkia>( |
Seigo Nonaka | 5dd6dd5 | 2021-02-11 20:02:27 -0800 | [diff] [blame] | 142 | std::move(newTypeface), minikinSkia->GetSourceId(), minikinSkia->GetFontData(), |
| 143 | minikinSkia->GetFontSize(), minikinSkia->getFilePath(), minikinSkia->GetFontIndex(), |
| 144 | builder->axes); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 145 | std::shared_ptr<minikin::Font> newFont = minikin::Font::Builder(newMinikinFont) |
| 146 | .setWeight(weight) |
| 147 | .setSlant(static_cast<minikin::FontStyle::Slant>(italic)) |
| 148 | .build(); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 149 | return reinterpret_cast<jlong>(new FontWrapper(std::move(newFont))); |
| 150 | } |
| 151 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 152 | /////////////////////////////////////////////////////////////////////////////// |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 153 | // Font JNI functions |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 154 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 155 | // Fast Native |
| 156 | static jfloat Font_getGlyphBounds(JNIEnv* env, jobject, jlong fontHandle, jint glyphId, |
| 157 | jlong paintHandle, jobject rect) { |
| 158 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 159 | MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get()); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 160 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 161 | |
| 162 | SkFont* skFont = &paint->getSkFont(); |
| 163 | // We don't use populateSkFont since it is designed to be used for layout result with addressing |
| 164 | // auto fake-bolding. |
| 165 | skFont->setTypeface(minikinSkia->RefSkTypeface()); |
| 166 | |
| 167 | uint16_t glyph16 = glyphId; |
| 168 | SkRect skBounds; |
| 169 | SkScalar skWidth; |
| 170 | skFont->getWidthsBounds(&glyph16, 1, &skWidth, &skBounds, nullptr); |
| 171 | GraphicsJNI::rect_to_jrectf(skBounds, env, rect); |
| 172 | return SkScalarToFloat(skWidth); |
| 173 | } |
| 174 | |
| 175 | // Fast Native |
| 176 | static jfloat Font_getFontMetrics(JNIEnv* env, jobject, jlong fontHandle, jlong paintHandle, |
| 177 | jobject metricsObj) { |
| 178 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontHandle); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 179 | MinikinFontSkia* minikinSkia = static_cast<MinikinFontSkia*>(font->font->typeface().get()); |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 180 | Paint* paint = reinterpret_cast<Paint*>(paintHandle); |
| 181 | |
| 182 | SkFont* skFont = &paint->getSkFont(); |
| 183 | // We don't use populateSkFont since it is designed to be used for layout result with addressing |
| 184 | // auto fake-bolding. |
| 185 | skFont->setTypeface(minikinSkia->RefSkTypeface()); |
| 186 | |
| 187 | SkFontMetrics metrics; |
| 188 | SkScalar spacing = skFont->getMetrics(&metrics); |
| 189 | GraphicsJNI::set_metrics(env, metricsObj, metrics); |
| 190 | return spacing; |
| 191 | } |
| 192 | |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 193 | // Critical Native |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 194 | static jlong Font_getMinikinFontPtr(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) { |
| 195 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
Kohsuke Yatoh | a3f5f22 | 2021-06-09 20:09:49 +0000 | [diff] [blame] | 196 | return reinterpret_cast<jlong>(font->font.get()); |
Seigo Nonaka | 760d351 | 2020-10-01 12:29:03 -0700 | [diff] [blame] | 197 | } |
| 198 | |
Seigo Nonaka | 31bf860 | 2020-10-14 15:04:01 -0700 | [diff] [blame] | 199 | // Critical Native |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 200 | static jlong Font_cloneFont(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) { |
| 201 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
| 202 | std::shared_ptr<minikin::Font> ref = font->font; |
| 203 | return reinterpret_cast<jlong>(new FontWrapper(std::move(ref))); |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Fast Native |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 207 | static jobject Font_newByteBuffer(JNIEnv* env, jobject, jlong fontPtr) { |
| 208 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
| 209 | const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface(); |
| 210 | return env->NewDirectByteBuffer(const_cast<void*>(minikinFont->GetFontData()), |
| 211 | minikinFont->GetFontSize()); |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 212 | } |
| 213 | |
| 214 | // Critical Native |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 215 | static jlong Font_getBufferAddress(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) { |
| 216 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
| 217 | return reinterpret_cast<jlong>(font->font->typeface()->GetFontData()); |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 220 | // Critical Native |
Jerome Gaillard | b71861c | 2021-02-09 13:10:49 +0000 | [diff] [blame] | 221 | static jlong Font_getReleaseNativeFontFunc(CRITICAL_JNI_PARAMS) { |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 222 | return reinterpret_cast<jlong>(releaseFont); |
| 223 | } |
| 224 | |
| 225 | // Fast Native |
| 226 | static jstring Font_getFontPath(JNIEnv* env, jobject, jlong fontPtr) { |
| 227 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
Kohsuke Yatoh | a3f5f22 | 2021-06-09 20:09:49 +0000 | [diff] [blame] | 228 | minikin::BufferReader reader = font->font->typefaceMetadataReader(); |
| 229 | if (reader.data() != nullptr) { |
| 230 | std::string path = std::string(reader.readString()); |
| 231 | if (path.empty()) { |
| 232 | return nullptr; |
| 233 | } |
| 234 | return env->NewStringUTF(path.c_str()); |
| 235 | } else { |
| 236 | const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface(); |
| 237 | const std::string& path = minikinFont->GetFontPath(); |
| 238 | if (path.empty()) { |
| 239 | return nullptr; |
| 240 | } |
| 241 | return env->NewStringUTF(path.c_str()); |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 242 | } |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 243 | } |
| 244 | |
| 245 | // Fast Native |
| 246 | static jstring Font_getLocaleList(JNIEnv* env, jobject, jlong fontPtr) { |
| 247 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
| 248 | uint32_t localeListId = font->font->getLocaleListId(); |
| 249 | if (localeListId == 0) { |
| 250 | return nullptr; |
| 251 | } |
| 252 | std::string langTags = minikin::getLocaleString(localeListId); |
| 253 | if (langTags.empty()) { |
| 254 | return nullptr; |
| 255 | } |
| 256 | return env->NewStringUTF(langTags.c_str()); |
| 257 | } |
| 258 | |
| 259 | // Critical Native |
| 260 | static jint Font_getPackedStyle(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) { |
| 261 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
| 262 | uint32_t weight = font->font->style().weight(); |
| 263 | uint32_t isItalic = font->font->style().slant() == minikin::FontStyle::Slant::ITALIC ? 1 : 0; |
| 264 | return (isItalic << 16) | weight; |
| 265 | } |
| 266 | |
| 267 | // Critical Native |
| 268 | static jint Font_getIndex(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) { |
| 269 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
Kohsuke Yatoh | a3f5f22 | 2021-06-09 20:09:49 +0000 | [diff] [blame] | 270 | minikin::BufferReader reader = font->font->typefaceMetadataReader(); |
| 271 | if (reader.data() != nullptr) { |
| 272 | reader.skipString(); // fontPath |
| 273 | return reader.read<int>(); |
| 274 | } else { |
| 275 | const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface(); |
| 276 | return minikinFont->GetFontIndex(); |
| 277 | } |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 278 | } |
| 279 | |
| 280 | // Critical Native |
| 281 | static jint Font_getAxisCount(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) { |
| 282 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
Kohsuke Yatoh | a3f5f22 | 2021-06-09 20:09:49 +0000 | [diff] [blame] | 283 | minikin::BufferReader reader = font->font->typefaceMetadataReader(); |
| 284 | if (reader.data() != nullptr) { |
| 285 | reader.skipString(); // fontPath |
| 286 | reader.skip<int>(); // fontIndex |
| 287 | return reader.readArray<minikin::FontVariation>().second; |
| 288 | } else { |
| 289 | const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface(); |
| 290 | return minikinFont->GetAxes().size(); |
| 291 | } |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | // Critical Native |
| 295 | static jlong Font_getAxisInfo(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr, jint index) { |
| 296 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
Kohsuke Yatoh | a3f5f22 | 2021-06-09 20:09:49 +0000 | [diff] [blame] | 297 | minikin::BufferReader reader = font->font->typefaceMetadataReader(); |
| 298 | minikin::FontVariation var; |
| 299 | if (reader.data() != nullptr) { |
| 300 | reader.skipString(); // fontPath |
| 301 | reader.skip<int>(); // fontIndex |
| 302 | var = reader.readArray<minikin::FontVariation>().first[index]; |
| 303 | } else { |
| 304 | const std::shared_ptr<minikin::MinikinFont>& minikinFont = font->font->typeface(); |
| 305 | var = minikinFont->GetAxes().at(index); |
| 306 | } |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 307 | uint32_t floatBinary = *reinterpret_cast<const uint32_t*>(&var.value); |
| 308 | return (static_cast<uint64_t>(var.axisTag) << 32) | static_cast<uint64_t>(floatBinary); |
| 309 | } |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 310 | |
Seigo Nonaka | 5dd6dd5 | 2021-02-11 20:02:27 -0800 | [diff] [blame] | 311 | // Critical Native |
| 312 | static jint Font_getSourceId(CRITICAL_JNI_PARAMS_COMMA jlong fontPtr) { |
| 313 | FontWrapper* font = reinterpret_cast<FontWrapper*>(fontPtr); |
| 314 | return font->font->typeface()->GetSourceId(); |
| 315 | } |
| 316 | |
Seigo Nonaka | d5ecaba | 2021-05-27 15:49:37 -0700 | [diff] [blame] | 317 | static jlongArray Font_getAvailableFontSet(JNIEnv* env, jobject) { |
| 318 | std::vector<jlong> refArray; |
| 319 | minikin::SystemFonts::getFontSet( |
| 320 | [&refArray](const std::vector<std::shared_ptr<minikin::Font>>& fontSet) { |
| 321 | refArray.reserve(fontSet.size()); |
| 322 | for (const auto& font : fontSet) { |
| 323 | std::shared_ptr<minikin::Font> fontRef = font; |
| 324 | refArray.push_back( |
| 325 | reinterpret_cast<jlong>(new FontWrapper(std::move(fontRef)))); |
| 326 | } |
| 327 | }); |
| 328 | jlongArray r = env->NewLongArray(refArray.size()); |
| 329 | env->SetLongArrayRegion(r, 0, refArray.size(), refArray.data()); |
| 330 | return r; |
| 331 | } |
| 332 | |
Seigo Nonaka | 47b6c1b | 2021-01-13 15:02:47 -0800 | [diff] [blame] | 333 | // Fast Native |
| 334 | static jlong FontFileUtil_getFontRevision(JNIEnv* env, jobject, jobject buffer, jint index) { |
| 335 | NPE_CHECK_RETURN_ZERO(env, buffer); |
| 336 | const void* fontPtr = env->GetDirectBufferAddress(buffer); |
| 337 | if (fontPtr == nullptr) { |
| 338 | jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer"); |
| 339 | return 0; |
| 340 | } |
| 341 | jlong fontSize = env->GetDirectBufferCapacity(buffer); |
| 342 | if (fontSize <= 0) { |
| 343 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 344 | "buffer size must not be zero or negative"); |
| 345 | return 0; |
| 346 | } |
| 347 | minikin::FontFileParser parser(fontPtr, fontSize, index); |
| 348 | std::optional<uint32_t> revision = parser.getFontRevision(); |
| 349 | if (!revision.has_value()) { |
| 350 | return -1L; |
| 351 | } |
| 352 | return revision.value(); |
| 353 | } |
| 354 | |
| 355 | static jstring FontFileUtil_getFontPostScriptName(JNIEnv* env, jobject, jobject buffer, |
| 356 | jint index) { |
| 357 | NPE_CHECK_RETURN_ZERO(env, buffer); |
| 358 | const void* fontPtr = env->GetDirectBufferAddress(buffer); |
| 359 | if (fontPtr == nullptr) { |
| 360 | jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer"); |
| 361 | return nullptr; |
| 362 | } |
| 363 | jlong fontSize = env->GetDirectBufferCapacity(buffer); |
| 364 | if (fontSize <= 0) { |
| 365 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 366 | "buffer size must not be zero or negative"); |
| 367 | return nullptr; |
| 368 | } |
| 369 | minikin::FontFileParser parser(fontPtr, fontSize, index); |
| 370 | std::optional<std::string> psName = parser.getPostScriptName(); |
| 371 | if (!psName.has_value()) { |
| 372 | return nullptr; // null |
| 373 | } |
| 374 | return env->NewStringUTF(psName->c_str()); |
| 375 | } |
Seigo Nonaka | 9d5ab7e | 2021-01-20 22:50:09 -0800 | [diff] [blame] | 376 | |
| 377 | static jint FontFileUtil_isPostScriptType1Font(JNIEnv* env, jobject, jobject buffer, jint index) { |
| 378 | NPE_CHECK_RETURN_ZERO(env, buffer); |
| 379 | const void* fontPtr = env->GetDirectBufferAddress(buffer); |
| 380 | if (fontPtr == nullptr) { |
| 381 | jniThrowException(env, "java/lang/IllegalArgumentException", "Not a direct buffer"); |
| 382 | return -1; |
| 383 | } |
| 384 | jlong fontSize = env->GetDirectBufferCapacity(buffer); |
| 385 | if (fontSize <= 0) { |
| 386 | jniThrowException(env, "java/lang/IllegalArgumentException", |
| 387 | "buffer size must not be zero or negative"); |
| 388 | return -1; |
| 389 | } |
| 390 | minikin::FontFileParser parser(fontPtr, fontSize, index); |
| 391 | std::optional<bool> isType1 = parser.isPostScriptType1Font(); |
| 392 | if (!isType1.has_value()) { |
| 393 | return -1; // not an OpenType font. HarfBuzz failed to parse it. |
| 394 | } |
| 395 | return isType1.value(); |
| 396 | } |
| 397 | |
Seigo Nonaka | 47b6c1b | 2021-01-13 15:02:47 -0800 | [diff] [blame] | 398 | /////////////////////////////////////////////////////////////////////////////// |
| 399 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 400 | static const JNINativeMethod gFontBuilderMethods[] = { |
Seigo Nonaka | 99c0756 | 2021-02-03 21:37:57 -0800 | [diff] [blame] | 401 | {"nInitBuilder", "()J", (void*)Font_Builder_initBuilder}, |
| 402 | {"nAddAxis", "(JIF)V", (void*)Font_Builder_addAxis}, |
| 403 | {"nBuild", "(JLjava/nio/ByteBuffer;Ljava/lang/String;Ljava/lang/String;IZI)J", |
| 404 | (void*)Font_Builder_build}, |
| 405 | {"nClone", "(JJIZI)J", (void*)Font_Builder_clone}, |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 406 | }; |
| 407 | |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 408 | static const JNINativeMethod gFontMethods[] = { |
Seigo Nonaka | 1dd39f3 | 2021-02-05 19:15:29 -0800 | [diff] [blame] | 409 | {"nGetMinikinFontPtr", "(J)J", (void*)Font_getMinikinFontPtr}, |
| 410 | {"nCloneFont", "(J)J", (void*)Font_cloneFont}, |
| 411 | {"nNewByteBuffer", "(J)Ljava/nio/ByteBuffer;", (void*)Font_newByteBuffer}, |
| 412 | {"nGetBufferAddress", "(J)J", (void*)Font_getBufferAddress}, |
| 413 | {"nGetReleaseNativeFont", "()J", (void*)Font_getReleaseNativeFontFunc}, |
| 414 | {"nGetGlyphBounds", "(JIJLandroid/graphics/RectF;)F", (void*)Font_getGlyphBounds}, |
| 415 | {"nGetFontMetrics", "(JJLandroid/graphics/Paint$FontMetrics;)F", |
| 416 | (void*)Font_getFontMetrics}, |
| 417 | {"nGetFontPath", "(J)Ljava/lang/String;", (void*)Font_getFontPath}, |
| 418 | {"nGetLocaleList", "(J)Ljava/lang/String;", (void*)Font_getLocaleList}, |
| 419 | {"nGetPackedStyle", "(J)I", (void*)Font_getPackedStyle}, |
| 420 | {"nGetIndex", "(J)I", (void*)Font_getIndex}, |
| 421 | {"nGetAxisCount", "(J)I", (void*)Font_getAxisCount}, |
| 422 | {"nGetAxisInfo", "(JI)J", (void*)Font_getAxisInfo}, |
Seigo Nonaka | 5dd6dd5 | 2021-02-11 20:02:27 -0800 | [diff] [blame] | 423 | {"nGetSourceId", "(J)I", (void*)Font_getSourceId}, |
Seigo Nonaka | d5ecaba | 2021-05-27 15:49:37 -0700 | [diff] [blame] | 424 | |
| 425 | // System font accessors |
| 426 | {"nGetAvailableFontSet", "()[J", (void*)Font_getAvailableFontSet}, |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 427 | }; |
| 428 | |
Seigo Nonaka | 47b6c1b | 2021-01-13 15:02:47 -0800 | [diff] [blame] | 429 | static const JNINativeMethod gFontFileUtilMethods[] = { |
| 430 | { "nGetFontRevision", "(Ljava/nio/ByteBuffer;I)J", (void*) FontFileUtil_getFontRevision }, |
| 431 | { "nGetFontPostScriptName", "(Ljava/nio/ByteBuffer;I)Ljava/lang/String;", |
| 432 | (void*) FontFileUtil_getFontPostScriptName }, |
Seigo Nonaka | 9d5ab7e | 2021-01-20 22:50:09 -0800 | [diff] [blame] | 433 | { "nIsPostScriptType1Font", "(Ljava/nio/ByteBuffer;I)I", |
| 434 | (void*) FontFileUtil_isPostScriptType1Font }, |
Seigo Nonaka | 47b6c1b | 2021-01-13 15:02:47 -0800 | [diff] [blame] | 435 | }; |
| 436 | |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 437 | int register_android_graphics_fonts_Font(JNIEnv* env) { |
| 438 | return RegisterMethodsOrDie(env, "android/graphics/fonts/Font$Builder", gFontBuilderMethods, |
Seigo Nonaka | 1ed4f64 | 2020-09-10 17:19:34 -0700 | [diff] [blame] | 439 | NELEM(gFontBuilderMethods)) + |
| 440 | RegisterMethodsOrDie(env, "android/graphics/fonts/Font", gFontMethods, |
Seigo Nonaka | f3a1915 | 2020-09-14 15:29:42 -0700 | [diff] [blame] | 441 | NELEM(gFontMethods)) + |
Seigo Nonaka | 47b6c1b | 2021-01-13 15:02:47 -0800 | [diff] [blame] | 442 | RegisterMethodsOrDie(env, "android/graphics/fonts/FontFileUtil", gFontFileUtilMethods, |
| 443 | NELEM(gFontFileUtilMethods)); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 444 | } |
| 445 | |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 446 | namespace fonts { |
| 447 | |
| 448 | std::shared_ptr<minikin::MinikinFont> createMinikinFontSkia( |
| 449 | sk_sp<SkData>&& data, std::string_view fontPath, const void *fontPtr, size_t fontSize, |
| 450 | int ttcIndex, const std::vector<minikin::FontVariation>& axes) { |
| 451 | FatVector<SkFontArguments::VariationPosition::Coordinate, 2> skVariation; |
| 452 | for (const auto& axis : axes) { |
| 453 | skVariation.push_back({axis.axisTag, axis.value}); |
| 454 | } |
| 455 | |
| 456 | std::unique_ptr<SkStreamAsset> fontData(new SkMemoryStream(std::move(data))); |
| 457 | |
| 458 | SkFontArguments args; |
| 459 | args.setCollectionIndex(ttcIndex); |
| 460 | args.setVariationDesignPosition({skVariation.data(), static_cast<int>(skVariation.size())}); |
| 461 | |
| 462 | sk_sp<SkFontMgr> fm(SkFontMgr::RefDefault()); |
| 463 | sk_sp<SkTypeface> face(fm->makeFromStream(std::move(fontData), args)); |
| 464 | if (face == nullptr) { |
| 465 | return nullptr; |
| 466 | } |
Seigo Nonaka | 5dd6dd5 | 2021-02-11 20:02:27 -0800 | [diff] [blame] | 467 | return std::make_shared<MinikinFontSkia>(std::move(face), getNewSourceId(), fontPtr, fontSize, |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 468 | fontPath, ttcIndex, axes); |
Seigo Nonaka | a1c21c0 | 2018-07-20 15:57:39 -0700 | [diff] [blame] | 469 | } |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 470 | |
Seigo Nonaka | 5dd6dd5 | 2021-02-11 20:02:27 -0800 | [diff] [blame] | 471 | int getNewSourceId() { |
| 472 | static std::atomic<int> sSourceId = {0}; |
| 473 | return sSourceId++; |
| 474 | } |
| 475 | |
Kohsuke Yatoh | 1ca390e | 2020-11-06 16:21:30 -0800 | [diff] [blame] | 476 | } // namespace fonts |
| 477 | |
| 478 | } // namespace android |