xmlgraphics-batik-users mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Andrew Rowlands <a.rowla...@uq.edu.au>
Subject BBox for text on path
Date Wed, 07 Mar 2012 23:08:52 GMT
Hi Thomas,

You might remember I mentioned I previously had issues with the bbox when using text on path.
 The idea was to resize the text so that the entire string would fit along the path such that
the bbox would be <= a certain width.

Basically, the kind of approach I was taking was as below (no canvas, running as servlet under
Tomcat).  For certain strings though, bbox didn't seem to calculate the bbox correctly.

In the end I used getComputedTextLength() instead which worked out great.. but I really would
be interested to know any ideas why using bbox didn't work so well...


String parser = XMLResourceDescriptor.getXMLParserClassName();
SAXSVGDocumentFactory factory = new SAXSVGDocumentFactory(parser);
curDocument = factory.createSVGDocument(
               new File(svgFileName).toURL().toString());

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

// Text element to update
Element elem = curDocument.getElementById("text_element");
Node n = elem.getFirstChild();
Text text;

// There is a textPath that wraps the text
text = (Text) n.getFirstChild();
text.setNodeValue("a b c d e f g h i j k l m n");

// Resize text

// The SVG was then saved using the transcoder
// ...

// Resize function:
public void resizeTextSizeToFit(String elemID) {

       Element elem = curDocument.getElementById(elemID);
       if (elem == null) {

       SVGLocatable elemLocatable = (SVGLocatable) curDocument.getElementById(elemID);
       float currentFontSize;

       SVGRect bbox = elemLocatable.getBBox();
       float width = bbox.getWidth();

       System.out.println("Original width: " + Float.toString(width));

   if (width >= 430) {
      float currentFontSize = Float.parseFloat(elem.getAttribute("font-size"));
       currentFontSize -= 2;
       elem.setAttributeNS(null, "font-size", Float.toString(currentFontSize));


From: Thomas DeWeese [thomas.deweese@gmail.com]
Sent: 22 February 2012 10:12
To: batik-users@xmlgraphics.apache.org<mailto:batik-users@xmlgraphics.apache.org>
Cc: batik-users@xmlgraphics.apache.org<mailto:batik-users@xmlgraphics.apache.org>
Subject: Re: Bounding Box accuracy for text containing ISO (&#198;) characters

   The bbox and other APIs in Batik should always return the right value as long as you don't
have multiple threads manipulating the tree at the same time.

View raw message