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 <jni.h>
9cb93a386Sopenharmony_ci
10cb93a386Sopenharmony_ci#include "experimental/sktext/src/Paint.h"
11cb93a386Sopenharmony_ci#include "modules/androidkit/src/Utils.h"
12cb93a386Sopenharmony_ci#include <string>
13cb93a386Sopenharmony_ci
14cb93a386Sopenharmony_ciusing namespace skia::text;
15cb93a386Sopenharmony_ci
16cb93a386Sopenharmony_cinamespace {
17cb93a386Sopenharmony_ci
18cb93a386Sopenharmony_ci// conversion function for passing jstrings to SkText static calls
19cb93a386Sopenharmony_cistatic std::u16string JStringToU16String(JNIEnv* env, const jstring& jstr) {
20cb93a386Sopenharmony_ci    const jchar* u16 = env->GetStringChars(jstr, nullptr);
21cb93a386Sopenharmony_ci    std::u16string str(reinterpret_cast<const char16_t*>(u16), env->GetStringLength(jstr));
22cb93a386Sopenharmony_ci    env->ReleaseStringChars(jstr, u16);
23cb93a386Sopenharmony_ci    return str;
24cb93a386Sopenharmony_ci}
25cb93a386Sopenharmony_ci
26cb93a386Sopenharmony_cistatic void Text_RenderText(JNIEnv* env, jobject, jstring jtext,
27cb93a386Sopenharmony_ci                            jlong native_canvas, jlong native_fg_paint,
28cb93a386Sopenharmony_ci                            jfloat x, jfloat y) {
29cb93a386Sopenharmony_ci    auto* canvas = reinterpret_cast<SkCanvas*>(native_canvas);
30cb93a386Sopenharmony_ci    auto foreground = reinterpret_cast<SkPaint*>(native_fg_paint);
31cb93a386Sopenharmony_ci    std::u16string u16str = JStringToU16String(env, jtext);
32cb93a386Sopenharmony_ci    if (canvas && foreground) {
33cb93a386Sopenharmony_ci        SkPaint background(SkColors::kTransparent);
34cb93a386Sopenharmony_ci        Paint::drawText(u16str, canvas,
35cb93a386Sopenharmony_ci                        skia::text::TextDirection::kLtr, skia::text::TextAlign::kLeft,
36cb93a386Sopenharmony_ci                        *foreground, background, SkString("arial"), 50, SkFontStyle::Bold(), x, y);
37cb93a386Sopenharmony_ci    }
38cb93a386Sopenharmony_ci}
39cb93a386Sopenharmony_ci
40cb93a386Sopenharmony_ci} //namespace
41cb93a386Sopenharmony_ci
42cb93a386Sopenharmony_ciint register_androidkit_Text(JNIEnv* env) {
43cb93a386Sopenharmony_ci    static const JNINativeMethod methods[] = {
44cb93a386Sopenharmony_ci        {"nRenderText", "(Ljava/lang/String;JJFF)V", reinterpret_cast<void*>(Text_RenderText)},
45cb93a386Sopenharmony_ci    };
46cb93a386Sopenharmony_ci
47cb93a386Sopenharmony_ci    const auto clazz = env->FindClass("org/skia/androidkit/Text");
48cb93a386Sopenharmony_ci    return clazz
49cb93a386Sopenharmony_ci        ? env->RegisterNatives(clazz, methods, SK_ARRAY_COUNT(methods))
50cb93a386Sopenharmony_ci        : JNI_ERR;
51cb93a386Sopenharmony_ci}
52