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:[EMAIL PROTECTED] Sent: den 6 mars 2007 13:36 To: [email protected] 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: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: den 6 mars 2007 12:36 To: [email protected] Cc: [email protected] Subject: Re: Getting the maximum height of a character Hi Henric, "Henric Rosvall" <[EMAIL PROTECTED]> 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: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] --------------------------------------------------------------------- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED]
