Hi,
as Thomas explains, not every SVG-Tag supports the getBBox()-function.
So perhaps you can introduce an comparison with all SVG-Tags supporting
the getBBox-function.
I have realized it in the following way:
private static String[] svgTags2 = {
SVGConstants.SVG_A_TAG,
SVGConstants.SVG_ALT_GLYPH_TAG,
SVGConstants.SVG_ALT_GLYPH_DEF_TAG,
SVGConstants.SVG_ALT_GLYPH_ITEM_TAG,
SVGConstants.SVG_ANIMATE_TAG,
SVGConstants.SVG_ANIMATE_COLOR_TAG,
SVGConstants.SVG_ANIMATE_MOTION_TAG,
SVGConstants.SVG_ANIMATE_TRANSFORM_TAG,
SVGConstants.SVG_CIRCLE_TAG,
SVGConstants.SVG_CLIP_PATH_TAG,
SVGConstants.SVG_COLOR_PROFILE_TAG,
SVGConstants.SVG_CURSOR_TAG,
SVGConstants.SVG_DEFINITION_SRC_TAG,
SVGConstants.SVG_DEFS_TAG,
SVGConstants.SVG_DESC_TAG,
SVGConstants.SVG_ELLIPSE_TAG,
SVGConstants.SVG_FE_BLEND_TAG,
SVGConstants.SVG_FE_COLOR_MATRIX_TAG,
SVGConstants.SVG_FE_COMPONENT_TRANSFER_TAG,
SVGConstants.SVG_FE_COMPOSITE_TAG,
SVGConstants.SVG_FE_CONVOLVE_MATRIX_TAG,
SVGConstants.SVG_FE_DIFFUSE_LIGHTING_TAG,
SVGConstants.SVG_FE_DISPLACEMENT_MAP_TAG,
SVGConstants.SVG_FE_DISTANT_LIGHT_TAG,
SVGConstants.SVG_FE_FLOOD_TAG,
SVGConstants.SVG_FE_FUNC_A_TAG,
SVGConstants.SVG_FE_FUNC_B_TAG,
SVGConstants.SVG_FE_FUNC_G_TAG,
SVGConstants.SVG_FE_FUNC_R_TAG,
SVGConstants.SVG_FE_GAUSSIAN_BLUR_TAG,
SVGConstants.SVG_FE_IMAGE_TAG,
SVGConstants.SVG_FE_MERGE_NODE_TAG,
SVGConstants.SVG_FE_MERGE_TAG,
SVGConstants.SVG_FE_MORPHOLOGY_TAG,
SVGConstants.SVG_FE_OFFSET_TAG,
SVGConstants.SVG_FE_POINT_LIGHT_TAG,
SVGConstants.SVG_FE_SPECULAR_LIGHTING_TAG,
SVGConstants.SVG_FE_SPOT_LIGHT_TAG,
SVGConstants.SVG_FE_TILE_TAG,
SVGConstants.SVG_FE_TURBULENCE_TAG,
SVGConstants.SVG_FILTER_TAG,
SVGConstants.SVG_FONT_TAG,
SVGConstants.SVG_FONT_FACE_TAG,
SVGConstants.SVG_FONT_FACE_FORMAT_TAG,
SVGConstants.SVG_FONT_FACE_NAME_TAG,
SVGConstants.SVG_FONT_FACE_SRC_TAG,
SVGConstants.SVG_FONT_FACE_URI_TAG,
SVGConstants.SVG_FOREIGN_OBJECT_TAG,
SVGConstants.SVG_G_TAG,
SVGConstants.SVG_GLYPH_TAG,
SVGConstants.SVG_GLYPH_REF_TAG,
SVGConstants.SVG_HKERN_TAG,
SVGConstants.SVG_IMAGE_TAG,
SVGConstants.SVG_LINE_TAG,
SVGConstants.SVG_LINEAR_GRADIENT_TAG,
SVGConstants.SVG_MARKER_TAG,
SVGConstants.SVG_MASK_TAG,
SVGConstants.SVG_METADATA_TAG,
SVGConstants.SVG_MISSING_GLYPH_TAG,
SVGConstants.SVG_MPATH_TAG,
SVGConstants.SVG_PATH_TAG,
SVGConstants.SVG_PATTERN_TAG,
SVGConstants.SVG_POLYGON_TAG,
SVGConstants.SVG_POLYLINE_TAG,
SVGConstants.SVG_RADIAL_GRADIENT_TAG,
SVGConstants.SVG_RECT_TAG,
SVGConstants.SVG_SET_TAG,
SVGConstants.SVG_SCRIPT_TAG,
SVGConstants.SVG_STOP_TAG,
SVGConstants.SVG_STYLE_TAG,
SVGConstants.SVG_SVG_TAG,
SVGConstants.SVG_SWITCH_TAG,
SVGConstants.SVG_SYMBOL_TAG,
SVGConstants.SVG_TEXT_PATH_TAG,
SVGConstants.SVG_TEXT_TAG,
SVGConstants.SVG_TITLE_TAG,
SVGConstants.SVG_TREF_TAG,
SVGConstants.SVG_TSPAN_TAG,
SVGConstants.SVG_USE_TAG,
SVGConstants.SVG_VIEW_TAG,
SVGConstants.SVG_VKERN_TAG,
};
private static HashSet<String> svgTagSet = new
HashSet<String>(Arrays.asList(svgTags));
later in the code I use
if (node instanceof SVGLocatable)
{
SVGLocatable locatable = (SVGLocatable) node;
if (svgTagSet.contains(node.getNodeName()))
{
SVGRect rect = locatable.getBBox();
....
}
}
This works for me without any problem.
Florian
-----Original Message-----
From: Nazar Stasiv (Lohika, Inc) [mailto:[EMAIL PROTECTED]
Sent: Tuesday, August 15, 2006 12:37 PM
To: [email protected]
Subject: Re: SVGOMGElement and coordinates?
Well, that is the way I continued investigation yesterday. The answer I
had found was that not every SVGLocatable.getBBox returns valid value.
For most of elements it returned null. However some of the elements had
reasonable values.
if (e instanceof SVGLocatable){
SVGRect r = getSVGRect((SVGLocatable) e);
if (r!= null) {
System.out.print("+");
System.out.println(e.getClass().getName());
} else {
System.out.print("-");
System.out.println(e.getClass().getName());
}
}
Sample output (excerpt)
+org.apache.batik.dom.svg.SVGOMGElement
+org.apache.batik.dom.svg.SVGOMRectElement
+org.apache.batik.dom.svg.SVGOMGElement
+org.apache.batik.dom.svg.SVGOMPathElement
-org.apache.batik.dom.svg.SVGOMPathElement
-org.apache.batik.dom.svg.SVGOMRectElement
-org.apache.batik.dom.svg.SVGOMGElement
What is the reason that the element has no boundingBox?
Pepping, Florian wrote:
> Hi Nazer,
>
> mhhh, that's strange. As Thomas said, that's the way it should
normally
> work and my code looks very similiar to yours and it works.
> Perhaps you can do one more thing. Test, if the node you are looking
for
> is an instance of SVGLocatable [if (node instanceof SVGLocatable)
{...}]
> Only if this is true, you can get the BoundingBox of this element.
>
> If this also doesn't work, make a very simple example and test it
there
> first. Perhaps you can get closer to the problem then.
>
> Florian
>
>
> -----Original Message-----
> From: Nazar Stasiv (Lohika, Inc) [mailto:[EMAIL PROTECTED]
> Sent: Monday, August 14, 2006 5:47 PM
> To: [email protected]
> Subject: Re: SVGOMGElement and coordinates?
>
> Florian,
>
> I get null here for return value
>
> public static SVGRect getSVGRect(SVGLocatable e){
> return e.getBBox(); //<-- returns null.
> }
>
> And initialization I do with the code below
>
> public static SVGDocument parseSvg(String uri) {
> UserAgent userAgent = new UserAgentAdapter();
> DocumentLoader loader = new DocumentLoader(userAgent);
> SVGDocument doc;
> try {
> doc = (SVGDocument) loader.loadDocument(uri);
> } catch (Exception e) {
> e.printStackTrace();
> throw new RuntimeException(e);
> }
> BridgeContext ctx = new BridgeContext(userAgent, loader);
> ctx.setDynamicState(BridgeContext.DYNAMIC);
> GVTBuilder builder = new GVTBuilder();
> GraphicsNode rootGN = builder.build(ctx, doc);
> loader.dispose();
> return doc;
> }
> Everything goes fine (no exceptions in parseSvg(String)) untill
> getSVGRect is called. Please help.
>
> Pepping, Florian wrote:
>
>> Hi Nazer,
>>
>> I think I had the same problem some time ago. Before you can use
>> getBBox() you have to build the GVT-Tree in order to get this
>> information.
>> You can do this in the following way:
>> SVGDocument svgDoc =
>> (SVGDocument)loader.loadDocument(file.toURL().toString());
>> loader.dispose();
>> BridgeContext ctx = new BridgeContext(userAgent,
>> loader);
>> ctx.setDynamicState(BridgeContext.DYNAMIC);
>> GVTBuilder builder = new GVTBuilder();
>> builder.build(ctx, svgDoc);
>>
>> // use getBBox() afterwards
>>
>> After this, SVGLocatable.getBBox() shouldn't return null any longer.
>>
>> Florian
>>
>> -----Original Message-----
>> From: Nazar Stasiv (Lohika, Inc) [mailto:[EMAIL PROTECTED]
>> Sent: Monday, August 14, 2006 3:16 PM
>> To: [email protected]
>> Subject: Re: SVGOMGElement and coordinates?
>>
>>
>> SVGLocatable.getBBox(elem) returns null No javadoc comments are there
>>
> to
>
>> unveil the reason of such behaviour
>>
>> Is there the other way to get coordinates of SVGOMGElement ?
>>
>>
>> [EMAIL PROTECTED] wrote:
>>
>>
>>> Hi Nazer,
>>>
>>> "Nazar Stasiv (Lohika, Inc)" <[EMAIL PROTECTED]> wrote on
08/14/2006
>>>
>
>
>>> 07:29:19 AM:
>>>
>>>
>>>
>>>
>>>> I use batik to operate SVG at runtime.
>>>>
>>>> The question is how can I get SVGOMGElement coordinates when
walking
>>>>
>>>>
>> DOM
>>
>>
>>>> tree of the SVG document?
>>>>
>>>>
>>>>
>>> You should look at the SVG DOM. In particular the 'getBBox'
>>>
>>>
>> method.
>>
>>
>>> There are also methods get 'getIntersectionList' that could be
>>>
> useful.
>
>>>
>>>
>>>
>>>> The reason I'm asking such a thing it that requirements are to
>>>>
>>>>
>> display
>>
>>
>>>> info popup near named elements of SVG. If I had coordinates of such
>>>>
>>>>
>> an
>>
>>
>>>> element I could use DOM to add new element to the tree with
computed
>>>> coordinates for correct positioning of the info popup.
>>>>
>>>>
>>>>
>>> This is typically done with mouse over/out event handlers. This
>>> allows you to receive a DOM event when the mouse enters/exits a
>>>
>>>
>> particular
>>
>>
>>> element or group in the SVG document. If you need the sensitive
area
>>>
>>>
>> to
>>
>>
>>> be larger than the rendered area you can add a hidden element
>>> (visibility="hidden" pointer-events="fill") that can cover the
>>>
>>>
>> expanded
>>
>>
>>> area and tie your event handler to that.
>>>
>>>
>>>
---------------------------------------------------------------------
>>> To unsubscribe, e-mail:
>>>
> [EMAIL PROTECTED]
>
>>> For additional commands, e-mail:
>>>
>>>
>> [EMAIL PROTECTED]
>>
>>
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
[EMAIL PROTECTED]
>> For additional commands, e-mail:
>>
> [EMAIL PROTECTED]
>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
[EMAIL PROTECTED]
>> For additional commands, e-mail:
>>
> [EMAIL PROTECTED]
>
>>
>>
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail:
[EMAIL PROTECTED]
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: [EMAIL PROTECTED]
> For additional commands, e-mail:
[EMAIL PROTECTED]
>
>
>
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]