1 /*
2 * Copyright 2021 Google Inc.
3 *
4 * Use of this source code is governed by a BSD-style license that can be
5 * found in the LICENSE file.
6 */
7
8 #include "experimental/sktext/include/Text.h"
9 #include "experimental/sktext/include/Types.h"
10 #include "include/core/SkCanvas.h"
11 #include "modules/androidkit/src/Utils.h"
12
13 #include <jni.h>
14
15 namespace {
16 static jmethodID gFontChain_countMethodID;
17 static jmethodID gFontChain_getAtMethodID;
18 static jmethodID gFontChain_fontSizeMethodID;
19 static jmethodID gFontChain_localeMethodID;
20
21 class FontChainAdapter : public skia::text::FontChain {
22 public:
FontChainAdapter(JNIEnv* env, jobject jFontChain)23 FontChainAdapter(JNIEnv* env, jobject jFontChain)
24 : fEnv(env)
25 , fFontChainImpl(jFontChain)
26 { }
27
28 size_t count() const override {
29 return fEnv->CallIntMethod(fFontChainImpl, gFontChain_countMethodID);
30 }
31
32 sk_sp<SkTypeface> operator[](size_t index) const override {
33 SkASSERT(index < this->count());
34 SkFont* font = reinterpret_cast<SkFont*>(fEnv->CallLongMethod(fFontChainImpl, gFontChain_getAtMethodID, index));
35 if (font) {
36 return sk_ref_sp(font->getTypeface());
37 }
38 return nullptr;
39 }
40
41 float fontSize() const override {
42 return fEnv->CallFloatMethod(fFontChainImpl, gFontChain_fontSizeMethodID);
43 }
44
45 SkString locale() const override {
46 jstring jLocale = (jstring)fEnv->CallObjectMethod(fFontChainImpl, gFontChain_localeMethodID);
47
48 const androidkit::utils::CString cstr(fEnv, jLocale);
49 SkString str(cstr);
50 return str;
51 }
52
53 private:
54 // TODO: Check, can we hold on to env here or do we have to get it every time?
55 JNIEnv* fEnv;
56 jobject fFontChainImpl;
57 };
58
FontChain_Create(JNIEnv* env, jobject, jobject jFontChain)59 static jlong FontChain_Create(JNIEnv* env, jobject, jobject jFontChain) {
60 return reinterpret_cast<jlong>(new FontChainAdapter(env, jFontChain));
61 }
62
FontChain_Release(JNIEnv* env, jobject, jlong native_FontChain)63 static void FontChain_Release(JNIEnv* env, jobject, jlong native_FontChain) {
64 SkSafeUnref(reinterpret_cast<FontChainAdapter*>(native_FontChain));
65 }
66
67 } // namespace
68
register_androidkit_FontChain(JNIEnv* env)69 int register_androidkit_FontChain(JNIEnv* env) {
70 const auto clazz = env->FindClass("org/skia/androidkit/FontChain");
71
72 gFontChain_countMethodID = env->GetMethodID(clazz, "count", "()I");
73 gFontChain_getAtMethodID = env->GetMethodID(clazz, "getAt", "(I)J");
74 gFontChain_fontSizeMethodID = env->GetMethodID(clazz, "fontSize", "()F");
75 gFontChain_localeMethodID = env->GetMethodID(clazz, "locale", "()Ljava/lang/String;");
76
77 if (!gFontChain_countMethodID || !gFontChain_getAtMethodID ||
78 !gFontChain_fontSizeMethodID || !gFontChain_localeMethodID) {
79 return JNI_ERR;
80 }
81
82 static const JNINativeMethod methods[] = {
83 {"nCreate" , "(Lorg/skia/androidkit/FontChain;)J", reinterpret_cast<void*>(FontChain_Create)},
84 {"nRelease", "(J)V", reinterpret_cast<void*>(FontChain_Release)},
85 };
86
87 return clazz
88 ? env->RegisterNatives(clazz, methods, SK_ARRAY_COUNT(methods))
89 : JNI_ERR;
90 }
91