xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Alan Deikman <Alan.Deik...@znyx.com>
Subject Re: Merging one document with another in a JSVGCanvas -- one solution
Date Mon, 13 Apr 2009 18:57:04 GMT
Helder Magalhães wrote:
> I'm not particularly interested, but yes: sharing a/the solution (or a
> possible workaround, etc.), even if just a summary of it, is generally
> welcome [1]... ;-)
OK.   I do have an outstanding question about my own code, but it is an 
SVG question and not anything to do with Batik.  The question is what is 
the role of the viewBox attribute in the imported node?  I found that if 
I do not set it to the viewport parameters this doesn't work.

        SVGDocument ourDoc = getSVGDocument();
        SVGSVGElement root = ourDoc.getRootElement();

        SVGDocument doc = /* the document to import */ ;

        // import the root SVG element

        SVGSVGElement panelRoot = doc.getRootElement();
        SVGOMSVGElement node = (SVGOMSVGElement)
                                ourDoc.importNode(panelRoot, true);

        // make sure we are using the right namespace

        String ns = root.getNamespaceURI();

        // discover the rect where we want the new graphic to go

        SVGRect position = /* where we want node to appear */;

        // the original panel width is used to calculate the scale
        // factor.  We assume that the aspect ratio of node and root are 
the same

        float originalPanelWidth = node.getWidth().getBaseVal().getValue();

        // set the viewport

        node.setAttribute("x", Double.toString(position.getX()));
        node.setAttribute("y", Double.toString(position.getY()));
        node.setAttribute("width", Double.toString(position.getWidth()));
        node.setAttribute("height", Double.toString(position.getHeight()));

        // set the viewbox to the same as the view port

        node.setAttribute("viewBox", String.format("%f %f %f %f",
                       position.getX(), position.getY(),
                       position.getWidth(), position.getHeight()));

        // create a g element and transfer all of the svg element's
        // elements to it, then make the g element the only child
        // of the original g element

        SVGOMGElement g = (SVGOMGElement)
                            ourDoc.createElementNS(ns, "g");
        Node child;
        while ((child = node.getFirstElementChild()) != null)

        // move and scale to the position without rotation

        if (!isRotationNeeded()) {
            g.setAttribute("transform", String.format(
                    "translate(%f, %f) scale(%f)",
                    position.getX(), position.getY(),
                    position.getWidth() / originalPanelWidth));

        // or rotate 90 degrees if needed

        else {
            g.setAttribute("transform", String.format(
                    "rotate(90) translate(%f, %f) scale(%f)",
                    0 - position.getX() - position.getWidth(),
                    position.getHeight() / originalPanelWidth));

        // attach to the new SVG element to our root and we are done

I hope someone finds this useful even though there are probably more 
elegant ways of getting this done.

Alan Deikman

To unsubscribe, e-mail: batik-users-unsubscribe@xmlgraphics.apache.org
For additional commands, e-mail: batik-users-help@xmlgraphics.apache.org

View raw message