Currently i'm developing a new Look and Feel and i had to start from scratch for many reasons , my biggest problem now is getting the width and height of the String in components that have getText()
to determine where it should be painted , and i know how to get it for normal text but how to do it if it's rendered as html text ??.
using the FontMetrics
really provided every thing needed for plain text but using it for html gives as if it's plain , so i'v tried View.getGraphics()
and do the same thing on it with FontMetrics
of the view object graphics but still the same while what i want is the Dimension of the text that is painted (the differences that matters like if it contains <br />
the height will not be accurate ! or if the font is changed in html tags ,etc...) .
public Dimension getTextSize(JComponent c, Graphics g, String text) {
//this works if you want to get the size as if it's plain text
//and later in the drawing calculate the descent line distance to draw the text
g.setFont(c.getFont());
FontMetrics fontMetrics = g.getFontMetrics();
Dimension plaintTextSize = new Dimension(fontMetrics.stringWidth(text),fontMetrics.getHeight());
return plaintTextSize;
}
i have tried the above with
View view = (View) JComponent.getClientProperty(BasicHTML.propertyKey);
and pass on the view.getGraphics()
to the getTextSize
method but still the same .I'v searched a lot about it but i couldn't found any relative results and i got nothing about what class to use and i can't use TextLayout
since some times the JComponent
might not be a JTextComponent
.
please help and thank you.
Edit
it seems like it's a real semi-impossible thing to do so as another solution am gonna settle with View.getPreferredSpan(int axsis)
to get the size but some time's it's not reliable to scale the size , i mean like in component's we can predict this conflict but how to predict this in html string painted BasicHTML
view to at least handle the size manually .
sadly searching the whole web with no any satisfying answer for the real question
how to get the size of String when it's rendered as HTML?
but the other question after the edit of how to get differences between the real size and the preferred size of the view using
View.getPreferredSpan(int axsis);
that the return size will give the size of the typed content not the drawn one's like if the text was
<html>
<body>
first line<br />
second line <hr />
third line
</body>
</html>
it will use the size of the
View.getPreferredSpan(int axsis);
to get the text size of the first ,second and third line sentences but it will use the Rectangle
parameter to determine the end of that <hr />
line .