diff --git a/core/java/android/content/res/FontResourcesParser.java b/core/java/android/content/res/FontResourcesParser.java index 0edbc70c88395..b21ccf12d1333 100644 --- a/core/java/android/content/res/FontResourcesParser.java +++ b/core/java/android/content/res/FontResourcesParser.java @@ -19,6 +19,7 @@ import com.android.internal.R; import android.annotation.NonNull; import android.annotation.Nullable; import android.util.AttributeSet; +import android.util.Log; import android.util.Xml; import org.xmlpull.v1.XmlPullParser; @@ -34,6 +35,7 @@ import java.util.List; * @hide */ public class FontResourcesParser { + private static final String TAG = "FontResourcesParser"; private static final int NORMAL_WEIGHT = 400; private static final int ITALIC = 1; @@ -79,12 +81,10 @@ public class FontResourcesParser { private boolean mItalic; private int mResourceId; - public FontFileResourceEntry(@NonNull String fileName, int weight, boolean italic, - int resourceId) { + public FontFileResourceEntry(@NonNull String fileName, int weight, boolean italic) { mFileName = fileName; mWeight = weight; mItalic = italic; - mResourceId = resourceId; } public @NonNull String getFileName() { @@ -98,10 +98,6 @@ public class FontResourcesParser { public boolean isItalic() { return mItalic; } - - public int getResourceId() { - return mResourceId; - } } // A class represents file based font-family element in xml file. @@ -140,6 +136,7 @@ public class FontResourcesParser { return readFamily(parser, resources); } else { skip(parser); + Log.e(TAG, "Failed to find font-family tag"); return null; } } @@ -184,7 +181,10 @@ public class FontResourcesParser { if (parser.getEventType() != XmlPullParser.START_TAG) continue; String tag = parser.getName(); if (tag.equals("font")) { - fonts.add(readFont(parser, resources)); + final FontFileResourceEntry entry = readFont(parser, resources); + if (entry != null) { + fonts.add(entry); + } } else { skip(parser); } @@ -203,12 +203,14 @@ public class FontResourcesParser { int weight = array.getInt(R.styleable.FontFamilyFont_fontWeight, NORMAL_WEIGHT); boolean isItalic = ITALIC == array.getInt(R.styleable.FontFamilyFont_fontStyle, 0); String filename = array.getString(R.styleable.FontFamilyFont_font); - int resourceId = array.getResourceId(R.styleable.FontFamilyFont_font, 0); array.recycle(); while (parser.next() != XmlPullParser.END_TAG) { skip(parser); } - return new FontFileResourceEntry(filename, weight, isItalic, resourceId); + if (filename == null) { + return null; + } + return new FontFileResourceEntry(filename, weight, isItalic); } private static void skip(XmlPullParser parser) throws XmlPullParserException, IOException { diff --git a/core/java/android/content/res/ResourcesImpl.java b/core/java/android/content/res/ResourcesImpl.java index 02ddc89f177ee..bdfef8326fdf4 100644 --- a/core/java/android/content/res/ResourcesImpl.java +++ b/core/java/android/content/res/ResourcesImpl.java @@ -789,7 +789,6 @@ public class ResourcesImpl { final FontResourcesParser.FamilyResourceEntry familyEntry = FontResourcesParser.parse(rp, wrapper); if (familyEntry == null) { - Log.e(TAG, "Failed to find font-family tag"); return null; } return Typeface.createFromResources(familyEntry, mAssets, file); diff --git a/graphics/java/android/graphics/Typeface.java b/graphics/java/android/graphics/Typeface.java index ad6ea2bdb284c..f38d8d26757f4 100644 --- a/graphics/java/android/graphics/Typeface.java +++ b/graphics/java/android/graphics/Typeface.java @@ -230,6 +230,7 @@ public class Typeface { FontFamily fontFamily = new FontFamily(); for (final FontFileResourceEntry fontFile : filesEntry.getEntries()) { + // TODO: Add ttc and variation font support. (b/37853920) if (!fontFamily.addFontFromAssetManager(mgr, fontFile.getFileName(), 0 /* resourceCookie */, false /* isAsset */, 0 /* ttcIndex */, fontFile.getWeight(), fontFile.isItalic() ? STYLE_ITALIC : STYLE_NORMAL, @@ -237,11 +238,9 @@ public class Typeface { return null; } } - // Due to backward compatibility, even if the font is not supported by our font stack, - // we need to place the empty font at the first place. The typeface with empty font - // behaves different from default typeface especially in fallback font selection. - fontFamily.allowUnsupportedFont(); - fontFamily.freeze(); + if (!fontFamily.freeze()) { + return null; + } FontFamily[] familyChain = { fontFamily }; typeface = createFromFamiliesWithDefault(familyChain, RESOLVE_BY_FONT_TABLE, RESOLVE_BY_FONT_TABLE);