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 | /** |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 33 | * <p>This class gives users a simple way to draw hardware accelerated text. |
| 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 |
| 45 | * them in the script to suit the user's rendering needs. Font colors work as a state machine. |
| 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 | |
| 74 | public enum Style { |
| 75 | NORMAL, |
| 76 | BOLD, |
| 77 | ITALIC, |
| 78 | BOLD_ITALIC; |
| 79 | } |
| 80 | |
| 81 | private static void addFamilyToMap(FontFamily family) { |
| 82 | for(int i = 0; i < family.mNames.length; i ++) { |
| 83 | sFontFamilyMap.put(family.mNames[i], family); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | private static void initFontFamilyMap() { |
| 88 | sFontFamilyMap = new HashMap<String, FontFamily>(); |
| 89 | |
| 90 | FontFamily sansFamily = new FontFamily(); |
| 91 | sansFamily.mNames = sSansNames; |
Christian Robertson | beb2b5c | 2011-08-09 15:24:25 -0700 | [diff] [blame^] | 92 | sansFamily.mNormalFileName = "Roboto-Regular.ttf"; |
| 93 | sansFamily.mBoldFileName = "Roboto-Bold.ttf"; |
| 94 | sansFamily.mItalicFileName = "Roboto-Italic.ttf"; |
| 95 | sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf"; |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 96 | addFamilyToMap(sansFamily); |
| 97 | |
| 98 | FontFamily serifFamily = new FontFamily(); |
| 99 | serifFamily.mNames = sSerifNames; |
| 100 | serifFamily.mNormalFileName = "DroidSerif-Regular.ttf"; |
| 101 | serifFamily.mBoldFileName = "DroidSerif-Bold.ttf"; |
| 102 | serifFamily.mItalicFileName = "DroidSerif-Italic.ttf"; |
| 103 | serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf"; |
| 104 | addFamilyToMap(serifFamily); |
| 105 | |
| 106 | FontFamily monoFamily = new FontFamily(); |
| 107 | monoFamily.mNames = sMonoNames; |
| 108 | monoFamily.mNormalFileName = "DroidSansMono.ttf"; |
| 109 | monoFamily.mBoldFileName = "DroidSansMono.ttf"; |
| 110 | monoFamily.mItalicFileName = "DroidSansMono.ttf"; |
| 111 | monoFamily.mBoldItalicFileName = "DroidSansMono.ttf"; |
| 112 | addFamilyToMap(monoFamily); |
| 113 | } |
| 114 | |
| 115 | static { |
| 116 | initFontFamilyMap(); |
| 117 | } |
| 118 | |
| 119 | static String getFontFileName(String familyName, Style style) { |
| 120 | FontFamily family = sFontFamilyMap.get(familyName); |
| 121 | if(family != null) { |
| 122 | switch(style) { |
| 123 | case NORMAL: |
| 124 | return family.mNormalFileName; |
| 125 | case BOLD: |
| 126 | return family.mBoldFileName; |
| 127 | case ITALIC: |
| 128 | return family.mItalicFileName; |
| 129 | case BOLD_ITALIC: |
| 130 | return family.mBoldItalicFileName; |
| 131 | } |
| 132 | } |
| 133 | // Fallback if we could not find the desired family |
| 134 | return "DroidSans.ttf"; |
| 135 | } |
| 136 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 137 | Font(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 138 | super(id, rs); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 141 | /** |
| 142 | * Takes a specific file name as an argument |
| 143 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 144 | static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 145 | rs.validate(); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 146 | int dpi = res.getDisplayMetrics().densityDpi; |
| 147 | int fontId = rs.nFontCreateFromFile(path, pointSize, dpi); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 148 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 149 | if(fontId == 0) { |
| 150 | throw new RSRuntimeException("Unable to create font from file " + path); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 151 | } |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 152 | Font rsFont = new Font(fontId, rs); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 153 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 154 | return rsFont; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 155 | } |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 156 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 157 | static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 158 | return createFromFile(rs, res, path.getAbsolutePath(), pointSize); |
| 159 | } |
| 160 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 161 | static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) { |
| 162 | rs.validate(); |
| 163 | AssetManager mgr = res.getAssets(); |
| 164 | int dpi = res.getDisplayMetrics().densityDpi; |
| 165 | |
| 166 | int fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi); |
| 167 | if(fontId == 0) { |
| 168 | throw new RSRuntimeException("Unable to create font from asset " + path); |
| 169 | } |
| 170 | Font rsFont = new Font(fontId, rs); |
| 171 | return rsFont; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 172 | } |
| 173 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 174 | static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) { |
| 175 | String name = "R." + Integer.toString(id); |
| 176 | |
| 177 | rs.validate(); |
| 178 | InputStream is = null; |
| 179 | try { |
| 180 | is = res.openRawResource(id); |
| 181 | } catch (Exception e) { |
| 182 | throw new RSRuntimeException("Unable to open resource " + id); |
| 183 | } |
| 184 | |
| 185 | int dpi = res.getDisplayMetrics().densityDpi; |
| 186 | |
| 187 | int fontId = 0; |
| 188 | if (is instanceof AssetManager.AssetInputStream) { |
| 189 | int asset = ((AssetManager.AssetInputStream) is).getAssetInt(); |
| 190 | fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset); |
| 191 | } else { |
| 192 | throw new RSRuntimeException("Unsupported asset stream created"); |
| 193 | } |
| 194 | |
| 195 | if(fontId == 0) { |
| 196 | throw new RSRuntimeException("Unable to create font from resource " + id); |
| 197 | } |
| 198 | Font rsFont = new Font(fontId, rs); |
| 199 | return rsFont; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 200 | } |
| 201 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 202 | /** |
| 203 | * Accepts one of the following family names as an argument |
Stephen Hines | 3d78266 | 2011-06-23 16:18:28 -0700 | [diff] [blame] | 204 | * and will attempt to produce the best match with a system font: |
| 205 | * |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 206 | * "sans-serif" "arial" "helvetica" "tahoma" "verdana" |
| 207 | * "serif" "times" "times new roman" "palatino" "georgia" "baskerville" |
| 208 | * "goudy" "fantasy" "cursive" "ITC Stone Serif" |
| 209 | * "monospace" "courier" "courier new" "monaco" |
Stephen Hines | 3d78266 | 2011-06-23 16:18:28 -0700 | [diff] [blame] | 210 | * |
| 211 | * Returns default font if no match could be found. |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 212 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 213 | 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] | 214 | String fileName = getFontFileName(familyName, fontStyle); |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 215 | String fontPath = Environment.getRootDirectory().getAbsolutePath(); |
| 216 | fontPath += "/fonts/" + fileName; |
| 217 | return createFromFile(rs, res, fontPath, pointSize); |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 218 | } |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 219 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 220 | } |