FileDocCategorySizeDatePackage
Font.javaAPI DocAndroid 5.1 API8665Thu Mar 12 22:22:42 GMT 2015android.renderscript

Font

public class Font extends BaseObj
hide
deprecated
in API 16

This class gives users a simple way to draw hardware accelerated text. Internally, the glyphs are rendered using the Freetype library and an internal cache of rendered glyph bitmaps is maintained. Each font object represents a combination of a typeface, and point size. You can create multiple font objects to represent styles such as bold or italic text, faces, and different font sizes. During creation, the Android system quieries device's screen DPI to ensure proper sizing across multiple device configurations.

Fonts are rendered using screen-space positions and no state setup beyond binding a font to the RenderScript is required. A note of caution on performance, though the state changes are transparent to the user, they do happen internally, and it is more efficient to render large batches of text in sequence. It is also more efficient to render multiple characters at once instead of one by one to improve draw call batching.

Font color and transparency are not part of the font object and you can freely modify them in the script to suit the user's rendering needs. Font colors work as a state machine. Every new call to draw text uses the last color set in the script.

Fields Summary
private static final String[]
sSansNames
private static final String[]
sSerifNames
private static final String[]
sMonoNames
private static Map
sFontFamilyMap
Constructors Summary
Font(long id, RenderScript rs)

        super(id, rs);
    
Methods Summary
private static voidaddFamilyToMap(android.renderscript.Font$FontFamily family)

deprecated
in API 16


             
       
                     
        
                     
        
                     
        
                     
        
    

         
        for(int i = 0; i < family.mNames.length; i ++) {
            sFontFamilyMap.put(family.mNames[i], family);
        }
    
public static android.renderscript.Fontcreate(RenderScript rs, android.content.res.Resources res, java.lang.String familyName, android.renderscript.Font$Style fontStyle, float pointSize)

deprecated
in API 16 Accepts one of the following family names as an argument and will attempt to produce the best match with a system font: "sans-serif" "arial" "helvetica" "tahoma" "verdana" "serif" "times" "times new roman" "palatino" "georgia" "baskerville" "goudy" "fantasy" "cursive" "ITC Stone Serif" "monospace" "courier" "courier new" "monaco" Returns default font if no match could be found.

        String fileName = getFontFileName(familyName, fontStyle);
        String fontPath = Environment.getRootDirectory().getAbsolutePath();
        fontPath += "/fonts/" + fileName;
        return createFromFile(rs, res, fontPath, pointSize);
    
public static android.renderscript.FontcreateFromAsset(RenderScript rs, android.content.res.Resources res, java.lang.String path, float pointSize)

deprecated
in API 16

        rs.validate();
        AssetManager mgr = res.getAssets();
        int dpi = res.getDisplayMetrics().densityDpi;

        long fontId = rs.nFontCreateFromAsset(mgr, path, pointSize, dpi);
        if(fontId == 0) {
            throw new RSRuntimeException("Unable to create font from asset " + path);
        }
        Font rsFont = new Font(fontId, rs);
        return rsFont;
    
public static android.renderscript.FontcreateFromFile(RenderScript rs, android.content.res.Resources res, java.lang.String path, float pointSize)

deprecated
in API 16 Takes a specific file name as an argument

        rs.validate();
        int dpi = res.getDisplayMetrics().densityDpi;
        long fontId = rs.nFontCreateFromFile(path, pointSize, dpi);

        if(fontId == 0) {
            throw new RSRuntimeException("Unable to create font from file " + path);
        }
        Font rsFont = new Font(fontId, rs);

        return rsFont;
    
public static android.renderscript.FontcreateFromFile(RenderScript rs, android.content.res.Resources res, java.io.File path, float pointSize)

deprecated
in API 16

        return createFromFile(rs, res, path.getAbsolutePath(), pointSize);
    
public static android.renderscript.FontcreateFromResource(RenderScript rs, android.content.res.Resources res, int id, float pointSize)

deprecated
in API 16

        String name = "R." + Integer.toString(id);

        rs.validate();
        InputStream is = null;
        try {
            is = res.openRawResource(id);
        } catch (Exception e) {
            throw new RSRuntimeException("Unable to open resource " + id);
        }

        int dpi = res.getDisplayMetrics().densityDpi;

        long fontId = 0;
        if (is instanceof AssetManager.AssetInputStream) {
            long asset = ((AssetManager.AssetInputStream) is).getNativeAsset();
            fontId = rs.nFontCreateFromAssetStream(name, pointSize, dpi, asset);
        } else {
            throw new RSRuntimeException("Unsupported asset stream created");
        }

        if(fontId == 0) {
            throw new RSRuntimeException("Unable to create font from resource " + id);
        }
        Font rsFont = new Font(fontId, rs);
        return rsFont;
    
static java.lang.StringgetFontFileName(java.lang.String familyName, android.renderscript.Font$Style style)

        initFontFamilyMap();
    
        FontFamily family = sFontFamilyMap.get(familyName);
        if(family != null) {
            switch(style) {
                case NORMAL:
                    return family.mNormalFileName;
                case BOLD:
                    return family.mBoldFileName;
                case ITALIC:
                    return family.mItalicFileName;
                case BOLD_ITALIC:
                    return family.mBoldItalicFileName;
            }
        }
        // Fallback if we could not find the desired family
        return "DroidSans.ttf";
    
private static voidinitFontFamilyMap()

        sFontFamilyMap = new HashMap<String, FontFamily>();

        FontFamily sansFamily = new FontFamily();
        sansFamily.mNames = sSansNames;
        sansFamily.mNormalFileName = "Roboto-Regular.ttf";
        sansFamily.mBoldFileName = "Roboto-Bold.ttf";
        sansFamily.mItalicFileName = "Roboto-Italic.ttf";
        sansFamily.mBoldItalicFileName = "Roboto-BoldItalic.ttf";
        addFamilyToMap(sansFamily);

        FontFamily serifFamily = new FontFamily();
        serifFamily.mNames = sSerifNames;
        serifFamily.mNormalFileName = "NotoSerif-Regular.ttf";
        serifFamily.mBoldFileName = "NotoSerif-Bold.ttf";
        serifFamily.mItalicFileName = "NotoSerif-Italic.ttf";
        serifFamily.mBoldItalicFileName = "NotoSerif-BoldItalic.ttf";
        addFamilyToMap(serifFamily);

        FontFamily monoFamily = new FontFamily();
        monoFamily.mNames = sMonoNames;
        monoFamily.mNormalFileName = "DroidSansMono.ttf";
        monoFamily.mBoldFileName = "DroidSansMono.ttf";
        monoFamily.mItalicFileName = "DroidSansMono.ttf";
        monoFamily.mBoldItalicFileName = "DroidSansMono.ttf";
        addFamilyToMap(monoFamily);