22

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.

correct and incorrect rendering of the same string

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).

Community
  • 1
  • 1
s.d
  • 4,017
  • 5
  • 35
  • 65
  • 3
    i don't know about this stuff at all, but looks very interesting and i would like to learn more about it. can you please post some links? Advance मध्ये धन्यवाद! – Bhushan May 13 '11 at 16:27
  • 1
    I think you hunch is wrong, but I can’t tell you what the right answer is. [CP1252 is no more a UTF-8 subset than is MacRoman; Latin1 is a UTF-8 subset, though. This very probably has nothing at all to do with Java’s notoriously dumb old broken default 8-bit encoding issue.] I would really like to learn it, though. – tchrist May 13 '11 at 22:44
  • @Bhushan: Thanks for your interest. I'm afraid I haven't got any helpful links to share. Yet. Once I find something (or get some help here), I'll let everyone know. @tchrist: Of course you're right about cp1252! I'm not a char encoding expert, though I'm afraid I'll have to be one before soon. Here's the info I've found about character encoding on Mac v Win: [Java Devel Guide for OS X](http://developer.apple.com/library/mac/#documentation/Java/Conceptual/Java14Development/04-JavaUIToolkits/JavaUIToolkits.html#//apple_ref/doc/uid/TP40001901-SW1). Thanks for your interest & correction! – s.d May 13 '11 at 23:17
  • @tchrist again: I've edited the post to more obviously reflect the difference between Latin1 & cp1252. – s.d May 13 '11 at 23:25
  • 1
    Does this text render correctly in non-Java Mac programs? For example, if you paste it in TextEdit.app, does it look right? (Unfortunately, I know nothing about Devanagari, so I can't easily figure out the characters from your screenshot.) – Ken May 14 '11 at 04:47
  • @Ken: I thought it might be a problem with the OS X rendering engine myself, but the layout engine of OS X can definitely render Devanagari text including ligatures correctly. I've seen applications based on the Cocoa API do it. – s.d May 14 '11 at 15:37
  • Which font family is it actually using on your test platform? (It's not a general encoding issue; fonts include information about what encoding they're using, which Java correctly understands. Plus Macs have been generally UTF-8 at the system level for years.) – Donal Fellows May 15 '11 at 10:41
  • Also, which version of Java are you actually using when you run the code? _My_ OSX 10.5 system has 1.4, 1.5 and 1.6 installed, and I could well imagine older versions having bugs in ligature handling that have been subsequently fixed. – Donal Fellows May 15 '11 at 10:44
  • @Donal Fellows: I need to find out about the font family "remotely" since I don't own a Mac yet. This might take a wee while since I need to send JARs to other folk to test it. Thanks for your interesting remarks about font encoding as well. Re Java version: On the Mac that provided the screenshot, it was java version 1.6.0_24 (Java(TM) SE Runtime Environment (build 1.6.0_24-b07-334-10M3326); Java HotSpot(TM) 64-Bit Server VM (build 19.1-b02-334, mixed mode)). Will be back with the additional info soon. Thanks all for your support for the moment. This is such a great platform! – s.d May 15 '11 at 18:35
  • @Donal Fellows: Since you didn't ask for Mac specifically, I can tell you what Font family and Java version it's using on Win7 & Ubuntu, which is `Dialog` (and specifically, `Dialog.PLAIN`) & Sun Java 1.6.0_24 (OpenJDK 6 JRE on Ubuntu respectively). That's just for the meantime. Waiting on replies from the owner of the test Mac. – s.d May 16 '11 at 20:41
  • @Donal Fellows & All: Here is the Font Family info: Java Version: 1.6.0_24; Java Vendor: Apple Inc.; Default Charset: MacRoman; Default Font: Dialog; Font Family: Dialog. I hope this information will help in suggesting a solution. – s.d May 17 '11 at 11:23
  • @Bhushan and @tchrist: Cf. my update for how to do it :). – s.d Jun 22 '11 at 13:51

4 Answers4

6

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.

Community
  • 1
  • 1
MicSim
  • 26,265
  • 16
  • 90
  • 133
  • Thank you very much for your great research! I thought I had read everything that is to be read on Wikipedia (could be I was stuck with the German pages though). Will follow up on this here once I've tried it. Might take a couple of days though. – s.d May 17 '11 at 15:49
  • Update: I've found out that the only font that comes with Mac OS X that has an AAT table and includes Devanagari glyphs is, surprisingly, Devanagari MT. I will try to set that Font and see what happens. Once my MacBook is delivered. Thanks for all your help! – s.d May 20 '11 at 17:02
  • I've just tested it with Devanagari MT (the font that ships with Mac OS X 10.6), and it doesn't work. I'll start to rip my hair out just now then. Must be a Java rather than a Mac OS X problem after all (or a combination of the two). Thanks for your help, though, much appreciated! – s.d Jun 15 '11 at 12:38
  • I'm sorry that it didn't work. Good luck then! Seems, you need it ... :-) – MicSim Jun 15 '11 at 14:26
  • Thanks a lot. I've one last clue (using the Quartz renderer instead of the Java 2D renderer), and then I'll just hire someone from India who actually uses Devanagari to solve this for me. – s.d Jun 15 '11 at 14:32
3

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.

trashgod
  • 203,806
  • 29
  • 246
  • 1,045
  • Although you started your answer with a quote from my own comment (in which I principally answered my own question, haha), this is still the best answer as it was supplied before I could comment that I have solved the problem, and additionally you added value by supplying a piece of code to query for OS, as well as hinting at TextLayout (although it's a bit out of context for my sepcific issue), hence I will award the bounty to you. Thanks! – s.d Jun 22 '11 at 13:44
  • Glad it helped. Indeed, without the query, the setting hangs on Windows. – trashgod Jun 22 '11 at 20:52
1

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.

Kulpreet
  • 160
  • 2
  • 14
  • Try setting the `System` property for the Quartz renderer to `"true"`, as suggested by myself, trashgod and updated in my original [question](http://stackoverflow.com/q/5994815/731040). I'd be interested in hearing how this works for you. – s.d Jun 22 '11 at 13:50
  • Have you found any solution then dude? –  May 01 '14 at 18:03
  • Have you tried using `Font.PLAIN` or were you only trying to get it to work with concrete fonts? Would http://stackoverflow.com/a/36069987/731040 help you at all? – s.d Mar 18 '16 at 12:48
0

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.

  • Okay, so it seems this is another complication when you want to work with concrete fonts. However, I didn't even try to do that, just using `Font.PLAIN`. While your answer isn't a direct answer to my question, I think it'll be valuable for others, so thanks! – s.d Mar 18 '16 at 12:46