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