Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 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 | |
| 17 | package android.graphics; |
| 18 | |
| 19 | import com.android.layoutlib.bridge.DelegateManager; |
| 20 | import com.android.layoutlib.bridge.FontLoader; |
| 21 | |
| 22 | import android.content.res.AssetManager; |
| 23 | |
| 24 | import java.awt.Font; |
| 25 | import java.util.ArrayList; |
| 26 | import java.util.Collections; |
| 27 | import 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 Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 37 | * it and the original Typeface class. |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 38 | * |
| 39 | * @see DelegateManager |
| 40 | * |
| 41 | */ |
| 42 | public 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 Ducrohet | 9f63ff2 | 2010-10-28 11:52:00 -0700 | [diff] [blame] | 75 | 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 Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 80 | if (delegate == null) { |
| 81 | assert false; |
| 82 | return null; |
| 83 | } |
| 84 | |
| 85 | return delegate.mFonts; |
| 86 | } |
| 87 | |
| 88 | |
| 89 | // ---- native methods ---- |
| 90 | |
Xavier Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 91 | /*package*/ static synchronized int nativeCreate(String familyName, int style) { |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 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 | |
Xavier Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 109 | /*package*/ static synchronized int nativeCreateFromTypeface(int native_instance, int style) { |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 110 | 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 Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 129 | /*package*/ static synchronized int nativeCreateFromAsset(AssetManager mgr, String path) { |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 130 | // FIXME |
Xavier Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 131 | throw new UnsupportedOperationException("Native delegate needed: Typeface_Delegate.nativeCreateFromAsset"); |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Xavier Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 134 | /*package*/ static synchronized int nativeCreateFromFile(String path) { |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 135 | // FIXME |
Xavier Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 136 | throw new UnsupportedOperationException("Native delegate needed: Typeface_Delegate.nativeCreateFromFile"); |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Xavier Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 139 | /*package*/ static void nativeUnref(int native_instance) { |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 140 | sManager.removeDelegate(native_instance); |
| 141 | } |
| 142 | |
Xavier Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 143 | /*package*/ static int nativeGetStyle(int native_instance) { |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 144 | 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 Ducrohet | 5de11a1 | 2010-10-29 16:01:40 -0700 | [diff] [blame] | 153 | /*package*/ static void setGammaForText(float blackGamma, float whiteGamma) { |
Xavier Ducrohet | 5e7ed8d | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 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 | } |