Search in sources :

Example 1 with SVGLength

use of org.w3c.dom.svg.SVGLength 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)

Aggregations

SVGDOMImplementation (org.apache.batik.anim.dom.SVGDOMImplementation)1 SVGOMDocument (org.apache.batik.anim.dom.SVGOMDocument)1 SVGStylableElement (org.apache.batik.anim.dom.SVGStylableElement)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 SVGSVGElement (org.w3c.dom.svg.SVGSVGElement)1