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 Henric Rosvall <he...@opv.se> on 2007/03/06 12:18:47 UTC

Getting the maximum height of a character

I'm trying to implement multiline text just using a text-element and
some tspans. But in order to do this, I need to know the maximum height
that the characters of a line can occupy (so I know at what position to
insert the next line).
There must be some better way than the one below, that doesn't even work
since getExtentOfChar throws an exception. Do any of you have any hints
on how to do this? Cause I'm really ashamed of the ugly code below.
Belongs more to thedailywtf.com than in my software...
 
The only way I could think of to accurately calculate this, seems like a
really ugly hack - but as I said, it's the only way I can come up with
since different fonts of the same size has radically different heights:
 
Basically I thought I'd set the value of the Text-element to "Og" and
then measure the combined height of these characters:
 
   // Get the maximum rowHeight
   SVGTextElement svgTxt = (SVGTextElement)TextElement;
   String strToMeasure = "Og";
   svgTxt.setNodeValue(strToMeasure);
   float minY = Integer.MAX_VALUE;
   float maxY = Integer.MIN_VALUE;
   for (int x = 0; x < strToMeasure.length(); x++)
   {
    SVGRect r = svgTxt.getExtentOfChar(x);
    
    if (r.getY() < minY)
     minY = r.getY();
    if (r.getY() + r.getHeight() > maxY)
     maxY = r.getY() + r.getHeight();
   }
   svgTxt.setNodeValue("");
   
   float rowOffset = maxY - minY;
 
But the getExtentOfChar-method throws an exception:
org.w3c.dom.DOMException: 
 at org.apache.batik.dom.AbstractNode.createDOMException(Unknown Source)
 at
org.apache.batik.dom.svg.SVGTextContentSupport.getExtentOfChar(Unknown
Source)
 at
org.apache.batik.dom.svg.SVGOMTextContentElement.getExtentOfChar(Unknown
Source)
 at
printit.template.controls.MultilineTextTemplateControl.setSVGTextElement
Text(MultilineTextTemplateControl.java:86)
 at
printit.template.controls.MultilineTextTemplateControl.setText(Multiline
TextTemplateControl.java:54)
 at
printit.template.controlpanels.MultilineTextControlPanel$1.run(Multiline
TextControlPanel.java:94)
 at org.apache.batik.util.RunnableQueue.run(Unknown Source)
 at java.lang.Thread.run(Unknown Source)
 
So, if there is a better way, please tell me. Even a simple
unit-converter might be bettereven though the distance between the lines
might not be that accurate. Otherwise, if there really isn't a better
way, do you have any idea what might be wrong? Is it some
rendering-issue, like, the string hasn't been rendered, and can
therefore not be measured?
 
Regards
Ehhh.... Definately not Henric Rosvall. He's too leet to write code like
this :P

RE: Getting the maximum height of a character

Posted by Henric Rosvall <he...@opv.se>.
No need to reply. It was the fact that I hadn't added it to the Canvas
that made it impossible to measure. Now it seems to work like it should.

-----Original Message-----
From: Henric Rosvall [mailto:henric@opv.se] 
Sent: den 6 mars 2007 13:36
To: batik-users@xmlgraphics.apache.org
Subject: RE: Getting the maximum height of a character


That solved it. Now I want to get the length of the string inside a
TSpan-object, but neither getComputedTextLength() or
getEndPositionOfChar(currLineText.length()-1).getX() works. I get a
NullPointerException on both dispite the TSpan obviously having a
textnode:

String currTextLine = "Retrieve my width";
Text tn =
SvgEditor.getCanvas().getSVGDocument().createTextNode(currTextLine);
currTSpan.appendChild((Node)tn);
float length = currTSpan.getComputedTextLength();	//
getEndPositionOfChar(currTextLine.length()-1).getX();  also fails

The exception:

java.lang.NullPointerException
	at
org.apache.batik.dom.svg.SVGTextContentSupport.getNumberOfChars(Unknown
Source)
	at
org.apache.batik.dom.svg.SVGTextContentSupport.getEndPositionOfChar(Unkn
own Source)
	at
org.apache.batik.dom.svg.SVGOMTextContentElement.getEndPositionOfChar(Un
known Source)
	at
printit.template.controls.MultilineTextTemplateControl.setSVGTextElement
Text(MultilineTextTemplateControl.java:141)
	at
printit.template.controls.MultilineTextTemplateControl.setText(Multiline
TextTemplateControl.java:56)
	at
printit.template.controlpanels.MultilineTextControlPanel$1.run(Multiline
TextControlPanel.java:94)
	at org.apache.batik.util.RunnableQueue.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

The TSpan isn't "attached" to the canvas yet. Can that be the cause of
the error?


-----Original Message-----
From: thomas.deweese@kodak.com [mailto:thomas.deweese@kodak.com] 
Sent: den 6 mars 2007 12:36
To: batik-users@xmlgraphics.apache.org
Cc: batik-users@xmlgraphics.apache.org
Subject: Re: Getting the maximum height of a character


