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