FontFamily.java revision 20e5d91739fb88a02afb4888bf9f938308bc9b7b
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; 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 // Points native font family builder. Must be zero after freezing this family. 44 private long mBuilderPtr; 45 46 public FontFamily() { 47 mBuilderPtr = nInitBuilder(null, 0); 48 } 49 50 public FontFamily(String lang, int variant) { 51 mBuilderPtr = nInitBuilder(lang, variant); 52 } 53 54 public void freeze() { 55 if (mBuilderPtr == 0) { 56 throw new IllegalStateException("This FontFamily is already frozen"); 57 } 58 mNativePtr = nCreateFamily(mBuilderPtr); 59 mBuilderPtr = 0; 60 } 61 62 public void abortCreation() { 63 if (mBuilderPtr == 0) { 64 throw new IllegalStateException("This FontFamily is already frozen or abandoned"); 65 } 66 nAbort(mBuilderPtr); 67 mBuilderPtr = 0; 68 } 69 70 @Override 71 protected void finalize() throws Throwable { 72 try { 73 if (mNativePtr != 0) { 74 nUnrefFamily(mNativePtr); 75 } 76 if (mBuilderPtr != 0) { 77 nAbort(mBuilderPtr); 78 } 79 } finally { 80 super.finalize(); 81 } 82 } 83 84 public boolean addFont(String path, int ttcIndex, FontConfig.Axis[] axes, int weight, 85 int italic) { 86 if (mBuilderPtr == 0) { 87 throw new IllegalStateException("Unable to call addFont after freezing."); 88 } 89 try (FileInputStream file = new FileInputStream(path)) { 90 FileChannel fileChannel = file.getChannel(); 91 long fontSize = fileChannel.size(); 92 ByteBuffer fontBuffer = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fontSize); 93 if (axes != null) { 94 for (FontConfig.Axis axis : axes) { 95 nAddAxisValue(mBuilderPtr, axis.getTag(), axis.getStyleValue()); 96 } 97 } 98 return nAddFont(mBuilderPtr, fontBuffer, ttcIndex, weight, italic); 99 } catch (IOException e) { 100 Log.e(TAG, "Error mapping font file " + path); 101 return false; 102 } 103 } 104 105 public boolean addFontFromBuffer(ByteBuffer font, int ttcIndex, FontConfig.Axis[] axes, 106 int weight, int italic) { 107 if (mBuilderPtr == 0) { 108 throw new IllegalStateException("Unable to call addFontWeightStyle after freezing."); 109 } 110 if (axes != null) { 111 for (FontConfig.Axis axis : axes) { 112 nAddAxisValue(mBuilderPtr, axis.getTag(), axis.getStyleValue()); 113 } 114 } 115 return nAddFontWeightStyle(mBuilderPtr, font, ttcIndex, weight, italic); 116 } 117 118 /** 119 * @param mgr The AssetManager to use for this context. 120 * @param path The path to the font file to load. 121 * @param cookie If available, the resource cookie given by Resources. 122 * @param isAsset {@code true} if this is from the assets/ folder, {@code false} if from 123 * resources 124 * @param weight The weight of the font. If 0 is given, the weight and italic will be resolved 125 * using the OS/2 table in the font. 126 * @param isItalic Whether this font is italic. If the weight is set to 0, this will be resolved 127 * using the OS/2 table in the font. 128 * @return 129 */ 130 public boolean addFontFromAssetManager(AssetManager mgr, String path, int cookie, 131 boolean isAsset, int ttcIndex, int weight, int isItalic, 132 FontConfig.Axis[] axes) { 133 if (mBuilderPtr == 0) { 134 throw new IllegalStateException("Unable to call addFontFromAsset after freezing."); 135 } 136 if (axes != null) { 137 for (FontConfig.Axis axis : axes) { 138 nAddAxisValue(mBuilderPtr, axis.getTag(), axis.getStyleValue()); 139 } 140 } 141 return nAddFontFromAssetManager(mBuilderPtr, mgr, path, cookie, isAsset, ttcIndex, weight, 142 isItalic); 143 } 144 145 private static native long nInitBuilder(String lang, int variant); 146 147 @CriticalNative 148 private static native long nCreateFamily(long mBuilderPtr); 149 150 @CriticalNative 151 private static native void nAbort(long mBuilderPtr); 152 153 @CriticalNative 154 private static native void nUnrefFamily(long nativePtr); 155 // By passing -1 to weigth argument, the weight value is resolved by OS/2 table in the font. 156 // By passing -1 to italic argument, the italic value is resolved by OS/2 table in the font. 157 private static native boolean nAddFont(long builderPtr, ByteBuffer font, int ttcIndex, 158 int weight, int isItalic); 159 private static native boolean nAddFontWeightStyle(long builderPtr, ByteBuffer font, 160 int ttcIndex, int weight, int isItalic); 161 private static native boolean nAddFontFromAssetManager(long builderPtr, AssetManager mgr, 162 String path, int cookie, boolean isAsset, int ttcIndex, int weight, int isItalic); 163 164 // The added axis values are only valid for the next nAddFont* method call. 165 @CriticalNative 166 private static native void nAddAxisValue(long builderPtr, int tag, float value); 167} 168