blob: 7e90e7d84a5d1e405b56a8f157c3ec71448b6e7f [file] [log] [blame]
Xavier Ducrohet5e7ed8d2010-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
Xavier Ducrohet5de11a12010-10-29 16:01:40 -070037 * it and the original Typeface class.
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -070038 *
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) {
Xavier Ducrohet9f63ff22010-10-28 11:52:00 -070075 return getFonts(typeface.native_instance);
76 }
77
78 public static List<Font> getFonts(int native_int) {
79 Typeface_Delegate delegate = sManager.getDelegate(native_int);
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -070080 if (delegate == null) {
81 assert false;
82 return null;
83 }
84
85 return delegate.mFonts;
86 }
87
88
89 // ---- native methods ----
90
Xavier Ducrohet5de11a12010-10-29 16:01:40 -070091 /*package*/ static synchronized int nativeCreate(String familyName, int style) {
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -070092 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
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700109 /*package*/ static synchronized int nativeCreateFromTypeface(int native_instance, int style) {
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700110 Typeface_Delegate delegate = sManager.getDelegate(native_instance);
111 if (delegate == null) {
112 assert false;
113 return 0;
114 }
115
116 Typeface_Delegate newDelegate = new Typeface_Delegate(delegate.mFamily, style);
117 if (sFontLoader != null) {
118 newDelegate.init();
119 } else {
120 // font loader has not been initialized yet, add the delegate to a list of delegates
121 // to init when the font loader is initialized.
122 // There won't be any rendering before this happens anyway.
123 sPostInitDelegate.add(newDelegate);
124 }
125
126 return sManager.addDelegate(newDelegate);
127 }
128
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700129 /*package*/ static synchronized int nativeCreateFromAsset(AssetManager mgr, String path) {
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700130 // FIXME
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700131 throw new UnsupportedOperationException("Native delegate needed: Typeface_Delegate.nativeCreateFromAsset");
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700132 }
133
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700134 /*package*/ static synchronized int nativeCreateFromFile(String path) {
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700135 // FIXME
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700136 throw new UnsupportedOperationException("Native delegate needed: Typeface_Delegate.nativeCreateFromFile");
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700137 }
138
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700139 /*package*/ static void nativeUnref(int native_instance) {
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700140 sManager.removeDelegate(native_instance);
141 }
142
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700143 /*package*/ static int nativeGetStyle(int native_instance) {
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700144 Typeface_Delegate delegate = sManager.getDelegate(native_instance);
145 if (delegate == null) {
146 assert false;
147 return 0;
148 }
149
150 return delegate.mStyle;
151 }
152
Xavier Ducrohet5de11a12010-10-29 16:01:40 -0700153 /*package*/ static void setGammaForText(float blackGamma, float whiteGamma) {
Xavier Ducrohet5e7ed8d2010-10-27 16:45:38 -0700154 // 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}