FileDocCategorySizeDatePackage
Typeface.javaAPI DocAndroid 5.1 API14073Thu Mar 12 22:22:30 GMT 2015android.graphics

Typeface

public class Typeface extends Object
The Typeface class specifies the typeface and intrinsic style of a font. This is used in the paint, along with optionally Paint settings like textSize, textSkewX, textScaleX to specify how text appears when drawn (and measured).

Fields Summary
private static String
TAG
public static final Typeface
DEFAULT
The default NORMAL typeface object
public static final Typeface
DEFAULT_BOLD
The default BOLD typeface object. Note: this may be not actually be bold, depending on what fonts are installed. Call getStyle() to know for sure.
public static final Typeface
SANS_SERIF
The NORMAL style of the default sans serif typeface.
public static final Typeface
SERIF
The NORMAL style of the default serif typeface.
public static final Typeface
MONOSPACE
The NORMAL style of the default monospace typeface.
static Typeface[]
sDefaults
private static final android.util.LongSparseArray
sTypefaceCache
static Typeface
sDefaultTypeface
static Map
sSystemFontMap
static FontFamily[]
sFallbackFonts
static final String
FONTS_CONFIG
public long
native_instance
public static final int
NORMAL
public static final int
BOLD
public static final int
ITALIC
public static final int
BOLD_ITALIC
private int
mStyle
Constructors Summary
private Typeface(long ni)

        if (ni == 0) {
            throw new RuntimeException("native typeface cannot be made");
        }

        native_instance = ni;
        mStyle = nativeGetStyle(ni);
    
Methods Summary
public static android.graphics.Typefacecreate(java.lang.String familyName, int style)
Create a typeface object given a family name, and option style information. If null is passed for the name, then the "default" font will be chosen. The resulting typeface object can be queried (getStyle()) to discover what its "real" style characteristics are.

param
familyName May be null. The name of the font family.
param
style The style (normal, bold, italic) of the typeface. e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
return
The best matching typeface.

        if (sSystemFontMap != null) {
            return create(sSystemFontMap.get(familyName), style);
        }
        return null;
    
public static android.graphics.Typefacecreate(android.graphics.Typeface family, int style)
Create a typeface object that best matches the specified existing typeface and the specified Style. Use this call if you want to pick a new style from the same family of an existing typeface object. If family is null, this selects from the default font's family.

param
family May be null. The name of the existing type face.
param
style The style (normal, bold, italic) of the typeface. e.g. NORMAL, BOLD, ITALIC, BOLD_ITALIC
return
The best matching typeface.

        if (style < 0 || style > 3) {
            style = 0;
        }
        long ni = 0;
        if (family != null) {
            // Return early if we're asked for the same face/style
            if (family.mStyle == style) {
                return family;
            }

            ni = family.native_instance;
        }

        Typeface typeface;
        SparseArray<Typeface> styles = sTypefaceCache.get(ni);

        if (styles != null) {
            typeface = styles.get(style);
            if (typeface != null) {
                return typeface;
            }
        }

        typeface = new Typeface(nativeCreateFromTypeface(ni, style));
        if (styles == null) {
            styles = new SparseArray<Typeface>(4);
            sTypefaceCache.put(ni, styles);
        }
        styles.put(style, typeface);

        return typeface;
    
public static android.graphics.TypefacecreateFromAsset(android.content.res.AssetManager mgr, java.lang.String path)
Create a new typeface from the specified font data.

param
mgr The application's asset manager
param
path The file name of the font data in the assets directory
return
The new typeface.

        if (sFallbackFonts != null) {
            FontFamily fontFamily = new FontFamily();
            if (fontFamily.addFontFromAsset(mgr, path)) {
                FontFamily[] families = { fontFamily };
                return createFromFamiliesWithDefault(families);
            }
        }
        throw new RuntimeException("Font asset not found " + path);
    
public static android.graphics.TypefacecreateFromFamilies(FontFamily[] families)
Create a new typeface from an array of font families.

param
families array of font families
hide

        long[] ptrArray = new long[families.length];
        for (int i = 0; i < families.length; i++) {
            ptrArray[i] = families[i].mNativePtr;
        }
        return new Typeface(nativeCreateFromArray(ptrArray));
    
public static android.graphics.TypefacecreateFromFamiliesWithDefault(FontFamily[] families)
Create a new typeface from an array of font families, including also the font families in the fallback list.

param
families array of font families
hide

        long[] ptrArray = new long[families.length + sFallbackFonts.length];
        for (int i = 0; i < families.length; i++) {
            ptrArray[i] = families[i].mNativePtr;
        }
        for (int i = 0; i < sFallbackFonts.length; i++) {
            ptrArray[i + families.length] = sFallbackFonts[i].mNativePtr;
        }
        return new Typeface(nativeCreateFromArray(ptrArray));
    
public static android.graphics.TypefacecreateFromFile(java.lang.String path)
Create a new typeface from the specified font file.

param
path The full path to the font data.
return
The new typeface.

        if (sFallbackFonts != null) {
            FontFamily fontFamily = new FontFamily();
            if (fontFamily.addFont(path)) {
                FontFamily[] families = { fontFamily };
                return createFromFamiliesWithDefault(families);
            }
        }
        throw new RuntimeException("Font not found " + path);
    
