Home | History | Annotate | Download | only in renderscript
      1 /*
      2  * Copyright (C) 2008-2012 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 
     19 import java.io.File;
     20 import java.io.IOException;
     21 import java.io.InputStream;
     22 import java.util.HashMap;
     23 import java.util.Map;
     24 
     25 import android.os.Environment;
     26 
     27 import android.content.res.AssetManager;
     28 import android.content.res.Resources;
     29 import android.util.Log;
     30 import android.util.TypedValue;
     31 
     32 /**
     33  * @hide
     34  * @deprecated in API 16
     35  * <p>This class gives users a simple way to draw hardware accelerated text.
     36  * Internally, the glyphs are rendered using the Freetype library and an internal cache of
     37  * rendered glyph bitmaps is maintained. Each font object represents a combination of a typeface,
     38  * and point size. You can create multiple font objects to represent styles such as bold or italic text,
     39  * faces, and different font sizes. During creation, the Android system quieries device's screen DPI to
     40  * ensure proper sizing across multiple device configurations.</p>
     41  * <p>Fonts are rendered using screen-space positions and no state setup beyond binding a
     42  * font to the RenderScript is required. A note of caution on performance, though the state changes
     43  * are transparent to the user, they do happen internally, and it is more efficient to
     44  * render large batches of text in sequence. It is also more efficient to render multiple
     45  * characters at once instead of one by one to improve draw call batching.</p>
     46  * <p>Font color and transparency are not part of the font object and you can freely modify
     47  * them in the script to suit the user's rendering needs. Font colors work as a state machine.
     48  * Every new call to draw text uses the last color set in the script.</p>
     49  **/
     50 public class Font extends BaseObj {
     51 
     52     //These help us create a font by family name
     53     private static final String[] sSansNames = {
     54         "sans-serif", "arial", "helvetica", "tahoma", "verdana"
     55     };
     56 
     57     private static final String[] sSerifNames = {
     58         "serif", "times", "times new roman", "palatino", "georgia", "baskerville",
     59         "goudy", "fantasy", "cursive", "ITC Stone Serif"
     60     };
     61 
     62     private static final String[] sMonoNames = {
     63         "monospace", "courier", "courier new", "monaco"
     64     };
     65 
     66     private static class FontFamily {
     67         String[] mNames;
     68         String mNormalFileName;
     69         String mBoldFileName;
     70         String mItalicFileName;
     71         String mBoldItalicFileName;
     72     }
     73 
     74     private static Map<String, FontFamily> sFontFamilyMap;
     75 
     76     /**
     77      * @deprecated in API 16
     78      */
     79     public enum Style {
     80         /**
     81          * @deprecated in API 16
     82          */
     83         NORMAL,
     84         /**
     85          * @deprecated in API 16
     86          */
     87         BOLD,
     88         /**
     89          * @deprecated in API 16
     90          */
     91         ITALIC,
     92         /**
     93          * @deprecated in API 16
     94          */
     95         BOLD_ITALIC;
     96     }
     97 
     98     private static void addFamilyToMap(FontFamily family) {
     99         for(int i = 0; i < family.mNames.length; i ++) {
    100             sFontFamilyMap.put(family.mNames[i], family);
    101         }
    102     }
    103 
    104     private static void initFontFamilyMap() {
    105         sFontFamilyMap = new HashMap<String, FontFamily>();
    106 
    107         FontFamily sansFamily = new FontFamily();
    108         sansFamily.mNames = sSansNames;
    109         sansFamily.mNormalFileName = "Roboto-Regular.ttf";
    110         sansFamily.mBoldFileName = "Roboto-Bold.ttf";
    111         sansFamily.mItalicFileName = "Roboto-Italic.ttf";
    112         sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
    113         addFamilyToMap(sansFamily);
    114 
    115         FontFamily serifFamily = new FontFamily();
    116         serifFamily.mNames = sSerifNames;
    117         serifFamily.mNormalFileName = "DroidSerif-Regular.ttf";
    118         serifFamily.mBoldFileName = "DroidSerif-Bold.ttf";
    119         serifFamily.mItalicFileName = "DroidSerif-Italic.ttf";
    120         serifFamily.mBoldItalicFileName = "DroidSerif-BoldItalic.ttf";
    121         addFamilyToMap(serifFamily);
    122 
    123         FontFamily monoFamily = new FontFamily();
    124         monoFamily.mNames = sMonoNames;
    125         monoFamily.mNormalFileName = "DroidSansMono.ttf";
    126         monoFamily.mBoldFileName = "DroidSansMono.ttf";
    127         monoFamily.mItalicFileName = "DroidSansMono.ttf";
    128         monoFamily.mBoldItalicFileName = "DroidSansMono.ttf";
    129         addFamilyToMap(monoFamily);
    130     }
    131 
    132     static {
    133         initFontFamilyMap();
    134     }
    135 
    136     static String getFontFileName(String familyName, Style style) {
    137         FontFamily family = sFontFamilyMap.get(familyName);
    138         if(family != null) {
    139             switch(style) {
    140                 case NORMAL:
    141                     return family.mNormalFileName;
    142                 case BOLD:
    143                     return family.mBoldFileName;
    144                 case ITALIC:
    145                     return family.mItalicFileName;
    146                 case BOLD_ITALIC:
    147                     return family.mBoldItalicFileName;
    148             }
    149         }
    150         // Fallback if we could not find the desired family
    151         return "DroidSans.ttf";
    152     }
    153 
    154     Font(int id, RenderScript rs) {
    155         super(id, rs);
    156     }
    157 
    158     /**
    159      * @deprecated in API 16
    160      * Takes a specific file name as an argument
    161      */
    162     static public Font createFromFile(RenderScript rs, Resources res, String path, float pointSize) {
    163         rs.validate();
    164         int dpi = res.getDisplayMetrics().densityDpi;
    165         int fontId = rs.nFontCreateFromFile(path, pointSize, dpi);
    166 
    167         if(fontId == 0) {
    168             throw new RSRuntimeException("Unable to create font from file " + path);
    169         }
    170         Font rsFont = new Font(fontId, rs);
    171 
    172         return rsFont;
    173     }
    174 
    175     /**
    176      * @deprecated in API 16
    177      */
    178     static public Font createFromFile(RenderScript rs, Resources res, File path, float pointSize) {
    179         return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
    180     }
    181 
    182     /**
    183      * @deprecated in API 16
    184      */
    185     static public Font createFromAsset(RenderScript rs, Resources res, String path, float pointSize) {
    186         rs.validate();
    187         AssetManager mgr = res.getAssets();
    188         int dpi = res.getDisplayMetrics().densityDpi;
    189 
    190         int fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
    191         if(fontId == 0) {
    192             throw new RSRuntimeException("Unable to create font from asset " + path);
    193         }
    194         Font rsFont = new Font(fontId, rs);
    195         return rsFont;
    196     }
    197 
    198     /**
    199      * @deprecated in API 16
    200      */
    201     static public Font createFromResource(RenderScript rs, Resources res, int id, float pointSize) {
    202         String name = "R." + Integer.toString(id);
    203 
    204         rs.validate();
    205         InputStream is = null;
    206         try {
    207             is = res.openRawResource(id);
    208         } catch (Exception e) {
    209             throw new RSRuntimeException("Unable to open resource " + id);
    210         }
    211 
    212         int dpi = res.getDisplayMetrics().densityDpi;
    213 
    214         int fontId = 0;
    215         if (is instanceof AssetManager.AssetInputStream) {
    216             int asset = ((AssetManager.AssetInputStream) is).getAssetInt();
    217             fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset);
    218         } else {
    219             throw new RSRuntimeException("Unsupported asset stream created");
    220         }
    221 
    222         if(fontId == 0) {
    223             throw new RSRuntimeException("Unable to create font from resource " + id);
    224         }
    225         Font rsFont = new Font(fontId, rs);
    226         return rsFont;
    227     }
    228 
    229     /**
    230      * @deprecated in API 16
    231      * Accepts one of the following family names as an argument
    232      * and will attempt to produce the best match with a system font:
    233      *
    234      * "sans-serif" "arial" "helvetica" "tahoma" "verdana"
    235      * "serif" "times" "times new roman" "palatino" "georgia" "baskerville"
    236      * "goudy" "fantasy" "cursive" "ITC Stone Serif"
    237      * "monospace" "courier" "courier new" "monaco"
    238      *
    239      * Returns default font if no match could be found.
    240      */
    241     static public Font create(RenderScript rs, Resources res, String familyName, Style fontStyle, float pointSize) {
    242         String fileName = getFontFileName(familyName, fontStyle);
    243         String fontPath = Environment.getRootDirectory().getAbsolutePath();
    244         fontPath += "/fonts/" + fileName;
    245         return createFromFile(rs, res, fontPath, pointSize);
    246     }
    247 
    248 }
    249