1896 lines · 75.9 KB · Java
RawDownload
/* * Copyright (c) 1998, 2007, 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.lang.ref.SoftReference;import java.util.Hashtable;import sun.font.CharToGlyphMapper;import sun.font.CompositeFont;import sun.font.Font2D;import sun.font.Font2DHandle;import sun.font.FontManager;import sun.font.FontManagerFactory;import sun.font.FontUtilities; import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.awt.Image;import java.awt.Paint;import java.awt.Polygon;import java.awt.Shape; import java.text.AttributedCharacterIterator; import java.awt.font.FontRenderContext;import java.awt.font.GlyphVector;import java.awt.font.TextAttribute;import java.awt.font.TextLayout; import java.awt.geom.AffineTransform;import java.awt.geom.Arc2D;import java.awt.geom.Ellipse2D;import java.awt.geom.Line2D;import java.awt.geom.Point2D;import java.awt.geom.Rectangle2D;import java.awt.geom.RoundRectangle2D;import java.awt.geom.PathIterator; import java.awt.image.BufferedImage;import java.awt.image.BufferedImageOp;import java.awt.image.ColorModel;import java.awt.image.DataBuffer;import java.awt.image.DataBufferInt;import java.awt.image.ImageObserver;import java.awt.image.IndexColorModel;import java.awt.image.Raster;import java.awt.image.RenderedImage;import java.awt.image.SampleModel;import java.awt.image.SinglePixelPackedSampleModel;import java.awt.image.VolatileImage;import sun.awt.image.ByteComponentRaster;import sun.awt.image.ToolkitImage;import sun.awt.image.SunWritableRaster; import java.awt.print.PageFormat;import java.awt.print.Printable;import java.awt.print.PrinterException;import java.awt.print.PrinterGraphics;import java.awt.print.PrinterJob; import java.util.Map; public abstract class PathGraphics extends ProxyGraphics2D {     private Printable mPainter;    private PageFormat mPageFormat;    private int mPageIndex;    private boolean mCanRedraw;    protected boolean printingGlyphVector;     protected PathGraphics(Graphics2D graphics, PrinterJob printerJob,                           Printable painter, PageFormat pageFormat,                           int pageIndex, boolean canRedraw) {        super(graphics, printerJob);         mPainter = painter;        mPageFormat = pageFormat;        mPageIndex = pageIndex;        mCanRedraw = canRedraw;    }     /**     * Return the Printable instance responsible for drawing     * into this Graphics.     */    protected Printable getPrintable() {        return mPainter;    }     /**     * Return the PageFormat associated with this page of     * Graphics.     */    protected PageFormat getPageFormat() {        return mPageFormat;    }     /**     * Return the page index associated with this Graphics.     */    protected int getPageIndex() {        return mPageIndex;    }     /**     * Return true if we are allowed to ask the application     * to redraw portions of the page. In general, with the     * PrinterJob API, the application can be asked to do a     * redraw. When PrinterJob is emulating PrintJob then we     * can not.     */    public boolean canDoRedraws() {        return mCanRedraw;    }      /**      * Redraw a rectanglular area using a proxy graphics      */    public abstract void redrawRegion(Rectangle2D region,                                      double scaleX, double scaleY,                                      Shape clip,                                      AffineTransform devTransform)                     throws PrinterException ;     /**     * Draws a line, using the current color, between the points     * <code>(x1,&nbsp;y1)</code> and <code>(x2,&nbsp;y2)</code>     * in this graphics context's coordinate system.     * @param   x1  the first point's <i>x</i> coordinate.     * @param   y1  the first point's <i>y</i> coordinate.     * @param   x2  the second point's <i>x</i> coordinate.     * @param   y2  the second point's <i>y</i> coordinate.     */    public void drawLine(int x1, int y1, int x2, int y2) {         Paint paint = getPaint();         try {            AffineTransform deviceTransform = getTransform();            if (getClip() != null) {                deviceClip(getClip().getPathIterator(deviceTransform));            }             deviceDrawLine(x1, y1, x2, y2, (Color) paint);         } catch (ClassCastException e) {            throw new IllegalArgumentException("Expected a Color instance");        }    }      /**     * Draws the outline of the specified rectangle.     * The left and right edges of the rectangle are at     * <code>x</code> and <code>x&nbsp;+&nbsp;width</code>.     * The top and bottom edges are at     * <code>y</code> and <code>y&nbsp;+&nbsp;height</code>.     * The rectangle is drawn using the graphics context's current color.     * @param         x   the <i>x</i> coordinate     *                         of the rectangle to be drawn.     * @param         y   the <i>y</i> coordinate     *                         of the rectangle to be drawn.     * @param         width   the width of the rectangle to be drawn.     * @param         height   the height of the rectangle to be drawn.     * @see          java.awt.Graphics#fillRect     * @see          java.awt.Graphics#clearRect     */    public void drawRect(int x, int y, int width, int height) {         Paint paint = getPaint();         try {            AffineTransform deviceTransform = getTransform();            if (getClip() != null) {                deviceClip(getClip().getPathIterator(deviceTransform));            }             deviceFrameRect(x, y, width, height, (Color) paint);         } catch (ClassCastException e) {            throw new IllegalArgumentException("Expected a Color instance");        }     }     /**     * Fills the specified rectangle.     * The left and right edges of the rectangle are at     * <code>x</code> and <code>x&nbsp;+&nbsp;width&nbsp;-&nbsp;1</code>.     * The top and bottom edges are at     * <code>y</code> and <code>y&nbsp;+&nbsp;height&nbsp;-&nbsp;1</code>.     * The resulting rectangle covers an area     * <code>width</code> pixels wide by     * <code>height</code> pixels tall.     * The rectangle is filled using the graphics context's current color.     * @param         x   the <i>x</i> coordinate     *                         of the rectangle to be filled.     * @param         y   the <i>y</i> coordinate     *                         of the rectangle to be filled.     * @param         width   the width of the rectangle to be filled.     * @param         height   the height of the rectangle to be filled.     * @see           java.awt.Graphics#clearRect     * @see           java.awt.Graphics#drawRect     */    public void fillRect(int x, int y, int width, int height){         Paint paint = getPaint();         try {            AffineTransform deviceTransform = getTransform();            if (getClip() != null) {                deviceClip(getClip().getPathIterator(deviceTransform));            }             deviceFillRect(x, y, width, height, (Color) paint);         } catch (ClassCastException e) {            throw new IllegalArgumentException("Expected a Color instance");        }    }        /**     * Clears the specified rectangle by filling it with the background     * color of the current drawing surface. This operation does not     * use the current paint mode.     * <p>     * Beginning with Java&nbsp;1.1, the background color     * of offscreen images may be system dependent. Applications should     * use <code>setColor</code> followed by <code>fillRect</code> to     * ensure that an offscreen image is cleared to a specific color.     * @param       x the <i>x</i> coordinate of the rectangle to clear.     * @param       y the <i>y</i> coordinate of the rectangle to clear.     * @param       width the width of the rectangle to clear.     * @param       height the height of the rectangle to clear.     * @see         java.awt.Graphics#fillRect(int, int, int, int)     * @see         java.awt.Graphics#drawRect     * @see         java.awt.Graphics#setColor(java.awt.Color)     * @see         java.awt.Graphics#setPaintMode     * @see         java.awt.Graphics#setXORMode(java.awt.Color)     */    public void clearRect(int x, int y, int width, int height) {         fill(new Rectangle2D.Float(x, y, width, height), getBackground());    }         /**     * Draws an outlined round-cornered rectangle using this graphics     * context's current color. The left and right edges of the rectangle     * are at <code>x</code> and <code>x&nbsp;+&nbsp;width</code>,     * respectively. The top and bottom edges of the rectangle are at     * <code>y</code> and <code>y&nbsp;+&nbsp;height</code>.     * @param      x the <i>x</i> coordinate of the rectangle to be drawn.     * @param      y the <i>y</i> coordinate of the rectangle to be drawn.     * @param      width the width of the rectangle to be drawn.     * @param      height the height of the rectangle to be drawn.     * @param      arcWidth the horizontal diameter of the arc     *                    at the four corners.     * @param      arcHeight the vertical diameter of the arc     *                    at the four corners.     * @see        java.awt.Graphics#fillRoundRect     */    public void drawRoundRect(int x, int y, int width, int height,                              int arcWidth, int arcHeight) {         draw(new RoundRectangle2D.Float(x, y,                                        width, height,                                        arcWidth, arcHeight));    }      /**     * Fills the specified rounded corner rectangle with the current color.     * The left and right edges of the rectangle     * are at <code>x</code> and <code>x&nbsp;+&nbsp;width&nbsp;-&nbsp;1</code>,     * respectively. The top and bottom edges of the rectangle are at     * <code>y</code> and <code>y&nbsp;+&nbsp;height&nbsp;-&nbsp;1</code>.     * @param       x the <i>x</i> coordinate of the rectangle to be filled.     * @param       y the <i>y</i> coordinate of the rectangle to be filled.     * @param       width the width of the rectangle to be filled.     * @param       height the height of the rectangle to be filled.     * @param       arcWidth the horizontal diameter     *                     of the arc at the four corners.     * @param       arcHeight the vertical diameter     *                     of the arc at the four corners.     * @see         java.awt.Graphics#drawRoundRect     */    public void fillRoundRect(int x, int y, int width, int height,                              int arcWidth, int arcHeight) {         fill(new RoundRectangle2D.Float(x, y,                                        width, height,                                        arcWidth, arcHeight));    }     /**     * Draws the outline of an oval.     * The result is a circle or ellipse that fits within the     * rectangle specified by the <code>x</code>, <code>y</code>,     * <code>width</code>, and <code>height</code> arguments.     * <p>     * The oval covers an area that is     * <code>width&nbsp;+&nbsp;1</code> pixels wide     * and <code>height&nbsp;+&nbsp;1</code> pixels tall.     * @param       x the <i>x</i> coordinate of the upper left     *                     corner of the oval to be drawn.     * @param       y the <i>y</i> coordinate of the upper left     *                     corner of the oval to be drawn.     * @param       width the width of the oval to be drawn.     * @param       height the height of the oval to be drawn.     * @see         java.awt.Graphics#fillOval     * @since       JDK1.0     */    public void drawOval(int x, int y, int width, int height) {        draw(new Ellipse2D.Float(x, y, width, height));    }         /**     * Fills an oval bounded by the specified rectangle with the     * current color.     * @param       x the <i>x</i> coordinate of the upper left corner     *                     of the oval to be filled.     * @param       y the <i>y</i> coordinate of the upper left corner     *                     of the oval to be filled.     * @param       width the width of the oval to be filled.     * @param       height the height of the oval to be filled.     * @see         java.awt.Graphics#drawOval     */    public void fillOval(int x, int y, int width, int height){         fill(new Ellipse2D.Float(x, y, width, height));    }     /**     * Draws the outline of a circular or elliptical arc     * covering the specified rectangle.     * <p>     * The resulting arc begins at <code>startAngle</code> and extends     * for <code>arcAngle</code> degrees, using the current color.     * Angles are interpreted such that 0&nbsp;degrees     * is at the 3&nbsp;o'clock position.     * A positive value indicates a counter-clockwise rotation     * while a negative value indicates a clockwise rotation.     * <p>     * The center of the arc is the center of the rectangle whose origin     * is (<i>x</i>,&nbsp;<i>y</i>) and whose size is specified by the     * <code>width</code> and <code>height</code> arguments.     * <p>     * The resulting arc covers an area     * <code>width&nbsp;+&nbsp;1</code> pixels wide     * by <code>height&nbsp;+&nbsp;1</code> pixels tall.     * <p>     * The angles are specified relative to the non-square extents of     * the bounding rectangle such that 45 degrees always falls on the     * line from the center of the ellipse to the upper right corner of     * the bounding rectangle. As a result, if the bounding rectangle is     * noticeably longer in one axis than the other, the angles to the     * start and end of the arc segment will be skewed farther along the     * longer axis of the bounds.     * @param        x the <i>x</i> coordinate of the     *                    upper-left corner of the arc to be drawn.     * @param        y the <i>y</i>  coordinate of the     *                    upper-left corner of the arc to be drawn.     * @param        width the width of the arc to be drawn.     * @param        height the height of the arc to be drawn.     * @param        startAngle the beginning angle.     * @param        arcAngle the angular extent of the arc,     *                    relative to the start angle.     * @see         java.awt.Graphics#fillArc     */    public void drawArc(int x, int y, int width, int height,                                 int startAngle, int arcAngle) {        draw(new Arc2D.Float(x, y, width, height,                             startAngle, arcAngle,                             Arc2D.OPEN));    }      /**     * Fills a circular or elliptical arc covering the specified rectangle.     * <p>     * The resulting arc begins at <code>startAngle</code> and extends     * for <code>arcAngle</code> degrees.     * Angles are interpreted such that 0&nbsp;degrees     * is at the 3&nbsp;o'clock position.     * A positive value indicates a counter-clockwise rotation     * while a negative value indicates a clockwise rotation.     * <p>     * The center of the arc is the center of the rectangle whose origin     * is (<i>x</i>,&nbsp;<i>y</i>) and whose size is specified by the     * <code>width</code> and <code>height</code> arguments.     * <p>     * The resulting arc covers an area     * <code>width&nbsp;+&nbsp;1</code> pixels wide     * by <code>height&nbsp;+&nbsp;1</code> pixels tall.     * <p>     * The angles are specified relative to the non-square extents of     * the bounding rectangle such that 45 degrees always falls on the     * line from the center of the ellipse to the upper right corner of     * the bounding rectangle. As a result, if the bounding rectangle is     * noticeably longer in one axis than the other, the angles to the     * start and end of the arc segment will be skewed farther along the     * longer axis of the bounds.     * @param        x the <i>x</i> coordinate of the     *                    upper-left corner of the arc to be filled.     * @param        y the <i>y</i>  coordinate of the     *                    upper-left corner of the arc to be filled.     * @param        width the width of the arc to be filled.     * @param        height the height of the arc to be filled.     * @param        startAngle the beginning angle.     * @param        arcAngle the angular extent of the arc,     *                    relative to the start angle.     * @see         java.awt.Graphics#drawArc     */    public void fillArc(int x, int y, int width, int height,                                 int startAngle, int arcAngle) {         fill(new Arc2D.Float(x, y, width, height,                             startAngle, arcAngle,                             Arc2D.PIE));    }     /**     * Draws a sequence of connected lines defined by     * arrays of <i>x</i> and <i>y</i> coordinates.     * Each pair of (<i>x</i>,&nbsp;<i>y</i>) coordinates defines a point.     * The figure is not closed if the first point     * differs from the last point.     * @param       xPoints an array of <i>x</i> points     * @param       yPoints an array of <i>y</i> points     * @param       nPoints the total number of points     * @see         java.awt.Graphics#drawPolygon(int[], int[], int)     * @since       JDK1.1     */    public void drawPolyline(int xPoints[], int yPoints[],                             int nPoints) {        float fromX;        float fromY;        float toX;        float toY;         if (nPoints > 0) {            fromX = xPoints[0];            fromY = yPoints[0];            for(int i = 1; i < nPoints; i++) {                toX = xPoints[i];                toY = yPoints[i];                draw(new Line2D.Float(fromX, fromY, toX, toY));                fromX = toX;                fromY = toY;            }        }     }      /**     * Draws a closed polygon defined by     * arrays of <i>x</i> and <i>y</i> coordinates.     * Each pair of (<i>x</i>,&nbsp;<i>y</i>) coordinates defines a point.     * <p>     * This method draws the polygon defined by <code>nPoint</code> line     * segments, where the first <code>nPoint&nbsp;-&nbsp;1</code>     * line segments are line segments from     * <code>(xPoints[i&nbsp;-&nbsp;1],&nbsp;yPoints[i&nbsp;-&nbsp;1])</code>     * to <code>(xPoints[i],&nbsp;yPoints[i])</code>, for     * 1&nbsp;&le;&nbsp;<i>i</i>&nbsp;&le;&nbsp;<code>nPoints</code>.     * The figure is automatically closed by drawing a line connecting     * the final point to the first point, if those points are different.     * @param        xPoints   a an array of <code>x</code> coordinates.     * @param        yPoints   a an array of <code>y</code> coordinates.     * @param        nPoints   a the total number of points.     * @see          java.awt.Graphics#fillPolygon     * @see          java.awt.Graphics#drawPolyline     */    public void drawPolygon(int xPoints[], int yPoints[],                                     int nPoints) {

Lines 1–500 of 1896.