character widths in JTextPane

Hello everybody

I write a WYSIWYG Report-Editor. My Problem is that the character-widths in the Editor is different to the Output (rtf,pdf).

For Exaple in the Editor i can write 45 w's (Arial,plain,11) in one Line an in the Output is was 57. With Times New Roman i can write 64 ant in the Output is possible to set only 57 in one line.

So i have write an own BoxView for the TextPane and set int the paint method "g2D.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON)" The result is that the characterwitds are identic to the output but the caretpositions in the editor are wrong.

Then i try to set putProperty("i18n", Boolean.TRUE) in the document. The result is that the characterwidts are equals the the version without fractionalmetrics->ON.

What can i do?

Thanks Thomas

[866 byte] By [staubta] at [2007-11-27 10:40:06]
# 1

Getting and setting the caret position relies on the viewToModel() and modelToView() methods, which use a FontMetrics object that's derived from the Graphics object. From what I can see, you have to make sure the Graphics object has the rendering hint set before the FontMetrics object is created. It doesn't look it's going to be easy. :-/

uncle_alicea at 2007-7-28 19:04:54 > top of Java-index,Desktop,Core GUI APIs...
# 2

Never mind: my original reply finally showed up.

Message was edited by:

uncle_alice

uncle_alicea at 2007-7-28 19:04:55 > top of Java-index,Desktop,Core GUI APIs...
# 3

Thank You for the tip

I overwrites the getFontMetrics Method in the JTextPane

@Override

public FontMetrics getFontMetrics(Font font) {

Graphics graphics = getGraphics();

Graphics2D g2d = (Graphics2D) graphics;

if(g2d == null){

return super.getFontMetrics(font);

}

g2d.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,RenderingHints.VALUE_FRACTIONALMETRICS_ON);

return g2d.getFontMetrics(font);

}

It works!

staubta at 2007-7-28 19:04:55 > top of Java-index,Desktop,Core GUI APIs...
# 4

Nice one!

uncle_alicea at 2007-7-28 19:04:55 > top of Java-index,Desktop,Core GUI APIs...