0

I am creating a Java application in Hindi Language, National Language of India for which Devanagari Script is used.

I need to print some text in Hindi Language using Java Printing API. But, I am facing some problems and can't able to figure out what is wrong!

Here is some screenshots that will state the problem.

When I show the text preview to user in my Java Application (Using Mangal Font):

enter image description here


But When I print the same using Java Printing Support (Using Mangal Font): enter image description here This is the problem. Notice some diacritics (matras) are shifted to the right!


However, MS Word seems to print it correctly using same Mangal Font: enter image description here


And when I use Arial Unicode font in my Java Application, it works fine, too: enter image description here

That's why, I am forced to use Arial Unicode Font! But, it's size is 22MB compared to 200KB size of Mangal Font. This makes my build (jar) very big in size. Application this big in size, cannot be used in production.

I don't even know where to start to know the cause of this problem. The shifted diacritics problem when printing Devnagari Unicode Text


Update: Here is the code:

(Make sure both font files are on classpath! Both fonts are available on Windows named "Arial Unicode MS Regular" & "Mangal".)

import java.awt.BorderLayout;
import java.awt.Font;
import java.awt.FontFormatException;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.print.PageFormat;
import java.awt.print.Printable;
import java.awt.print.PrinterException;
import java.awt.print.PrinterJob;
import java.io.IOException;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;


public class DevnagariTest {

    private static Font createFont(String url) throws FontFormatException, IOException {
        return Font.createFont(
                    Font.TRUETYPE_FONT,
                    DevnagariTest.class.getClassLoader().getResourceAsStream(url))
                .deriveFont(30f);
    }

    private static JLabel getLabel(String text, Font font) {
        JLabel label = new JLabel(text);
        label.setFont(font);
        return label;
    }

    public static void main(String[] args) throws FontFormatException, IOException {
        Font mangal = createFont("mangal.ttf");
        Font arial = createFont("ARIALUNI.TTF");

        JFrame frame = new JFrame();

        final Box box = Box.createVerticalBox();
        box.add(getLabel("गणेश वार्ड रमेश सुरेश पप्पू पृथ्वी", mangal));
        box.add(getLabel("गणेश वार्ड रमेश सुरेश पप्पू पृथ्वी", arial));

        JButton print = new JButton("Print");
        print.addActionListener(new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                PrinterJob job = PrinterJob.getPrinterJob();
                job.setPrintable(new Printable() {

                    @Override
                    public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
                            throws PrinterException {
                        if (pageIndex > 0) {
                            return NO_SUCH_PAGE;
                        }

                        Graphics2D g2 = (Graphics2D) graphics;
                        g2.translate(pageFormat.getImageableX(), pageFormat.getImageableY());
                        box.printAll(g2);
                        g2.dispose();

                        return PAGE_EXISTS;
                    }

                });
                try {
                    job.print();
                } catch (PrinterException ex) {
                    ex.printStackTrace();
                }
            }

        });

        frame.add(box, BorderLayout.CENTER);
        frame.add(print, BorderLayout.SOUTH);

        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setVisible(true);
    }

}
Akshat
  • 720
  • 9
  • 24
  • JPS is based on the printing functionality baked into Swing components, does your application use those, or is your application an AWT application that you are then trying to pring using JPS? Also, since this is stackoverflow and code is extremely important, can you add minimal working code to your post that sets up a simplified application with a panel with that line of text, with the JPS functionality tied to a button or the like (this should be fairly little code)? That way people can duplicate your problem and investigate it properly. – Mike 'Pomax' Kamermans Apr 09 '15 at 17:19
  • Ok, I'll post a short code, but what about the font files? – Akshat Apr 10 '15 at 08:58
  • I don't know about OSX, but mangal comes free with windows, and I'd expect this to go wrong for any devanagari font rather than specifically this one, so just the code that shows the correct rendering on a Java panel but incorrect print should be enough. – Mike 'Pomax' Kamermans Apr 10 '15 at 15:34
  • Added the code, I tested it. See if you can print using this code! – Akshat Apr 10 '15 at 18:19
  • this still needs a few imports before it'll run (getting javac errors when compiling) – Mike 'Pomax' Kamermans Apr 10 '15 at 22:57
  • Imports added, check now and don't forget to add font files! – Akshat Apr 11 '15 at 02:49
  • hm, that's an odd mix of AWT and Swing (generally, those two are not safely combined), and I see the same problem. The text set in Arial Unicode seems to shape correctly, while the text set in Mangal does not. It's possible that Mangal uses OpenType typographical features that are supported by Word that are not supported by Java's shaping engine, with Arial using *different* table structures to effect the same mark positioning. – Mike 'Pomax' Kamermans Apr 11 '15 at 03:47
  • Well, I guess only solution is to try different fonts of devnagari and see which one shapes correctly and is small in size. – Akshat Apr 11 '15 at 04:15
  • this might be a good but to file against Java, too. I don't know enough about the shaping engine used to be able to tell whether it's the font or the shaper in this case causing the problem. Alternatively, or in addition, might be an interesting question to ask on the [OpenType mailing list](http://www.microsoft.com/typography/otspec/otlist.htm) (there's a fair number of people familiar with devanagari typesetting on that list as well) and possibly on [typophile](http://typophile.com/) (probably in the build section) – Mike 'Pomax' Kamermans Apr 11 '15 at 04:24

0 Answers0