Search in sources :

Example 6 with SVGPathSegClosePath

use of net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath in project latexdraw by arnobl.

the class SVGBezierCurve method pathToBezierCurve.

/**
 * Creates a bezier curve and initialises its path from an SVG element.
 */
private static IBezierCurve pathToBezierCurve(final SVGElement elt) {
    if (!(elt instanceof SVGPathElement)) {
        return null;
    }
    final SVGPathSegList list = ((SVGPathElement) elt).getSegList();
    if (list == null || list.size() < 2 || !(list.get(0) instanceof SVGPathSegMoveto)) {
        return null;
    }
    final SVGPathSegMoveto m = (SVGPathSegMoveto) list.get(0);
    CtrlPointsSeg c;
    int i = 1;
    final int size = list.size();
    // Creating a point to support when the first path element is relative.
    Point2D pt = new Point2D.Double();
    final List<IPoint> pts = new ArrayList<>();
    final List<IPoint> ctrlpts = new ArrayList<>();
    final boolean closed;
    pt = m.getPoint(pt);
    pts.add(ShapeFactory.INST.createPoint(pt));
    if (list.get(1) instanceof CtrlPointsSeg) {
        // We set the control point of the first point.
        c = (CtrlPointsSeg) list.get(1);
        ctrlpts.add(ShapeFactory.INST.createPoint(c.getCtrl1(pt)));
    }
    while (i < size && list.get(i) instanceof CtrlPointsSeg) {
        c = (CtrlPointsSeg) list.get(i);
        final Point2D currPt = c.getPoint(pt);
        pts.add(ShapeFactory.INST.createPoint(currPt));
        ctrlpts.add(ShapeFactory.INST.createPoint(c.getCtrl2(pt)));
        pt = currPt;
        i++;
    }
    if (pts.size() > 2 && pts.get(pts.size() - 1).equals(pts.get(0), 0.00001)) {
        // We set the shape as closed
        pts.remove(pts.size() - 1);
        ctrlpts.remove(ctrlpts.size() - 1);
        closed = true;
    } else {
        // There is something else at the end of the path.
        closed = i < size && list.get(i) instanceof SVGPathSegClosePath;
    }
    final IBezierCurve bc = ShapeFactory.INST.createBezierCurve(pts, ctrlpts);
    bc.setOpened(!closed);
    return bc;
}
Also used : SVGPathSegMoveto(net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto) SVGPathElement(net.sf.latexdraw.parsers.svg.SVGPathElement) ArrayList(java.util.ArrayList) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) SVGPathSegList(net.sf.latexdraw.parsers.svg.path.SVGPathSegList) SVGPathSegClosePath(net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) IBezierCurve(net.sf.latexdraw.models.interfaces.shape.IBezierCurve) Point2D(java.awt.geom.Point2D) CtrlPointsSeg(net.sf.latexdraw.parsers.svg.path.CtrlPointsSeg)

Example 7 with SVGPathSegClosePath

use of net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath in project latexdraw by arnobl.

the class SVGPathElement method isPolygon.

/**
 * @return True if the path is composed of lines and has a 'close path' segment at the end.
 * @since 0.1
 */
public boolean isPolygon() {
    final SVGPathSegList segList = getSegList();
    if (segList.isEmpty() || !(segList.get(0) instanceof SVGPathSegMoveto))
        return false;
    boolean ok = true;
    int i;
    final int size;
    for (i = 1, size = segList.size() - 1; i < size && ok; i++) if (!(segList.get(i) instanceof SVGPathSegLineto))
        ok = false;
    if (!(segList.get(segList.size() - 1) instanceof SVGPathSegClosePath))
        ok = false;
    return ok;
}
Also used : SVGPathSegMoveto(net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto) SVGPathSegLineto(net.sf.latexdraw.parsers.svg.path.SVGPathSegLineto) SVGPathSegList(net.sf.latexdraw.parsers.svg.path.SVGPathSegList) SVGPathSegClosePath(net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath)

Example 8 with SVGPathSegClosePath

use of net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath in project latexdraw by arnobl.

the class SVGPathElement method isBezierCurve.

public boolean isBezierCurve() {
    final SVGPathSegList segList = getSegList();
    if (segList.isEmpty() || !(segList.get(0) instanceof SVGPathSegMoveto))
        return false;
    final int size = segList.size() - 1;
    boolean ok = true;
    int i;
    for (i = 1; i < size && ok; i++) if (!(segList.get(i) instanceof SVGPathSegCurvetoCubic) && !(segList.get(i) instanceof SVGPathSegCurvetoCubicSmooth))
        ok = false;
    return ok && (segList.get(size) instanceof SVGPathSegClosePath || segList.get(size) instanceof SVGPathSegCurvetoCubic || segList.get(size) instanceof SVGPathSegCurvetoCubicSmooth);
}
Also used : SVGPathSegCurvetoCubicSmooth(net.sf.latexdraw.parsers.svg.path.SVGPathSegCurvetoCubicSmooth) SVGPathSegMoveto(net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto) SVGPathSegCurvetoCubic(net.sf.latexdraw.parsers.svg.path.SVGPathSegCurvetoCubic) SVGPathSegList(net.sf.latexdraw.parsers.svg.path.SVGPathSegList) SVGPathSegClosePath(net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath)

Aggregations

SVGPathSegClosePath (net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath)8 SVGPathSegList (net.sf.latexdraw.parsers.svg.path.SVGPathSegList)6 SVGPathSegMoveto (net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto)5 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)4 SVGPathSegLineto (net.sf.latexdraw.parsers.svg.path.SVGPathSegLineto)3 Point2D (java.awt.geom.Point2D)2 ArrayList (java.util.ArrayList)2 SVGPathElement (net.sf.latexdraw.parsers.svg.SVGPathElement)2 SVGPathSegCurvetoCubic (net.sf.latexdraw.parsers.svg.path.SVGPathSegCurvetoCubic)2 ArcStyle (net.sf.latexdraw.models.interfaces.shape.ArcStyle)1 IBezierCurve (net.sf.latexdraw.models.interfaces.shape.IBezierCurve)1 SVGDefsElement (net.sf.latexdraw.parsers.svg.SVGDefsElement)1 SVGElement (net.sf.latexdraw.parsers.svg.SVGElement)1 SVGGElement (net.sf.latexdraw.parsers.svg.SVGGElement)1 CtrlPointsSeg (net.sf.latexdraw.parsers.svg.path.CtrlPointsSeg)1 SVGPathSeg (net.sf.latexdraw.parsers.svg.path.SVGPathSeg)1 SVGPathSegArc (net.sf.latexdraw.parsers.svg.path.SVGPathSegArc)1 SVGPathSegCurvetoCubicSmooth (net.sf.latexdraw.parsers.svg.path.SVGPathSegCurvetoCubicSmooth)1