1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.graphics;
18
19import com.android.ide.common.rendering.api.LayoutLog;
20import com.android.layoutlib.bridge.Bridge;
21import com.android.layoutlib.bridge.impl.DelegateManager;
22import com.android.layoutlib.bridge.impl.FontLoader;
23import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
24
25import android.content.res.AssetManager;
26
27import java.awt.Font;
28import java.io.File;
29import java.util.ArrayList;
30import java.util.List;
31
32/**
33 * Delegate implementing the native methods of android.graphics.Typeface
34 *
35 * Through the layoutlib_create tool, the original native methods of Typeface have been replaced
36 * by calls to methods of the same name in this delegate class.
37 *
38 * This class behaves like the original native implementation, but in Java, keeping previously
39 * native data into its own objects and mapping them to int that are sent back and forth between
40 * it and the original Typeface class.
41 *
42 * @see DelegateManager
43 *
44 */
45public final class Typeface_Delegate {
46
47    private static final String SYSTEM_FONTS = "/system/fonts/";
48
49    // ---- delegate manager ----
50    private static final DelegateManager<Typeface_Delegate> sManager =
51            new DelegateManager<Typeface_Delegate>(Typeface_Delegate.class);
52
53    // ---- delegate helper data ----
54    private static final String DEFAULT_FAMILY = "sans-serif";
55
56    private static FontLoader sFontLoader;
57    private static final List<Typeface_Delegate> sPostInitDelegate =
58            new ArrayList<Typeface_Delegate>();
59
60    // ---- delegate data ----
61
62    private final String mFamily;
63    private int mStyle;
64    private List<Font> mFonts;
65
66
67    // ---- Public Helper methods ----
68
69    public static synchronized void init(FontLoader fontLoader) {
70        sFontLoader = fontLoader;
71
72        for (Typeface_Delegate delegate : sPostInitDelegate) {
73            delegate.init();
74        }
75        sPostInitDelegate.clear();
76    }
77
78    public static Typeface_Delegate getDelegate(int nativeTypeface) {
79        return sManager.getDelegate(nativeTypeface);
80    }
81
82    public static List<Font> getFonts(Typeface typeface) {
83        return getFonts(typeface.native_instance);
84    }
85
86    public static List<Font> getFonts(int native_int) {
87        Typeface_Delegate delegate = sManager.getDelegate(native_int);
88        if (delegate == null) {
89            return null;
90        }
91
92        return delegate.getFonts();
93    }
94
95    public List<Font> getFonts() {
96        return mFonts;
97    }
98
99    // ---- native methods ----
100
101    @LayoutlibDelegate
102    /*package*/ static synchronized int nativeCreate(String familyName, int style) {
103        if (familyName == null) {
104            familyName = DEFAULT_FAMILY;
105        }
106        if (style < 0) {
107            style = Typeface.NORMAL;
108        }
109
110        Typeface_Delegate newDelegate = new Typeface_Delegate(familyName, style);
111        if (sFontLoader != null) {
112            newDelegate.init();
113        } else {
114            // font loader has not been initialized yet, add the delegate to a list of delegates
115            // to init when the font loader is initialized.
116            // There won't be any rendering before this happens anyway.
117            sPostInitDelegate.add(newDelegate);
118        }
119
120        return sManager.addNewDelegate(newDelegate);
121    }
122
123    @LayoutlibDelegate
124    /*package*/ static synchronized int nativeCreateFromTypeface(int native_instance, int style) {
125        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
126        if (delegate == null) {
127            return 0;
128        }
129
130        Typeface_Delegate newDelegate = new Typeface_Delegate(delegate.mFamily, style);
131        if (sFontLoader != null) {
132            newDelegate.init();
133        } else {
134            // font loader has not been initialized yet, add the delegate to a list of delegates
135            // to init when the font loader is initialized.
136            // There won't be any rendering before this happens anyway.
137            sPostInitDelegate.add(newDelegate);
138        }
139
140        return sManager.addNewDelegate(newDelegate);
141    }
142
143    @LayoutlibDelegate
144    /*package*/ static synchronized int nativeCreateFromAsset(AssetManager mgr, String path) {
145        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
146                "Typeface.createFromAsset() is not supported.", null /*throwable*/, null /*data*/);
147        return 0;
148    }
149
150    @LayoutlibDelegate
151    /*package*/ static synchronized int nativeCreateFromFile(String path) {
152        if (path.startsWith(SYSTEM_FONTS) ) {
153            String relativePath = path.substring(SYSTEM_FONTS.length());
154            File f = new File(sFontLoader.getOsFontsLocation(), relativePath);
155
156            try {
157                Font font = Font.createFont(Font.TRUETYPE_FONT, f);
158                if (font != null) {
159                    Typeface_Delegate newDelegate = new Typeface_Delegate(font);
160                    return sManager.addNewDelegate(newDelegate);
161                }
162            } catch (Exception e) {
163                Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
164                        String.format("Unable to load font %1$s", relativePath),
165                            null /*throwable*/, null /*data*/);
166            }
167        } else {
168            Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
169                    "Typeface.createFromFile() can only work with platform fonts located in " +
170                        SYSTEM_FONTS,
171                    null /*throwable*/, null /*data*/);
172        }
173
174
175        // return a copy of the base font
176        return nativeCreate(null, 0);
177    }
178
179    @LayoutlibDelegate
180    /*package*/ static void nativeUnref(int native_instance) {
181        sManager.removeJavaReferenceFor(native_instance);
182    }
183
184    @LayoutlibDelegate
185    /*package*/ static int nativeGetStyle(int native_instance) {
186        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
187        if (delegate == null) {
188            return 0;
189        }
190
191        return delegate.mStyle;
192    }
193
194    // ---- Private delegate/helper methods ----
195
196    private Typeface_Delegate(String family, int style) {
197        mFamily = family;
198        mStyle = style;
199    }
200
201    private Typeface_Delegate(Font font) {
202        mFamily = font.getFamily();
203        mStyle = Typeface.NORMAL;
204
205        mFonts = sFontLoader.getFallbackFonts(mStyle);
206
207        // insert the font glyph first.
208        mFonts.add(0, font);
209    }
210
211    private void init() {
212        mFonts = sFontLoader.getFont(mFamily, mStyle);
213    }
214}
215