FontFamily.java revision a87b07d7fafd59ae26073a80cd742b17ea427ecd
1/*
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
19import android.content.res.AssetManager;
20
21import java.util.List;
22
23/**
24 * A family of typefaces with different styles.
25 *
26 * @hide
27 */
28public class FontFamily {
29    /**
30     * @hide
31     */
32    public long mNativePtr;
33
34    public FontFamily() {
35        mNativePtr = nCreateFamily(null, 0);
36        if (mNativePtr == 0) {
37            throw new IllegalStateException("error creating native FontFamily");
38        }
39    }
40
41    public FontFamily(String lang, String variant) {
42        int varEnum = 0;
43        if ("compact".equals(variant)) {
44            varEnum = 1;
45        } else if ("elegant".equals(variant)) {
46            varEnum = 2;
47        }
48        mNativePtr = nCreateFamily(lang, varEnum);
49        if (mNativePtr == 0) {
50            throw new IllegalStateException("error creating native FontFamily");
51        }
52    }
53
54    @Override
55    protected void finalize() throws Throwable {
56        try {
57            nUnrefFamily(mNativePtr);
58        } finally {
59            super.finalize();
60        }
61    }
62
63    public boolean addFont(String path, int ttcIndex) {
64        return nAddFont(mNativePtr, path, ttcIndex);
65    }
66
67    public boolean addFontWeightStyle(String path, int ttcIndex, List<FontListParser.Axis> axes,
68            int weight, boolean style) {
69        return nAddFontWeightStyle(mNativePtr, path, ttcIndex, axes, weight, style);
70    }
71
72    public boolean addFontFromAsset(AssetManager mgr, String path) {
73        return nAddFontFromAsset(mNativePtr, mgr, path);
74    }
75
76    private static native long nCreateFamily(String lang, int variant);
77    private static native void nUnrefFamily(long nativePtr);
78    private static native boolean nAddFont(long nativeFamily, String path, int ttcIndex);
79    private static native boolean nAddFontWeightStyle(long nativeFamily, String path,
80            int ttcIndex, List<FontListParser.Axis> listOfAxis,
81            int weight, boolean isItalic);
82    private static native boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr,
83            String path);
84}
85