blob: 0f7c24d7badc721a84aa80663e7c32e6e7a845bb [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 Sakhartchouke27cdee2010-12-17 11:41:08 -0800132 static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize)
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700133 throws IllegalArgumentException {
134
135 rs.validate();
136 try {
137 int dpi = res.getDisplayMetrics().densityDpi;
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800138 int fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700139
140 if(fontId == 0) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700141 throw new IllegalStateException("Failed loading a font");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700142 }
143 Font rsFont = new Font(fontId, rs);
144
145 return rsFont;
146
147 } catch (Exception e) {
148 // Ignore
149 }
150
151 return null;
152 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700153
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800154 static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize)
155 throws IllegalArgumentException {
156 return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
157 }
158
159 static public Font createFromAsset(RenderScript rs, Resources res, AssetManager mgr, String path, float pointSize)
160 throws IllegalArgumentException {
161 return null;
162 }
163
164 static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize)
165 throws IllegalArgumentException {
166 return null;
167 }
168
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700169 /**
170 * Accepts one of the following family names as an argument
171 * and will attemp to produce the best match with a system font
172 * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
173 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
174 * "goudy" "fantasy" "cursive" "ITC Stone Serif"
175 * "monospace" "courier" "courier new" "monaco"
176 * Returns default font if no match could be found
177 */
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800178 static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize)
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700179 throws IllegalArgumentException {
180 String fileName = getFontFileName(familyName, fontStyle);
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800181 String fontPath = Environment.getRootDirectory().getAbsolutePath();
182 fontPath += "/fonts/" + fileName;
183 return createFromFile(rs, res, fontPath, pointSize);
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700184 }
Alex Sakhartchouke27cdee2010-12-17 11:41:08 -0800185
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700186}