FontFamily.java revision 4a65687b853a92268f4f0eb52f22e092b16f8ed3
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;
20import android.text.FontConfig;
21import android.util.Log;
22
23import java.io.FileInputStream;
24import java.io.IOException;
25import java.nio.ByteBuffer;
26import java.nio.channels.FileChannel;
27import java.util.List;
28
29/**
30 * A family of typefaces with different styles.
31 *
32 * @hide
33 */
34public class FontFamily {
35
36    private static String TAG = "FontFamily";
37
38    /**
39     * @hide
40     */
41    public long mNativePtr;
42
43    public FontFamily() {
44        mNativePtr = nCreateFamily(null, 0);
45        if (mNativePtr == 0) {
46            throw new IllegalStateException("error creating native FontFamily");
47        }
48    }
49
50    public FontFamily(String lang, String variant) {
51        int varEnum = 0;
52        if ("compact".equals(variant)) {
53            varEnum = 1;
54        } else if ("elegant".equals(variant)) {
55            varEnum = 2;
56        }
57        mNativePtr = nCreateFamily(lang, varEnum);
58        if (mNativePtr == 0) {
59            throw new IllegalStateException("error creating native FontFamily");
60        }
61    }
62
63    @Override
64    protected void finalize() throws Throwable {
65        try {
66            nUnrefFamily(mNativePtr);
67        } finally {
68            super.finalize();
69        }
70    }
71
72    public boolean addFont(String path, int ttcIndex) {
73        try (FileInputStream file = new FileInputStream(path)) {
74            FileChannel fileChannel = file.getChannel();
75            long fontSize = fileChannel.size();
76            ByteBuffer fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);
77            return nAddFont(mNativePtr, fontBuffer, ttcIndex);
78        } catch (IOException e) {
79            Log.e(TAG, "Error mapping font file " + path);
80            return false;
81        }
82    }
83
84    public boolean addFontWeightStyle(ByteBuffer font, int ttcIndex, List<FontConfig.Axis> axes,
85            int weight, boolean style) {
86        return nAddFontWeightStyle(mNativePtr, font, ttcIndex, axes, weight, style);
87    }
88
89    public boolean addFontFromAssetManager(AssetManager mgr, String path, int cookie,
90            boolean isAsset) {
91        return nAddFontFromAssetManager(mNativePtr, mgr, path, cookie, isAsset);
92    }
93
94    private static native long nCreateFamily(String lang, int variant);
95    private static native void nUnrefFamily(long nativePtr);
96    private static native boolean nAddFont(long nativeFamily, ByteBuffer font, int ttcIndex);
97    private static native boolean nAddFontWeightStyle(long nativeFamily, ByteBuffer font,
98            int ttcIndex, List<FontConfig.Axis> listOfAxis,
99            int weight, boolean isItalic);
100    private static native boolean nAddFontFromAssetManager(long nativeFamily, AssetManager mgr,
101            String path, int cookie, boolean isAsset);
102}
103