Loading tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java +38 −3 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import com.android.tools.layoutlib.annotations.LayoutlibDelegate; import android.content.res.AssetManager; import java.awt.Font; import java.io.File; import java.util.ArrayList; import java.util.List; Loading @@ -43,6 +44,8 @@ import java.util.List; */ public final class Typeface_Delegate { private static final String SYSTEM_FONTS = "/system/fonts/"; // ---- delegate manager ---- private static final DelegateManager<Typeface_Delegate> sManager = new DelegateManager<Typeface_Delegate>(Typeface_Delegate.class); Loading Loading @@ -143,9 +146,31 @@ public final class Typeface_Delegate { @LayoutlibDelegate /*package*/ static synchronized int nativeCreateFromFile(String path) { if (path.startsWith(SYSTEM_FONTS) ) { String relativePath = path.substring(SYSTEM_FONTS.length()); File f = new File(sFontLoader.getOsFontsLocation(), relativePath); try { Font font = Font.createFont(Font.TRUETYPE_FONT, f); if (font != null) { Typeface_Delegate newDelegate = new Typeface_Delegate(font); return sManager.addNewDelegate(newDelegate); } } catch (Exception e) { Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN, String.format("Unable to load font %1$s", relativePath), null /*throwable*/, null /*data*/); } } else { Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "Typeface.createFromFile() is not supported.", null /*throwable*/, null /*data*/); return 0; "Typeface.createFromFile() can only work with platform fonts located in " + SYSTEM_FONTS, null /*throwable*/, null /*data*/); } // return a copy of the base font return nativeCreate(null, 0); } @LayoutlibDelegate Loading Loading @@ -175,6 +200,16 @@ public final class Typeface_Delegate { mStyle = style; } private Typeface_Delegate(Font font) { mFamily = font.getFamily(); mStyle = Typeface.NORMAL; mFonts = sFontLoader.getFallbackFonts(mStyle); // insert the font glyph first. mFonts.add(0, font); } private void init() { mFonts = sFontLoader.getFont(mFamily, mStyle); } Loading tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java +23 −3 Original line number Diff line number Diff line Loading @@ -75,6 +75,8 @@ public final class FontLoader { private static final List<FontInfo> mMainFonts = new ArrayList<FontInfo>(); private static final List<FontInfo> mFallbackFonts = new ArrayList<FontInfo>(); private final String mOsFontsLocation; public static FontLoader create(String fontOsLocation) { try { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); Loading @@ -89,7 +91,7 @@ public final class FontLoader { handler = parseFontFile(parserFactory, fontOsLocation, FONTS_FALLBACK); List<FontInfo> fallbackFonts = handler.getFontList(); return new FontLoader(systemFonts, fallbackFonts); return new FontLoader(fontOsLocation, systemFonts, fallbackFonts); } catch (ParserConfigurationException e) { // return null below } catch (SAXException e) { Loading @@ -116,11 +118,18 @@ public final class FontLoader { return definitionParser; } private FontLoader(List<FontInfo> fontList, List<FontInfo> fallBackList) { private FontLoader(String fontOsLocation, List<FontInfo> fontList, List<FontInfo> fallBackList) { mOsFontsLocation = fontOsLocation; mMainFonts.addAll(fontList); mFallbackFonts.addAll(fallBackList); } public String getOsFontsLocation() { return mOsFontsLocation; } /** * Returns a {@link Font} object given a family name and a style value (constant in * {@link Typeface}). Loading @@ -146,7 +155,7 @@ public final class FontLoader { } } // add all the fallback fonts // add all the fallback fonts for the given style for (FontInfo info : mFallbackFonts) { result.add(info.font[style]); } Loading @@ -154,6 +163,17 @@ public final class FontLoader { return result; } public synchronized List<Font> getFallbackFonts(int style) { List<Font> result = new ArrayList<Font>(); // add all the fallback fonts for (FontInfo info : mFallbackFonts) { result.add(info.font[style]); } return result; } private final static class FontInfo { final Font[] font = new Font[4]; // Matches the 4 type-face styles. final Set<String> families; Loading Loading
tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java +38 −3 Original line number Diff line number Diff line Loading @@ -25,6 +25,7 @@ import com.android.tools.layoutlib.annotations.LayoutlibDelegate; import android.content.res.AssetManager; import java.awt.Font; import java.io.File; import java.util.ArrayList; import java.util.List; Loading @@ -43,6 +44,8 @@ import java.util.List; */ public final class Typeface_Delegate { private static final String SYSTEM_FONTS = "/system/fonts/"; // ---- delegate manager ---- private static final DelegateManager<Typeface_Delegate> sManager = new DelegateManager<Typeface_Delegate>(Typeface_Delegate.class); Loading Loading @@ -143,9 +146,31 @@ public final class Typeface_Delegate { @LayoutlibDelegate /*package*/ static synchronized int nativeCreateFromFile(String path) { if (path.startsWith(SYSTEM_FONTS) ) { String relativePath = path.substring(SYSTEM_FONTS.length()); File f = new File(sFontLoader.getOsFontsLocation(), relativePath); try { Font font = Font.createFont(Font.TRUETYPE_FONT, f); if (font != null) { Typeface_Delegate newDelegate = new Typeface_Delegate(font); return sManager.addNewDelegate(newDelegate); } } catch (Exception e) { Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN, String.format("Unable to load font %1$s", relativePath), null /*throwable*/, null /*data*/); } } else { Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, "Typeface.createFromFile() is not supported.", null /*throwable*/, null /*data*/); return 0; "Typeface.createFromFile() can only work with platform fonts located in " + SYSTEM_FONTS, null /*throwable*/, null /*data*/); } // return a copy of the base font return nativeCreate(null, 0); } @LayoutlibDelegate Loading Loading @@ -175,6 +200,16 @@ public final class Typeface_Delegate { mStyle = style; } private Typeface_Delegate(Font font) { mFamily = font.getFamily(); mStyle = Typeface.NORMAL; mFonts = sFontLoader.getFallbackFonts(mStyle); // insert the font glyph first. mFonts.add(0, font); } private void init() { mFonts = sFontLoader.getFont(mFamily, mStyle); } Loading
tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java +23 −3 Original line number Diff line number Diff line Loading @@ -75,6 +75,8 @@ public final class FontLoader { private static final List<FontInfo> mMainFonts = new ArrayList<FontInfo>(); private static final List<FontInfo> mFallbackFonts = new ArrayList<FontInfo>(); private final String mOsFontsLocation; public static FontLoader create(String fontOsLocation) { try { SAXParserFactory parserFactory = SAXParserFactory.newInstance(); Loading @@ -89,7 +91,7 @@ public final class FontLoader { handler = parseFontFile(parserFactory, fontOsLocation, FONTS_FALLBACK); List<FontInfo> fallbackFonts = handler.getFontList(); return new FontLoader(systemFonts, fallbackFonts); return new FontLoader(fontOsLocation, systemFonts, fallbackFonts); } catch (ParserConfigurationException e) { // return null below } catch (SAXException e) { Loading @@ -116,11 +118,18 @@ public final class FontLoader { return definitionParser; } private FontLoader(List<FontInfo> fontList, List<FontInfo> fallBackList) { private FontLoader(String fontOsLocation, List<FontInfo> fontList, List<FontInfo> fallBackList) { mOsFontsLocation = fontOsLocation; mMainFonts.addAll(fontList); mFallbackFonts.addAll(fallBackList); } public String getOsFontsLocation() { return mOsFontsLocation; } /** * Returns a {@link Font} object given a family name and a style value (constant in * {@link Typeface}). Loading @@ -146,7 +155,7 @@ public final class FontLoader { } } // add all the fallback fonts // add all the fallback fonts for the given style for (FontInfo info : mFallbackFonts) { result.add(info.font[style]); } Loading @@ -154,6 +163,17 @@ public final class FontLoader { return result; } public synchronized List<Font> getFallbackFonts(int style) { List<Font> result = new ArrayList<Font>(); // add all the fallback fonts for (FontInfo info : mFallbackFonts) { result.add(info.font[style]); } return result; } private final static class FontInfo { final Font[] font = new Font[4]; // Matches the 4 type-face styles. final Set<String> families; Loading