Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 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 | |
| 17 | package android.renderscript; |
| 18 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 19 | import java.io.File; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 22 | import java.util.HashMap; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 23 | import java.util.Map; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 24 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 25 | import android.os.Environment; |
| 26 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 27 | import android.content.res.AssetManager; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 28 | import android.content.res.Resources; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | import android.util.TypedValue; |
| 31 | |
| 32 | /** |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 33 | * |
| 34 | **/ |
| 35 | public class Font extends BaseObj { |
| 36 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 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 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 124 | Font(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 125 | super(id, rs); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 128 | /** |
| 129 | * Takes a specific file name as an argument |
| 130 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 131 | static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 132 | rs.validate(); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 133 | int dpi = res.getDisplayMetrics().densityDpi; |
| 134 | int fontId = rs.nFontCreateFromFile(path, pointSize, dpi); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 135 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 136 | if(fontId == 0) { |
| 137 | throw new RSRuntimeException("Unable to create font from file " + path); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 138 | } |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 139 | Font rsFont = new Font(fontId, rs); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 140 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 141 | return rsFont; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 142 | } |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 143 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 144 | static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 145 | return createFromFile(rs, res, path.getAbsolutePath(), pointSize); |
| 146 | } |
| 147 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 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; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 159 | } |
| 160 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 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; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 187 | } |
| 188 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 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 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 198 | static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) { |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 199 | String fileName = getFontFileName(familyName, fontStyle); |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 200 | String fontPath = Environment.getRootDirectory().getAbsolutePath(); |
| 201 | fontPath += "/fonts/" + fileName; |
| 202 | return createFromFile(rs, res, fontPath, pointSize); |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 203 | } |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 204 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 205 | } |