Skip to content

Commit e5493f3

Browse files
Xavier DucrohetAndroid (Google) Code Review
authored andcommitted
Merge "Layoutlib: Typeface support for loading fonts manually."
2 parents 0bb83a2 + 1409409 commit e5493f3

File tree

2 files changed

+61
-6
lines changed

2 files changed

+61
-6
lines changed

tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java

Lines changed: 38 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import android.content.res.AssetManager;
2626

2727
import java.awt.Font;
28+
import java.io.File;
2829
import java.util.ArrayList;
2930
import java.util.List;
3031

@@ -43,6 +44,8 @@
4344
*/
4445
public final class Typeface_Delegate {
4546

47+
private static final String SYSTEM_FONTS = "/system/fonts/";
48+
4649
// ---- delegate manager ----
4750
private static final DelegateManager<Typeface_Delegate> sManager =
4851
new DelegateManager<Typeface_Delegate>(Typeface_Delegate.class);
@@ -143,9 +146,31 @@ public List<Font> getFonts() {
143146

144147
@LayoutlibDelegate
145148
/*package*/ static synchronized int nativeCreateFromFile(String path) {
146-
Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
147-
"Typeface.createFromFile() is not supported.", null /*throwable*/, null /*data*/);
148-
return 0;
149+
if (path.startsWith(SYSTEM_FONTS) ) {
150+
String relativePath = path.substring(SYSTEM_FONTS.length());
151+
File f = new File(sFontLoader.getOsFontsLocation(), relativePath);
152+
153+
try {
154+
Font font = Font.createFont(Font.TRUETYPE_FONT, f);
155+
if (font != null) {
156+
Typeface_Delegate newDelegate = new Typeface_Delegate(font);
157+
return sManager.addNewDelegate(newDelegate);
158+
}
159+
} catch (Exception e) {
160+
Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
161+
String.format("Unable to load font %1$s", relativePath),
162+
null /*throwable*/, null /*data*/);
163+
}
164+
} else {
165+
Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
166+
"Typeface.createFromFile() can only work with platform fonts located in " +
167+
SYSTEM_FONTS,
168+
null /*throwable*/, null /*data*/);
169+
}
170+
171+
172+
// return a copy of the base font
173+
return nativeCreate(null, 0);
149174
}
150175

151176
@LayoutlibDelegate
@@ -175,6 +200,16 @@ private Typeface_Delegate(String family, int style) {
175200
mStyle = style;
176201
}
177202

203+
private Typeface_Delegate(Font font) {
204+
mFamily = font.getFamily();
205+
mStyle = Typeface.NORMAL;
206+
207+
mFonts = sFontLoader.getFallbackFonts(mStyle);
208+
209+
// insert the font glyph first.
210+
mFonts.add(0, font);
211+
}
212+
178213
private void init() {
179214
mFonts = sFontLoader.getFont(mFamily, mStyle);
180215
}

tools/layoutlib/bridge/src/com/android/layoutlib/bridge/impl/FontLoader.java

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,8 @@ public final class FontLoader {
7575
private static final List<FontInfo> mMainFonts = new ArrayList<FontInfo>();
7676
private static final List<FontInfo> mFallbackFonts = new ArrayList<FontInfo>();
7777

78+
private final String mOsFontsLocation;
79+
7880
public static FontLoader create(String fontOsLocation) {
7981
try {
8082
SAXParserFactory parserFactory = SAXParserFactory.newInstance();
@@ -89,7 +91,7 @@ public static FontLoader create(String fontOsLocation) {
8991
handler = parseFontFile(parserFactory, fontOsLocation, FONTS_FALLBACK);
9092
List<FontInfo> fallbackFonts = handler.getFontList();
9193

92-
return new FontLoader(systemFonts, fallbackFonts);
94+
return new FontLoader(fontOsLocation, systemFonts, fallbackFonts);
9395
} catch (ParserConfigurationException e) {
9496
// return null below
9597
} catch (SAXException e) {
@@ -116,11 +118,18 @@ private static FontHandler parseFontFile(SAXParserFactory parserFactory,
116118
return definitionParser;
117119
}
118120

119-
private FontLoader(List<FontInfo> fontList, List<FontInfo> fallBackList) {
121+
private FontLoader(String fontOsLocation,
122+
List<FontInfo> fontList, List<FontInfo> fallBackList) {
123+
mOsFontsLocation = fontOsLocation;
120124
mMainFonts.addAll(fontList);
121125
mFallbackFonts.addAll(fallBackList);
122126
}
123127

128+
129+
public String getOsFontsLocation() {
130+
return mOsFontsLocation;
131+
}
132+
124133
/**
125134
* Returns a {@link Font} object given a family name and a style value (constant in
126135
* {@link Typeface}).
@@ -146,14 +155,25 @@ public synchronized List<Font> getFont(String family, int style) {
146155
}
147156
}
148157

149-
// add all the fallback fonts
158+
// add all the fallback fonts for the given style
150159
for (FontInfo info : mFallbackFonts) {
151160
result.add(info.font[style]);
152161
}
153162

154163
return result;
155164
}
156165

166+
167+
public synchronized List<Font> getFallbackFonts(int style) {
168+
List<Font> result = new ArrayList<Font>();
169+
// add all the fallback fonts
170+
for (FontInfo info : mFallbackFonts) {
171+
result.add(info.font[style]);
172+
}
173+
return result;
174+
}
175+
176+
157177
private final static class FontInfo {
158178
final Font[] font = new Font[4]; // Matches the 4 type-face styles.
159179
final Set<String> families;

0 commit comments

Comments
 (0)