blob: d5ca31e934187de98634825e0a1643fedba690f9 [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.InputStream;
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070021import java.util.HashMap;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080022import java.util.Map;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070023
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080024import android.os.Environment;
25
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070026import android.content.res.AssetManager;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080027import android.content.res.Resources;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070028
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070029/**
Tim Murraya9084222013-04-05 22:06:43 +000030 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070031 * @deprecated in API 16
Tim Murraya9084222013-04-05 22:06:43 +000032 * <p>This class gives users a simple way to draw hardware accelerated text.
Robert Ly11518ac2011-02-09 13:57:06 -080033 * Internally, the glyphs are rendered using the Freetype library and an internal cache of
34 * rendered glyph bitmaps is maintained. Each font object represents a combination of a typeface,
35 * and point size. You can create multiple font objects to represent styles such as bold or italic text,
36 * faces, and different font sizes. During creation, the Android system quieries device's screen DPI to
37 * ensure proper sizing across multiple device configurations.</p>
38 * <p>Fonts are rendered using screen-space positions and no state setup beyond binding a
Tim Murrayc11e25c2013-04-09 11:01:01 -070039 * font to the RenderScript is required. A note of caution on performance, though the state changes
Robert Ly11518ac2011-02-09 13:57:06 -080040 * are transparent to the user, they do happen internally, and it is more efficient to
41 * render large batches of text in sequence. It is also more efficient to render multiple
42 * characters at once instead of one by one to improve draw call batching.</p>
43 * <p>Font color and transparency are not part of the font object and you can freely modify
Tim Murraya9084222013-04-05 22:06:43 +000044 * 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 -080045 * Every new call to draw text uses the last color set in the script.</p>
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070046 **/
47public class Font extends BaseObj {
48
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070049 //These help us create a font by family name
50 private static final String[] sSansNames = {
51 "sans-serif", "arial", "helvetica", "tahoma", "verdana"
52 };
53
54 private static final String[] sSerifNames = {
55 "serif", "times", "times new roman", "palatino", "georgia", "baskerville",
56 "goudy", "fantasy", "cursive", "ITC Stone Serif"
57 };
58
59 private static final String[] sMonoNames = {
60 "monospace", "courier", "courier new", "monaco"
61 };
62
63 private static class FontFamily {
64 String[] mNames;
65 String mNormalFileName;
66 String mBoldFileName;
67 String mItalicFileName;
68 String mBoldItalicFileName;
69 }
70
71 private static Map<String, FontFamily> sFontFamilyMap;
72
Jason Sams65c80f82012-05-08 17:30:26 -070073 /**
74 * @deprecated in API 16
75 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070076 public enum Style {
Jason Sams65c80f82012-05-08 17:30:26 -070077 /**
78 * @deprecated in API 16
79 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070080 NORMAL,
Jason Sams65c80f82012-05-08 17:30:26 -070081 /**
82 * @deprecated in API 16
83 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070084 BOLD,
Jason Sams65c80f82012-05-08 17:30:26 -070085 /**
86 * @deprecated in API 16
87 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070088 ITALIC,
Jason Sams65c80f82012-05-08 17:30:26 -070089 /**
90 * @deprecated in API 16
91 */
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070092 BOLD_ITALIC;
93 }
94
95 private static void addFamilyToMap(FontFamily family) {
96 for(int i = 0; i < family.mNames.length; i ++) {
97 sFontFamilyMap.put(family.mNames[i], family);
98 }
99 }
100
101 private static void initFontFamilyMap() {
102 sFontFamilyMap = new HashMap<String, FontFamily>();
103
104 FontFamily sansFamily = new FontFamily();
105 sansFamily.mNames = sSansNames;
Christian Robertsonbeb2b5c2011-08-09 15:24:25 -0700106 sansFamily.mNormalFileName = "Roboto-Regular.ttf";
107 sansFamily.mBoldFileName = "Roboto-Bold.ttf";
108 sansFamily.mItalicFileName = "Roboto-Italic.ttf";
109 sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700110 addFamilyToMap(sansFamily);
111
112 FontFamily serifFamily = new FontFamily();
113 serifFamily.mNames = sSerifNames;
Stephen Hines6f09d082014-06-11 17:57:16 -0700114 serifFamily.mNormalFileName = "NotoSerif-Regular.ttf";
115 serifFamily.mBoldFileName = "NotoSerif-Bold.ttf";
116 serifFamily.mItalicFileName = "NotoSerif-Italic.ttf";
117 serifFamily.mBoldItalicFileName = "NotoSerif-BoldItalic.ttf";
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700118 addFamilyToMap(serifFamily);
119
120 FontFamily monoFamily = new FontFamily();
121 monoFamily.mNames = sMonoNames;
122 monoFamily.mNormalFileName = "DroidSansMono.ttf";
123 monoFamily.mBoldFileName = "DroidSansMono.ttf";
124 monoFamily.mItalicFileName = "DroidSansMono.ttf";
125 monoFamily.mBoldItalicFileName = "DroidSansMono.ttf";
126 addFamilyToMap(monoFamily);
127 }
128
129 static {
130 initFontFamilyMap();
131 }
132
133 static String getFontFileName(String familyName, Style style) {
134 FontFamily family = sFontFamilyMap.get(familyName);
135 if(family != null) {
136 switch(style) {
137 case NORMAL:
138 return family.mNormalFileName;
139 case BOLD:
140 return family.mBoldFileName;
141 case ITALIC:
142 return family.mItalicFileName;
143 case BOLD_ITALIC:
144 return family.mBoldItalicFileName;
145 }
146 }
147 // Fallback if we could not find the desired family
148 return "DroidSans.ttf";
149 }
150
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000151 Font(long id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700152 super(id, rs);
Yang Ni6484b6b2016-03-24 09:40:32 -0700153 guard.open("destroy");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700154 }
155
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700156 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700157 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700158 * Takes a specific file name as an argument
159 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800160 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700161 rs.validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800162 int dpi = res.getDisplayMetrics().densityDpi;
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000163 long fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700164
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800165 if(fontId == 0) {
166 throw new RSRuntimeException("Unable to create font from file " + path);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700167 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800168 Font rsFont = new Font(fontId, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700169
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800170 return rsFont;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700171 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700172
Jason Sams65c80f82012-05-08 17:30:26 -0700173 /**
174 * @deprecated in API 16
175 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800176 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800177 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
178 }
179
Jason Sams65c80f82012-05-08 17:30:26 -0700180 /**
181 * @deprecated in API 16
182 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800183 static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) {
184 rs.validate();
185 AssetManager mgr = res.getAssets();
186 int dpi = res.getDisplayMetrics().densityDpi;
187
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000188 long fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800189 if(fontId == 0) {
190 throw new RSRuntimeException("Unable to create font from asset " + path);
191 }
192 Font rsFont = new Font(fontId, rs);
193 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800194 }
195
Jason Sams65c80f82012-05-08 17:30:26 -0700196 /**
197 * @deprecated in API 16
198 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800199 static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) {
200 String name = "R." + Integer.toString(id);
201
202 rs.validate();
203 InputStream is = null;
204 try {
205 is = res.openRawResource(id);
206 } catch (Exception e) {
207 throw new RSRuntimeException("Unable to open resource " + id);
208 }
209
210 int dpi = res.getDisplayMetrics().densityDpi;
211
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000212 long fontId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800213 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000214 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800215 fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset);
216 } else {
217 throw new RSRuntimeException("Unsupported asset stream created");
218 }
219
220 if(fontId == 0) {
221 throw new RSRuntimeException("Unable to create font from resource " + id);
222 }
223 Font rsFont = new Font(fontId, rs);
224 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800225 }
226
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700227 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700228 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700229 * Accepts one of the following family names as an argument
Stephen Hines3d782662011-06-23 16:18:28 -0700230 * and will attempt to produce the best match with a system font:
231 *
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700232 * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
233 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
234 * "goudy" "fantasy" "cursive" "ITC Stone Serif"
235 * "monospace" "courier" "courier new" "monaco"
Stephen Hines3d782662011-06-23 16:18:28 -0700236 *
237 * Returns default font if no match could be found.
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700238 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800239 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700240 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800241 String fontPath = Environment.getRootDirectory().getAbsolutePath();
242 fontPath += "/fonts/" + fileName;
243 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700244 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800245
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700246}