Skip to content

Commit 556f941

Browse files
deepanshu-Android (Google) Code Review
authored andcommitted
Merge "Don't show warnings for fonts not bundled." into lmp-dev
2 parents b205fa6 + e644ff8 commit 556f941

4 files changed

Lines changed: 54 additions & 1 deletion

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,10 @@ public BidiRenderer(Graphics2D graphics, Paint_Delegate paint, char[] text) {
8080
mText = text;
8181
mFonts = new ArrayList<Font>(paint.getFonts().size());
8282
for (FontInfo fontInfo : paint.getFonts()) {
83+
if (fontInfo == null) {
84+
mFonts.add(null);
85+
continue;
86+
}
8387
mFonts.add(fontInfo.mFont);
8488
}
8589
mBounds = new RectF();

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,11 @@
2727
import java.awt.FontFormatException;
2828
import java.io.File;
2929
import java.util.ArrayList;
30+
import java.util.Arrays;
31+
import java.util.Collections;
32+
import java.util.HashSet;
3033
import java.util.List;
34+
import java.util.Set;
3135

3236
import static android.graphics.Typeface_Delegate.SYSTEM_FONTS;
3337

@@ -51,6 +55,14 @@ public class FontFamily_Delegate {
5155
private static final String FONT_SUFFIX_BOLD = "Bold.ttf";
5256
private static final String FONT_SUFFIX_ITALIC = "Italic.ttf";
5357

58+
private static final Set<String> MISSING_FONTS =
59+
Collections.unmodifiableSet(new HashSet<String>(Arrays.asList(
60+
"NotoSansHans-Regular.otf",
61+
"NotoSansHant-Regular.otf",
62+
"NotoSansJP-Regular.otf",
63+
"NotoSansKR-Regular.otf"
64+
)));
65+
5466
/**
5567
* A class associating {@link Font} with its metadata.
5668
*/
@@ -82,6 +94,8 @@ private static final class FontInfo {
8294
private FontVariant mVariant;
8395
// Path of fonts that haven't been created since sFontLoader hasn't been initialized.
8496
private List<String> mPath = new ArrayList<String>();
97+
/** @see #isValid() */
98+
private boolean mValid = false;
8599

86100

87101
// ---- Public helper class ----
@@ -132,6 +146,16 @@ public FontVariant getVariant() {
132146
return mVariant;
133147
}
134148

149+
/**
150+
* Returns if the FontFamily should contain any fonts. If this returns true and
151+
* {@link #getFont(int)} returns an empty list, it means that an error occurred while loading
152+
* the fonts. However, some fonts are deliberately skipped, for example they are not bundled
153+
* with the SDK. In such a case, this method returns false.
154+
*/
155+
public boolean isValid() {
156+
return mValid;
157+
}
158+
135159
/*package*/ static int getFontStyle(String path) {
136160
int style = Font.PLAIN;
137161
String fontName = path.substring(path.lastIndexOf('/'));
@@ -201,6 +225,13 @@ public FontVariant getVariant() {
201225
/*package*/ static boolean nAddFont(long nativeFamily, String path) {
202226
FontFamily_Delegate delegate = getDelegate(nativeFamily);
203227
if (delegate != null) {
228+
// If the font to be added is known to be missing from the SDK, don't try to load it and
229+
// mark the FontFamily to be not valid.
230+
if (path.startsWith(SYSTEM_FONTS) &&
231+
MISSING_FONTS.contains(path.substring(SYSTEM_FONTS.length()))) {
232+
return delegate.mValid = false;
233+
}
234+
delegate.mValid = true;
204235
if (sFontLocation == null) {
205236
delegate.mPath.add(path);
206237
return true;

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,8 @@ public class Paint_Delegate {
6565
new DelegateManager<Paint_Delegate>(Paint_Delegate.class);
6666

6767
// ---- delegate helper data ----
68+
69+
// This list can contain null elements.
6870
private List<FontInfo> mFonts;
6971

7072
// ---- delegate data ----
@@ -1171,6 +1173,12 @@ private void updateFontObject() {
11711173
// and skew info.
11721174
ArrayList<FontInfo> infoList = new ArrayList<FontInfo>(fonts.size());
11731175
for (Font font : fonts) {
1176+
if (font == null) {
1177+
// If the font is null, add null to infoList. When rendering the text, if this
1178+
// null is reached, a warning will be logged.
1179+
infoList.add(null);
1180+
continue;
1181+
}
11741182
FontInfo info = new FontInfo();
11751183
info.mFont = font.deriveFont(mTextSize);
11761184
if (mTextScaleX != 1.0 || mTextSkewX != 0) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ public static Typeface_Delegate getDelegate(long nativeTypeface) {
7474
* Return a list of fonts that match the style and variant. The list is ordered according to
7575
* preference of fonts.
7676
*
77+
* The list may contain null when the font failed to load. If null is reached when trying to
78+
* render with this list of fonts, then a warning should be logged letting the user know that
79+
* some font failed to load.
80+
*
7781
* @param variant The variant preferred. Can only be {@link FontVariant#COMPACT} or
7882
* {@link FontVariant#ELEGANT}
7983
*/
@@ -83,7 +87,7 @@ public List<Font> getFonts(FontVariant variant) {
8387
List<Font> fonts = new ArrayList<Font>(mFontFamilies.length);
8488
for (int i = 0; i < mFontFamilies.length; i++) {
8589
FontFamily_Delegate ffd = mFontFamilies[i];
86-
if (ffd != null) {
90+
if (ffd != null && ffd.isValid()) {
8791
Font font = ffd.getFont(mStyle);
8892
if (font != null) {
8993
FontVariant ffdVariant = ffd.getVariant();
@@ -107,6 +111,12 @@ public List<Font> getFonts(FontVariant variant) {
107111
} else {
108112
fonts.add(font2);
109113
}
114+
} else {
115+
// The FontFamily is valid but doesn't contain any matching font. This means
116+
// that the font failed to load. We add null to the list of fonts. Don't throw
117+
// the warning just yet. If this is a non-english font, we don't want to warn
118+
// users who are trying to render only english text.
119+
fonts.add(null);
110120
}
111121
}
112122
}

0 commit comments

Comments
 (0)