summaryrefslogtreecommitdiffstats
path: root/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
diff options
context:
space:
mode:
authorDeepanshu Gupta <deepanshu@google.com>2014-06-30 15:00:59 -0700
committerDeepanshu Gupta <deepanshu@google.com>2014-07-01 17:32:12 -0700
commit3c937cf5c730519e750cdee4d5fa61e2a593e33a (patch)
treec2848966f9a0ee382f53a373ab7e6d0fef84ec93 /tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
parent7ea293bc6a31c5b1bf3b2663376a39dc3a79b671 (diff)
downloadframeworks_base-3c937cf5c730519e750cdee4d5fa61e2a593e33a.zip
frameworks_base-3c937cf5c730519e750cdee4d5fa61e2a593e33a.tar.gz
frameworks_base-3c937cf5c730519e750cdee4d5fa61e2a593e33a.tar.bz2
Support Typeface.createFromFile()
Add support for Typeface.createFromFile() for platform fonts. The feature existed in the KitKat LayoutLib but was dropped for the L preview. This change adds it back. Change-Id: Ib1abe67a32c28a1fb0e2a4f3061c358b55129434
Diffstat (limited to 'tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java')
-rw-r--r--tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java88
1 files changed, 45 insertions, 43 deletions
diff --git a/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
index d73adab..30b0ce5 100644
--- a/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
+++ b/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java
@@ -83,6 +83,13 @@ public class FontFamily_Delegate {
private List<String> mPath = new ArrayList<String>();
+ // ---- Public helper class ----
+
+ public enum FontVariant {
+ // The order needs to be kept in sync with android.graphics.FontFamily.
+ NONE, COMPACT, ELEGANT
+ }
+
// ---- Public Helper methods ----
public static FontFamily_Delegate getDelegate(long nativeFontFamily) {
@@ -124,6 +131,40 @@ public class FontFamily_Delegate {
return mVariant;
}
+ /*package*/ static int getFontStyle(String path) {
+ int style = Font.PLAIN;
+ String fontName = path.substring(path.lastIndexOf('/'));
+ if (fontName.endsWith(FONT_SUFFIX_BOLDITALIC)) {
+ style = Font.BOLD | Font.ITALIC;
+ } else if (fontName.endsWith(FONT_SUFFIX_BOLD)) {
+ style = Font.BOLD;
+ } else if (fontName.endsWith(FONT_SUFFIX_ITALIC)) {
+ style = Font.ITALIC;
+ }
+ return style;
+ }
+
+ /*package*/ static Font loadFont(String path) {
+ if (path.startsWith(SYSTEM_FONTS) ) {
+ String relativePath = path.substring(SYSTEM_FONTS.length());
+ File f = new File(sFontLocation, relativePath);
+
+ try {
+ return Font.createFont(Font.TRUETYPE_FONT, f);
+ } catch (Exception e) {
+ Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
+ String.format("Unable to load font %1$s", relativePath),
+ e /*throwable*/, null /*data*/);
+ }
+ } else {
+ Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
+ "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.",
+ null /*throwable*/, null /*data*/);
+ }
+
+ return null;
+ }
+
// ---- native methods ----
@@ -169,6 +210,9 @@ public class FontFamily_Delegate {
return false;
}
+
+ // ---- private helper methods ----
+
private void init() {
for (String path : mPath) {
addFont(path);
@@ -183,51 +227,9 @@ public class FontFamily_Delegate {
}
FontInfo fontInfo = new FontInfo();
fontInfo.mFont = font;
- addFontMetadata(fontInfo, path);
+ fontInfo.mStyle = getFontStyle(path);
// TODO ensure that mFonts doesn't have the font with this style already.
mFonts.add(fontInfo);
return true;
}
-
- private static void addFontMetadata(FontInfo fontInfo, String path) {
- int style = Font.PLAIN;
- String fontName = path.substring(path.lastIndexOf('/'), path.length());
- if (fontName.endsWith(FONT_SUFFIX_BOLDITALIC)) {
- style = Font.BOLD | Font.ITALIC;
- } else if (fontName.endsWith(FONT_SUFFIX_BOLD)) {
- style = Font.BOLD;
- } else if (fontName.endsWith(FONT_SUFFIX_ITALIC)) {
- style = Font.ITALIC;
- }
- fontInfo.mStyle = style;
- }
-
- private static Font loadFont(String path) {
- if (path.startsWith(SYSTEM_FONTS) ) {
- String relativePath = path.substring(SYSTEM_FONTS.length());
- File f = new File(sFontLocation, relativePath);
-
- try {
- return Font.createFont(Font.TRUETYPE_FONT, f);
- } catch (Exception e) {
- Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
- String.format("Unable to load font %1$s", relativePath),
- e /*throwable*/, null /*data*/);
- }
- } else {
- Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
- "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.",
- null /*throwable*/, null /*data*/);
- }
-
- return null;
- }
-
-
- // ---- Public helper class ----
-
- public enum FontVariant {
- // The order needs to be kept in sync with android.graphics.FontFamily.
- NONE, COMPACT, ELEGANT
- }
}