public static android.graphics.TypefacecreateFromFile(java.io.File path)
Create a new typeface from the specified font file.

param
path The path to the font data.
return
The new typeface.

        return createFromFile(path.getAbsolutePath());
    
public static android.graphics.TypefacedefaultFromStyle(int style)
Returns one of the default typeface objects, based on the specified style

return
the default typeface that corresponds to the style

        return sDefaults[style];
    
public booleanequals(java.lang.Object o)

        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;

        Typeface typeface = (Typeface) o;

        return mStyle == typeface.mStyle && native_instance == typeface.native_instance;
    
protected voidfinalize()

        try {
            nativeUnref(native_instance);
        } finally {
            super.finalize();
        }
    
public intgetStyle()
Returns the typeface's intrinsic style attributes

        return mStyle;
    
private static java.io.FilegetSystemFontConfigLocation()

        init();
        // Set up defaults and typefaces exposed in public API
        DEFAULT         = create((String) null, 0);
        DEFAULT_BOLD    = create((String) null, Typeface.BOLD);
        SANS_SERIF      = create("sans-serif", 0);
        SERIF           = create("serif", 0);
        MONOSPACE       = create("monospace", 0);

        sDefaults = new Typeface[] {
            DEFAULT,
            DEFAULT_BOLD,
            create((String) null, Typeface.ITALIC),
            create((String) null, Typeface.BOLD_ITALIC),
        };

    
        return new File("/system/etc/");
    
public inthashCode()

        /*
         * Modified method for hashCode with long native_instance derived from
         * http://developer.android.com/reference/java/lang/Object.html
         */
        int result = 17;
        result = 31 * result + (int) (native_instance ^ (native_instance >>> 32));
        result = 31 * result + mStyle;
        return result;
    
private static voidinit()

        // Load font config and initialize Minikin state
        File systemFontConfigLocation = getSystemFontConfigLocation();
        File configFilename = new File(systemFontConfigLocation, FONTS_CONFIG);
        try {
            FileInputStream fontsIn = new FileInputStream(configFilename);
            FontListParser.Config fontConfig = FontListParser.parse(fontsIn);

            List<FontFamily> familyList = new ArrayList<FontFamily>();
            // Note that the default typeface is always present in the fallback list;
            // this is an enhancement from pre-Minikin behavior.
            for (int i = 0; i < fontConfig.families.size(); i++) {
                Family f = fontConfig.families.get(i);
                if (i == 0 || f.name == null) {
                    familyList.add(makeFamilyFromParsed(f));
                }
            }
            sFallbackFonts = familyList.toArray(new FontFamily[familyList.size()]);
            setDefault(Typeface.createFromFamilies(sFallbackFonts));

            Map<String, Typeface> systemFonts = new HashMap<String, Typeface>();
            for (int i = 0; i < fontConfig.families.size(); i++) {
                Typeface typeface;
                Family f = fontConfig.families.get(i);
                if (f.name != null) {
                    if (i == 0) {
                        // The first entry is the default typeface; no sense in
                        // duplicating the corresponding FontFamily.
                        typeface = sDefaultTypeface;
                    } else {
                        FontFamily fontFamily = makeFamilyFromParsed(f);
                        FontFamily[] families = { fontFamily };
                        typeface = Typeface.createFromFamiliesWithDefault(families);
                    }
                    systemFonts.put(f.name, typeface);
                }
            }
            for (FontListParser.Alias alias : fontConfig.aliases) {
                Typeface base = systemFonts.get(alias.toName);
                Typeface newFace = base;
                int weight = alias.weight;
                if (weight != 400) {
                    newFace = new Typeface(nativeCreateWeightAlias(base.native_instance, weight));
                }
                systemFonts.put(alias.name, newFace);
            }
            sSystemFontMap = systemFonts;

        } catch (RuntimeException e) {
            Log.w(TAG, "Didn't create default family (most likely, non-Minikin build)", e);
            // TODO: normal in non-Minikin case, remove or make error when Minikin-only
        } catch (FileNotFoundException e) {
            Log.e(TAG, "Error opening " + configFilename);
        } catch (IOException e) {
            Log.e(TAG, "Error reading " + configFilename);
        } catch (XmlPullParserException e) {
            Log.e(TAG, "XML parse exception for " + configFilename);
        }
    
public final booleanisBold()
Returns true if getStyle() has the BOLD bit set.

        return (mStyle & BOLD) != 0;
    
public final booleanisItalic()
Returns true if getStyle() has the ITALIC bit set.

        return (mStyle & ITALIC) != 0;
    
private static FontFamilymakeFamilyFromParsed(android.graphics.FontListParser.Family family)

        FontFamily fontFamily = new FontFamily(family.lang, family.variant);
        for (FontListParser.Font font : family.fonts) {
            fontFamily.addFontWeightStyle(font.fontName, font.weight, font.isItalic);
        }
        return fontFamily;
    
private static native longnativeCreateFromArray(long[] familyArray)

private static native longnativeCreateFromTypeface(long native_instance, int style)

private static native longnativeCreateWeightAlias(long native_instance, int weight)

private static native intnativeGetStyle(long native_instance)

private static native voidnativeSetDefault(long native_instance)

private static native voidnativeUnref(long native_instance)

private static voidsetDefault(android.graphics.Typeface t)


         
        sDefaultTypeface = t;
        nativeSetDefault(t.native_instance);