FileDocCategorySizeDatePackage
Typeface_Delegate.javaAPI DocAndroid 5.1 API8316Thu Mar 12 22:22:44 GMT 2015android.graphics

Typeface_Delegate

public final class Typeface_Delegate extends Object
Delegate implementing the native methods of android.graphics.Typeface Through the layoutlib_create tool, the original native methods of Typeface have been replaced by calls to methods of the same name in this delegate class. This class behaves like the original native implementation, but in Java, keeping previously native data into its own objects and mapping them to int that are sent back and forth between it and the original Typeface class.
see
DelegateManager

Fields Summary
public static final String
SYSTEM_FONTS
private static final com.android.layoutlib.bridge.impl.DelegateManager
sManager
private final FontFamily_Delegate[]
mFontFamilies
private final int
mStyle
private final int
mWeight
private static long
sDefaultTypeface
Constructors Summary
private Typeface_Delegate(FontFamily_Delegate[] fontFamilies, int style)

        this(fontFamilies, style, FontFamily_Delegate.DEFAULT_FONT_WEIGHT);
    
public Typeface_Delegate(FontFamily_Delegate[] fontFamilies, int style, int weight)

        mFontFamilies = fontFamilies;
        mStyle = style;
        mWeight = weight;
    
Methods Summary
public static android.graphics.Typeface_DelegategetDelegate(long nativeTypeface)



    // ---- Public Helper methods ----

         
        return sManager.getDelegate(nativeTypeface);
    
public java.util.ListgetFonts(android.graphics.FontFamily_Delegate.FontVariant variant)
Return a list of fonts that match the style and variant. The list is ordered according to preference of fonts. The list may contain null when the font failed to load. If null is reached when trying to render with this list of fonts, then a warning should be logged letting the user know that some font failed to load.

param
variant The variant preferred. Can only be {@link FontVariant#COMPACT} or {@link FontVariant#ELEGANT}

        assert variant != FontVariant.NONE;

        // Calculate the required weight based on style and weight of this typeface.
        int weight = mWeight + ((mStyle & Font.BOLD) == 0 ? 0 : FontFamily_Delegate.BOLD_FONT_WEIGHT_DELTA);
        if (weight > 900) {
            weight = 900;
        }
        final boolean isItalic = (mStyle & Font.ITALIC) != 0;
        List<Font> fonts = new ArrayList<Font>(mFontFamilies.length);
        for (int i = 0; i < mFontFamilies.length; i++) {
            FontFamily_Delegate ffd = mFontFamilies[i];
            if (ffd != null && ffd.isValid()) {
                Font font = ffd.getFont(weight, isItalic);
                if (font != null) {
                    FontVariant ffdVariant = ffd.getVariant();
                    if (ffdVariant == FontVariant.NONE) {
                        fonts.add(font);
                        continue;
                    }
                    // We cannot open each font and get locales supported, etc to match the fonts.
                    // As a workaround, we hardcode certain assumptions like Elegant and Compact
                    // always appear in pairs.
                    assert i < mFontFamilies.length - 1;
                    FontFamily_Delegate ffd2 = mFontFamilies[++i];
                    assert ffd2 != null;
                    FontVariant ffd2Variant = ffd2.getVariant();
                    Font font2 = ffd2.getFont(weight, isItalic);
                    assert ffd2Variant != FontVariant.NONE && ffd2Variant != ffdVariant
                            && font2 != null;
                    // Add the font with the matching variant to the list.
                    if (variant == ffd.getVariant()) {
                        fonts.add(font);
                    } else {
                        fonts.add(font2);
                    }
                } else {
                    // The FontFamily is valid but doesn't contain any matching font. This means
                    // that the font failed to load. We add null to the list of fonts. Don't throw
                    // the warning just yet. If this is a non-english font, we don't want to warn
                    // users who are trying to render only english text.
                    fonts.add(null);
                }
            }
        }
        return fonts;
    
static java.io.FilegetSystemFontConfigLocation()

        return new File(getFontLocation());
    
static synchronized longnativeCreateFromArray(long[] familyArray)

        FontFamily_Delegate[] fontFamilies = new FontFamily_Delegate[familyArray.length];
        for (int i = 0; i < familyArray.length; i++) {
            fontFamilies[i] = FontFamily_Delegate.getDelegate(familyArray[i]);
        }
        Typeface_Delegate delegate = new Typeface_Delegate(fontFamilies, Typeface.NORMAL);
        return sManager.addNewDelegate(delegate);
    
static synchronized longnativeCreateFromTypeface(long native_instance, int style)

        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
        if (delegate == null) {
            delegate = sManager.getDelegate(sDefaultTypeface);
        }
        if (delegate == null) {
            return 0;
        }

        return sManager.addNewDelegate(new Typeface_Delegate(delegate.mFontFamilies, style,
                delegate.mWeight));
    
static longnativeCreateWeightAlias(long native_instance, int weight)

        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
        if (delegate == null) {
            delegate = sManager.getDelegate(sDefaultTypeface);
        }
        if (delegate == null) {
            return 0;
        }
        Typeface_Delegate weightAlias =
                new Typeface_Delegate(delegate.mFontFamilies, delegate.mStyle, weight);
        return sManager.addNewDelegate(weightAlias);
    
static intnativeGetStyle(long native_instance)

        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
        if (delegate == null) {
            return 0;
        }

        return delegate.mStyle;
    
static voidnativeSetDefault(long native_instance)

        sDefaultTypeface = native_instance;
    
static voidnativeUnref(long native_instance)

        sManager.removeJavaReferenceFor(native_instance);
    
public static voidresetDefaults()
Clear the default typefaces when disposing bridge.

        // Sometimes this is called before the Bridge is initialized. In that case, we don't want to
        // initialize Typeface because the SDK fonts location hasn't been set.
        if (FontFamily_Delegate.getFontLocation() != null) {
            Typeface.sDefaults = null;
        }