Hi Henric,

"Henric Rosvall" <he...@opv.se> wrote on 03/06/2007 06:18:47 AM:

> I'm trying to implement multiline text just using a text-element and
> some tspans. But in order to do this, I need to know the maximum 
> height that the characters of a line can occupy (so I know at what 
> position to insert the next line).

> Basically I thought I'd set the value of the Text-element to "Og" and 
> then measure the combined height of these characters:

  The code below doesn't set the value of the TextElement.
You need to create/fetch a DOM TextNode with the desired string.

>    // Get the maximum rowHeight
>    SVGTextElement svgTxt = (SVGTextElement)TextElement;
     TextNode tn = doc.createTextNode("Og");
     svgTxt.appendChild(tn);

>    float minY = Integer.MAX_VALUE;
>    float maxY = Integer.MIN_VALUE;
>    for (int x = 0; x < strToMeasure.length(); x++)
>    {
>     SVGRect r = svgTxt.getExtentOfChar(x);
> 
>     if (r.getY() < minY)
>      minY = r.getY();
>     if (r.getY() + r.getHeight() > maxY)
>      maxY = r.getY() + r.getHeight();
>    }
>    svgTxt.setNodeValue("");
> 
>    float rowOffset = maxY - minY;
> 
> But the getExtentOfChar-method throws an exception:
> org.w3c.dom.DOMException:
>  at org.apache.batik.dom.AbstractNode.createDOMException(Unknown
Source)
>  at org.apache.batik.dom.svg.SVGTextContentSupport.
> getExtentOfChar(Unknown Source)
>  at org.apache.batik.dom.svg.SVGOMTextContentElement.
> getExtentOfChar(Unknown Source)
>  at printit.template.controls.MultilineTextTemplateControl.
> setSVGTextElementText(MultilineTextTemplateControl.java:86)
>  at printit.template.controls.MultilineTextTemplateControl.
> setText(MultilineTextTemplateControl.java:54)
>  at printit.template.controlpanels.MultilineTextControlPanel$1.
> run(MultilineTextControlPanel.java:94)
>  at org.apache.batik.util.RunnableQueue.run(Unknown Source)  at 
> java.lang.Thread.run(Unknown Source)
> 
> So, if there is a better way, please tell me. Even a simple unit-
> converter might be bettereven though the distance between the lines 
> might not be that accurate. Otherwise, if there really isn't a better 
> way, do you have any idea what might be wrong? Is it some 
> rendering-issue, like, the string hasn't been rendered, and can 
> therefore not be measured?
> 
> Regards
> Ehhh.... Definately not Henric Rosvall. He's too leet to write code 
> like this :P

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




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




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


RE: Getting the maximum height of a character

Posted by Henric Rosvall <he...@opv.se>.
That solved it. Now I want to get the length of the string inside a
TSpan-object, but neither getComputedTextLength() or
getEndPositionOfChar(currLineText.length()-1).getX() works. I get a
NullPointerException on both dispite the TSpan obviously having a
textnode:

String currTextLine = "Retrieve my width";
Text tn =
SvgEditor.getCanvas().getSVGDocument().createTextNode(currTextLine);
currTSpan.appendChild((Node)tn);
float length = currTSpan.getComputedTextLength();	//
getEndPositionOfChar(currTextLine.length()-1).getX();  also fails

The exception:

java.lang.NullPointerException
	at
org.apache.batik.dom.svg.SVGTextContentSupport.getNumberOfChars(Unknown
Source)
	at
org.apache.batik.dom.svg.SVGTextContentSupport.getEndPositionOfChar(Unkn
own Source)
	at
org.apache.batik.dom.svg.SVGOMTextContentElement.getEndPositionOfChar(Un
known Source)
	at
printit.template.controls.MultilineTextTemplateControl.setSVGTextElement
Text(MultilineTextTemplateControl.java:141)
	at
printit.template.controls.MultilineTextTemplateControl.setText(Multiline
TextTemplateControl.java:56)
	at
printit.template.controlpanels.MultilineTextControlPanel$1.run(Multiline
TextControlPanel.java:94)
	at org.apache.batik.util.RunnableQueue.run(Unknown Source)
	at java.lang.Thread.run(Unknown Source)

The TSpan isn't "attached" to the canvas yet. Can that be the cause of
the error?


-----Original Message-----
From: thomas.deweese@kodak.com [mailto:thomas.deweese@kodak.com] 
Sent: den 6 mars 2007 12:36
To: batik-users@xmlgraphics.apache.org
Cc: batik-users@xmlgraphics.apache.org
Subject: Re: Getting the maximum height of a character


Hi Henric,

"Henric Rosvall" <he...@opv.se> wrote on 03/06/2007 06:18:47 AM:

> I'm trying to implement multiline text just using a text-element and 
> some tspans. But in order to do this, I need to know the maximum 
> height that the characters of a line can occupy (so I know at what 
> position to insert the next line).

