Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package android.renderscript; |
| 18 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 19 | import java.io.File; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 20 | import java.io.IOException; |
| 21 | import java.io.InputStream; |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 22 | import java.util.HashMap; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 23 | import java.util.Map; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 24 | |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 25 | import android.os.Environment; |
| 26 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 27 | import android.content.res.AssetManager; |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 28 | import android.content.res.Resources; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 29 | import android.util.Log; |
| 30 | import android.util.TypedValue; |
| 31 | |
| 32 | /** |
| 33 | * @hide |
| 34 | * |
| 35 | **/ |
| 36 | public class Font extends BaseObj { |
| 37 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 38 | //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 Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 125 | Font(int id, RenderScript rs) { |
Alex Sakhartchouk | 0de9444 | 2010-08-11 14:41:28 -0700 | [diff] [blame] | 126 | super(id, rs); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 127 | } |
| 128 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 129 | /** |
| 130 | * Takes a specific file name as an argument |
| 131 | */ |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 132 | static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) { |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 133 | rs.validate(); |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 134 | int dpi = res.getDisplayMetrics().densityDpi; |
| 135 | int fontId = rs.nFontCreateFromFile(path, pointSize, dpi); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 136 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 137 | if(fontId == 0) { |
| 138 | throw new RSRuntimeException("Unable to create font from file " + path); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 139 | } |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 140 | Font rsFont = new Font(fontId, rs); |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 141 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 142 | return rsFont; |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 143 | } |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 144 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 145 | static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) { |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 146 | return createFromFile(rs, res, path.getAbsolutePath(), pointSize); |
| 147 | } |
| 148 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 149 | 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 Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 160 | } |
| 161 | |
Alex Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 162 | 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 Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 188 | } |
| 189 | |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 190 | /** |
| 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 Sakhartchouk | b0253ea | 2011-01-07 11:12:08 -0800 | [diff] [blame^] | 199 | static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) { |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 200 | String fileName = getFontFileName(familyName, fontStyle); |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 201 | String fontPath = Environment.getRootDirectory().getAbsolutePath(); |
| 202 | fontPath += "/fonts/" + fileName; |
| 203 | return createFromFile(rs, res, fontPath, pointSize); |
Alex Sakhartchouk | 27f5052 | 2010-08-18 15:46:43 -0700 | [diff] [blame] | 204 | } |
Alex Sakhartchouk | e27cdee | 2010-12-17 11:41:08 -0800 | [diff] [blame] | 205 | |
Alex Sakhartchouk | 9b949fc | 2010-06-24 17:15:34 -0700 | [diff] [blame] | 206 | } |