blob: cfd11c0e6f58423e3d4eaad1f3af045dce3cb6b9 [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/**
Tim Murraya9084222013-04-05 22:06:43 +000033 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070034 * @deprecated in API 16
Tim Murraya9084222013-04-05 22:06:43 +000035 * <p>This class gives users a simple way to draw hardware accelerated text.
Robert Ly11518ac2011-02-09 13:57:06 -080036 * Internally, the glyphs are rendered using the Freetype library and an internal cache of
37 * rendered glyph bitmaps is maintained. Each font object represents a combination of a typeface,
38 * and point size. You can create multiple font objects to represent styles such as bold or italic text,
39 * faces, and different font sizes. During creation, the Android system quieries device's screen DPI to
40 * ensure proper sizing across multiple device configurations.</p>
41 * <p>Fonts are rendered using screen-space positions and no state setup beyond binding a
Tim Murrayc11e25c2013-04-09 11:01:01 -070042 * font to the RenderScript is required. A note of caution on performance, though the state changes
Robert Ly11518ac2011-02-09 13:57:06 -080043 * are transparent to the user, they do happen internally, and it is more efficient to
44 * render large batches of text in sequence. It is also more efficient to render multiple
45 * characters at once instead of one by one to improve draw call batching.</p>
46 * <p>Font color and transparency are not part of the font object and you can freely modify
Tim Murraya9084222013-04-05 22:06:43 +000047 * 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 -080048 * Every new call to draw text uses the last color set in the script.</p>
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070049 **/
50public class Font extends BaseObj {
51
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070052 //These help us create a font by family name
53 private static final String[] sSansNames = {
54 "sans-serif", "arial", "helvetica", "tahoma", "verdana"
55 };
56
57 private static final String[] sSerifNames = {
58 "serif", "times", "times new roman", "palatino", "georgia", "baskerville",
59 "goudy", "fantasy", "cursive", "ITC Stone Serif"
60 };
61
62 private static final String[] sMonoNames = {
63 "monospace", "courier", "courier new", "monaco"
64 };
65
66 private static class FontFamily {
67 String[] mNames;
68 String mNormalFileName;
69 String mBoldFileName;
70 String mItalicFileName;
71 String mBoldItalicFileName;
72 }
73
74 private static Map<String, FontFamily> sFontFamilyMap;
75
Jason Sams65c80f82012-05-08 17:30:26 -070076 /**
77 * @deprecated in API 16
78 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070079 public enum Style {
Jason Sams65c80f82012-05-08 17:30:26 -070080 /**
81 * @deprecated in API 16
82 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070083 NORMAL,
Jason Sams65c80f82012-05-08 17:30:26 -070084 /**
85 * @deprecated in API 16
86 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070087 BOLD,
Jason Sams65c80f82012-05-08 17:30:26 -070088 /**
89 * @deprecated in API 16
90 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070091 ITALIC,
Jason Sams65c80f82012-05-08 17:30:26 -070092 /**
93 * @deprecated in API 16
94 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070095 BOLD_ITALIC;
96 }
97
98 private static void addFamilyToMap(FontFamily family) {
99 for(int i = 0; i < family.mNames.length; i ++) {
100 sFontFamilyMap.put(family.mNames[i], family);
101 }
102 }
103
104 private static void initFontFamilyMap() {
105 sFontFamilyMap = new HashMap<String, FontFamily>();
106
107 FontFamily sansFamily = new FontFamily();
108 sansFamily.mNames = sSansNames;
Christian Robertsonbeb2b5c2011-08-09 15:24:25 -0700109 sansFamily.mNormalFileName = "Roboto-Regular.ttf";
110 sansFamily.mBoldFileName = "Roboto-Bold.ttf";
111 sansFamily.mItalicFileName = "Roboto-Italic.ttf";
112 sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700113 addFamilyToMap(sansFamily);
114
115 FontFamily serifFamily = new FontFamily();
116 serifFamily.mNames = sSerifNames;
117 serifFamily.mNormalFileName = "DroidSerif-Regular.ttf";
118 serifFamily.mBoldFileName = "DroidSerif-Bold.ttf";
119 serifFamily.mItalicFileName = "DroidSerif-Italic.ttf";
120 serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf";
121 addFamilyToMap(serifFamily);
122
123 FontFamily monoFamily = new FontFamily();
124 monoFamily.mNames = sMonoNames;
125 monoFamily.mNormalFileName = "DroidSansMono.ttf";
126 monoFamily.mBoldFileName = "DroidSansMono.ttf";
127 monoFamily.mItalicFileName = "DroidSansMono.ttf";
128 monoFamily.mBoldItalicFileName = "DroidSansMono.ttf";
129 addFamilyToMap(monoFamily);
130 }
131
132 static {
133 initFontFamilyMap();
134 }
135
136 static String getFontFileName(String familyName, Style style) {
137 FontFamily family = sFontFamilyMap.get(familyName);
138 if(family != null) {
139 switch(style) {
140 case NORMAL:
141 return family.mNormalFileName;
142 case BOLD:
143 return family.mBoldFileName;
144 case ITALIC:
145 return family.mItalicFileName;
146 case BOLD_ITALIC:
147 return family.mBoldItalicFileName;
148 }
149 }
150 // Fallback if we could not find the desired family
151 return "DroidSans.ttf";
152 }
153
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000154 Font(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700155 super(id, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700156 }
157
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700158 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700159 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700160 * Takes a specific file name as an argument
161 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800162 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700163 rs.validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800164 int dpi = res.getDisplayMetrics().densityDpi;
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000165 long fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700166
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800167 if(fontId == 0) {
168 throw new RSRuntimeException("Unable to create font from file " + path);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700169 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800170 Font rsFont = new Font(fontId, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700171
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800172 return rsFont;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700173 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700174
Jason Sams65c80f82012-05-08 17:30:26 -0700175 /**
176 * @deprecated in API 16
177 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800178 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800179 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
180 }
181
Jason Sams65c80f82012-05-08 17:30:26 -0700182 /**
183 * @deprecated in API 16
184 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800185 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 Bhat0e0c0882014-02-04 14:57:58 +0000190 long fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800191 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 Sakhartchouke27cdee2010-12-17 11:41:08 -0800196 }
197
Jason Sams65c80f82012-05-08 17:30:26 -0700198 /**
199 * @deprecated in API 16
200 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800201 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 Bhat0e0c0882014-02-04 14:57:58 +0000214 long fontId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800215 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000216 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800217 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 Sakhartchouke27cdee2010-12-17 11:41:08 -0800227 }
228
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700229 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700230 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700231 * Accepts one of the following family names as an argument
Stephen Hines3d782662011-06-23 16:18:28 -0700232 * and will attempt to produce the best match with a system font:
233 *
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700234 * "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 Hines3d782662011-06-23 16:18:28 -0700238 *
239 * Returns default font if no match could be found.
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700240 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800241 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700242 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800243 String fontPath = Environment.getRootDirectory().getAbsolutePath();
244 fontPath += "/fonts/" + fileName;
245 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700246 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800247
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700248}