Search in sources :

Example 11 with SVGElement

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

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

the class SVGShape method getLaTeXDrawElement.

/**
 * @param elt The source <code>g</code> element.
 * @param type The type of the latexdraw element (double borders, shadow, main), if null, the main element is returned.
 * @return The Researched element.
 */
protected static SVGElement getLaTeXDrawElement(final SVGGElement elt, final String type) {
    if (elt == null)
        return null;
    final NodeList nl = elt.getChildNodes();
    int i = 0;
    final int size = nl.getLength();
    Node ltdElt = null;
    Node n;
    final String bis = elt.lookupPrefixUsable(LNamespace.LATEXDRAW_NAMESPACE_URI);
    if (type == null) {
        while (i < size && ltdElt == null) {
            n = nl.item(i);
            if (((SVGElement) n).getAttribute(bis + LNamespace.XML_TYPE) == null) {
                ltdElt = n;
            } else {
                i++;
            }
        }
    } else {
        while (i < size && ltdElt == null) {
            n = nl.item(i);
            if (type.equals(((SVGElement) n).getAttribute(bis + LNamespace.XML_TYPE))) {
                ltdElt = n;
            } else {
                i++;
            }
        }
    }
    return (SVGElement) ltdElt;
}
Also used : SVGElement(net.sf.latexdraw.parsers.svg.SVGElement) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint)

Example 13 with SVGElement

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

the class SVGShape method setFillFromSVG.

/**
 * Sets the fill properties to the given figure.
 * @param shape The figure to set.
 * @param fill The fill properties
 * @param defs The definition that may be useful to the the fill properties (url), may be null.
 * @param opacity The possible fill-opacity of the colour. May be null.
 */
public static void setFillFromSVG(final IShape shape, final String fill, final String opacity, final SVGDefsElement defs) {
    if (fill == null || shape == null || fill.equals(SVGAttributes.SVG_VALUE_NONE)) {
        return;
    }
    // Getting the url to the SVG symbol of the filling.
    if (fill.startsWith(SVG_URL_TOKEN_BEGIN) && fill.endsWith(")") && defs != null) {
        final String uri = fill.substring(5, fill.length() - 1);
        final SVGElement def = defs.getDef(uri);
        // A pattern means hatchings.
        if (def instanceof SVGPatternElement) {
            setHatchingsFromSVG(shape, (SVGPatternElement) def);
        } else {
            // A linear gradient means a gradient.
            if (def instanceof SVGLinearGradientElement) {
                setGradientFromSVG(shape, (SVGLinearGradientElement) def);
            }
        }
    } else {
        setPlainFillFromSVG(shape, fill, opacity);
    }
}
Also used : SVGPatternElement(net.sf.latexdraw.parsers.svg.SVGPatternElement) SVGElement(net.sf.latexdraw.parsers.svg.SVGElement) SVGLinearGradientElement(net.sf.latexdraw.parsers.svg.SVGLinearGradientElement)

Example 14 with SVGElement

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

the class SVGCircle method toSVG.

@Override
public SVGElement toSVG(final SVGDocument doc) {
    if (doc == null || doc.getFirstChild().getDefs() == null) {
        return null;
    }
    final IPoint tl = shape.getTopLeftPoint();
    final IPoint br = shape.getBottomRightPoint();
    final double tlx = tl.getX();
    final double tly = tl.getY();
    final double brx = br.getX();
    final double bry = br.getY();
    SVGElement elt;
    final SVGElement shad;
    final SVGElement dblBord;
    final SVGElement root = new SVGGElement(doc);
    root.setAttribute(LNamespace.LATEXDRAW_NAMESPACE + ':' + LNamespace.XML_TYPE, LNamespace.XML_TYPE_CIRCLE);
    root.setAttribute(SVGAttributes.SVG_ID, getSVGID());
    final double gap = getPositionGap();
    if (shape.hasShadow()) {
        shad = new SVGCircleElement((brx + tlx) / 2d, (bry + tly) / 2d, Math.abs((brx - tlx + gap) / 2d), doc);
        setSVGShadowAttributes(shad, true);
        root.appendChild(shad);
    }
    if (shape.hasShadow() && !shape.getLineStyle().getLatexToken().equals(PSTricksConstants.LINE_NONE_STYLE)) {
        // The background of the borders must be filled is there is a shadow.
        elt = new SVGCircleElement((brx + tlx) / 2d, (bry + tly) / 2d, Math.abs((brx - tlx + gap) / 2d), doc);
        setSVGBorderBackground(elt, root);
    }
    elt = new SVGCircleElement((brx + tlx) / 2d, (bry + tly) / 2d, Math.abs((brx - tlx + gap) / 2d), doc);
    root.appendChild(elt);
    if (shape.hasDbleBord()) {
        dblBord = new SVGCircleElement((brx + tlx) / 2d, (bry + tly) / 2d, Math.abs((brx - tlx + gap) / 2d), doc);
        setSVGDoubleBordersAttributes(dblBord);
        root.appendChild(dblBord);
    }
    setSVGAttributes(doc, elt, true);
    setSVGRotationAttribute(root);
    return root;
}
Also used : SVGGElement(net.sf.latexdraw.parsers.svg.SVGGElement) SVGCircleElement(net.sf.latexdraw.parsers.svg.SVGCircleElement) SVGElement(net.sf.latexdraw.parsers.svg.SVGElement) IPoint(net.sf.latexdraw.models.interfaces.shape.IPoint)

Example 15 with SVGElement

use of net.sf.latexdraw.parsers.svg.SVGElement 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)

Aggregations

SVGElement (net.sf.latexdraw.parsers.svg.SVGElement)39 SVGGElement (net.sf.latexdraw.parsers.svg.SVGGElement)24 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)15 SVGPolygonElement (net.sf.latexdraw.parsers.svg.SVGPolygonElement)5 Test (org.junit.Test)5 SVGDefsElement (net.sf.latexdraw.parsers.svg.SVGDefsElement)4 SVGPathElement (net.sf.latexdraw.parsers.svg.SVGPathElement)4 SVGCircleElement (net.sf.latexdraw.parsers.svg.SVGCircleElement)3 SVGRectElement (net.sf.latexdraw.parsers.svg.SVGRectElement)3 Point2D (java.awt.geom.Point2D)2 ParseException (java.text.ParseException)2 List (java.util.List)2 ShapeFactory (net.sf.latexdraw.models.ShapeFactory)2 Color (net.sf.latexdraw.models.interfaces.shape.Color)2 IArrow (net.sf.latexdraw.models.interfaces.shape.IArrow)2 IPolyline (net.sf.latexdraw.models.interfaces.shape.IPolyline)2 SVGAttr (net.sf.latexdraw.parsers.svg.SVGAttr)2 SVGAttributes (net.sf.latexdraw.parsers.svg.SVGAttributes)2 SVGDocument (net.sf.latexdraw.parsers.svg.SVGDocument)2 SVGLineElement (net.sf.latexdraw.parsers.svg.SVGLineElement)2