Printing Issue for Unicode Devnagari Script in Java
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):
But When I print the same using Java Printing Support (Using Mangal Font): 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:
And when I use Arial Unicode font in my Java Application, it works fine, too:
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);
}
}
链接地址: http://www.djcxy.com/p/88678.html