1 /*
2  * Copyright (C) 2008-2012 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 
17 package android.renderscript;
18 
19 import android.compat.annotation.UnsupportedAppUsage;
20 import android.content.res.AssetManager;
21 import android.content.res.Resources;
22 import android.os.Build;
23 import android.os.Environment;
24 
25 import java.io.File;
26 import java.io.InputStream;
27 import java.util.HashMap;
28 import java.util.Map;
29 
30 /**
31  * @hide
32  * @deprecated in API 16
33  * <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>
47  **/
48 @Deprecated
49 public class Font extends BaseObj {
50 
51     //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 
75     /**
76      * @deprecated in API 16
77      */
78     public enum Style {
79         /**
80          * @deprecated in API 16
81          */
82         NORMAL,
83         /**
84          * @deprecated in API 16
85          */
86         BOLD,
87         /**
88          * @deprecated in API 16
89          */
90         @UnsupportedAppUsage
91         ITALIC,
92         /**
93          * @deprecated in API 16
94          */
95         BOLD_ITALIC;
96     }
97 
addFamilyToMap(FontFamily family)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 
initFontFamilyMap()104     private static void initFontFamilyMap() {
105         sFontFamilyMap = new HashMap<String, FontFamily>();
106 
107         FontFamily sansFamily = new FontFamily();
108         sansFamily.mNames = sSansNames;
109         sansFamily.mNormalFileName = "Roboto-Regular.ttf";
110         sansFamily.mBoldFileName = "Roboto-Bold.ttf";
111         sansFamily.mItalicFileName = "Roboto-Italic.ttf";
112         sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
113         addFamilyToMap(sansFamily);
114 
115         FontFamily serifFamily = new FontFamily();
116         serifFamily.mNames = sSerifNames;
117         serifFamily.mNormalFileName = "NotoSerif-Regular.ttf";
118         serifFamily.mBoldFileName = "NotoSerif-Bold.ttf";
119         serifFamily.mItalicFileName = "NotoSerif-Italic.ttf";
120         serifFamily.mBoldItalicFileName = "NotoSerif-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 {
initFontFamilyMap()133         initFontFamilyMap();
134     }
135 
getFontFileName(String familyName, Style style)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 
Font(long id, RenderScript rs)154     Font(long id, RenderScript rs) {
155         super(id, rs);
156         guard.open("destroy");
157     }
158 
159     /**
160      * @deprecated in API 16
161      * Takes a specific file name as an argument
162      */
createFromFile(RenderScript rs, Resources res, String path, float pointSize)163     static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
164         rs.validate();
165         int dpi = res.getDisplayMetrics().densityDpi;
166         long fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
167 
168         if(fontId == 0) {
169             throw new RSRuntimeException("Unable to create font from file " + path);
170         }
171         Font rsFont = new Font(fontId, rs);
172 
173         return rsFont;
174     }
175 
176     /**
177      * @deprecated in API 16
178      */
createFromFile(RenderScript rs, Resources res, File path, float pointSize)179     static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
180         return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
181     }
182 
183     /**
184      * @deprecated in API 16
185      */
createFromAsset(RenderScript rs, Resources res, String path, float pointSize)186     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 
191         long fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
192         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;
197     }
198 
199     /**
200      * @deprecated in API 16
201      */
createFromResource(RenderScript rs, Resources res, int id, float pointSize)202     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 
215         long fontId = 0;
216         if (is instanceof AssetManager.AssetInputStream) {
217             long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
218             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;
228     }
229 
230     /**
231      * @deprecated in API 16
232      * Accepts one of the following family names as an argument
233      * and will attempt to produce the best match with a system font:
234      *
235      * "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"
239      *
240      * Returns default font if no match could be found.
241      */
242     @UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.R, trackingBug = 170729553)
create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize)243     static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
244         String fileName = getFontFileName(familyName, fontStyle);
245         String fontPath = Environment.getRootDirectory().getAbsolutePath();
246         fontPath += "/fonts/" + fileName;
247         return createFromFile(rs, res, fontPath, pointSize);
248     }
249 
250 }
251