Merge "Layoutlib: Typeface support for loading fonts manually."
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 android.content.res.AssetManager;
 
 import java.awt.Font;
+import java.io.File;
 import java.util.ArrayList;
 import java.util.List;
 
@@ -43,6 +44,8 @@
  */
 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 @@
 
     @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 @@
         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 @@
     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 @@
             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 @@
         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 @@
             }
         }
 
-        // 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 @@
         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;