xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dao <dao.ho...@gmail.com>
Subject unlinking a use element from its symbol
Date Wed, 09 Jun 2010 14:48:41 GMT
hello,

I want to unlink a use element from the symbol it refers. I use the piece of
code below to implement the behavior described in
http://www.w3.org/TR/SVG11/struct.html#UseElement, section *If the
'use'element references a
'symbol' element*

Now, I set a mouse move event handler on the <g> element, I modify the
"transform" element but nothing hapends. I save the modified document (ie:
does not contain the use element anymore) and reopen it (new canvas) and it
works now.

can someone tell me what's wrong below? By the way, perhaps the unlink from
symbol facility exists in batik?

thank's


    /**
     * actually making what is performed in
http://www.w3.org/TR/SVG11/struct.html#UseElement
     * to really have handles on the id's that are necessary for the engine.
     * @param component
     */
    public static void unlinkSymbol(SvgComponent component) {
        SVGDocument doc = component.getCanvas().getSVGDocument();
        SVGOMElement use = component.getElt();

        Element symbol = getSymbolElement(component, doc);

        //the new group element contains an SVG one.
        Element g = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI,
"g");
        Element svg = doc.createElementNS(SVGConstants.SVG_NAMESPACE_URI,
"svg");
        g.appendChild(svg);

        /**
         * add the attributes of the g and svg nodes to keep the
transformation attributes
         *
         */
        String x="0";
        String y="0";
        NamedNodeMap useAttributes = use.getAttributes();
        for (int i = 0; i < useAttributes.getLength(); i++) {
            Attr attr = (Attr) useAttributes.item(i);
            if (!specialBehaviorAttr.containsKey(attr.getName())) {
                g.setAttributeNode(attr);
            } else if (attr.getName().equals("x")) {
                x=attr.getValue();
            } else if (attr.getName().equals("y")) {
                y=attr.getValue();
            }
        }
        if ("0"!=x && "0"!="y") {
            String transform =
use.getAttribute(SVGConstants.SVG_TRANSFORM_ATTRIBUTE);
            g.setAttributeNS(null, SVGConstants.SVG_TRANSFORM_ATTRIBUTE,
transform+" translate("+x+","+y+")");
        }

        //inline the nodes of the symbol in the svg. ensuring width and
height is at least present.
        svg.setAttributeNS(null, "width", "100%");
        svg.setAttributeNS(null, "height", "100%");
        NamedNodeMap symbolAttributes = symbol.getAttributes();
        for (int i = 0; i < symbolAttributes.getLength(); i++) {
            Attr attr = (Attr) symbolAttributes.item(i);
            if (!"id".equals(attr.getName()))
                svg.setAttributeNode(attr);
        }

        //overriding the width and height
        if (use.getAttribute(SVGConstants.SVG_WIDTH_ATTRIBUTE).length()>0) {
            svg.setAttribute(SVGConstants.SVG_WIDTH_ATTRIBUTE,
use.getAttribute(SVGConstants.SVG_WIDTH_ATTRIBUTE));
        }
        if (use.getAttribute(SVGConstants.SVG_HEIGHT_ATTRIBUTE).length()>0)
{
            svg.setAttribute(SVGConstants.SVG_HEIGHT_ATTRIBUTE,
use.getAttribute(SVGConstants.SVG_HEIGHT_ATTRIBUTE));
        }

        NodeList childNodes = symbol.getChildNodes();
        for (int i=0; i<childNodes.getLength(); i++) {
            Node node = childNodes.item(i).cloneNode(true);
            if (node instanceof Element) {
                Element clone = (Element) node;
                //recursively change the id of the elements to avoid
conflicts
                generateId(doc, clone,
component.getId()+"_"+clone.getAttribute("id"));
                changeId(doc, clone.getChildNodes(), component.getId());

            }
            svg.appendChild(node);
        }


        //mark the element as an unlinked symbol so as it will be processed
        //as a symbol
        g.setAttributeNS(null, UNLINKEDSYMBOL_ATTR_NAME,
symbol.getAttribute("id"));

        Node parent = use.getParentNode();
        parent.insertBefore(g,use);
        parent.removeChild(use);

        component.setElt((SVGOMElement) g);

        component.getPcs().firePropertyChange(SYMBOL_UNLINKED_PROP, null,
null);
    }


-- 
Dao Hodac

Mime
View raw message