6601097: Margins are not reset to hardware margins when width/height is 0 or -ve alongwith x, y
Reviewed-by: prr, jdv
--- a/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java Thu Jun 23 12:53:56 2016 +0300
+++ b/jdk/src/java.desktop/share/classes/sun/print/RasterPrinterJob.java Thu Jun 23 16:46:13 2016 +0530
@@ -683,7 +683,21 @@
float iw = (float)(page.getPaper().getImageableWidth()/DPI);
float iy = (float)(page.getPaper().getImageableY()/DPI);
float ih = (float)(page.getPaper().getImageableHeight()/DPI);
- if (ix < 0) ix = 0f; if (iy < 0) iy = 0f;
+
+ if (ix < 0) ix = 0; if (iy < 0) iy = 0;
+ if (iw <= 0) iw = (float)(page.getPaper().getWidth()/DPI) - (ix*2);
+
+ // If iw is still negative, it means ix is too large to print
+ // anything inside printable area if we have to leave the same margin
+ // in the right side of paper so we go back to default mpa values
+ if (iw < 0) iw = 0;
+
+ if (ih <= 0) ih = (float)(page.getPaper().getHeight()/DPI) - (iy*2);
+
+ // If ih is still negative, it means iy is too large to print
+ // anything inside printable area if we have to leave the same margin
+ // in the bottom side of paper so we go back to default mpa values
+ if (ih < 0) ih = 0;
try {
pageAttributes.add(new MediaPrintableArea(ix, iy, iw, ih,
MediaPrintableArea.INCH));
--- a/jdk/test/java/awt/print/PrinterJob/Margins.java Thu Jun 23 12:53:56 2016 +0300
+++ b/jdk/test/java/awt/print/PrinterJob/Margins.java Thu Jun 23 16:46:13 2016 +0530
@@ -1,5 +1,5 @@
/*
- * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@@ -23,15 +23,22 @@
/**
* @test
- * @bug 6543815
+ * @bug 6543815 6601097
* @summary Image should be sent to printer, no exceptions thrown.
- * The 2 printouts should have a rectangle which is the minimum
- * possible margin.
+ * The 3 printouts should have a rectangle which is the minimum
+ * possible margins ie, the margins should be hardware margins
+ * and not java default 1 inch margins.
* @run main/manual Margins
*/
-import java.awt.*;
-import java.awt.print.*;
+import java.awt.print.PrinterJob;
+import java.awt.print.Printable;
+import java.awt.print.PageFormat;
+import java.awt.print.Paper;
+import java.awt.print.PrinterException;
+import java.awt.Graphics;
+import java.awt.Graphics2D;
+import java.awt.Color;
public class Margins implements Printable {
@@ -59,7 +66,22 @@
job.print();
} catch (PrinterException e) {
}
- }
+
+ pageFormat = job.defaultPage();
+ paper = pageFormat.getPaper();
+ wid = paper.getWidth();
+ hgt = paper.getHeight();
+
+ paper.setImageableArea(0, -10, -wid, hgt);
+ pageFormat = job.pageDialog(pageFormat);
+ pageFormat.setPaper(paper);
+
+ job.setPrintable(new Margins(), pageFormat);
+ try {
+ job.print();
+ } catch (PrinterException e) {
+ }
+ }
public int print(Graphics g, PageFormat pf, int page)
throws PrinterException {
@@ -76,12 +98,22 @@
throw new RuntimeException("Imageable x or y is a negative value.");
}
+
Paper paper = pf.getPaper();
double wid = paper.getWidth();
double hgt = paper.getHeight();
+ /* If imageable width/height is -ve, then print was done with 1" margin
+ * ie ix=72 iy=72 iw=451 ih=697 and wid=595
+ * but with fix, we get print with hardware margin ie
+ * ix=12, iy=12, iw=571, ih=817
+ */
+ if ((wid - iw > 72) || (hgt - ih > 72)) {
+ throw new RuntimeException("Imageable width or height is negative value");
+ }
if ((ix+iw > wid) || (iy+ih > hgt)) {
- throw new RuntimeException("Printable width or height exceeds paper width or height.");
+ throw new RuntimeException("Printable width or height "
+ + "exceeds paper width or height.");
}
Graphics2D g2d = (Graphics2D)g;