Search in sources :

Example 1 with SVGLinearGradientElement

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

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

Aggregations

SVGElement (net.sf.latexdraw.parsers.svg.SVGElement)2 SVGLinearGradientElement (net.sf.latexdraw.parsers.svg.SVGLinearGradientElement)2 SVGDefsElement (net.sf.latexdraw.parsers.svg.SVGDefsElement)1 SVGPatternElement (net.sf.latexdraw.parsers.svg.SVGPatternElement)1 SVGStopElement (net.sf.latexdraw.parsers.svg.SVGStopElement)1 SVGTransform (net.sf.latexdraw.parsers.svg.SVGTransform)1