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