1 /*
   2  * Copyright 1998-2007 Sun Microsystems, Inc.  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.  Sun designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
  22  * CA 95054 USA or visit www.sun.com if you need additional information or
  23  * have any questions.
  24  */
  25 
  26 package sun.java2d.pipe;
  27 
  28 import sun.java2d.SunGraphics2D;
  29 import sun.java2d.SurfaceData;
  30 import java.awt.Rectangle;
  31 import java.awt.Shape;
  32 import java.awt.BasicStroke;
  33 import java.awt.geom.PathIterator;
  34 import java.awt.geom.AffineTransform;
  35 import java.awt.geom.Rectangle2D;
  36 import sun.awt.SunHints;
  37 
  38 /**
  39  * This class is used to convert raw geometry into a span iterator
  40  * object using a simple flattening polygon scan converter.
  41  * The iterator can be passed on to special SpanFiller loops to
  42  * perform the actual rendering.
  43  */
  44 public abstract class SpanShapeRenderer implements ShapeDrawPipe {
  45     final static RenderingEngine RenderEngine = RenderingEngine.getInstance();
  46 
  47     public static class Composite extends SpanShapeRenderer {
  48         CompositePipe comppipe;
  49 
  50         public Composite(CompositePipe pipe) {
  51             comppipe = pipe;
  52         }
  53 
  54         public Object startSequence(SunGraphics2D sg, Shape s,
  55                                     Rectangle devR, int[] bbox) {
  56             return comppipe.startSequence(sg, s, devR, bbox);
  57         }
  58 
  59         public void renderBox(Object ctx, int x, int y, int w, int h) {
  60             comppipe.renderPathTile(ctx, null, 0, w, x, y, w, h);
  61         }
  62 
  63         public void endSequence(Object ctx) {
  64             comppipe.endSequence(ctx);
  65         }
  66     }
  67 
  68     public static class Simple extends SpanShapeRenderer {
  69         public Object startSequence(SunGraphics2D sg, Shape s,
  70                                     Rectangle devR, int[] bbox) {
  71             return sg;
  72         }
  73 
  74         public void renderBox(Object ctx, int x, int y, int w, int h) {
  75             SunGraphics2D sg2d = (SunGraphics2D) ctx;
  76             SurfaceData sd = sg2d.getSurfaceData();
  77             sg2d.getLoops().fillRectLoop.FillRect(sg2d, sd, x, y, w, h);
  78         }
  79 
  80         public void endSequence(Object ctx) {
  81         }
  82     }
  83 
  84     public void draw(SunGraphics2D sg, Shape s) {
  85         if (sg.stroke instanceof BasicStroke) {
  86             ShapeSpanIterator sr = LoopPipe.getStrokeSpans(sg, s);
  87             try {
  88                 renderSpans(sg, sg.getCompClip(), s, sr);
  89             } finally {
  90                 sr.dispose();
  91             }
  92         } else {
  93             fill(sg, sg.stroke.createStrokedShape(s));
  94         }
  95     }
  96 
  97     public static final int NON_RECTILINEAR_TRANSFORM_MASK =
  98         (AffineTransform.TYPE_GENERAL_TRANSFORM |
  99          AffineTransform.TYPE_GENERAL_ROTATION);
 100 
 101     public void fill(SunGraphics2D sg, Shape s) {
 102         if (s instanceof Rectangle2D &&
 103             (sg.transform.getType() & NON_RECTILINEAR_TRANSFORM_MASK) == 0)
 104         {
 105             renderRect(sg, (Rectangle2D) s);
 106             return;
 107         }
 108 
 109         Region clipRegion = sg.getCompClip();
 110         ShapeSpanIterator sr = LoopPipe.getFillSSI(sg);
 111         try {
 112             sr.setOutputArea(clipRegion);
 113             sr.appendPath(s.getPathIterator(sg.transform));
 114             renderSpans(sg, clipRegion, s, sr);
 115         } finally {
 116             sr.dispose();
 117         }
 118     }
 119 
 120     public abstract Object startSequence(SunGraphics2D sg, Shape s,
 121                                          Rectangle devR, int[] bbox);
 122 
 123     public abstract void renderBox(Object ctx, int x, int y, int w, int h);
 124 
 125     public abstract void endSequence(Object ctx);
 126 
 127     public void renderRect(SunGraphics2D sg, Rectangle2D r) {
 128         double corners[] = {
 129             r.getX(), r.getY(), r.getWidth(), r.getHeight(),
 130         };
 131         corners[2] += corners[0];
 132         corners[3] += corners[1];
 133         if (corners[2] <= corners[0] || corners[3] <= corners[1]) {
 134             return;
 135         }
 136         sg.transform.transform(corners, 0, corners, 0, 2);
 137         if (corners[2] < corners[0]) {
 138             double t = corners[2];
 139             corners[2] = corners[0];
 140             corners[0] = t;
 141         }
 142         if (corners[3] < corners[1]) {
 143             double t = corners[3];
 144             corners[3] = corners[1];
 145             corners[1] = t;
 146         }
 147         int abox[] = {
 148             (int) corners[0],
 149             (int) corners[1],
 150             (int) corners[2],
 151             (int) corners[3],
 152         };
 153         Rectangle devR = new Rectangle(abox[0], abox[1],
 154                                        abox[2] - abox[0],
 155                                        abox[3] - abox[1]);
 156         Region clipRegion = sg.getCompClip();
 157         clipRegion.clipBoxToBounds(abox);
 158         if (abox[0] >= abox[2] || abox[1] >= abox[3]) {
 159             return;
 160         }
 161         Object context = startSequence(sg, r, devR, abox);
 162         if (clipRegion.isRectangular()) {
 163             renderBox(context, abox[0], abox[1],
 164                       abox[2] - abox[0],
 165                       abox[3] - abox[1]);
 166         } else {
 167             SpanIterator sr = clipRegion.getSpanIterator(abox);
 168             while (sr.nextSpan(abox)) {
 169                 renderBox(context, abox[0], abox[1],
 170                               abox[2] - abox[0],
 171                               abox[3] - abox[1]);
 172             }
 173         }
 174         endSequence(context);
 175     }
 176 
 177     public void renderSpans(SunGraphics2D sg, Region clipRegion, Shape s,
 178                             ShapeSpanIterator sr)
 179     {
 180         Object context = null;
 181         int abox[] = new int[4];
 182         try {
 183             sr.getPathBox(abox);
 184             Rectangle devR = new Rectangle(abox[0], abox[1],
 185                                            abox[2] - abox[0],
 186                                            abox[3] - abox[1]);
 187             clipRegion.clipBoxToBounds(abox);
 188             if (abox[0] >= abox[2] || abox[1] >= abox[3]) {
 189                 return;
 190             }
 191             sr.intersectClipBox(abox[0], abox[1], abox[2], abox[3]);
 192             context = startSequence(sg, s, devR, abox);
 193 
 194             spanClipLoop(context, sr, clipRegion, abox);
 195 
 196         } finally {
 197             if (context != null) {
 198                 endSequence(context);
 199             }
 200         }
 201     }
 202 
 203     public void spanClipLoop(Object ctx, SpanIterator sr,
 204                              Region r, int[] abox) {
 205         if (!r.isRectangular()) {
 206             sr = r.filter(sr);
 207         }
 208         while (sr.nextSpan(abox)) {
 209             int x = abox[0];
 210             int y = abox[1];
 211             renderBox(ctx, x, y, abox[2] - x, abox[3] - y);
 212         }
 213     }
 214 }