Font.java revision b11e3d2b0edb03a5e3ea535d58b0cbe2d920ed16
1/* 2 * Copyright (C) 2008 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.renderscript; 18 19import java.io.File; 20import java.io.IOException; 21import java.io.InputStream; 22import java.util.HashMap; 23import java.util.Map; 24 25import android.os.Environment; 26 27import android.content.res.AssetManager; 28import android.content.res.Resources; 29import android.util.Log; 30import android.util.TypedValue; 31 32/** 33 * 34 **/ 35public class Font extends BaseObj { 36 37 //These help us create a font by family name 38 private static final String[] sSansNames = { 39 "sans-serif", "arial", "helvetica", "tahoma", "verdana" 40 }; 41 42 private static final String[] sSerifNames = { 43 "serif", "times", "times new roman", "palatino", "georgia", "baskerville", 44 "goudy", "fantasy", "cursive", "ITC Stone Serif" 45 }; 46 47 private static final String[] sMonoNames = { 48 "monospace", "courier", "courier new", "monaco" 49 }; 50 51 private static class FontFamily { 52 String[] mNames; 53 String mNormalFileName; 54 String mBoldFileName; 55 String mItalicFileName; 56 String mBoldItalicFileName; 57 } 58 59 private static Map<String, FontFamily> sFontFamilyMap; 60 61 public enum Style { 62 NORMAL, 63 BOLD, 64 ITALIC, 65 BOLD_ITALIC; 66 } 67 68 private static void addFamilyToMap(FontFamily family) { 69 for(int i = 0; i < family.mNames.length; i ++) { 70 sFontFamilyMap.put(family.mNames[i], family); 71 } 72 } 73 74 private static void initFontFamilyMap() { 75 sFontFamilyMap = new HashMap<String, FontFamily>(); 76 77 FontFamily sansFamily = new FontFamily(); 78 sansFamily.mNames = sSansNames; 79 sansFamily.mNormalFileName = "DroidSans.ttf"; 80 sansFamily.mBoldFileName = "DroidSans-Bold.ttf"; 81 sansFamily.mItalicFileName = "DroidSans.ttf"; 82 sansFamily.mBoldItalicFileName = "DroidSans-Bold.ttf"; 83 addFamilyToMap(sansFamily); 84 85 FontFamily serifFamily = new FontFamily(); 86 serifFamily.mNames = sSerifNames; 87 serifFamily.mNormalFileName = "DroidSerif-Regular.ttf"; 88 serifFamily.mBoldFileName = "DroidSerif-Bold.ttf"; 89 serifFamily.mItalicFileName = "DroidSerif-Italic.ttf"; 90 serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf"; 91 addFamilyToMap(serifFamily); 92 93 FontFamily monoFamily = new FontFamily(); 94 monoFamily.mNames = sMonoNames; 95 monoFamily.mNormalFileName = "DroidSansMono.ttf"; 96 monoFamily.mBoldFileName = "DroidSansMono.ttf"; 97 monoFamily.mItalicFileName = "DroidSansMono.ttf"; 98 monoFamily.mBoldItalicFileName = "DroidSansMono.ttf"; 99 addFamilyToMap(monoFamily); 100 } 101 102 static { 103 initFontFamilyMap(); 104 } 105 106 static String getFontFileName(String familyName, Style style) { 107 FontFamily family = sFontFamilyMap.get(familyName); 108 if(family != null) { 109 switch(style) { 110 case NORMAL: 111 return family.mNormalFileName; 112 case BOLD: 113 return family.mBoldFileName; 114 case ITALIC: 115 return family.mItalicFileName; 116 case BOLD_ITALIC: 117 return family.mBoldItalicFileName; 118 } 119 } 120 // Fallback if we could not find the desired family 121 return "DroidSans.ttf"; 122 } 123 124 Font(int id, RenderScript rs) { 125 super(id, rs); 126 } 127 128 /** 129 * Takes a specific file name as an argument 130 */ 131 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) { 132 rs.validate(); 133 int dpi = res.getDisplayMetrics().densityDpi; 134 int fontId = rs.nFontCreateFromFile(path, pointSize, dpi); 135 136 if(fontId == 0) { 137 throw new RSRuntimeException("Unable to create font from file " + path); 138 } 139 Font rsFont = new Font(fontId, rs); 140 141 return rsFont; 142 } 143 144 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) { 145 return createFromFile(rs, res, path.getAbsolutePath(), pointSize); 146 } 147 148 static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) { 149 rs.validate(); 150 AssetManager mgr = res.getAssets(); 151 int dpi = res.getDisplayMetrics().densityDpi; 152 153 int fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi); 154 if(fontId == 0) { 155 throw new RSRuntimeException("Unable to create font from asset " + path); 156 } 157 Font rsFont = new Font(fontId, rs); 158 return rsFont; 159 } 160 161 static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) { 162 String name = "R." + Integer.toString(id); 163 164 rs.validate(); 165 InputStream is = null; 166 try { 167 is = res.openRawResource(id); 168 } catch (Exception e) { 169 throw new RSRuntimeException("Unable to open resource " + id); 170 } 171 172 int dpi = res.getDisplayMetrics().densityDpi; 173 174 int fontId = 0; 175 if (is instanceof AssetManager.AssetInputStream) { 176 int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); 177 fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset); 178 } else { 179 throw new RSRuntimeException("Unsupported asset stream created"); 180 } 181 182 if(fontId == 0) { 183 throw new RSRuntimeException("Unable to create font from resource " + id); 184 } 185 Font rsFont = new Font(fontId, rs); 186 return rsFont; 187 } 188 189 /** 190 * Accepts one of the following family names as an argument 191 * and will attemp to produce the best match with a system font 192 * "sans-serif" "arial" "helvetica" "tahoma" "verdana" 193 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville" 194 * "goudy" "fantasy" "cursive" "ITC Stone Serif" 195 * "monospace" "courier" "courier new" "monaco" 196 * Returns default font if no match could be found 197 */ 198 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) { 199 String fileName = getFontFileName(familyName, fontStyle); 200 String fontPath = Environment.getRootDirectory().getAbsolutePath(); 201 fontPath += "/fonts/" + fileName; 202 return createFromFile(rs, res, fontPath, pointSize); 203 } 204 205} 206