blob: 252ffc115c44330024287fcf60a5fa47e1374349 [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/**
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070033 *
34 **/
35public class Font extends BaseObj {
36
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070037 //These help us create a font by family name
38 private static final String[] sSansNames = {
39 "sans-serif", "arial", "helvetica", "tahoma", "verdana"
40 };
41
42 private static final String[] sSerifNames = {
43 "serif", "times", "times new roman", "palatino", "georgia", "baskerville",
44 "goudy", "fantasy", "cursive", "ITC Stone Serif"
45 };
46
47 private static final String[] sMonoNames = {
48 "monospace", "courier", "courier new", "monaco"
49 };
50
51 private static class FontFamily {
52 String[] mNames;
53 String mNormalFileName;
54 String mBoldFileName;
55 String mItalicFileName;
56 String mBoldItalicFileName;
57 }
58
59 private static Map<String, FontFamily> sFontFamilyMap;
60
61 public enum Style {
62 NORMAL,
63 BOLD,
64 ITALIC,
65 BOLD_ITALIC;
66 }
67
68 private static void addFamilyToMap(FontFamily family) {
69 for(int i = 0; i < family.mNames.length; i ++) {
70 sFontFamilyMap.put(family.mNames[i], family);
71 }
72 }
73
74 private static void initFontFamilyMap() {
75 sFontFamilyMap = new HashMap<String, FontFamily>();
76
77 FontFamily sansFamily = new FontFamily();
78 sansFamily.mNames = sSansNames;
79 sansFamily.mNormalFileName = "DroidSans.ttf";
80 sansFamily.mBoldFileName = "DroidSans-Bold.ttf";
81 sansFamily.mItalicFileName = "DroidSans.ttf";
82 sansFamily.mBoldItalicFileName = "DroidSans-Bold.ttf";
83 addFamilyToMap(sansFamily);
84
85 FontFamily serifFamily = new FontFamily();
86 serifFamily.mNames = sSerifNames;
87 serifFamily.mNormalFileName = "DroidSerif-Regular.ttf";
88 serifFamily.mBoldFileName = "DroidSerif-Bold.ttf";
89 serifFamily.mItalicFileName = "DroidSerif-Italic.ttf";
90 serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf";
91 addFamilyToMap(serifFamily);
92
93 FontFamily monoFamily = new FontFamily();
94 monoFamily.mNames = sMonoNames;
95 monoFamily.mNormalFileName = "DroidSansMono.ttf";
96 monoFamily.mBoldFileName = "DroidSansMono.ttf";
97 monoFamily.mItalicFileName = "DroidSansMono.ttf";
98 monoFamily.mBoldItalicFileName = "DroidSansMono.ttf";
99 addFamilyToMap(monoFamily);
100 }
101
102 static {
103 initFontFamilyMap();
104 }
105
106 static String getFontFileName(String familyName, Style style) {
107 FontFamily family = sFontFamilyMap.get(familyName);
108 if(family != null) {
109 switch(style) {
110 case NORMAL:
111 return family.mNormalFileName;
112 case BOLD:
113 return family.mBoldFileName;
114 case ITALIC:
115 return family.mItalicFileName;
116 case BOLD_ITALIC:
117 return family.mBoldItalicFileName;
118 }
119 }
120 // Fallback if we could not find the desired family
121 return "DroidSans.ttf";
122 }
123
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700124 Font(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700125 super(id, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700126 }
127
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700128 /**
129 * Takes a specific file name as an argument
130 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800131 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700132 rs.validate();
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800133 int dpi = res.getDisplayMetrics().densityDpi;
134 int fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700135
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800136 if(fontId == 0) {
137 throw new RSRuntimeException("Unable to create font from file " + path);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700138 }
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800139 Font rsFont = new Font(fontId, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700140
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800141 return rsFont;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700142 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700143
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800144 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800145 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
146 }
147
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800148 static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) {
149 rs.validate();
150 AssetManager mgr = res.getAssets();
151 int dpi = res.getDisplayMetrics().densityDpi;
152
153 int fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
154 if(fontId == 0) {
155 throw new RSRuntimeException("Unable to create font from asset " + path);
156 }
157 Font rsFont = new Font(fontId, rs);
158 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800159 }
160
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800161 static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) {
162 String name = "R." + Integer.toString(id);
163
164 rs.validate();
165 InputStream is = null;
166 try {
167 is = res.openRawResource(id);
168 } catch (Exception e) {
169 throw new RSRuntimeException("Unable to open resource " + id);
170 }
171
172 int dpi = res.getDisplayMetrics().densityDpi;
173
174 int fontId = 0;
175 if (is instanceof AssetManager.AssetInputStream) {
176 int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
177 fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset);
178 } else {
179 throw new RSRuntimeException("Unsupported asset stream created");
180 }
181
182 if(fontId == 0) {
183 throw new RSRuntimeException("Unable to create font from resource " + id);
184 }
185 Font rsFont = new Font(fontId, rs);
186 return rsFont;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800187 }
188
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700189 /**
190 * Accepts one of the following family names as an argument
191 * and will attemp to produce the best match with a system font
192 * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
193 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
194 * "goudy" "fantasy" "cursive" "ITC Stone Serif"
195 * "monospace" "courier" "courier new" "monaco"
196 * Returns default font if no match could be found
197 */
Alex Sakhartchoukb0253ea2011-01-07 11:12:08 -0800198 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700199 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800200 String fontPath = Environment.getRootDirectory().getAbsolutePath();
201 fontPath += "/fonts/" + fileName;
202 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700203 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800204
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700205}