diff options
| author | Xavier Ducrohet <xav@android.com> | 2011-08-03 17:11:33 -0700 |
|---|---|---|
| committer | Xavier Ducrohet <xav@android.com> | 2011-10-04 18:24:30 -0700 |
| commit | 14094097329b3bdcbf26392141111d74c8b89718 (patch) | |
| tree | 9a41ea4a27d5bb311916b66c0f756055f97c78cf /tools/layoutlib/bridge/src | |
| parent | 178006a0e05b41b4c4de93aec30368a9102ca140 (diff) | |
| download | frameworks_base-14094097329b3bdcbf26392141111d74c8b89718.zip frameworks_base-14094097329b3bdcbf26392141111d74c8b89718.tar.gz frameworks_base-14094097329b3bdcbf26392141111d74c8b89718.tar.bz2 | |
Layoutlib: Typeface support for loading fonts manually.
If the font being loaded is a system font, then we can find the font
file and manually load it.
Change-Id: I95473b1f1b88df64316b77c41ed05d4d09ab61ed
Diffstat (limited to 'tools/layoutlib/bridge/src')
| -rw-r--r-- | tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java | 41 | ||||
| -rw-r--r-- | tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java | 26 |
2 files changed, 61 insertions, 6 deletions
diff --git a/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java index dd14355..2414d70 100644 --- a/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java @@ -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; @@ -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); @@ -143,9 +146,31 @@ public final class Typeface_Delegate { @LayoutlibDelegate /*package*/ static synchronized int nativeCreateFromFile(String path) { - Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED, - "Typeface.createFromFile() is not supported.", null /*throwable*/, null /*data*/); - return 0; + 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() 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 @@ -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); } diff --git a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java index 0d1ba7c..081ce67 100644 --- a/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java +++ b/tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java @@ -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(); @@ -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) { @@ -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}). @@ -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]); } @@ -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; |
