1918 lines · 70.2 KB · Java
RawDownload
/* * Copyright (c) 1998, 2006, 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.  Oracle designates this * particular file as subject to the "Classpath" exception as provided * by Oracle in the LICENSE file that accompanied this code. * * 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. */ package sun.print; import java.util.Map; import java.awt.BasicStroke;import java.awt.Color;import java.awt.Composite;import java.awt.Graphics;import java.awt.Graphics2D;import java.awt.Font;import java.awt.FontMetrics;import java.awt.font.FontRenderContext;import java.awt.Graphics;import java.awt.GraphicsConfiguration;import java.awt.Image;import java.awt.Paint;import java.awt.Rectangle;import java.awt.Shape;import java.awt.Stroke;import java.awt.RenderingHints;import java.awt.RenderingHints.Key; import java.awt.font.GlyphVector;import java.awt.font.TextLayout; import java.awt.geom.AffineTransform;import java.awt.geom.Line2D;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.awt.geom.RoundRectangle2D;import java.awt.image.BufferedImage;import java.awt.image.BufferedImageOp;import java.awt.image.ImageObserver;import java.awt.image.RenderedImage;import java.awt.image.renderable.RenderableImage;import java.awt.print.PrinterGraphics;import java.awt.print.PrinterJob; import java.text.AttributedCharacterIterator; import sun.java2d.Spans; public class PeekGraphics extends Graphics2D                          implements PrinterGraphics,                                     ImageObserver,                                     Cloneable {     /**     * Drawing methods will be forwarded to this object.     */    Graphics2D mGraphics;     /**     * The PrinterJob controlling the current printing.     */    PrinterJob mPrinterJob;     /**     * Keeps track of where drawing occurs on the page.     */    private Spans mDrawingArea = new Spans();     /**     * Track information about the types of drawing     * performed by the printing application.     */    private PeekMetrics mPrintMetrics = new PeekMetrics();     /**     * If true the application will only be drawing AWT style     * graphics, no Java2D graphics.     */    private boolean mAWTDrawingOnly = false;     /**     * The new PeekGraphics2D will forward state changing     * calls to 'graphics'. 'printerJob' is stored away     * so that the printing application can get the PrinterJob     * if needed.     */    public PeekGraphics(Graphics2D graphics, PrinterJob printerJob) {         mGraphics = graphics;        mPrinterJob = printerJob;    }     /**     * Return the Graphics2D object that does the drawing     * for this instance.     */    public Graphics2D getDelegate() {        return mGraphics;    }     /**     * Set the Graphics2D instance which will do the     * drawing.     */    public void setDelegate(Graphics2D graphics) {        mGraphics = graphics;    }     public PrinterJob getPrinterJob() {        return mPrinterJob;    }     /**     * The caller promises that only AWT graphics will be drawn.     * The print system can use this information to make general     * assumptions about the types of graphics to be drawn without     * requiring the application to draw the contents multiple     * times.     */    public void setAWTDrawingOnly() {        mAWTDrawingOnly = true;    }     public boolean getAWTDrawingOnly() {        return mAWTDrawingOnly;    }     /**     * Return a Spans instance describing the parts of the page in     * to which drawing occurred.     */    public Spans getDrawingArea() {        return mDrawingArea;    }     /**     * Returns the device configuration associated with this Graphics2D.     */    public GraphicsConfiguration getDeviceConfiguration() {        return ((RasterPrinterJob)mPrinterJob).getPrinterGraphicsConfig();    } /* The Delegated Graphics Methods */     /**     * Creates a new <code>Graphics</code> object that is     * a copy of this <code>Graphics</code> object.     * @return     a new graphics context that is a copy of     *                       this graphics context.     * @since      JDK1.0     */    public Graphics create() {        PeekGraphics newGraphics = null;         try {            newGraphics = (PeekGraphics) clone();            newGraphics.mGraphics = (Graphics2D) mGraphics.create();         /* This exception can not happen unless this         * class no longer implements the Cloneable         * interface.         */        } catch (CloneNotSupportedException e) {            // can never happen.        }         return newGraphics;    }     /**     * Translates the origin of the graphics context to the point     * (<i>x</i>,&nbsp;<i>y</i>) in the current coordinate system.     * Modifies this graphics context so that its new origin corresponds     * to the point (<i>x</i>,&nbsp;<i>y</i>) in this graphics context's     * original coordinate system.  All coordinates used in subsequent     * rendering operations on this graphics context will be relative     * to this new origin.     * @param  x   the <i>x</i> coordinate.     * @param  y   the <i>y</i> coordinate.     * @since   JDK1.0     */    public void translate(int x, int y) {        mGraphics.translate(x, y);    }     /**     * Concatenates the current transform of this Graphics2D with a     * translation transformation.     * This is equivalent to calling transform(T), where T is an     * AffineTransform represented by the following matrix:     * <pre>     *          [   1    0    tx  ]     *          [   0    1    ty  ]     *          [   0    0    1   ]     * </pre>     */    public void translate(double tx, double ty) {        mGraphics.translate(tx, ty);    }     /**     * Concatenates the current transform of this Graphics2D with a     * rotation transformation.     * This is equivalent to calling transform(R), where R is an     * AffineTransform represented by the following matrix:     * <pre>     *          [   cos(theta)    -sin(theta)    0   ]     *          [   sin(theta)     cos(theta)    0   ]     *          [       0              0         1   ]     * </pre>     * Rotating with a positive angle theta rotates points on the positive     * x axis toward the positive y axis.     * @param theta The angle of rotation in radians.     */    public void rotate(double theta) {        mGraphics.rotate(theta);    }     /**     * Concatenates the current transform of this Graphics2D with a     * translated rotation transformation.     * This is equivalent to the following sequence of calls:     * <pre>     *          translate(x, y);     *          rotate(theta);     *          translate(-x, -y);     * </pre>     * Rotating with a positive angle theta rotates points on the positive     * x axis toward the positive y axis.     * @param theta The angle of rotation in radians.     * @param x The x coordinate of the origin of the rotation     * @param y The x coordinate of the origin of the rotation     */    public void rotate(double theta, double x, double y) {        mGraphics.rotate(theta, x, y);    }     /**     * Concatenates the current transform of this Graphics2D with a     * scaling transformation.     * This is equivalent to calling transform(S), where S is an     * AffineTransform represented by the following matrix:     * <pre>     *          [   sx   0    0   ]     *          [   0    sy   0   ]     *          [   0    0    1   ]     * </pre>     */    public void scale(double sx, double sy) {        mGraphics.scale(sx, sy);    }     /**     * Concatenates the current transform of this Graphics2D with a     * shearing transformation.     * This is equivalent to calling transform(SH), where SH is an     * AffineTransform represented by the following matrix:     * <pre>     *          [   1   shx   0   ]     *          [  shy   1    0   ]     *          [   0    0    1   ]     * </pre>     * @param shx The factor by which coordinates are shifted towards the     * positive X axis direction according to their Y coordinate     * @param shy The factor by which coordinates are shifted towards the     * positive Y axis direction according to their X coordinate     */    public void shear(double shx, double shy) {        mGraphics.shear(shx, shy);    }     /**     * Gets this graphics context's current color.     * @return    this graphics context's current color.     * @see       java.awt.Color     * @see       java.awt.Graphics#setColor     * @since     JDK1.0     */    public Color getColor() {        return mGraphics.getColor();    }     /**     * Sets this graphics context's current color to the specified     * color. All subsequent graphics operations using this graphics     * context use this specified color.     * @param     c   the new rendering color.     * @see       java.awt.Color     * @see       java.awt.Graphics#getColor     * @since     JDK1.0     */    public void setColor(Color c) {        mGraphics.setColor(c);    }     /**     * Sets the paint mode of this graphics context to overwrite the     * destination with this graphics context's current color.     * This sets the logical pixel operation function to the paint or     * overwrite mode.  All subsequent rendering operations will     * overwrite the destination with the current color.     * @since   JDK1.0     */    public void setPaintMode() {        mGraphics.setPaintMode();    }     /**     * Sets the paint mode of this graphics context to alternate between     * this graphics context's current color and the new specified color.     * This specifies that logical pixel operations are performed in the     * XOR mode, which alternates pixels between the current color and     * a specified XOR color.     * <p>     * When drawing operations are performed, pixels which are the     * current color are changed to the specified color, and vice versa.     * <p>     * Pixels that are of colors other than those two colors are changed     * in an unpredictable but reversible manner; if the same figure is     * drawn twice, then all pixels are restored to their original values.     * @param     c1 the XOR alternation color     * @since     JDK1.0     */    public void setXORMode(Color c1) {        mGraphics.setXORMode(c1);    }     /**     * Gets the current font.     * @return    this graphics context's current font.     * @see       java.awt.Font     * @see       java.awt.Graphics#setFont     * @since     JDK1.0     */    public Font getFont() {        return mGraphics.getFont();    }     /**     * Sets this graphics context's font to the specified font.     * All subsequent text operations using this graphics context     * use this font.     * @param  font   the font.     * @see     java.awt.Graphics#getFont     * @see     java.awt.Graphics#drawChars(java.lang.String, int, int)     * @see     java.awt.Graphics#drawString(byte[], int, int, int, int)     * @see     java.awt.Graphics#drawBytes(char[], int, int, int, int)     * @since   JDK1.0    */    public void setFont(Font font) {        mGraphics.setFont(font);    }     /**     * Gets the font metrics for the specified font.     * @return    the font metrics for the specified font.     * @param     f the specified font     * @see       java.awt.Graphics#getFont     * @see       java.awt.FontMetrics     * @see       java.awt.Graphics#getFontMetrics()     * @since     JDK1.0     */    public FontMetrics getFontMetrics(Font f) {        return mGraphics.getFontMetrics(f);    }     /**    * Get the rendering context of the font    * within this Graphics2D context.    */    public FontRenderContext getFontRenderContext() {        return mGraphics.getFontRenderContext();    }     /**     * Returns the bounding rectangle of the current clipping area.     * The coordinates in the rectangle are relative to the coordinate     * system origin of this graphics context.     * @return      the bounding rectangle of the current clipping area.     * @see         java.awt.Graphics#getClip     * @see         java.awt.Graphics#clipRect     * @see         java.awt.Graphics#setClip(int, int, int, int)     * @see         java.awt.Graphics#setClip(Shape)     * @since       JDK1.1     */    public Rectangle getClipBounds() {        return mGraphics.getClipBounds();    }      /**     * Intersects the current clip with the specified rectangle.     * The resulting clipping area is the intersection of the current     * clipping area and the specified rectangle.     * This method can only be used to make the current clip smaller.     * To set the current clip larger, use any of the setClip methods.     * Rendering operations have no effect outside of the clipping area.     * @param x the x coordinate of the rectangle to intersect the clip with     * @param y the y coordinate of the rectangle to intersect the clip with     * @param width the width of the rectangle to intersect the clip with     * @param height the height of the rectangle to intersect the clip with     * @see #setClip(int, int, int, int)     * @see #setClip(Shape)     */    public void clipRect(int x, int y, int width, int height) {        mGraphics.clipRect(x, y, width, height);    }      /**     * Sets the current clip to the rectangle specified by the given     * coordinates.     * Rendering operations have no effect outside of the clipping area.     * @param       x the <i>x</i> coordinate of the new clip rectangle.     * @param       y the <i>y</i> coordinate of the new clip rectangle.     * @param       width the width of the new clip rectangle.     * @param       height the height of the new clip rectangle.     * @see         java.awt.Graphics#clipRect     * @see         java.awt.Graphics#setClip(Shape)     * @since       JDK1.1     */    public void setClip(int x, int y, int width, int height) {        mGraphics.setClip(x, y, width, height);    }     /**     * Gets the current clipping area.     * @return      a <code>Shape</code> object representing the     *                      current clipping area.     * @see         java.awt.Graphics#getClipBounds     * @see         java.awt.Graphics#clipRect     * @see         java.awt.Graphics#setClip(int, int, int, int)     * @see         java.awt.Graphics#setClip(Shape)     * @since       JDK1.1     */    public Shape getClip() {        return mGraphics.getClip();    }      /**     * Sets the current clipping area to an arbitrary clip shape.     * Not all objects which implement the <code>Shape</code>     * interface can be used to set the clip.  The only     * <code>Shape</code> objects which are guaranteed to be     * supported are <code>Shape</code> objects which are     * obtained via the <code>getClip</code> method and via     * <code>Rectangle</code> objects.     * @see         java.awt.Graphics#getClip()     * @see         java.awt.Graphics#clipRect     * @see         java.awt.Graphics#setClip(int, int, int, int)     * @since       JDK1.1     */    public void setClip(Shape clip) {        mGraphics.setClip(clip);    }      /**     * Copies an area of the component by a distance specified by     * <code>dx</code> and <code>dy</code>. From the point specified     * by <code>x</code> and <code>y</code>, this method     * copies downwards and to the right.  To copy an area of the     * component to the left or upwards, specify a negative value for     * <code>dx</code> or <code>dy</code>.     * If a portion of the source rectangle lies outside the bounds     * of the component, or is obscured by another window or component,     * <code>copyArea</code> will be unable to copy the associated     * pixels. The area that is omitted can be refreshed by calling     * the component's <code>paint</code> method.     * @param       x the <i>x</i> coordinate of the source rectangle.     * @param       y the <i>y</i> coordinate of the source rectangle.     * @param       width the width of the source rectangle.     * @param       height the height of the source rectangle.     * @param       dx the horizontal distance to copy the pixels.     * @param       dy the vertical distance to copy the pixels.     * @since       JDK1.0     */    public void copyArea(int x, int y, int width, int height,                         int dx, int dy) {        // This method is not supported for printing so we do nothing here.

Lines 1–500 of 1918.