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

Printed text letter spacing incorrect after affine transform

XMLWordPrintable

    • 2d
    • x86_64
    • windows_7

      FULL PRODUCT VERSION :
      java version "1.8.0_60"
      Java(TM) SE Runtime Environment (build 1.8.0_60-b27)
      Java HotSpot(TM) 64-Bit Server VM (build 25.60-b23, mixed mode)

      ADDITIONAL OS VERSION INFORMATION :
      Windows 7 Professional SP-1 (64bit)

      EXTRA RELEVANT SYSTEM CONFIGURATION :
      Printers tested:
      HP 4100
      Dymo Label Writer

      A DESCRIPTION OF THE PROBLEM :
      I need to print variable-length strings within a pre-defined space on the printed page. I am using an affine transform scale operation to shrink text horizontally to fit the given space. I can render the text into a window on the screen and it appears as desired. However, when printed (in landscape orientation), the letters are spaced out as though no scale had been applied (even though the individual letters themselves are scaled correctly). This only happens if the page format is set to landscape, and only with certain fonts.
      Fonts causing the problem include: Amienne, Byington, Huxtable, Symbol, Tahoma, Tandelle, Time New Roman.
      Fonts that do NOT cause the problem: Arial, Blue Highway, Boopee, Calibri, Georgia, Verdana,

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please see test case code.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Printed text should appear the same as screen text after affine transform is applied to graphics2d.
      ACTUAL -
      The letters are spaced out as though no scale had been applied (even though the individual letters themselves are scaled correctly).

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import javax.swing.JFrame;
      import javax.swing.JPanel;
      import javax.swing.WindowConstants;

      import java.awt.Font;
      import java.awt.Graphics;
      import java.awt.Graphics2D;
      import java.awt.Rectangle;
      import java.awt.RenderingHints;

      import java.awt.print.PageFormat;
      import java.awt.print.Paper;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;

      /**
       * @author csk
       *
       */
      public class App
      extends JPanel
      implements Printable
      {

          public App()
      {
      }
          
          
          public void paint(Graphics g)
              {
           // Define size of box into which to print
              Rectangle rect = new Rectangle(252, 90);

              Graphics2D g2d = (Graphics2D) g;
              
              PageFormat pageFormat = new PageFormat();
              Paper paper = new Paper();
              paper.setSize(rect.getWidth(), rect.getHeight());
              paper.setImageableArea(0, 0, rect.getWidth(), rect.getHeight());
              pageFormat.setPaper(paper);
              
              // Outline the paper
              g2d.drawRoundRect(100, 100, (int)paper.getWidth(), (int)paper.getHeight(), 9, 9);

              g2d.setFont(new Font("Times New Roman", Font.PLAIN, 36));

              // Set the transform to shrink the text horizontally
              g2d.scale(0.5, 1.0);
              
              g2d.drawString("Long Text Shrunk To Fit", 244, 160);

              }
          
          public int print(Graphics graphics, PageFormat pageFormat, int pageIndex)
          throws PrinterException
              {
              if (pageIndex >= 1)
                  return Printable.NO_SUCH_PAGE;

              ((Graphics2D) graphics).setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); // See http://docs.oracle.com/javase/tutorial/2d/advanced/quality.html
              paint(graphics);
              
              return Printable.PAGE_EXISTS;
              }
              
          /**
           * @param args
           */
          public static void main(String[] args)
              {
              
              JFrame f = new JFrame();
              f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
              
              App a = new App();
              f.add(a);

              f.setSize(400, 400);
              f.setVisible(true);
              
              final PrinterJob printJob = PrinterJob.getPrinterJob();
              PageFormat pageFormat = printJob.defaultPage();

              pageFormat.setOrientation(PageFormat.LANDSCAPE);
              
              printJob.setPrintable(a, pageFormat);
              if (true)
              try
                  {
                  printJob.print();
                  }
              catch (PrinterException e)
                  {
                  // TODO Auto-generated catch block
                  e.printStackTrace();
                  }

              }

      }

      ---------- END SOURCE ----------

      CUSTOMER SUBMITTED WORKAROUND :
      None found.

            kabhishek Kumar Abhishek (Inactive)
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            7 Start watching this issue

              Created:
              Updated: