Search in sources :

Example 1 with SVGDefsElement

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

the class SVGShape method setSVGLinearGradient.

private void setSVGLinearGradient(final SVGDocument doc, final SVGElement root) {
    final SVGDefsElement defs = doc.getFirstChild().getDefs();
    final SVGElement grad = new SVGLinearGradientElement(doc);
    final double gradMidPt = shape.getGradMidPt();
    final double gradAngle = shape.getGradAngle();
    final String id = SVGElements.SVG_LINEAR_GRADIENT + shape.hashCode();
    grad.setAttribute(SVGAttributes.SVG_ID, id);
    if (!MathUtils.INST.equalsDouble(MathUtils.INST.mod2pi(gradAngle + Math.PI / 2d), 0d)) {
        final SVGTransform rotate = new SVGTransform();
        rotate.setRotate(toDegrees(gradAngle) + 90d, 0.5, 0.5);
        grad.setAttribute(SVGAttributes.SVG_GRADIENT_TRANSFORM, rotate.toString());
    }
    final SVGStopElement stop1 = new SVGStopElement(doc);
    stop1.setAttribute(SVGAttributes.SVG_OFFSET, "0");
    stop1.setAttribute(SVGAttributes.SVG_STOP_COLOR, CSSColors.INSTANCE.getColorName(shape.getGradColStart(), true));
    if (shape.getGradColStart().getO() < 1d) {
        stop1.setAttribute(SVGAttributes.SVG_STOP_OPACITY, MathUtils.INST.format.format(shape.getGradColStart().getO()));
    }
    grad.appendChild(stop1);
    final SVGStopElement stop2 = new SVGStopElement(doc);
    stop2.setAttribute(SVGAttributes.SVG_OFFSET, MathUtils.INST.format.format(gradMidPt));
    stop2.setAttribute(SVGAttributes.SVG_STOP_COLOR, CSSColors.INSTANCE.getColorName(shape.getGradColEnd(), true));
    if (shape.getGradColEnd().getO() < 1d) {
        stop2.setAttribute(SVGAttributes.SVG_STOP_OPACITY, MathUtils.INST.format.format(shape.getGradColEnd().getO()));
    }
    grad.appendChild(stop2);
    defs.appendChild(grad);
    root.setAttribute(SVGAttributes.SVG_FILL, SVG_URL_TOKEN_BEGIN + id + ')');
}
Also used : SVGTransform(net.sf.latexdraw.parsers.svg.SVGTransform) SVGElement(net.sf.latexdraw.parsers.svg.SVGElement) SVGDefsElement(net.sf.latexdraw.parsers.svg.SVGDefsElement) SVGStopElement(net.sf.latexdraw.parsers.svg.SVGStopElement) SVGLinearGradientElement(net.sf.latexdraw.parsers.svg.SVGLinearGradientElement)

Example 2 with SVGDefsElement

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

the class SVGShape method setSVGHatchings.

