Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit b0d767dc authored by Deepanshu Gupta's avatar Deepanshu Gupta
Browse files

Skip loading otf fonts on Java 6

Change-Id: I9668b84812e9be333d10603657dd911024fc292d
parent 0c5cd965
Loading
Loading
Loading
Loading
+19 −0
Original line number Original line Diff line number Diff line
@@ -16,6 +16,9 @@


package android.graphics;
package android.graphics;


import com.android.ide.common.rendering.api.LayoutLog;
import com.android.layoutlib.bridge.Bridge;

import java.awt.Font;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.Toolkit;
@@ -58,6 +61,8 @@ public class BidiRenderer {
    private final Graphics2D mGraphics;
    private final Graphics2D mGraphics;
    private final Paint_Delegate mPaint;
    private final Paint_Delegate mPaint;
    private char[] mText;
    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;
    private List<Font> mFonts;
    // Bounds of the text drawn so far.
    // Bounds of the text drawn so far.
    private RectF mBounds;
    private RectF mBounds;
@@ -169,6 +174,10 @@ public class BidiRenderer {
            // fonts to check which one can draw it.
            // fonts to check which one can draw it.
            int charCount = Character.isHighSurrogate(mText[start]) ? 2 : 1;
            int charCount = Character.isHighSurrogate(mText[start]) ? 2 : 1;
            for (Font font : mFonts) {
            for (Font font : mFonts) {
                if (font == null) {
                    logFontWarning();
                    continue;
                }
                canDisplayUpTo = font.canDisplayUpTo(mText, start, start + charCount);
                canDisplayUpTo = font.canDisplayUpTo(mText, start, start + charCount);
                if (canDisplayUpTo == -1) {
                if (canDisplayUpTo == -1) {
                    render(start, start+charCount, font, flag, advances, advancesIndex, draw);
                    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.
     * Renders the text to the right of the bounds with the given font.
     * @param font The font to render the text with.
     * @param font The font to render the text with.
@@ -266,6 +281,10 @@ public class BidiRenderer {
    private static void setScriptFont(char[] text, ScriptRun run,
    private static void setScriptFont(char[] text, ScriptRun run,
            List<Font> fonts) {
            List<Font> fonts) {
        for (Font font : fonts) {
        for (Font font : fonts) {
            if (font == null) {
                logFontWarning();
                continue;
            }
            if (font.canDisplayUpTo(text, run.start, run.limit) == -1) {
            if (font.canDisplayUpTo(text, run.start, run.limit) == -1) {
                run.font = font;
                run.font = font;
                return;
                return;
+10 −3
Original line number Original line Diff line number Diff line
@@ -24,6 +24,7 @@ import com.android.tools.layoutlib.annotations.LayoutlibDelegate;
import android.content.res.AssetManager;
import android.content.res.AssetManager;


import java.awt.Font;
import java.awt.Font;
import java.awt.FontFormatException;
import java.io.File;
import java.io.File;
import java.util.ArrayList;
import java.util.ArrayList;
import java.util.List;
import java.util.List;
@@ -152,14 +153,20 @@ public class FontFamily_Delegate {
            try {
            try {
                return Font.createFont(Font.TRUETYPE_FONT, f);
                return Font.createFont(Font.TRUETYPE_FONT, f);
            } catch (Exception e) {
            } 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,
                Bridge.getLog().fidelityWarning(LayoutLog.TAG_BROKEN,
                        String.format("Unable to load font %1$s", relativePath),
                        String.format("Unable to load font %1$s", relativePath),
                        e /*throwable*/, null /*data*/);
                        e, null);
            }
            }
        } else {
        } else {
            Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
            Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
                    "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.",
                    "Only platform fonts located in " + SYSTEM_FONTS + "can be loaded.",
                    null /*throwable*/, null /*data*/);
                    null, null);
        }
        }


        return null;
        return null;
@@ -206,7 +213,7 @@ public class FontFamily_Delegate {
    @LayoutlibDelegate
    @LayoutlibDelegate
    /*package*/ static boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
    /*package*/ static boolean nAddFontFromAsset(long nativeFamily, AssetManager mgr, String path) {
        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
        Bridge.getLog().fidelityWarning(LayoutLog.TAG_UNSUPPORTED,
                "FontFamily.addFontFromAsset is not supported.", null /*throwable*/, null /*data*/);
                "FontFamily.addFontFromAsset is not supported.", null, null);
        return false;
        return false;
    }
    }