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