private void setSVGHatchings(final SVGDocument doc, final SVGElement root, final boolean shadowFills) {
    final SVGDefsElement defs = doc.getFirstChild().getDefs();
    final String id = SVGElements.SVG_PATTERN + shape.hashCode();
    final SVGPatternElement hatch = new SVGPatternElement(doc);
    final SVGGElement gPath = new SVGGElement(doc);
    final IPoint max = shape.getFullBottomRightPoint();
    final SVGPathElement path = new SVGPathElement(doc);
    root.setAttribute(SVGAttributes.SVG_FILL, SVG_URL_TOKEN_BEGIN + id + ')');
    hatch.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_TYPE, shape.getFillingStyle().getLatexToken());
    hatch.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_ROTATION, String.valueOf(shape.getHatchingsAngle()));
    hatch.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_SIZE, String.valueOf(shape.getHatchingsSep()));
    hatch.setAttribute(SVGAttributes.SVG_PATTERN_UNITS, SVGAttributes.SVG_UNITS_VALUE_USR);
    hatch.setAttribute(SVGAttributes.SVG_ID, id);
    hatch.setAttribute(SVGAttributes.SVG_X, "0");
    hatch.setAttribute(SVGAttributes.SVG_Y, "0");
    hatch.setAttribute(SVGAttributes.SVG_WIDTH, String.valueOf((int) max.getX()));
    hatch.setAttribute(SVGAttributes.SVG_HEIGHT, String.valueOf((int) max.getY()));
    gPath.setAttribute(SVGAttributes.SVG_STROKE, CSSColors.INSTANCE.getColorName(shape.getHatchingsCol(), true));
    gPath.setAttribute(SVGAttributes.SVG_STROKE_WIDTH, String.valueOf(shape.getHatchingsWidth()));
    gPath.setAttribute(SVGAttributes.SVG_STROKE_DASHARRAY, SVGAttributes.SVG_VALUE_NONE);
    if (shape.getHatchingsCol().getO() < 1d) {
        gPath.setAttribute(SVGAttributes.SVG_STROKE_OPACITY, MathUtils.INST.format.format(shape.getHatchingsCol().getO()));
    }
    path.setAttribute(SVGAttributes.SVG_D, getSVGHatchingsPath().toString());
    gPath.appendChild(path);
    // Several shapes having hatching must have their shadow filled.
    if (shape.isFilled() || (shape.hasShadow() && shadowFills)) {
        final SVGRectElement fill = new SVGRectElement(doc);
        fill.setAttribute(SVGAttributes.SVG_FILL, CSSColors.INSTANCE.getColorName(shape.getFillingCol(), true));
        if (shape.getFillingCol().getO() < 1d) {
            fill.setAttribute(SVGAttributes.SVG_FILL_OPACITY, MathUtils.INST.format.format(shape.getFillingCol().getO()));
        }
        fill.setAttribute(SVGAttributes.SVG_STROKE, SVGAttributes.SVG_VALUE_NONE);
        fill.setAttribute(SVGAttributes.SVG_WIDTH, String.valueOf((int) max.getX()));
        fill.setAttribute(SVGAttributes.SVG_HEIGHT, String.valueOf((int) max.getY()));
        hatch.appendChild(fill);
    }
    defs.appendChild(hatch);
    hatch.appendChild(gPath);
}
Also used : SVGPatternElement(net.sf.latexdraw.parsers.svg.SVGPatternElement) SVGGElement(net.sf.latexdraw.parsers.svg.SVGGElement) SVGRectElement(net.sf.latexdraw.parsers.svg.SVGRectElement) SVGPathElement(net.sf.latexdraw.parsers.svg.SVGPathElement) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) SVGDefsElement(net.sf.latexdraw.parsers.svg.SVGDefsElement)

Example 3 with SVGDefsElement

use of net.sf.latexdraw.parsers.svg.SVGDefsElement 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 4 with SVGDefsElement

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

the class SVGBezierCurve 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 SVGElement root = new SVGGElement(doc);
    SVGElement elt;
    final String path = getPathSegList().toString();
    root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_TYPE, LNamespace.XML_TYPE_BEZIER_CURVE);
    root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
    if (shape.hasShadow()) {
        final SVGElement shad = new SVGPathElement(doc);
        shad.setAttribute(SVGAttributes.SVG_D, path);
        setSVGShadowAttributes(shad, false);
        root.appendChild(shad);
        if (shape.isOpened()) {
            setSVGArrow(shape, shad, 0, true, doc, defs);
            setSVGArrow(shape, shad, 1, true, doc, defs);
        }
    }
    if (shape.hasShadow() && !shape.getLineStyle().getLatexToken().equals(PSTricksConstants.LINE_NONE_STYLE) && shape.isFilled()) {
        // The background of the borders must be filled is there is a shadow.
        elt = new SVGPathElement(doc);
        elt.setAttribute(SVGAttributes.SVG_D, path);
        setSVGBorderBackground(elt, root);
    }
    elt = new SVGPathElement(doc);
    elt.setAttribute(SVGAttributes.SVG_D, path);
    root.appendChild(elt);
    if (shape.hasDbleBord()) {
        final SVGElement dblBord = new SVGPathElement(doc);
        dblBord.setAttribute(SVGAttributes.SVG_D, path);
        setSVGDoubleBordersAttributes(dblBord);
        root.appendChild(dblBord);
    }
    setSVGAttributes(doc, elt, false);
    elt.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_ROTATION, String.valueOf(shape.getRotationAngle()));
    if (shape.isOpened()) {
        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) SVGPathElement(net.sf.latexdraw.parsers.svg.SVGPathElement) SVGDefsElement(net.sf.latexdraw.parsers.svg.SVGDefsElement)

Example 5 with SVGDefsElement

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

the class SVGPolylines method toSVG.

