xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thomas DeWeese <Thomas.DeWe...@Kodak.com>
Subject Re: Obtaining glyph bounding box
Date Wed, 02 Feb 2005 20:53:03 GMT
Hi James,

    You will need to build the GVT tree (which is what the
JSVGCanvas calls do except you aren't waiting for the build
events, you are also not making the canvas ALWAYS_DYNAMIC
so for a simple document it won't link the DOM to graphics).

    The code to build the graphics tree for the DOM explicitly
rather than relying on the JSVGCanvas (which is kinda ugly):

         UserAgent userAgent   = new UserAgentAdapter();
         DocumentLoader loader = new DocumentLoader(userAgent);
         BridgeContext ctx     = new BridgeContext(userAgent,loader);
         ctx.setDynamicState(BridgeContext.DYNAMIC);
         GVTBuilder builder    = new GVTBuilder();
         builder.build(ctx, document);

    Most of the classes are from the bridge package.

James Shaw wrote:

> Thomas DeWeese wrote:
> 
>> Hi James,
>>
>>    To get back to your original question if all you want is the
>> bounding box of elements in the DOM, you can do this right with
>> the SVG DOM (svgelem.getBBox()).  The only advantage of this is
>> that you can using getElementById to avoid the need to walk the
>> whole tree.  Also using the SVG DOM you can even get the
>> location/size of individual glyphs from a text node.  Take a look
>> at:  samples/tests/spec/scripting/text_content.svg
>>
>>
> This method looks promising, but I've failed to make it work.  A 
> NullPointerException is raised when I do bbox.getX() (or any other 
> method of SVGRect).
> I think this is due to a null SVGContext.  Presumably some intermediate 
> steps need to be taken before I can invoke the SVGRect methods.
> 
> My code looks like this:
> 
> Element textNode = doc.createElementNS(SVG_NS, "svg:text");
> textNode.setAttributeNS(null, "font-family", "LilyPond-feta-20");
> textNode.setAttributeNS(null, "font-size",   "18pt");
> textNode.setAttributeNS(null, "x", "0");
> textNode.setAttributeNS(null, "y", "0");
> 
> Element altGlyph = doc.createElementNS(SVG_NS, "svg:altGlyph");
> altGlyph.setAttributeNS(XLINK_NS, "xlink:href", "#sixtyfourthrest");
> altGlyph.appendChild(doc.createTextNode("?"));
> 
> textNode.appendChild(altGlyph);
> doc.getDocumentElement().appendChild(textNode);
> 
> // not sure if the following two lines are needed
> JSVGComponent svg = new JSVGComponent();
> svg.setSVGDocument((SVGDocument)doc);
> 
> SVGRect bbox = ((SVGOMTextElement)textNode).getBBox();
> System.out.println("MinX: " + bbox.getX());
> 
> Thanks
> James Shaw
> 
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: batik-users-unsubscribe@xml.apache.org
> For additional commands, e-mail: batik-users-help@xml.apache.org
> 


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


Mime
View raw message