src/share/classes/sun/java2d/SunGraphics2D.java

Print this page


   1 /*
   2  * Copyright (c) 1996, 2008, 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


  65 import java.awt.Transparency;
  66 import java.awt.font.GlyphVector;
  67 import java.awt.font.TextLayout;
  68 import sun.font.FontDesignMetrics;
  69 import sun.font.FontUtilities;
  70 import sun.java2d.pipe.PixelDrawPipe;
  71 import sun.java2d.pipe.PixelFillPipe;
  72 import sun.java2d.pipe.ShapeDrawPipe;
  73 import sun.java2d.pipe.ValidatePipe;
  74 import sun.java2d.pipe.ShapeSpanIterator;
  75 import sun.java2d.pipe.Region;
  76 import sun.java2d.pipe.TextPipe;
  77 import sun.java2d.pipe.DrawImagePipe;
  78 import sun.java2d.pipe.LoopPipe;
  79 import sun.java2d.loops.FontInfo;
  80 import sun.java2d.loops.RenderLoops;
  81 import sun.java2d.loops.CompositeType;
  82 import sun.java2d.loops.SurfaceType;
  83 import sun.java2d.loops.Blit;
  84 import sun.java2d.loops.MaskFill;
  85 import sun.font.FontManager;
  86 import java.awt.font.FontRenderContext;
  87 import sun.java2d.loops.XORComposite;
  88 import sun.awt.ConstrainableGraphics;
  89 import sun.awt.SunHints;
  90 import java.util.Map;
  91 import java.util.Iterator;
  92 import sun.java2d.DestSurfaceProvider;
  93 import sun.misc.PerformanceLogger;
  94 
  95 import javax.tools.annotation.GenerateNativeHeader;
  96 
  97 /**
  98  * This is a the master Graphics2D superclass for all of the Sun
  99  * Graphics implementations.  This class relies on subclasses to
 100  * manage the various device information, but provides an overall
 101  * general framework for performing all of the requests in the
 102  * Graphics and Graphics2D APIs.
 103  *
 104  * @author Jim Graham
 105  */
 106 /* No native methods here, but the constants are needed in the supporting JNI code */
 107 @GenerateNativeHeader
 108 public final class SunGraphics2D
 109     extends Graphics2D
 110     implements ConstrainableGraphics, Cloneable, DestSurfaceProvider
 111 {
 112     /*


1732     public Color getBackground() {
1733         return backgroundColor;
1734     }
1735 
1736     /**
1737      * Returns the current Stroke in the Graphics2D state.
1738      * @see setStroke
1739      */
1740     public Stroke getStroke() {
1741         return stroke;
1742     }
1743 
1744     public Rectangle getClipBounds() {
1745         Rectangle r;
1746         if (clipState == CLIP_DEVICE) {
1747             r = null;
1748         } else if (transformState <= TRANSFORM_INT_TRANSLATE) {
1749             if (usrClip instanceof Rectangle) {
1750                 r = new Rectangle((Rectangle) usrClip);
1751             } else {
1752                 r = usrClip.getBounds();

1753             }
1754             r.translate(-transX, -transY);
1755         } else {
1756             r = getClip().getBounds();

1757         }
1758         return r;
1759     }
1760 
1761     public Rectangle getClipBounds(Rectangle r) {
1762         if (clipState != CLIP_DEVICE) {
1763             if (transformState <= TRANSFORM_INT_TRANSLATE) {
1764                 if (usrClip instanceof Rectangle) {
1765                     r.setBounds((Rectangle) usrClip);
1766                 } else {
1767                     r.setBounds(usrClip.getBounds());
1768                 }
1769                 r.translate(-transX, -transY);
1770             } else {
1771                 r.setBounds(getClip().getBounds());
1772             }
1773         } else if (r == null) {
1774             throw new NullPointerException("null rectangle parameter");
1775         }
1776         return r;
1777     }
1778 
1779     public boolean hitClip(int x, int y, int width, int height) {
1780         if (width <= 0 || height <= 0) {
1781             return false;
1782         }
1783         if (transformState > TRANSFORM_INT_TRANSLATE) {
1784             // Note: Technically the most accurate test would be to
1785             // raster scan the parallelogram of the transformed rectangle
1786             // and do a span for span hit test against the clip, but for
1787             // speed we approximate the test with a bounding box of the
1788             // transformed rectangle.  The cost of rasterizing the
1789             // transformed rectangle is probably high enough that it is
1790             // not worth doing so to save the caller from having to call
1791             // a rendering method where we will end up discovering the


1926 
1927         AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
1928         return mat.createTransformedShape(s);
1929     }
1930 
1931     protected static Shape transformShape(AffineTransform tx, Shape clip) {
1932         if (clip == null) {
1933             return null;
1934         }
1935 
1936         if (clip instanceof Rectangle2D &&
1937             (tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)
1938         {
1939             Rectangle2D rect = (Rectangle2D) clip;
1940             double matrix[] = new double[4];
1941             matrix[0] = rect.getX();
1942             matrix[1] = rect.getY();
1943             matrix[2] = matrix[0] + rect.getWidth();
1944             matrix[3] = matrix[1] + rect.getHeight();
1945             tx.transform(matrix, 0, matrix, 0, 2);
1946             rect = new Rectangle2D.Float();
1947             rect.setFrameFromDiagonal(matrix[0], matrix[1],
1948                                       matrix[2], matrix[3]);
1949             return rect;
1950         }
1951 
1952         if (tx.isIdentity()) {
1953             return cloneShape(clip);
1954         }
1955 
1956         return tx.createTransformedShape(clip);
















1957     }
1958 
1959     public void clipRect(int x, int y, int w, int h) {
1960         clip(new Rectangle(x, y, w, h));
1961     }
1962 
1963     public void setClip(int x, int y, int w, int h) {
1964         setClip(new Rectangle(x, y, w, h));
1965     }
1966 
1967     public Shape getClip() {
1968         return untransformShape(usrClip);
1969     }
1970 
1971     public void setClip(Shape sh) {
1972         usrClip = transformShape(sh);
1973         validateCompClip();
1974     }
1975 
1976     /**


   1 /*
   2  * Copyright (c) 1996, 2013, 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


  65 import java.awt.Transparency;
  66 import java.awt.font.GlyphVector;
  67 import java.awt.font.TextLayout;
  68 import sun.font.FontDesignMetrics;
  69 import sun.font.FontUtilities;
  70 import sun.java2d.pipe.PixelDrawPipe;
  71 import sun.java2d.pipe.PixelFillPipe;
  72 import sun.java2d.pipe.ShapeDrawPipe;
  73 import sun.java2d.pipe.ValidatePipe;
  74 import sun.java2d.pipe.ShapeSpanIterator;
  75 import sun.java2d.pipe.Region;
  76 import sun.java2d.pipe.TextPipe;
  77 import sun.java2d.pipe.DrawImagePipe;
  78 import sun.java2d.pipe.LoopPipe;
  79 import sun.java2d.loops.FontInfo;
  80 import sun.java2d.loops.RenderLoops;
  81 import sun.java2d.loops.CompositeType;
  82 import sun.java2d.loops.SurfaceType;
  83 import sun.java2d.loops.Blit;
  84 import sun.java2d.loops.MaskFill;

  85 import java.awt.font.FontRenderContext;
  86 import sun.java2d.loops.XORComposite;
  87 import sun.awt.ConstrainableGraphics;
  88 import sun.awt.SunHints;
  89 import java.util.Map;
  90 import java.util.Iterator;

  91 import sun.misc.PerformanceLogger;
  92 
  93 import javax.tools.annotation.GenerateNativeHeader;
  94 
  95 /**
  96  * This is a the master Graphics2D superclass for all of the Sun
  97  * Graphics implementations.  This class relies on subclasses to
  98  * manage the various device information, but provides an overall
  99  * general framework for performing all of the requests in the
 100  * Graphics and Graphics2D APIs.
 101  *
 102  * @author Jim Graham
 103  */
 104 /* No native methods here, but the constants are needed in the supporting JNI code */
 105 @GenerateNativeHeader
 106 public final class SunGraphics2D
 107     extends Graphics2D
 108     implements ConstrainableGraphics, Cloneable, DestSurfaceProvider
 109 {
 110     /*


1730     public Color getBackground() {
1731         return backgroundColor;
1732     }
1733 
1734     /**
1735      * Returns the current Stroke in the Graphics2D state.
1736      * @see setStroke
1737      */
1738     public Stroke getStroke() {
1739         return stroke;
1740     }
1741 
1742     public Rectangle getClipBounds() {
1743         Rectangle r;
1744         if (clipState == CLIP_DEVICE) {
1745             r = null;
1746         } else if (transformState <= TRANSFORM_INT_TRANSLATE) {
1747             if (usrClip instanceof Rectangle) {
1748                 r = new Rectangle((Rectangle) usrClip);
1749             } else {
1750                 r = new Rectangle();
1751                 r.setFrame(usrClip.getBounds2D());
1752             }
1753             r.translate(-transX, -transY);
1754         } else {
1755             r = new Rectangle();
1756             r.setFrame(getClip().getBounds2D());
1757         }
1758         return r;
1759     }
1760 
1761     public Rectangle getClipBounds(Rectangle r) {
1762         if (clipState != CLIP_DEVICE) {
1763             if (transformState <= TRANSFORM_INT_TRANSLATE) {
1764                 if (usrClip instanceof Rectangle) {
1765                     r.setBounds((Rectangle) usrClip);
1766                 } else {
1767                     r.setFrame(usrClip.getBounds2D());
1768                 }
1769                 r.translate(-transX, -transY);
1770             } else {
1771                 r.setFrame(getClip().getBounds2D());
1772             }
1773         } else if (r == null) {
1774             throw new NullPointerException("null rectangle parameter");
1775         }
1776         return r;
1777     }
1778 
1779     public boolean hitClip(int x, int y, int width, int height) {
1780         if (width <= 0 || height <= 0) {
1781             return false;
1782         }
1783         if (transformState > TRANSFORM_INT_TRANSLATE) {
1784             // Note: Technically the most accurate test would be to
1785             // raster scan the parallelogram of the transformed rectangle
1786             // and do a span for span hit test against the clip, but for
1787             // speed we approximate the test with a bounding box of the
1788             // transformed rectangle.  The cost of rasterizing the
1789             // transformed rectangle is probably high enough that it is
1790             // not worth doing so to save the caller from having to call
1791             // a rendering method where we will end up discovering the


1926 
1927         AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
1928         return mat.createTransformedShape(s);
1929     }
1930 
1931     protected static Shape transformShape(AffineTransform tx, Shape clip) {
1932         if (clip == null) {
1933             return null;
1934         }
1935 
1936         if (clip instanceof Rectangle2D &&
1937             (tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)
1938         {
1939             Rectangle2D rect = (Rectangle2D) clip;
1940             double matrix[] = new double[4];
1941             matrix[0] = rect.getX();
1942             matrix[1] = rect.getY();
1943             matrix[2] = matrix[0] + rect.getWidth();
1944             matrix[3] = matrix[1] + rect.getHeight();
1945             tx.transform(matrix, 0, matrix, 0, 2);
1946             fixRectangleOrientation(matrix, rect);
1947             return new Rectangle2D.Double(matrix[0], matrix[1],
1948                                           matrix[2] - matrix[0],
1949                                           matrix[3] - matrix[1]);
1950         }
1951 
1952         if (tx.isIdentity()) {
1953             return cloneShape(clip);
1954         }
1955 
1956         return tx.createTransformedShape(clip);
1957     }
1958 
1959     /**
1960      * Sets orientation of the rectangle according to the clip.
1961      */
1962     private static void fixRectangleOrientation(double[] m, Rectangle2D clip) {
1963         if (clip.getWidth() > 0 != (m[2] - m[0] > 0)) {
1964             double t = m[0];
1965             m[0] = m[2];
1966             m[2] = t;
1967         }
1968         if (clip.getHeight() > 0 != (m[3] - m[1] > 0)) {
1969             double t = m[1];
1970             m[1] = m[3];
1971             m[3] = t;
1972         }
1973     }
1974 
1975     public void clipRect(int x, int y, int w, int h) {
1976         clip(new Rectangle(x, y, w, h));
1977     }
1978 
1979     public void setClip(int x, int y, int w, int h) {
1980         setClip(new Rectangle(x, y, w, h));
1981     }
1982 
1983     public Shape getClip() {
1984         return untransformShape(usrClip);
1985     }
1986 
1987     public void setClip(Shape sh) {
1988         usrClip = transformShape(sh);
1989         validateCompClip();
1990     }
1991 
1992     /**