Rendering Devanagari ligatures (Unicode) in Java Swing JComponent on Mac OS X

╄→尐↘猪︶ㄣ 提交于 2019-12-17 19:09:06

问题


I'm trying to get Devanagari ligatures (in Unicode strings) rendered correctly on Mac OS X 10.6.

The strings are drawn on a JComponent and take RenderingHints for Antialiasing. The ligatures are displayed correctly in Windows XP SP2 and 7, and Ubuntu, but in Mac OS X, the ligatures are decomposed (or rather, not merged correctly), diacritics are moved away from their positions, etc. (cf. screenshots below, correct rendering example from Win XP SP2 on the left (with RenderingHints Antialiasing Key ON), wrong rendering example from Mac OS X 10.6.7 on the right (Antialising DEFAULT = OFF).

I have set the font as follows, so it should use a default font on any system:

new Font(null,Font.PLAIN,20);

I believe all this might have something to do with the fact that the default character encoding on Macs is MacRoman (NOT a UTF-8 subset), and that other systems (like Windows) use a UTF-8 subset (such as WinLatin-1) or cp1252 or such.

Even with this information at hand, I'm in the dark about how to handle this problem. Thus I would be very grateful if someone was able to point me in the right direction.

I've tried a number of things already:

  • Setting the font to Devanagari MT didn't solve the issue
  • TextAttribute LIGATURES_ON didn't solve the problem

I'd be extremely thankful for any hints, or code snippets by other developers (preferrably with a Hindi background who develop on Mac).


回答1:


Using the Quartz renderer instead of the Java 2D renderer.

This make a substantial difference in the quality of glyph rendering. It needs to be done first, as suggested here.

if (System.getProperty("os.name").startsWith("Mac OS X")) {
    System.setProperty("apple.awt.graphics.UseQuartz", "true");
}

You might also look at using TextLayout, as the FontRenderContext can apply RenderingHints.KEY_FRACTIONALMETRICS.




回答2:


I'm not an expert myself, but here some pointers. As far as I understood from Wikipedia, the problem is very likely to be your font. Apologies in advance for the long quotes, but else I could have just linked the 2 Wikipedia articles.

Here a part of the section about ligatures from the Unicode article:

Ligatures

Many scripts, including Arabic and Devanagari, have special orthographic rules that require certain combinations of letterforms to be combined into special ligature forms. The rules governing ligature formation can be quite complex, requiring special script-shaping technologies such as ACE (Arabic Calligraphic Engine by DecoType in the 1980s and used to generate all the Arabic examples in the printed editions of the Unicode Standard), which became the proof of concept for OpenType (by Adobe and Microsoft), Graphite (by SIL International), or AAT (by Apple).

Reading further in the AAT (Apple Advanced Typography) article, reveals following information. I recommend reading the whole article.

AAT and OpenType in Mac OS X

As of Mac OS X 10.5 Leopard, partial support for OpenType is available. The support is currently limited to Western scripts and Arabic (as of 2011). If a font has AAT tables, they will be used for typography. If the font does not have AAT tables but does have OpenType tables, they will be used to the extent that the system supports them.

This means that many OpenType fonts for Western or Middle Eastern scripts can be used without modification on Mac OS X 10.5, but South Asian scripts such as Thai and Devanagari cannot. These require AAT tables for proper layout.

And later down in the section about font layout:

Since AAT operates entirely with glyphs and never with characters, all the layout information necessary for producing the proper display resides within the font itself. This allows fonts to be added for new scripts without requiring any specific support from the OS.

And last:

AAT for Indic scripts

For Indic scripts, the only features that are necessary are glyph re-ordering and substitution. AAT supports both of these. As noted above, OpenType fonts for Indic scripts require AAT tables to be added before they will function properly on Mac OS X. Note, however, that this applies only to software dependent upon the system support of OpenType. Programs which provide their own implementation of OpenType will render Indic properly with OpenType fonts. (They may, however, not render Indic fonts with AAT tables correctly.)

Mac OS X 10.5 ships with fonts for Devanagari, Gurmukhi, Gujarati, Thai, Tibetan, and Tamil. Fonts for other Indic scripts are available from third parties.

Maybe you need to choose a font that explicitly supports Devanagari.




回答3:


I'm having this exact same problem with a project I am working on with Gurmukhi script. I've now tried both fonts with AAT tables that come bundles with Mac OS X (Gurmukhi MT, Gurmukhi MN) and fonts with OpenType tables. Neither works in JAVA on Mac OS X, but the OpenType fonts are slightly more readable. The only problem is that the "halant" character doesn't render the half forms of characters like it should.

I think the problem is with the fonts we are using and their compatibility with JAVA on Mac OS X.




回答4:


I faced the same issue when I tried http://jambula.sourceforge.net on Mac for Tamil language.

The workaround I came up with was to use Font.createFont to load an explicit font.

You can see a Test program at: TestRendering.java

java org.jambula.image.TestRendering text.png

renders the Tamil text incorrectly in text.png

However

java org.jambula.image.TestRendering text.png "/Library/Fonts/Arial Unicode.ttf"

renders it correctly.

It might be due to https://bugs.openjdk.java.net/browse/JDK-7162125.



来源:https://stackoverflow.com/questions/5994815/rendering-devanagari-ligatures-unicode-in-java-swing-jcomponent-on-mac-os-x

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!