1/*
2 * Copyright (c) 1998, 2000, 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
26package sun.print;
27
28import java.awt.AlphaComposite;
29import java.awt.Color;
30import java.awt.Composite;
31import java.awt.Graphics2D;
32import java.awt.Image;
33import java.awt.Paint;
34
35import java.awt.font.TextLayout;
36
37import java.awt.image.RenderedImage;
38import java.awt.image.renderable.RenderableImage;
39
40/**
41 * Maintain information about the type of drawing
42 * performed by a printing application.
43 */
44public class PeekMetrics {
45
46    private boolean mHasNonSolidColors;
47
48    private boolean mHasCompositing;
49
50    private boolean mHasText;
51
52    private boolean mHasImages;
53
54    /**
55     * Return {@code true} if the application
56     * has done any drawing with a Paint that
57     * is not an instance of {@code Color}
58     */
59    public boolean hasNonSolidColors() {
60        return mHasNonSolidColors;
61    }
62
63    /**
64     * Return true if the application has
65     * done any drawing with an alpha other
66     * than 1.0.
67     */
68    public boolean hasCompositing() {
69        return mHasCompositing;
70    }
71
72    /**
73     * Return true if the application has
74     * drawn any text.
75     */
76    public boolean hasText() {
77        return mHasText;
78    }
79
80    /**
81     * Return true if the application has
82     * drawn any images.
83     */
84    public boolean hasImages() {
85        return mHasImages;
86    }
87
88    /**
89     * The application is performing a fill
90     * so record the needed information.
91     */
92    public void fill(Graphics2D g) {
93        checkDrawingMode(g);
94    }
95
96    /**
97     * The application is performing a draw
98     * so record the needed information.
99     */
100    public void draw(Graphics2D g) {
101        checkDrawingMode(g);
102    }
103
104    /**
105     * The application is performing a clearRect
106     * so record the needed information.
107     */
108    public void clear(Graphics2D g) {
109        checkPaint(g.getBackground());
110    }
111    /**
112     * The application is drawing text
113     * so record the needed information.
114     */
115    public void drawText(Graphics2D g) {
116        mHasText = true;
117        checkDrawingMode(g);
118    }
119
120    /**
121     * The application is drawing text
122     * defined by {@code TextLayout}
123     * so record the needed information.
124     */
125    public void drawText(Graphics2D g, TextLayout textLayout) {
126        mHasText = true;
127        checkDrawingMode(g);
128    }
129
130    /**
131     * The application is drawing the passed
132     * in image.
133     */
134    public void drawImage(Graphics2D g, Image image) {
135        mHasImages = true;
136    }
137
138    /**
139     * The application is drawing the passed
140     * in image.
141     */
142    public void drawImage(Graphics2D g, RenderedImage image) {
143        mHasImages = true;
144    }
145
146    /**
147     * The application is drawing the passed
148     * in image.
149     */
150    public void drawImage(Graphics2D g, RenderableImage image) {
151        mHasImages = true;
152    }
153
154    /**
155     * Record information about the current paint
156     * and composite.
157     */
158    private void checkDrawingMode(Graphics2D g) {
159
160        checkPaint(g.getPaint());
161        checkAlpha(g.getComposite());
162
163    }
164
165    /**
166     * Record information about drawing done
167     * with the supplied {@code Paint}.
168     */
169    private void checkPaint(Paint paint) {
170
171        if (paint instanceof Color) {
172            if (((Color)paint).getAlpha() < 255) {
173                mHasNonSolidColors = true;
174            }
175        } else {
176            mHasNonSolidColors = true;
177        }
178    }
179
180    /**
181     * Record information about drawing done
182     * with the supplied {@code Composite}.
183     */
184    private void checkAlpha(Composite composite) {
185
186        if (composite instanceof AlphaComposite) {
187            AlphaComposite alphaComposite = (AlphaComposite) composite;
188            float alpha = alphaComposite.getAlpha();
189            int rule = alphaComposite.getRule();
190
191            if (alpha != 1.0
192                    || (rule != AlphaComposite.SRC
193                        && rule != AlphaComposite.SRC_OVER)) {
194
195                mHasCompositing = true;
196            }
197
198        } else {
199            mHasCompositing = true;
200        }
201
202    }
203
204}
205