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