001: /*
002: * Copyright 1998-2000 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package sun.print;
027:
028: import java.awt.AlphaComposite;
029: import java.awt.Color;
030: import java.awt.Composite;
031: import java.awt.Graphics2D;
032: import java.awt.Image;
033: import java.awt.Paint;
034:
035: import java.awt.font.TextLayout;
036:
037: import java.awt.image.RenderedImage;
038: import java.awt.image.renderable.RenderableImage;
039:
040: /**
041: * Maintain information about the type of drawing
042: * performed by a printing application.
043: */
044: public class PeekMetrics {
045:
046: private boolean mHasNonSolidColors;
047:
048: private boolean mHasCompositing;
049:
050: private boolean mHasText;
051:
052: private boolean mHasImages;
053:
054: /**
055: * Return <code>true</code> if the application
056: * has done any drawing with a Paint that
057: * is not an instance of <code>Color</code>
058: */
059: public boolean hasNonSolidColors() {
060: return mHasNonSolidColors;
061: }
062:
063: /**
064: * Return true if the application has
065: * done any drawing with an alpha other
066: * than 1.0.
067: */
068: public boolean hasCompositing() {
069: return mHasCompositing;
070: }
071:
072: /**
073: * Return true if the application has
074: * drawn any text.
075: */
076: public boolean hasText() {
077: return mHasText;
078: }
079:
080: /**
081: * Return true if the application has
082: * drawn any images.
083: */
084: public boolean hasImages() {
085: return mHasImages;
086: }
087:
088: /**
089: * The application is performing a fill
090: * so record the needed information.
091: */
092: public void fill(Graphics2D g) {
093: checkDrawingMode(g);
094: }
095:
096: /**
097: * The application is performing a draw
098: * so record the needed information.
099: */
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: /**
113: * The application is drawing text
114: * so record the needed information.
115: */
116: public void drawText(Graphics2D g) {
117: mHasText = true;
118: checkDrawingMode(g);
119: }
120:
121: /**
122: * The application is drawing text
123: * defined by <code>TextLayout</code>
124: * so record the needed information.
125: */
126: public void drawText(Graphics2D g, TextLayout textLayout) {
127: mHasText = true;
128: checkDrawingMode(g);
129: }
130:
131: /**
132: * The application is drawing the passed
133: * in image.
134: */
135: public void drawImage(Graphics2D g, Image image) {
136: mHasImages = true;
137: }
138:
139: /**
140: * The application is drawing the passed
141: * in image.
142: */
143: public void drawImage(Graphics2D g, RenderedImage image) {
144: mHasImages = true;
145: }
146:
147: /**
148: * The application is drawing the passed
149: * in image.
150: */
151: public void drawImage(Graphics2D g, RenderableImage image) {
152: mHasImages = true;
153: }
154:
155: /**
156: * Record information about the current paint
157: * and composite.
158: */
159: private void checkDrawingMode(Graphics2D g) {
160:
161: checkPaint(g.getPaint());
162: checkAlpha(g.getComposite());
163:
164: }
165:
166: /**
167: * Record information about drawing done
168: * with the supplied <code>Paint</code>.
169: */
170: private void checkPaint(Paint paint) {
171:
172: if (paint instanceof Color) {
173: if (((Color) paint).getAlpha() < 255) {
174: mHasNonSolidColors = true;
175: }
176: } else {
177: mHasNonSolidColors = true;
178: }
179: }
180:
181: /**
182: * Record information about drawing done
183: * with the supplied <code>Composite</code>.
184: */
185: private void checkAlpha(Composite composite) {
186:
187: if (composite instanceof AlphaComposite) {
188: AlphaComposite alphaComposite = (AlphaComposite) composite;
189: float alpha = alphaComposite.getAlpha();
190: int rule = alphaComposite.getRule();
191:
192: if (alpha != 1.0
193: || (rule != AlphaComposite.SRC && rule != AlphaComposite.SRC_OVER)) {
194:
195: mHasCompositing = true;
196: }
197:
198: } else {
199: mHasCompositing = true;
200: }
201:
202: }
203:
204: }
|