hello list,
i am having difficulties grasping the relationship between the SvgBuilder and
the DOM tree. for the
task i am trying to implement, an svg document would travel along a pipeline
with several steps,
each step adding stuff to it.
the things added are nodes copied from other svgs, but they also need to be
transformed, so the
initial approach was to create an empty SVG and boot it, then pass it along
with it's bridge context,
graphics node and svg graphics 2d through this pipeline, the each step could
use regular DOM
methods to clone elements from other SVGs into the current one, then use SVG
specific versions
of the elements to do transforms etc.
the problem occurs when i try to get the SVG node associated with the "dom
version" of it - i can get
a node of the correct type, but i cannot get it's BBox. below is the code i am
using and it's output.
a sidenote: what's the best way to make sure that all drawing operations on
SVGGraphics2D are
reflected in the DOM? obviously, i could do stream() and then parse the output
again … but that
seems like a rather expensive way to do that …
any suggestions? what am i missing?
thanks!
.rm
---------------------------------------------------------------------------------------------------------------------------------------------------
DOMImplementation domImpl = new SVGDOMImplementation();
SVGDocument svg = (SVGDocument) domImpl.createDocument(
SvgNamespaceContext.SVG_NS_URI,
SvgNamespaceContext.SVG_NS_PREFIX,
null);
SVGGraphics2D graphics = new SVGGraphics2D(svg);
UserAgent userAgent = new UserAgentAdapter();
DocumentLoader loader = new DocumentLoader(userAgent);
BridgeContext ctx = new BridgeContext(userAgent,
loader);
ctx.setDynamicState(BridgeContext.DYNAMIC);
GVTBuilder builder = new GVTBuilder();
GraphicsNode rootGN = builder.build(ctx, svg);
System.err.println("===> A");
// does not work:
graphics.drawRect(10,10,200,300);
Document d = ctx.getDocument();
ctx.get
System.err.println();
XmlUtil.transform(ctx.getDocument(), System.err);
System.err.println();
System.err.println("===> B");
Element n =
ctx.getDocument().createElementNS(SvgNamespaceContext.SVG_NS_URI,"rect");
SvgUtil.setSvgAttribute(n, "id", "test-circle");
SvgUtil.setSvgAttribute(n, "x", 2);
SvgUtil.setSvgAttribute(n, "y", 30);
SvgUtil.setSvgAttribute(n, "width", 200);
SvgUtil.setSvgAttribute(n, "height", 100);
SvgUtil.setSvgAttribute(n, "style",
"fill:#ff0000;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1");
ctx.getDocument().getDocumentElement().appendChild(n);
System.err.println();
XmlUtil.transform(ctx.getDocument(), System.err);
System.err.println();
System.err.println("===> C: ");
Node n2 = graphics.getRoot(n);
System.err.println();
System.err.println(" ========> "+n2);
System.err.println(" ========> "+((SVGLocatable
)n2).getBBox());
System.err.println();
---------------------------------------------------------------------------------------------------------------------------------------------------
and the output is as follows:
===> A
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
contentScriptType="text/ecmascript" zoomAndPan="magnify"
contentStyleType="text/css" preserveAspectRatio="xMidYMid meet"
version="1.0" />
===> B
<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns:xlink="http://www.w3.org/1999/xlink"
xmlns="http://www.w3.org/2000/svg"
contentScriptType="text/ecmascript" zoomAndPan="magnify"
contentStyleType="text/css" preserveAspectRatio="xMidYMid meet"
version="1.0">
<rect y="30"
style="fill:#ff0000;fill-rule:evenodd;stroke:#000000;stroke-width:1px;stroke-linecap:butt;stroke-linejoin:miter;stroke-opacity:1"
x="2" id="test-circle" width="200" height="100" />
</svg>
===> C:
========> org.apache.batik.dom.svg.SVGOMRectElement@3c3a1834
^^^ this is actually exactly what i expect
========> null
^^^ this is not quite what i want …