Search in sources :

Example 6 with Path

use of com.xenoage.zong.symbols.path.Path in project Zong by Xenoage.

the class SvgPathReaderTest method testImplicitLineToRel.

@Test
public void testImplicitLineToRel() {
    String validPath = "m1200,1300 100,100 z";
    Path p = new SvgPathReader(validPath).read();
    assertEquals(alist(new MoveTo(p(2, 3)), new LineTo(p(3, 4f)), new ClosePath()), p.getElements());
}
Also used : ClosePath(com.xenoage.zong.symbols.path.ClosePath) Path(com.xenoage.zong.symbols.path.Path) ClosePath(com.xenoage.zong.symbols.path.ClosePath) MoveTo(com.xenoage.zong.symbols.path.MoveTo) LineTo(com.xenoage.zong.symbols.path.LineTo) Test(org.junit.Test)

Example 7 with Path

use of com.xenoage.zong.symbols.path.Path in project Zong by Xenoage.

the class SvgPathReader method read.

/**
 * Creates a path from the given d attribute value of a SVG path element.
 * The type of the path is implementation dependent.
 * The path and its bounding rect is returned.
 */
public Path read() {
    // parse commands
    char tokenChar = '?';
    String token = getNextToken();
    Point2f p, cp1, cp2;
    float x, y;
    while (token != null) {
        char nextTokenChar = token.charAt(0);
        if (Character.isDigit(nextTokenChar) || nextTokenChar == '-' || nextTokenChar == '+') {
            // number. reuse last command (if not 'M' or 'm' - then it is 'L' or 'l'. see SVG spec)
            pos -= token.length();
            if (tokenChar == 'M')
                tokenChar = 'L';
            else if (tokenChar == 'm')
                tokenChar = 'l';
        } else {
            // next command
            tokenChar = nextTokenChar;
        }
        switch(tokenChar) {
            // MoveTo (absolute)
            case 'M':
                p = readPointAbs();
                moveTo(p);
                break;
            // MoveTo (relative)
            case 'm':
                p = readPointRel();
                moveTo(pCurrent.add(p));
                break;
            // ClosePath
            case 'Z':
            case 'z':
                closePath();
                break;
            // LineTo (absolute)
            case 'L':
                p = readPointAbs();
                lineTo(p);
                break;
            // LineTo (relative)
            case 'l':
                p = readPointRel();
                lineTo(pCurrent.add(p));
                break;
            // Horizontal LineTo (absolute)
            case 'H':
                x = readCoordAbs();
                lineTo(p(x, pCurrent.y));
                break;
            // Horizontal LineTo (relative)
            case 'h':
                x = readCoordRel();
                lineTo(p(pCurrent.x + x, pCurrent.y));
                break;
            // Vertical LineTo (absolute)
            case 'V':
                y = readCoordAbs();
                lineTo(p(pCurrent.x, y));
                break;
            // Vertical LineTo (relative)
            case 'v':
                y = readCoordRel();
                lineTo(p(pCurrent.x, pCurrent.y + y));
                break;
            // Cubic CurveTo (absolute)
            case 'C':
                cp1 = readPointAbs();
                cp2 = readPointAbs();
                p = readPointAbs();
                cubicCurveTo(cp1, cp2, p);
                break;
            // Cubic CurveTo (relative)
            case 'c':
                cp1 = readPointRel();
                cp2 = readPointRel();
                p = readPointRel();
                cubicCurveTo(pCurrent.add(cp1), pCurrent.add(cp2), pCurrent.add(p));
                break;
            // Quadratic CurveTo (absolute)
            case 'Q':
                cp1 = readPointAbs();
                p = readPointAbs();
                quadraticCurveTo(cp1, p);
                break;
            // Quadratic CurveTo (relative)
            case 'q':
                cp1 = readPointRel();
                p = readPointRel();
                quadraticCurveTo(pCurrent.add(cp1), pCurrent.add(p));
                break;
            // not implemented commands
            case 'T':
            case 't':
            case 'S':
            case 's':
            case 'A':
            case 'a':
                throw new IllegalStateException("SVG command \"" + token + "\" not implemented yet.");
            // unknown command
            default:
                throw new IllegalStateException("Unknown SVG command: \"" + token + "\"");
        }
        token = getNextToken();
    }
    return new Path(elements);
}
Also used : ClosePath(com.xenoage.zong.symbols.path.ClosePath) Path(com.xenoage.zong.symbols.path.Path) Point2f(com.xenoage.utils.math.geom.Point2f)

Aggregations

Path (com.xenoage.zong.symbols.path.Path)7 ClosePath (com.xenoage.zong.symbols.path.ClosePath)5 MoveTo (com.xenoage.zong.symbols.path.MoveTo)5 LineTo (com.xenoage.zong.symbols.path.LineTo)4 Test (org.junit.Test)4 Point2f (com.xenoage.utils.math.geom.Point2f)2 PathSymbol (com.xenoage.zong.symbols.PathSymbol)1 CubicCurveTo (com.xenoage.zong.symbols.path.CubicCurveTo)1 PathElement (com.xenoage.zong.symbols.path.PathElement)1