blob: 18dacac3ff3b773ad3d410cc20396b785a8b48b8 [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
32/**
Robert Ly11518ac2011-02-09 13:57:06 -080033 * <p>This class gives users a simple way to draw hardware accelerated text.
34 * 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
45 * them in the script to suit the user's rendering needs. Font colors work as a state machine.
46 * 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
74 public enum Style {
75 NORMAL,
76 BOLD,
77 ITALIC,
78 BOLD_ITALIC;
79 }
80
81 private static void addFamilyToMap(FontFamily family) {
82 for(int i = 0; i < family.mNames.length; i ++) {
83 sFontFamilyMap.put(family.mNames[i], family);
84 }
85 }
86
87 private static void initFontFamilyMap() {
88 sFontFamilyMap = new HashMap<String, FontFamily>();
89
90 FontFamily sansFamily = new FontFamily();
91 sansFamily.mNames = sSansNames;
Christian Robertsonbeb2b5c2011-08-09 15:24:25 -070092 sansFamily.mNormalFileName = "Roboto-Regular.ttf";
93 sansFamily.mBoldFileName = "Roboto-Bold.ttf";
94 sansFamily.mItalicFileName = "Roboto-Italic.ttf";
95 sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070096 addFamilyToMap(sansFamily);
97
98 FontFamily serifFamily = new FontFamily();
99 serifFamily.mNames = sSerifNames;
100 serifFamily.mNormalFileName = "DroidSerif-Regular.ttf";
101 serifFamily.mBoldFileName = "DroidSerif-Bold.ttf";
102 serifFamily.mItalicFileName = "DroidSerif-Italic.ttf";
103 serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf";
104 addFamilyToMap(serifFamily);
105
106 FontFamily monoFamily = new FontFamily();
107 monoFamily.mNames = sMonoNames;
108 monoFamily.mNormalFileName = "DroidSansMono.ttf";
109 monoFamily.mBoldFileName = "DroidSansMono.ttf";
110 monoFamily.mItalicFileName = "DroidSansMono.ttf";
111 monoFamily.mBoldItalicFileName = "DroidSansMono.ttf";
112 addFamilyToMap(monoFamily);
113 }
114
115 static {
116 initFontFamilyMap();
117 }
118
119 static String getFontFileName(String familyName, Style style) {
120 FontFamily family = sFontFamilyMap.get(familyName);
121 if(family != null) {
122 switch(style) {
123 case NORMAL:
124 return family.mNormalFileName;
125 case BOLD:
126 return family.mBoldFileName;
127 case ITALIC:
128 return family.mItalicFileName;
129 case BOLD_ITALIC:
130 return family.mBoldItalicFileName;
131 }
132 }
133 // Fallback if we could not find the desired family
134 return "DroidSans.ttf";
135 }
136
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700137 Font(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700138 super(id, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700139 }
140
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700141 /**
142 * Takes a specific file name as an argument
143 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800144 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700145 rs.validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800146 int dpi = res.getDisplayMetrics().densityDpi;
147 int fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700148
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800149 if(fontId == 0) {
150 throw new RSRuntimeException("Unable to create font from file " + path);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700151 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800152 Font rsFont = new Font(fontId, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700153
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800154 return rsFont;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700155 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700156
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800157 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800158 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
159 }
160
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800161 static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) {
162 rs.validate();
163 AssetManager mgr = res.getAssets();
164 int dpi = res.getDisplayMetrics().densityDpi;
165
166 int fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
167 if(fontId == 0) {
168 throw new RSRuntimeException("Unable to create font from asset " + path);
169 }
170 Font rsFont = new Font(fontId, rs);
171 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800172 }
173
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800174 static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) {
175 String name = "R." + Integer.toString(id);
176
177 rs.validate();
178 InputStream is = null;
179 try {
180 is = res.openRawResource(id);
181 } catch (Exception e) {
182 throw new RSRuntimeException("Unable to open resource " + id);
183 }
184
185 int dpi = res.getDisplayMetrics().densityDpi;
186
187 int fontId = 0;
188 if (is instanceof AssetManager.AssetInputStream) {
189 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
190 fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset);
191 } else {
192 throw new RSRuntimeException("Unsupported asset stream created");
193 }
194
195 if(fontId == 0) {
196 throw new RSRuntimeException("Unable to create font from resource " + id);
197 }
198 Font rsFont = new Font(fontId, rs);
199 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800200 }
201
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700202 /**
203 * Accepts one of the following family names as an argument
Stephen Hines3d782662011-06-23 16:18:28 -0700204 * and will attempt to produce the best match with a system font:
205 *
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700206 * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
207 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
208 * "goudy" "fantasy" "cursive" "ITC Stone Serif"
209 * "monospace" "courier" "courier new" "monaco"
Stephen Hines3d782662011-06-23 16:18:28 -0700210 *
211 * Returns default font if no match could be found.
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700212 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800213 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700214 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800215 String fontPath = Environment.getRootDirectory().getAbsolutePath();
216 fontPath += "/fonts/" + fileName;
217 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700218 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800219
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700220}