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
  23  * questions.
  24  */
  25 
  26 package sun.java2d;
  27 
  28 import java.awt.Graphics;
  29 import java.awt.Graphics2D;
  30 import java.awt.RenderingHints;
  31 import java.awt.RenderingHints.Key;
  32 import java.awt.geom.Area;
  33 import java.awt.geom.AffineTransform;
  34 import java.awt.geom.NoninvertibleTransformException;
  35 import java.awt.AlphaComposite;
  36 import java.awt.BasicStroke;
  37 import java.awt.image.BufferedImage;
  38 import java.awt.image.BufferedImageOp;
  39 import java.awt.image.RenderedImage;
  40 import java.awt.image.renderable.RenderableImage;
  41 import java.awt.image.renderable.RenderContext;
  42 import java.awt.image.AffineTransformOp;
  43 import java.awt.image.Raster;
  44 import java.awt.image.WritableRaster;
  45 import java.awt.Image;
  46 import java.awt.Composite;
  47 import java.awt.Color;
  48 import java.awt.image.ColorModel;
  49 import java.awt.GraphicsConfiguration;
  50 import java.awt.Paint;
  51 import java.awt.GradientPaint;
  52 import java.awt.LinearGradientPaint;
  53 import java.awt.RadialGradientPaint;
  54 import java.awt.TexturePaint;
  55 import java.awt.geom.Rectangle2D;
  56 import java.awt.geom.PathIterator;
  57 import java.awt.geom.GeneralPath;
  58 import java.awt.Shape;
  59 import java.awt.Stroke;
  60 import java.awt.FontMetrics;
  61 import java.awt.Rectangle;
  62 import java.text.AttributedCharacterIterator;
  63 import java.awt.Font;
  64 import java.awt.image.ImageObserver;
  65 import java.awt.Transparency;
  66 import java.awt.font.GlyphVector;
  67 import java.awt.font.TextLayout;
  68 
  69 import sun.awt.image.SurfaceManager;
  70 import sun.font.FontDesignMetrics;
  71 import sun.font.FontUtilities;
  72 import sun.java2d.pipe.PixelDrawPipe;
  73 import sun.java2d.pipe.PixelFillPipe;
  74 import sun.java2d.pipe.ShapeDrawPipe;
  75 import sun.java2d.pipe.ValidatePipe;
  76 import sun.java2d.pipe.ShapeSpanIterator;
  77 import sun.java2d.pipe.Region;
  78 import sun.java2d.pipe.TextPipe;
  79 import sun.java2d.pipe.DrawImagePipe;
  80 import sun.java2d.pipe.LoopPipe;
  81 import sun.java2d.loops.FontInfo;
  82 import sun.java2d.loops.RenderLoops;
  83 import sun.java2d.loops.CompositeType;
  84 import sun.java2d.loops.SurfaceType;
  85 import sun.java2d.loops.Blit;
  86 import sun.java2d.loops.MaskFill;
  87 import java.awt.font.FontRenderContext;
  88 import sun.java2d.loops.XORComposite;
  89 import sun.awt.ConstrainableGraphics;
  90 import sun.awt.SunHints;
  91 import java.util.Map;
  92 import java.util.Iterator;
  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     /*
 113      * Attribute States
 114      */
 115     /* Paint */
 116     public static final int PAINT_CUSTOM       = 6; /* Any other Paint object */
 117     public static final int PAINT_TEXTURE      = 5; /* Tiled Image */
 118     public static final int PAINT_RAD_GRADIENT = 4; /* Color RadialGradient */
 119     public static final int PAINT_LIN_GRADIENT = 3; /* Color LinearGradient */
 120     public static final int PAINT_GRADIENT     = 2; /* Color Gradient */
 121     public static final int PAINT_ALPHACOLOR   = 1; /* Non-opaque Color */
 122     public static final int PAINT_OPAQUECOLOR  = 0; /* Opaque Color */
 123 
 124     /* Composite*/
 125     public static final int COMP_CUSTOM = 3;/* Custom Composite */
 126     public static final int COMP_XOR    = 2;/* XOR Mode Composite */
 127     public static final int COMP_ALPHA  = 1;/* AlphaComposite */
 128     public static final int COMP_ISCOPY = 0;/* simple stores into destination,
 129                                              * i.e. Src, SrcOverNoEa, and other
 130                                              * alpha modes which replace
 131                                              * the destination.
 132                                              */
 133 
 134     /* Stroke */
 135     public static final int STROKE_CUSTOM = 3; /* custom Stroke */
 136     public static final int STROKE_WIDE   = 2; /* BasicStroke */
 137     public static final int STROKE_THINDASHED   = 1; /* BasicStroke */
 138     public static final int STROKE_THIN   = 0; /* BasicStroke */
 139 
 140     /* Transform */
 141     public static final int TRANSFORM_GENERIC = 4; /* any 3x2 */
 142     public static final int TRANSFORM_TRANSLATESCALE = 3; /* scale XY */
 143     public static final int TRANSFORM_ANY_TRANSLATE = 2; /* non-int translate */
 144     public static final int TRANSFORM_INT_TRANSLATE = 1; /* int translate */
 145     public static final int TRANSFORM_ISIDENT = 0; /* Identity */
 146 
 147     /* Clipping */
 148     public static final int CLIP_SHAPE       = 2; /* arbitrary clip */
 149     public static final int CLIP_RECTANGULAR = 1; /* rectangular clip */
 150     public static final int CLIP_DEVICE      = 0; /* no clipping set */
 151 
 152     /* The following fields are used when the current Paint is a Color. */
 153     public int eargb;  // ARGB value with ExtraAlpha baked in
 154     public int pixel;  // pixel value for eargb
 155 
 156     public SurfaceData surfaceData;
 157 
 158     public PixelDrawPipe drawpipe;
 159     public PixelFillPipe fillpipe;
 160     public DrawImagePipe imagepipe;
 161     public ShapeDrawPipe shapepipe;
 162     public TextPipe textpipe;
 163     public MaskFill alphafill;
 164 
 165     public RenderLoops loops;
 166 
 167     public CompositeType imageComp;     /* Image Transparency checked on fly */
 168 
 169     public int paintState;
 170     public int compositeState;
 171     public int strokeState;
 172     public int transformState;
 173     public int clipState;
 174 
 175     public Color foregroundColor;
 176     public Color backgroundColor;
 177 
 178     public AffineTransform transform;
 179     public int transX;
 180     public int transY;
 181 
 182     protected static final Stroke defaultStroke = new BasicStroke();
 183     protected static final Composite defaultComposite = AlphaComposite.SrcOver;
 184     private static final Font defaultFont =
 185         new Font(Font.DIALOG, Font.PLAIN, 12);
 186 
 187     public Paint paint;
 188     public Stroke stroke;
 189     public Composite composite;
 190     protected Font font;
 191     protected FontMetrics fontMetrics;
 192 
 193     public int renderHint;
 194     public int antialiasHint;
 195     public int textAntialiasHint;
 196     protected int fractionalMetricsHint;
 197 
 198     /* A gamma adjustment to the colour used in lcd text blitting */
 199     public int lcdTextContrast;
 200     private static int lcdTextContrastDefaultValue = 140;
 201 
 202     private int interpolationHint;      // raw value of rendering Hint
 203     public int strokeHint;
 204 
 205     public int interpolationType;       // algorithm choice based on
 206                                         // interpolation and render Hints
 207 
 208     public RenderingHints hints;
 209 
 210     public Region constrainClip;        // lightweight bounds in pixels
 211     public int constrainX;
 212     public int constrainY;
 213 
 214     public Region clipRegion;
 215     public Shape usrClip;
 216     protected Region devClip;           // Actual physical drawable in pixels
 217 
 218     private final int devScale;         // Actual physical scale factor
 219 
 220     // cached state for text rendering
 221     private boolean validFontInfo;
 222     private FontInfo fontInfo;
 223     private FontInfo glyphVectorFontInfo;
 224     private FontRenderContext glyphVectorFRC;
 225 
 226     private final static int slowTextTransformMask =
 227                             AffineTransform.TYPE_GENERAL_TRANSFORM
 228                         |   AffineTransform.TYPE_MASK_ROTATION
 229                         |   AffineTransform.TYPE_FLIP;
 230 
 231     static {
 232         if (PerformanceLogger.loggingEnabled()) {
 233             PerformanceLogger.setTime("SunGraphics2D static initialization");
 234         }
 235     }
 236 
 237     public SunGraphics2D(SurfaceData sd, Color fg, Color bg, Font f) {
 238         surfaceData = sd;
 239         foregroundColor = fg;
 240         backgroundColor = bg;
 241 
 242         transform = new AffineTransform();
 243         stroke = defaultStroke;
 244         composite = defaultComposite;
 245         paint = foregroundColor;
 246 
 247         imageComp = CompositeType.SrcOverNoEa;
 248 
 249         renderHint = SunHints.INTVAL_RENDER_DEFAULT;
 250         antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;
 251         textAntialiasHint = SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT;
 252         fractionalMetricsHint = SunHints.INTVAL_FRACTIONALMETRICS_OFF;
 253         lcdTextContrast = lcdTextContrastDefaultValue;
 254         interpolationHint = -1;
 255         strokeHint = SunHints.INTVAL_STROKE_DEFAULT;
 256 
 257         interpolationType = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
 258 
 259         validateColor();
 260 
 261         devScale = sd.getDefaultScale();
 262         if (devScale != 1) {
 263             transform.setToScale(devScale, devScale);
 264             invalidateTransform();
 265         }
 266 
 267         font = f;
 268         if (font == null) {
 269             font = defaultFont;
 270         }
 271 
 272         setDevClip(sd.getBounds());
 273         invalidatePipe();
 274     }
 275 
 276     protected Object clone() {
 277         try {
 278             SunGraphics2D g = (SunGraphics2D) super.clone();
 279             g.transform = new AffineTransform(this.transform);
 280             if (hints != null) {
 281                 g.hints = (RenderingHints) this.hints.clone();
 282             }
 283             /* FontInfos are re-used, so must be cloned too, if they
 284              * are valid, and be nulled out if invalid.
 285              * The implied trade-off is that there is more to be gained
 286              * from re-using these objects than is lost by having to
 287              * clone them when the SG2D is cloned.
 288              */
 289             if (this.fontInfo != null) {
 290                 if (this.validFontInfo) {
 291                     g.fontInfo = (FontInfo)this.fontInfo.clone();
 292                 } else {
 293                     g.fontInfo = null;
 294                 }
 295             }
 296             if (this.glyphVectorFontInfo != null) {
 297                 g.glyphVectorFontInfo =
 298                     (FontInfo)this.glyphVectorFontInfo.clone();
 299                 g.glyphVectorFRC = this.glyphVectorFRC;
 300             }
 301             //g.invalidatePipe();
 302             return g;
 303         } catch (CloneNotSupportedException e) {
 304         }
 305         return null;
 306     }
 307 
 308     /**
 309      * Create a new SunGraphics2D based on this one.
 310      */
 311     public Graphics create() {
 312         return (Graphics) clone();
 313     }
 314 
 315     public void setDevClip(int x, int y, int w, int h) {
 316         Region c = constrainClip;
 317         if (c == null) {
 318             devClip = Region.getInstanceXYWH(x, y, w, h);
 319         } else {
 320             devClip = c.getIntersectionXYWH(x, y, w, h);
 321         }
 322         validateCompClip();
 323     }
 324 
 325     public void setDevClip(Rectangle r) {
 326         setDevClip(r.x, r.y, r.width, r.height);
 327     }
 328 
 329     /**
 330      * Constrain rendering for lightweight objects.
 331      */
 332     public void constrain(int x, int y, int w, int h, Region region) {
 333         if ((x | y) != 0) {
 334             translate(x, y);
 335         }
 336         if (transformState > TRANSFORM_TRANSLATESCALE) {
 337             clipRect(0, 0, w, h);
 338             return;
 339         }
 340         // changes parameters according to the current scale and translate.
 341         final double scaleX = transform.getScaleX();
 342         final double scaleY = transform.getScaleY();
 343         x = constrainX = (int) transform.getTranslateX();
 344         y = constrainY = (int) transform.getTranslateY();
 345         w = Region.dimAdd(x, Region.clipScale(w, scaleX));
 346         h = Region.dimAdd(y, Region.clipScale(h, scaleY));
 347 
 348         Region c = constrainClip;
 349         if (c == null) {
 350             c = Region.getInstanceXYXY(x, y, w, h);
 351         } else {
 352             c = c.getIntersectionXYXY(x, y, w, h);
 353         }
 354         if (region != null) {
 355             region = region.getScaledRegion(scaleX, scaleY);
 356             region = region.getTranslatedRegion(x, y);
 357             c = c.getIntersection(region);
 358         }
 359 
 360         if (c == constrainClip) {
 361             // Common case to ignore
 362             return;
 363         }
 364 
 365         constrainClip = c;
 366         if (!devClip.isInsideQuickCheck(c)) {
 367             devClip = devClip.getIntersection(c);
 368             validateCompClip();
 369         }
 370     }
 371 
 372     /**
 373      * Constrain rendering for lightweight objects.
 374      *
 375      * REMIND: This method will back off to the "workaround"
 376      * of using translate and clipRect if the Graphics
 377      * to be constrained has a complex transform.  The
 378      * drawback of the workaround is that the resulting
 379      * clip and device origin cannot be "enforced".
 380      *
 381      * @exception IllegalStateException If the Graphics
 382      * to be constrained has a complex transform.
 383      */
 384     @Override
 385     public void constrain(int x, int y, int w, int h) {
 386         constrain(x, y, w, h, null);
 387     }
 388 
 389     protected static ValidatePipe invalidpipe = new ValidatePipe();
 390 
 391     /*
 392      * Invalidate the pipeline
 393      */
 394     protected void invalidatePipe() {
 395         drawpipe = invalidpipe;
 396         fillpipe = invalidpipe;
 397         shapepipe = invalidpipe;
 398         textpipe = invalidpipe;
 399         imagepipe = invalidpipe;
 400         loops = null;
 401     }
 402 
 403     public void validatePipe() {
 404         /* This workaround is for the situation when we update the Pipelines
 405          * for invalid SurfaceData and run further code when the current
 406          * pipeline doesn't support the type of new SurfaceData created during
 407          * the current pipeline's work (in place of the invalid SurfaceData).
 408          * Usually SurfaceData and Pipelines are repaired (through revalidateAll)
 409          * and called again in the exception handlers */
 410 
 411         if (!surfaceData.isValid()) {
 412             throw new InvalidPipeException("attempt to validate Pipe with invalid SurfaceData");
 413         }
 414 
 415         surfaceData.validatePipe(this);
 416     }
 417 
 418     /*
 419      * Intersect two Shapes by the simplest method, attempting to produce
 420      * a simplified result.
 421      * The boolean arguments keep1 and keep2 specify whether or not
 422      * the first or second shapes can be modified during the operation
 423      * or whether that shape must be "kept" unmodified.
 424      */
 425     Shape intersectShapes(Shape s1, Shape s2, boolean keep1, boolean keep2) {
 426         if (s1 instanceof Rectangle && s2 instanceof Rectangle) {
 427             return ((Rectangle) s1).intersection((Rectangle) s2);
 428         }
 429         if (s1 instanceof Rectangle2D) {
 430             return intersectRectShape((Rectangle2D) s1, s2, keep1, keep2);
 431         } else if (s2 instanceof Rectangle2D) {
 432             return intersectRectShape((Rectangle2D) s2, s1, keep2, keep1);
 433         }
 434         return intersectByArea(s1, s2, keep1, keep2);
 435     }
 436 
 437     /*
 438      * Intersect a Rectangle with a Shape by the simplest method,
 439      * attempting to produce a simplified result.
 440      * The boolean arguments keep1 and keep2 specify whether or not
 441      * the first or second shapes can be modified during the operation
 442      * or whether that shape must be "kept" unmodified.
 443      */
 444     Shape intersectRectShape(Rectangle2D r, Shape s,
 445                              boolean keep1, boolean keep2) {
 446         if (s instanceof Rectangle2D) {
 447             Rectangle2D r2 = (Rectangle2D) s;
 448             Rectangle2D outrect;
 449             if (!keep1) {
 450                 outrect = r;
 451             } else if (!keep2) {
 452                 outrect = r2;
 453             } else {
 454                 outrect = new Rectangle2D.Float();
 455             }
 456             double x1 = Math.max(r.getX(), r2.getX());
 457             double x2 = Math.min(r.getX()  + r.getWidth(),
 458                                  r2.getX() + r2.getWidth());
 459             double y1 = Math.max(r.getY(), r2.getY());
 460             double y2 = Math.min(r.getY()  + r.getHeight(),
 461                                  r2.getY() + r2.getHeight());
 462 
 463             if (((x2 - x1) < 0) || ((y2 - y1) < 0))
 464                 // Width or height is negative. No intersection.
 465                 outrect.setFrameFromDiagonal(0, 0, 0, 0);
 466             else
 467                 outrect.setFrameFromDiagonal(x1, y1, x2, y2);
 468             return outrect;
 469         }
 470         if (r.contains(s.getBounds2D())) {
 471             if (keep2) {
 472                 s = cloneShape(s);
 473             }
 474             return s;
 475         }
 476         return intersectByArea(r, s, keep1, keep2);
 477     }
 478 
 479     protected static Shape cloneShape(Shape s) {
 480         return new GeneralPath(s);
 481     }
 482 
 483     /*
 484      * Intersect two Shapes using the Area class.  Presumably other
 485      * attempts at simpler intersection methods proved fruitless.
 486      * The boolean arguments keep1 and keep2 specify whether or not
 487      * the first or second shapes can be modified during the operation
 488      * or whether that shape must be "kept" unmodified.
 489      * @see #intersectShapes
 490      * @see #intersectRectShape
 491      */
 492     Shape intersectByArea(Shape s1, Shape s2, boolean keep1, boolean keep2) {
 493         Area a1, a2;
 494 
 495         // First see if we can find an overwriteable source shape
 496         // to use as our destination area to avoid duplication.
 497         if (!keep1 && (s1 instanceof Area)) {
 498             a1 = (Area) s1;
 499         } else if (!keep2 && (s2 instanceof Area)) {
 500             a1 = (Area) s2;
 501             s2 = s1;
 502         } else {
 503             a1 = new Area(s1);
 504         }
 505 
 506         if (s2 instanceof Area) {
 507             a2 = (Area) s2;
 508         } else {
 509             a2 = new Area(s2);
 510         }
 511 
 512         a1.intersect(a2);
 513         if (a1.isRectangular()) {
 514             return a1.getBounds();
 515         }
 516 
 517         return a1;
 518     }
 519 
 520     /*
 521      * Intersect usrClip bounds and device bounds to determine the composite
 522      * rendering boundaries.
 523      */
 524     public Region getCompClip() {
 525         if (!surfaceData.isValid()) {
 526             // revalidateAll() implicitly recalculcates the composite clip
 527             revalidateAll();
 528         }
 529 
 530         return clipRegion;
 531     }
 532 
 533     public Font getFont() {
 534         if (font == null) {
 535             font = defaultFont;
 536         }
 537         return font;
 538     }
 539 
 540     private static final double[] IDENT_MATRIX = {1, 0, 0, 1};
 541     private static final AffineTransform IDENT_ATX =
 542         new AffineTransform();
 543 
 544     private static final int MINALLOCATED = 8;
 545     private static final int TEXTARRSIZE = 17;
 546     private static double[][] textTxArr = new double[TEXTARRSIZE][];
 547     private static AffineTransform[] textAtArr =
 548         new AffineTransform[TEXTARRSIZE];
 549 
 550     static {
 551         for (int i=MINALLOCATED;i<TEXTARRSIZE;i++) {
 552           textTxArr[i] = new double [] {i, 0, 0, i};
 553           textAtArr[i] = new AffineTransform( textTxArr[i]);
 554         }
 555     }
 556 
 557     // cached state for various draw[String,Char,Byte] optimizations
 558     public FontInfo checkFontInfo(FontInfo info, Font font,
 559                                   FontRenderContext frc) {
 560         /* Do not create a FontInfo object as part of construction of an
 561          * SG2D as its possible it may never be needed - ie if no text
 562          * is drawn using this SG2D.
 563          */
 564         if (info == null) {
 565             info = new FontInfo();
 566         }
 567 
 568         float ptSize = font.getSize2D();
 569         int txFontType;
 570         AffineTransform devAt, textAt=null;
 571         if (font.isTransformed()) {
 572             textAt = font.getTransform();
 573             textAt.scale(ptSize, ptSize);
 574             txFontType = textAt.getType();
 575             info.originX = (float)textAt.getTranslateX();
 576             info.originY = (float)textAt.getTranslateY();
 577             textAt.translate(-info.originX, -info.originY);
 578             if (transformState >= TRANSFORM_TRANSLATESCALE) {
 579                 transform.getMatrix(info.devTx = new double[4]);
 580                 devAt = new AffineTransform(info.devTx);
 581                 textAt.preConcatenate(devAt);
 582             } else {
 583                 info.devTx = IDENT_MATRIX;
 584                 devAt = IDENT_ATX;
 585             }
 586             textAt.getMatrix(info.glyphTx = new double[4]);
 587             double shearx = textAt.getShearX();
 588             double scaley = textAt.getScaleY();
 589             if (shearx != 0) {
 590                 scaley = Math.sqrt(shearx * shearx + scaley * scaley);
 591             }
 592             info.pixelHeight = (int)(Math.abs(scaley)+0.5);
 593         } else {
 594             txFontType = AffineTransform.TYPE_IDENTITY;
 595             info.originX = info.originY = 0;
 596             if (transformState >= TRANSFORM_TRANSLATESCALE) {
 597                 transform.getMatrix(info.devTx = new double[4]);
 598                 devAt = new AffineTransform(info.devTx);
 599                 info.glyphTx = new double[4];
 600                 for (int i = 0; i < 4; i++) {
 601                     info.glyphTx[i] = info.devTx[i] * ptSize;
 602                 }
 603                 textAt = new AffineTransform(info.glyphTx);
 604                 double shearx = transform.getShearX();
 605                 double scaley = transform.getScaleY();
 606                 if (shearx != 0) {
 607                     scaley = Math.sqrt(shearx * shearx + scaley * scaley);
 608                 }
 609                 info.pixelHeight = (int)(Math.abs(scaley * ptSize)+0.5);
 610             } else {
 611                 /* If the double represents a common integral, we
 612                  * may have pre-allocated objects.
 613                  * A "sparse" array be seems to be as fast as a switch
 614                  * even for 3 or 4 pt sizes, and is more flexible.
 615                  * This should perform comparably in single-threaded
 616                  * rendering to the old code which synchronized on the
 617                  * class and scale better on MP systems.
 618                  */
 619                 int pszInt = (int)ptSize;
 620                 if (ptSize == pszInt &&
 621                     pszInt >= MINALLOCATED && pszInt < TEXTARRSIZE) {
 622                     info.glyphTx = textTxArr[pszInt];
 623                     textAt = textAtArr[pszInt];
 624                     info.pixelHeight = pszInt;
 625                 } else {
 626                     info.pixelHeight = (int)(ptSize+0.5);
 627                 }
 628                 if (textAt == null) {
 629                     info.glyphTx = new double[] {ptSize, 0, 0, ptSize};
 630                     textAt = new AffineTransform(info.glyphTx);
 631                 }
 632 
 633                 info.devTx = IDENT_MATRIX;
 634                 devAt = IDENT_ATX;
 635             }
 636         }
 637 
 638         info.font2D = FontUtilities.getFont2D(font);
 639 
 640         int fmhint = fractionalMetricsHint;
 641         if (fmhint == SunHints.INTVAL_FRACTIONALMETRICS_DEFAULT) {
 642             fmhint = SunHints.INTVAL_FRACTIONALMETRICS_OFF;
 643         }
 644         info.lcdSubPixPos = false; // conditionally set true in LCD mode.
 645 
 646         /* The text anti-aliasing hints that are set by the client need
 647          * to be interpreted for the current state and stored in the
 648          * FontInfo.aahint which is what will actually be used and
 649          * will be one of OFF, ON, LCD_HRGB or LCD_VRGB.
 650          * This is what pipe selection code should typically refer to, not
 651          * textAntialiasHint. This means we are now evaluating the meaning
 652          * of "default" here. Any pipe that really cares about that will
 653          * also need to consult that variable.
 654          * Otherwise these are being used only as args to getStrike,
 655          * and are encapsulated in that object which is part of the
 656          * FontInfo, so we do not need to store them directly as fields
 657          * in the FontInfo object.
 658          * That could change if FontInfo's were more selectively
 659          * revalidated when graphics state changed. Presently this
 660          * method re-evaluates all fields in the fontInfo.
 661          * The strike doesn't need to know the RGB subpixel order. Just
 662          * if its H or V orientation, so if an LCD option is specified we
 663          * always pass in the RGB hint to the strike.
 664          * frc is non-null only if this is a GlyphVector. For reasons
 665          * which are probably a historical mistake the AA hint in a GV
 666          * is honoured when we render, overriding the Graphics setting.
 667          */
 668         int aahint;
 669         if (frc == null) {
 670             aahint = textAntialiasHint;
 671         } else {
 672             aahint = ((SunHints.Value)frc.getAntiAliasingHint()).getIndex();
 673         }
 674         if (aahint == SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT) {
 675             if (antialiasHint == SunHints.INTVAL_ANTIALIAS_ON) {
 676                 aahint = SunHints.INTVAL_TEXT_ANTIALIAS_ON;
 677             } else {
 678                 aahint = SunHints.INTVAL_TEXT_ANTIALIAS_OFF;
 679             }
 680         } else {
 681             /* If we are in checkFontInfo because a rendering hint has been
 682              * set then all pipes are revalidated. But we can also
 683              * be here because setFont() has been called when the 'gasp'
 684              * hint is set, as then the font size determines the text pipe.
 685              * See comments in SunGraphics2d.setFont(Font).
 686              */
 687             if (aahint == SunHints.INTVAL_TEXT_ANTIALIAS_GASP) {
 688                 if (info.font2D.useAAForPtSize(info.pixelHeight)) {
 689                     aahint = SunHints.INTVAL_TEXT_ANTIALIAS_ON;
 690                 } else {
 691                     aahint = SunHints.INTVAL_TEXT_ANTIALIAS_OFF;
 692                 }
 693             } else if (aahint >= SunHints.INTVAL_TEXT_ANTIALIAS_LCD_HRGB) {
 694                 /* loops for default rendering modes are installed in the SG2D
 695                  * constructor. If there are none this will be null.
 696                  * Not all compositing modes update the render loops, so
 697                  * we also test that this is a mode we know should support
 698                  * this. One minor issue is that the loops aren't necessarily
 699                  * installed for a new rendering mode until after this
 700                  * method is called during pipeline validation. So it is
 701                  * theoretically possible that it was set to null for a
 702                  * compositing mode, the composite is then set back to Src,
 703                  * but the loop is still null when this is called and AA=ON
 704                  * is installed instead of an LCD mode.
 705                  * However this is done in the right order in SurfaceData.java
 706                  * so this is not likely to be a problem - but not
 707                  * guaranteed.
 708                  */
 709                 if (
 710                     !surfaceData.canRenderLCDText(this)
 711 //                    loops.drawGlyphListLCDLoop == null ||
 712 //                    compositeState > COMP_ISCOPY ||
 713 //                    paintState > PAINT_ALPHACOLOR
 714                       ) {
 715                     aahint = SunHints.INTVAL_TEXT_ANTIALIAS_ON;
 716                 } else {
 717                     info.lcdRGBOrder = true;
 718                     /* Collapse these into just HRGB or VRGB.
 719                      * Pipe selection code needs only to test for these two.
 720                      * Since these both select the same pipe anyway its
 721                      * tempting to collapse into one value. But they are
 722                      * different strikes (glyph caches) so the distinction
 723                      * needs to be made for that purpose.
 724                      */
 725                     if (aahint == SunHints.INTVAL_TEXT_ANTIALIAS_LCD_HBGR) {
 726                         aahint = SunHints.INTVAL_TEXT_ANTIALIAS_LCD_HRGB;
 727                         info.lcdRGBOrder = false;
 728                     } else if
 729                         (aahint == SunHints.INTVAL_TEXT_ANTIALIAS_LCD_VBGR) {
 730                         aahint = SunHints.INTVAL_TEXT_ANTIALIAS_LCD_VRGB;
 731                         info.lcdRGBOrder = false;
 732                     }
 733                     /* Support subpixel positioning only for the case in
 734                      * which the horizontal resolution is increased
 735                      */
 736                     info.lcdSubPixPos =
 737                         fmhint == SunHints.INTVAL_FRACTIONALMETRICS_ON &&
 738                         aahint == SunHints.INTVAL_TEXT_ANTIALIAS_LCD_HRGB;
 739                 }
 740             }
 741         }
 742         info.aaHint = aahint;
 743         info.fontStrike = info.font2D.getStrike(font, devAt, textAt,
 744                                                 aahint, fmhint);
 745         return info;
 746     }
 747 
 748     public static boolean isRotated(double [] mtx) {
 749         if ((mtx[0] == mtx[3]) &&
 750             (mtx[1] == 0.0) &&
 751             (mtx[2] == 0.0) &&
 752             (mtx[0] > 0.0))
 753         {
 754             return false;
 755         }
 756 
 757         return true;
 758     }
 759 
 760     public void setFont(Font font) {
 761         /* replacing the reference equality test font != this.font with
 762          * !font.equals(this.font) did not yield any measurable difference
 763          * in testing, but there may be yet to be identified cases where it
 764          * is beneficial.
 765          */
 766         if (font != null && font!=this.font/*!font.equals(this.font)*/) {
 767             /* In the GASP AA case the textpipe depends on the glyph size
 768              * as determined by graphics and font transforms as well as the
 769              * font size, and information in the font. But we may invalidate
 770              * the pipe only to find that it made no difference.
 771              * Deferring pipe invalidation to checkFontInfo won't work because
 772              * when called we may already be rendering to the wrong pipe.
 773              * So, if the font is transformed, or the graphics has more than
 774              * a simple scale, we'll take that as enough of a hint to
 775              * revalidate everything. But if they aren't we will
 776              * use the font's point size to query the gasp table and see if
 777              * what it says matches what's currently being used, in which
 778              * case there's no need to invalidate the textpipe.
 779              * This should be sufficient for all typical uses cases.
 780              */
 781             if (textAntialiasHint == SunHints.INTVAL_TEXT_ANTIALIAS_GASP &&
 782                 textpipe != invalidpipe &&
 783                 (transformState > TRANSFORM_ANY_TRANSLATE ||
 784                  font.isTransformed() ||
 785                  fontInfo == null || // Precaution, if true shouldn't get here
 786                  (fontInfo.aaHint == SunHints.INTVAL_TEXT_ANTIALIAS_ON) !=
 787                      FontUtilities.getFont2D(font).
 788                          useAAForPtSize(font.getSize()))) {
 789                 textpipe = invalidpipe;
 790             }
 791             this.font = font;
 792             this.fontMetrics = null;
 793             this.validFontInfo = false;
 794         }
 795     }
 796 
 797     public FontInfo getFontInfo() {
 798         if (!validFontInfo) {
 799             this.fontInfo = checkFontInfo(this.fontInfo, font, null);
 800             validFontInfo = true;
 801         }
 802         return this.fontInfo;
 803     }
 804 
 805     /* Used by drawGlyphVector which specifies its own font. */
 806     public FontInfo getGVFontInfo(Font font, FontRenderContext frc) {
 807         if (glyphVectorFontInfo != null &&
 808             glyphVectorFontInfo.font == font &&
 809             glyphVectorFRC == frc) {
 810             return glyphVectorFontInfo;
 811         } else {
 812             glyphVectorFRC = frc;
 813             return glyphVectorFontInfo =
 814                 checkFontInfo(glyphVectorFontInfo, font, frc);
 815         }
 816     }
 817 
 818     public FontMetrics getFontMetrics() {
 819         if (this.fontMetrics != null) {
 820             return this.fontMetrics;
 821         }
 822         /* NB the constructor and the setter disallow "font" being null */
 823         return this.fontMetrics =
 824            FontDesignMetrics.getMetrics(font, getFontRenderContext());
 825     }
 826 
 827     public FontMetrics getFontMetrics(Font font) {
 828         if ((this.fontMetrics != null) && (font == this.font)) {
 829             return this.fontMetrics;
 830         }
 831         FontMetrics fm =
 832           FontDesignMetrics.getMetrics(font, getFontRenderContext());
 833 
 834         if (this.font == font) {
 835             this.fontMetrics = fm;
 836         }
 837         return fm;
 838     }
 839 
 840     /**
 841      * Checks to see if a Path intersects the specified Rectangle in device
 842      * space.  The rendering attributes taken into account include the
 843      * clip, transform, and stroke attributes.
 844      * @param rect The area in device space to check for a hit.
 845      * @param p The path to check for a hit.
 846      * @param onStroke Flag to choose between testing the stroked or
 847      * the filled path.
 848      * @return True if there is a hit, false otherwise.
 849      * @see #setStroke
 850      * @see #fillPath
 851      * @see #drawPath
 852      * @see #transform
 853      * @see #setTransform
 854      * @see #clip
 855      * @see #setClip
 856      */
 857     public boolean hit(Rectangle rect, Shape s, boolean onStroke) {
 858         if (onStroke) {
 859             s = stroke.createStrokedShape(s);
 860         }
 861 
 862         s = transformShape(s);
 863         if ((constrainX|constrainY) != 0) {
 864             rect = new Rectangle(rect);
 865             rect.translate(constrainX, constrainY);
 866         }
 867 
 868         return s.intersects(rect);
 869     }
 870 
 871     /**
 872      * Return the ColorModel associated with this Graphics2D.
 873      */
 874     public ColorModel getDeviceColorModel() {
 875         return surfaceData.getColorModel();
 876     }
 877 
 878     /**
 879      * Return the device configuration associated with this Graphics2D.
 880      */
 881     public GraphicsConfiguration getDeviceConfiguration() {
 882         return surfaceData.getDeviceConfiguration();
 883     }
 884 
 885     /**
 886      * Return the SurfaceData object assigned to manage the destination
 887      * drawable surface of this Graphics2D.
 888      */
 889     public final SurfaceData getSurfaceData() {
 890         return surfaceData;
 891     }
 892 
 893     /**
 894      * Sets the Composite in the current graphics state. Composite is used
 895      * in all drawing methods such as drawImage, drawString, drawPath,
 896      * and fillPath.  It specifies how new pixels are to be combined with
 897      * the existing pixels on the graphics device in the rendering process.
 898      * @param comp The Composite object to be used for drawing.
 899      * @see java.awt.Graphics#setXORMode
 900      * @see java.awt.Graphics#setPaintMode
 901      * @see AlphaComposite
 902      */
 903     public void setComposite(Composite comp) {
 904         if (composite == comp) {
 905             return;
 906         }
 907         int newCompState;
 908         CompositeType newCompType;
 909         if (comp instanceof AlphaComposite) {
 910             AlphaComposite alphacomp = (AlphaComposite) comp;
 911             newCompType = CompositeType.forAlphaComposite(alphacomp);
 912             if (newCompType == CompositeType.SrcOverNoEa) {
 913                 if (paintState == PAINT_OPAQUECOLOR ||
 914                     (paintState > PAINT_ALPHACOLOR &&
 915                      paint.getTransparency() == Transparency.OPAQUE))
 916                 {
 917                     newCompState = COMP_ISCOPY;
 918                 } else {
 919                     newCompState = COMP_ALPHA;
 920                 }
 921             } else if (newCompType == CompositeType.SrcNoEa ||
 922                        newCompType == CompositeType.Src ||
 923                        newCompType == CompositeType.Clear)
 924             {
 925                 newCompState = COMP_ISCOPY;
 926             } else if (surfaceData.getTransparency() == Transparency.OPAQUE &&
 927                        newCompType == CompositeType.SrcIn)
 928             {
 929                 newCompState = COMP_ISCOPY;
 930             } else {
 931                 newCompState = COMP_ALPHA;
 932             }
 933         } else if (comp instanceof XORComposite) {
 934             newCompState = COMP_XOR;
 935             newCompType = CompositeType.Xor;
 936         } else if (comp == null) {
 937             throw new IllegalArgumentException("null Composite");
 938         } else {
 939             surfaceData.checkCustomComposite();
 940             newCompState = COMP_CUSTOM;
 941             newCompType = CompositeType.General;
 942         }
 943         if (compositeState != newCompState ||
 944             imageComp != newCompType)
 945         {
 946             compositeState = newCompState;
 947             imageComp = newCompType;
 948             invalidatePipe();
 949             validFontInfo = false;
 950         }
 951         composite = comp;
 952         if (paintState <= PAINT_ALPHACOLOR) {
 953             validateColor();
 954         }
 955     }
 956 
 957     /**
 958      * Sets the Paint in the current graphics state.
 959      * @param paint The Paint object to be used to generate color in
 960      * the rendering process.
 961      * @see java.awt.Graphics#setColor
 962      * @see GradientPaint
 963      * @see TexturePaint
 964      */
 965     public void setPaint(Paint paint) {
 966         if (paint instanceof Color) {
 967             setColor((Color) paint);
 968             return;
 969         }
 970         if (paint == null || this.paint == paint) {
 971             return;
 972         }
 973         this.paint = paint;
 974         if (imageComp == CompositeType.SrcOverNoEa) {
 975             // special case where compState depends on opacity of paint
 976             if (paint.getTransparency() == Transparency.OPAQUE) {
 977                 if (compositeState != COMP_ISCOPY) {
 978                     compositeState = COMP_ISCOPY;
 979                 }
 980             } else {
 981                 if (compositeState == COMP_ISCOPY) {
 982                     compositeState = COMP_ALPHA;
 983                 }
 984             }
 985         }
 986         Class<? extends Paint> paintClass = paint.getClass();
 987         if (paintClass == GradientPaint.class) {
 988             paintState = PAINT_GRADIENT;
 989         } else if (paintClass == LinearGradientPaint.class) {
 990             paintState = PAINT_LIN_GRADIENT;
 991         } else if (paintClass == RadialGradientPaint.class) {
 992             paintState = PAINT_RAD_GRADIENT;
 993         } else if (paintClass == TexturePaint.class) {
 994             paintState = PAINT_TEXTURE;
 995         } else {
 996             paintState = PAINT_CUSTOM;
 997         }
 998         validFontInfo = false;
 999         invalidatePipe();
1000     }
1001 
1002     static final int NON_UNIFORM_SCALE_MASK =
1003         (AffineTransform.TYPE_GENERAL_TRANSFORM |
1004          AffineTransform.TYPE_GENERAL_SCALE);
1005     public static final double MinPenSizeAA =
1006         sun.java2d.pipe.RenderingEngine.getInstance().getMinimumAAPenSize();
1007     public static final double MinPenSizeAASquared =
1008         (MinPenSizeAA * MinPenSizeAA);
1009     // Since inaccuracies in the trig package can cause us to
1010     // calculated a rotated pen width of just slightly greater
1011     // than 1.0, we add a fudge factor to our comparison value
1012     // here so that we do not misclassify single width lines as
1013     // wide lines under certain rotations.
1014     public static final double MinPenSizeSquared = 1.000000001;
1015 
1016     private void validateBasicStroke(BasicStroke bs) {
1017         boolean aa = (antialiasHint == SunHints.INTVAL_ANTIALIAS_ON);
1018         if (transformState < TRANSFORM_TRANSLATESCALE) {
1019             if (aa) {
1020                 if (bs.getLineWidth() <= MinPenSizeAA) {
1021                     if (bs.getDashArray() == null) {
1022                         strokeState = STROKE_THIN;
1023                     } else {
1024                         strokeState = STROKE_THINDASHED;
1025                     }
1026                 } else {
1027                     strokeState = STROKE_WIDE;
1028                 }
1029             } else {
1030                 if (bs == defaultStroke) {
1031                     strokeState = STROKE_THIN;
1032                 } else if (bs.getLineWidth() <= 1.0f) {
1033                     if (bs.getDashArray() == null) {
1034                         strokeState = STROKE_THIN;
1035                     } else {
1036                         strokeState = STROKE_THINDASHED;
1037                     }
1038                 } else {
1039                     strokeState = STROKE_WIDE;
1040                 }
1041             }
1042         } else {
1043             double widthsquared;
1044             if ((transform.getType() & NON_UNIFORM_SCALE_MASK) == 0) {
1045                 /* sqrt omitted, compare to squared limits below. */
1046                 widthsquared = Math.abs(transform.getDeterminant());
1047             } else {
1048                 /* First calculate the "maximum scale" of this transform. */
1049                 double A = transform.getScaleX();       // m00
1050                 double C = transform.getShearX();       // m01
1051                 double B = transform.getShearY();       // m10
1052                 double D = transform.getScaleY();       // m11
1053 
1054                 /*
1055                  * Given a 2 x 2 affine matrix [ A B ] such that
1056                  *                             [ C D ]
1057                  * v' = [x' y'] = [Ax + Cy, Bx + Dy], we want to
1058                  * find the maximum magnitude (norm) of the vector v'
1059                  * with the constraint (x^2 + y^2 = 1).
1060                  * The equation to maximize is
1061                  *     |v'| = sqrt((Ax+Cy)^2+(Bx+Dy)^2)
1062                  * or  |v'| = sqrt((AA+BB)x^2 + 2(AC+BD)xy + (CC+DD)y^2).
1063                  * Since sqrt is monotonic we can maximize |v'|^2
1064                  * instead and plug in the substitution y = sqrt(1 - x^2).
1065                  * Trigonometric equalities can then be used to get
1066                  * rid of most of the sqrt terms.
1067                  */
1068                 double EA = A*A + B*B;          // x^2 coefficient
1069                 double EB = 2*(A*C + B*D);      // xy coefficient
1070                 double EC = C*C + D*D;          // y^2 coefficient
1071 
1072                 /*
1073                  * There is a lot of calculus omitted here.
1074                  *
1075                  * Conceptually, in the interests of understanding the
1076                  * terms that the calculus produced we can consider
1077                  * that EA and EC end up providing the lengths along
1078                  * the major axes and the hypot term ends up being an
1079                  * adjustment for the additional length along the off-axis
1080                  * angle of rotated or sheared ellipses as well as an
1081                  * adjustment for the fact that the equation below
1082                  * averages the two major axis lengths.  (Notice that
1083                  * the hypot term contains a part which resolves to the
1084                  * difference of these two axis lengths in the absence
1085                  * of rotation.)
1086                  *
1087                  * In the calculus, the ratio of the EB and (EA-EC) terms
1088                  * ends up being the tangent of 2*theta where theta is
1089                  * the angle that the long axis of the ellipse makes
1090                  * with the horizontal axis.  Thus, this equation is
1091                  * calculating the length of the hypotenuse of a triangle
1092                  * along that axis.
1093                  */
1094                 double hypot = Math.sqrt(EB*EB + (EA-EC)*(EA-EC));
1095 
1096                 /* sqrt omitted, compare to squared limits below. */
1097                 widthsquared = ((EA + EC + hypot)/2.0);
1098             }
1099             if (bs != defaultStroke) {
1100                 widthsquared *= bs.getLineWidth() * bs.getLineWidth();
1101             }
1102             if (widthsquared <=
1103                 (aa ? MinPenSizeAASquared : MinPenSizeSquared))
1104             {
1105                 if (bs.getDashArray() == null) {
1106                     strokeState = STROKE_THIN;
1107                 } else {
1108                     strokeState = STROKE_THINDASHED;
1109                 }
1110             } else {
1111                 strokeState = STROKE_WIDE;
1112             }
1113         }
1114     }
1115 
1116     /*
1117      * Sets the Stroke in the current graphics state.
1118      * @param s The Stroke object to be used to stroke a Path in
1119      * the rendering process.
1120      * @see BasicStroke
1121      */
1122     public void setStroke(Stroke s) {
1123         if (s == null) {
1124             throw new IllegalArgumentException("null Stroke");
1125         }
1126         int saveStrokeState = strokeState;
1127         stroke = s;
1128         if (s instanceof BasicStroke) {
1129             validateBasicStroke((BasicStroke) s);
1130         } else {
1131             strokeState = STROKE_CUSTOM;
1132         }
1133         if (strokeState != saveStrokeState) {
1134             invalidatePipe();
1135         }
1136     }
1137 
1138     /**
1139      * Sets the preferences for the rendering algorithms.
1140      * Hint categories include controls for rendering quality and
1141      * overall time/quality trade-off in the rendering process.
1142      * @param hintKey The key of hint to be set. The strings are
1143      * defined in the RenderingHints class.
1144      * @param hintValue The value indicating preferences for the specified
1145      * hint category. These strings are defined in the RenderingHints
1146      * class.
1147      * @see RenderingHints
1148      */
1149     public void setRenderingHint(Key hintKey, Object hintValue) {
1150         // If we recognize the key, we must recognize the value
1151         //     otherwise throw an IllegalArgumentException
1152         //     and do not change the Hints object
1153         // If we do not recognize the key, just pass it through
1154         //     to the Hints object untouched
1155         if (!hintKey.isCompatibleValue(hintValue)) {
1156             throw new IllegalArgumentException
1157                 (hintValue+" is not compatible with "+hintKey);
1158         }
1159         if (hintKey instanceof SunHints.Key) {
1160             boolean stateChanged;
1161             boolean textStateChanged = false;
1162             boolean recognized = true;
1163             SunHints.Key sunKey = (SunHints.Key) hintKey;
1164             int newHint;
1165             if (sunKey == SunHints.KEY_TEXT_ANTIALIAS_LCD_CONTRAST) {
1166                 newHint = ((Integer)hintValue).intValue();
1167             } else {
1168                 newHint = ((SunHints.Value) hintValue).getIndex();
1169             }
1170             switch (sunKey.getIndex()) {
1171             case SunHints.INTKEY_RENDERING:
1172                 stateChanged = (renderHint != newHint);
1173                 if (stateChanged) {
1174                     renderHint = newHint;
1175                     if (interpolationHint == -1) {
1176                         interpolationType =
1177                             (newHint == SunHints.INTVAL_RENDER_QUALITY
1178                              ? AffineTransformOp.TYPE_BILINEAR
1179                              : AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
1180                     }
1181                 }
1182                 break;
1183             case SunHints.INTKEY_ANTIALIASING:
1184                 stateChanged = (antialiasHint != newHint);
1185                 antialiasHint = newHint;
1186                 if (stateChanged) {
1187                     textStateChanged =
1188                         (textAntialiasHint ==
1189                          SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT);
1190                     if (strokeState != STROKE_CUSTOM) {
1191                         validateBasicStroke((BasicStroke) stroke);
1192                     }
1193                 }
1194                 break;
1195             case SunHints.INTKEY_TEXT_ANTIALIASING:
1196                 stateChanged = (textAntialiasHint != newHint);
1197                 textStateChanged = stateChanged;
1198                 textAntialiasHint = newHint;
1199                 break;
1200             case SunHints.INTKEY_FRACTIONALMETRICS:
1201                 stateChanged = (fractionalMetricsHint != newHint);
1202                 textStateChanged = stateChanged;
1203                 fractionalMetricsHint = newHint;
1204                 break;
1205             case SunHints.INTKEY_AATEXT_LCD_CONTRAST:
1206                 stateChanged = false;
1207                 /* Already have validated it is an int 100 <= newHint <= 250 */
1208                 lcdTextContrast = newHint;
1209                 break;
1210             case SunHints.INTKEY_INTERPOLATION:
1211                 interpolationHint = newHint;
1212                 switch (newHint) {
1213                 case SunHints.INTVAL_INTERPOLATION_BICUBIC:
1214                     newHint = AffineTransformOp.TYPE_BICUBIC;
1215                     break;
1216                 case SunHints.INTVAL_INTERPOLATION_BILINEAR:
1217                     newHint = AffineTransformOp.TYPE_BILINEAR;
1218                     break;
1219                 default:
1220                 case SunHints.INTVAL_INTERPOLATION_NEAREST_NEIGHBOR:
1221                     newHint = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
1222                     break;
1223                 }
1224                 stateChanged = (interpolationType != newHint);
1225                 interpolationType = newHint;
1226                 break;
1227             case SunHints.INTKEY_STROKE_CONTROL:
1228                 stateChanged = (strokeHint != newHint);
1229                 strokeHint = newHint;
1230                 break;
1231             default:
1232                 recognized = false;
1233                 stateChanged = false;
1234                 break;
1235             }
1236             if (recognized) {
1237                 if (stateChanged) {
1238                     invalidatePipe();
1239                     if (textStateChanged) {
1240                         fontMetrics = null;
1241                         this.cachedFRC = null;
1242                         validFontInfo = false;
1243                         this.glyphVectorFontInfo = null;
1244                     }
1245                 }
1246                 if (hints != null) {
1247                     hints.put(hintKey, hintValue);
1248                 }
1249                 return;
1250             }
1251         }
1252         // Nothing we recognize so none of "our state" has changed
1253         if (hints == null) {
1254             hints = makeHints(null);
1255         }
1256         hints.put(hintKey, hintValue);
1257     }
1258 
1259 
1260     /**
1261      * Returns the preferences for the rendering algorithms.
1262      * @param hintCategory The category of hint to be set. The strings
1263      * are defined in the RenderingHints class.
1264      * @return The preferences for rendering algorithms. The strings
1265      * are defined in the RenderingHints class.
1266      * @see RenderingHints
1267      */
1268     public Object getRenderingHint(Key hintKey) {
1269         if (hints != null) {
1270             return hints.get(hintKey);
1271         }
1272         if (!(hintKey instanceof SunHints.Key)) {
1273             return null;
1274         }
1275         int keyindex = ((SunHints.Key)hintKey).getIndex();
1276         switch (keyindex) {
1277         case SunHints.INTKEY_RENDERING:
1278             return SunHints.Value.get(SunHints.INTKEY_RENDERING,
1279                                       renderHint);
1280         case SunHints.INTKEY_ANTIALIASING:
1281             return SunHints.Value.get(SunHints.INTKEY_ANTIALIASING,
1282                                       antialiasHint);
1283         case SunHints.INTKEY_TEXT_ANTIALIASING:
1284             return SunHints.Value.get(SunHints.INTKEY_TEXT_ANTIALIASING,
1285                                       textAntialiasHint);
1286         case SunHints.INTKEY_FRACTIONALMETRICS:
1287             return SunHints.Value.get(SunHints.INTKEY_FRACTIONALMETRICS,
1288                                       fractionalMetricsHint);
1289         case SunHints.INTKEY_AATEXT_LCD_CONTRAST:
1290             return new Integer(lcdTextContrast);
1291         case SunHints.INTKEY_INTERPOLATION:
1292             switch (interpolationHint) {
1293             case SunHints.INTVAL_INTERPOLATION_NEAREST_NEIGHBOR:
1294                 return SunHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
1295             case SunHints.INTVAL_INTERPOLATION_BILINEAR:
1296                 return SunHints.VALUE_INTERPOLATION_BILINEAR;
1297             case SunHints.INTVAL_INTERPOLATION_BICUBIC:
1298                 return SunHints.VALUE_INTERPOLATION_BICUBIC;
1299             }
1300             return null;
1301         case SunHints.INTKEY_STROKE_CONTROL:
1302             return SunHints.Value.get(SunHints.INTKEY_STROKE_CONTROL,
1303                                       strokeHint);
1304         }
1305         return null;
1306     }
1307 
1308     /**
1309      * Sets the preferences for the rendering algorithms.
1310      * Hint categories include controls for rendering quality and
1311      * overall time/quality trade-off in the rendering process.
1312      * @param hints The rendering hints to be set
1313      * @see RenderingHints
1314      */
1315     public void setRenderingHints(Map<?,?> hints) {
1316         this.hints = null;
1317         renderHint = SunHints.INTVAL_RENDER_DEFAULT;
1318         antialiasHint = SunHints.INTVAL_ANTIALIAS_OFF;
1319         textAntialiasHint = SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT;
1320         fractionalMetricsHint = SunHints.INTVAL_FRACTIONALMETRICS_OFF;
1321         lcdTextContrast = lcdTextContrastDefaultValue;
1322         interpolationHint = -1;
1323         interpolationType = AffineTransformOp.TYPE_NEAREST_NEIGHBOR;
1324         boolean customHintPresent = false;
1325         Iterator<?> iter = hints.keySet().iterator();
1326         while (iter.hasNext()) {
1327             Object key = iter.next();
1328             if (key == SunHints.KEY_RENDERING ||
1329                 key == SunHints.KEY_ANTIALIASING ||
1330                 key == SunHints.KEY_TEXT_ANTIALIASING ||
1331                 key == SunHints.KEY_FRACTIONALMETRICS ||
1332                 key == SunHints.KEY_TEXT_ANTIALIAS_LCD_CONTRAST ||
1333                 key == SunHints.KEY_STROKE_CONTROL ||
1334                 key == SunHints.KEY_INTERPOLATION)
1335             {
1336                 setRenderingHint((Key) key, hints.get(key));
1337             } else {
1338                 customHintPresent = true;
1339             }
1340         }
1341         if (customHintPresent) {
1342             this.hints = makeHints(hints);
1343         }
1344         invalidatePipe();
1345     }
1346 
1347     /**
1348      * Adds a number of preferences for the rendering algorithms.
1349      * Hint categories include controls for rendering quality and
1350      * overall time/quality trade-off in the rendering process.
1351      * @param hints The rendering hints to be set
1352      * @see RenderingHints
1353      */
1354     public void addRenderingHints(Map<?,?> hints) {
1355         boolean customHintPresent = false;
1356         Iterator<?> iter = hints.keySet().iterator();
1357         while (iter.hasNext()) {
1358             Object key = iter.next();
1359             if (key == SunHints.KEY_RENDERING ||
1360                 key == SunHints.KEY_ANTIALIASING ||
1361                 key == SunHints.KEY_TEXT_ANTIALIASING ||
1362                 key == SunHints.KEY_FRACTIONALMETRICS ||
1363                 key == SunHints.KEY_TEXT_ANTIALIAS_LCD_CONTRAST ||
1364                 key == SunHints.KEY_STROKE_CONTROL ||
1365                 key == SunHints.KEY_INTERPOLATION)
1366             {
1367                 setRenderingHint((Key) key, hints.get(key));
1368             } else {
1369                 customHintPresent = true;
1370             }
1371         }
1372         if (customHintPresent) {
1373             if (this.hints == null) {
1374                 this.hints = makeHints(hints);
1375             } else {
1376                 this.hints.putAll(hints);
1377             }
1378         }
1379     }
1380 
1381     /**
1382      * Gets the preferences for the rendering algorithms.
1383      * Hint categories include controls for rendering quality and
1384      * overall time/quality trade-off in the rendering process.
1385      * @see RenderingHints
1386      */
1387     public RenderingHints getRenderingHints() {
1388         if (hints == null) {
1389             return makeHints(null);
1390         } else {
1391             return (RenderingHints) hints.clone();
1392         }
1393     }
1394 
1395     RenderingHints makeHints(Map hints) {
1396         RenderingHints model = new RenderingHints(hints);
1397         model.put(SunHints.KEY_RENDERING,
1398                   SunHints.Value.get(SunHints.INTKEY_RENDERING,
1399                                      renderHint));
1400         model.put(SunHints.KEY_ANTIALIASING,
1401                   SunHints.Value.get(SunHints.INTKEY_ANTIALIASING,
1402                                      antialiasHint));
1403         model.put(SunHints.KEY_TEXT_ANTIALIASING,
1404                   SunHints.Value.get(SunHints.INTKEY_TEXT_ANTIALIASING,
1405                                      textAntialiasHint));
1406         model.put(SunHints.KEY_FRACTIONALMETRICS,
1407                   SunHints.Value.get(SunHints.INTKEY_FRACTIONALMETRICS,
1408                                      fractionalMetricsHint));
1409         model.put(SunHints.KEY_TEXT_ANTIALIAS_LCD_CONTRAST,
1410                   Integer.valueOf(lcdTextContrast));
1411         Object value;
1412         switch (interpolationHint) {
1413         case SunHints.INTVAL_INTERPOLATION_NEAREST_NEIGHBOR:
1414             value = SunHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR;
1415             break;
1416         case SunHints.INTVAL_INTERPOLATION_BILINEAR:
1417             value = SunHints.VALUE_INTERPOLATION_BILINEAR;
1418             break;
1419         case SunHints.INTVAL_INTERPOLATION_BICUBIC:
1420             value = SunHints.VALUE_INTERPOLATION_BICUBIC;
1421             break;
1422         default:
1423             value = null;
1424             break;
1425         }
1426         if (value != null) {
1427             model.put(SunHints.KEY_INTERPOLATION, value);
1428         }
1429         model.put(SunHints.KEY_STROKE_CONTROL,
1430                   SunHints.Value.get(SunHints.INTKEY_STROKE_CONTROL,
1431                                      strokeHint));
1432         return model;
1433     }
1434 
1435     /**
1436      * Concatenates the current transform of this Graphics2D with a
1437      * translation transformation.
1438      * This is equivalent to calling transform(T), where T is an
1439      * AffineTransform represented by the following matrix:
1440      * <pre>
1441      *          [   1    0    tx  ]
1442      *          [   0    1    ty  ]
1443      *          [   0    0    1   ]
1444      * </pre>
1445      */
1446     public void translate(double tx, double ty) {
1447         transform.translate(tx, ty);
1448         invalidateTransform();
1449     }
1450 
1451     /**
1452      * Concatenates the current transform of this Graphics2D with a
1453      * rotation transformation.
1454      * This is equivalent to calling transform(R), where R is an
1455      * AffineTransform represented by the following matrix:
1456      * <pre>
1457      *          [   cos(theta)    -sin(theta)    0   ]
1458      *          [   sin(theta)     cos(theta)    0   ]
1459      *          [       0              0         1   ]
1460      * </pre>
1461      * Rotating with a positive angle theta rotates points on the positive
1462      * x axis toward the positive y axis.
1463      * @param theta The angle of rotation in radians.
1464      */
1465     public void rotate(double theta) {
1466         transform.rotate(theta);
1467         invalidateTransform();
1468     }
1469 
1470     /**
1471      * Concatenates the current transform of this Graphics2D with a
1472      * translated rotation transformation.
1473      * This is equivalent to the following sequence of calls:
1474      * <pre>
1475      *          translate(x, y);
1476      *          rotate(theta);
1477      *          translate(-x, -y);
1478      * </pre>
1479      * Rotating with a positive angle theta rotates points on the positive
1480      * x axis toward the positive y axis.
1481      * @param theta The angle of rotation in radians.
1482      * @param x The x coordinate of the origin of the rotation
1483      * @param y The x coordinate of the origin of the rotation
1484      */
1485     public void rotate(double theta, double x, double y) {
1486         transform.rotate(theta, x, y);
1487         invalidateTransform();
1488     }
1489 
1490     /**
1491      * Concatenates the current transform of this Graphics2D with a
1492      * scaling transformation.
1493      * This is equivalent to calling transform(S), where S is an
1494      * AffineTransform represented by the following matrix:
1495      * <pre>
1496      *          [   sx   0    0   ]
1497      *          [   0    sy   0   ]
1498      *          [   0    0    1   ]
1499      * </pre>
1500      */
1501     public void scale(double sx, double sy) {
1502         transform.scale(sx, sy);
1503         invalidateTransform();
1504     }
1505 
1506     /**
1507      * Concatenates the current transform of this Graphics2D with a
1508      * shearing transformation.
1509      * This is equivalent to calling transform(SH), where SH is an
1510      * AffineTransform represented by the following matrix:
1511      * <pre>
1512      *          [   1   shx   0   ]
1513      *          [  shy   1    0   ]
1514      *          [   0    0    1   ]
1515      * </pre>
1516      * @param shx The factor by which coordinates are shifted towards the
1517      * positive X axis direction according to their Y coordinate
1518      * @param shy The factor by which coordinates are shifted towards the
1519      * positive Y axis direction according to their X coordinate
1520      */
1521     public void shear(double shx, double shy) {
1522         transform.shear(shx, shy);
1523         invalidateTransform();
1524     }
1525 
1526     /**
1527      * Composes a Transform object with the transform in this
1528      * Graphics2D according to the rule last-specified-first-applied.
1529      * If the currrent transform is Cx, the result of composition
1530      * with Tx is a new transform Cx'.  Cx' becomes the current
1531      * transform for this Graphics2D.
1532      * Transforming a point p by the updated transform Cx' is
1533      * equivalent to first transforming p by Tx and then transforming
1534      * the result by the original transform Cx.  In other words,
1535      * Cx'(p) = Cx(Tx(p)).
1536      * A copy of the Tx is made, if necessary, so further
1537      * modifications to Tx do not affect rendering.
1538      * @param Tx The Transform object to be composed with the current
1539      * transform.
1540      * @see #setTransform
1541      * @see AffineTransform
1542      */
1543     public void transform(AffineTransform xform) {
1544         this.transform.concatenate(xform);
1545         invalidateTransform();
1546     }
1547 
1548     /**
1549      * Translate
1550      */
1551     public void translate(int x, int y) {
1552         transform.translate(x, y);
1553         if (transformState <= TRANSFORM_INT_TRANSLATE) {
1554             transX += x;
1555             transY += y;
1556             transformState = (((transX | transY) == 0) ?
1557                               TRANSFORM_ISIDENT : TRANSFORM_INT_TRANSLATE);
1558         } else {
1559             invalidateTransform();
1560         }
1561     }
1562 
1563     /**
1564      * Sets the Transform in the current graphics state.
1565      * @param Tx The Transform object to be used in the rendering process.
1566      * @see #transform
1567      * @see TransformChain
1568      * @see AffineTransform
1569      */
1570     @Override
1571     public void setTransform(AffineTransform Tx) {
1572         if ((constrainX | constrainY) == 0 && devScale == 1) {
1573             transform.setTransform(Tx);
1574         } else {
1575             transform.setTransform(devScale, 0, 0, devScale, constrainX,
1576                                    constrainY);
1577             transform.concatenate(Tx);
1578         }
1579         invalidateTransform();
1580     }
1581 
1582     protected void invalidateTransform() {
1583         int type = transform.getType();
1584         int origTransformState = transformState;
1585         if (type == AffineTransform.TYPE_IDENTITY) {
1586             transformState = TRANSFORM_ISIDENT;
1587             transX = transY = 0;
1588         } else if (type == AffineTransform.TYPE_TRANSLATION) {
1589             double dtx = transform.getTranslateX();
1590             double dty = transform.getTranslateY();
1591             transX = (int) Math.floor(dtx + 0.5);
1592             transY = (int) Math.floor(dty + 0.5);
1593             if (dtx == transX && dty == transY) {
1594                 transformState = TRANSFORM_INT_TRANSLATE;
1595             } else {
1596                 transformState = TRANSFORM_ANY_TRANSLATE;
1597             }
1598         } else if ((type & (AffineTransform.TYPE_FLIP |
1599                             AffineTransform.TYPE_MASK_ROTATION |
1600                             AffineTransform.TYPE_GENERAL_TRANSFORM)) == 0)
1601         {
1602             transformState = TRANSFORM_TRANSLATESCALE;
1603             transX = transY = 0;
1604         } else {
1605             transformState = TRANSFORM_GENERIC;
1606             transX = transY = 0;
1607         }
1608 
1609         if (transformState >= TRANSFORM_TRANSLATESCALE ||
1610             origTransformState >= TRANSFORM_TRANSLATESCALE)
1611         {
1612             /* Its only in this case that the previous or current transform
1613              * was more than a translate that font info is invalidated
1614              */
1615             cachedFRC = null;
1616             this.validFontInfo = false;
1617             this.fontMetrics = null;
1618             this.glyphVectorFontInfo = null;
1619 
1620             if (transformState != origTransformState) {
1621                 invalidatePipe();
1622             }
1623         }
1624         if (strokeState != STROKE_CUSTOM) {
1625             validateBasicStroke((BasicStroke) stroke);
1626         }
1627     }
1628 
1629     /**
1630      * Returns the current Transform in the Graphics2D state.
1631      * @see #transform
1632      * @see #setTransform
1633      */
1634     @Override
1635     public AffineTransform getTransform() {
1636         if ((constrainX | constrainY) == 0 && devScale == 1) {
1637             return new AffineTransform(transform);
1638         }
1639         final double invScale = 1.0 / devScale;
1640         AffineTransform tx = new AffineTransform(invScale, 0, 0, invScale,
1641                                                  -constrainX, -constrainY);
1642         tx.concatenate(transform);
1643         return tx;
1644     }
1645 
1646     /**
1647      * Returns the current Transform ignoring the "constrain"
1648      * rectangle.
1649      */
1650     public AffineTransform cloneTransform() {
1651         return new AffineTransform(transform);
1652     }
1653 
1654     /**
1655      * Returns the current Paint in the Graphics2D state.
1656      * @see #setPaint
1657      * @see java.awt.Graphics#setColor
1658      */
1659     public Paint getPaint() {
1660         return paint;
1661     }
1662 
1663     /**
1664      * Returns the current Composite in the Graphics2D state.
1665      * @see #setComposite
1666      */
1667     public Composite getComposite() {
1668         return composite;
1669     }
1670 
1671     public Color getColor() {
1672         return foregroundColor;
1673     }
1674 
1675     /*
1676      * Validate the eargb and pixel fields against the current color.
1677      *
1678      * The eargb field must take into account the extraAlpha
1679      * value of an AlphaComposite.  It may also take into account
1680      * the Fsrc Porter-Duff blending function if such a function is
1681      * a constant (see handling of Clear mode below).  For instance,
1682      * by factoring in the (Fsrc == 0) state of the Clear mode we can
1683      * use a SrcNoEa loop just as easily as a general Alpha loop
1684      * since the math will be the same in both cases.
1685      *
1686      * The pixel field will always be the best pixel data choice for
1687      * the final result of all calculations applied to the eargb field.
1688      *
1689      * Note that this method is only necessary under the following
1690      * conditions:
1691      *     (paintState <= PAINT_ALPHA_COLOR &&
1692      *      compositeState <= COMP_CUSTOM)
1693      * though nothing bad will happen if it is run in other states.
1694      */
1695     final void validateColor() {
1696         int eargb;
1697         if (imageComp == CompositeType.Clear) {
1698             eargb = 0;
1699         } else {
1700             eargb = foregroundColor.getRGB();
1701             if (compositeState <= COMP_ALPHA &&
1702                 imageComp != CompositeType.SrcNoEa &&
1703                 imageComp != CompositeType.SrcOverNoEa)
1704             {
1705                 AlphaComposite alphacomp = (AlphaComposite) composite;
1706                 int a = Math.round(alphacomp.getAlpha() * (eargb >>> 24));
1707                 eargb = (eargb & 0x00ffffff) | (a << 24);
1708             }
1709         }
1710         this.eargb = eargb;
1711         this.pixel = surfaceData.pixelFor(eargb);
1712     }
1713 
1714     public void setColor(Color color) {
1715         if (color == null || color == paint) {
1716             return;
1717         }
1718         this.paint = foregroundColor = color;
1719         validateColor();
1720         if ((eargb >> 24) == -1) {
1721             if (paintState == PAINT_OPAQUECOLOR) {
1722                 return;
1723             }
1724             paintState = PAINT_OPAQUECOLOR;
1725             if (imageComp == CompositeType.SrcOverNoEa) {
1726                 // special case where compState depends on opacity of paint
1727                 compositeState = COMP_ISCOPY;
1728             }
1729         } else {
1730             if (paintState == PAINT_ALPHACOLOR) {
1731                 return;
1732             }
1733             paintState = PAINT_ALPHACOLOR;
1734             if (imageComp == CompositeType.SrcOverNoEa) {
1735                 // special case where compState depends on opacity of paint
1736                 compositeState = COMP_ALPHA;
1737             }
1738         }
1739         validFontInfo = false;
1740         invalidatePipe();
1741     }
1742 
1743     /**
1744      * Sets the background color in this context used for clearing a region.
1745      * When Graphics2D is constructed for a component, the backgroung color is
1746      * inherited from the component. Setting the background color in the
1747      * Graphics2D context only affects the subsequent clearRect() calls and
1748      * not the background color of the component. To change the background
1749      * of the component, use appropriate methods of the component.
1750      * @param color The background color that should be used in
1751      * subsequent calls to clearRect().
1752      * @see getBackground
1753      * @see Graphics.clearRect()
1754      */
1755     public void setBackground(Color color) {
1756         backgroundColor = color;
1757     }
1758 
1759     /**
1760      * Returns the background color used for clearing a region.
1761      * @see setBackground
1762      */
1763     public Color getBackground() {
1764         return backgroundColor;
1765     }
1766 
1767     /**
1768      * Returns the current Stroke in the Graphics2D state.
1769      * @see setStroke
1770      */
1771     public Stroke getStroke() {
1772         return stroke;
1773     }
1774 
1775     public Rectangle getClipBounds() {
1776         Rectangle r;
1777         if (clipState == CLIP_DEVICE) {
1778             r = null;
1779         } else if (transformState <= TRANSFORM_INT_TRANSLATE) {
1780             if (usrClip instanceof Rectangle) {
1781                 r = new Rectangle((Rectangle) usrClip);
1782             } else {
1783                 r = usrClip.getBounds();
1784             }
1785             r.translate(-transX, -transY);
1786         } else {
1787             r = getClip().getBounds();
1788         }
1789         return r;
1790     }
1791 
1792     public Rectangle getClipBounds(Rectangle r) {
1793         if (clipState != CLIP_DEVICE) {
1794             if (transformState <= TRANSFORM_INT_TRANSLATE) {
1795                 if (usrClip instanceof Rectangle) {
1796                     r.setBounds((Rectangle) usrClip);
1797                 } else {
1798                     r.setBounds(usrClip.getBounds());
1799                 }
1800                 r.translate(-transX, -transY);
1801             } else {
1802                 r.setBounds(getClip().getBounds());
1803             }
1804         } else if (r == null) {
1805             throw new NullPointerException("null rectangle parameter");
1806         }
1807         return r;
1808     }
1809 
1810     public boolean hitClip(int x, int y, int width, int height) {
1811         if (width <= 0 || height <= 0) {
1812             return false;
1813         }
1814         if (transformState > TRANSFORM_INT_TRANSLATE) {
1815             // Note: Technically the most accurate test would be to
1816             // raster scan the parallelogram of the transformed rectangle
1817             // and do a span for span hit test against the clip, but for
1818             // speed we approximate the test with a bounding box of the
1819             // transformed rectangle.  The cost of rasterizing the
1820             // transformed rectangle is probably high enough that it is
1821             // not worth doing so to save the caller from having to call
1822             // a rendering method where we will end up discovering the
1823             // same answer in about the same amount of time anyway.
1824             // This logic breaks down if this hit test is being performed
1825             // on the bounds of a group of shapes in which case it might
1826             // be beneficial to be a little more accurate to avoid lots
1827             // of subsequent rendering calls.  In either case, this relaxed
1828             // test should not be significantly less accurate than the
1829             // optimal test for most transforms and so the conservative
1830             // answer should not cause too much extra work.
1831 
1832             double d[] = {
1833                 x, y,
1834                 x+width, y,
1835                 x, y+height,
1836                 x+width, y+height
1837             };
1838             transform.transform(d, 0, d, 0, 4);
1839             x = (int) Math.floor(Math.min(Math.min(d[0], d[2]),
1840                                           Math.min(d[4], d[6])));
1841             y = (int) Math.floor(Math.min(Math.min(d[1], d[3]),
1842                                           Math.min(d[5], d[7])));
1843             width = (int) Math.ceil(Math.max(Math.max(d[0], d[2]),
1844                                              Math.max(d[4], d[6])));
1845             height = (int) Math.ceil(Math.max(Math.max(d[1], d[3]),
1846                                               Math.max(d[5], d[7])));
1847         } else {
1848             x += transX;
1849             y += transY;
1850             width += x;
1851             height += y;
1852         }
1853 
1854         try {
1855             if (!getCompClip().intersectsQuickCheckXYXY(x, y, width, height)) {
1856                 return false;
1857             }
1858         } catch (InvalidPipeException e) {
1859             return false;
1860         }
1861         // REMIND: We could go one step further here and examine the
1862         // non-rectangular clip shape more closely if there is one.
1863         // Since the clip has already been rasterized, the performance
1864         // penalty of doing the scan is probably still within the bounds
1865         // of a good tradeoff between speed and quality of the answer.
1866         return true;
1867     }
1868 
1869     protected void validateCompClip() {
1870         int origClipState = clipState;
1871         if (usrClip == null) {
1872             clipState = CLIP_DEVICE;
1873             clipRegion = devClip;
1874         } else if (usrClip instanceof Rectangle2D) {
1875             clipState = CLIP_RECTANGULAR;
1876             if (usrClip instanceof Rectangle) {
1877                 clipRegion = devClip.getIntersection((Rectangle)usrClip);
1878             } else {
1879                 clipRegion = devClip.getIntersection(usrClip.getBounds());
1880             }
1881         } else {
1882             PathIterator cpi = usrClip.getPathIterator(null);
1883             int box[] = new int[4];
1884             ShapeSpanIterator sr = LoopPipe.getFillSSI(this);
1885             try {
1886                 sr.setOutputArea(devClip);
1887                 sr.appendPath(cpi);
1888                 sr.getPathBox(box);
1889                 Region r = Region.getInstance(box);
1890                 r.appendSpans(sr);
1891                 clipRegion = r;
1892                 clipState =
1893                     r.isRectangular() ? CLIP_RECTANGULAR : CLIP_SHAPE;
1894             } finally {
1895                 sr.dispose();
1896             }
1897         }
1898         if (origClipState != clipState &&
1899             (clipState == CLIP_SHAPE || origClipState == CLIP_SHAPE))
1900         {
1901             validFontInfo = false;
1902             invalidatePipe();
1903         }
1904     }
1905 
1906     static final int NON_RECTILINEAR_TRANSFORM_MASK =
1907         (AffineTransform.TYPE_GENERAL_TRANSFORM |
1908          AffineTransform.TYPE_GENERAL_ROTATION);
1909 
1910     protected Shape transformShape(Shape s) {
1911         if (s == null) {
1912             return null;
1913         }
1914         if (transformState > TRANSFORM_INT_TRANSLATE) {
1915             return transformShape(transform, s);
1916         } else {
1917             return transformShape(transX, transY, s);
1918         }
1919     }
1920 
1921     public Shape untransformShape(Shape s) {
1922         if (s == null) {
1923             return null;
1924         }
1925         if (transformState > TRANSFORM_INT_TRANSLATE) {
1926             try {
1927                 return transformShape(transform.createInverse(), s);
1928             } catch (NoninvertibleTransformException e) {
1929                 return null;
1930             }
1931         } else {
1932             return transformShape(-transX, -transY, s);
1933         }
1934     }
1935 
1936     protected static Shape transformShape(int tx, int ty, Shape s) {
1937         if (s == null) {
1938             return null;
1939         }
1940 
1941         if (s instanceof Rectangle) {
1942             Rectangle r = s.getBounds();
1943             r.translate(tx, ty);
1944             return r;
1945         }
1946         if (s instanceof Rectangle2D) {
1947             Rectangle2D rect = (Rectangle2D) s;
1948             return new Rectangle2D.Double(rect.getX() + tx,
1949                                           rect.getY() + ty,
1950                                           rect.getWidth(),
1951                                           rect.getHeight());
1952         }
1953 
1954         if (tx == 0 && ty == 0) {
1955             return cloneShape(s);
1956         }
1957 
1958         AffineTransform mat = AffineTransform.getTranslateInstance(tx, ty);
1959         return mat.createTransformedShape(s);
1960     }
1961 
1962     protected static Shape transformShape(AffineTransform tx, Shape clip) {
1963         if (clip == null) {
1964             return null;
1965         }
1966 
1967         if (clip instanceof Rectangle2D &&
1968             (tx.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)
1969         {
1970             Rectangle2D rect = (Rectangle2D) clip;
1971             double matrix[] = new double[4];
1972             matrix[0] = rect.getX();
1973             matrix[1] = rect.getY();
1974             matrix[2] = matrix[0] + rect.getWidth();
1975             matrix[3] = matrix[1] + rect.getHeight();
1976             tx.transform(matrix, 0, matrix, 0, 2);
1977             rect = new Rectangle2D.Float();
1978             rect.setFrameFromDiagonal(matrix[0], matrix[1],
1979                                       matrix[2], matrix[3]);
1980             return rect;
1981         }
1982 
1983         if (tx.isIdentity()) {
1984             return cloneShape(clip);
1985         }
1986 
1987         return tx.createTransformedShape(clip);
1988     }
1989 
1990     public void clipRect(int x, int y, int w, int h) {
1991         clip(new Rectangle(x, y, w, h));
1992     }
1993 
1994     public void setClip(int x, int y, int w, int h) {
1995         setClip(new Rectangle(x, y, w, h));
1996     }
1997 
1998     public Shape getClip() {
1999         return untransformShape(usrClip);
2000     }
2001 
2002     public void setClip(Shape sh) {
2003         usrClip = transformShape(sh);
2004         validateCompClip();
2005     }
2006 
2007     /**
2008      * Intersects the current clip with the specified Path and sets the
2009      * current clip to the resulting intersection. The clip is transformed
2010      * with the current transform in the Graphics2D state before being
2011      * intersected with the current clip. This method is used to make the
2012      * current clip smaller. To make the clip larger, use any setClip method.
2013      * @param p The Path to be intersected with the current clip.
2014      */
2015     public void clip(Shape s) {
2016         s = transformShape(s);
2017         if (usrClip != null) {
2018             s = intersectShapes(usrClip, s, true, true);
2019         }
2020         usrClip = s;
2021         validateCompClip();
2022     }
2023 
2024     public void setPaintMode() {
2025         setComposite(AlphaComposite.SrcOver);
2026     }
2027 
2028     public void setXORMode(Color c) {
2029         if (c == null) {
2030             throw new IllegalArgumentException("null XORColor");
2031         }
2032         setComposite(new XORComposite(c, surfaceData));
2033     }
2034 
2035     Blit lastCAblit;
2036     Composite lastCAcomp;
2037 
2038     public void copyArea(int x, int y, int w, int h, int dx, int dy) {
2039         try {
2040             doCopyArea(x, y, w, h, dx, dy);
2041         } catch (InvalidPipeException e) {
2042             try {
2043                 revalidateAll();
2044                 doCopyArea(x, y, w, h, dx, dy);
2045             } catch (InvalidPipeException e2) {
2046                 // Still catching the exception; we are not yet ready to
2047                 // validate the surfaceData correctly.  Fail for now and
2048                 // try again next time around.
2049             }
2050         } finally {
2051             surfaceData.markDirty();
2052         }
2053     }
2054 
2055     private void doCopyArea(int x, int y, int w, int h, int dx, int dy) {
2056         if (w <= 0 || h <= 0) {
2057             return;
2058         }
2059         SurfaceData theData = surfaceData;
2060         if (theData.copyArea(this, x, y, w, h, dx, dy)) {
2061             return;
2062         }
2063         if (transformState >= TRANSFORM_TRANSLATESCALE) {
2064             throw new InternalError("transformed copyArea not implemented yet");
2065         }
2066         // REMIND: This method does not deal with missing data from the
2067         // source object (i.e. it does not send exposure events...)
2068 
2069         Region clip = getCompClip();
2070 
2071         Composite comp = composite;
2072         if (lastCAcomp != comp) {
2073             SurfaceType dsttype = theData.getSurfaceType();
2074             CompositeType comptype = imageComp;
2075             if (CompositeType.SrcOverNoEa.equals(comptype) &&
2076                 theData.getTransparency() == Transparency.OPAQUE)
2077             {
2078                 comptype = CompositeType.SrcNoEa;
2079             }
2080             lastCAblit = Blit.locate(dsttype, comptype, dsttype);
2081             lastCAcomp = comp;
2082         }
2083 
2084         x += transX;
2085         y += transY;
2086 
2087         Blit ob = lastCAblit;
2088         if (dy == 0 && dx > 0 && dx < w) {
2089             while (w > 0) {
2090                 int partW = Math.min(w, dx);
2091                 w -= partW;
2092                 int sx = x + w;
2093                 ob.Blit(theData, theData, comp, clip,
2094                         sx, y, sx+dx, y+dy, partW, h);
2095             }
2096             return;
2097         }
2098         if (dy > 0 && dy < h && dx > -w && dx < w) {
2099             while (h > 0) {
2100                 int partH = Math.min(h, dy);
2101                 h -= partH;
2102                 int sy = y + h;
2103                 ob.Blit(theData, theData, comp, clip,
2104                         x, sy, x+dx, sy+dy, w, partH);
2105             }
2106             return;
2107         }
2108         ob.Blit(theData, theData, comp, clip, x, y, x+dx, y+dy, w, h);
2109     }
2110 
2111     /*
2112     public void XcopyArea(int x, int y, int w, int h, int dx, int dy) {
2113         Rectangle rect = new Rectangle(x, y, w, h);
2114         rect = transformBounds(rect, transform);
2115         Point2D    point = new Point2D.Float(dx, dy);
2116         Point2D    root  = new Point2D.Float(0, 0);
2117         point = transform.transform(point, point);
2118         root  = transform.transform(root, root);
2119         int fdx = (int)(point.getX()-root.getX());
2120         int fdy = (int)(point.getY()-root.getY());
2121 
2122         Rectangle r = getCompBounds().intersection(rect.getBounds());
2123 
2124         if (r.isEmpty()) {
2125             return;
2126         }
2127 
2128         // Begin Rasterizer for Clip Shape
2129         boolean skipClip = true;
2130         byte[] clipAlpha = null;
2131 
2132         if (clipState == CLIP_SHAPE) {
2133 
2134             int box[] = new int[4];
2135 
2136             clipRegion.getBounds(box);
2137             Rectangle devR = new Rectangle(box[0], box[1],
2138                                            box[2] - box[0],
2139                                            box[3] - box[1]);
2140             if (!devR.isEmpty()) {
2141                 OutputManager mgr = getOutputManager();
2142                 RegionIterator ri = clipRegion.getIterator();
2143                 while (ri.nextYRange(box)) {
2144                     int spany = box[1];
2145                     int spanh = box[3] - spany;
2146                     while (ri.nextXBand(box)) {
2147                         int spanx = box[0];
2148                         int spanw = box[2] - spanx;
2149                         mgr.copyArea(this, null,
2150                                      spanw, 0,
2151                                      spanx, spany,
2152                                      spanw, spanh,
2153                                      fdx, fdy,
2154                                      null);
2155                     }
2156                 }
2157             }
2158             return;
2159         }
2160         // End Rasterizer for Clip Shape
2161 
2162         getOutputManager().copyArea(this, null,
2163                                     r.width, 0,
2164                                     r.x, r.y, r.width,
2165                                     r.height, fdx, fdy,
2166                                     null);
2167     }
2168     */
2169 
2170     public void drawLine(int x1, int y1, int x2, int y2) {
2171         try {
2172             drawpipe.drawLine(this, x1, y1, x2, y2);
2173         } catch (InvalidPipeException e) {
2174             try {
2175                 revalidateAll();
2176                 drawpipe.drawLine(this, x1, y1, x2, y2);
2177             } catch (InvalidPipeException e2) {
2178                 // Still catching the exception; we are not yet ready to
2179                 // validate the surfaceData correctly.  Fail for now and
2180                 // try again next time around.
2181             }
2182         } finally {
2183             surfaceData.markDirty();
2184         }
2185     }
2186 
2187     public void drawRoundRect(int x, int y, int w, int h, int arcW, int arcH) {
2188         try {
2189             drawpipe.drawRoundRect(this, x, y, w, h, arcW, arcH);
2190         } catch (InvalidPipeException e) {
2191             try {
2192                 revalidateAll();
2193                 drawpipe.drawRoundRect(this, x, y, w, h, arcW, arcH);
2194             } catch (InvalidPipeException e2) {
2195                 // Still catching the exception; we are not yet ready to
2196                 // validate the surfaceData correctly.  Fail for now and
2197                 // try again next time around.
2198             }
2199         } finally {
2200             surfaceData.markDirty();
2201         }
2202     }
2203 
2204     public void fillRoundRect(int x, int y, int w, int h, int arcW, int arcH) {
2205         try {
2206             fillpipe.fillRoundRect(this, x, y, w, h, arcW, arcH);
2207         } catch (InvalidPipeException e) {
2208             try {
2209                 revalidateAll();
2210                 fillpipe.fillRoundRect(this, x, y, w, h, arcW, arcH);
2211             } catch (InvalidPipeException e2) {
2212                 // Still catching the exception; we are not yet ready to
2213                 // validate the surfaceData correctly.  Fail for now and
2214                 // try again next time around.
2215             }
2216         } finally {
2217             surfaceData.markDirty();
2218         }
2219     }
2220 
2221     public void drawOval(int x, int y, int w, int h) {
2222         try {
2223             drawpipe.drawOval(this, x, y, w, h);
2224         } catch (InvalidPipeException e) {
2225             try {
2226                 revalidateAll();
2227                 drawpipe.drawOval(this, x, y, w, h);
2228             } catch (InvalidPipeException e2) {
2229                 // Still catching the exception; we are not yet ready to
2230                 // validate the surfaceData correctly.  Fail for now and
2231                 // try again next time around.
2232             }
2233         } finally {
2234             surfaceData.markDirty();
2235         }
2236     }
2237 
2238     public void fillOval(int x, int y, int w, int h) {
2239         try {
2240             fillpipe.fillOval(this, x, y, w, h);
2241         } catch (InvalidPipeException e) {
2242             try {
2243                 revalidateAll();
2244                 fillpipe.fillOval(this, x, y, w, h);
2245             } catch (InvalidPipeException e2) {
2246                 // Still catching the exception; we are not yet ready to
2247                 // validate the surfaceData correctly.  Fail for now and
2248                 // try again next time around.
2249             }
2250         } finally {
2251             surfaceData.markDirty();
2252         }
2253     }
2254 
2255     public void drawArc(int x, int y, int w, int h,
2256                         int startAngl, int arcAngl) {
2257         try {
2258             drawpipe.drawArc(this, x, y, w, h, startAngl, arcAngl);
2259         } catch (InvalidPipeException e) {
2260             try {
2261                 revalidateAll();
2262                 drawpipe.drawArc(this, x, y, w, h, startAngl, arcAngl);
2263             } catch (InvalidPipeException e2) {
2264                 // Still catching the exception; we are not yet ready to
2265                 // validate the surfaceData correctly.  Fail for now and
2266                 // try again next time around.
2267             }
2268         } finally {
2269             surfaceData.markDirty();
2270         }
2271     }
2272 
2273     public void fillArc(int x, int y, int w, int h,
2274                         int startAngl, int arcAngl) {
2275         try {
2276             fillpipe.fillArc(this, x, y, w, h, startAngl, arcAngl);
2277         } catch (InvalidPipeException e) {
2278             try {
2279                 revalidateAll();
2280                 fillpipe.fillArc(this, x, y, w, h, startAngl, arcAngl);
2281             } catch (InvalidPipeException e2) {
2282                 // Still catching the exception; we are not yet ready to
2283                 // validate the surfaceData correctly.  Fail for now and
2284                 // try again next time around.
2285             }
2286         } finally {
2287             surfaceData.markDirty();
2288         }
2289     }
2290 
2291     public void drawPolyline(int xPoints[], int yPoints[], int nPoints) {
2292         try {
2293             drawpipe.drawPolyline(this, xPoints, yPoints, nPoints);
2294         } catch (InvalidPipeException e) {
2295             try {
2296                 revalidateAll();
2297                 drawpipe.drawPolyline(this, xPoints, yPoints, nPoints);
2298             } catch (InvalidPipeException e2) {
2299                 // Still catching the exception; we are not yet ready to
2300                 // validate the surfaceData correctly.  Fail for now and
2301                 // try again next time around.
2302             }
2303         } finally {
2304             surfaceData.markDirty();
2305         }
2306     }
2307 
2308     public void drawPolygon(int xPoints[], int yPoints[], int nPoints) {
2309         try {
2310             drawpipe.drawPolygon(this, xPoints, yPoints, nPoints);
2311         } catch (InvalidPipeException e) {
2312             try {
2313                 revalidateAll();
2314                 drawpipe.drawPolygon(this, xPoints, yPoints, nPoints);
2315             } catch (InvalidPipeException e2) {
2316                 // Still catching the exception; we are not yet ready to
2317                 // validate the surfaceData correctly.  Fail for now and
2318                 // try again next time around.
2319             }
2320         } finally {
2321             surfaceData.markDirty();
2322         }
2323     }
2324 
2325     public void fillPolygon(int xPoints[], int yPoints[], int nPoints) {
2326         try {
2327             fillpipe.fillPolygon(this, xPoints, yPoints, nPoints);
2328         } catch (InvalidPipeException e) {
2329             try {
2330                 revalidateAll();
2331                 fillpipe.fillPolygon(this, xPoints, yPoints, nPoints);
2332             } catch (InvalidPipeException e2) {
2333                 // Still catching the exception; we are not yet ready to
2334                 // validate the surfaceData correctly.  Fail for now and
2335                 // try again next time around.
2336             }
2337         } finally {
2338             surfaceData.markDirty();
2339         }
2340     }
2341 
2342     public void drawRect (int x, int y, int w, int h) {
2343         try {
2344             drawpipe.drawRect(this, x, y, w, h);
2345         } catch (InvalidPipeException e) {
2346             try {
2347                 revalidateAll();
2348                 drawpipe.drawRect(this, x, y, w, h);
2349             } catch (InvalidPipeException e2) {
2350                 // Still catching the exception; we are not yet ready to
2351                 // validate the surfaceData correctly.  Fail for now and
2352                 // try again next time around.
2353             }
2354         } finally {
2355             surfaceData.markDirty();
2356         }
2357     }
2358 
2359     public void fillRect (int x, int y, int w, int h) {
2360         try {
2361             fillpipe.fillRect(this, x, y, w, h);
2362         } catch (InvalidPipeException e) {
2363             try {
2364                 revalidateAll();
2365                 fillpipe.fillRect(this, x, y, w, h);
2366             } catch (InvalidPipeException e2) {
2367                 // Still catching the exception; we are not yet ready to
2368                 // validate the surfaceData correctly.  Fail for now and
2369                 // try again next time around.
2370             }
2371         } finally {
2372             surfaceData.markDirty();
2373         }
2374     }
2375 
2376     private void revalidateAll() {
2377         try {
2378             // REMIND: This locking needs to be done around the
2379             // caller of this method so that the pipe stays valid
2380             // long enough to call the new primitive.
2381             // REMIND: No locking yet in screen SurfaceData objects!
2382             // surfaceData.lock();
2383             surfaceData = surfaceData.getReplacement();
2384             if (surfaceData == null) {
2385                 surfaceData = NullSurfaceData.theInstance;
2386             }
2387 
2388             // this will recalculate the composite clip
2389             setDevClip(surfaceData.getBounds());
2390 
2391             if (paintState <= PAINT_ALPHACOLOR) {
2392                 validateColor();
2393             }
2394             if (composite instanceof XORComposite) {
2395                 Color c = ((XORComposite) composite).getXorColor();
2396                 setComposite(new XORComposite(c, surfaceData));
2397             }
2398             validatePipe();
2399         } finally {
2400             // REMIND: No locking yet in screen SurfaceData objects!
2401             // surfaceData.unlock();
2402         }
2403     }
2404 
2405     public void clearRect(int x, int y, int w, int h) {
2406         // REMIND: has some "interesting" consequences if threads are
2407         // not synchronized
2408         Composite c = composite;
2409         Paint p = paint;
2410         setComposite(AlphaComposite.Src);
2411         setColor(getBackground());
2412         fillRect(x, y, w, h);
2413         setPaint(p);
2414         setComposite(c);
2415     }
2416 
2417     /**
2418      * Strokes the outline of a Path using the settings of the current
2419      * graphics state.  The rendering attributes applied include the
2420      * clip, transform, paint or color, composite and stroke attributes.
2421      * @param p The path to be drawn.
2422      * @see #setStroke
2423      * @see #setPaint
2424      * @see java.awt.Graphics#setColor
2425      * @see #transform
2426      * @see #setTransform
2427      * @see #clip
2428      * @see #setClip
2429      * @see #setComposite
2430      */
2431     public void draw(Shape s) {
2432         try {
2433             shapepipe.draw(this, s);
2434         } catch (InvalidPipeException e) {
2435             try {
2436                 revalidateAll();
2437                 shapepipe.draw(this, s);
2438             } catch (InvalidPipeException e2) {
2439                 // Still catching the exception; we are not yet ready to
2440                 // validate the surfaceData correctly.  Fail for now and
2441                 // try again next time around.
2442             }
2443         } finally {
2444             surfaceData.markDirty();
2445         }
2446     }
2447 
2448 
2449     /**
2450      * Fills the interior of a Path using the settings of the current
2451      * graphics state. The rendering attributes applied include the
2452      * clip, transform, paint or color, and composite.
2453      * @see #setPaint
2454      * @see java.awt.Graphics#setColor
2455      * @see #transform
2456      * @see #setTransform
2457      * @see #setComposite
2458      * @see #clip
2459      * @see #setClip
2460      */
2461     public void fill(Shape s) {
2462         try {
2463             shapepipe.fill(this, s);
2464         } catch (InvalidPipeException e) {
2465             try {
2466                 revalidateAll();
2467                 shapepipe.fill(this, s);
2468             } catch (InvalidPipeException e2) {
2469                 // Still catching the exception; we are not yet ready to
2470                 // validate the surfaceData correctly.  Fail for now and
2471                 // try again next time around.
2472             }
2473         } finally {
2474             surfaceData.markDirty();
2475         }
2476     }
2477 
2478     /**
2479      * Returns true if the given AffineTransform is an integer
2480      * translation.
2481      */
2482     private static boolean isIntegerTranslation(AffineTransform xform) {
2483         if (xform.isIdentity()) {
2484             return true;
2485         }
2486         if (xform.getType() == AffineTransform.TYPE_TRANSLATION) {
2487             double tx = xform.getTranslateX();
2488             double ty = xform.getTranslateY();
2489             return (tx == (int)tx && ty == (int)ty);
2490         }
2491         return false;
2492     }
2493 
2494     /**
2495      * Returns the index of the tile corresponding to the supplied position
2496      * given the tile grid offset and size along the same axis.
2497      */
2498     private static int getTileIndex(int p, int tileGridOffset, int tileSize) {
2499         p -= tileGridOffset;
2500         if (p < 0) {
2501             p += 1 - tileSize;          // force round to -infinity (ceiling)
2502         }
2503         return p/tileSize;
2504     }
2505 
2506     /**
2507      * Returns a rectangle in image coordinates that may be required
2508      * in order to draw the given image into the given clipping region
2509      * through a pair of AffineTransforms.  In addition, horizontal and
2510      * vertical padding factors for antialising and interpolation may
2511      * be used.
2512      */
2513     private static Rectangle getImageRegion(RenderedImage img,
2514                                             Region compClip,
2515                                             AffineTransform transform,
2516                                             AffineTransform xform,
2517                                             int padX, int padY) {
2518         Rectangle imageRect =
2519             new Rectangle(img.getMinX(), img.getMinY(),
2520                           img.getWidth(), img.getHeight());
2521 
2522         Rectangle result = null;
2523         try {
2524             double p[] = new double[8];
2525             p[0] = p[2] = compClip.getLoX();
2526             p[4] = p[6] = compClip.getHiX();
2527             p[1] = p[5] = compClip.getLoY();
2528             p[3] = p[7] = compClip.getHiY();
2529 
2530             // Inverse transform the output bounding rect
2531             transform.inverseTransform(p, 0, p, 0, 4);
2532             xform.inverseTransform(p, 0, p, 0, 4);
2533 
2534             // Determine a bounding box for the inverse transformed region
2535             double x0,x1,y0,y1;
2536             x0 = x1 = p[0];
2537             y0 = y1 = p[1];
2538 
2539             for (int i = 2; i < 8; ) {
2540                 double pt = p[i++];
2541                 if (pt < x0)  {
2542                     x0 = pt;
2543                 } else if (pt > x1) {
2544                     x1 = pt;
2545                 }
2546                 pt = p[i++];
2547                 if (pt < y0)  {
2548                     y0 = pt;
2549                 } else if (pt > y1) {
2550                     y1 = pt;
2551                 }
2552             }
2553 
2554             // This is padding for anti-aliasing and such.  It may
2555             // be more than is needed.
2556             int x = (int)x0 - padX;
2557             int w = (int)(x1 - x0 + 2*padX);
2558             int y = (int)y0 - padY;
2559             int h = (int)(y1 - y0 + 2*padY);
2560 
2561             Rectangle clipRect = new Rectangle(x,y,w,h);
2562             result = clipRect.intersection(imageRect);
2563         } catch (NoninvertibleTransformException nte) {
2564             // Worst case bounds are the bounds of the image.
2565             result = imageRect;
2566         }
2567 
2568         return result;
2569     }
2570 
2571     /**
2572      * Draws an image, applying a transform from image space into user space
2573      * before drawing.
2574      * The transformation from user space into device space is done with
2575      * the current transform in the Graphics2D.
2576      * The given transformation is applied to the image before the
2577      * transform attribute in the Graphics2D state is applied.
2578      * The rendering attributes applied include the clip, transform,
2579      * and composite attributes. Note that the result is
2580      * undefined, if the given transform is noninvertible.
2581      * @param img The image to be drawn. Does nothing if img is null.
2582      * @param xform The transformation from image space into user space.
2583      * @see #transform
2584      * @see #setTransform
2585      * @see #setComposite
2586      * @see #clip
2587      * @see #setClip
2588      */
2589     public void drawRenderedImage(RenderedImage img,
2590                                   AffineTransform xform) {
2591 
2592         if (img == null) {
2593             return;
2594         }
2595 
2596         // BufferedImage case: use a simple drawImage call
2597         if (img instanceof BufferedImage) {
2598             BufferedImage bufImg = (BufferedImage)img;
2599             drawImage(bufImg,xform,null);
2600             return;
2601         }
2602 
2603         // transformState tracks the state of transform and
2604         // transX, transY contain the integer casts of the
2605         // translation factors
2606         boolean isIntegerTranslate =
2607             (transformState <= TRANSFORM_INT_TRANSLATE) &&
2608             isIntegerTranslation(xform);
2609 
2610         // Include padding for interpolation/antialiasing if necessary
2611         int pad = isIntegerTranslate ? 0 : 3;
2612 
2613         Region clip;
2614         try {
2615             clip = getCompClip();
2616         } catch (InvalidPipeException e) {
2617             return;
2618         }
2619 
2620         // Determine the region of the image that may contribute to
2621         // the clipped drawing area
2622         Rectangle region = getImageRegion(img,
2623                                           clip,
2624                                           transform,
2625                                           xform,
2626                                           pad, pad);
2627         if (region.width <= 0 || region.height <= 0) {
2628             return;
2629         }
2630 
2631         // Attempt to optimize integer translation of tiled images.
2632         // Although theoretically we are O.K. if the concatenation of
2633         // the user transform and the device transform is an integer
2634         // translation, we'll play it safe and only optimize the case
2635         // where both are integer translations.
2636         if (isIntegerTranslate) {
2637             // Use optimized code
2638             // Note that drawTranslatedRenderedImage calls copyImage
2639             // which takes the user space to device space transform into
2640             // account, but we need to provide the image space to user space
2641             // translations.
2642 
2643             drawTranslatedRenderedImage(img, region,
2644                                         (int) xform.getTranslateX(),
2645                                         (int) xform.getTranslateY());
2646             return;
2647         }
2648 
2649         // General case: cobble the necessary region into a single Raster
2650         Raster raster = img.getData(region);
2651 
2652         // Make a new Raster with the same contents as raster
2653         // but starting at (0, 0).  This raster is thus in the same
2654         // coordinate system as the SampleModel of the original raster.
2655         WritableRaster wRaster =
2656               Raster.createWritableRaster(raster.getSampleModel(),
2657                                           raster.getDataBuffer(),
2658                                           null);
2659 
2660         // If the original raster was in a different coordinate
2661         // system than its SampleModel, we need to perform an
2662         // additional translation in order to get the (minX, minY)
2663         // pixel of raster to be pixel (0, 0) of wRaster.  We also
2664         // have to have the correct width and height.
2665         int minX = raster.getMinX();
2666         int minY = raster.getMinY();
2667         int width = raster.getWidth();
2668         int height = raster.getHeight();
2669         int px = minX - raster.getSampleModelTranslateX();
2670         int py = minY - raster.getSampleModelTranslateY();
2671         if (px != 0 || py != 0 || width != wRaster.getWidth() ||
2672             height != wRaster.getHeight()) {
2673             wRaster =
2674                 wRaster.createWritableChild(px,
2675                                             py,
2676                                             width,
2677                                             height,
2678                                             0, 0,
2679                                             null);
2680         }
2681 
2682         // Now we have a BufferedImage starting at (0, 0)
2683         // with the same contents that started at (minX, minY)
2684         // in raster.  So we must draw the BufferedImage with a
2685         // translation of (minX, minY).
2686         AffineTransform transXform = (AffineTransform)xform.clone();
2687         transXform.translate(minX, minY);
2688 
2689         ColorModel cm = img.getColorModel();
2690         BufferedImage bufImg = new BufferedImage(cm,
2691                                                  wRaster,
2692                                                  cm.isAlphaPremultiplied(),
2693                                                  null);
2694         drawImage(bufImg, transXform, null);
2695     }
2696 
2697     /**
2698      * Intersects <code>destRect</code> with <code>clip</code> and
2699      * overwrites <code>destRect</code> with the result.
2700      * Returns false if the intersection was empty, true otherwise.
2701      */
2702     private boolean clipTo(Rectangle destRect, Rectangle clip) {
2703         int x1 = Math.max(destRect.x, clip.x);
2704         int x2 = Math.min(destRect.x + destRect.width, clip.x + clip.width);
2705         int y1 = Math.max(destRect.y, clip.y);
2706         int y2 = Math.min(destRect.y + destRect.height, clip.y + clip.height);
2707         if (((x2 - x1) < 0) || ((y2 - y1) < 0)) {
2708             destRect.width = -1; // Set both just to be safe
2709             destRect.height = -1;
2710             return false;
2711         } else {
2712             destRect.x = x1;
2713             destRect.y = y1;
2714             destRect.width = x2 - x1;
2715             destRect.height = y2 - y1;
2716             return true;
2717         }
2718     }
2719 
2720     /**
2721      * Draw a portion of a RenderedImage tile-by-tile with a given
2722      * integer image to user space translation.  The user to
2723      * device transform must also be an integer translation.
2724      */
2725     private void drawTranslatedRenderedImage(RenderedImage img,
2726                                              Rectangle region,
2727                                              int i2uTransX,
2728                                              int i2uTransY) {
2729         // Cache tile grid info
2730         int tileGridXOffset = img.getTileGridXOffset();
2731         int tileGridYOffset = img.getTileGridYOffset();
2732         int tileWidth = img.getTileWidth();
2733         int tileHeight = img.getTileHeight();
2734 
2735         // Determine the tile index extrema in each direction
2736         int minTileX =
2737             getTileIndex(region.x, tileGridXOffset, tileWidth);
2738         int minTileY =
2739             getTileIndex(region.y, tileGridYOffset, tileHeight);
2740         int maxTileX =
2741             getTileIndex(region.x + region.width - 1,
2742                          tileGridXOffset, tileWidth);
2743         int maxTileY =
2744             getTileIndex(region.y + region.height - 1,
2745                          tileGridYOffset, tileHeight);
2746 
2747         // Create a single ColorModel to use for all BufferedImages
2748         ColorModel colorModel = img.getColorModel();
2749 
2750         // Reuse the same Rectangle for each iteration
2751         Rectangle tileRect = new Rectangle();
2752 
2753         for (int ty = minTileY; ty <= maxTileY; ty++) {
2754             for (int tx = minTileX; tx <= maxTileX; tx++) {
2755                 // Get the current tile.
2756                 Raster raster = img.getTile(tx, ty);
2757 
2758                 // Fill in tileRect with the tile bounds
2759                 tileRect.x = tx*tileWidth + tileGridXOffset;
2760                 tileRect.y = ty*tileHeight + tileGridYOffset;
2761                 tileRect.width = tileWidth;
2762                 tileRect.height = tileHeight;
2763 
2764                 // Clip the tile against the image bounds and
2765                 // backwards mapped clip region
2766                 // The result can't be empty
2767                 clipTo(tileRect, region);
2768 
2769                 // Create a WritableRaster containing the tile
2770                 WritableRaster wRaster = null;
2771                 if (raster instanceof WritableRaster) {
2772                     wRaster = (WritableRaster)raster;
2773                 } else {
2774                     // Create a WritableRaster in the same coordinate system
2775                     // as the original raster.
2776                     wRaster =
2777                         Raster.createWritableRaster(raster.getSampleModel(),
2778                                                     raster.getDataBuffer(),
2779                                                     null);
2780                 }
2781 
2782                 // Translate wRaster to start at (0, 0) and to contain
2783                 // only the relevent portion of the tile
2784                 wRaster = wRaster.createWritableChild(tileRect.x, tileRect.y,
2785                                                       tileRect.width,
2786                                                       tileRect.height,
2787                                                       0, 0,
2788                                                       null);
2789 
2790                 // Wrap wRaster in a BufferedImage
2791                 BufferedImage bufImg =
2792                     new BufferedImage(colorModel,
2793                                       wRaster,
2794                                       colorModel.isAlphaPremultiplied(),
2795                                       null);
2796                 // Now we have a BufferedImage starting at (0, 0) that
2797                 // represents data from a Raster starting at
2798                 // (tileRect.x, tileRect.y).  Additionally, it needs
2799                 // to be translated by (i2uTransX, i2uTransY).  We call
2800                 // copyImage to draw just the region of interest
2801                 // without needing to create a child image.
2802                 copyImage(bufImg, tileRect.x + i2uTransX,
2803                           tileRect.y + i2uTransY, 0, 0, tileRect.width,
2804                           tileRect.height, null, null);
2805             }
2806         }
2807     }
2808 
2809     public void drawRenderableImage(RenderableImage img,
2810                                     AffineTransform xform) {
2811 
2812         if (img == null) {
2813             return;
2814         }
2815 
2816         AffineTransform pipeTransform = transform;
2817         AffineTransform concatTransform = new AffineTransform(xform);
2818         concatTransform.concatenate(pipeTransform);
2819         AffineTransform reverseTransform;
2820 
2821         RenderContext rc = new RenderContext(concatTransform);
2822 
2823         try {
2824             reverseTransform = pipeTransform.createInverse();
2825         } catch (NoninvertibleTransformException nte) {
2826             rc = new RenderContext(pipeTransform);
2827             reverseTransform = new AffineTransform();
2828         }
2829 
2830         RenderedImage rendering = img.createRendering(rc);
2831         drawRenderedImage(rendering,reverseTransform);
2832     }
2833 
2834 
2835 
2836     /*
2837      * Transform the bounding box of the BufferedImage
2838      */
2839     protected Rectangle transformBounds(Rectangle rect,
2840                                         AffineTransform tx) {
2841         if (tx.isIdentity()) {
2842             return rect;
2843         }
2844 
2845         Shape s = transformShape(tx, rect);
2846         return s.getBounds();
2847     }
2848 
2849     // text rendering methods
2850     public void drawString(String str, int x, int y) {
2851         if (str == null) {
2852             throw new NullPointerException("String is null");
2853         }
2854 
2855         if (font.hasLayoutAttributes()) {
2856             if (str.length() == 0) {
2857                 return;
2858             }
2859             new TextLayout(str, font, getFontRenderContext()).draw(this, x, y);
2860             return;
2861         }
2862 
2863         try {
2864             textpipe.drawString(this, str, x, y);
2865         } catch (InvalidPipeException e) {
2866             try {
2867                 revalidateAll();
2868                 textpipe.drawString(this, str, x, y);
2869             } catch (InvalidPipeException e2) {
2870                 // Still catching the exception; we are not yet ready to
2871                 // validate the surfaceData correctly.  Fail for now and
2872                 // try again next time around.
2873             }
2874         } finally {
2875             surfaceData.markDirty();
2876         }
2877     }
2878 
2879     public void drawString(String str, float x, float y) {
2880         if (str == null) {
2881             throw new NullPointerException("String is null");
2882         }
2883 
2884         if (font.hasLayoutAttributes()) {
2885             if (str.length() == 0) {
2886                 return;
2887             }
2888             new TextLayout(str, font, getFontRenderContext()).draw(this, x, y);
2889             return;
2890         }
2891 
2892         try {
2893             textpipe.drawString(this, str, x, y);
2894         } catch (InvalidPipeException e) {
2895             try {
2896                 revalidateAll();
2897                 textpipe.drawString(this, str, x, y);
2898             } catch (InvalidPipeException e2) {
2899                 // Still catching the exception; we are not yet ready to
2900                 // validate the surfaceData correctly.  Fail for now and
2901                 // try again next time around.
2902             }
2903         } finally {
2904             surfaceData.markDirty();
2905         }
2906     }
2907 
2908     public void drawString(AttributedCharacterIterator iterator,
2909                            int x, int y) {
2910         if (iterator == null) {
2911             throw new NullPointerException("AttributedCharacterIterator is null");
2912         }
2913         if (iterator.getBeginIndex() == iterator.getEndIndex()) {
2914             return; /* nothing to draw */
2915         }
2916         TextLayout tl = new TextLayout(iterator, getFontRenderContext());
2917         tl.draw(this, (float) x, (float) y);
2918     }
2919 
2920     public void drawString(AttributedCharacterIterator iterator,
2921                            float x, float y) {
2922         if (iterator == null) {
2923             throw new NullPointerException("AttributedCharacterIterator is null");
2924         }
2925         if (iterator.getBeginIndex() == iterator.getEndIndex()) {
2926             return; /* nothing to draw */
2927         }
2928         TextLayout tl = new TextLayout(iterator, getFontRenderContext());
2929         tl.draw(this, x, y);
2930     }
2931 
2932     public void drawGlyphVector(GlyphVector gv, float x, float y)
2933     {
2934         if (gv == null) {
2935             throw new NullPointerException("GlyphVector is null");
2936         }
2937 
2938         try {
2939             textpipe.drawGlyphVector(this, gv, x, y);
2940         } catch (InvalidPipeException e) {
2941             try {
2942                 revalidateAll();
2943                 textpipe.drawGlyphVector(this, gv, x, y);
2944             } catch (InvalidPipeException e2) {
2945                 // Still catching the exception; we are not yet ready to
2946                 // validate the surfaceData correctly.  Fail for now and
2947                 // try again next time around.
2948             }
2949         } finally {
2950             surfaceData.markDirty();
2951         }
2952     }
2953 
2954     public void drawChars(char data[], int offset, int length, int x, int y) {
2955 
2956         if (data == null) {
2957             throw new NullPointerException("char data is null");
2958         }
2959         if (offset < 0 || length < 0 || offset + length > data.length) {
2960             throw new ArrayIndexOutOfBoundsException("bad offset/length");
2961         }
2962         if (font.hasLayoutAttributes()) {
2963             if (data.length == 0) {
2964                 return;
2965             }
2966             new TextLayout(new String(data, offset, length),
2967                            font, getFontRenderContext()).draw(this, x, y);
2968             return;
2969         }
2970 
2971         try {
2972             textpipe.drawChars(this, data, offset, length, x, y);
2973         } catch (InvalidPipeException e) {
2974             try {
2975                 revalidateAll();
2976                 textpipe.drawChars(this, data, offset, length, x, y);
2977             } catch (InvalidPipeException e2) {
2978                 // Still catching the exception; we are not yet ready to
2979                 // validate the surfaceData correctly.  Fail for now and
2980                 // try again next time around.
2981             }
2982         } finally {
2983             surfaceData.markDirty();
2984         }
2985     }
2986 
2987     public void drawBytes(byte data[], int offset, int length, int x, int y) {
2988         if (data == null) {
2989             throw new NullPointerException("byte data is null");
2990         }
2991         if (offset < 0 || length < 0 || offset + length > data.length) {
2992             throw new ArrayIndexOutOfBoundsException("bad offset/length");
2993         }
2994         /* Byte data is interpreted as 8-bit ASCII. Re-use drawChars loops */
2995         char chData[] = new char[length];
2996         for (int i = length; i-- > 0; ) {
2997             chData[i] = (char)(data[i+offset] & 0xff);
2998         }
2999         if (font.hasLayoutAttributes()) {
3000             if (data.length == 0) {
3001                 return;
3002             }
3003             new TextLayout(new String(chData),
3004                            font, getFontRenderContext()).draw(this, x, y);
3005             return;
3006         }
3007 
3008         try {
3009             textpipe.drawChars(this, chData, 0, length, x, y);
3010         } catch (InvalidPipeException e) {
3011             try {
3012                 revalidateAll();
3013                 textpipe.drawChars(this, chData, 0, length, x, y);
3014             } catch (InvalidPipeException e2) {
3015                 // Still catching the exception; we are not yet ready to
3016                 // validate the surfaceData correctly.  Fail for now and
3017                 // try again next time around.
3018             }
3019         } finally {
3020             surfaceData.markDirty();
3021         }
3022     }
3023 // end of text rendering methods
3024 
3025     private static boolean isHiDPIImage(final Image img) {
3026         return SurfaceManager.getImageScale(img) != 1;
3027     }
3028 
3029     private boolean drawHiDPIImage(Image img, int dx1, int dy1, int dx2,
3030                                    int dy2, int sx1, int sy1, int sx2, int sy2,
3031                                    Color bgcolor, ImageObserver observer) {
3032         final int scale = SurfaceManager.getImageScale(img);
3033         sx1 = Region.clipScale(sx1, scale);
3034         sx2 = Region.clipScale(sx2, scale);
3035         sy1 = Region.clipScale(sy1, scale);
3036         sy2 = Region.clipScale(sy2, scale);
3037         try {
3038             return imagepipe.scaleImage(this, img, dx1, dy1, dx2, dy2, sx1, sy1,
3039                                         sx2, sy2, bgcolor, observer);
3040         } catch (InvalidPipeException e) {
3041             try {
3042                 revalidateAll();
3043                 return imagepipe.scaleImage(this, img, dx1, dy1, dx2, dy2, sx1,
3044                                             sy1, sx2, sy2, bgcolor, observer);
3045             } catch (InvalidPipeException e2) {
3046                 // Still catching the exception; we are not yet ready to
3047                 // validate the surfaceData correctly.  Fail for now and
3048                 // try again next time around.
3049                 return false;
3050             }
3051         } finally {
3052             surfaceData.markDirty();
3053         }
3054     }
3055 
3056     /**
3057      * Draws an image scaled to x,y,w,h in nonblocking mode with a
3058      * callback object.
3059      */
3060     public boolean drawImage(Image img, int x, int y, int width, int height,
3061                              ImageObserver observer) {
3062         return drawImage(img, x, y, width, height, null, observer);
3063     }
3064 
3065     /**
3066      * Not part of the advertised API but a useful utility method
3067      * to call internally.  This is for the case where we are
3068      * drawing to/from given coordinates using a given width/height,
3069      * but we guarantee that the surfaceData's width/height of the src and dest
3070      * areas are equal (no scale needed). Note that this method intentionally
3071      * ignore scale factor of the source image, and copy it as is.
3072      */
3073     public boolean copyImage(Image img, int dx, int dy, int sx, int sy,
3074                              int width, int height, Color bgcolor,
3075                              ImageObserver observer) {
3076         try {
3077             return imagepipe.copyImage(this, img, dx, dy, sx, sy,
3078                                        width, height, bgcolor, observer);
3079         } catch (InvalidPipeException e) {
3080             try {
3081                 revalidateAll();
3082                 return imagepipe.copyImage(this, img, dx, dy, sx, sy,
3083                                            width, height, bgcolor, observer);
3084             } catch (InvalidPipeException e2) {
3085                 // Still catching the exception; we are not yet ready to
3086                 // validate the surfaceData correctly.  Fail for now and
3087                 // try again next time around.
3088                 return false;
3089             }
3090         } finally {
3091             surfaceData.markDirty();
3092         }
3093     }
3094 
3095     /**
3096      * Draws an image scaled to x,y,w,h in nonblocking mode with a
3097      * solid background color and a callback object.
3098      */
3099     public boolean drawImage(Image img, int x, int y, int width, int height,
3100                              Color bg, ImageObserver observer) {
3101 
3102         if (img == null) {
3103             return true;
3104         }
3105 
3106         if ((width == 0) || (height == 0)) {
3107             return true;
3108         }
3109 
3110         final int imgW = img.getWidth(null);
3111         final int imgH = img.getHeight(null);
3112         if (isHiDPIImage(img)) {
3113             return drawHiDPIImage(img, x, y, x + width, y + height, 0, 0, imgW,
3114                                   imgH, bg, observer);
3115         }
3116 
3117         if (width == imgW && height == imgH) {
3118             return copyImage(img, x, y, 0, 0, width, height, bg, observer);
3119         }
3120 
3121         try {
3122             return imagepipe.scaleImage(this, img, x, y, width, height,
3123                                         bg, observer);
3124         } catch (InvalidPipeException e) {
3125             try {
3126                 revalidateAll();
3127                 return imagepipe.scaleImage(this, img, x, y, width, height,
3128                                             bg, observer);
3129             } catch (InvalidPipeException e2) {
3130                 // Still catching the exception; we are not yet ready to
3131                 // validate the surfaceData correctly.  Fail for now and
3132                 // try again next time around.
3133                 return false;
3134             }
3135         } finally {
3136             surfaceData.markDirty();
3137         }
3138     }
3139 
3140     /**
3141      * Draws an image at x,y in nonblocking mode.
3142      */
3143     public boolean drawImage(Image img, int x, int y, ImageObserver observer) {
3144         return drawImage(img, x, y, null, observer);
3145     }
3146 
3147     /**
3148      * Draws an image at x,y in nonblocking mode with a solid background
3149      * color and a callback object.
3150      */
3151     public boolean drawImage(Image img, int x, int y, Color bg,
3152                              ImageObserver observer) {
3153 
3154         if (img == null) {
3155             return true;
3156         }
3157 
3158         if (isHiDPIImage(img)) {
3159             final int imgW = img.getWidth(null);
3160             final int imgH = img.getHeight(null);
3161             return drawHiDPIImage(img, x, y, x + imgW, y + imgH, 0, 0, imgW,
3162                                   imgH, bg, observer);
3163         }
3164 
3165         try {
3166             return imagepipe.copyImage(this, img, x, y, bg, observer);
3167         } catch (InvalidPipeException e) {
3168             try {
3169                 revalidateAll();
3170                 return imagepipe.copyImage(this, img, x, y, bg, observer);
3171             } catch (InvalidPipeException e2) {
3172                 // Still catching the exception; we are not yet ready to
3173                 // validate the surfaceData correctly.  Fail for now and
3174                 // try again next time around.
3175                 return false;
3176             }
3177         } finally {
3178             surfaceData.markDirty();
3179         }
3180     }
3181 
3182     /**
3183      * Draws a subrectangle of an image scaled to a destination rectangle
3184      * in nonblocking mode with a callback object.
3185      */
3186     public boolean drawImage(Image img,
3187                              int dx1, int dy1, int dx2, int dy2,
3188                              int sx1, int sy1, int sx2, int sy2,
3189                              ImageObserver observer) {
3190         return drawImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2, null,
3191                          observer);
3192     }
3193 
3194     /**
3195      * Draws a subrectangle of an image scaled to a destination rectangle in
3196      * nonblocking mode with a solid background color and a callback object.
3197      */
3198     public boolean drawImage(Image img,
3199                              int dx1, int dy1, int dx2, int dy2,
3200                              int sx1, int sy1, int sx2, int sy2,
3201                              Color bgcolor, ImageObserver observer) {
3202 
3203         if (img == null) {
3204             return true;
3205         }
3206 
3207         if (dx1 == dx2 || dy1 == dy2 ||
3208             sx1 == sx2 || sy1 == sy2)
3209         {
3210             return true;
3211         }
3212 
3213         if (isHiDPIImage(img)) {
3214             return drawHiDPIImage(img, dx1, dy1, dx2, dy2, sx1, sy1, sx2, sy2,
3215                                   bgcolor, observer);
3216         }
3217 
3218         if (((sx2 - sx1) == (dx2 - dx1)) &&
3219             ((sy2 - sy1) == (dy2 - dy1)))
3220         {
3221             // Not a scale - forward it to a copy routine
3222             int srcX, srcY, dstX, dstY, width, height;
3223             if (sx2 > sx1) {
3224                 width = sx2 - sx1;
3225                 srcX = sx1;
3226                 dstX = dx1;
3227             } else {
3228                 width = sx1 - sx2;
3229                 srcX = sx2;
3230                 dstX = dx2;
3231             }
3232             if (sy2 > sy1) {
3233                 height = sy2-sy1;
3234                 srcY = sy1;
3235                 dstY = dy1;
3236             } else {
3237                 height = sy1-sy2;
3238                 srcY = sy2;
3239                 dstY = dy2;
3240             }
3241             return copyImage(img, dstX, dstY, srcX, srcY,
3242                              width, height, bgcolor, observer);
3243         }
3244 
3245         try {
3246             return imagepipe.scaleImage(this, img, dx1, dy1, dx2, dy2,
3247                                           sx1, sy1, sx2, sy2, bgcolor,
3248                                           observer);
3249         } catch (InvalidPipeException e) {
3250             try {
3251                 revalidateAll();
3252                 return imagepipe.scaleImage(this, img, dx1, dy1, dx2, dy2,
3253                                               sx1, sy1, sx2, sy2, bgcolor,
3254                                               observer);
3255             } catch (InvalidPipeException e2) {
3256                 // Still catching the exception; we are not yet ready to
3257                 // validate the surfaceData correctly.  Fail for now and
3258                 // try again next time around.
3259                 return false;
3260             }
3261         } finally {
3262             surfaceData.markDirty();
3263         }
3264     }
3265 
3266     /**
3267      * Draw an image, applying a transform from image space into user space
3268      * before drawing.
3269      * The transformation from user space into device space is done with
3270      * the current transform in the Graphics2D.
3271      * The given transformation is applied to the image before the
3272      * transform attribute in the Graphics2D state is applied.
3273      * The rendering attributes applied include the clip, transform,
3274      * paint or color and composite attributes. Note that the result is
3275      * undefined, if the given transform is non-invertible.
3276      * @param img The image to be drawn.
3277      * @param xform The transformation from image space into user space.
3278      * @param observer The image observer to be notified on the image producing
3279      * progress.
3280      * @see #transform
3281      * @see #setComposite
3282      * @see #setClip
3283      */
3284     public boolean drawImage(Image img,
3285                              AffineTransform xform,
3286                              ImageObserver observer) {
3287 
3288         if (img == null) {
3289             return true;
3290         }
3291 
3292         if (xform == null || xform.isIdentity()) {
3293             return drawImage(img, 0, 0, null, observer);
3294         }
3295 
3296         if (isHiDPIImage(img)) {
3297             final int w = img.getWidth(null);
3298             final int h = img.getHeight(null);
3299             final AffineTransform tx = new AffineTransform(transform);
3300             transform(xform);
3301             boolean result = drawHiDPIImage(img, 0, 0, w, h, 0, 0, w, h, null,
3302                                             observer);
3303             transform.setTransform(tx);
3304             invalidateTransform();
3305             return result;
3306         }
3307 
3308         try {
3309             return imagepipe.transformImage(this, img, xform, observer);
3310         } catch (InvalidPipeException e) {
3311             try {
3312                 revalidateAll();
3313                 return imagepipe.transformImage(this, img, xform, observer);
3314             } catch (InvalidPipeException e2) {
3315                 // Still catching the exception; we are not yet ready to
3316                 // validate the surfaceData correctly.  Fail for now and
3317                 // try again next time around.
3318                 return false;
3319             }
3320         } finally {
3321             surfaceData.markDirty();
3322         }
3323     }
3324 
3325     public void drawImage(BufferedImage bImg,
3326                           BufferedImageOp op,
3327                           int x,
3328                           int y)  {
3329 
3330         if (bImg == null) {
3331             return;
3332         }
3333 
3334         try {
3335             imagepipe.transformImage(this, bImg, op, x, y);
3336         } catch (InvalidPipeException e) {
3337             try {
3338                 revalidateAll();
3339                 imagepipe.transformImage(this, bImg, op, x, y);
3340             } catch (InvalidPipeException e2) {
3341                 // Still catching the exception; we are not yet ready to
3342                 // validate the surfaceData correctly.  Fail for now and
3343                 // try again next time around.
3344             }
3345         } finally {
3346             surfaceData.markDirty();
3347         }
3348     }
3349 
3350     /**
3351     * Get the rendering context of the font
3352     * within this Graphics2D context.
3353     */
3354     public FontRenderContext getFontRenderContext() {
3355         if (cachedFRC == null) {
3356             int aahint = textAntialiasHint;
3357             if (aahint == SunHints.INTVAL_TEXT_ANTIALIAS_DEFAULT &&
3358                 antialiasHint == SunHints.INTVAL_ANTIALIAS_ON) {
3359                 aahint = SunHints.INTVAL_TEXT_ANTIALIAS_ON;
3360             }
3361             // Translation components should be excluded from the FRC transform
3362             AffineTransform tx = null;
3363             if (transformState >= TRANSFORM_TRANSLATESCALE) {
3364                 if (transform.getTranslateX() == 0 &&
3365                     transform.getTranslateY() == 0) {
3366                     tx = transform;
3367                 } else {
3368                     tx = new AffineTransform(transform.getScaleX(),
3369                                              transform.getShearY(),
3370                                              transform.getShearX(),
3371                                              transform.getScaleY(),
3372                                              0, 0);
3373                 }
3374             }
3375             cachedFRC = new FontRenderContext(tx,
3376              SunHints.Value.get(SunHints.INTKEY_TEXT_ANTIALIASING, aahint),
3377              SunHints.Value.get(SunHints.INTKEY_FRACTIONALMETRICS,
3378                                 fractionalMetricsHint));
3379         }
3380         return cachedFRC;
3381     }
3382     private FontRenderContext cachedFRC;
3383 
3384     /**
3385      * This object has no resources to dispose of per se, but the
3386      * doc comments for the base method in java.awt.Graphics imply
3387      * that this object will not be useable after it is disposed.
3388      * So, we sabotage the object to prevent further use to prevent
3389      * developers from relying on behavior that may not work on
3390      * other, less forgiving, VMs that really need to dispose of
3391      * resources.
3392      */
3393     public void dispose() {
3394         surfaceData = NullSurfaceData.theInstance;
3395         invalidatePipe();
3396     }
3397 
3398     /**
3399      * Graphics has a finalize method that automatically calls dispose()
3400      * for subclasses.  For SunGraphics2D we do not need to be finalized
3401      * so that method simply causes us to be enqueued on the Finalizer
3402      * queues for no good reason.  Unfortunately, that method and
3403      * implementation are now considered part of the public contract
3404      * of that base class so we can not remove or gut the method.
3405      * We override it here with an empty method and the VM is smart
3406      * enough to know that if our override is empty then it should not
3407      * mark us as finalizeable.
3408      */
3409     public void finalize() {
3410         // DO NOT REMOVE THIS METHOD
3411     }
3412 
3413     /**
3414      * Returns destination that this Graphics renders to.  This could be
3415      * either an Image or a Component; subclasses of SurfaceData are
3416      * responsible for returning the appropriate object.
3417      */
3418     public Object getDestination() {
3419         return surfaceData.getDestination();
3420     }
3421 
3422     /**
3423      * {@inheritDoc}
3424      *
3425      * @see sun.java2d.DestSurfaceProvider#getDestSurface
3426      */
3427     @Override
3428     public Surface getDestSurface() {
3429         return surfaceData;
3430     }
3431 }