Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8279299

MacOS native Printer calls AttributedString to print Japanese causes garbage

XMLWordPrintable

    • 2d
    • generic
    • generic

      ADDITIONAL SYSTEM INFORMATION :
      MacOS 11.6

      A DESCRIPTION OF THE PROBLEM :
      Hello!
      I uses AttributedString to print Japanese (or Chinese),
      it can't print correctly, it will print as garbage.
      Codes are below. Thanks!

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      1. run PrintTest.java pasted
      2. select printer (PDF Printer) to print
      3. open pdf file printed

      ACTUAL -
      first line Japanese is incorrect printed by g2.drawString(AttributedCharacterIterator, x, y)
      second line Japanese is correct printed by g2.drawString(String, x, y)

      ---------- BEGIN SOURCE ----------
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.print.PageFormat;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;
      import java.text.AttributedString;

      import javax.swing.SwingUtilities;

      public class PrintTest implements Printable {
          public int print(Graphics g, PageFormat pf, int pageIndex) throws PrinterException {
              if (pageIndex > 1) {
                  return Printable.NO_SUCH_PAGE;
              }

              String str = "日本語印刷テスト : English Print Test";
              Graphics2D g2 = (Graphics2D) g;
              AttributedString as = new AttributedString(str);
              g2.drawString(as.getIterator(), 100, 100); //NG
              g2.drawString(str, 100, 200); //OK

              return Printable.PAGE_EXISTS;
          }

          public static void main(String[] args) {
              SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      try {
                          PrinterJob job = PrinterJob.getPrinterJob();
                          PrintTest printable = new PrintTest();
                          job.setPrintable(printable);
                          if (job.printDialog()) {
                              job.print();
                          }
                      } catch (PrinterException e) {
                          e.printStackTrace();
                      }
                  }
              });
          }
      }
      ---------- END SOURCE ----------

      FREQUENCY : always


            psadhukhan Prasanta Sadhukhan
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            5 Start watching this issue

              Created:
              Updated:
              Resolved: