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