> Basically I thought I'd set the value of the Text-element to "Og"
> and then measure the combined height of these characters:

  The code below doesn't set the value of the TextElement.
You need to create/fetch a DOM TextNode with the desired string.

>    // Get the maximum rowHeight
>    SVGTextElement svgTxt = (SVGTextElement)TextElement;
     TextNode tn = doc.createTextNode("Og");
     svgTxt.appendChild(tn);

>    float minY = Integer.MAX_VALUE;
>    float maxY = Integer.MIN_VALUE;
>    for (int x = 0; x < strToMeasure.length(); x++)
>    {
>     SVGRect r = svgTxt.getExtentOfChar(x);
> 
>     if (r.getY() < minY)
>      minY = r.getY();
>     if (r.getY() + r.getHeight() > maxY)
>      maxY = r.getY() + r.getHeight();
>    }
>    svgTxt.setNodeValue("");
> 
>    float rowOffset = maxY - minY;
> 
> But the getExtentOfChar-method throws an exception:
> org.w3c.dom.DOMException:
>  at org.apache.batik.dom.AbstractNode.createDOMException(Unknown
Source)
>  at org.apache.batik.dom.svg.SVGTextContentSupport.
> getExtentOfChar(Unknown Source)
>  at org.apache.batik.dom.svg.SVGOMTextContentElement.
> getExtentOfChar(Unknown Source)
>  at printit.template.controls.MultilineTextTemplateControl.
> setSVGTextElementText(MultilineTextTemplateControl.java:86)
>  at printit.template.controls.MultilineTextTemplateControl.
> setText(MultilineTextTemplateControl.java:54)
>  at printit.template.controlpanels.MultilineTextControlPanel$1.
> run(MultilineTextControlPanel.java:94)
>  at org.apache.batik.util.RunnableQueue.run(Unknown Source)
>  at java.lang.Thread.run(Unknown Source)
> 
> So, if there is a better way, please tell me. Even a simple unit- 
> converter might be bettereven though the distance between the lines 
> might not be that accurate. Otherwise, if there really isn't a better 
> way, do you have any idea what might be wrong? Is it some 
> rendering-issue, like, the string hasn't been rendered, and can 
> therefore not be measured?
> 
> Regards
> Ehhh.... Definately not Henric Rosvall. He's too leet to write code
> like this :P

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




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


Re: Getting the maximum height of a character

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

"Henric Rosvall" <he...@opv.se> wrote on 03/06/2007 06:18:47 AM:

> I'm trying to implement multiline text just using a text-element and
> some tspans. But in order to do this, I need to know the maximum 
> height that the characters of a line can occupy (so I know at what 
> position to insert the next line).

> Basically I thought I'd set the value of the Text-element to "Og" 
> and then measure the combined height of these characters:

  The code below doesn't set the value of the TextElement.
You need to create/fetch a DOM TextNode with the desired
string.

>    // Get the maximum rowHeight
>    SVGTextElement svgTxt = (SVGTextElement)TextElement;
     TextNode tn = doc.createTextNode("Og");
     svgTxt.appendChild(tn);

>    float minY = Integer.MAX_VALUE;
>    float maxY = Integer.MIN_VALUE;
>    for (int x = 0; x < strToMeasure.length(); x++)
>    {
>     SVGRect r = svgTxt.getExtentOfChar(x);
> 
>     if (r.getY() < minY)
>      minY = r.getY();
>     if (r.getY() + r.getHeight() > maxY)
>      maxY = r.getY() + r.getHeight();
>    }
>    svgTxt.setNodeValue("");
> 
>    float rowOffset = maxY - minY;
> 
> But the getExtentOfChar-method throws an exception:
> org.w3c.dom.DOMException: 
>  at org.apache.batik.dom.AbstractNode.createDOMException(Unknown Source)
>  at org.apache.batik.dom.svg.SVGTextContentSupport.
> getExtentOfChar(Unknown Source)
>  at org.apache.batik.dom.svg.SVGOMTextContentElement.
> getExtentOfChar(Unknown Source)
>  at printit.template.controls.MultilineTextTemplateControl.
> setSVGTextElementText(MultilineTextTemplateControl.java:86)
>  at printit.template.controls.MultilineTextTemplateControl.
> setText(MultilineTextTemplateControl.java:54)
>  at printit.template.controlpanels.MultilineTextControlPanel$1.
> run(MultilineTextControlPanel.java:94)
>  at org.apache.batik.util.RunnableQueue.run(Unknown Source)
>  at java.lang.Thread.run(Unknown Source)
> 
> So, if there is a better way, please tell me. Even a simple unit-
> converter might be bettereven though the distance between the lines 
> might not be that accurate. Otherwise, if there really isn't a 
> better way, do you have any idea what might be wrong? Is it some 
> rendering-issue, like, the string hasn't been rendered, and can 
> therefore not be measured?
> 
> Regards
> Ehhh.... Definately not Henric Rosvall. He's too leet to write code 
> like this :P

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