summaryrefslogtreecommitdiffstats
path: root/graphics
diff options
context:
space:
mode:
Diffstat (limited to 'graphics')
-rw-r--r--graphics/java/android/graphics/FontListParser.java62
-rw-r--r--graphics/java/android/graphics/Typeface.java11
2 files changed, 47 insertions, 26 deletions
diff --git a/graphics/java/android/graphics/FontListParser.java b/graphics/java/android/graphics/FontListParser.java
index 1ca464d..d789690 100644
--- a/graphics/java/android/graphics/FontListParser.java
+++ b/graphics/java/android/graphics/FontListParser.java
@@ -21,6 +21,7 @@ import android.util.Xml;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
+import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
@@ -76,30 +77,53 @@ public class FontListParser {
}
/* Parse fallback list (no names) */
- public static Config parse(File configFilename, File fontDir)
+ public static Config parse(File configFilename, String fontDir)
throws XmlPullParserException, IOException {
- FileInputStream in = new FileInputStream(configFilename);
- if (isLegacyFormat(configFilename)) {
- return parseLegacyFormat(in, fontDir.getAbsolutePath());
- } else {
- return parseNormalFormat(in, fontDir.getAbsolutePath());
- }
+ FileInputStream in = null;
+ in = new FileInputStream(configFilename);
+ return FontListParser.parse(in, fontDir);
}
- private static boolean isLegacyFormat(File configFilename)
+ /* Parse fallback list (no names) */
+ public static Config parse(InputStream in, String fontDir)
throws XmlPullParserException, IOException {
- FileInputStream in = new FileInputStream(configFilename);
- boolean isLegacy = false;
+ BufferedInputStream bis = null;
try {
- XmlPullParser parser = Xml.newPullParser();
- parser.setInput(in, null);
- parser.nextTag();
- parser.require(XmlPullParser.START_TAG, null, "familyset");
- String version = parser.getAttributeValue(null, "version");
- isLegacy = version == null;
+ // wrap input stream in a BufferedInputStream, if it's not already, for mark support
+ if (!(in instanceof BufferedInputStream)) {
+ bis = new BufferedInputStream(in);
+ } else {
+ bis = (BufferedInputStream) in;
+ }
+ // mark the beginning so we can reset to this position after checking format
+ bis.mark(in.available());
+ if (isLegacyFormat(bis)) {
+ return parseLegacyFormat(bis, fontDir);
+ } else {
+ return parseNormalFormat(bis, fontDir);
+ }
} finally {
- in.close();
+ if (bis != null) bis.close();
}
+ }
+
+ public static boolean isLegacyFormat(InputStream in)
+ throws XmlPullParserException, IOException {
+ if (!in.markSupported()) {
+ throw new IllegalArgumentException("InputStream does not support mark");
+ }
+ boolean isLegacy = false;
+
+ XmlPullParser parser = Xml.newPullParser();
+ parser.setInput(in, null);
+ parser.nextTag();
+ parser.require(XmlPullParser.START_TAG, null, "familyset");
+ String version = parser.getAttributeValue(null, "version");
+ isLegacy = version == null;
+
+ // reset the stream so we can read it
+ in.reset();
+
return isLegacy;
}
@@ -116,14 +140,10 @@ public class FontListParser {
public static Config parseNormalFormat(InputStream in, String dirName)
throws XmlPullParserException, IOException {
- try {
XmlPullParser parser = Xml.newPullParser();
parser.setInput(in, null);
parser.nextTag();
return readFamilies(parser, dirName);
- } finally {
- in.close();
- }
}
private static Config readFamilies(XmlPullParser parser, String dirPath)
diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java
index 68c9c0b..a64c0af 100644
--- a/graphics/java/android/graphics/Typeface.java
+++ b/graphics/java/android/graphics/Typeface.java
@@ -369,7 +369,8 @@ public class Typeface {
}
try {
- FontListParser.Config fontConfig = FontListParser.parse(configFile, fontDir);
+ FontListParser.Config fontConfig = FontListParser.parse(configFile,
+ fontDir.getAbsolutePath());
FontListParser.Config systemFontConfig = null;
// If the fonts are coming from a theme, we will need to make sure that we include
@@ -377,7 +378,7 @@ public class Typeface {
// NOTE: All the system font families without names ALWAYS get added.
if (configFile == themeConfigFile) {
systemFontConfig = FontListParser.parse(systemConfigFile,
- getSystemFontDirLocation());
+ getSystemFontDirLocation().getAbsolutePath());
addMissingFontFamilies(systemFontConfig, fontConfig);
addMissingFontAliases(systemFontConfig, fontConfig);
addFallbackFontsForFamilyName(systemFontConfig, fontConfig, SANS_SERIF_FAMILY_NAME);
@@ -428,11 +429,11 @@ public class Typeface {
Log.w(TAG, "Didn't create default family (most likely, non-Minikin build)", e);
// TODO: normal in non-Minikin case, remove or make error when Minikin-only
} catch (FileNotFoundException e) {
- Log.e(TAG, "Error opening " + configFile);
+ Log.e(TAG, "Error opening " + configFile, e);
} catch (IOException e) {
- Log.e(TAG, "Error reading " + configFile);
+ Log.e(TAG, "Error reading " + configFile, e);
} catch (XmlPullParserException e) {
- Log.e(TAG, "XML parse exception for " + configFile);
+ Log.e(TAG, "XML parse exception for " + configFile, e);
}
}