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

Commit 5a836f74 authored by ztenghui's avatar ztenghui Committed by Tenghui Zhu
Browse files

Add negative sign separation support in the pathData

bug:14585171

Change-Id: I61dec27856be09c44bb1d32ff61b3c3cd458cc34
parent f84bc66b
Loading
Loading
Loading
Loading
+66 −27
Original line number Diff line number Diff line
@@ -54,9 +54,11 @@ public class PathParser {
        ArrayList<PathDataNode> list = new ArrayList<PathDataNode>();
        while (end < pathData.length()) {
            end = nextStart(pathData, end);
            String s = pathData.substring(start, end);
            String s = pathData.substring(start, end).trim();
            if (s.length() > 0) {
                float[] val = getFloats(s);
                addNode(list, s.charAt(0), val);
            }

            start = end;
            end++;
@@ -135,6 +137,12 @@ public class PathParser {
        list.add(new PathDataNode(cmd, val));
    }

    private static class ExtractFloatResult {
        // We need to return the position of the next separator and whether the
        // next float starts with a '-'.
        int mEndPosition;
        boolean mEndWithNegSign;
    }

    /**
     * Parse the floats in the string.
@@ -148,20 +156,34 @@ public class PathParser {
            return new float[0];
        }
        try {
            float[] tmp = new float[s.length()];
            float[] results = new float[s.length()];
            int count = 0;
            int pos = 1, end;
            while ((end = extract(s, pos)) >= 0) {
                if (pos < end) {
                    tmp[count++] = Float.parseFloat(s.substring(pos, end));
            int startPosition = 1;
            int endPosition = 0;

            ExtractFloatResult result = new ExtractFloatResult();
            int totalLength = s.length();

            // The startPosition should always be the first character of the
            // current number, and endPosition is the character after the current
            // number.
            while (startPosition < totalLength) {
                extract(s, startPosition, result);
                endPosition = result.mEndPosition;

                if (startPosition < endPosition) {
                    results[count++] = Float.parseFloat(
                            s.substring(startPosition, endPosition));
                }
                pos = end + 1;

                if (result.mEndWithNegSign) {
                    // Keep the '-' sign with next number.
                    startPosition = endPosition;
                } else {
                    startPosition = endPosition + 1;
                }
            // handle the final float if there is one
            if (pos < s.length()) {
                tmp[count++] = Float.parseFloat(s.substring(pos, s.length()));
            }
            return Arrays.copyOf(tmp, count);
            return Arrays.copyOf(results, count);
        } catch (NumberFormatException e) {
            Log.e(LOGTAG, "error in parsing \"" + s + "\"");
            throw e;
@@ -169,21 +191,38 @@ public class PathParser {
    }

    /**
     * Calculate the position of the next comma or space
     * Calculate the position of the next comma or space or negative sign
     * @param s the string to search
     * @param start the position to start searching
     * @return the position of the next comma or space or -1 if none found
     * @param result the result of the extraction, including the position of the
     * the starting position of next number, whether it is ending with a '-'.
     */
    private static int extract(String s, int start) {
        int space = s.indexOf(' ', start);
        int comma = s.indexOf(',', start);
        if (space == -1) {
            return comma;
    private static void extract(String s, int start, ExtractFloatResult result) {
        // Now looking for ' ', ',' or '-' from the start.
        int currentIndex = start;
        boolean foundSeparator = false;
        result.mEndWithNegSign = false;
        for (; currentIndex < s.length(); currentIndex++) {
            char currentChar = s.charAt(currentIndex);
            switch (currentChar) {
                case ' ':
                case ',':
                    foundSeparator = true;
                    break;
                case '-':
                    if (currentIndex != start) {
                        foundSeparator = true;
                        result.mEndWithNegSign = true;
                    }
                    break;
            }
            if (foundSeparator) {
                break;
            }
        if (comma == -1) {
            return space;
        }
        return (comma > space) ? space : comma;
        // When there is nothing found, then we put the end position to the end
        // of the string.
        result.mEndPosition = currentIndex;
    }

    /**
+1 −3
Original line number Diff line number Diff line
@@ -740,9 +740,7 @@ public class VectorDrawable extends Drawable {
            final float minScale = Math.min(scaleX, scaleY);

            mFinalPathMatrix.set(vGroup.mStackedMatrix);
            mFinalPathMatrix.postScale(scaleX, scaleY, mViewportWidth / 2f, mViewportHeight / 2f);
            mFinalPathMatrix.postTranslate(
                    w / 2f - mViewportWidth / 2f, h / 2f - mViewportHeight / 2f);
            mFinalPathMatrix.postScale(scaleX, scaleY);

            vPath.toPath(mPath);
            final Path path = mPath;
+1 −1
Original line number Diff line number Diff line
@@ -100,7 +100,7 @@
        </activity>
        <activity
            android:name="VectorDrawableStaticPerf"
            android:label="Performance of vector images" >
            android:label="System icons" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

+1 −1
Original line number Diff line number Diff line
@@ -26,7 +26,7 @@
    <group>
        <path
            android:name="box1"
            android:pathData="m20,200l100,90l180,-180l-35,-35l-145,145l-60,-60l-40,40z"
            android:pathData="m20,200l100,90l180-180l-35-35l-145,145l-60-60l-40,40z"
            android:fill="?android:attr/colorControlActivated"
            android:stroke="?android:attr/colorControlActivated"
            android:strokeLineCap="round"
+14 −14
Original line number Diff line number Diff line
@@ -42,9 +42,9 @@
        <path
            android:name="one"
            android:fill="#ff88ff"
            android:pathData="M 1.215625,9.5l 1.9375,0.0 0.0,-6.671875 -2.109375,0.421875 0.0,-1.078125
                l 2.09375,-0.421875 1.1874998,0.0 0.0,7.75 1.9375,0.0 0.0,1.0
                l -5.046875,0.0 0.0,-1.0Z" />
            android:pathData="M 1.215625,9.5l 1.9375,0.0 0.0-6.671875-2.109375,0.421875 0.0-1.078125
                l 2.09375-0.421875 1.1874998,0.0 0.0,7.75 1.9375,0.0 0.0,1.0
                l-5.046875,0.0 0.0-1.0Z" />
    </group>
    <group
        android:pivotX="3.65"
@@ -64,12 +64,12 @@
        <path
            android:name="two"
            android:fill="#ff88ff"
            android:pathData="M 2.534375,9.6875l 4.140625,0.0 0.0,1.0 -5.5625,0.0 0.0,-1.0q 0.671875,-0.6875 1.828125,-1.859375
                        q 1.1718752,-1.1875 1.4687502,-1.53125 0.578125,-0.625 0.796875,-1.0625
                        q 0.234375,-0.453125 0.234375,-0.875 0.0,-0.703125 -0.5,-1.140625
                        q -0.484375,-0.4375 -1.2656252,-0.4375 -0.5625,0.0 -1.1875,0.1875
                        q -0.609375,0.1875 -1.3125,0.59375l 0.0,-1.203125q 0.71875,-0.28125 1.328125,-0.421875
                        q 0.625,-0.15625 1.140625,-0.15625 1.3593752,0.0 2.1718752,0.6875
            android:pathData="M 2.534375,9.6875l 4.140625,0.0 0.0,1.0-5.5625,0.0 0.0-1.0q 0.671875-0.6875 1.828125-1.859375
                        q 1.1718752-1.1875 1.4687502-1.53125 0.578125-0.625 0.796875-1.0625
                        q 0.234375-0.453125 0.234375-0.875 0.0-0.703125-0.5-1.140625
                        q-0.484375-0.4375-1.2656252-0.4375-0.5625,0.0-1.1875,0.1875
                        q-0.609375,0.1875-1.3125,0.59375l 0.0-1.203125q 0.71875-0.28125 1.328125-0.421875
                        q 0.625-0.15625 1.140625-0.15625 1.3593752,0.0 2.1718752,0.6875
                        q 0.8125,0.671875 0.8125,1.8125 0.0,0.53125-0.203125,1.015625
                        q-0.203125,0.484375-0.734375,1.140625-0.15625,0.171875-0.9375,0.984375
                        q-0.78125024,0.8125-2.2187502,2.265625Z" />
Loading