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