1 /*
2  * Copyright (c) 1998, 2018, 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.pipe;
27 
28 import java.awt.geom.PathIterator;
29 import java.awt.Rectangle;
30 import sun.awt.geom.PathConsumer2D;
31 
32 /**
33  * This class can iterate individual span elements generated by scan
34  * converting a Shape.
35  * This particular implementation flattens the incoming path and then
36  * performs simple polygon tracing to calculate the spans.
37  *
38  * Note that this class holds pointers to native data which must be
39  * disposed.  It is not marked as finalizable since it is intended
40  * to be very lightweight and finalization is a comparitively expensive
41  * procedure.  The caller must specifically use try{} finally{} to
42  * manually ensure that the object is disposed after use, otherwise
43  * native data structures might be leaked.
44  *
45  * Here is a code sample for using this class:
46  *
47  * public void fillShape(Shape s, Rectangle clipRect) {
48  *     ShapeSpanIterator ssi = new ShapeSpanIterator(false);
49  *     try {
50  *         ssi.setOutputArea(clipRect);
51  *         ssi.appendPath(s.getPathIterator(null));
52  *         int spanbox[] = new int[4];
53  *         while (ssi.nextSpan(spanbox)) {
54  *             int x = spanbox[0];
55  *             int y = spanbox[1];
56  *             int w = spanbox[2] - x;
57  *             int h = spanbox[3] - y;
58  *             fillRect(x, y, w, h);
59  *         }
60  *     } finally {
61  *         ssi.dispose();
62  *     }
63  * }
64  */
65 public final class ShapeSpanIterator
66     implements SpanIterator, PathConsumer2D
67 {
68     long pData;
69 
70     static {
initIDs()71         initIDs();
72     }
73 
initIDs()74     public static native void initIDs();
75 
ShapeSpanIterator(boolean adjust)76     public ShapeSpanIterator(boolean adjust) {
77         setNormalize(adjust);
78     }
79 
80     /*
81      * Appends the geometry and winding rule from the indicated
82      * path iterator.
83      */
appendPath(PathIterator pi)84     public void appendPath(PathIterator pi) {
85         float[] coords = new float[6];
86 
87         setRule(pi.getWindingRule());
88         while (!pi.isDone()) {
89             addSegment(pi.currentSegment(coords), coords);
90             pi.next();
91         }
92         pathDone();
93     }
94 
95     /*
96      * Appends the geometry from the indicated set of polygon points.
97      */
appendPoly(int[] xPoints, int[] yPoints, int nPoints, int xoff, int yoff)98     public native void appendPoly(int[] xPoints, int[] yPoints, int nPoints,
99                                   int xoff, int yoff);
100 
101     /*
102      * Sets the normalization flag so that incoming data is
103      * adjusted to nearest (0.25, 0.25) subpixel position.
104      */
setNormalize(boolean adjust)105     private native void setNormalize(boolean adjust);
106 
107     /*
108      * Sets the rectangle of interest for storing and returning
109      * span segments.
110      */
setOutputAreaXYWH(int x, int y, int w, int h)111     public void setOutputAreaXYWH(int x, int y, int w, int h) {
112         setOutputAreaXYXY(x, y, Region.dimAdd(x, w), Region.dimAdd(y, h));
113     }
114 
115     /*
116      * Sets the rectangle of interest for storing and returning
117      * span segments.
118      */
setOutputAreaXYXY(int lox, int loy, int hix, int hiy)119     public native void setOutputAreaXYXY(int lox, int loy, int hix, int hiy);
120 
121     /*
122      * Sets the rectangle of interest for storing and returning
123      * span segments to the specified Rectangle.
124      */
setOutputArea(Rectangle r)125     public void setOutputArea(Rectangle r) {
126         setOutputAreaXYWH(r.x, r.y, r.width, r.height);
127     }
128 
129     /*
130      * Sets the rectangle of interest for storing and returning
131      * span segments to the bounds of the specified Region.
132      */
setOutputArea(Region r)133     public void setOutputArea(Region r) {
134         setOutputAreaXYXY(r.getLoX(), r.getLoY(), r.getHiX(), r.getHiY());
135     }
136 
137     /*
138      * Sets the winding rule in the native data structures.
139      */
setRule(int rule)140     public native void setRule(int rule);
141 
142     /*
143      * Adds a single PathIterator segment to the internal list of
144      * path element structures.
145      */
addSegment(int type, float[] coords)146     public native void addSegment(int type, float[] coords);
147 
148     /*
149      * Gets the bbox of the available path segments, clipped to the
150      * OutputArea.
151      */
getPathBox(int[] pathbox)152     public native void getPathBox(int[] pathbox);
153 
154     /*
155      * Intersects the path box with the given bbox.
156      * Returned spans are clipped to this region, or discarded
157      * altogether if they lie outside it.
158      */
intersectClipBox(int lox, int loy, int hix, int hiy)159     public native void intersectClipBox(int lox, int loy, int hix, int hiy);
160 
161     /*
162      * Fetches the next span that needs to be operated on.
163      * If the return value is false then there are no more spans.
164      */
nextSpan(int[] spanbox)165     public native boolean nextSpan(int[] spanbox);
166 
167     /**
168      * This method tells the iterator that it may skip all spans
169      * whose Y range is completely above the indicated Y coordinate.
170      */
skipDownTo(int y)171     public native void skipDownTo(int y);
172 
173     /**
174      * This method returns a native pointer to a function block that
175      * can be used by a native method to perform the same iteration
176      * cycle that the above methods provide while avoiding upcalls to
177      * the Java object.
178      * The definition of the structure whose pointer is returned by
179      * this method is defined in:
180      * <pre>
181      *     src/share/native/sun/java2d/pipe/SpanIterator.h
182      * </pre>
183      */
getNativeIterator()184     public native long getNativeIterator();
185 
186     /*
187      * Cleans out all internal data structures.
188      */
dispose()189     public native void dispose();
190 
moveTo(float x, float y)191     public native void moveTo(float x, float y);
lineTo(float x, float y)192     public native void lineTo(float x, float y);
quadTo(float x1, float y1, float x2, float y2)193     public native void quadTo(float x1, float y1,
194                               float x2, float y2);
curveTo(float x1, float y1, float x2, float y2, float x3, float y3)195     public native void curveTo(float x1, float y1,
196                                float x2, float y2,
197                                float x3, float y3);
closePath()198     public native void closePath();
pathDone()199     public native void pathDone();
getNativeConsumer()200     public native long getNativeConsumer();
201 }
202