Search in sources :

Example 1 with Path2D

use of com.ait.lienzo.client.core.Path2D in project lienzo-core by ahome-it.

the class SVGPath method parse.

public static final void parse(final PathPartList partlist, String path) {
    partlist.clear();
    path = path.replaceAll("\\s+", " ").trim();
    if (Path2D.isSupported()) {
        partlist.setPath2D(new Path2D(path));
    }
    for (final String element : COMMANDS) {
        path = path.replaceAll(element + " ", element);
    }
    path = path.replaceAll(" ", ",");
    for (final String element : COMMANDS) {
        path = path.replaceAll(element, "#" + element);
    }
    final String[] list = path.split("#");
    double cpx = 0;
    double cpy = 0;
    for (int n = 1, l = list.length; n < l; n++) {
        String str = list[n];
        char chr = str.charAt(0);
        str = str.substring(1).replaceAll(",-", "-").replaceAll("-", ",-").replaceAll("e,-", "e-");
        final String[] pts = str.split(",");
        int beg = 0;
        if ((pts.length > 0) && (pts[0].isEmpty())) {
            beg = 1;
        }
        final NFastDoubleArrayJSO source = NFastDoubleArrayJSO.make();
        for (int i = beg, z = pts.length; i < z; i++) {
            source.push(Double.valueOf(pts[i]).doubleValue());
        }
        PathPartEntryJSO prev;
        double ctx, cty;
        while (source.size() > 0) {
            int cmd = PathPartEntryJSO.UNDEFINED_PATH_PART;
            final NFastDoubleArrayJSO points = NFastDoubleArrayJSO.make();
            switch(chr) {
                case 'l':
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'L':
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'm':
                    final double dx = source.shift();
                    final double dy = source.shift();
                    cpx += dx;
                    cpy += dy;
                    final int size = partlist.size();
                    if ((size > 2) && (partlist.get(size - 1).getCommand() == PathPartEntryJSO.CLOSE_PATH_PART)) {
                        for (int idx = size - 2; idx >= 0; idx--) {
                            prev = partlist.get(idx);
                            if (prev.getCommand() == PathPartEntryJSO.MOVETO_ABSOLUTE) {
                                cpx = prev.getPoints().get(0) + dx;
                                cpy = prev.getPoints().get(1) + dy;
                                break;
                            }
                        }
                    }
                    points.push(cpx);
                    points.push(cpy);
                    chr = 'l';
                    cmd = PathPartEntryJSO.MOVETO_ABSOLUTE;
                    break;
                case 'M':
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    chr = 'L';
                    cmd = PathPartEntryJSO.MOVETO_ABSOLUTE;
                    break;
                case 'h':
                    cpx += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'H':
                    cpx = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'v':
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'V':
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.LINETO_ABSOLUTE;
                    break;
                case 'C':
                    points.push(source.shift());
                    points.push(source.shift());
                    points.push(source.shift());
                    points.push(source.shift());
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 'c':
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 'S':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(2));
                        cty = cpy + (cpy - prev.getPoints().get(3));
                    }
                    points.push(ctx);
                    points.push(cty);
                    points.push(source.shift());
                    points.push(source.shift());
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 's':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(2));
                        cty = cpy + (cpy - prev.getPoints().get(3));
                    }
                    points.push(ctx);
                    points.push(cty);
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.BEZIER_CURVETO_ABSOLUTE;
                    break;
                case 'Q':
                    points.push(source.shift());
                    points.push(source.shift());
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 'q':
                    points.push(cpx + source.shift());
                    points.push(cpy + source.shift());
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 'T':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(0));
                        cty = cpy + (cpy - prev.getPoints().get(1));
                    }
                    cpx = source.shift();
                    cpy = source.shift();
                    points.push(ctx);
                    points.push(cty);
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 't':
                    ctx = cpx;
                    cty = cpy;
                    prev = partlist.get(partlist.size() - 1);
                    if (prev.getCommand() == PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE) {
                        ctx = cpx + (cpx - prev.getPoints().get(0));
                        cty = cpy + (cpy - prev.getPoints().get(1));
                    }
                    cpx += source.shift();
                    cpy += source.shift();
                    points.push(ctx);
                    points.push(cty);
                    points.push(cpx);
                    points.push(cpy);
                    cmd = PathPartEntryJSO.QUADRATIC_CURVETO_ABSOLUTE;
                    break;
                case 'A':
                    {
                        final double rx = source.shift();
                        final double ry = source.shift();
                        final double ps = source.shift();
                        final double fa = source.shift();
                        final double fs = source.shift();
                        final double x1 = cpx;
                        final double y1 = cpy;
                        cpx = source.shift();
                        cpy = source.shift();
                        PathPartList.convertEndpointToCenterParameterization(points, x1, y1, cpx, cpy, fa, fs, rx, ry, ps);
                        points.push(cpx);
                        points.push(cpy);
                        cmd = PathPartEntryJSO.ARCTO_ABSOLUTE;
                        break;
                    }
                case 'a':
                    {
                        final double rx = source.shift();
                        final double ry = source.shift();
                        final double ps = source.shift();
                        final double fa = source.shift();
                        final double fs = source.shift();
                        final double x1 = cpx;
                        final double y1 = cpy;
                        cpx += source.shift();
                        cpy += source.shift();
                        PathPartList.convertEndpointToCenterParameterization(points, x1, y1, cpx, cpy, fa, fs, rx, ry, ps);
                        points.push(cpx);
                        points.push(cpy);
                        cmd = PathPartEntryJSO.ARCTO_ABSOLUTE;
                        break;
                    }
            }
            if (cmd != PathPartEntryJSO.UNDEFINED_PATH_PART) {
                partlist.push(PathPartEntryJSO.make(cmd, points));
            }
        }
        if ((chr == 'z') || (chr == 'Z')) {
            partlist.Z();
        }
    }
}
Also used : NFastDoubleArrayJSO(com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO) Path2D(com.ait.lienzo.client.core.Path2D) PathPartEntryJSO(com.ait.lienzo.client.core.types.PathPartEntryJSO)

Example 2 with Path2D

use of com.ait.lienzo.client.core.Path2D in project lienzo-core by ahome-it.

the class SVGPath method drawWithoutTransforms.

@Override
protected void drawWithoutTransforms(final Context2D context, double alpha, final BoundingBox bounds) {
    if (m_list.size() < 1) {
        return;
    }
    final Attributes attr = getAttributes();
    alpha = alpha * attr.getAlpha();
    if (alpha <= 0) {
        return;
    }
    if (context.isSelection()) {
        if (dofillBoundsForSelection(context, attr, alpha)) {
            return;
        }
    } else {
        setAppliedShadow(false);
    }
    final Path2D path = m_list.getPath2D();
    boolean fill = false;
    if (null != path) {
        if (path.isClosed()) {
            fill = fill(context, attr, alpha, path);
        }
        stroke(context, attr, alpha, path, fill);
    } else {
        if (context.path(m_list)) {
            fill = fill(context, attr, alpha);
        }
        stroke(context, attr, alpha, fill);
    }
}
Also used : Path2D(com.ait.lienzo.client.core.Path2D)

Aggregations

Path2D (com.ait.lienzo.client.core.Path2D)2 PathPartEntryJSO (com.ait.lienzo.client.core.types.PathPartEntryJSO)1 NFastDoubleArrayJSO (com.ait.tooling.nativetools.client.collection.NFastDoubleArrayJSO)1