You are viewing a plain text version of this content. The canonical link for it is here.
Posted to batik-users@xmlgraphics.apache.org by Даниил Баскаков <db...@gmail.com> on 2011/11/10 07:35:58 UTC

Use (xlink), Style and dynamic updates problem

Hi,

found a strange behavior when dynamically update a style of element reused
by <use xlink:href="..."/>.
I have a drawing where a path element declared in defs like that:

    <defs>
        <g id="stage1src">
            <path id="propeller" d="M36.039,210c7.751,0,15.501,0,23.253,0
..... "/>
        </g>
    </defs>

    <g id="stage1">
        <use xlink:href="#stage1src"/>
    </g>

When I try to dynamically change the "propeller" path color as follows:

    final SVGDocument svgDocument = canvas.getSVGDocument();
    final SVGStylableElement propeller = (SVGStylableElement)
svgDocument.getElementById("propeller");

    updateInRunnableQueue(new Runnable()
    {
      public void run()
      {
        final SVGStylableElement propeller =
(SVGStylableElement) canvas.getSVGDocument().getElementById("propeller");
        CSSStyleDeclaration overrideStyle = propeller.getOverrideStyle();
        overrideStyle.setCssText("fill: " + htmlColor);
      }
    });

Nothing happens. But this is more or less predictable. Assume that Batik
can not dynamically track changes for <use xlink:href="#stage1src"/>.
OK. I dig this list and found that most common decision to make the Batik
to handle such changes is to reset affected document nodes.
So, let's reset the root node for simplicity after making a style changes
(in updateRunnableQueue):

final SVGStylableElement propeller =
(SVGStylableElement) canvas.getSVGDocument().getElementById("propeller");
CSSStyleDeclaration overrideStyle = propeller.getOverrideStyle();
overrideStyle.setCssText("fill: " + htmlColor);

final SVGSVGElement rootElement = svgDocument.getRootElement();
svgDocument.removeChild(rootElement);
svgDocument.appendChild(rootElement);

Viola! Now the drawing visually change it's color.

The problem begins when I add a style declaration in the drawing's SVG
file, even empty one:

    <style type="text/css">
    </style>

The root node resetting stops to make the drawing changes visible! The one
thing helps to dynamically see the changes is to completely reload the
document (in updateRunnableQueue):

final SVGStylableElement propeller =
(SVGStylableElement) canvas.getSVGDocument().getElementById("propeller");
CSSStyleDeclaration overrideStyle = propeller.getOverrideStyle();
overrideStyle.setCssText("fill: " + htmlColor);

canvas.setSVGDocument(canvas.getSVGDocument());

But completely reloading a document is a very expensive operation.

Can someone tell me why resetting the root node not works in this case?
How to avoid complete document reloading to see the document changes?

PS: A SVG file and a demo program are attached.