RFR: 8251928: [macos] the printer DPI always be 72, cause some content lost when print out [v4]
GennadiyKrivoshein
duke at openjdk.org
Thu Sep 4 17:42:21 UTC 2025
On Wed, 3 Sep 2025 22:26:55 GMT, Phil Race <prr at openjdk.org> wrote:
>> GennadiyKrivoshein has updated the pull request incrementally with one additional commit since the last revision:
>>
>> remove setDevClip usage. Update names. Handle page format orientation.
>
> src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java line 854:
>
>> 852: double scaleY = getYRes()/ USER_SPACE_DPI;
>> 853: PageFormat scaledPage = scalePageFormat(page, scaleX, scaleY);
>> 854: SurfaceData sd = CPrinterSurfaceData.createData(scaledPage, context); // Just stores page into an ivar
>
> This bleeds through in an application visible way.
> Take your test case and add this code to the print(..) method and you'll see what I mean.
>
> GraphicsConfiguration gc = g2.getDeviceConfiguration();
> Rectangle bds = gc.getBounds();
> System.out.println("bds="+bds);
>
> For me it then prints
> bds=java.awt.Rectangle[x=0,y=0,width=2550,height=3300]
This is the expected behavior, the same as on other OS. I checked the GraphicsConfiguration on Windows 11, Ubuntu 22.04 and macOS 10.15 with DPI 300 and A4 format. The device boundaries on macOS are 1 pixel different. The console output is below.
**Windows 11**
Page format size: 595.275574 x 841.889771
Device bounds: java.awt.Rectangle[x=0,y=0,width=2480,height=3507]
Run: 1, Page index: 0
java.lang.Throwable
at DevConfig$DevicePrintable.print(DevConfig.java:38)
at java.desktop/sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:2212)
at java.desktop/sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1601)
at DevConfig.main(DevConfig.java:23)
Page format size: 595.275574 x 841.889771
Device bounds: java.awt.Rectangle[x=0,y=0,width=2480,height=3507]
Run: 2, Page index: 0
java.lang.Throwable
at DevConfig$DevicePrintable.print(DevConfig.java:38)
at java.desktop/sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:2252)
at java.desktop/sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1601)
at DevConfig.main(DevConfig.java:23)
**Ubunut 22.04**
Page format size: 595,275574 x 841,889771
Device bounds: java.awt.Rectangle[x=0,y=0,width=2480,height=3507]
Run: 1, Page index: 0
java.lang.Throwable
at DevConfig$DevicePrintable.print(DevConfig.java:41)
at java.desktop/sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:2243)
at java.desktop/sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1632)
at DevConfig.main(DevConfig.java:25)
Page format size: 595,275574 x 841,889771
Device bounds: java.awt.Rectangle[x=0,y=0,width=2480,height=3507]
Run: 2, Page index: 0
java.lang.Throwable
at DevConfig$DevicePrintable.print(DevConfig.java:41)
at java.desktop/sun.print.RasterPrinterJob.printPage(RasterPrinterJob.java:2283)
at java.desktop/sun.print.RasterPrinterJob.print(RasterPrinterJob.java:1632)
at DevConfig.main(DevConfig.java:25)
**macOS 10.15**
Page format size: 595.275567 x 841.889771
Device bounds: java.awt.Rectangle[x=0,y=0,width=2479,height=3508]
Run: 1, Page index: 0
java.lang.Throwable
at DevConfig$DevicePrintable.print(DevConfig.java:36)
at java.desktop/sun.lwawt.macosx.CPrinterJob$4.run(CPrinterJob.java:919)
at java.desktop/sun.lwawt.macosx.CPrinterJob.printAndGetPageFormatArea(CPrinterJob.java:938)
at java.desktop/sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
at java.desktop/sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:383)
at DevConfig.main(DevConfig.java:21)
Page format size: 595.275567 x 841.889771
Device bounds: java.awt.Rectangle[x=0,y=0,width=2479,height=3508]
run: 2, Page index: 0
java.lang.Throwable
at DevConfig$DevicePrintable.print(DevConfig.java:36)
at java.desktop/sun.lwawt.macosx.CPrinterJob$2.run(CPrinterJob.java:838)
at java.desktop/sun.lwawt.macosx.CPrinterJob.printToPathGraphics(CPrinterJob.java:852)
at java.desktop/sun.lwawt.macosx.CPrinterJob.printLoop(Native Method)
at java.desktop/sun.lwawt.macosx.CPrinterJob.print(CPrinterJob.java:383)
at DevConfig.main(DevConfig.java:21)
> src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java line 904:
>
>> 902: new BufferedImage(
>> 903: (int)Math.round(scaledPageFormat.getWidth()),
>> 904: (int)Math.round(scaledPageFormat.getHeight()),
>
> Wow, that is going to be one big BufferedImage. This doesn't seem right or necessary for a PeekGraphics.
>
> Look at RasterPrinterGraphics. It uses a 1x1 image (!) It just sets things up so that when the app asks for the graphics config it gets the page dimensions back.
This is a good point, but I have not changed the existing approach to creating a PeekGraphics. A BufferedImage is created based on the paper format size and DPI.
Does changing the approach to memory consumption align with the goals of this PR, to be able to set the document's DPI to match the printer's DPI?
> src/java.desktop/macosx/native/libawt_lwawt/awt/CGraphicsDevice.h line 29:
>
>> 27: * Some default values for invalid CoreGraphics display ID.
>> 28: */
>> 29: #define DEFAULT_DEVICE_WIDTH 1024
>
> You should revert adding this file and put these defines back into CGraphicsDevice.m.
>
> The only thing you us from here is DEFAULT_DEVICE_DPI and that appears to be because it is "72", but where you are using it in PrinterView.m it is really based on the 72 DPI default defined by Java 2D. See the class doc for Graphics2D. And also the size for java.awt.print.Paper is specified in points too.
>
> I suggest just creating a local variable in PrinterView.m
Done
> src/java.desktop/macosx/native/libawt_lwawt/awt/PrinterView.m line 119:
>
>> 117: double scaleY = DEFAULT_DEVICE_DPI / vRes;
>> 118: if (scaleX != 1 || scaleY != 1) {
>> 119: if ([[[NSPrintOperation currentOperation] printInfo] orientation] == NSPortraitOrientation) {
>
> This fails to compile for me
> src/java.desktop/macosx/native/libawt_lwawt/awt/PrinterView.m:119:79: error: comparison of different enumeration types ('NSPaperOrientation' (aka 'enum NSPaperOrientation') and 'enum NSPrintingOrientation') [-Werror,-Wenum-compare]
> 119 | if ([[[NSPrintOperation currentOperation] printInfo] orientation] == NSPortraitOrientation) {
> | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~~~
> It should be NSPaperOrientationPortrait
Thanks, NSPortraitOrientation was deprecated for a long time. I replaced NSPortraitOrientation with NSPaperOrientationPortrait.
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2322943014
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2322938418
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2322927603
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2322932711
More information about the client-libs-dev
mailing list