Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1 | /* |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 2 | * Copyright (C) 2008-2012 The Android Open Source Project |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 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.InputStream; |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 21 | import java.util.HashMap; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 22 | import java.util.Map; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 23 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 24 | import android.os.Environment; |
| 25 | |
Mathew Inwood | f0c90b1 | 2018-08-01 10:05:11 +0100 | [diff] [blame] | 26 | import android.annotation.UnsupportedAppUsage; |
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 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 30 | /** |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 31 | * @hide |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 32 | * @deprecated in API 16 |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [diff] [blame] | 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 |
Tim Murray | c11e25c | 2013-04-09 11:01:01 -0700 | [diff] [blame] | 40 | * font to the RenderScript is required. A note of caution on performance, though the state changes |
Robert Ly | 11518ac | 2011-02-09 13:57:06 -0800 | [diff] [blame] | 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 |
Tim Murray | a908422 | 2013-04-05 22:06:43 +0000 | [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 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 74 | /** |
| 75 | * @deprecated in API 16 |
| 76 | */ |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 77 | public enum Style { |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 78 | /** |
| 79 | * @deprecated in API 16 |
| 80 | */ |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 81 | NORMAL, |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 82 | /** |
| 83 | * @deprecated in API 16 |
| 84 | */ |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 85 | BOLD, |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 86 | /** |
| 87 | * @deprecated in API 16 |
| 88 | */ |
Mathew Inwood | f0c90b1 | 2018-08-01 10:05:11 +0100 | [diff] [blame] | 89 | @UnsupportedAppUsage |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 90 | ITALIC, |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 91 | /** |
| 92 | * @deprecated in API 16 |
| 93 | */ |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 94 | BOLD_ITALIC; |
| 95 | } |
| 96 | |
| 97 | private static void addFamilyToMap(FontFamily family) { |
| 98 | for(int i = 0; i < family.mNames.length; i ++) { |
| 99 | sFontFamilyMap.put(family.mNames[i], family); |
| 100 | } |
| 101 | } |
| 102 | |
| 103 | private static void initFontFamilyMap() { |
| 104 | sFontFamilyMap = new HashMap<String, FontFamily>(); |
| 105 | |
| 106 | FontFamily sansFamily = new FontFamily(); |
| 107 | sansFamily.mNames = sSansNames; |
Christian Robertson | beb2b5c | 2011-08-09 15:24:25 -0700 | [diff] [blame] | 108 | sansFamily.mNormalFileName = "Roboto-Regular.ttf"; |
| 109 | sansFamily.mBoldFileName = "Roboto-Bold.ttf"; |
| 110 | sansFamily.mItalicFileName = "Roboto-Italic.ttf"; |
| 111 | sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf"; |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 112 | addFamilyToMap(sansFamily); |
| 113 | |
| 114 | FontFamily serifFamily = new FontFamily(); |
| 115 | serifFamily.mNames = sSerifNames; |
Stephen Hines | 6f09d08 | 2014-06-11 17:57:16 -0700 | [diff] [blame] | 116 | serifFamily.mNormalFileName = "NotoSerif-Regular.ttf"; |
| 117 | serifFamily.mBoldFileName = "NotoSerif-Bold.ttf"; |
| 118 | serifFamily.mItalicFileName = "NotoSerif-Italic.ttf"; |
| 119 | serifFamily.mBoldItalicFileName = "NotoSerif-BoldItalic.ttf"; |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 120 | addFamilyToMap(serifFamily); |
| 121 | |
| 122 | FontFamily monoFamily = new FontFamily(); |
| 123 | monoFamily.mNames = sMonoNames; |
| 124 | monoFamily.mNormalFileName = "DroidSansMono.ttf"; |
| 125 | monoFamily.mBoldFileName = "DroidSansMono.ttf"; |
| 126 | monoFamily.mItalicFileName = "DroidSansMono.ttf"; |
| 127 | monoFamily.mBoldItalicFileName = "DroidSansMono.ttf"; |
| 128 | addFamilyToMap(monoFamily); |
| 129 | } |
| 130 | |
| 131 | static { |
| 132 | initFontFamilyMap(); |
| 133 | } |
| 134 | |
| 135 | static String getFontFileName(String familyName, Style style) { |
| 136 | FontFamily family = sFontFamilyMap.get(familyName); |
| 137 | if(family != null) { |
| 138 | switch(style) { |
| 139 | case NORMAL: |
| 140 | return family.mNormalFileName; |
| 141 | case BOLD: |
| 142 | return family.mBoldFileName; |
| 143 | case ITALIC: |
| 144 | return family.mItalicFileName; |
| 145 | case BOLD_ITALIC: |
| 146 | return family.mBoldItalicFileName; |
| 147 | } |
| 148 | } |
| 149 | // Fallback if we could not find the desired family |
| 150 | return "DroidSans.ttf"; |
| 151 | } |
| 152 | |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 153 | Font(long id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 154 | super(id, rs); |
Yang Ni | eb4dd08 | 2016-03-24 09:40:32 -0700 | [diff] [blame] | 155 | guard.open("destroy"); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 158 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 159 | * @deprecated in API 16 |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 160 | * Takes a specific file name as an argument |
| 161 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 162 | static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 163 | rs.validate(); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 164 | int dpi = res.getDisplayMetrics().densityDpi; |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 165 | long fontId = rs.nFontCreateFromFile(path, pointSize, dpi); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 166 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 167 | if(fontId == 0) { |
| 168 | throw new RSRuntimeException("Unable to create font from file " + path); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 169 | } |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 170 | Font rsFont = new Font(fontId, rs); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 171 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 172 | return rsFont; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 173 | } |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 174 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 175 | /** |
| 176 | * @deprecated in API 16 |
| 177 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 178 | static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 179 | return createFromFile(rs, res, path.getAbsolutePath(), pointSize); |
| 180 | } |
| 181 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 182 | /** |
| 183 | * @deprecated in API 16 |
| 184 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 185 | static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) { |
| 186 | rs.validate(); |
| 187 | AssetManager mgr = res.getAssets(); |
| 188 | int dpi = res.getDisplayMetrics().densityDpi; |
| 189 | |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 190 | long fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 191 | if(fontId == 0) { |
| 192 | throw new RSRuntimeException("Unable to create font from asset " + path); |
| 193 | } |
| 194 | Font rsFont = new Font(fontId, rs); |
| 195 | return rsFont; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 196 | } |
| 197 | |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 198 | /** |
| 199 | * @deprecated in API 16 |
| 200 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 201 | static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) { |
| 202 | String name = "R." + Integer.toString(id); |
| 203 | |
| 204 | rs.validate(); |
| 205 | InputStream is = null; |
| 206 | try { |
| 207 | is = res.openRawResource(id); |
| 208 | } catch (Exception e) { |
| 209 | throw new RSRuntimeException("Unable to open resource " + id); |
| 210 | } |
| 211 | |
| 212 | int dpi = res.getDisplayMetrics().densityDpi; |
| 213 | |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 214 | long fontId = 0; |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 215 | if (is instanceof AssetManager.AssetInputStream) { |
Ashok Bhat | 0e0c088 | 2014-02-04 14:57:58 +0000 | [diff] [blame] | 216 | long asset = ((AssetManager.AssetInputStream) is).getNativeAsset(); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 217 | fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset); |
| 218 | } else { |
| 219 | throw new RSRuntimeException("Unsupported asset stream created"); |
| 220 | } |
| 221 | |
| 222 | if(fontId == 0) { |
| 223 | throw new RSRuntimeException("Unable to create font from resource " + id); |
| 224 | } |
| 225 | Font rsFont = new Font(fontId, rs); |
| 226 | return rsFont; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 227 | } |
| 228 | |
Stephen Hines | 9c9ad3f8c2 | 2012-05-07 15:34:29 -0700 | [diff] [blame] | 229 | /** |
Jason Sams | 65c80f8 | 2012-05-08 17:30:26 -0700 | [diff] [blame] | 230 | * @deprecated in API 16 |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 231 | * Accepts one of the following family names as an argument |
Stephen Hines | 3d78266 | 2011-06-23 16:18:28 -0700 | [diff] [blame] | 232 | * and will attempt to produce the best match with a system font: |
| 233 | * |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 234 | * "sans-serif" "arial" "helvetica" "tahoma" "verdana" |
| 235 | * "serif" "times" "times new roman" "palatino" "georgia" "baskerville" |
| 236 | * "goudy" "fantasy" "cursive" "ITC Stone Serif" |
| 237 | * "monospace" "courier" "courier new" "monaco" |
Stephen Hines | 3d78266 | 2011-06-23 16:18:28 -0700 | [diff] [blame] | 238 | * |
| 239 | * Returns default font if no match could be found. |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 240 | */ |
Mathew Inwood | f0c90b1 | 2018-08-01 10:05:11 +0100 | [diff] [blame] | 241 | @UnsupportedAppUsage |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame] | 242 | 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] | 243 | String fileName = getFontFileName(familyName, fontStyle); |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 244 | String fontPath = Environment.getRootDirectory().getAbsolutePath(); |
| 245 | fontPath += "/fonts/" + fileName; |
| 246 | return createFromFile(rs, res, fontPath, pointSize); |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 247 | } |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 248 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 249 | } |