FontFamily_Delegate.java revision 9113968f9570b0c8ada2dec34fa6cf893da7c022
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
24baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.awt.Font;
25baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.io.File;
26baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.util.ArrayList;
27baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport java.util.List;
28baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
29baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptaimport static android.graphics.Typeface_Delegate.SYSTEM_FONTS;
30baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
31baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta/**
32baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * Delegate implementing the native methods of android.graphics.FontFamily
33baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
34baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * Through the layoutlib_create tool, the original native methods of FontFamily have been replaced
35baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * by calls to methods of the same name in this delegate class.
36baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
37baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * This class behaves like the original native implementation, but in Java, keeping previously
38baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * native data into its own objects and mapping them to int that are sent back and forth between
39baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * it and the original FontFamily class.
40baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta *
41baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta * @see DelegateManager
42baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta */
43baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Guptapublic class FontFamily_Delegate {
44baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
45baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // FONT_SUFFIX_ITALIC will always match FONT_SUFFIX_BOLDITALIC and hence it must be checked
46baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // separately.
47baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final String FONT_SUFFIX_BOLDITALIC = "BoldItalic.ttf";
48baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final String FONT_SUFFIX_BOLD = "Bold.ttf";
49baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final String FONT_SUFFIX_ITALIC = "Italic.ttf";
50baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final String FONT_SUBSTRING_COMPACT = "UI";
51baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
52baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    /**
53baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta     * A class associating {@link Font} with its metadata.
54baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta     */
55baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final class FontInfo {
56baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        Font mFont;
57baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        /** Regular, Bold, Italic, or BoldItalic. */
58baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        int mStyle;
59baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        /**
60baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta         * The variant of the Font - compact or elegant.
61baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta         * @see Paint#setElegantTextHeight(boolean)
62baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta         */
63baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        boolean mIsCompact;
64baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
65baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
66baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- delegate manager ----
67baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final DelegateManager<FontFamily_Delegate> sManager =
68baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            new DelegateManager<FontFamily_Delegate>(FontFamily_Delegate.class);
69baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
70baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- delegate helper data ----
71baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static String sFontLocation;
72baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static final List<FontFamily_Delegate> sPostInitDelegate = new
73baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            ArrayList<FontFamily_Delegate>();
74baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
75baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
76baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- delegate data ----
77baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private List<FontInfo> mFonts = new ArrayList<FontInfo>();
78baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // Path of fonts that haven't been created since sFontLoader hasn't been initialized.
79baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private List<String> mPath = new ArrayList<String>();
80baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
81baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
82baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- Public Helper methods ----
83baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
84baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    public static FontFamily_Delegate getDelegate(long nativeFontFamily) {
85baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return sManager.getDelegate(nativeFontFamily);
86baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
87baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
88baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    public static synchronized void setFontLocation(String fontLocation) {
89baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        sFontLocation = fontLocation;
90baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        for (FontFamily_Delegate fontFamily : sPostInitDelegate) {
91baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            fontFamily.init();
92baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
93baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        sPostInitDelegate.clear();
94baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
95baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
96baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    public Font getFont(int style, boolean isCompact) {
97baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontInfo plainFont = null;
98baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontInfo styledFont = null;  // Font matching the style but not isCompact
99baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        for (FontInfo font : mFonts) {
100baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            if (font.mStyle == style) {
101baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                if (font.mIsCompact == isCompact) {
102baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    return font.mFont;
103baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                }
104baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                styledFont = font;
105baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
106baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            if (font.mStyle == Font.PLAIN) {
107baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                if (plainFont == null) {
108baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    plainFont = font;
109baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    continue;
110baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                }
111baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                if (font.mIsCompact == isCompact) {
112baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    // Override the previous selection of plain font since we've found a better one.
113baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    plainFont = font;
114baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                }
115baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
116baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
117baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (styledFont != null) {
118baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            return styledFont.mFont;
119baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
120baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
121baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        // No font with the mentioned style is found. Try to derive one.
122baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (plainFont != null && style > 0 && style < 4) {
123baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            styledFont = new FontInfo();
124baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            styledFont.mFont = plainFont.mFont.deriveFont(style);
125baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            styledFont.mStyle = style;
126baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            styledFont.mIsCompact = plainFont.mIsCompact;
127baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            // Add the font to the list of fonts so that we don't have to derive it the next time.
128baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            mFonts.add(styledFont);
129baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            return styledFont.mFont;
130baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
131baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return null;
132baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
133baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
134baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    // ---- native methods ----
135baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
136baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    @LayoutlibDelegate
137baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    /*package*/ static long nCreateFamily() {
138baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontFamily_Delegate delegate = new FontFamily_Delegate();
139baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (sFontLocation != null) {
140baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            delegate.init();
141baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else {
142baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            sPostInitDelegate.add(delegate);
143baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
144baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return sManager.addNewDelegate(delegate);
145baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
146baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
147baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    @LayoutlibDelegate
148baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    /*package*/ static void nUnrefFamily(long nativePtr) {
1499113968f9570b0c8ada2dec34fa6cf893da7c022Deepanshu Gupta        // Removing the java reference for the object doesn't mean that it's freed for garbage
1509113968f9570b0c8ada2dec34fa6cf893da7c022Deepanshu Gupta        // collection. Typeface_Delegate may still hold a reference for it.
151baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        sManager.removeJavaReferenceFor(nativePtr);
152baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
153baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
154baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    @LayoutlibDelegate
155baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    /*package*/ static boolean nAddFont(long nativeFamily, String path) {
156baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontFamily_Delegate delegate = getDelegate(nativeFamily);
157baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (delegate != null) {
158baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            if (sFontLocation == null) {
159baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                delegate.mPath.add(path);
160baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                return true;
161baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
162baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            return delegate.addFont(path);
163baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
164baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return false;
165baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
166baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
167baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private void init() {
168baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        for (String path : mPath) {
169baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            addFont(path);
170baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
171baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        mPath = null;
172baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
173baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
174baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private boolean addFont(String path) {
175baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        Font font = loadFont(path);
176baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (font == null) {
177baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            return false;
178baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
179baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        FontInfo fontInfo = new FontInfo();
180baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        fontInfo.mFont = font;
181baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        addFontMetadata(fontInfo, path);
182baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        // TODO ensure that mFonts doesn't have the font with this style already.
183baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        mFonts.add(fontInfo);
184baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return true;
185baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
186baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
187baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static void addFontMetadata(FontInfo fontInfo, String path) {
188baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        int style = Font.PLAIN;
189baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        String fontName = path.substring(path.lastIndexOf('/'), path.length());
190baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (fontName.endsWith(FONT_SUFFIX_BOLDITALIC)) {
191baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            style = Font.BOLD | Font.ITALIC;
192baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else if (fontName.endsWith(FONT_SUFFIX_BOLD)) {
193baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            style = Font.BOLD;
194baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else if (fontName.endsWith(FONT_SUFFIX_ITALIC)) {
195baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            style = Font.ITALIC;
196baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
197baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        fontInfo.mStyle = style;
198baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
199baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        // Names of compact fonts end with UI-<style>.ttf. For example, NotoNakshUI-Regular.ttf.
200baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        // This should go away when this info is passed on by nAddFont().
201baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        int hyphenIndex = fontName.lastIndexOf('-');
202baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        fontInfo.mIsCompact = hyphenIndex > 0 &&
203baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                fontName.substring(0, hyphenIndex).endsWith(FONT_SUBSTRING_COMPACT);
204baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
205baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
206baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
207baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    private static Font loadFont(String path) {
208baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        if (path.startsWith(SYSTEM_FONTS) ) {
209baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            String relativePath = path.substring(SYSTEM_FONTS.length());
210baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            File f = new File(sFontLocation, relativePath);
211baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
212baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            try {
213baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                return Font.createFont(Font.TRUETYPE_FONT, f);
214baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            } catch (Exception e) {
215baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
216baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                        String.format("Unable to load font %1$s", relativePath),
217baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                        null /*throwable*/, null /*data*/);
218baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            }
219baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        } else {
220baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta            Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
221baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.",
222baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta                    null /*throwable*/, null /*data*/);
223baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        }
224baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta
225baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta        return null;
226baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta    }
227baef8c1ffe5c900fb0da9512654bf249b5fc9269Deepanshu Gupta}
228