FontFamily.java revision 8b48e624457e438fcc2b6b9363329036ef2f7743
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;
22import dalvik.annotation.optimization.CriticalNative;
23
24import java.io.FileInputStream;
25import java.io.IOException;
26import java.nio.ByteBuffer;
27import java.nio.channels.FileChannel;
28import java.util.List;
29
30/**
31 * A family of typefaces with different styles.
32 *
33 * @hide
34 */
35public class FontFamily {
36
37    private static String TAG = "FontFamily";
38
39    /**
40     * @hide
41     */
42    public long mNativePtr;
43
44    // Points native font family builder. Must be zero after freezing this family.
45    private long mBuilderPtr;
46
47    public FontFamily() {
48        mBuilderPtr = nInitBuilder(null, 0);
49    }
50
51    public FontFamily(String lang, String variant) {
52        int varEnum = 0;
53        if ("compact".equals(variant)) {
54            varEnum = 1;
55        } else if ("elegant".equals(variant)) {
56            varEnum = 2;
57        }
58        mBuilderPtr = nInitBuilder(lang, varEnum);
59    }
60
61    public void freeze() {
62        if (mBuilderPtr == 0) {
63            throw new IllegalStateException("This FontFamily is already frozen");
64        }
65        mNativePtr = nCreateFamily(mBuilderPtr);
66        mBuilderPtr = 0;
67    }
68
69    public void abortCreation() {
70        if (mBuilderPtr == 0) {
71            throw new IllegalStateException("This FontFamily is already frozen or abandoned");
72        }
73        nAbort(mBuilderPtr);
74        mBuilderPtr = 0;
75    }
76
77    @Override
78    protected void finalize() throws Throwable {
79        try {
80            if (mNativePtr != 0) {
81                nUnrefFamily(mNativePtr);
82            }
83            if (mBuilderPtr != 0) {
84                nAbort(mBuilderPtr);
85            }
86        } finally {
87            super.finalize();
88        }
89    }
90
91    public boolean addFont(String path, int ttcIndex) {
92        if (mBuilderPtr == 0) {
93            throw new IllegalStateException("Unable to call addFont after freezing.");
94        }
95        try (FileInputStream file = new FileInputStream(path)) {
96            FileChannel fileChannel = file.getChannel();
97            long fontSize = fileChannel.size();
98            ByteBuffer fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize);
99            return nAddFont(mBuilderPtr, fontBuffer, ttcIndex);
100        } catch (IOException e) {
101            Log.e(TAG, "Error mapping font file " + path);
102            return false;
103        }
104    }
105
106    public boolean addFontWeightStyle(ByteBuffer font, int ttcIndex, List<FontConfig.Axis> axes,
107            int weight, boolean style) {
108        if (mBuilderPtr == 0) {
109            throw new IllegalStateException("Unable to call addFontWeightStyle after freezing.");
110        }
111        return nAddFontWeightStyle(mBuilderPtr, font, ttcIndex, axes, weight, style);
112    }
113
114    public boolean addFontFromAssetManager(AssetManager mgr, String path, int cookie,
115            boolean isAsset) {
116        if (mBuilderPtr == 0) {
117            throw new IllegalStateException("Unable to call addFontFromAsset after freezing.");
118        }
119        return nAddFontFromAssetManager(mBuilderPtr, mgr, path, cookie, isAsset);
120    }
121
122    private static native long nInitBuilder(String lang, int variant);
123
124    @CriticalNative
125    private static native long nCreateFamily(long mBuilderPtr);
126
127    @CriticalNative
128    private static native void nAbort(long mBuilderPtr);
129
130    @CriticalNative
131    private static native void nUnrefFamily(long nativePtr);
132    private static native boolean nAddFont(long builderPtr, ByteBuffer font, int ttcIndex);
133    private static native boolean nAddFontWeightStyle(long builderPtr, ByteBuffer font,
134            int ttcIndex, List<FontConfig.Axis> listOfAxis,
135            int weight, boolean isItalic);
136    private static native boolean nAddFontFromAssetManager(long builderPtr, AssetManager mgr,
137            String path, int cookie, boolean isAsset);
138}
139