FontFamily_Delegate.java revision ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5
1baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta/*
2baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * Copyright (C) 2014 The Android Open Source Project
3baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
4baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * Licensed under the Apache License, Version 2.0 (the "License");
5baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * you may not use this file except in compliance with the License.
6baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * You may obtain a copy of the License at
7baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
8baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *      http://www.apache.org/licenses/LICENSE-2.0
9baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
10baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * Unless required by applicable law or agreed to in writing, software
11baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * distributed under the License is distributed on an "AS IS" BASIS,
12baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * See the License for the specific language governing permissions and
14baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * limitations under the License.
15baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta */
16baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
17baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptapackage android.graphics;
18baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
19baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport com.android.ide.common.rendering.api.LayoutLog;
20baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport com.android.layoutlib.bridge.Bridge;
21baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport com.android.layoutlib.bridge.impl.DelegateManager;
22baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport com.android.tools.layoutlib.annotations.LayoutlibDelegate;
23baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
24ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Guptaimport android.content.res.AssetManager;
25ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta
26baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.awt.Font;
27baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.io.File;
28baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.util.ArrayList;
29baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.util.List;
30baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
31baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport static android.graphics.Typeface_Delegate.SYSTEM_FONTS;
32baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
33baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta/**
34baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * Delegate implementing the native methods of android.graphics.FontFamily
35baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
36baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * Through the layoutlib_create tool, the original native methods of FontFamily have been replaced
37baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * by calls to methods of the same name in this delegate class.
38baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
39baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * This class behaves like the original native implementation, but in Java, keeping previously
40baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * native data into its own objects and mapping them to int that are sent back and forth between
41baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * it and the original FontFamily class.
42baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
43baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * @see DelegateManager
44baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta */
45baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptapublic class FontFamily_Delegate {
46baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
47baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // FONT_SUFFIX_ITALIC will always match FONT_SUFFIX_BOLDITALIC and hence it must be checked
48baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // separately.
49baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final String FONT_SUFFIX_BOLDITALIC = "BoldItalic.ttf";
50baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final String FONT_SUFFIX_BOLD = "Bold.ttf";
51baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final String FONT_SUFFIX_ITALIC = "Italic.ttf";
52baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
53baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    /**
54baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta     * A class associating {@link Font} with its metadata.
55baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta     */
56baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final class FontInfo {
57baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        Font mFont;
58baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        /** Regular, Bold, Italic, or BoldItalic. */
59baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        int mStyle;
60baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
61baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
62baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- delegate manager ----
63baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final DelegateManager<FontFamily_Delegate> sManager =
64baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            new DelegateManager<FontFamily_Delegate>(FontFamily_Delegate.class);
65baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
66baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- delegate helper data ----
67baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static String sFontLocation;
68baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final List<FontFamily_Delegate> sPostInitDelegate = new
69baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            ArrayList<FontFamily_Delegate>();
70baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
71baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
72baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- delegate data ----
73baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private List<FontInfo> mFonts = new ArrayList<FontInfo>();
74ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    /**
75ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta     * The variant of the Font Family - compact or elegant.
76ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta     * 0 is unspecified, 1 is compact and 2 is elegant. This needs to be kept in sync with values in
77ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta     * android.graphics.FontFamily
78ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta     *
79ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta     * @see Paint#setElegantTextHeight(boolean)
80ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta     */
81ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    private FontVariant mVariant;
82baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // Path of fonts that haven't been created since sFontLoader hasn't been initialized.
83baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private List<String> mPath = new ArrayList<String>();
84baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
85baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
86baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- Public Helper methods ----
87baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
88baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    public static FontFamily_Delegate getDelegate(long nativeFontFamily) {
89baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return sManager.getDelegate(nativeFontFamily);
90baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
91baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
92baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    public static synchronized void setFontLocation(String fontLocation) {
93baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        sFontLocation = fontLocation;
94baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        for (FontFamily_Delegate fontFamily : sPostInitDelegate) {
95baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            fontFamily.init();
96baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
97baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        sPostInitDelegate.clear();
98baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
99baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
100ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    public Font getFont(int style) {
101baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontInfo plainFont = null;
102baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        for (FontInfo font : mFonts) {
103baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            if (font.mStyle == style) {
104ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta                return font.mFont;
105baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
106ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta            if (font.mStyle == Font.PLAIN && plainFont == null) {
107ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta                plainFont = font;
108baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
109baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
110baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
111baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        // No font with the mentioned style is found. Try to derive one.
112baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (plainFont != null && style > 0 && style < 4) {
113ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta            FontInfo styledFont = new FontInfo();
114baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            styledFont.mFont = plainFont.mFont.deriveFont(style);
115baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            styledFont.mStyle = style;
116baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            // Add the font to the list of fonts so that we don't have to derive it the next time.
117baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            mFonts.add(styledFont);
118baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            return styledFont.mFont;
119baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
120baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return null;
121baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
122baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
123ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    public FontVariant getVariant() {
124ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        return mVariant;
125ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    }
126ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta
127ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta
128baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- native methods ----
129baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
130baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    @LayoutlibDelegate
131ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    /*package*/ static long nCreateFamily(String lang, int variant) {
132ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        // TODO: support lang. This is required for japanese locale.
133baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontFamily_Delegate delegate = new FontFamily_Delegate();
134ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        // variant can be 0, 1 or 2.
135ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        assert variant < 3;
136ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        delegate.mVariant = FontVariant.values()[variant];
137baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (sFontLocation != null) {
138baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            delegate.init();
139baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else {
140baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            sPostInitDelegate.add(delegate);
141baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
142baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return sManager.addNewDelegate(delegate);
143baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
144baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
145baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    @LayoutlibDelegate
146baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    /*package*/ static void nUnrefFamily(long nativePtr) {
1479113968f9570b0c8ada2dec34fa6cf893da7c022Deepanshu Gupta        // Removing the java reference for the object doesn't mean that it's freed for garbage
1489113968f9570b0c8ada2dec34fa6cf893da7c022Deepanshu Gupta        // collection. Typeface_Delegate may still hold a reference for it.
149baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        sManager.removeJavaReferenceFor(nativePtr);
150baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
151baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
152baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    @LayoutlibDelegate
153baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    /*package*/ static boolean nAddFont(long nativeFamily, String path) {
154baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontFamily_Delegate delegate = getDelegate(nativeFamily);
155baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (delegate != null) {
156baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            if (sFontLocation == null) {
157baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                delegate.mPath.add(path);
158baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                return true;
159baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
160baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            return delegate.addFont(path);
161baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
162baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return false;
163baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
164baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
165ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    @LayoutlibDelegate
166ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    /*package*/ static boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
167ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
168ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta                "FontFamily.addFontFromAsset is not supported.", null /*throwable*/, null /*data*/);
169ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        return false;
170ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    }
171ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta
172baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private void init() {
173baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        for (String path : mPath) {
174baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            addFont(path);
175baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
176baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        mPath = null;
177baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
178baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
179baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private boolean addFont(String path) {
180baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        Font font = loadFont(path);
181baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (font == null) {
182baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            return false;
183baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
184baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontInfo fontInfo = new FontInfo();
185baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        fontInfo.mFont = font;
186baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        addFontMetadata(fontInfo, path);
187baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        // TODO ensure that mFonts doesn't have the font with this style already.
188baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        mFonts.add(fontInfo);
189baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return true;
190baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
191baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
192baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static void addFontMetadata(FontInfo fontInfo, String path) {
193baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        int style = Font.PLAIN;
194baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        String fontName = path.substring(path.lastIndexOf('/'), path.length());
195baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (fontName.endsWith(FONT_SUFFIX_BOLDITALIC)) {
196baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            style = Font.BOLD | Font.ITALIC;
197baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else if (fontName.endsWith(FONT_SUFFIX_BOLD)) {
198baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            style = Font.BOLD;
199baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else if (fontName.endsWith(FONT_SUFFIX_ITALIC)) {
200baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            style = Font.ITALIC;
201baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
202baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        fontInfo.mStyle = style;
203baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
204baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
205baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static Font loadFont(String path) {
206baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (path.startsWith(SYSTEM_FONTS) ) {
207baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            String relativePath = path.substring(SYSTEM_FONTS.length());
208baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            File f = new File(sFontLocation, relativePath);
209baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
210baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            try {
211baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                return Font.createFont(Font.TRUETYPE_FONT, f);
212baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            } catch (Exception e) {
213baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
214baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                        String.format("Unable to load font %1$s", relativePath),
215ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta                        e /*throwable*/, null /*data*/);
216baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
217baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else {
218baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
219baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.",
220baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    null /*throwable*/, null /*data*/);
221baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
222baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
223baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return null;
224baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
225ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta
226ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta
227ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    // ---- Public helper class ----
228ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta
229ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    public enum FontVariant {
230ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        // The order needs to be kept in sync with android.graphics.FontFamily.
231ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta        NONE, COMPACT, ELEGANT
232ad69aee5ed503d9592c10a0f6d1a5b617d99e6c5Deepanshu Gupta    }
233baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta}
234