blob: b8b7e76e3b207f28b79c1d61376cdbb4d8f76890 [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
17package android.graphics;
18
Raph Leviend5737942014-06-01 22:52:12 -070019import android.content.res.AssetManager;
20
Raph Levien1a73f7322014-01-30 16:06:28 -080021/**
22 * A family of typefaces with different styles.
23 *
24 * @hide
25 */
26public class FontFamily {
Raph Levien9a5b61c2014-04-29 18:26:48 -070027 /**
28 * @hide
29 */
Raph Levien1a73f7322014-01-30 16:06:28 -080030 public long mNativePtr;
Raph Levien9a5b61c2014-04-29 18:26:48 -070031
Raph Levien1a73f7322014-01-30 16:06:28 -080032 public FontFamily() {
Raph Levienf9e3d312014-05-27 16:33:11 -070033 mNativePtr = nCreateFamily(null, 0);
Raph Levien9a5b61c2014-04-29 18:26:48 -070034 if (mNativePtr == 0) {
Raph Levienf9e3d312014-05-27 16:33:11 -070035 throw new IllegalStateException("error creating native FontFamily");
36 }
37 }
38
39 public FontFamily(String lang, String variant) {
40 int varEnum = 0;
41 if ("compact".equals(variant)) {
42 varEnum = 1;
43 } else if ("elegant".equals(variant)) {
44 varEnum = 2;
45 }
46 mNativePtr = nCreateFamily(lang, varEnum);
47 if (mNativePtr == 0) {
48 throw new IllegalStateException("error creating native FontFamily");
Raph Levien9a5b61c2014-04-29 18:26:48 -070049 }
Raph Levien1a73f7322014-01-30 16:06:28 -080050 }
Raph Levien15cf4752014-05-05 16:08:07 -070051
52 @Override
53 protected void finalize() throws Throwable {
54 try {
55 nUnrefFamily(mNativePtr);
56 } finally {
57 super.finalize();
58 }
59 }
Raph Levien1a73f7322014-01-30 16:06:28 -080060
Raph Leviend5737942014-06-01 22:52:12 -070061 public boolean addFont(String path) {
62 return nAddFont(mNativePtr, path);
Raph Levien1a73f7322014-01-30 16:06:28 -080063 }
64
Raph Levien117cbeb2014-08-25 13:47:16 -070065 public boolean addFontWeightStyle(String path, int weight, boolean style) {
66 return nAddFontWeightStyle(mNativePtr, path, weight, style);
67 }
68
Raph Leviend5737942014-06-01 22:52:12 -070069 public boolean addFontFromAsset(AssetManager mgr, String path) {
70 return nAddFontFromAsset(mNativePtr, mgr, path);
71 }
72
73 private static native long nCreateFamily(String lang, int variant);
74 private static native void nUnrefFamily(long nativePtr);
75 private static native boolean nAddFont(long nativeFamily, String path);
Raph Levien117cbeb2014-08-25 13:47:16 -070076 private static native boolean nAddFontWeightStyle(long nativeFamily, String path,
77 int weight, boolean isItalic);
Raph Leviend5737942014-06-01 22:52:12 -070078 private static native boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr,
79 String path);
Raph Levien1a73f7322014-01-30 16:06:28 -080080}