am e5493f31: Merge "Layoutlib: Typeface support for loading fonts manually."
* commit 'e5493f31152ebb2e9c9818699ba6f853a9a59894': Layoutlib: Typeface support for loading fonts manually.
This commit is contained in:
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user