Xavier Ducrohet | f33eec6 | 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 | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 37 | * it and the original Typeface class. |
Xavier Ducrohet | f33eec6 | 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) { |
| 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 | |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 87 | /*package*/ static synchronized int nativeCreate(String familyName, int style) { |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 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 | |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 105 | /*package*/ static synchronized int nativeCreateFromTypeface(int native_instance, int style) { |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 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 | |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 125 | /*package*/ static synchronized int nativeCreateFromAsset(AssetManager mgr, String path) { |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 126 | // FIXME |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 127 | throw new UnsupportedOperationException("Native delegate needed: Typeface_Delegate.nativeCreateFromAsset"); |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 128 | } |
| 129 | |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 130 | /*package*/ static synchronized int nativeCreateFromFile(String path) { |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 131 | // FIXME |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 132 | throw new UnsupportedOperationException("Native delegate needed: Typeface_Delegate.nativeCreateFromFile"); |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 133 | } |
| 134 | |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 135 | /*package*/ static void nativeUnref(int native_instance) { |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 136 | sManager.removeDelegate(native_instance); |
| 137 | } |
| 138 | |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 139 | /*package*/ static int nativeGetStyle(int native_instance) { |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 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 | |
Xavier Ducrohet | 221f678 | 2010-10-29 16:01:40 -0700 | [diff] [blame^] | 149 | /*package*/ static void setGammaForText(float blackGamma, float whiteGamma) { |
Xavier Ducrohet | f33eec6 | 2010-10-27 16:45:38 -0700 | [diff] [blame] | 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 | } |