JDK-8251928 : [macos] the printer DPI always be 72, cause some content lost when print out
  • Type: Bug
  • Component: client-libs
  • Sub-Component: 2d
  • Affected Version: 8,9,11,14.0.1,15,16
  • Priority: P4
  • Status: Open
  • Resolution: Unresolved
  • OS: os_x
  • CPU: x86_64
  • Submitted: 2020-08-17
  • Updated: 2025-05-28
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
Other
tbdUnresolved
Related Reports
Relates :  
Description
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 ----------


Comments
A pull request was submitted for review. Branch: master URL: https://git.openjdk.org/jdk/pull/25489 Date: 2025-05-28 10:29:17 +0000
28-05-2025

Verified executing testcase Test Result: ============ Windows 10 OS- 8u261: Pass 14.0.1: Pass 15ea36: Pass (Screenshot Attached) MacOS 10.10.5- 8u231: Fail (screenshot attached)
18-08-2020