Typeface_Delegate.java revision 9113968f9570b0c8ada2dec34fa6cf893da7c022
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.tools.layoutlib.annotations.LayoutlibDelegate; 23 24import android.content.res.AssetManager; 25 26import java.awt.Font; 27import java.io.File; 28import java.util.ArrayList; 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 public static final String SYSTEM_FONTS = "/system/fonts/"; 47 48 // ---- delegate manager ---- 49 private static final DelegateManager<Typeface_Delegate> sManager = 50 new DelegateManager<Typeface_Delegate>(Typeface_Delegate.class); 51 52 // ---- delegate helper data ---- 53 private static String sFontLocation; 54 55 // ---- delegate data ---- 56 57 private final FontFamily_Delegate[] mFontFamilies; // the reference to FontFamily_Delegate. 58 private int mStyle; 59 60 private static long sDefaultTypeface; 61 62 // ---- Public Helper methods ---- 63 public static synchronized void setFontLocation(String fontLocation) { 64 sFontLocation = fontLocation; 65 FontFamily_Delegate.setFontLocation(fontLocation); 66 } 67 68 public static Typeface_Delegate getDelegate(long nativeTypeface) { 69 return sManager.getDelegate(nativeTypeface); 70 } 71 72 public List<Font> getFonts(boolean compact) { 73 List<Font> fonts = new ArrayList<Font>(mFontFamilies.length); 74 for (FontFamily_Delegate ffd : mFontFamilies) { 75 if (ffd != null) { 76 Font font = ffd.getFont(mStyle, compact); 77 if (font != null) { 78 fonts.add(font); 79 } 80 } 81 } 82 return fonts; 83 } 84 85 // ---- native methods ---- 86 87 @LayoutlibDelegate 88 /*package*/ static synchronized long nativeCreate(String familyName, int style) { 89 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, 90 "Could not find font with family \"" + familyName + "\".", 91 null /*throwable*/, null /*data*/); 92 return 0; 93 } 94 95 @LayoutlibDelegate 96 /*package*/ static synchronized long nativeCreateFromTypeface(long native_instance, int style) { 97 Typeface_Delegate delegate = sManager.getDelegate(native_instance); 98 if (delegate == null) { 99 delegate = sManager.getDelegate(sDefaultTypeface); 100 } 101 if (delegate == null) { 102 return 0; 103 } 104 105 return sManager.addNewDelegate(new Typeface_Delegate(delegate.mFontFamilies, style)); 106 } 107 108 @LayoutlibDelegate 109 /*package*/ static synchronized long nativeCreateFromAsset(AssetManager mgr, String path) { 110 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, 111 "Typeface.createFromAsset() is not supported.", null /*throwable*/, null /*data*/); 112 return 0; 113 } 114 115 @LayoutlibDelegate 116 /*package*/ static synchronized long nativeCreateFromFile(String path) { 117 Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, 118 "Typeface.createFromFile() is not supported.,", null, null); 119 return 0; 120 } 121 122 @LayoutlibDelegate 123 /*package*/ static synchronized long nativeCreateFromArray(long[] familyArray) { 124 FontFamily_Delegate[] fontFamilies = new FontFamily_Delegate[familyArray.length]; 125 for (int i = 0; i < familyArray.length; i++) { 126 fontFamilies[i] = FontFamily_Delegate.getDelegate(familyArray[i]); 127 } 128 Typeface_Delegate delegate = new Typeface_Delegate(fontFamilies, Typeface.NORMAL); 129 return sManager.addNewDelegate(delegate); 130 } 131 132 @LayoutlibDelegate 133 /*package*/ static void nativeUnref(long native_instance) { 134 sManager.removeJavaReferenceFor(native_instance); 135 } 136 137 @LayoutlibDelegate 138 /*package*/ static int nativeGetStyle(long native_instance) { 139 Typeface_Delegate delegate = sManager.getDelegate(native_instance); 140 if (delegate == null) { 141 return 0; 142 } 143 144 return delegate.mStyle; 145 } 146 147 @LayoutlibDelegate 148 /*package*/ static void nativeSetDefault(long native_instance) { 149 sDefaultTypeface = native_instance; 150 } 151 152 @LayoutlibDelegate 153 /*package*/ static File getSystemFontConfigLocation() { 154 return new File(sFontLocation); 155 } 156 157 // ---- Private delegate/helper methods ---- 158 159 private Typeface_Delegate(FontFamily_Delegate[] fontFamilies, int style) { 160 mFontFamilies = fontFamilies; 161 mStyle = style; 162 } 163} 164