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 "Litton, Tom - CEPM" <To...@cepm-us.com> on 2004/12/27 23:52:56 UTC

calculating the width of text

I'm trying to calculate the amount of text that can fit into a specified width on an SVG image.  My calculations are based on the FontMetrics.stringWidth method.

For example, I have some text I want to put into a space that is 250 units long.  The FontMetrics.stringWidth gives 249 for the width of the text, however, the text overruns in the image.

I'm assuming this is because the SVG image and the FontMetrics.stringWidth are returning coordinates in different units (or a bug in my code.  Can't ever rule that out).  I believe FontMetrics.stringWidth uses pixels (please correct me if I'm wrong).  What does SVG use by default?  It should be pixels correct?  

Is there a reason why the text doesn't line up?  Is there an easier way of determining what text will fit in a given space?

Thanks,

Tom

RE: calculating the width of text

Posted by TJ Teegan <tj...@hotmail.com>.
Hello,

I have never had to do this, so I am not sure how well my suggestion will 
work.  Perhaps someone with more experience can chime in and tell you if 
this would work or not.

I think you can get the bounding box for the image you want to cover, and 
then use that width to determine how much space you have.  To determine how 
much space your text will take get the bounding box for the text element.  
Then adjust your text until its boudnding box matches the images bounding 
box.  I think to get the bounding boxes you should look at the SVGLocatable 
interface in the org.w3c.dom.svg package.

There may be better, or more efficient ways to do this.  I am just 
paraphrasing something I read in this list awhile back.

Hope it helps,
TJ Teegan

>From: "Litton, Tom - CEPM" <To...@cepm-us.com>
>Reply-To: "Batik Users" <ba...@xml.apache.org>
>To: "Batik Users" <ba...@xml.apache.org>
>Subject: calculating the width of text
>Date: Mon, 27 Dec 2004 16:52:56 -0600
>
>I'm trying to calculate the amount of text that can fit into a specified 
>width on an SVG image.  My calculations are based on the 
>FontMetrics.stringWidth method.
>
>For example, I have some text I want to put into a space that is 250 units 
>long.  The FontMetrics.stringWidth gives 249 for the width of the text, 
>however, the text overruns in the image.
>
>I'm assuming this is because the SVG image and the FontMetrics.stringWidth 
>are returning coordinates in different units (or a bug in my code.  Can't 
>ever rule that out).  I believe FontMetrics.stringWidth uses pixels (please 
>correct me if I'm wrong).  What does SVG use by default?  It should be 
>pixels correct?
>
>Is there a reason why the text doesn't line up?  Is there an easier way of 
>determining what text will fit in a given space?
>
>Thanks,
>
>Tom

_________________________________________________________________
Express yourself instantly with MSN Messenger! Download today - it's FREE! 
http://messenger.msn.click-url.com/go/onm00200471ave/direct/01/


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