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

Different printeroutput with Graphics2D.drawString() and TextLayout.draw()

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • 1.4.0
    • 1.3.0
    • client-libs
    • 2d
    • beta
    • x86
    • windows_nt
    • Not verified



      Name: boT120536 Date: 02/06/2001


      java version "1.3.0_01"
      Java(TM) 2 Runtime Environment, Standard Edition (build 1.3.0_01)
      Java HotSpot(TM) Client VM (build 1.3.0_01, mixed mode)

      Using the same string and the same font results in a different output on the
      printer, when using Graphics2D.drawString() or when using TextLayout.draw().
      With the bold font the output of TextLayout.draw() has bolder letters.
      With the plain font the difference is very little.
      When printing german umlauts with Graphics2D.drawString() the output on the
      printer is ok, but when printing with TextLayout.draw() the two points above
      the letters are not in the right position. They appear to far to the left.
      The following program will demonstrate this.

      import java.awt.Font;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.RenderingHints;
      import java.awt.font.FontRenderContext;
      import java.awt.font.TextLayout;
      import java.awt.print.*;

      public class PrintDemo implements Printable {

          private static final int PAPER_WIDTH = 594;
          private static final int PAPER_HEIGTH = 840;

          private Font fontBold10;
          private Font fontPlain10;

          private FontRenderContext fontRenderContext;

      public PrintDemo() {
      PrinterJob printerJob = PrinterJob.getPrinterJob();
      PageFormat pageFormat = printerJob.defaultPage();;
                 Paper paper = new Paper();
                 paper.setSize(PAPER_WIDTH, PAPER_HEIGTH);
                 paper.setImageableArea(36, 24, PAPER_WIDTH - 72, PAPER_HEIGTH - 48);
                 pageFormat.setPaper(paper);
                 pageFormat.setOrientation(PageFormat.PORTRAIT);
         fontBold10 = new Font("Helvetica", Font.BOLD, 10);
         fontPlain10 = new Font("Helvetica", Font.PLAIN, 10);
         printerJob.setPrintable(this,pageFormat);
      if (printerJob.printDialog()) {
      try {
      printerJob.print();
      } catch (Exception e) {
      }
      }
         }
        

      public int print(Graphics g, PageFormat pf, int index) throws
      PrinterException {

      if (index > 0) return NO_SUCH_PAGE;

                 Graphics2D g2 = (Graphics2D)g;
                 g2.setRenderingHint(RenderingHints.KEY_FRACTIONALMETRICS,
                                    RenderingHints.VALUE_FRACTIONALMETRICS_ON);

                 fontRenderContext = g2.getFontRenderContext();

                 //using a bold font
                 g2.setFont(fontBold10);
                 g2.drawString("TestString", 80, 60);

                 TextLayout textLayout =
                 new TextLayout("TestString", fontBold10, fontRenderContext);
      textLayout.draw(g2, 80, 80);

                 //using a plain font
                 g2.setFont(fontPlain10);
                 g2.drawString("TestString", 80, 100);

                 textLayout =
                    new TextLayout("TestString", fontPlain10, fontRenderContext);
      textLayout.draw(g2, 80, 120);
              
      //printing german umlauts: a o u A O U
      String string = "\u00e4 \u00f6 \u00fc \u00c4 \u00d6 \u00dc";
                g2.setFont(fontPlain10);
                g2.drawString(string, 80, 140);

                textLayout = new TextLayout(string, fontPlain10, fontRenderContext);
      textLayout.draw(g2, 80, 160);

      return PAGE_EXISTS;
      }

      public static void main(String[] args) {
      PrintDemo printDemo = new PrintDemo();
      System.exit(0);
      }
      }
      (Review ID: 116421)
      ======================================================================

            prr Philip Race
            bonealsunw Bret O'neal (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: