Search in sources :

Example 11 with SVGPathSegMoveto

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

the class SVGCircleArc method toSVG.

@Override
public SVGElement toSVG(final SVGDocument doc) {
    if (doc == null || doc.getFirstChild().getDefs() == null) {
        return null;
    }
    final SVGDefsElement defs = doc.getFirstChild().getDefs();
    final double rotationAngle = shape.getRotationAngle();
    final double startAngle = shape.getAngleStart() % (2. * Math.PI);
    final double endAngle = shape.getAngleEnd() % (2. * Math.PI);
    final ArcStyle type = shape.getArcStyle();
    final SVGElement root = new SVGGElement(doc);
    final IPoint start = shape.getStartPoint();
    final IPoint end = shape.getEndPoint();
    final double radius = shape.getWidth() / 2.0;
    final boolean largeArcFlag = Math.abs(endAngle - startAngle) >= Math.PI;
    final boolean sweepFlag = startAngle >= endAngle;
    final SVGPathSegList path = new SVGPathSegList();
    SVGElement elt;
    root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_TYPE, LNamespace.XML_TYPE_ARC);
    root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
    path.add(new SVGPathSegMoveto(start.getX(), start.getY(), false));
    path.add(new SVGPathSegArc(end.getX(), end.getY(), radius, radius, 0, largeArcFlag, sweepFlag, false));
    if (type == ArcStyle.CHORD) {
        path.add(new SVGPathSegClosePath());
    } else {
        if (type == ArcStyle.WEDGE) {
            final IPoint gravityCenter = shape.getGravityCentre();
            path.add(new SVGPathSegLineto(gravityCenter.getX(), gravityCenter.getY(), false));
            path.add(new SVGPathSegClosePath());
        }
    }
    if (shape.hasShadow()) {
        final SVGElement shad = new SVGPathElement(doc);
        shad.setAttribute(SVGAttributes.SVG_D, path.toString());
        setSVGShadowAttributes(shad, true);
        root.appendChild(shad);
        setSVGArrow(shape, shad, 0, true, doc, defs);
        setSVGArrow(shape, shad, 1, true, doc, defs);
    }
    // The background of the borders must be filled is there is a shadow.
    if (shape.hasShadow()) {
        elt = new SVGPathElement(doc);
        elt.setAttribute(SVGAttributes.SVG_D, path.toString());
        setSVGBorderBackground(elt, root);
    }
    elt = new SVGPathElement(doc);
    elt.setAttribute(SVGAttributes.SVG_D, path.toString());
    root.appendChild(elt);
    if (shape.hasDbleBord()) {
        final SVGElement dble = new SVGPathElement(doc);
        dble.setAttribute(SVGAttributes.SVG_D, path.toString());
        setSVGDoubleBordersAttributes(dble);
        root.appendChild(dble);
    }
    setSVGRotationAttribute(root);
    setSVGAttributes(doc, elt, true);
    elt.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_ROTATION, String.valueOf(rotationAngle));
    setSVGArrow(shape, elt, 0, false, doc, defs);
    setSVGArrow(shape, elt, 1, false, doc, defs);
    if (shape.isShowPts()) {
        root.appendChild(getShowPointsElement(doc));
    }
    return root;
}
Also used : SVGGElement(net.sf.latexdraw.parsers.svg.SVGGElement) SVGElement(net.sf.latexdraw.parsers.svg.SVGElement) SVGPathSegMoveto(net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto) SVGPathElement(net.sf.latexdraw.parsers.svg.SVGPathElement) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) SVGPathSegLineto(net.sf.latexdraw.parsers.svg.path.SVGPathSegLineto) SVGPathSegList(net.sf.latexdraw.parsers.svg.path.SVGPathSegList) SVGPathSegClosePath(net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath) SVGPathSegArc(net.sf.latexdraw.parsers.svg.path.SVGPathSegArc) ArcStyle(net.sf.latexdraw.models.interfaces.shape.ArcStyle) SVGDefsElement(net.sf.latexdraw.parsers.svg.SVGDefsElement)

Example 12 with SVGPathSegMoveto

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

the class SVGArrow method setArrow.

/**
 * Initialises the arrowhead using a path arrow.
 * @param path The path element.
 * @param owner The shape that has the arrow.
 */
void setArrow(final SVGPathElement path, final IShape owner, final String svgMarker) {
    final SVGPathSegList list = path.getSegList();
    final SVGPathSegMoveto m = (SVGPathSegMoveto) list.get(0);
    final double lineWidth = owner.hasDbleBord() ? owner.getDbleBordSep() + 2d * owner.getThickness() : owner.getThickness();
    // == 4 is legacy code for 3.x
    if (list.size() == 2 || list.size() == 4 || list.size() == 6) {
        setArrowBarBracket(path, m, lineWidth, list.get(1), list, svgMarker);
    } else {
        // == 10 is legacy code for the 3.x branch. Now the double arrow has 12 elements.
        if (list.size() == 5 || list.size() == 10 || list.size() == 12) {
            setArrowArrow(path, m, lineWidth, list.get(1), list, svgMarker);
        }
    }
}
Also used : SVGPathSegMoveto(net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto) SVGPathSegList(net.sf.latexdraw.parsers.svg.path.SVGPathSegList)

Example 13 with SVGPathSegMoveto

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

