1 /*
   2  * Copyright (c) 2013, 2015, 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 com.sun.prism.j2d;
  27 
  28 import com.sun.javafx.geom.Rectangle;
  29 import com.sun.prism.PresentableState;
  30 import com.sun.prism.PrinterGraphics;
  31 
  32 public final class PrismPrintGraphics
  33     extends J2DPrismGraphics
  34     implements PrinterGraphics {
  35 
  36     static class PrintResourceFactory extends J2DResourceFactory {
  37         PrintResourceFactory() {
  38             super(null);
  39         }
  40 
  41         @Override
  42         J2DPrismGraphics createJ2DPrismGraphics(J2DPresentable target,
  43                                                 java.awt.Graphics2D g2d) {
  44             J2DPrismGraphics pg = new PrismPrintGraphics(target, g2d);
  45             Rectangle cr = new Rectangle(0, 0, target.getContentWidth(),
  46                                          target.getContentHeight());
  47             pg.setClipRect(cr);
  48             return pg;
  49         }
  50     } 
  51 
  52     static class PagePresentable extends J2DPresentable {
  53         private int width;
  54         private int height;
  55 
  56         static J2DResourceFactory factory = new PrintResourceFactory();
  57 
  58         PagePresentable(int width, int height) {
  59             super(null, factory);
  60             this.width = width;
  61             this.height = height;
  62         }
  63 
  64         @Override
  65         public java.awt.image.BufferedImage createBuffer(int w, int h) {
  66             throw new UnsupportedOperationException("cannot create new buffers for image");
  67         }
  68 
  69         @Override
  70         public boolean lockResources(PresentableState pState) {
  71             return false;
  72         }
  73 
  74         public boolean prepare(Rectangle dirtyregion) {
  75             throw new UnsupportedOperationException("Cannot prepare an image");
  76         }
  77 
  78         public boolean present() {
  79             throw new UnsupportedOperationException("Cannot present on image");
  80         }
  81 
  82         public int getContentWidth() {
  83             return width;
  84         }
  85 
  86         public int getContentHeight() {
  87             return height;
  88         }
  89 
  90         private boolean opaque;
  91         public void setOpaque(boolean opaque) {
  92             this.opaque = opaque;
  93         }
  94 
  95         public boolean isOpaque() {
  96             return opaque;
  97         }
  98     }
  99 
 100     // The FX code thinks it can call setTransform() without
 101     // doing harm. Need to intercept all such calls and convert
 102     // them into a concatenation on the original/default transform.
 103     protected void setTransformG2D(java.awt.geom.AffineTransform tx) {
 104         g2d.setTransform(origTx2D);
 105         g2d.transform(tx);
 106     }
 107 
 108     // Because super() has to the first call, I need this function
 109     // to be called by the super-class constructor. This is all
 110     // just to have the by-product effect of grabbing the tx early.
 111     // I could make the caller of the constructor pass it, but it
 112     // really shouldn't have to be responsible for the necessity
 113     private java.awt.geom.AffineTransform origTx2D;
 114     protected void captureTransform(java.awt.Graphics2D g2d) {
 115         origTx2D = g2d.getTransform();
 116     }
 117 
 118     public PrismPrintGraphics(java.awt.Graphics2D g2d, int width, int height) {
 119         super(new PagePresentable(width, height), g2d);
 120         setClipRect(new Rectangle(0,0,width,height));
 121     }
 122 
 123     PrismPrintGraphics(J2DPresentable target, java.awt.Graphics2D g2d) {
 124         super(target, g2d);
 125     }
 126 }