String width via fontmetrics calculation is very slow if there are arabic or persian letters in text?

I performed some tests with other languages using your code. First you are right: calculations of Persian string took a lot of time.

I performed some tests with other languages using your code. First you are right: calculations of Persian string took a lot of time. I played with font type and size and did not see significant differences.

But the result definitely depend on the script you are using. Here are the results I got on my machine. Calculation time for persian : 2877 ms Calculation time for english : 8 ms Calculation time for russian : 47 ms Calculation time for hebrew : 16815 ms As you can see Russian is 6 times slower than English.

I believe that it is because the internal representation of strings is unicode. In UTF-8 English characters occupy one byte, all others 2 bytes. I am not sure it can satisfy you :) but Hebrew test is 4 times slower than Persian.

Both are slow, so I guess that right-to-left calculations kill it. It seems that we have nothing to do with this.

But minimum 500 times slower kills performance :( – user527759 Dec 2 '10 at 10:17 Agree only if you really have to calculate thousands metrics. If you have 10 lines on screen it does not matter: drawing of UI controls also takes time. BTW if you find solution post it here.

I'd love to know... – AlexR Dec 2 '10 at 10:26 I think that the solution will be in rewriting and further replacing of sun.font. FontDesignMetrics class in some way. Maybe adding some cache.It looks that it will be not so easy.

– user527759 Dec 2 '10 at 12:48.

Could you try to use Font class' method. Public GlyphVector layoutGlyphVector(FontRenderContext frc, char text, int start, int limit, int flags) ANd use the GlyphVector to measure your string? Or TextLayout public TextLayout(String string, Font font, FontRenderContext frc).

The problem is that standard swing components also call sun.font. FontDesignMetrics.stringWidth() method. And standard swing components become slow if there are few thousands of non english strings.

– user527759 Dec 2 '10 at 15:32 Interesting thing. Text layout works slow for both cases. However stringWidth from FontDesignMetrics has some kind of cache for latin characters – user527759 Dec 2 '10 at 18:19 if (ch.

I've dug for a little and found next: From the source of the FontDesignMetrics we can see the main actions sequence public int stringWidth(String str) { float width = 0; if (font. HasLayoutAttributes()) { /* TextLayout throws IAE for null, so throw NPE explicitly */ if (str == null) { throw new NullPointerException("str is null"); } if (str.length() == 0) { return 0; } width = new TextLayout(str, font, frc).getAdvance(); } else { int length = str.length(); for (int I = 0; I IsNonSimpleChar(ch)) { width = new TextLayout(str, font, frc).getAdvance(); break; } else { width += handleCharWidth(ch); } } } return (int) (0.5 + width); } For latin characters method getLatinCharWidth(ch) is used. It caches all the characters widths.

But for persian and arabic characters TextLayout is used instead of. The main purpose is because eastern characters may have varios shape and width depend on context. It is possible to add method which will cache characters width but it will not give exact values such as it will ignore nuances of different characters widths.

Also it will ignore various ligatures. I've tested TextLayout separately and it is slow for both languages english and persian. So the real cause of the slow performance is the slow work of the sun.font.

TextLayout class. It is used to determine string width in case characters in the string are not simple. Unfortunately I don't know how to boost TextLayout performance for a now.

If somebody is interested here the article about various font and text layout nuances is http://download.oracle.com/javase/1.4.2/docs/guide/2d/spec/j2d-fonts.html.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions