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