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

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

Layoutlib fixes for L [DO NOT MERGE]

This adds the new delegates that were missing. This starts the work on
changes related to Minikin Fonts.

There are some changes related to TypedArray that still need to be
fixed.

Change-Id: Ic2397b64aa3f1f48926e849b14689c47d9ee7f8c
(cherry picked from commit 7ca3612094270183243938e79337c84effea7ad0)
parent 80962666
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -30,7 +30,6 @@ public class FontFamily {
    public long mNativePtr;

    public FontFamily() {
        mNativePtr = nCreateFamily();
        mNativePtr = nCreateFamily();
        if (mNativePtr == 0) {
            throw new RuntimeException();
+20 −3
Original line number Diff line number Diff line
@@ -66,6 +66,9 @@ public class Typeface {
    static Map<String, Typeface> sSystemFontMap;
    static FontFamily[] sFallbackFonts;

    static final String SYSTEM_FONTS_CONFIG = "system_fonts.xml";
    static final String FALLBACK_FONTS_CONFIG = "fallback_fonts.xml";

    /**
     * @hide
     */
@@ -249,10 +252,16 @@ public class Typeface {
        return fontFamily;
    }

    static {
    /*
     * (non-Javadoc)
     *
     * This should only be called once, from the static class initializer block.
     */
    private static void init() {
        // Load font config and initialize Minikin state
        String systemConfigFilename = "/system/etc/system_fonts.xml";
        String configFilename = "/system/etc/fallback_fonts.xml";
        File systemFontConfigLocation = getSystemFontConfigLocation();
        File systemConfigFilename = new File(systemFontConfigLocation, SYSTEM_FONTS_CONFIG);
        File configFilename = new File(systemFontConfigLocation, FALLBACK_FONTS_CONFIG);
        try {
            // TODO: throws an exception non-Minikin builds, to fail early;
            // remove when Minikin-only
@@ -301,7 +310,10 @@ public class Typeface {
        } catch (XmlPullParserException e) {
            Log.e(TAG, "XML parse exception for " + configFilename);
        }
    }

    static {
        init();
        // Set up defaults and typefaces exposed in public API
        DEFAULT         = create((String) null, 0);
        DEFAULT_BOLD    = create((String) null, Typeface.BOLD);
@@ -315,6 +327,11 @@ public class Typeface {
            create((String) null, Typeface.ITALIC),
            create((String) null, Typeface.BOLD_ITALIC),
        };

    }

    private static File getSystemFontConfigLocation() {
        return new File("/system/etc/");
    }

    @Override
+7 −0
Original line number Diff line number Diff line
@@ -97,6 +97,13 @@ public class Resources_Theme_Delegate {
        return found;
    }

    @LayoutlibDelegate
    /*package*/ static TypedArray resolveAttributes(Resources thisResources, Theme thisTheme,
            int[] values, int[] attrs) {
        // FIXME
        return null;
    }

    // ---- private helper methods ----

    private static boolean setupResources(Theme thisTheme) {
+6 −0
Original line number Diff line number Diff line
@@ -27,4 +27,10 @@ public class TypedArray_Delegate {
        // pass
        return false;
    }

    @LayoutlibDelegate
    /*package*/ static TypedArray obtain(Resources res, int len) {
        // FIXME
        return null;
    }
}
+34 −24
Original line number Diff line number Diff line
@@ -18,9 +18,11 @@ package android.graphics;

import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Toolkit;
import java.awt.font.FontRenderContext;
import java.awt.font.GlyphVector;
import java.awt.geom.Rectangle2D;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

@@ -41,7 +43,7 @@ public class BidiRenderer {
        int limit;
        boolean isRtl;
        int scriptCode;
        FontInfo font;
        Font font;

        public ScriptRun(int start, int limit, boolean isRtl) {
            this.start = start;
@@ -51,9 +53,10 @@ public class BidiRenderer {
        }
    }

    private Graphics2D mGraphics;
    private Paint_Delegate mPaint;
    private final Graphics2D mGraphics;
    private final Paint_Delegate mPaint;
    private char[] mText;
    private List<Font> mFonts;
    // Bounds of the text drawn so far.
    private RectF mBounds;
    private float mBaseline;
@@ -68,6 +71,10 @@ public class BidiRenderer {
        mGraphics = graphics;
        mPaint = paint;
        mText = text;
        mFonts = new ArrayList<Font>(paint.getFonts().size());
        for (FontInfo fontInfo : paint.getFonts()) {
            mFonts.add(fontInfo.mFont);
        }
    }

    /**
@@ -94,7 +101,7 @@ public class BidiRenderer {
        // the script runs.
        mBounds = new RectF(x, y, x, y);
        mBaseline = y;
        for (ScriptRun run : getScriptRuns(mText, start, limit, isRtl, mPaint.getFonts())) {
        for (ScriptRun run : getScriptRuns(mText, start, limit, isRtl, mFonts)) {
            int flag = Font.LAYOUT_NO_LIMIT_CONTEXT | Font.LAYOUT_NO_START_CONTEXT;
            flag |= isRtl ? Font.LAYOUT_RIGHT_TO_LEFT : Font.LAYOUT_LEFT_TO_RIGHT;
            renderScript(run.start, run.limit, run.font, flag, advances, advancesIndex, draw);
@@ -108,16 +115,15 @@ public class BidiRenderer {
     * much as possible. This also implements a fallback mechanism to render characters that cannot
     * be drawn using the preferred font.
     */
    private void renderScript(int start, int limit, FontInfo preferredFont, int flag,
    private void renderScript(int start, int limit, Font preferredFont, int flag,
            float[] advances, int advancesIndex, boolean draw) {
        List<FontInfo> fonts = mPaint.getFonts();
        if (fonts == null || preferredFont == null) {
        if (mFonts.size() == 0 || preferredFont == null) {
            return;
        }

        while (start < limit) {
            boolean foundFont = false;
            int canDisplayUpTo = preferredFont.mFont.canDisplayUpTo(mText, start, limit);
            int canDisplayUpTo = preferredFont.canDisplayUpTo(mText, start, limit);
            if (canDisplayUpTo == -1) {
                // We can draw all characters in the text.
                render(start, limit, preferredFont, flag, advances, advancesIndex, draw);
@@ -133,8 +139,8 @@ public class BidiRenderer {
            // The current character cannot be drawn with the preferred font. Cycle through all the
            // fonts to check which one can draw it.
            int charCount = Character.isHighSurrogate(mText[start]) ? 2 : 1;
            for (FontInfo font : fonts) {
                canDisplayUpTo = font.mFont.canDisplayUpTo(mText, start, start + charCount);
            for (Font font : mFonts) {
                canDisplayUpTo = font.canDisplayUpTo(mText, start, start + charCount);
                if (canDisplayUpTo == -1) {
                    render(start, start+charCount, font, flag, advances, advancesIndex, draw);
                    start += charCount;
@@ -160,15 +166,19 @@ public class BidiRenderer {
     * Renders the text to the right of the bounds with the given font.
     * @param font The font to render the text with.
     */
    private void render(int start, int limit, FontInfo font, int flag, float[] advances,
    private void render(int start, int limit, Font font, int flag, float[] advances,
            int advancesIndex, boolean draw) {

        // Since the metrics don't have anti-aliasing set, we create a new FontRenderContext with
        // the anti-aliasing set.
        FontRenderContext f = font.mMetrics.getFontRenderContext();
        FontRenderContext frc = new FontRenderContext(f.getTransform(), mPaint.isAntiAliased(),
                f.usesFractionalMetrics());
        GlyphVector gv = font.mFont.layoutGlyphVector(frc, mText, start, limit, flag);
        FontRenderContext frc;
        if (mGraphics != null) {
            frc = mGraphics.getFontRenderContext();
        } else {
            frc = Toolkit.getDefaultToolkit().getFontMetrics(font).getFontRenderContext();
            // Metrics obtained this way don't have anti-aliasing set. So,
            // we create a new FontRenderContext with anti-aliasing set.
            frc = new FontRenderContext(font.getTransform(), mPaint.isAntiAliased(), frc.usesFractionalMetrics());
        }
        GlyphVector gv = font.layoutGlyphVector(frc, mText, start, limit, flag);
        int ng = gv.getNumGlyphs();
        int[] ci = gv.getGlyphCharIndices(0, ng, null);
        if (advances != null) {
@@ -206,7 +216,7 @@ public class BidiRenderer {
    }

    /* package */  static List<ScriptRun> getScriptRuns(char[] text, int start, int limit,
            boolean isRtl, List<FontInfo> fonts) {
            boolean isRtl, List<Font> fonts) {
        LinkedList<ScriptRun> scriptRuns = new LinkedList<ScriptRun>();

        int count = limit - start;
@@ -225,10 +235,10 @@ public class BidiRenderer {

    // TODO: Replace this method with one which returns the font based on the scriptCode.
    private static void setScriptFont(char[] text, ScriptRun run,
            List<FontInfo> fonts) {
        for (FontInfo fontInfo : fonts) {
            if (fontInfo.mFont.canDisplayUpTo(text, run.start, run.limit) == -1) {
                run.font = fontInfo;
            List<Font> fonts) {
        for (Font font : fonts) {
            if (font.canDisplayUpTo(text, run.start, run.limit) == -1) {
                run.font = font;
                return;
            }
        }
Loading