@Override
public SVGElement toSVG(final SVGDocument doc) {
    if (doc == null) {
        return null;
    }
    final SVGElement root = new SVGGElement(doc);
    final SVGDefsElement defs = doc.getFirstChild().getDefs();
    final StringBuilder points = new StringBuilder();
    final List<IPoint> pts = shape.getPoints();
    SVGPolyLineElement elt;
    root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_TYPE, LNamespace.XML_TYPE_JOINED_LINES);
    root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
    for (final IPoint pt : pts) {
        points.append(pt.getX()).append(',').append(pt.getY()).append(' ');
    }
    final String pointsStr = points.toString();
    if (shape.hasShadow()) {
        final SVGPolyLineElement shad = new SVGPolyLineElement(doc);
        try {
            shad.setPoints(pointsStr);
        } catch (final ParseException ex) {
            BadaboomCollector.INSTANCE.add(ex);
        }
        setSVGShadowAttributes(shad, false);
        root.appendChild(shad);
        setSVGArrow(shape, shad, 0, true, doc, defs);
        setSVGArrow(shape, shad, 1, true, doc, defs);
    }
    if (shape.hasShadow() && !shape.getLineStyle().getLatexToken().equals(PSTricksConstants.LINE_NONE_STYLE) && shape.isFilled()) {
        // The background of the borders must be filled is there is a shadow.
        elt = new SVGPolyLineElement(doc);
        try {
            elt.setPoints(pointsStr);
        } catch (final ParseException ex) {
            BadaboomCollector.INSTANCE.add(ex);
        }
        setSVGBorderBackground(elt, root);
    }
    elt = new SVGPolyLineElement(doc);
    try {
        elt.setPoints(pointsStr);
    } catch (final ParseException ex) {
        BadaboomCollector.INSTANCE.add(ex);
    }
    root.appendChild(elt);
    if (shape.hasDbleBord()) {
        final SVGPolyLineElement dblBord = new SVGPolyLineElement(doc);
        try {
            dblBord.setPoints(pointsStr);
        } catch (final ParseException ex) {
            BadaboomCollector.INSTANCE.add(ex);
        }
        setSVGDoubleBordersAttributes(dblBord);
        root.appendChild(dblBord);
    }
    setSVGAttributes(doc, elt, false);
    elt.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_ROTATION, String.valueOf(shape.getRotationAngle()));
    setSVGArrow(shape, elt, 0, false, doc, defs);
    setSVGArrow(shape, elt, shape.getNbArrows() - 1, false, doc, defs);
    return root;
}
Also used : SVGPolyLineElement(net.sf.latexdraw.parsers.svg.SVGPolyLineElement) SVGGElement(net.sf.latexdraw.parsers.svg.SVGGElement) SVGElement(net.sf.latexdraw.parsers.svg.SVGElement) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint) SVGDefsElement(net.sf.latexdraw.parsers.svg.SVGDefsElement) ParseException(java.text.ParseException)

Aggregations

SVGDefsElement (net.sf.latexdraw.parsers.svg.SVGDefsElement)6 SVGElement (net.sf.latexdraw.parsers.svg.SVGElement)4 SVGGElement (net.sf.latexdraw.parsers.svg.SVGGElement)4 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)3 SVGPathElement (net.sf.latexdraw.parsers.svg.SVGPathElement)3 ParseException (java.text.ParseException)1 ArcStyle (net.sf.latexdraw.models.interfaces.shape.ArcStyle)1 SVGDocument (net.sf.latexdraw.parsers.svg.SVGDocument)1 SVGLinearGradientElement (net.sf.latexdraw.parsers.svg.SVGLinearGradientElement)1 SVGPatternElement (net.sf.latexdraw.parsers.svg.SVGPatternElement)1 SVGPolyLineElement (net.sf.latexdraw.parsers.svg.SVGPolyLineElement)1 SVGRectElement (net.sf.latexdraw.parsers.svg.SVGRectElement)1 SVGSVGElement (net.sf.latexdraw.parsers.svg.SVGSVGElement)1 SVGStopElement (net.sf.latexdraw.parsers.svg.SVGStopElement)1 SVGTransform (net.sf.latexdraw.parsers.svg.SVGTransform)1 SVGPathSegArc (net.sf.latexdraw.parsers.svg.path.SVGPathSegArc)1 SVGPathSegClosePath (net.sf.latexdraw.parsers.svg.path.SVGPathSegClosePath)1 SVGPathSegLineto (net.sf.latexdraw.parsers.svg.path.SVGPathSegLineto)1 SVGPathSegList (net.sf.latexdraw.parsers.svg.path.SVGPathSegList)1 SVGPathSegMoveto (net.sf.latexdraw.parsers.svg.path.SVGPathSegMoveto)1