/* * Copyright (c) 2007, 2022, 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. */ import java.awt.BorderLayout; import java.awt.Button; import java.awt.Color; import java.awt.Component; import java.awt.Dimension; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Frame; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.Panel; import java.awt.geom.AffineTransform; import java.awt.image.BufferedImage; import java.awt.print.PageFormat; import java.awt.print.Pageable; import java.awt.print.Printable; import java.awt.print.PrinterException; import java.awt.print.PrinterJob; import java.lang.reflect.InvocationTargetException; import java.util.Objects; import javax.print.attribute.HashPrintRequestAttributeSet; import javax.print.attribute.PrintRequestAttributeSet; import static java.awt.EventQueue.invokeAndWait; /* * @test * @bug 6531728 * @key printer * @library ../../../regtesthelpers * @build PassFailJFrame * @summary Test printing of images which need to have src area clipped * @run main/manual ClippedImages */ public class ClippedImages { private static ClippedImageCanvas c; private static Frame frame; public static void main(String[] args) throws InterruptedException, InvocationTargetException { if (PrinterJob.lookupPrintServices().length > 0) { String instruction = """ This is a manual test as it requires that you compare the on-screen rendering with the printed output. Select the 'Print All' button to print out the test. It will generate 4 sides of content: as it will print each of 2 sets of transformed images in portrait, and landscape orientations. The sets of images are in turn made up of two similar sets of pages: one is 'random' images, the other is 16 squares. Use the 'Toggle Contents' button to view the screen rendering. For each page compare the printed content to the same on-screen one taking careful note of a) the positions of the red/blue circles on the corners b) that numerical text on the image is displayed similarly e) that the green quadrilaterals match on-screen f) that the rendering is clipped at the default (typically 1 inch) margins of the page. The test PASSES if the onscreen and printed rendering match """; PassFailJFrame passFailJFrame = new PassFailJFrame("Test " + "Instruction", instruction, 15); invokeAndWait(ClippedImages::createTestUI); passFailJFrame.awaitAndCheck(); } else { System.out.println("Printer not configured or available." + " Test cannot continue."); PassFailJFrame.forcePass(); } } public static void createTestUI() { frame = new Frame("Clipped Src Area Image Printing Test"); c = new ClippedImageCanvas(); frame.add(c, BorderLayout.CENTER); Button paintButton = new Button("Toggle Contents"); paintButton.addActionListener((ae) -> { c.toggleContents(); c.repaint(); }); Button printThisButton = new Button("Print This"); printThisButton.addActionListener((ae) -> printOne()); Button printAllButton = new Button("Print All"); printAllButton.addActionListener((ae) -> printAll()); Panel p = new Panel(); p.add(paintButton); p.add(printThisButton); p.add(printAllButton); frame.add(p, BorderLayout.SOUTH); frame.setLocationRelativeTo(null); frame.pack(); PassFailJFrame.addTestWindow(frame); PassFailJFrame.positionTestWindow(frame, PassFailJFrame.Position.HORIZONTAL); frame.setVisible(true); } private static void printOne() { PrinterJob pj = PrinterJob.getPrinterJob(); PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); c.setPrinterJob(pj, false); pj.setPrintable(c); if (pj.printDialog(attrs)) { try { pj.print(attrs); } catch (PrinterException pe) { pe.printStackTrace(); throw new RuntimeException("Exception whilst printing."); } finally { System.out.println("PRINT RETURNED OK."); } } else { throw new RuntimeException("Test failed : " + "User selected 'Cancel' button on the print dialog"); } } private static void printAll() { PrinterJob pj = PrinterJob.getPrinterJob(); PrintRequestAttributeSet attrs = new HashPrintRequestAttributeSet(); c.setPrinterJob(pj, true); pj.setPageable(c); if (pj.printDialog(attrs)) { try { pj.print(attrs); } catch (PrinterException pe) { pe.printStackTrace(); throw new RuntimeException("Exception whilst printing."); } finally { System.out.println("PRINT RETURNED OK."); } } else { throw new RuntimeException("Test failed : " + "User selected 'Cancel' button on the print dialog"); } } } class ClippedImageCanvas extends Component implements Printable, Pageable { BufferedImage img = null; int sw=50, sh=50; ClippedImageCanvas() { img = new BufferedImage(sw, sh, BufferedImage.TYPE_INT_RGB); Graphics2D g2d = img.createGraphics(); g2d.setColor(Color.red); g2d.fillRect(0 ,0, sw, sh); g2d.setColor(Color.black); int cnt = 0; Font font = new Font("Serif", Font.PLAIN, 11); g2d.setFont(font); FontMetrics fm = g2d.getFontMetrics(); for (int y=12;y getNumberOfPages()-1) { return Printable.NO_SUCH_PAGE; } Graphics2D g2d = (Graphics2D)g; g2d.translate(pgFmt.getImageableX(), pgFmt.getImageableY()); g.drawString(getOrientStr(pgFmt), 0, 12); paint(g2d); return Printable.PAGE_EXISTS; } private void drawImage(Graphics g, int dx1, int dy1, int dx2, int dy2, int sx1, int sy1, int sx2, int sy2) { int rx = (dx1 < dx2) ? dx1 : dx2; int ry = (dy1 < dy2) ? dy1 : dy2; int rw = dx2-dx1; if (rw < 0) rw = -rw; int rh = dy2-dy1; if (rh < 0) rh = -rh; g.setColor(Color.green); g.drawRect(rx-1 ,ry-1, rw+1, rh+1); g.drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null); g.setColor(Color.blue); int r=5; g.drawOval(dx1-r, dy1-r, 2*r, 2*r); g.setColor(Color.red); g.drawOval(dx2-r, dy2-r, 2*r, 2*r); } private AffineTransform savedTx = null; private void saveTx(Graphics2D g2d) { savedTx = g2d.getTransform(); } private void restoreTx(Graphics2D g2d) { g2d.setTransform(savedTx); } public void paint(Graphics g) { Dimension size = getSize(); g.setColor(Color.black); for (int p=0;p dx1,dy1 and * sx2,sy2 -> dx2,dy2 which may imply flips and scales. * To test this we need to test all combinations of these parameters * with drawImage. * If we have a source rectangle with vertices, sA, sB, sC, sD * there are 4 combinations : sA+sD, sD+sA, sB+sC, sC+sB. * Similarly for the destination with vertices, dA, dB, dC, dD * there are 4 combinations : dA+dD, dD+dA, dB+dC, dC+dB. * Thus we need 16 calls to test all combinations. * Note that we set the source area coordinates (x and y -20->80) * to be beyond the image size (50x50) so clipping is always needed. */ int sxa = -20, sya = -20; int sxb = 80, syb = -20; int sxc = -20, syc = 80; int sxd = 80, syd = 80; int dxa = 0, dya = 0; int dxb = 80, dyb = 0; int dxc = 0, dyc = 80; int dxd = 80, dyd = 80; int incX = 100; int incY = 100; g.translate(20, 20); /* sA + sD -> dA + dD - the normal untransformed case */ drawImage(g, dxa, dya, dxd, dyd, sxa, sya, sxd, syd); g.translate(incX, 0); /* sD + sA -> dA + dD */ drawImage(g, dxa, dya, dxd, dyd, sxd, syd, sxa, sya); g.translate(incX, 0); /* sB + sC -> dA + dD */ drawImage(g, dxa, dya, dxd, dyd, sxb, syb, sxc, syc); g.translate(incX, 0); /* sC + sB -> dA + dD */ drawImage(g, dxa, dya, dxd, dyd, sxc, syc, sxb, syb); g.translate(-3*incX, incY); /* sA + sD -> dD + dA */ drawImage(g, dxd, dyd, dxa, dya, sxa, sya, sxd, syd); g.translate(incX, 0); /* sD + sA -> dD + dA */ drawImage(g, dxd, dyd, dxa, dya, sxd, syd, sxa, sya); g.translate(incX, 0); /* sB + sC -> dD + dA */ drawImage(g, dxd, dyd, dxa, dya, sxb, syb, sxc, syc); g.translate(incX, 0); /* sC + sB -> dD + dA */ drawImage(g, dxd, dyd, dxa, dya, sxc, syc, sxb, syb); g.translate(-3*incX, incY); /* sA + sD -> dB + dC */ drawImage(g, dxb, dyb, dxc, dyc, sxa, sya, sxd, syd); g.translate(incX, 0); /* sD + sA -> dB + dC */ drawImage(g, dxb, dyb, dxc, dyc, sxd, syd, sxa, sya); g.translate(incX, 0); /* sB + sC -> dB + dC */ drawImage(g, dxb, dyb, dxc, dyc, sxb, syb, sxc, syc); g.translate(incX, 0); /* sC + sB -> dB + dC */ drawImage(g, dxb, dyb, dxc, dyc, sxc, syc, sxb, syb); g.translate(-3*incX, incY); /* sA + sD -> dC + dB */ drawImage(g, dxc, dyc, dxb, dyb, sxa, sya, sxd, syd); g.translate(incX, 0); /* sD + sA -> dC + dB */ drawImage(g, dxc, dyc, dxb, dyb, sxd, syd, sxa, sya); g.translate(incX, 0); /* sB + sC -> dC + dB */ drawImage(g, dxc, dyc, dxb, dyb, sxb, syb, sxc, syc); g.translate(incX, 0); /* sC + sB -> dC + dB */ drawImage(g, dxc, dyc, dxb, dyb, sxc, syc, sxb, syb); } /* Size is chosen to match default imageable width of a NA letter * page. This means there will be clipping, what is clipped will * depend on PageFormat orientation. */ public Dimension getPreferredSize() { return new Dimension(468, 468); } }