blob: 8a49abb50fd19188a3d19625fee7bf0e69eaf1a6 [file] [log] [blame]
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07001/*
Jason Sams65c80f82012-05-08 17:30:26 -07002 * Copyright (C) 2008-2012 The Android Open Source Project
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -07003 *
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
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070032/**
Jason Sams65c80f82012-05-08 17:30:26 -070033 * @deprecated in API 16
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070034 * <p>This class gives users a simple way to draw hardware accelerated text.
Robert Ly11518ac2011-02-09 13:57:06 -080035 * Internally, the glyphs are rendered using the Freetype library and an internal cache of
36 * rendered glyph bitmaps is maintained. Each font object represents a combination of a typeface,
37 * and point size. You can create multiple font objects to represent styles such as bold or italic text,
38 * faces, and different font sizes. During creation, the Android system quieries device's screen DPI to
39 * ensure proper sizing across multiple device configurations.</p>
40 * <p>Fonts are rendered using screen-space positions and no state setup beyond binding a
41 * font to the Renderscript is required. A note of caution on performance, though the state changes
42 * are transparent to the user, they do happen internally, and it is more efficient to
43 * render large batches of text in sequence. It is also more efficient to render multiple
44 * characters at once instead of one by one to improve draw call batching.</p>
45 * <p>Font color and transparency are not part of the font object and you can freely modify
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070046 * 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 -080047 * Every new call to draw text uses the last color set in the script.</p>
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070048 **/
49public class Font extends BaseObj {
50
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070051 //These help us create a font by family name
52 private static final String[] sSansNames = {
53 "sans-serif", "arial", "helvetica", "tahoma", "verdana"
54 };
55
56 private static final String[] sSerifNames = {
57 "serif", "times", "times new roman", "palatino", "georgia", "baskerville",
58 "goudy", "fantasy", "cursive", "ITC Stone Serif"
59 };
60
61 private static final String[] sMonoNames = {
62 "monospace", "courier", "courier new", "monaco"
63 };
64
65 private static class FontFamily {
66 String[] mNames;
67 String mNormalFileName;
68 String mBoldFileName;
69 String mItalicFileName;
70 String mBoldItalicFileName;
71 }
72
73 private static Map<String, FontFamily> sFontFamilyMap;
74
Jason Sams65c80f82012-05-08 17:30:26 -070075 /**
76 * @deprecated in API 16
77 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070078 public enum Style {
Jason Sams65c80f82012-05-08 17:30:26 -070079 /**
80 * @deprecated in API 16
81 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070082 NORMAL,
Jason Sams65c80f82012-05-08 17:30:26 -070083 /**
84 * @deprecated in API 16
85 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070086 BOLD,
Jason Sams65c80f82012-05-08 17:30:26 -070087 /**
88 * @deprecated in API 16
89 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070090 ITALIC,
Jason Sams65c80f82012-05-08 17:30:26 -070091 /**
92 * @deprecated in API 16
93 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070094 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 Robertsonbeb2b5c2011-08-09 15:24:25 -0700108 sansFamily.mNormalFileName = "Roboto-Regular.ttf";
109 sansFamily.mBoldFileName = "Roboto-Bold.ttf";
110 sansFamily.mItalicFileName = "Roboto-Italic.ttf";
111 sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700112 addFamilyToMap(sansFamily);
113
114 FontFamily serifFamily = new FontFamily();
115 serifFamily.mNames = sSerifNames;
116 serifFamily.mNormalFileName = "DroidSerif-Regular.ttf";
117 serifFamily.mBoldFileName = "DroidSerif-Bold.ttf";
118 serifFamily.mItalicFileName = "DroidSerif-Italic.ttf";
119 serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf";
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
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700153 Font(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700154 super(id, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700155 }
156
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700157 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700158 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700159 * Takes a specific file name as an argument
160 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800161 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700162 rs.validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800163 int dpi = res.getDisplayMetrics().densityDpi;
164 int fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700165
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800166 if(fontId == 0) {
167 throw new RSRuntimeException("Unable to create font from file " + path);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700168 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800169 Font rsFont = new Font(fontId, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700170
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800171 return rsFont;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700172 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700173
Jason Sams65c80f82012-05-08 17:30:26 -0700174 /**
175 * @deprecated in API 16
176 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800177 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800178 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
179 }
180
Jason Sams65c80f82012-05-08 17:30:26 -0700181 /**
182 * @deprecated in API 16
183 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800184 static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) {
185 rs.validate();
186 AssetManager mgr = res.getAssets();
187 int dpi = res.getDisplayMetrics().densityDpi;
188
189 int fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
190 if(fontId == 0) {
191 throw new RSRuntimeException("Unable to create font from asset " + path);
192 }
193 Font rsFont = new Font(fontId, rs);
194 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800195 }
196
Jason Sams65c80f82012-05-08 17:30:26 -0700197 /**
198 * @deprecated in API 16
199 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800200 static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) {
201 String name = "R." + Integer.toString(id);
202
203 rs.validate();
204 InputStream is = null;
205 try {
206 is = res.openRawResource(id);
207 } catch (Exception e) {
208 throw new RSRuntimeException("Unable to open resource " + id);
209 }
210
211 int dpi = res.getDisplayMetrics().densityDpi;
212
213 int fontId = 0;
214 if (is instanceof AssetManager.AssetInputStream) {
215 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
216 fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset);
217 } else {
218 throw new RSRuntimeException("Unsupported asset stream created");
219 }
220
221 if(fontId == 0) {
222 throw new RSRuntimeException("Unable to create font from resource " + id);
223 }
224 Font rsFont = new Font(fontId, rs);
225 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800226 }
227
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700228 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700229 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700230 * Accepts one of the following family names as an argument
Stephen Hines3d782662011-06-23 16:18:28 -0700231 * and will attempt to produce the best match with a system font:
232 *
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700233 * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
234 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
235 * "goudy" "fantasy" "cursive" "ITC Stone Serif"
236 * "monospace" "courier" "courier new" "monaco"
Stephen Hines3d782662011-06-23 16:18:28 -0700237 *
238 * Returns default font if no match could be found.
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700239 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800240 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700241 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800242 String fontPath = Environment.getRootDirectory().getAbsolutePath();
243 fontPath += "/fonts/" + fileName;
244 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700245 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800246
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700247}