Typeface_Delegate.java revision 918aaa5717fce6081557c82ce1c439b6922737d5
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;
23
24import android.content.res.AssetManager;
25
26import java.awt.Font;
27import java.util.ArrayList;
28import java.util.Collections;
29import java.util.List;
30
31/**
32 * Delegate implementing the native methods of android.graphics.Typeface
33 *
34 * Through the layoutlib_create tool, the original native methods of Typeface have been replaced
35 * by calls to methods of the same name in this delegate class.
36 *
37 * This class behaves like the original native implementation, but in Java, keeping previously
38 * native data into its own objects and mapping them to int that are sent back and forth between
39 * it and the original Typeface class.
40 *
41 * @see DelegateManager
42 *
43 */
44public final class Typeface_Delegate {
45
46    // ---- delegate manager ----
47    private static final DelegateManager<Typeface_Delegate> sManager =
48            new DelegateManager<Typeface_Delegate>();
49
50    // ---- delegate helper data ----
51    private static final String DEFAULT_FAMILY = "sans-serif";
52    private static final int[] STYLE_BUFFER = new int[1];
53
54    private static FontLoader sFontLoader;
55    private static final List<Typeface_Delegate> sPostInitDelegate =
56            new ArrayList<Typeface_Delegate>();
57
58    // ---- delegate data ----
59
60    private final String mFamily;
61    private int mStyle;
62    private List<Font> mFonts;
63
64
65    // ---- Public Helper methods ----
66
67    public static synchronized void init(FontLoader fontLoader) {
68        sFontLoader = fontLoader;
69
70        for (Typeface_Delegate delegate : sPostInitDelegate) {
71            delegate.init();
72        }
73        sPostInitDelegate.clear();
74    }
75
76    public static List<Font> getFonts(Typeface typeface) {
77        return getFonts(typeface.native_instance);
78    }
79
80    public static List<Font> getFonts(int native_int) {
81        Typeface_Delegate delegate = sManager.getDelegate(native_int);
82        if (delegate == null) {
83            return null;
84        }
85
86        return delegate.mFonts;
87    }
88
89    // ---- native methods ----
90
91    /*package*/ static synchronized int nativeCreate(String familyName, int style) {
92        if (familyName == null) {
93            familyName = DEFAULT_FAMILY;
94        }
95
96        Typeface_Delegate newDelegate = new Typeface_Delegate(familyName, style);
97        if (sFontLoader != null) {
98            newDelegate.init();
99        } else {
100            // font loader has not been initialized yet, add the delegate to a list of delegates
101            // to init when the font loader is initialized.
102            // There won't be any rendering before this happens anyway.
103            sPostInitDelegate.add(newDelegate);
104        }
105
106        return sManager.addDelegate(newDelegate);
107    }
108
109    /*package*/ static synchronized int nativeCreateFromTypeface(int native_instance, int style) {
110        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
111        if (delegate == null) {
112            return 0;
113        }
114
115        Typeface_Delegate newDelegate = new Typeface_Delegate(delegate.mFamily, style);
116        if (sFontLoader != null) {
117            newDelegate.init();
118        } else {
119            // font loader has not been initialized yet, add the delegate to a list of delegates
120            // to init when the font loader is initialized.
121            // There won't be any rendering before this happens anyway.
122            sPostInitDelegate.add(newDelegate);
123        }
124
125        return sManager.addDelegate(newDelegate);
126    }
127
128    /*package*/ static synchronized int nativeCreateFromAsset(AssetManager mgr, String path) {
129        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
130                "Typeface.createFromAsset() is not supported.", null);
131        return 0;
132    }
133
134    /*package*/ static synchronized int nativeCreateFromFile(String path) {
135        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
136                "Typeface.createFromFile() is not supported.", null);
137        return 0;
138    }
139
140    /*package*/ static void nativeUnref(int native_instance) {
141        sManager.removeDelegate(native_instance);
142    }
143
144    /*package*/ static int nativeGetStyle(int native_instance) {
145        Typeface_Delegate delegate = sManager.getDelegate(native_instance);
146        if (delegate == null) {
147            return 0;
148        }
149
150        return delegate.mStyle;
151    }
152
153    /*package*/ static void setGammaForText(float blackGamma, float whiteGamma) {
154        // This is for device testing only: pass
155    }
156
157    // ---- Private delegate/helper methods ----
158
159    private Typeface_Delegate(String family, int style) {
160        mFamily = family;
161        mStyle = style;
162    }
163
164    private void init() {
165        STYLE_BUFFER[0] = mStyle;
166        Font font = sFontLoader.getFont(mFamily, STYLE_BUFFER);
167        if (font != null) {
168            List<Font> list = new ArrayList<Font>();
169            list.add(font);
170            list.addAll(sFontLoader.getFallBackFonts());
171            mFonts = Collections.unmodifiableList(list);
172            mStyle = STYLE_BUFFER[0];
173        }
174    }
175}
176