Skip to content

Commit f056d24

Browse files
committed
8248908: Printer.createPageLayout() returns 0.75" margins instead of hardware margins
Reviewed-by: kcr
1 parent 5e7e452 commit f056d24

File tree

2 files changed

+121
-12
lines changed

2 files changed

+121
-12
lines changed

modules/javafx.graphics/src/main/java/com/sun/prism/j2d/print/J2DPrinter.java

Lines changed: 27 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -698,7 +698,10 @@ private void populateMedia() {
698698
for (int i=0; i<media.length; i++) {
699699
Media m = media[i];
700700
if (m instanceof MediaSizeName) {
701-
pSet.add(addPaper(((MediaSizeName)m)));
701+
Paper p = addPaper(((MediaSizeName)m));
702+
if (p != null) {
703+
pSet.add(p);
704+
}
702705
} else if (m instanceof MediaTray) {
703706
tSet.add(addPaperSource((MediaTray)m));
704707
}
@@ -782,22 +785,34 @@ private synchronized final PaperSource addPaperSource(MediaTray tray) {
782785
private final Map<Paper, MediaSizeName> paperToMediaMap
783786
= new HashMap<Paper, MediaSizeName>();
784787

788+
private Paper createPaper(MediaSizeName media) {
789+
Paper paper = null;
790+
MediaSize sz = MediaSize.getMediaSizeForName(media);
791+
if (sz != null) {
792+
double pw = sz.getX(1) / 1000.0;
793+
double ph = sz.getY(1) / 1000.0;
794+
paper = PrintHelper.createPaper(media.toString(),
795+
pw, ph, Units.MM);
796+
}
797+
return paper;
798+
}
799+
785800
private synchronized final Paper addPaper(MediaSizeName media) {
786801
Paper paper = predefinedPaperMap.get(media);
787-
if (paper == null ) {
788-
MediaSize sz = MediaSize.getMediaSizeForName(media);
789-
if (sz != null) {
790-
double pw = sz.getX(1) / 1000.0;
791-
double ph = sz.getY(1) / 1000.0;
792-
paper = PrintHelper.createPaper(media.toString(),
793-
pw, ph, Units.MM);
794-
}
802+
if (paper == null) {
803+
paper = createPaper(media);
795804
}
805+
/* If that failed create a Paper from the default MediaSizeName */
796806
if (paper == null) {
797-
paper = Paper.NA_LETTER;
807+
Media m = (Media)service.getDefaultAttributeValue(Media.class);
808+
if (m instanceof MediaSizeName) {
809+
paper = createPaper((MediaSizeName)m);
810+
}
811+
}
812+
if (paper != null) {
813+
paperToMediaMap.put(paper, media);
814+
mediaToPaperMap.put(media, paper);
798815
}
799-
paperToMediaMap.put(paper, media);
800-
mediaToPaperMap.put(media, paper);
801816
return paper;
802817
}
803818

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
/*
2+
* Copyright (c) 2020, Oracle and/or its affiliates. All rights reserved.
3+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4+
*
5+
* This code is free software; you can redistribute it and/or modify it
6+
* under the terms of the GNU General Public License version 2 only, as
7+
* published by the Free Software Foundation. Oracle designates this
8+
* particular file as subject to the "Classpath" exception as provided
9+
* by Oracle in the LICENSE file that accompanied this code.
10+
*
11+
* This code is distributed in the hope that it will be useful, but WITHOUT
12+
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13+
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
14+
* version 2 for more details (a copy is included in the LICENSE file that
15+
* accompanied this code).
16+
*
17+
* You should have received a copy of the GNU General Public License version
18+
* 2 along with this work; if not, write to the Free Software Foundation,
19+
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20+
*
21+
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22+
* or visit www.oracle.com if you need additional information or have any
23+
* questions.
24+
*/
25+
26+
package test.javafx.print;
27+
28+
import javafx.application.Platform;
29+
30+
import javafx.print.PageLayout;
31+
import javafx.print.PageOrientation;
32+
import javafx.print.Paper;
33+
import javafx.print.Printer;
34+
import static javafx.print.Printer.MarginType.HARDWARE_MINIMUM;
35+
36+
import javax.print.PrintService;
37+
import javax.print.PrintServiceLookup;
38+
import javax.print.attribute.HashPrintRequestAttributeSet;
39+
import javax.print.attribute.PrintRequestAttributeSet;
40+
import javax.print.attribute.standard.Media;
41+
import javax.print.attribute.standard.MediaPrintableArea;
42+
import static javax.print.attribute.standard.MediaPrintableArea.INCH;
43+
import javax.print.attribute.standard.MediaSizeName;
44+
45+
import org.junit.Test;
46+
import static org.junit.Assert.fail;
47+
import static org.junit.Assume.assumeNotNull;
48+
49+
public class MarginsTest {
50+
51+
@Test public void test() {
52+
53+
Printer printer = Printer.getDefaultPrinter();
54+
assumeNotNull(printer);
55+
56+
PageLayout layout =
57+
printer.createPageLayout(Paper.NA_LETTER,
58+
PageOrientation.PORTRAIT,
59+
HARDWARE_MINIMUM);
60+
61+
int lm = (int)Math.round(layout.getLeftMargin());
62+
int rm = (int)Math.round(layout.getRightMargin());
63+
int bm = (int)Math.round(layout.getBottomMargin());
64+
int tm = (int)Math.round(layout.getTopMargin());
65+
66+
System.out.println("FX : lm=" + lm + " rm=" + rm +
67+
" tm=" + tm + " bm=" + bm);
68+
/* 0.75ins * 72 = 54 pts */
69+
if (lm != 54 || rm != 54 || tm != 54 || bm != 54) {
70+
return; // Got something other than default.
71+
}
72+
73+
/* Could this be what we got from 2D ? Unlikely but check. */
74+
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
75+
/* If this is null, too much chance of false positive to continue */
76+
if (service == null) {
77+
return;
78+
}
79+
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
80+
pras.add(MediaSizeName.NA_LETTER);
81+
MediaPrintableArea[] mpa = (MediaPrintableArea[])service.
82+
getSupportedAttributeValues(MediaPrintableArea.class, null, pras);
83+
if (mpa.length == 0) { // never null.
84+
return;
85+
}
86+
int mlm = (int)(Math.round(mpa[0].getX(INCH)*72));
87+
int mtm = (int)(Math.round(mpa[0].getX(INCH)*72));
88+
System.out.println("2D : lm=" + mlm + " tm= " + mtm);
89+
if (mlm == 54 && mtm == 54) {
90+
return;
91+
}
92+
fail("Margins differ.");
93+
}
94+
}

0 commit comments

Comments
 (0)