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

PostScript PrintJob LANDSCAPE computes wrong value for getStringBounds

XMLWordPrintable

    • 2d
    • x86_64
    • linux

      FULL PRODUCT VERSION :
      java version "1.7.0_91"
      OpenJDK Runtime Environment (IcedTea 2.6.3) (7u91-2.6.3-1~deb8u1)
      OpenJDK 64-Bit Server VM (build 24.91-b01, mixed mode)


      ADDITIONAL OS VERSION INFORMATION :
      Linux outhouse 3.16.0-4-amd64 #1 SMP Debian 3.16.7-ckt11-1+deb8u3 (2015-08-04) x86_64 GNU/Linux

      A DESCRIPTION OF THE PROBLEM :
      Using StreamPrintServiceFactory, StreamPrintService, to create a print job for creating a PostScript file. Using 12pt "Monospaced.plain" font (character width 7.2pt). When printing LANDSCAPE, the text is squeezed together, compared to portrait. Examining PostScript, the string bounds for each line in LANDSCAPE is smaller that for the same text in PORTRAIT. Simple program to reproduce (pasted in "Steps to Reproduce") demonstrates issue. Output:

       Portrait "0123456789" Width: 72.000000
      Landscape "0123456789" Width: 69.599998

      When taking the same class files to a different platform (not openJDK) it runs as expected, both PORTRAIT and LANDSCAPE compute the same length of the string (72).

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      The included program should demonstrate the issue. No special compile or run options should be required. "bugtest.java". The program uses the same font for both cases, creates a PORTRAIT PostScript Stream PrintJob and measures the horizontal bounds of "0123456789". It then creates a LANDSCAPE print job and measures the bounds of the same string. It was expected that both cases would have the same string bounds, but LANDSCAPE is slightly squeezed. I have found no evidence that the font has been changed, no transform added or TRACKING applied.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
       Portrait "0123456789" Width: 72.000000
      Landscape "0123456789" Width: 72.000000
      ACTUAL -
       Portrait "0123456789" Width: 72.000000
      Landscape "0123456789" Width: 69.599998

      REPRODUCIBILITY :
      This bug can be reproduced always.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.print.*;
      import java.io.*;
      import javax.print.*;
      import java.awt.font.*;
      import java.awt.geom.Rectangle2D;
      import javax.print.attribute.*;
      import javax.print.attribute.standard.*;


      class BugPaper
              implements Printable
      {
              Font __font;
              String _str = "0123456789";
              int _lastPage;

              public BugPaper(Font font, FileOutputStream fos, OrientationRequested or) {
                      __font = font;
                      _lastPage = -1;

                      StreamPrintServiceFactory[] factories;
                      DocFlavor flavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
                      String psMimeType = DocFlavor.BYTE_ARRAY.POSTSCRIPT.getMimeType();

                      factories = StreamPrintServiceFactory.lookupStreamPrintServiceFactories(
                                              flavor, psMimeType);
                      if (factories.length == 0) {
                              System.err.println("No suitable factories");
                              System.exit(0);
                      }

                      try {
                              StreamPrintService sps = factories[0].getPrintService(fos);
                              DocPrintJob pj = sps.createPrintJob();

                              DocAttributeSet dset = new HashDocAttributeSet();
                              dset.add(or);
                              Doc doc = new SimpleDoc(this, flavor, dset);

                              PrintRequestAttributeSet aset = new HashPrintRequestAttributeSet();
                              aset.add(or);
                              pj.print(doc, aset);
                      } catch (PrintException pe) {
                              System.err.println(pe);
                      }
              }

              public int print(Graphics g, PageFormat pf, int pageIndex) {
                      if (pageIndex > 0) {
                              return Printable.NO_SUCH_PAGE;
                      }
                      if (_lastPage == pageIndex) {
                              return Printable.PAGE_EXISTS;
                      }
                      _lastPage = pageIndex;
                      Graphics2D g2d = (Graphics2D)g;
                      double w0 = pf.getImageableWidth();
                      double h0 = pf.getImageableHeight();
                      g2d.setFont(__font);
                      g2d.setColor(Color.black);
                      String orient;
                      if (w0 > h0) {
                              orient = "Landscape";
                      } else {
                              orient = "Portrait";
                      }

                      FontRenderContext frc = g2d.getFontRenderContext();
                      Rectangle2D r2d = g2d.getFont().getStringBounds(_str, frc);
                      System.err.format("%9s \"%s\" Width: %f\n", orient, _str, r2d.getWidth());
                      return Printable.PAGE_EXISTS;
              }
      }

      public class bugtest {
              public static void main(String[] args) {
                      FileOutputStream fos = null;
                      try {
                              fos = new FileOutputStream("out.ps");
                      } catch (Exception ee) {
                              ee.printStackTrace();
                              System.exit(1);
                      }
                      Font font = new Font("Monospaced", Font.PLAIN, 12);


                      BugPaper ppr = new BugPaper(font, fos, OrientationRequested.PORTRAIT);
                      ppr = new BugPaper(font, fos, OrientationRequested.LANDSCAPE);
                      try {
                              fos.close();
                      } catch (Exception ee) {}
              }
      }

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

            prr Philip Race
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: