| 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 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 32 | /** @deprecated renderscript is deprecated in J |
| 33 | * <p>This class gives users a simple way to draw hardware accelerated text. |
| Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 34 | * Internally, the glyphs are rendered using the Freetype library and an internal cache of |
| 35 | * rendered glyph bitmaps is maintained. Each font object represents a combination of a typeface, |
| 36 | * and point size. You can create multiple font objects to represent styles such as bold or italic text, |
| 37 | * faces, and different font sizes. During creation, the Android system quieries device's screen DPI to |
| 38 | * ensure proper sizing across multiple device configurations.</p> |
| 39 | * <p>Fonts are rendered using screen-space positions and no state setup beyond binding a |
| 40 | * font to the Renderscript is required. A note of caution on performance, though the state changes |
| 41 | * are transparent to the user, they do happen internally, and it is more efficient to |
| 42 | * render large batches of text in sequence. It is also more efficient to render multiple |
| 43 | * characters at once instead of one by one to improve draw call batching.</p> |
| 44 | * <p>Font color and transparency are not part of the font object and you can freely modify |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 45 | * them in the script to suit the user's rendering needs. Font colors work as a state machine. |
| Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 46 | * Every new call to draw text uses the last color set in the script.</p> |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 47 | **/ |
| 48 | public class Font extends BaseObj { |
| 49 | |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 50 | //These help us create a font by family name |
| 51 | private static final String[] sSansNames = { |
| 52 | "sans-serif", "arial", "helvetica", "tahoma", "verdana" |
| 53 | }; |
| 54 | |
| 55 | private static final String[] sSerifNames = { |
| 56 | "serif", "times", "times new roman", "palatino", "georgia", "baskerville", |
| 57 | "goudy", "fantasy", "cursive", "ITC Stone Serif" |
| 58 | }; |
| 59 | |
| 60 | private static final String[] sMonoNames = { |
| 61 | "monospace", "courier", "courier new", "monaco" |
| 62 | }; |
| 63 | |
| 64 | private static class FontFamily { |
| 65 | String[] mNames; |
| 66 | String mNormalFileName; |
| 67 | String mBoldFileName; |
| 68 | String mItalicFileName; |
| 69 | String mBoldItalicFileName; |
| 70 | } |
| 71 | |
| 72 | private static Map<String, FontFamily> sFontFamilyMap; |
| 73 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 74 | /** @deprecated renderscript is deprecated in J |
| 75 | */ |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 76 | public enum Style { |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 77 | /** @deprecated renderscript is deprecated in J |
| 78 | */ |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 79 | NORMAL, |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 80 | /** @deprecated renderscript is deprecated in J |
| 81 | */ |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 82 | BOLD, |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 83 | /** @deprecated renderscript is deprecated in J |
| 84 | */ |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 85 | ITALIC, |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 86 | /** @deprecated renderscript is deprecated in J |
| 87 | */ |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 88 | BOLD_ITALIC; |
| 89 | } |
| 90 | |
| 91 | private static void addFamilyToMap(FontFamily family) { |
| 92 | for(int i = 0; i < family.mNames.length; i ++) { |
| 93 | sFontFamilyMap.put(family.mNames[i], family); |
| 94 | } |
| 95 | } |
| 96 | |
| 97 | private static void initFontFamilyMap() { |
| 98 | sFontFamilyMap = new HashMap<String, FontFamily>(); |
| 99 | |
| 100 | FontFamily sansFamily = new FontFamily(); |
| 101 | sansFamily.mNames = sSansNames; |
| Christian Robertson | beb2b5c | 2011-08-09 15:24:25 -0700 | [diff] [blame] | 102 | sansFamily.mNormalFileName = "Roboto-Regular.ttf"; |
| 103 | sansFamily.mBoldFileName = "Roboto-Bold.ttf"; |
| 104 | sansFamily.mItalicFileName = "Roboto-Italic.ttf"; |
| 105 | sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf"; |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 106 | addFamilyToMap(sansFamily); |
| 107 | |
| 108 | FontFamily serifFamily = new FontFamily(); |
| 109 | serifFamily.mNames = sSerifNames; |
| 110 | serifFamily.mNormalFileName = "DroidSerif-Regular.ttf"; |
| 111 | serifFamily.mBoldFileName = "DroidSerif-Bold.ttf"; |
| 112 | serifFamily.mItalicFileName = "DroidSerif-Italic.ttf"; |
| 113 | serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf"; |
| 114 | addFamilyToMap(serifFamily); |
| 115 | |
| 116 | FontFamily monoFamily = new FontFamily(); |
| 117 | monoFamily.mNames = sMonoNames; |
| 118 | monoFamily.mNormalFileName = "DroidSansMono.ttf"; |
| 119 | monoFamily.mBoldFileName = "DroidSansMono.ttf"; |
| 120 | monoFamily.mItalicFileName = "DroidSansMono.ttf"; |
| 121 | monoFamily.mBoldItalicFileName = "DroidSansMono.ttf"; |
| 122 | addFamilyToMap(monoFamily); |
| 123 | } |
| 124 | |
| 125 | static { |
| 126 | initFontFamilyMap(); |
| 127 | } |
| 128 | |
| 129 | static String getFontFileName(String familyName, Style style) { |
| 130 | FontFamily family = sFontFamilyMap.get(familyName); |
| 131 | if(family != null) { |
| 132 | switch(style) { |
| 133 | case NORMAL: |
| 134 | return family.mNormalFileName; |
| 135 | case BOLD: |
| 136 | return family.mBoldFileName; |
| 137 | case ITALIC: |
| 138 | return family.mItalicFileName; |
| 139 | case BOLD_ITALIC: |
| 140 | return family.mBoldItalicFileName; |
| 141 | } |
| 142 | } |
| 143 | // Fallback if we could not find the desired family |
| 144 | return "DroidSans.ttf"; |
| 145 | } |
| 146 | |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 147 | Font(int id, RenderScript rs) { |
| Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 148 | super(id, rs); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 151 | /** @deprecated renderscript is deprecated in J |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 152 | * Takes a specific file name as an argument |
| 153 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 154 | static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) { |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 155 | rs.validate(); |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 156 | int dpi = res.getDisplayMetrics().densityDpi; |
| 157 | int fontId = rs.nFontCreateFromFile(path, pointSize, dpi); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 158 | |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 159 | if(fontId == 0) { |
| 160 | throw new RSRuntimeException("Unable to create font from file " + path); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 161 | } |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 162 | Font rsFont = new Font(fontId, rs); |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 163 | |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 164 | return rsFont; |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 165 | } |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 166 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 167 | /** @deprecated renderscript is deprecated in J |
| 168 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 169 | static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) { |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 170 | return createFromFile(rs, res, path.getAbsolutePath(), pointSize); |
| 171 | } |
| 172 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 173 | /** @deprecated renderscript is deprecated in J |
| 174 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 175 | static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) { |
| 176 | rs.validate(); |
| 177 | AssetManager mgr = res.getAssets(); |
| 178 | int dpi = res.getDisplayMetrics().densityDpi; |
| 179 | |
| 180 | int fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi); |
| 181 | if(fontId == 0) { |
| 182 | throw new RSRuntimeException("Unable to create font from asset " + path); |
| 183 | } |
| 184 | Font rsFont = new Font(fontId, rs); |
| 185 | return rsFont; |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 186 | } |
| 187 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 188 | /** @deprecated renderscript is deprecated in J |
| 189 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 190 | static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) { |
| 191 | String name = "R." + Integer.toString(id); |
| 192 | |
| 193 | rs.validate(); |
| 194 | InputStream is = null; |
| 195 | try { |
| 196 | is = res.openRawResource(id); |
| 197 | } catch (Exception e) { |
| 198 | throw new RSRuntimeException("Unable to open resource " + id); |
| 199 | } |
| 200 | |
| 201 | int dpi = res.getDisplayMetrics().densityDpi; |
| 202 | |
| 203 | int fontId = 0; |
| 204 | if (is instanceof AssetManager.AssetInputStream) { |
| 205 | int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); |
| 206 | fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset); |
| 207 | } else { |
| 208 | throw new RSRuntimeException("Unsupported asset stream created"); |
| 209 | } |
| 210 | |
| 211 | if(fontId == 0) { |
| 212 | throw new RSRuntimeException("Unable to create font from resource " + id); |
| 213 | } |
| 214 | Font rsFont = new Font(fontId, rs); |
| 215 | return rsFont; |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 216 | } |
| 217 | |
| Alex Sakhartchouk | a0c2eb2 | 2012-04-19 16:30:58 -0700 | [diff] [blame] | 218 | /** @deprecated renderscript is deprecated in J |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 219 | * Accepts one of the following family names as an argument |
| Stephen Hines | 3d78266 | 2011-06-23 16:18:28 -0700 | [diff] [blame] | 220 | * and will attempt to produce the best match with a system font: |
| 221 | * |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 222 | * "sans-serif" "arial" "helvetica" "tahoma" "verdana" |
| 223 | * "serif" "times" "times new roman" "palatino" "georgia" "baskerville" |
| 224 | * "goudy" "fantasy" "cursive" "ITC Stone Serif" |
| 225 | * "monospace" "courier" "courier new" "monaco" |
| Stephen Hines | 3d78266 | 2011-06-23 16:18:28 -0700 | [diff] [blame] | 226 | * |
| 227 | * Returns default font if no match could be found. |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 228 | */ |
| Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 229 | 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] | 230 | String fileName = getFontFileName(familyName, fontStyle); |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 231 | String fontPath = Environment.getRootDirectory().getAbsolutePath(); |
| 232 | fontPath += "/fonts/" + fileName; |
| 233 | return createFromFile(rs, res, fontPath, pointSize); |
| Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 234 | } |
| Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 235 | |
| Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 236 | } |