blob: 6f6f341d78a6518e92bfe946a0a5067255d8c52a [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
Artur Satayev2ebb31c2020-01-08 12:24:36 +000019import android.compat.annotation.UnsupportedAppUsage;
20import android.content.res.AssetManager;
21import android.content.res.Resources;
Mathew Inwood8e742f92020-10-27 11:47:29 +000022import android.os.Build;
Artur Satayev2ebb31c2020-01-08 12:24:36 +000023import android.os.Environment;
24
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080025import java.io.File;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070026import java.io.InputStream;
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070027import java.util.HashMap;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -080028import java.util.Map;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070029
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -070030/**
Tim Murraya9084222013-04-05 22:06:43 +000031 * @hide
Jason Sams65c80f82012-05-08 17:30:26 -070032 * @deprecated in API 16
Tim Murraya9084222013-04-05 22:06:43 +000033 * <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
Tim Murrayc11e25c2013-04-09 11:01:01 -070040 * font to the RenderScript is required. A note of caution on performance, though the state changes
Robert Ly11518ac2011-02-09 13:57:06 -080041 * 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 Murraya9084222013-04-05 22:06:43 +000045 * 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 **/
Xusong Wang8b4548c2021-01-05 10:09:52 -080048@Deprecated
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070049public 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 */
Mathew Inwood15324472018-08-06 11:18:49 +010090 @UnsupportedAppUsage
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;
Stephen Hines6f09d082014-06-11 17:57:16 -0700117 serifFamily.mNormalFileName = "NotoSerif-Regular.ttf";
118 serifFamily.mBoldFileName = "NotoSerif-Bold.ttf";
119 serifFamily.mItalicFileName = "NotoSerif-Italic.ttf";
120 serifFamily.mBoldItalicFileName = "NotoSerif-BoldItalic.ttf";
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700121 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);
Yang Nieb4dd082016-03-24 09:40:32 -0700156 guard.open("destroy");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700157 }
158
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700159 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700160 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700161 * Takes a specific file name as an argument
162 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800163 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700164 rs.validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800165 int dpi = res.getDisplayMetrics().densityDpi;
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000166 long fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700167
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800168 if(fontId == 0) {
169 throw new RSRuntimeException("Unable to create font from file " + path);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700170 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800171 Font rsFont = new Font(fontId, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700172
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800173 return rsFont;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700174 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700175
Jason Sams65c80f82012-05-08 17:30:26 -0700176 /**
177 * @deprecated in API 16
178 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800179 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800180 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
181 }
182
Jason Sams65c80f82012-05-08 17:30:26 -0700183 /**
184 * @deprecated in API 16
185 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800186 static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) {
187 rs.validate();
188 AssetManager mgr = res.getAssets();
189 int dpi = res.getDisplayMetrics().densityDpi;
190
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000191 long fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800192 if(fontId == 0) {
193 throw new RSRuntimeException("Unable to create font from asset " + path);
194 }
195 Font rsFont = new Font(fontId, rs);
196 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800197 }
198
Jason Sams65c80f82012-05-08 17:30:26 -0700199 /**
200 * @deprecated in API 16
201 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800202 static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) {
203 String name = "R." + Integer.toString(id);
204
205 rs.validate();
206 InputStream is = null;
207 try {
208 is = res.openRawResource(id);
209 } catch (Exception e) {
210 throw new RSRuntimeException("Unable to open resource " + id);
211 }
212
213 int dpi = res.getDisplayMetrics().densityDpi;
214
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000215 long fontId = 0;
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800216 if (is instanceof AssetManager.AssetInputStream) {
Ashok Bhat0e0c0882014-02-04 14:57:58 +0000217 long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800218 fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset);
219 } else {
220 throw new RSRuntimeException("Unsupported asset stream created");
221 }
222
223 if(fontId == 0) {
224 throw new RSRuntimeException("Unable to create font from resource " + id);
225 }
226 Font rsFont = new Font(fontId, rs);
227 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800228 }
229
Stephen Hines9c9ad3f8c22012-05-07 15:34:29 -0700230 /**
Jason Sams65c80f82012-05-08 17:30:26 -0700231 * @deprecated in API 16
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700232 * Accepts one of the following family names as an argument
Stephen Hines3d782662011-06-23 16:18:28 -0700233 * and will attempt to produce the best match with a system font:
234 *
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700235 * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
236 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
237 * "goudy" "fantasy" "cursive" "ITC Stone Serif"
238 * "monospace" "courier" "courier new" "monaco"
Stephen Hines3d782662011-06-23 16:18:28 -0700239 *
240 * Returns default font if no match could be found.
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700241 */
Mathew Inwood8e742f92020-10-27 11:47:29 +0000242 @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800243 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700244 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800245 String fontPath = Environment.getRootDirectory().getAbsolutePath();
246 fontPath += "/fonts/" + fileName;
247 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700248 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800249
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700250}