blob: d28669a3549295e7d4ee8063097aac43b2a8e1ad [file] [log] [blame]
Raph Levien1a73f7322014-01-30 16:06:28 -08001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#define LOG_TAG "Minikin"
18
19#include "JNIHelp.h"
Andreas Gampeed6b9df2014-11-20 22:02:20 -080020#include <core_jni_helpers.h>
Raph Levien1a73f7322014-01-30 16:06:28 -080021
Ben Wagnerf462c242015-01-12 14:26:14 -050022#include "SkData.h"
23#include "SkRefCnt.h"
Raph Levien1a73f7322014-01-30 16:06:28 -080024#include "SkTypeface.h"
25#include "GraphicsJNI.h"
26#include <ScopedPrimitiveArray.h>
27#include <ScopedUtfChars.h>
Raph Leviend5737942014-06-01 22:52:12 -070028#include <android_runtime/android_util_AssetManager.h>
29#include <androidfw/AssetManager.h>
30#include "Utils.h"
Raph Levien1a73f7322014-01-30 16:06:28 -080031
Raph Levien3d528c402014-06-26 09:04:54 -070032#include "TypefaceImpl.h"
Raph Levien1a73f7322014-01-30 16:06:28 -080033#include <minikin/FontFamily.h>
34#include "MinikinSkia.h"
Raph Levien1a73f7322014-01-30 16:06:28 -080035
36namespace android {
37
Raph Levienf9e3d312014-05-27 16:33:11 -070038static jlong FontFamily_create(JNIEnv* env, jobject clazz, jstring lang, jint variant) {
Raph Levienf9e3d312014-05-27 16:33:11 -070039 FontLanguage fontLanguage;
40 if (lang != NULL) {
41 ScopedUtfChars str(env, lang);
42 fontLanguage = FontLanguage(str.c_str(), str.size());
43 }
44 return (jlong)new FontFamily(fontLanguage, variant);
Raph Levien1a73f7322014-01-30 16:06:28 -080045}
46
Raph Levien15cf4752014-05-05 16:08:07 -070047static void FontFamily_unref(JNIEnv* env, jobject clazz, jlong familyPtr) {
Raph Levien15cf4752014-05-05 16:08:07 -070048 FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr);
49 fontFamily->Unref();
Raph Levien1a73f7322014-01-30 16:06:28 -080050}
51
Raph Leviend5737942014-06-01 22:52:12 -070052static jboolean addSkTypeface(FontFamily* family, SkTypeface* face) {
53 MinikinFont* minikinFont = new MinikinFontSkia(face);
54 bool result = family->addFont(minikinFont);
55 minikinFont->Unref();
56 return result;
57}
Raph Leviend5737942014-06-01 22:52:12 -070058
Raph Levien1a73f7322014-01-30 16:06:28 -080059static jboolean FontFamily_addFont(JNIEnv* env, jobject clazz, jlong familyPtr, jstring path) {
Raph Levien1a73f7322014-01-30 16:06:28 -080060 NPE_CHECK_RETURN_ZERO(env, path);
61 ScopedUtfChars str(env, path);
Raph Levien1a73f7322014-01-30 16:06:28 -080062 SkTypeface* face = SkTypeface::CreateFromFile(str.c_str());
Raph Levien9a5b61c2014-04-29 18:26:48 -070063 if (face == NULL) {
64 ALOGE("addFont failed to create font %s", str.c_str());
65 return false;
66 }
Raph Levien117cbeb2014-08-25 13:47:16 -070067 FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr);
Raph Leviend5737942014-06-01 22:52:12 -070068 return addSkTypeface(fontFamily, face);
Raph Leviend5737942014-06-01 22:52:12 -070069}
70
Raph Levien117cbeb2014-08-25 13:47:16 -070071static jboolean FontFamily_addFontWeightStyle(JNIEnv* env, jobject clazz, jlong familyPtr,
72 jstring path, jint weight, jboolean isItalic) {
73 NPE_CHECK_RETURN_ZERO(env, path);
74 ScopedUtfChars str(env, path);
75 SkTypeface* face = SkTypeface::CreateFromFile(str.c_str());
76 if (face == NULL) {
77 ALOGE("addFont failed to create font %s", str.c_str());
78 return false;
79 }
80 FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr);
81 MinikinFont* minikinFont = new MinikinFontSkia(face);
82 fontFamily->addFont(minikinFont, FontStyle(weight / 100, isItalic));
83 minikinFont->Unref();
84 return true;
85}
86
Ben Wagnerf462c242015-01-12 14:26:14 -050087static void releaseAsset(const void* ptr, size_t length, void* context) {
88 delete static_cast<Asset*>(context);
89}
90
Raph Leviend5737942014-06-01 22:52:12 -070091static jboolean FontFamily_addFontFromAsset(JNIEnv* env, jobject, jlong familyPtr,
92 jobject jassetMgr, jstring jpath) {
Raph Leviend5737942014-06-01 22:52:12 -070093 NPE_CHECK_RETURN_ZERO(env, jassetMgr);
94 NPE_CHECK_RETURN_ZERO(env, jpath);
95
96 AssetManager* mgr = assetManagerForJavaObject(env, jassetMgr);
97 if (NULL == mgr) {
98 return false;
99 }
100
101 ScopedUtfChars str(env, jpath);
102 Asset* asset = mgr->open(str.c_str(), Asset::ACCESS_BUFFER);
103 if (NULL == asset) {
104 return false;
105 }
106
Ben Wagnerf462c242015-01-12 14:26:14 -0500107 const void* buf = asset->getBuffer(false);
108 if (NULL == buf) {
109 delete asset;
110 return false;
111 }
112
113 SkAutoTUnref<SkData> data(SkData::NewWithProc(buf, asset->getLength(), releaseAsset, asset));
Leon Scroggins III34497892015-01-20 15:52:43 -0500114 SkMemoryStream* stream = new SkMemoryStream(data);
115 // CreateFromStream takes ownership of stream.
Raph Leviend5737942014-06-01 22:52:12 -0700116 SkTypeface* face = SkTypeface::CreateFromStream(stream);
Raph Leviend5737942014-06-01 22:52:12 -0700117 if (face == NULL) {
118 ALOGE("addFontFromAsset failed to create font %s", str.c_str());
119 return false;
120 }
Raph Levien117cbeb2014-08-25 13:47:16 -0700121 FontFamily* fontFamily = reinterpret_cast<FontFamily*>(familyPtr);
Raph Leviend5737942014-06-01 22:52:12 -0700122 return addSkTypeface(fontFamily, face);
Raph Levien1a73f7322014-01-30 16:06:28 -0800123}
124
125///////////////////////////////////////////////////////////////////////////////
126
127static JNINativeMethod gFontFamilyMethods[] = {
Raph Levien117cbeb2014-08-25 13:47:16 -0700128 { "nCreateFamily", "(Ljava/lang/String;I)J", (void*)FontFamily_create },
129 { "nUnrefFamily", "(J)V", (void*)FontFamily_unref },
130 { "nAddFont", "(JLjava/lang/String;)Z", (void*)FontFamily_addFont },
131 { "nAddFontWeightStyle", "(JLjava/lang/String;IZ)Z", (void*)FontFamily_addFontWeightStyle },
132 { "nAddFontFromAsset", "(JLandroid/content/res/AssetManager;Ljava/lang/String;)Z",
Raph Leviend5737942014-06-01 22:52:12 -0700133 (void*)FontFamily_addFontFromAsset },
Raph Levien1a73f7322014-01-30 16:06:28 -0800134};
135
136int register_android_graphics_FontFamily(JNIEnv* env)
137{
Andreas Gampeed6b9df2014-11-20 22:02:20 -0800138 return RegisterMethodsOrDie(env, "android/graphics/FontFamily", gFontFamilyMethods,
139 NELEM(gFontFamilyMethods));
Raph Levien1a73f7322014-01-30 16:06:28 -0800140}
141
142}