Path: blob/master/src/java.desktop/share/classes/sun/print/PeekMetrics.java
41153 views
/*1* Copyright (c) 1998, 2000, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.print;2627import java.awt.AlphaComposite;28import java.awt.Color;29import java.awt.Composite;30import java.awt.Graphics2D;31import java.awt.Image;32import java.awt.Paint;3334import java.awt.font.TextLayout;3536import java.awt.image.RenderedImage;37import java.awt.image.renderable.RenderableImage;3839/**40* Maintain information about the type of drawing41* performed by a printing application.42*/43public class PeekMetrics {4445private boolean mHasNonSolidColors;4647private boolean mHasCompositing;4849private boolean mHasText;5051private boolean mHasImages;5253/**54* Return {@code true} if the application55* has done any drawing with a Paint that56* is not an instance of {@code Color}57*/58public boolean hasNonSolidColors() {59return mHasNonSolidColors;60}6162/**63* Return true if the application has64* done any drawing with an alpha other65* than 1.0.66*/67public boolean hasCompositing() {68return mHasCompositing;69}7071/**72* Return true if the application has73* drawn any text.74*/75public boolean hasText() {76return mHasText;77}7879/**80* Return true if the application has81* drawn any images.82*/83public boolean hasImages() {84return mHasImages;85}8687/**88* The application is performing a fill89* so record the needed information.90*/91public void fill(Graphics2D g) {92checkDrawingMode(g);93}9495/**96* The application is performing a draw97* so record the needed information.98*/99public void draw(Graphics2D g) {100checkDrawingMode(g);101}102103/**104* The application is performing a clearRect105* so record the needed information.106*/107public void clear(Graphics2D g) {108checkPaint(g.getBackground());109}110/**111* The application is drawing text112* so record the needed information.113*/114public void drawText(Graphics2D g) {115mHasText = true;116checkDrawingMode(g);117}118119/**120* The application is drawing text121* defined by {@code TextLayout}122* so record the needed information.123*/124public void drawText(Graphics2D g, TextLayout textLayout) {125mHasText = true;126checkDrawingMode(g);127}128129/**130* The application is drawing the passed131* in image.132*/133public void drawImage(Graphics2D g, Image image) {134mHasImages = true;135}136137/**138* The application is drawing the passed139* in image.140*/141public void drawImage(Graphics2D g, RenderedImage image) {142mHasImages = true;143}144145/**146* The application is drawing the passed147* in image.148*/149public void drawImage(Graphics2D g, RenderableImage image) {150mHasImages = true;151}152153/**154* Record information about the current paint155* and composite.156*/157private void checkDrawingMode(Graphics2D g) {158159checkPaint(g.getPaint());160checkAlpha(g.getComposite());161162}163164/**165* Record information about drawing done166* with the supplied {@code Paint}.167*/168private void checkPaint(Paint paint) {169170if (paint instanceof Color) {171if (((Color)paint).getAlpha() < 255) {172mHasNonSolidColors = true;173}174} else {175mHasNonSolidColors = true;176}177}178179/**180* Record information about drawing done181* with the supplied {@code Composite}.182*/183private void checkAlpha(Composite composite) {184185if (composite instanceof AlphaComposite) {186AlphaComposite alphaComposite = (AlphaComposite) composite;187float alpha = alphaComposite.getAlpha();188int rule = alphaComposite.getRule();189190if (alpha != 1.0191|| (rule != AlphaComposite.SRC192&& rule != AlphaComposite.SRC_OVER)) {193194mHasCompositing = true;195}196197} else {198mHasCompositing = true;199}200201}202203}204205206