the class SVGArrow method createArc.

@Override
public void createArc(final double cx, final double cy, final double rx, final double ry, final double angle, final double length, final ObservableValue<Color> strokeProp, final ObservableDoubleValue strokeWidthProp) {
    final boolean sweepFlag = angle < 0d ^ arrow.isInverted() ^ !arrow.isLeftArrow();
    createPath();
    currentPath.add(new SVGPathSegMoveto(cx + rx * Math.cos(Math.toRadians(angle)), cy - ry * Math.sin(Math.toRadians(angle)), false));
    currentPath.add(new SVGPathSegArc(cx + rx * Math.cos(Math.toRadians(angle + length)), cy - ry * Math.sin(Math.toRadians(angle + length)), rx, ry, 0, false, sweepFlag, false));
    setPathStroke(strokeProp);
    setPathStrokeWidth(strokeWidthProp);
    setPathFill(null);
}
Also used : SVGPathSegMoveto(net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto) SVGPathSegArc(net.sf.latexdraw.parsers.svg.path.SVGPathSegArc)

Example 14 with SVGPathSegMoveto

use of net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto 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 15 with SVGPathSegMoveto

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

the class SVGShape method getSVGHatchingsPath2.

private void getSVGHatchingsPath2(final SVGPathSegList path, final double hAngle, final IRectangle bound) {
    if (path == null || bound == null)
        return;
    double angle2 = hAngle % (Math.PI * 2.);
    final double halphPI = Math.PI / 2.;
    final double hatchingWidth = shape.getHatchingsWidth();
    final double hatchingSep = shape.getHatchingsSep();
    final IPoint nw = bound.getTopLeftPoint();
    final IPoint se = bound.getBottomRightPoint();
    final double nwx = nw.getX();
    final double nwy = nw.getY();
    final double sex = se.getX();
    final double sey = se.getY();
    // Computing the angle.
    if (angle2 > 0.) {
        if (angle2 > 3. * halphPI) {
            angle2 -= Math.PI * 2.;
        } else {
            if (angle2 > halphPI) {
                angle2 -= Math.PI;
            }
        }
    } else {
        if (angle2 < -3. * halphPI) {
            angle2 += Math.PI * 2.;
        } else {
            if (angle2 < -halphPI) {
                angle2 += Math.PI;
            }
        }
    }
    final double val = hatchingWidth + hatchingSep;
    if (MathUtils.INST.equalsDouble(angle2, 0.))
        // Drawing the hatchings vertically.
        for (double x = nwx; x < sex; x += val) {
            path.add(new SVGPathSegMoveto(x, nwy, false));
            path.add(new SVGPathSegLineto(x, sey, false));
        }
    else // Drawing the hatchings horizontally.
    if (MathUtils.INST.equalsDouble(angle2, halphPI) || MathUtils.INST.equalsDouble(angle2, -halphPI))
        for (double y = nwy; y < sey; y += val) {
            path.add(new SVGPathSegMoveto(nwx, y, false));
            path.add(new SVGPathSegLineto(sex, y, false));
        }
    else {
        // Drawing the hatchings by rotation.
        final double incX = val / Math.cos(angle2);
        final double incY = val / Math.sin(angle2);
        final double maxX;
        double y1;
        double x2;
        final double y2;
        final double x1;
        if (angle2 > 0.) {
            y1 = nwy;
            maxX = sex + (sey - (nwy < 0 ? nwy : 0)) * Math.tan(angle2);
        } else {
            y1 = sey;
            maxX = sex - sey * Math.tan(angle2);
        }
        x1 = nwx;
        x2 = x1;
        y2 = y1;
        if (incX < 0. || MathUtils.INST.equalsDouble(incX, 0.)) {
            return;
        }
        while (x2 < maxX) {
            x2 += incX;
            y1 += incY;
            path.add(new SVGPathSegMoveto(x1, y1, false));
            path.add(new SVGPathSegLineto(x2, y2, false));
        }
    }
}
Also used : SVGPathSegMoveto(net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) SVGPathSegLineto(net.sf.latexdraw.parsers.svg.path.SVGPathSegLineto)

Aggregations

SVGPathSegMoveto (net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto)22 SVGPathParser (net.sf.latexdraw.parsers.svg.parsers.SVGPathParser)9 Test (org.junit.Test)9 SVGPathSegLineto (net.sf.latexdraw.parsers.svg.path.SVGPathSegLineto)7 SVGPathSegList (net.sf.latexdraw.parsers.svg.path.SVGPathSegList)7 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)6 SVGPathSegClosePath (net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath)5 SVGPathSegCurvetoCubic (net.sf.latexdraw.parsers.svg.path.SVGPathSegCurvetoCubic)4 SVGPathSegArc (net.sf.latexdraw.parsers.svg.path.SVGPathSegArc)3 SVGPathElement (net.sf.latexdraw.parsers.svg.SVGPathElement)2 SVGPathSegCurvetoCubicSmooth (net.sf.latexdraw.parsers.svg.path.SVGPathSegCurvetoCubicSmooth)2 Point2D (java.awt.geom.Point2D)1 ArrayList (java.util.ArrayList)1 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 SVGPathSegCurvetoQuadratic (net.sf.latexdraw.parsers.svg.path.SVGPathSegCurvetoQuadratic)1