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 Thomas Palfray <th...@gmail.com> on 2008/10/29 15:59:07 UTC

Define String Width with bounding box

Hi all,

I'm trying to find the width of a String, but i've a little problem.
in fact, i use getBBox() to find it, and it works ... but not in one case.

I have two test String :

"blabla bla"
"blabla bla "
getBBox() gives me the same length for the two String. It seems that it 
doesn't take the last blankspace.

Here is my method :
    private int getSVGStringWidth(String string, int fontSize){
        Element retourLigne = (Element)docCalcul.createElementNS(svgNS, 
"text");
        retourLigne.setAttributeNS(null,"x", "0");
        retourLigne.setAttributeNS(null,"y", "0");
        retourLigne.setAttributeNS(null,"font-family", "Verdana");
        
retourLigne.setAttributeNS(null,"font-size",String.valueOf(fontSize));
        Text tmpText = docCalcul.createTextNode(string);
        retourLigne.appendChild(tmpText);
        docCalcul.getDocumentElement().appendChild(retourLigne);
        int retour = (int)((SVGLocatable)retourLigne).getBBox().getWidth();
        docCalcul.getDocumentElement().removeChild(retourLigne);

        return retour;           
    }
.
If someone have some advice, it could help me :)
Cheers,
Thomas

---------------------------------------------------------------------
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 Thomas Palfray <th...@gmail.com>.
Hi !

Cameron McCormack a écrit :
> Cameron McCormack:
>   
>>> However, it seems that spaces at the start and end of the element don?t
>>> contribute to the bounding box, even if xml:space="preserve".  That
>>> seems to be a bug, so I’ve filed this:
>>>
>>>   https://issues.apache.org/bugzilla/show_bug.cgi?id=46127
>>>       
>
> Thomas DeWeese:
>   
>>    Do you have a section in the spec that says that unrendered
>> text content must contribute to the BBox of a text element?
>> There is an obvious conflict here between the bbox be some
>> sort of 'logical bounds' vs the 'rendered content bounds'.
>> So for example for putting a graphical element next to the
>> text you probably wouldn't want to consider the space.
>>     
>
> Ah I was following the SVG Tiny 1.2 definition, which says that the
> bounding box of a <text> element is the union of the bounding boxes of
> its glyph cells:
>
>   http://dev.w3.org/SVG/profiles/1.2T/publish/coords.html#BoundingBox
>
> The 1.1 definition just says geometry, so probably the current behaviour
> is OK for 1.1.
>
>   
>>    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. 
>>     
>
> Yes that should work. :)
>
>   
I will look.
Thanks for your help !

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 !

Re: Define String Width with bounding box

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

>    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 Cameron McCormack <ca...@mcc.id.au>.
Cameron McCormack:
> > However, it seems that spaces at the start and end of the element don?t
> > contribute to the bounding box, even if xml:space="preserve".  That
> > seems to be a bug, so I’ve filed this:
> > 
> >   https://issues.apache.org/bugzilla/show_bug.cgi?id=46127

Thomas DeWeese:
>    Do you have a section in the spec that says that unrendered
> text content must contribute to the BBox of a text element?
> There is an obvious conflict here between the bbox be some
> sort of 'logical bounds' vs the 'rendered content bounds'.
> So for example for putting a graphical element next to the
> text you probably wouldn't want to consider the space.

Ah I was following the SVG Tiny 1.2 definition, which says that the
bounding box of a <text> element is the union of the bounding boxes of
its glyph cells:

  http://dev.w3.org/SVG/profiles/1.2T/publish/coords.html#BoundingBox

The 1.1 definition just says geometry, so probably the current behaviour
is OK for 1.1.

>    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. 

Yes that should work. :)

-- 
Cameron McCormack ≝ http://mcc.id.au/

---------------------------------------------------------------------
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 Cameron, Thomas,

Thomas Palfray <th...@gmail.com> wrote on 10/30/2008 05:45:15 AM:

> Hi Cameron, thanks for your reply.
> 
> Cameron McCormack a écrit : 

> However, it seems that spaces at the start and end of the element don?t
> contribute to the bounding box, even if xml:space="preserve".  That
> seems to be a bug, so I?ve filed this:
> 
>   https://issues.apache.org/bugzilla/show_bug.cgi?id=46127

   Do you have a section in the spec that says that unrendered
text content must contribute to the BBox of a text element?
There is an obvious conflict here between the bbox be some
sort of 'logical bounds' vs the 'rendered content bounds'.
So for example for putting a graphical element next to the
text you probably wouldn't want to consider the space.

> So it's a bug of Batik ... So bad. I need this feature because i use
> BBox for doing diplomatic text rendering.
> Maybe i can substitute the blankspace caracter with another one 
> which have the same size. Any idea of which caracter could be use for 
that ?

   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. 

Re: Define String Width with bounding box

Posted by Thomas Palfray <th...@gmail.com>.
Hi Cameron, thanks for your reply.

Cameron McCormack a écrit :
> Hi Thomas.
>
> Thomas Palfray:
>   
>> I'm trying to find the width of a String, but i've a little problem.
>> in fact, i use getBBox() to find it, and it works ... but not in one case.
>>
>> I have two test String :
>>
>> "blabla bla"
>> "blabla bla "
>> getBBox() gives me the same length for the two String. It seems that it  
>> doesn't take the last blankspace.
>>     
>
> If you want it to consider all spaces in the <text> element literally,
> then you should set xml:space="preserve" on the element:
>
>   tmpText.setAttributeNS
>     ("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");
>
> However, it seems that spaces at the start and end of the element don’t
> contribute to the bounding box, even if xml:space="preserve".  That
> seems to be a bug, so I’ve filed this:
>
>   https://issues.apache.org/bugzilla/show_bug.cgi?id=46127
>
> Replacing it with a non-breaking space (&#x160;) doesn’t seem to help
> either, unfortunately.
>   
So it's a bug of Batik ... So bad. I need this feature because i use 
BBox for doing diplomatic text rendering.
Maybe i can substitute the blankspace caracter with another one which 
have the same size. Any idea of which caracter could be use for that ?



Re: Define String Width with bounding box

Posted by Cameron McCormack <ca...@mcc.id.au>.
Hi Thomas.

Thomas Palfray:
> I'm trying to find the width of a String, but i've a little problem.
> in fact, i use getBBox() to find it, and it works ... but not in one case.
>
> I have two test String :
>
> "blabla bla"
> "blabla bla "
> getBBox() gives me the same length for the two String. It seems that it  
> doesn't take the last blankspace.

If you want it to consider all spaces in the <text> element literally,
then you should set xml:space="preserve" on the element:

  tmpText.setAttributeNS
    ("http://www.w3.org/XML/1998/namespace", "xml:space", "preserve");

However, it seems that spaces at the start and end of the element don’t
contribute to the bounding box, even if xml:space="preserve".  That
seems to be a bug, so I’ve filed this:

  https://issues.apache.org/bugzilla/show_bug.cgi?id=46127

Replacing it with a non-breaking space (&#x160;) doesn’t seem to help
either, unfortunately.

-- 
Cameron McCormack ≝ http://mcc.id.au/

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