blob: de250146289d0ef132ed903622b74086c0f467b6 [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
19import java.io.IOException;
20import java.io.InputStream;
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070021import java.util.Map;
22import java.util.HashMap;
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -070023
24import android.content.res.Resources;
25import android.content.res.AssetManager;
26import android.util.Log;
27import android.util.TypedValue;
28
29/**
30 * @hide
31 *
32 **/
33public class Font extends BaseObj {
34
Alex Sakhartchouk27f50522010-08-18 15:46:43 -070035 //These help us create a font by family name
36 private static final String[] sSansNames = {
37 "sans-serif", "arial", "helvetica", "tahoma", "verdana"
38 };
39
40 private static final String[] sSerifNames = {
41 "serif", "times", "times new roman", "palatino", "georgia", "baskerville",
42 "goudy", "fantasy", "cursive", "ITC Stone Serif"
43 };
44
45 private static final String[] sMonoNames = {
46 "monospace", "courier", "courier new", "monaco"
47 };
48
49 private static class FontFamily {
50 String[] mNames;
51 String mNormalFileName;
52 String mBoldFileName;
53 String mItalicFileName;
54 String mBoldItalicFileName;
55 }
56
57 private static Map<String, FontFamily> sFontFamilyMap;
58
59 public enum Style {
60 NORMAL,
61 BOLD,
62 ITALIC,
63 BOLD_ITALIC;
64 }
65
66 private static void addFamilyToMap(FontFamily family) {
67 for(int i = 0; i < family.mNames.length; i ++) {
68 sFontFamilyMap.put(family.mNames[i], family);
69 }
70 }
71
72 private static void initFontFamilyMap() {
73 sFontFamilyMap = new HashMap<String, FontFamily>();
74
75 FontFamily sansFamily = new FontFamily();
76 sansFamily.mNames = sSansNames;
77 sansFamily.mNormalFileName = "DroidSans.ttf";
78 sansFamily.mBoldFileName = "DroidSans-Bold.ttf";
79 sansFamily.mItalicFileName = "DroidSans.ttf";
80 sansFamily.mBoldItalicFileName = "DroidSans-Bold.ttf";
81 addFamilyToMap(sansFamily);
82
83 FontFamily serifFamily = new FontFamily();
84 serifFamily.mNames = sSerifNames;
85 serifFamily.mNormalFileName = "DroidSerif-Regular.ttf";
86 serifFamily.mBoldFileName = "DroidSerif-Bold.ttf";
87 serifFamily.mItalicFileName = "DroidSerif-Italic.ttf";
88 serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf";
89 addFamilyToMap(serifFamily);
90
91 FontFamily monoFamily = new FontFamily();
92 monoFamily.mNames = sMonoNames;
93 monoFamily.mNormalFileName = "DroidSansMono.ttf";
94 monoFamily.mBoldFileName = "DroidSansMono.ttf";
95 monoFamily.mItalicFileName = "DroidSansMono.ttf";
96 monoFamily.mBoldItalicFileName = "DroidSansMono.ttf";
97 addFamilyToMap(monoFamily);
98 }
99
100 static {
101 initFontFamilyMap();
102 }
103
104 static String getFontFileName(String familyName, Style style) {
105 FontFamily family = sFontFamilyMap.get(familyName);
106 if(family != null) {
107 switch(style) {
108 case NORMAL:
109 return family.mNormalFileName;
110 case BOLD:
111 return family.mBoldFileName;
112 case ITALIC:
113 return family.mItalicFileName;
114 case BOLD_ITALIC:
115 return family.mBoldItalicFileName;
116 }
117 }
118 // Fallback if we could not find the desired family
119 return "DroidSans.ttf";
120 }
121
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700122 Font(int id, RenderScript rs) {
Alex Sakhartchouk0de94442010-08-11 14:41:28 -0700123 super(id, rs);
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700124 }
125
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700126 /**
127 * Takes a specific file name as an argument
128 */
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700129 static public Font create(RenderScript rs, Resources res, String fileName, int size)
130 throws IllegalArgumentException {
131
132 rs.validate();
133 try {
134 int dpi = res.getDisplayMetrics().densityDpi;
135 int fontId = rs.nFontCreateFromFile(fileName, size, dpi);
136
137 if(fontId == 0) {
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700138 throw new IllegalStateException("Failed loading a font");
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700139 }
140 Font rsFont = new Font(fontId, rs);
141
142 return rsFont;
143
144 } catch (Exception e) {
145 // Ignore
146 }
147
148 return null;
149 }
Alex Sakhartchouk27f50522010-08-18 15:46:43 -0700150
151 /**
152 * Accepts one of the following family names as an argument
153 * and will attemp to produce the best match with a system font
154 * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
155 * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
156 * "goudy" "fantasy" "cursive" "ITC Stone Serif"
157 * "monospace" "courier" "courier new" "monaco"
158 * Returns default font if no match could be found
159 */
160 static public Font createFromFamily(RenderScript rs, Resources res, String familyName, Style fontStyle, int size)
161 throws IllegalArgumentException {
162 String fileName = getFontFileName(familyName, fontStyle);
163 return create(rs, res, fileName, size);
164 }
Alex Sakhartchouk9b949fc2010-06-24 17:15:34 -0700165}