diff options
| author | 2014-08-09 14:14:32 -0700 | |
|---|---|---|
| committer | 2014-08-11 18:11:37 -0700 | |
| commit | e644ff8d92ba040d11636be0fb6c433b52bcc6c2 (patch) | |
| tree | e3b08352b6054f35e9e610fce98538ba4209b58d | |
| parent | 3afd00e9f2b55a21ca378d6e520d24283dbb62e0 (diff) | |
Don't show warnings for fonts not bundled.
The extended font set, used on Nexus 5, new Nexus 7 etc., uses OpenType
CFF fonts which don't work well with Java. These fonts are deliberately
excluded from the SDK build.
This change skips the warnings for the missing fonts. However, we still
show warnings for other fonts that failed to load. If the set of fonts
bundled with the SDK changes, we will need to update the list of missing
fonts in FontFamily_Delegate.java.
Change-Id: I3197b5350d048daa09512e8024188909179b1799
4 files changed, 54 insertions, 1 deletions
diff --git a/tools/layoutlib/bridge/src/android/graphics/BidiRenderer.java b/tools/layoutlib/bridge/src/android/graphics/BidiRenderer.java index 154851ba1813..a4a3b7d41c45 100644 --- a/tools/layoutlib/bridge/src/android/graphics/BidiRenderer.java +++ b/tools/layoutlib/bridge/src/android/graphics/BidiRenderer.java @@ -80,6 +80,10 @@ public class BidiRenderer { mText = text; mFonts = new ArrayList<Font>(paint.getFonts().size()); for (FontInfo fontInfo : paint.getFonts()) { + if (fontInfo == null) { + mFonts.add(null); + continue; + } mFonts.add(fontInfo.mFont); } mBounds = new RectF(); diff --git a/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java index de3307f4a4b5..bf4e288ced91 100644 --- a/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/FontFamily_Delegate.java @@ -27,7 +27,11 @@ import java.awt.Font; import java.awt.FontFormatException; import java.io.File; import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashSet; import java.util.List; +import java.util.Set; import static android.graphics.Typeface_Delegate.SYSTEM_FONTS; @@ -51,6 +55,14 @@ public class FontFamily_Delegate { private static final String FONT_SUFFIX_BOLD = "Bold.ttf"; private static final String FONT_SUFFIX_ITALIC = "Italic.ttf"; + private static final Set<String> MISSING_FONTS = + Collections.unmodifiableSet(new HashSet<String>(Arrays.asList( + "NotoSansHans-Regular.otf", + "NotoSansHant-Regular.otf", + "NotoSansJP-Regular.otf", + "NotoSansKR-Regular.otf" + ))); + /** * A class associating {@link Font} with its metadata. */ @@ -82,6 +94,8 @@ public class FontFamily_Delegate { private FontVariant mVariant; // Path of fonts that haven't been created since sFontLoader hasn't been initialized. private List<String> mPath = new ArrayList<String>(); + /** @see #isValid() */ + private boolean mValid = false; // ---- Public helper class ---- @@ -132,6 +146,16 @@ public class FontFamily_Delegate { return mVariant; } + /** + * Returns if the FontFamily should contain any fonts. If this returns true and + * {@link #getFont(int)} returns an empty list, it means that an error occurred while loading + * the fonts. However, some fonts are deliberately skipped, for example they are not bundled + * with the SDK. In such a case, this method returns false. + */ + public boolean isValid() { + return mValid; + } + /*package*/ static int getFontStyle(String path) { int style = Font.PLAIN; String fontName = path.substring(path.lastIndexOf('/')); @@ -201,6 +225,13 @@ public class FontFamily_Delegate { /*package*/ static boolean nAddFont(long nativeFamily, String path) { FontFamily_Delegate delegate = getDelegate(nativeFamily); if (delegate != null) { + // If the font to be added is known to be missing from the SDK, don't try to load it and + // mark the FontFamily to be not valid. + if (path.startsWith(SYSTEM_FONTS) && + MISSING_FONTS.contains(path.substring(SYSTEM_FONTS.length()))) { + return delegate.mValid = false; + } + delegate.mValid = true; if (sFontLocation == null) { delegate.mPath.add(path); return true; diff --git a/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java index 73d67a73d61b..7b07404ca401 100644 --- a/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/Paint_Delegate.java @@ -65,6 +65,8 @@ public class Paint_Delegate { new DelegateManager<Paint_Delegate>(Paint_Delegate.class); // ---- delegate helper data ---- + + // This list can contain null elements. private List<FontInfo> mFonts; // ---- delegate data ---- @@ -1171,6 +1173,12 @@ public class Paint_Delegate { // and skew info. ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size()); for (Font font : fonts) { + if (font == null) { + // If the font is null, add null to infoList. When rendering the text, if this + // null is reached, a warning will be logged. + infoList.add(null); + continue; + } FontInfo info = new FontInfo(); info.mFont = font.deriveFont(mTextSize); if (mTextScaleX != 1.0 || mTextSkewX != 0) { diff --git a/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java b/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java index f044def64697..72fe61c88923 100644 --- a/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java +++ b/tools/layoutlib/bridge/src/android/graphics/Typeface_Delegate.java @@ -74,6 +74,10 @@ public final class Typeface_Delegate { * Return a list of fonts that match the style and variant. The list is ordered according to * preference of fonts. * + * The list may contain null when the font failed to load. If null is reached when trying to + * render with this list of fonts, then a warning should be logged letting the user know that + * some font failed to load. + * * @param variant The variant preferred. Can only be {@link FontVariant#COMPACT} or * {@link FontVariant#ELEGANT} */ @@ -83,7 +87,7 @@ public final class Typeface_Delegate { List<Font> fonts = new ArrayList<Font>(mFontFamilies.length); for (int i = 0; i < mFontFamilies.length; i++) { FontFamily_Delegate ffd = mFontFamilies[i]; - if (ffd != null) { + if (ffd != null && ffd.isValid()) { Font font = ffd.getFont(mStyle); if (font != null) { FontVariant ffdVariant = ffd.getVariant(); @@ -107,6 +111,12 @@ public final class Typeface_Delegate { } else { fonts.add(font2); } + } else { + // The FontFamily is valid but doesn't contain any matching font. This means + // that the font failed to load. We add null to the list of fonts. Don't throw + // the warning just yet. If this is a non-english font, we don't want to warn + // users who are trying to render only english text. + fonts.add(null); } } } |