RFR: 8251928: [macos] the printer DPI always be 72, cause some content lost when print out [v4]
Phil Race
prr at openjdk.org
Wed Sep 3 22:30:46 UTC 2025
On Tue, 2 Sep 2025 11:26:30 GMT, GennadiyKrivoshein <duke at openjdk.org> wrote:
>> The fix for the https://bugs.openjdk.org/browse/JDK-8251928.
>>
>> **Description**.
>> This PR contains changes to be able to print with DPI higher than 72 on macOS, set default CPrinterJob DPI is 300 like in the PSPrinterJob.
>>
>> As described in the macOS drawing guide, the following steps are required to draw with high DPI (https://developer.apple.com/library/archive/documentation/Cocoa/Conceptual/CocoaDrawingGuide/Transforms/Transforms.html#//apple_ref/doc/uid/TP40003290-CH204-BCICIJAJ):
>> 1. Convert the user-space point, size, or rectangle value to device space coordinates;
>> 2. Normalize the value in device space so that it is aligned to the appropriate pixel boundary;
>> 3. Convert the normalized value back to user space;
>> 4. Draw your content using the adjusted value.
>>
>> The 1-st step is now implemented in the CPrinterJob, a Graphics provided to the print method adjusted to a printer's DPI.
>> The 2-nd step is a drawing process in the java code (without changes).
>> The 3-rd step is now implemented in the PrinterView.m, the drawing scaled back to the 72 DPI.
>> The 4-th step is a drawing process in the native code (without changes).
>>
>> **Tests**.
>> I run all tests from javax.print package and there is no any regression.
>> New test covers macOS and Linux only because we know its default DPI - 300.
>
> 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]
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.
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
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
-------------
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2320362696
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2320346423
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2319840369
PR Review Comment: https://git.openjdk.org/jdk/pull/25489#discussion_r2319952392
More information about the client-libs-dev
mailing list