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 <jni.h>
9
10#include "include/core/SkFont.h"
11#include "include/core/SkFontMgr.h"
12#include "modules/androidkit/src/Utils.h"
13
14namespace {
15
16//TODO: replace jstring fontFamily with TypeFace object. Wrap Typeface in JetSki
17static jlong Font_Create(JNIEnv* env, jobject, jstring jFontFamily, float size) {
18    const androidkit::utils::CString cFontFamily(env, jFontFamily);
19    sk_sp<SkFontMgr> fontMgr = SkFontMgr::RefDefault();
20    sk_sp<SkTypeface> typeface = sk_sp<SkTypeface>(fontMgr->matchFamilyStyle(cFontFamily, SkFontStyle()));
21    return reinterpret_cast<jlong>(new SkFont(std::move(typeface), size));
22}
23
24static void Font_Release(JNIEnv*, jobject, jlong native_Font) {
25    delete reinterpret_cast<SkFont*>(native_Font);
26}
27
28}  // namespace
29
30int register_androidkit_Font(JNIEnv* env) {
31    static const JNINativeMethod methods[] = {
32        {"nCreate" , "(Ljava/lang/String;F)J" , reinterpret_cast<void*>(Font_Create)},
33        {"nRelease", "(J)V"                  , reinterpret_cast<void*>(Font_Release)},
34    };
35
36    const auto clazz = env->FindClass("org/skia/androidkit/Font");
37    return clazz
38        ? env->RegisterNatives(clazz, methods, SK_ARRAY_COUNT(methods))
39        : JNI_ERR;
40}
41