FileDocCategorySizeDatePackage
FontLoader.javaAPI DocAndroid 1.5 API12066Wed May 06 22:42:02 BST 2009com.android.layoutlib.bridge

FontLoader

public final class FontLoader extends Object
Provides {@link Font} object to the layout lib.

The fonts are loaded from the SDK directory. Family/style mapping is done by parsing the fonts.xml file located alongside the ttf files.

Fields Summary
private static final String
FONTS_DEFINITIONS
private static final String
NODE_FONTS
private static final String
NODE_FONT
private static final String
NODE_NAME
private static final String
ATTR_TTF
private static final String[]
NODE_LEVEL
private static final String
FONT_EXT
private static final String[]
FONT_STYLE_DEFAULT
private static final String[]
FONT_STYLE_BOLD
private static final String[]
FONT_STYLE_ITALIC
private static final String[]
FONT_STYLE_BOLDITALIC
private static final String[]
FONT_STYLES
private final Map
mFamilyToTtf
private final Map
mTtfToFontMap
Constructors Summary
private FontLoader(List fontList)

        for (FontInfo info : fontList) {
            for (String family : info.families) {
                mFamilyToTtf.put(family, info.ttf);
            }
        }
    
Methods Summary
public static com.android.layoutlib.bridge.FontLoadercreate(java.lang.String fontOsLocation)

    
         
        try {
            SAXParserFactory parserFactory = SAXParserFactory.newInstance();
                parserFactory.setNamespaceAware(true);
    
            SAXParser parser = parserFactory.newSAXParser();
            File f = new File(fontOsLocation + File.separator + FONTS_DEFINITIONS);
            
            FontDefinitionParser definitionParser = new FontDefinitionParser(
                    fontOsLocation + File.separator);
            parser.parse(new FileInputStream(f), definitionParser);
            
            return definitionParser.getFontLoader();
        } catch (ParserConfigurationException e) {
            // return null below
        } catch (SAXException e) {
            // return null below
        } catch (FileNotFoundException e) {
            // return null below
        } catch (IOException e) {
            // return null below
        }

        return null;
    
public synchronized java.awt.FontgetFont(java.lang.String family, int[] style)

        if (family == null) {
            return null;
        }

        // get the ttf name from the family
        String ttf = mFamilyToTtf.get(family);
        
        if (ttf == null) {
            return null;
        }
        
        // get the font from the ttf
        Map<Integer, Font> styleMap = mTtfToFontMap.get(ttf);
        
        if (styleMap == null) {
            styleMap = new HashMap<Integer, Font>();
            mTtfToFontMap.put(ttf, styleMap);
        }
        
        Font f = styleMap.get(style);
        
        if (f != null) {
            return f;
        }
        
        // if it doesn't exist, we create it, and we can't, we try with a simpler style
        switch (style[0]) {
            case Typeface.NORMAL:
                f = getFont(ttf, FONT_STYLES[Typeface.NORMAL]);
                break;
            case Typeface.BOLD:
            case Typeface.ITALIC:
                f = getFont(ttf, FONT_STYLES[style[0]]);
                if (f == null) {
                    f = getFont(ttf, FONT_STYLES[Typeface.NORMAL]);
                    style[0] = Typeface.NORMAL;
                }
                break;
            case Typeface.BOLD_ITALIC:
                f = getFont(ttf, FONT_STYLES[style[0]]);
                if (f == null) {
                    f = getFont(ttf, FONT_STYLES[Typeface.BOLD]);
                    if (f != null) {
                        style[0] = Typeface.BOLD;
                    } else {
                        f = getFont(ttf, FONT_STYLES[Typeface.ITALIC]);
                        if (f != null) {
                            style[0] = Typeface.ITALIC;
                        } else {
                            f = getFont(ttf, FONT_STYLES[Typeface.NORMAL]);
                            style[0] = Typeface.NORMAL;
                        }
                    }
                }
                break;
        }

        if (f != null) {
            styleMap.put(style[0], f);
            return f;
        }

        return null;
    
private java.awt.FontgetFont(java.lang.String ttf, java.lang.String[] fontFileSuffix)

        for (String suffix : fontFileSuffix) {
            String name = ttf + suffix + FONT_EXT;
            
            File f = new File(name);
            if (f.isFile()) {
                try {
                    Font font = Font.createFont(Font.TRUETYPE_FONT, f);
                    if (font != null) {
                        return font;
                    }
                } catch (FontFormatException e) {
                    // skip this font name
                } catch (IOException e) {
                    // skip this font name
                }
            }
        }
        
        return null;