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 Thomy <th...@gmail.com> on 2009/05/26 20:11:45 UTC

Re: Define String Width with bounding box


>    If you want to know actual text content positions I would suggest
> that the text content SVG DOM Api's would be a better choice than
> getBBox:
> 
>         http://www.w3.org/TR/SVG/text.html#DOMInterfaces
> 
>   Take a look at 'samples/tests/spec/scripting/text_content.svg'
> in the distribution for an example of using these API's. 
> In particular you can call 'getEndPositionOfChar(index)' for the 
> trailing space to get the location of the end of the space. 

Hi,
I tried this solution and it's more accurate, but it's not the "ultimate"
solution for my space problem.
In fact, i compute height and width of each character in a String with the
following code :

		SVGTextElement retourLigne = (SVGTextElement)
docCalcul.createElementNS(svgNS, "text");
		retourLigne.setAttributeNS(null, "x", "0");
		retourLigne.setAttributeNS(null, "y", "0");
		retourLigne.setAttributeNS(null, "font-family", fontFamily);
		retourLigne.setAttributeNS(null, "font-size", String.valueOf(fontSize));
		retourLigne.setAttributeNS(xmlNS, "xml:space", "preserve"); 
		int retour = 0;

		Text tmpText = docCalcul.createTextNode(string);
		retourLigne.appendChild(tmpText);
		docCalcul.getDocumentElement().appendChild(retourLigne);
		retour += retourLigne.getEndPositionOfChar(0).getX();
		docCalcul.getDocumentElement().removeChild(retourLigne);

It's worked well for all character but the space width isn't exact.
Can you tell me why ? 
I need an exact size for my compute.

Here is a snapshot of the result :
Each red rectangle is the bounding rectangle of a character.
http://www.hiboox.fr/go/images/informatique/spaceproblem,8595d6a7be900c2c0c63600b7dc0678e.png.html

You can easily see the problem on the first space.
The more space i have in a string, the biggest the error is !

Cheers,
-- 
View this message in context: http://www.nabble.com/Define-String-Width-with-bounding-box-tp20228931p23728414.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Define String Width with bounding box

Posted by Thomy <th...@gmail.com>.

thomas.deweese wrote:
> 
> Hi Thomy,
> 
> Thomy <th...@gmail.com> wrote on 05/26/2009 02:11:45 PM:
> 
>> >    If you want to know actual text content positions I would suggest
>> > that the text content SVG DOM Api's would be a better choice than
>> > getBBox:
>> > 
>> >         http://www.w3.org/TR/SVG/text.html#DOMInterfaces
>> > 
>> >   Take a look at 'samples/tests/spec/scripting/text_content.svg'
>> > in the distribution for an example of using these API's. 
>> > In particular you can call 'getEndPositionOfChar(index)' for the 
>> > trailing space to get the location of the end of the space. 
> 
>> I tried this solution and it's more accurate, but it's not the 
> "ultimate"
>> solution for my space problem.
>> In fact, i compute height and width of each character in a String with 
> the
>> following code :
> 
>    This code looks a little weird.  It looks to me like you are
> calculating the width of each character in isolation. 

That's exactly what i'm trying to do. My algorithm evolve since my first
message and i need to work on character positions, not on string.
But i found why there is a size error : i use integer to store values. If i
pass them in float, there is not error anymore.

> This is a bad idea because Batik implements kerning which can adjust the
> space between characters on a pairwise basis.  The example for
> pairwise kerning I always use is my last name: DeWeese.  With a 
> lot of fonts the two 'e' glyphs will be kerned in under the 
> "arms" of the capital 'W', if you don't do that the W can 
> look disconnected, but the default advance can't be shorter
> otherwise tall letters (l, h, t, almost all Caps) would collide
> with the top of the 'W'.

Good to know, so i will do some tests with your name ;)
In fact i compute character size one by one, but when i add text element to
my svg, i pass the entire string
Then, i made one polygone shape for each character (red polygones). Each x2
positions of polygons are the x1 positions of the next character

Here is a snapshoot with your name, it's doesn't seems to be wrong, but
maybe it's because of the used font (Verdana ?).
http://www.hiboox.fr/go/images/informatique/spaceproblem,536d93528638ed63c070aaed88f63139.png.html

> 
>>       Text tmpText = docCalcul.createTextNode(string);
>>       retourLigne.appendChild(tmpText);
>>       docCalcul.getDocumentElement().appendChild(retourLigne);
>>       retour += retourLigne.getEndPositionOfChar(0).getX();
> 
>    See I would expect a loop here for the length of "string"
> asking for the end position of each character in the string.
> 
>> It's worked well for all character but the space width isn't exact.
>> Can you tell me why ? 
> 
>    Actually the error looks to start earlier in the string than
> the space (note the first 'e' isn't aligned with the red bar like
> it normally would be).

because of integer, if it can be useful for someone else.
-- 
View this message in context: http://www.nabble.com/Re%3A-Define-String-Width-with-bounding-box-tp23739342p23740555.html
Sent from the Batik - Users mailing list archive at Nabble.com.


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


Re: Define String Width with bounding box

Posted by th...@kodak.com.
Hi Thomy,

Thomy <th...@gmail.com> wrote on 05/26/2009 02:11:45 PM:

> >    If you want to know actual text content positions I would suggest
> > that the text content SVG DOM Api's would be a better choice than
> > getBBox:
> > 
> >         http://www.w3.org/TR/SVG/text.html#DOMInterfaces
> > 
> >   Take a look at 'samples/tests/spec/scripting/text_content.svg'
> > in the distribution for an example of using these API's. 
> > In particular you can call 'getEndPositionOfChar(index)' for the 
> > trailing space to get the location of the end of the space. 

> I tried this solution and it's more accurate, but it's not the 
"ultimate"
> solution for my space problem.
> In fact, i compute height and width of each character in a String with 
the
> following code :

   This code looks a little weird.  It looks to me like you are
calculating the width of each character in isolation.  This is
a bad idea because Batik implements kerning which can adjust the
space between characters on a pairwise basis.  The example for
pairwise kerning I always use is my last name: DeWeese.  With a 
lot of fonts the two 'e' glyphs will be kerned in under the 
"arms" of the capital 'W', if you don't do that the W can 
look disconnected, but the default advance can't be shorter
otherwise tall letters (l, h, t, almost all Caps) would collide
with the top of the 'W'.

>       Text tmpText = docCalcul.createTextNode(string);
>       retourLigne.appendChild(tmpText);
>       docCalcul.getDocumentElement().appendChild(retourLigne);
>       retour += retourLigne.getEndPositionOfChar(0).getX();

   See I would expect a loop here for the length of "string"
asking for the end position of each character in the string.

> It's worked well for all character but the space width isn't exact.
> Can you tell me why ? 

   Actually the error looks to start earlier in the string than
the space (note the first 'e' isn't aligned with the red bar like
it normally would be).

> I need an exact size for my compute.

   Then you will either need to exactly match Batik's layout
algorithm (see batik.gvt.text.doExplicitGlyphLayout), or
use the text content API for each string.

> Here is a snapshot of the result :
> Each red rectangle is the bounding rectangle of a character.
> http://www.hiboox.fr/go/images/informatique/spaceproblem,
> 8595d6a7be900c2c0c63600b7dc0678e.png.html
> 
> You can easily see the problem on the first space.
> The more space i have in a string, the biggest the error is !