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