Search in sources :

Example 1 with SVGOMDocument

use of org.apache.batik.anim.dom.SVGOMDocument in project yamcs-studio by yamcs.

the class SimpleImageTranscoder method initCSSEngine.

/**
 * Call before querying for CSS properties. If document has CSS engine installed returns null. Client is responsible
 * to dispose bridge context if it was returned by this method.
 */
public BridgeContext initCSSEngine() {
    if (this.document == null) {
        return null;
    }
    SVGOMDocument sd = (SVGOMDocument) this.document;
    if (sd.getCSSEngine() != null) {
        return null;
    }
    class BridgeContextEx extends BridgeContext {

        public BridgeContextEx() {
            super(SimpleImageTranscoder.this.userAgent);
            BridgeContextEx.this.setDocument(SimpleImageTranscoder.this.document);
            BridgeContextEx.this.initializeDocument(SimpleImageTranscoder.this.document);
        }
    }
    return new BridgeContextEx();
}
Also used : SVGOMDocument(org.apache.batik.anim.dom.SVGOMDocument) BridgeContext(org.apache.batik.bridge.BridgeContext)

Example 2 with SVGOMDocument

use of org.apache.batik.anim.dom.SVGOMDocument in project yamcs-studio by yamcs.

the class SimpleImageTranscoder method applyMatrix.

private Document applyMatrix(double[][] matrix) {
    // creation of the SVG document
    DOMImplementation impl = SVGDOMImplementation.getDOMImplementation();
    String svgNS = SVGDOMImplementation.SVG_NAMESPACE_URI;
    final Document newDocument = impl.createDocument(svgNS, "svg", null);
    // get the root element (the 'svg' element).
    Element svgRoot = newDocument.getDocumentElement();
    // get the original document size
    SVGSVGElement svgElmt = ((SVGOMDocument) originalDocument).getRootElement();
    double width = 30;
    double height = 30;
    try {
        width = svgElmt.getWidth().getBaseVal().getValue();
        height = svgElmt.getHeight().getBaseVal().getValue();
    } catch (NullPointerException e) {
        // FIXME
        // this is a dirty workaround for the RAP problem, which doesn't know how to
        // transform between units and pixels. Here we assume that all units are inches
        // and 96 dpi is used.
        SVGLength length = svgElmt.getWidth().getBaseVal();
        double value = length.getValueInSpecifiedUnits();
        width = value * 25.4 / 0.26458333333333333333333333333333;
        length = svgElmt.getHeight().getBaseVal();
        value = length.getValueInSpecifiedUnits();
        height = value * 25.4 / 0.26458333333333333333333333333333;
    }
    // current Transformation Matrix
    double[][] CTM = { { matrix[0][0], matrix[0][1], 0 }, { matrix[1][0], matrix[1][1], 0 }, { 0, 0, 1 } };
    // apply permutation to viewBox corner points
    double[] a = transformP(0.0, 0.0, 1.0, CTM);
    double[] b = transformP(width, 0.0, 1.0, CTM);
    double[] c = transformP(width, height, 1.0, CTM);
    double[] d = transformP(0.0, height, 1.0, CTM);
    // find new points
    double minX = findMin(a[0], b[0], c[0], d[0]);
    double minY = findMin(a[1], b[1], c[1], d[1]);
    double maxX = findMax(a[0], b[0], c[0], d[0]);
    double maxY = findMax(a[1], b[1], c[1], d[1]);
    double newWidth = maxX - minX;
    double newHeight = maxY - minY;
    // set the width and height attributes on the root 'svg' element.
    svgRoot.setAttributeNS(null, "width", String.valueOf(newWidth));
    svgRoot.setAttributeNS(null, "height", String.valueOf(newHeight));
    String vbs = minX + " " + minY + " " + newWidth + " " + newHeight;
    svgRoot.setAttributeNS(null, "viewBox", vbs);
    svgRoot.setAttributeNS(null, "preserveAspectRatio", "none");
    // Create the transform matrix
    StringBuilder sb = new StringBuilder();
    // a c e
    // b d f
    // 0 0 1
    sb.append("matrix(");
    sb.append(CTM[0][0] + ",");
    sb.append(CTM[1][0] + ",");
    sb.append(CTM[0][1] + ",");
    sb.append(CTM[1][1] + ",");
    sb.append(CTM[0][2] + ",");
    sb.append(CTM[1][2] + ")");
    Element graphic = newDocument.createElementNS(svgNS, "g");
    graphic.setAttributeNS(null, "transform", sb.toString());
    // Attach the transform to the root 'svg' element.
    Node copiedRoot = newDocument.importNode(originalDocument.getDocumentElement(), true);
    graphic.appendChild(copiedRoot);
    svgRoot.appendChild(graphic);
    // }
    return newDocument;
}
Also used : SVGSVGElement(org.w3c.dom.svg.SVGSVGElement) SVGStylableElement(org.apache.batik.anim.dom.SVGStylableElement) Element(org.w3c.dom.Element) Node(org.w3c.dom.Node) SVGLength(org.w3c.dom.svg.SVGLength) DOMImplementation(org.w3c.dom.DOMImplementation) SVGDOMImplementation(org.apache.batik.anim.dom.SVGDOMImplementation) Document(org.w3c.dom.Document) SVGOMDocument(org.apache.batik.anim.dom.SVGOMDocument) SVGSVGElement(org.w3c.dom.svg.SVGSVGElement) SVGOMDocument(org.apache.batik.anim.dom.SVGOMDocument)

Example 3 with SVGOMDocument

use of org.apache.batik.anim.dom.SVGOMDocument in project yamcs-studio by yamcs.

the class SimpleImageTranscoder method getDocumentSize.

public Dimension getDocumentSize() {
    SVGSVGElement svgElmt = ((SVGOMDocument) document).getRootElement();
    double width = svgElmt.getWidth().getBaseVal().getValue();
    double height = svgElmt.getHeight().getBaseVal().getValue();
    return new Dimension((int) Math.round(width), (int) Math.round(height));
}
Also used : SVGOMDocument(org.apache.batik.anim.dom.SVGOMDocument) Dimension(java.awt.Dimension) SVGSVGElement(org.w3c.dom.svg.SVGSVGElement)

Aggregations

SVGOMDocument (org.apache.batik.anim.dom.SVGOMDocument)3 SVGSVGElement (org.w3c.dom.svg.SVGSVGElement)2 Dimension (java.awt.Dimension)1 SVGDOMImplementation (org.apache.batik.anim.dom.SVGDOMImplementation)1 SVGStylableElement (org.apache.batik.anim.dom.SVGStylableElement)1 BridgeContext (org.apache.batik.bridge.BridgeContext)1 DOMImplementation (org.w3c.dom.DOMImplementation)1 Document (org.w3c.dom.Document)1 Element (org.w3c.dom.Element)1 Node (org.w3c.dom.Node)1 SVGLength (org.w3c.dom.svg.SVGLength)1