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