--- old/src/java.desktop/share/classes/sun/print/PSPrinterJob.java 2016-08-05 15:33:20.553132916 +0530 +++ new/src/java.desktop/share/classes/sun/print/PSPrinterJob.java 2016-08-05 15:33:20.309132916 +0530 @@ -68,6 +68,7 @@ import javax.print.attribute.standard.DialogTypeSelection; import javax.print.attribute.standard.JobName; import javax.print.attribute.standard.Sides; +import javax.print.attribute.standard.MediaPrintableArea; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; @@ -489,6 +490,92 @@ return doPrint; } + @Override + protected void validatePaper(Paper origPaper, Paper newPaper) { + if (origPaper == null || newPaper == null) { + return; + } else { + double wid = origPaper.getWidth(); + double hgt = origPaper.getHeight(); + double ix = origPaper.getImageableX(); + double iy = origPaper.getImageableY(); + double iw = origPaper.getImageableWidth(); + double ih = origPaper.getImageableHeight(); + double imgX, imgY, imgWid, imgHgt; + + /* + * this returns the imageable area of default media of chosen printer + * from CUPS via CUPSPrinter#getPageSizes(). + * If it returns null (which should not be the case) we fall back + * to default Paper (Letter in US, A4 otherwise) imageable area + */ + MediaPrintableArea mpa = (MediaPrintableArea) + getPrintService().getDefaultAttributeValue(MediaPrintableArea.class); + if (mpa != null) { + imgX = mpa.getX(MediaPrintableArea.INCH) * 72; + imgY = mpa.getY(MediaPrintableArea.INCH) * 72; + imgWid = mpa.getWidth(MediaPrintableArea.INCH) * 72; + imgHgt = mpa.getHeight(MediaPrintableArea.INCH) * 72; + } else { + imgX = newPaper.getImageableX(); + imgY = newPaper.getImageableY(); + imgWid = newPaper.getImageableWidth(); + imgHgt = newPaper.getImageableHeight(); + } + + wid = ((wid > 0.0) ? wid : newPaper.getWidth()); + hgt = ((hgt > 0.0) ? hgt : newPaper.getHeight()); + if ((imgX*2) + imgWid > wid) { + imgWid = wid - imgX*2; + } + if ((imgY*2) + imgHgt > hgt) { + imgHgt = hgt - imgY*2; + } + + /* We try to mitigate the effects of floating point rounding errors + * by only setting a value if it would differ from the value in the + * target by at least 0.10 points = 1/720 inches. + * eg if the values present in the target are close to the calculated + * values then we accept the target. + */ + final double epsilon = 0.10; + + if (ix < 0.0) { + ix = 0.0; + } + if (iy < 0.0) { + iy = 0.0; + } + if (iw < 0.0) { + iw = 0.0; + } + if (ih < 0.0) { + ih = 0.0; + } + if ((ix + epsilon) < imgX) { + ix = imgX; + } + if ((iy + epsilon) < imgY) { + iy = imgY; + } + if (iw + epsilon > imgWid) { + iw = imgWid; + } + if (ih + epsilon > imgHgt) { + ih = imgHgt; + } + if ((ix + iw + epsilon) > (imgX + imgWid)) { + ix = (imgX + imgWid) - iw; + } + if ((iy + ih + epsilon) > (imgY + imgHgt)) { + iy = (imgY + imgHgt) - ih; + } + + newPaper.setSize(wid, hgt); + newPaper.setImageableArea(ix, iy, iw, ih); + } + } + /** * Invoked by the RasterPrinterJob super class * this method is called to mark the start of a --- /dev/null 2016-08-05 10:58:58.361070000 +0530 +++ new/test/java/awt/print/PrinterJob/TestValidatePage.java 2016-08-05 15:33:21.225132916 +0530 @@ -0,0 +1,51 @@ +/* + * Copyright (c) 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 + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ +/* + * @test + * @bug 6574279 + * @summary Verifies if validatePage in linux returns valid PageFormat + * @requires (os.family == "linux") + * @run main TestValidatePage + */ +import java.awt.print.PageFormat; +import java.awt.print.Paper; +import java.awt.print.PrinterJob; + +public class TestValidatePage +{ + public static void main(String[] args) { + // get printerJob ang default pageFormat + PrinterJob javaPrinterJob = java.awt.print.PrinterJob.getPrinterJob(); + PageFormat pf = javaPrinterJob.defaultPage(); + + Paper p = pf.getPaper(); + p.setImageableArea(0.0,0.0,p.getWidth(),p.getHeight()); + pf.setPaper(p); + + pf = javaPrinterJob.validatePage(pf); + + if (pf.getImageableX() == 0.0f) { + throw new RuntimeException(" validatePage does not return valid PageFormat"); + } + } +}