Skip loading otf fonts on Java 6

Change-Id: I9668b84812e9be333d10603657dd911024fc292d
This commit is contained in:
Deepanshu Gupta
2014-08-04 13:57:56 -07:00
parent 0c5cd96522
commit b0d767dcf1
2 changed files with 29 additions and 3 deletions

View File

@@ -16,6 +16,9 @@
package android.graphics;
import com.android.ide.common.rendering.api.LayoutLog;
import com.android.layoutlib.bridge.Bridge;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Toolkit;
@@ -58,6 +61,8 @@ public class BidiRenderer {
private final Graphics2D mGraphics;
private final Paint_Delegate mPaint;
private char[] mText;
// This List can contain nulls. A null font implies that the we weren't able to load the font
// properly. So, if we encounter a situation where we try to use that font, log a warning.
private List<Font> mFonts;
// Bounds of the text drawn so far.
private RectF mBounds;
@@ -169,6 +174,10 @@ public class BidiRenderer {
// fonts to check which one can draw it.
int charCount = Character.isHighSurrogate(mText[start]) ? 2 : 1;
for (Font font : mFonts) {
if (font == null) {
logFontWarning();
continue;
}
canDisplayUpTo = font.canDisplayUpTo(mText, start, start + charCount);
if (canDisplayUpTo == -1) {
render(start, start+charCount, font, flag, advances, advancesIndex, draw);
@@ -191,6 +200,12 @@ public class BidiRenderer {
}
}
private static void logFontWarning() {
Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
"Some fonts could not be loaded. The rendering may not be perfect. " +
"Try running the IDE with JRE 7.", null, null);
}
/**
* Renders the text to the right of the bounds with the given font.
* @param font The font to render the text with.
@@ -266,6 +281,10 @@ public class BidiRenderer {
private static void setScriptFont(char[] text, ScriptRun run,
List<Font> fonts) {
for (Font font : fonts) {
if (font == null) {
logFontWarning();
continue;
}
if (font.canDisplayUpTo(text, run.start, run.limit) == -1) {
run.font = font;
return;

View File

@@ -24,6 +24,7 @@ import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
import android.content.res.AssetManager;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.util.ArrayList;
import java.util.List;
@@ -152,14 +153,20 @@ public class FontFamily_Delegate {
try {
return Font.createFont(Font.TRUETYPE_FONT, f);
} catch (Exception e) {
if (path.endsWith(".otf") && e instanceof FontFormatException) {
// If we aren't able to load an Open Type font, don't log a warning just yet.
// We wait for a case where font is being used. Only then we try to log the
// warning.
return null;
}
Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
String.format("Unable to load font %1$s", relativePath),
e /*throwable*/, null /*data*/);
e, null);
}
} else {
Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
"Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.",
null /*throwable*/, null /*data*/);
null, null);
}
return null;
@@ -206,7 +213,7 @@ public class FontFamily_Delegate {
@LayoutlibDelegate
/*package*/ static boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
"FontFamily.addFontFromAsset is not supported.", null /*throwable*/, null /*data*/);
"FontFamily.addFontFromAsset is not supported.", null, null);
return false;
}