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

[macos] the printer DPI always be 72, cause some content lost when print out

XMLWordPrintable

    • 2d
    • x86_64
    • os_x

      ADDITIONAL SYSTEM INFORMATION :
      MacOS 10.15.5
      JDK 14.0.1

      A DESCRIPTION OF THE PROBLEM :
      On windows and linux, java report the correct DPI of printer, on macOS it seems always be 72DPI.
      In Printable.print method, the transform of graphics object is always scale(1).

      When draw line with clip, if the clipped area too small, the line will not print, but on Windows and Linux, the same line can be print out.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      This can reproduce on many jdk version. I tested jdk8,10,14
      print with my test case.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      Expected two line be printed
      ACTUAL -
      Only one line be printed, the line within clipped area lost.

      ---------- BEGIN SOURCE ----------
      import java.awt.*;
      import java.awt.geom.GeneralPath;
      import java.awt.print.PageFormat;
      import java.awt.print.Printable;
      import java.awt.print.PrinterException;
      import java.awt.print.PrinterJob;
      import javax.print.PrintServiceLookup;

      /**
       *
       * @author Tilman Hausherr
       */
      public class PDFBOX4935PrintDpi implements Printable
      {

          public static void main(String[] args) throws PrinterException
          {
              new PDFBOX4935PrintDpi().doStuff();
          }

          PDFBOX4935PrintDpi()
          {
          }

          void doStuff() throws PrinterException
          {
              PrinterJob job = PrinterJob.getPrinterJob();
              job.setPrintService(PrintServiceLookup.lookupDefaultPrintService());
              job.setPrintable(this);
              if (job.printDialog())
              {
                  job.print();
              }
          }

          @Override
          public int print(Graphics graphics, PageFormat pageFormat, int index)
          {
              if (index == 0)
              {
                  Graphics2D g2d = (Graphics2D) graphics;
                  g2d.setFont(new Font("Courier", Font.PLAIN, 12));
                  g2d.setColor(Color.BLACK);

                  double pageWidth = pageFormat.getWidth();
                  double xLeft = pageWidth / 10;
                  double yBase = pageFormat.getHeight() / 2;
                  g2d.drawString("ScaleX: " + g2d.getTransform().getScaleX(),
                          (int) xLeft, (int) yBase);
                  g2d.drawString("ScaleY: " + g2d.getTransform().getScaleY(),
                          (int) xLeft, (int) yBase + 25);
                  g2d.drawString("DPI: " + Math.round(g2d.getTransform().getScaleX() * 72),
                          (int) xLeft, (int) yBase + 60);

                  float lineWidth = 0.5f;
                  g2d.setStroke(new BasicStroke(lineWidth));
                  // WITH 72DPI THIS LINE IS OK
                  double xRight = pageWidth - xLeft;
                  g2d.drawLine((int) xLeft, (int) yBase + 80,
                          (int) (xRight),(int) yBase + 80);

                  // DRAW LINE WITH CLIP
                  GeneralPath line = new GeneralPath();
                  double yLine = yBase + 100;
                  line.moveTo(xLeft, yLine);
                  line.lineTo(xLeft, yLine - lineWidth / 2.0f);
                  line.lineTo(xRight, yLine - lineWidth / 2.0f);
                  line.lineTo(xRight, yLine + lineWidth / 2.0f);
                  line.lineTo(xLeft, yLine + lineWidth / 2.0f );
                  line.closePath();
                  g2d.clip(line);
                  line.reset();
                  line.moveTo(xLeft, yLine);
                  line.lineTo(xRight, yLine);
                  g2d.draw(line);

                  return Printable.PAGE_EXISTS;
              }
              return Printable.NO_SUCH_PAGE;
          }
      }
      ---------- END SOURCE ----------

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

              Created:
              Updated: