Search in sources :

Example 1 with SVGPatternElement

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

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

SVGPatternElement (net.sf.latexdraw.parsers.svg.SVGPatternElement)2 IPoint (net.sf.latexdraw.models.interfaces.shape.IPoint)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 SVGLinearGradientElement (net.sf.latexdraw.parsers.svg.SVGLinearGradientElement)1 SVGPathElement (net.sf.latexdraw.parsers.svg.SVGPathElement)1 SVGRectElement (net.sf.latexdraw.parsers.svg.SVGRectElement)1