FontFamily.java revision b44abf290190ceee037f24c47493a34de45fa3f4
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 /** 115 * @param mgr The AssetManager to use for this context. 116 * @param path The path to the font file to load. 117 * @param cookie If available, the resource cookie given by Resources. 118 * @param isAsset {@code true} if this is from the assets/ folder, {@code false} if from 119 * resources 120 * @param weight The weight of the font. If 0 is given, the weight and italic will be resolved 121 * using the OS/2 table in the font. 122 * @param isItalic Whether this font is italic. If the weight is set to 0, this will be resolved 123 * using the OS/2 table in the font. 124 * @return 125 */ 126 public boolean addFontFromAssetManager(AssetManager mgr, String path, int cookie, 127 boolean isAsset, int weight, boolean isItalic) { 128 if (mBuilderPtr == 0) { 129 throw new IllegalStateException("Unable to call addFontFromAsset after freezing."); 130 } 131 return nAddFontFromAssetManager(mBuilderPtr, mgr, path, cookie, isAsset, weight, isItalic); 132 } 133 134 private static native long nInitBuilder(String lang, int variant); 135 136 @CriticalNative 137 private static native long nCreateFamily(long mBuilderPtr); 138 139 @CriticalNative 140 private static native void nAbort(long mBuilderPtr); 141 142 @CriticalNative 143 private static native void nUnrefFamily(long nativePtr); 144 private static native boolean nAddFont(long builderPtr, ByteBuffer font, int ttcIndex); 145 private static native boolean nAddFontWeightStyle(long builderPtr, ByteBuffer font, 146 int ttcIndex, List<FontConfig.Axis> listOfAxis, 147 int weight, boolean isItalic); 148 private static native boolean nAddFontFromAssetManager(long builderPtr, AssetManager mgr, 149 String path, int cookie, boolean isAsset, int weight, boolean isItalic); 150} 151