Skip to content

Commit 76a1109

Browse files
GennadiyKrivosheinprsadhuk
authored andcommitted
8251928: [macos] the printer DPI always be 72, cause some content lost when print out
Reviewed-by: psadhukhan, prr
1 parent e1c9526 commit 76a1109

File tree

5 files changed

+378
-12
lines changed

5 files changed

+378
-12
lines changed

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterGraphicsConfig.java

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -32,6 +32,7 @@
3232
import java.awt.Rectangle;
3333
import java.awt.Transparency;
3434
import java.awt.geom.AffineTransform;
35+
import java.awt.geom.Point2D;
3536
import java.awt.image.BufferedImage;
3637
import java.awt.image.ColorModel;
3738
import java.awt.image.VolatileImage;
@@ -45,10 +46,16 @@ public static CPrinterGraphicsConfig getConfig(PageFormat pf) {
4546

4647
private final GraphicsDevice device;
4748
private final PageFormat pf;
49+
private final AffineTransform deviceTransform;
4850

4951
public CPrinterGraphicsConfig(PageFormat pf) {
52+
this(pf, null);
53+
}
54+
55+
CPrinterGraphicsConfig(PageFormat pf, AffineTransform deviceTransform) {
5056
this.device = new CPrinterDevice(this);
5157
this.pf = pf;
58+
this.deviceTransform = deviceTransform;
5259
}
5360

5461
public PageFormat getPageFormat() {
@@ -178,7 +185,8 @@ public ColorModel getColorModel(int transparency) {
178185
*/
179186
@Override
180187
public AffineTransform getDefaultTransform() {
181-
return new AffineTransform();
188+
return deviceTransform == null ?
189+
new AffineTransform() : new AffineTransform(deviceTransform);
182190
}
183191

184192
/**
@@ -224,6 +232,10 @@ public AffineTransform getNormalizingTransform() {
224232
*/
225233
@Override
226234
public Rectangle getBounds() {
227-
return new Rectangle(0, 0, (int)pf.getWidth(), (int)pf.getHeight());
235+
Point2D.Double pt = new Point2D.Double(pf.getWidth(), pf.getHeight());
236+
Point2D.Double transformedPt = new Point2D.Double();
237+
getDefaultTransform().transform(pt, transformedPt);
238+
return new Rectangle(0, 0,
239+
(int)transformedPt.getX(), (int)transformedPt.getY());
228240
}
229241
}

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterJob.java

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import java.awt.GraphicsEnvironment;
3636
import java.awt.SecondaryLoop;
3737
import java.awt.Toolkit;
38+
import java.awt.geom.AffineTransform;
3839
import java.awt.geom.Rectangle2D;
3940
import java.awt.image.BufferedImage;
4041
import java.awt.print.Pageable;
@@ -85,6 +86,9 @@ public final class CPrinterJob extends RasterPrinterJob {
8586
// future compatibility and the state keeping that it handles.
8687

8788
private static String sShouldNotReachHere = "Should not reach here.";
89+
private static final double USER_SPACE_DPI = 72.0;
90+
private static final int DEFAULT_DOC_DPI_X = 300;
91+
private static final int DEFAULT_DOC_DPI_Y = 300;
8892

8993
private volatile SecondaryLoop printingLoop;
9094
private AtomicReference<Throwable> printErrorRef = new AtomicReference<>();
@@ -110,6 +114,9 @@ public final class CPrinterJob extends RasterPrinterJob {
110114
private final Object fNSPrintInfoLock = new Object();
111115
private final Object disposerReferent = new Object();
112116

117+
private double hRes = DEFAULT_DOC_DPI_X;
118+
private double vRes = DEFAULT_DOC_DPI_Y;
119+
113120
static {
114121
// AWT has to be initialized for the native code to function correctly.
115122
Toolkit.getDefaultToolkit();
@@ -461,8 +468,7 @@ public void print(PrintRequestAttributeSet attributes) throws PrinterException {
461468
*/
462469
@Override
463470
protected double getXRes() {
464-
// NOTE: This is not used in the CPrinterJob code path.
465-
return 0;
471+
return hRes;
466472
}
467473

468474
/**
@@ -471,8 +477,31 @@ protected double getXRes() {
471477
*/
472478
@Override
473479
protected double getYRes() {
474-
// NOTE: This is not used in the CPrinterJob code path.
475-
return 0;
480+
return vRes;
481+
}
482+
483+
@Override
484+
protected void setXYRes(double x, double y) {
485+
hRes = x;
486+
vRes = y;
487+
}
488+
489+
/**
490+
* Returns the resolution in dots per inch across the width
491+
* of the page. This method take into account the page orientation.
492+
*/
493+
private double getXRes(PageFormat pageFormat) {
494+
return pageFormat.getOrientation() == PageFormat.PORTRAIT ?
495+
getXRes() : getYRes();
496+
}
497+
498+
/**
499+
* Returns the resolution in dots per inch across the height
500+
* of the page. This method take into account the page orientation.
501+
*/
502+
private double getYRes(PageFormat pageFormat) {
503+
return pageFormat.getOrientation() == PageFormat.PORTRAIT ?
504+
getYRes() : getXRes();
476505
}
477506

478507
/**
@@ -817,7 +846,11 @@ private void printToPathGraphics( final PeekGraphics graphics, // Always an a
817846
// This is called from the native side.
818847
Runnable r = new Runnable() { public void run() {
819848
try {
820-
SurfaceData sd = CPrinterSurfaceData.createData(page, context); // Just stores page into an ivar
849+
AffineTransform deviceTransform = new AffineTransform(
850+
getXRes(page) / USER_SPACE_DPI, 0, 0,
851+
getYRes(page) / USER_SPACE_DPI, 0, 0);
852+
SurfaceData sd = CPrinterSurfaceData
853+
.createData(page, deviceTransform, context); // Just stores page into an ivar
821854
if (defaultFont == null) {
822855
defaultFont = new Font("Dialog", Font.PLAIN, 12);
823856
}
@@ -873,6 +906,9 @@ private Object[] getPageformatPrintablePeekgraphics(final int pageIndex) {
873906
Rectangle2D pageFormatArea =
874907
getPageFormatArea(pageFormat);
875908
initPrinterGraphics(peekGraphics, pageFormatArea);
909+
double scaleX = getXRes(pageFormat) / USER_SPACE_DPI;
910+
double scaleY = getYRes(pageFormat) / USER_SPACE_DPI;
911+
peekGraphics.scale(scaleX, scaleY);
876912

877913
// Do the assignment here!
878914
ret[0] = pageFormat;

src/java.desktop/macosx/classes/sun/lwawt/macosx/CPrinterSurfaceData.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2014, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -26,6 +26,7 @@
2626
package sun.lwawt.macosx;
2727

2828
import java.awt.*;
29+
import java.awt.geom.AffineTransform;
2930
import java.awt.image.*;
3031
import java.awt.print.PageFormat;
3132
import java.nio.ByteBuffer;
@@ -40,8 +41,8 @@ public final class CPrinterSurfaceData extends OSXSurfaceData{
4041
// public static final SurfaceType IntArgbPQ = SurfaceType.IntArgb.deriveSubType(DESC_INT_ARGB_PQ);
4142
public static final SurfaceType IntRgbPQ = SurfaceType.IntRgb.deriveSubType(DESC_INT_RGB_PQ);
4243

43-
static SurfaceData createData(PageFormat pf, long context) {
44-
return new CPrinterSurfaceData(CPrinterGraphicsConfig.getConfig(pf), context);
44+
static SurfaceData createData(PageFormat pf, AffineTransform deviceTransform, long context) {
45+
return new CPrinterSurfaceData(new CPrinterGraphicsConfig(pf, deviceTransform), context);
4546
}
4647

4748
private CPrinterSurfaceData(GraphicsConfiguration gc, long context) {

src/java.desktop/macosx/native/libawt_lwawt/awt/PrinterView.m

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2011, 2024, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2011, 2025, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -86,6 +86,8 @@ - (void)drawRect:(NSRect)aRect
8686
GET_CPRINTERJOB_CLASS();
8787
DECLARE_METHOD(jm_printToPathGraphics, sjc_CPrinterJob, "printToPathGraphics",
8888
"(Lsun/print/PeekGraphics;Ljava/awt/print/PrinterJob;Ljava/awt/print/Printable;Ljava/awt/print/PageFormat;IJ)V");
89+
DECLARE_METHOD(jm_getXRes, sjc_CPrinterJob, "getXRes", "(Ljava/awt/print/PageFormat;)D");
90+
DECLARE_METHOD(jm_getYRes, sjc_CPrinterJob, "getYRes", "(Ljava/awt/print/PageFormat;)D");
8991

9092
// Create and draw into a new CPrinterGraphics with the current Context.
9193
assert(fCurPageFormat != NULL);
@@ -103,6 +105,21 @@ - (void)drawRect:(NSRect)aRect
103105

104106
jlong context = ptr_to_jlong([printLoop context]);
105107
CGContextRef cgRef = (CGContextRef)[[printLoop context] graphicsPort];
108+
109+
// Scale from the java document DPI to the user space DPI
110+
jdouble hRes = (*env)->CallDoubleMethod(env, fPrinterJob, jm_getXRes, fCurPageFormat);
111+
CHECK_EXCEPTION();
112+
jdouble vRes = (*env)->CallDoubleMethod(env, fPrinterJob, jm_getYRes, fCurPageFormat);
113+
CHECK_EXCEPTION();
114+
if (hRes > 0 && vRes > 0) {
115+
double defaultDeviceDPI = 72;
116+
double scaleX = defaultDeviceDPI / hRes;
117+
double scaleY = defaultDeviceDPI / vRes;
118+
if (scaleX != 1 || scaleY != 1) {
119+
CGContextScaleCTM(cgRef, scaleX, scaleY);
120+
}
121+
}
122+
106123
CGContextSaveGState(cgRef); //04/28/2004: state needs to be saved here due to addition of lazy state management
107124

108125
(*env)->CallVoidMethod(env, fPrinterJob, jm_printToPathGraphics, fCurPeekGraphics, fPrinterJob,

0 commit comments

Comments
 (0)