blob: cd1010c7e4973d62b6baaf79f1d1a3686f21d14d [file] [log] [blame]
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001/*
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
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080019import java.io.File;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070020import java.io.IOException;
21import java.io.InputStream;
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070022import java.util.HashMap;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080023import java.util.Map;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070024
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080025import android.os.Environment;
26
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070027import android.content.res.AssetManager;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080028import android.content.res.Resources;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070029import android.util.Log;
30import android.util.TypedValue;
31
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070032/** @deprecated renderscript is deprecated in J
33 * <p>This class gives users a simple way to draw hardware accelerated text.
Robert Ly11518ac2011-02-09 13:57:06 -080034 * 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 Sakhartchouka0c2eb22012-04-19 16:30:58 -070045 * them in the script to suit the user's rendering needs. Font colors work as a state machine.
Robert Ly11518ac2011-02-09 13:57:06 -080046 * Every new call to draw text uses the last color set in the script.</p>
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070047 **/
48public class Font extends BaseObj {
49
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070050 //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 Sakhartchouka0c2eb22012-04-19 16:30:58 -070074 /** @deprecated renderscript is deprecated in J
75 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070076 public enum Style {
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070077 /** @deprecated renderscript is deprecated in J
78 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070079 NORMAL,
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070080 /** @deprecated renderscript is deprecated in J
81 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070082 BOLD,
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070083 /** @deprecated renderscript is deprecated in J
84 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070085 ITALIC,
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -070086 /** @deprecated renderscript is deprecated in J
87 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070088 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 Robertsonbeb2b5c2011-08-09 15:24:25 -0700102 sansFamily.mNormalFileName = "Roboto-Regular.ttf";
103 sansFamily.mBoldFileName = "Roboto-Bold.ttf";
104 sansFamily.mItalicFileName = "Roboto-Italic.ttf";
105 sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700106 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 Sakhartchouk9b949fc2010-06-24 17:15:34 -0700147 Font(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700148 super(id, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700149 }
150
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700151 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700152 * Takes a specific file name as an argument
153 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800154 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700155 rs.validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800156 int dpi = res.getDisplayMetrics().densityDpi;
157 int fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700158
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800159 if(fontId == 0) {
160 throw new RSRuntimeException("Unable to create font from file " + path);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700161 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800162 Font rsFont = new Font(fontId, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700163
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800164 return rsFont;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700165 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700166
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700167 /** @deprecated renderscript is deprecated in J
168 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800169 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800170 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
171 }
172
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700173 /** @deprecated renderscript is deprecated in J
174 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800175 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 Sakhartchouke27cdee2010-12-17 11:41:08 -0800186 }
187
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700188 /** @deprecated renderscript is deprecated in J
189 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800190 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 Sakhartchouke27cdee2010-12-17 11:41:08 -0800216 }
217
Alex Sakhartchouka0c2eb22012-04-19 16:30:58 -0700218 /** @deprecated renderscript is deprecated in J
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700219 * Accepts one of the following family names as an argument
Stephen Hines3d782662011-06-23 16:18:28 -0700220 * and will attempt to produce the best match with a system font:
221 *
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700222 * "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 Hines3d782662011-06-23 16:18:28 -0700226 *
227 * Returns default font if no match could be found.
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700228 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800229 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700230 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800231 String fontPath = Environment.getRootDirectory().getAbsolutePath();
232 fontPath += "/fonts/" + fileName;
233 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700234 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800235
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700236}