--- old/src/java.desktop/share/classes/sun/print/PSPrinterJob.java 2016-08-11 13:25:17.585823999 +0530 +++ new/src/java.desktop/share/classes/sun/print/PSPrinterJob.java 2016-08-11 13:25:17.321691999 +0530 @@ -106,6 +106,7 @@ //REMIND: Remove use of this class when IPPPrintService is moved to share directory. import java.lang.reflect.Method; import javax.print.attribute.standard.JobSheets; +import javax.print.attribute.standard.MediaPrintableArea; /** * A class which initiates and executes a PostScript printer job. @@ -260,7 +261,7 @@ private String mDestination = "lp"; private boolean mNoJobSheet = false; - + private String mOptions; private Font mLastFont; @@ -487,7 +488,121 @@ } return doPrint; + } + + private boolean isSupportedMediaPrintableArea(MediaPrintableArea mpa) { + int units = MediaPrintableArea.INCH; + MediaPrintableArea[] mediaPrintables = + new CUPSPrinter(getPrintService().getName()).getMediaPrintableArea(); + if (mediaPrintables != null) { + for (int i=0; i= mediaPrintables[i].getX(units)) && + (mpa.getY(units) >= mediaPrintables[i].getY(units)) && + (mpa.getX(units) + mpa.getWidth(units) <= + mediaPrintables[i].getX(units) + + mediaPrintables[i].getWidth(units)) && + (mpa.getY(units) + mpa.getHeight(units) <= + mediaPrintables[i].getY(units) + + mediaPrintables[i].getHeight(units))) { + return true; + } + } + } + return false; } + + @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; + + MediaPrintableArea mpa = null; + MediaPrintableArea origmpa = new MediaPrintableArea((float)ix/72.0f, + (float)iy/72.0f, (float)iw/72.0f, (float)ih/72.0f, MediaPrintableArea.INCH); + + if (isSupportedMediaPrintableArea(origmpa)) { + mpa = origmpa; + } else { + /* + * this returns the imageable area of default media of chosen printer + * from CUPS via CUPSPrinter#getPageSizes(). + */ + 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 @@ -1607,7 +1722,7 @@ } else if (getPrintService(). isAttributeCategorySupported(JobSheets.class)) { ncomps+=1; // for jobsheet - } + } String osname = System.getProperty("os.name"); if (osname.equals("Linux") || osname.contains("OS X")) { @@ -1630,7 +1745,7 @@ } if ((pFlags & OPTIONS) != 0) { execCmd[n++] = new String(options); - } + } } else { ncomps+=1; //add 1 arg for lp execCmd = new String[ncomps]; @@ -1653,7 +1768,7 @@ } if ((pFlags & OPTIONS) != 0) { execCmd[n++] = "-o" + options; - } + } } execCmd[n++] = spoolFile; return execCmd; --- /dev/null 2016-08-11 10:20:31.474120000 +0530 +++ new/test/java/awt/print/PrinterJob/TestValidatePage.java 2016-08-11 13:25:18.086074000 +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"); + } + } +}