Path: blob/master/src/java.desktop/share/classes/sun/java2d/pipe/ShapeSpanIterator.java
41159 views
/*1* Copyright (c) 1998, 2018, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package sun.java2d.pipe;2627import java.awt.geom.PathIterator;28import java.awt.Rectangle;29import sun.awt.geom.PathConsumer2D;3031/**32* This class can iterate individual span elements generated by scan33* converting a Shape.34* This particular implementation flattens the incoming path and then35* performs simple polygon tracing to calculate the spans.36*37* Note that this class holds pointers to native data which must be38* disposed. It is not marked as finalizable since it is intended39* to be very lightweight and finalization is a comparitively expensive40* procedure. The caller must specifically use try{} finally{} to41* manually ensure that the object is disposed after use, otherwise42* native data structures might be leaked.43*44* Here is a code sample for using this class:45*46* public void fillShape(Shape s, Rectangle clipRect) {47* ShapeSpanIterator ssi = new ShapeSpanIterator(false);48* try {49* ssi.setOutputArea(clipRect);50* ssi.appendPath(s.getPathIterator(null));51* int spanbox[] = new int[4];52* while (ssi.nextSpan(spanbox)) {53* int x = spanbox[0];54* int y = spanbox[1];55* int w = spanbox[2] - x;56* int h = spanbox[3] - y;57* fillRect(x, y, w, h);58* }59* } finally {60* ssi.dispose();61* }62* }63*/64public final class ShapeSpanIterator65implements SpanIterator, PathConsumer2D66{67long pData;6869static {70initIDs();71}7273public static native void initIDs();7475public ShapeSpanIterator(boolean adjust) {76setNormalize(adjust);77}7879/*80* Appends the geometry and winding rule from the indicated81* path iterator.82*/83public void appendPath(PathIterator pi) {84float[] coords = new float[6];8586setRule(pi.getWindingRule());87while (!pi.isDone()) {88addSegment(pi.currentSegment(coords), coords);89pi.next();90}91pathDone();92}9394/*95* Appends the geometry from the indicated set of polygon points.96*/97public native void appendPoly(int[] xPoints, int[] yPoints, int nPoints,98int xoff, int yoff);99100/*101* Sets the normalization flag so that incoming data is102* adjusted to nearest (0.25, 0.25) subpixel position.103*/104private native void setNormalize(boolean adjust);105106/*107* Sets the rectangle of interest for storing and returning108* span segments.109*/110public void setOutputAreaXYWH(int x, int y, int w, int h) {111setOutputAreaXYXY(x, y, Region.dimAdd(x, w), Region.dimAdd(y, h));112}113114/*115* Sets the rectangle of interest for storing and returning116* span segments.117*/118public native void setOutputAreaXYXY(int lox, int loy, int hix, int hiy);119120/*121* Sets the rectangle of interest for storing and returning122* span segments to the specified Rectangle.123*/124public void setOutputArea(Rectangle r) {125setOutputAreaXYWH(r.x, r.y, r.width, r.height);126}127128/*129* Sets the rectangle of interest for storing and returning130* span segments to the bounds of the specified Region.131*/132public void setOutputArea(Region r) {133setOutputAreaXYXY(r.getLoX(), r.getLoY(), r.getHiX(), r.getHiY());134}135136/*137* Sets the winding rule in the native data structures.138*/139public native void setRule(int rule);140141/*142* Adds a single PathIterator segment to the internal list of143* path element structures.144*/145public native void addSegment(int type, float[] coords);146147/*148* Gets the bbox of the available path segments, clipped to the149* OutputArea.150*/151public native void getPathBox(int[] pathbox);152153/*154* Intersects the path box with the given bbox.155* Returned spans are clipped to this region, or discarded156* altogether if they lie outside it.157*/158public native void intersectClipBox(int lox, int loy, int hix, int hiy);159160/*161* Fetches the next span that needs to be operated on.162* If the return value is false then there are no more spans.163*/164public native boolean nextSpan(int[] spanbox);165166/**167* This method tells the iterator that it may skip all spans168* whose Y range is completely above the indicated Y coordinate.169*/170public native void skipDownTo(int y);171172/**173* This method returns a native pointer to a function block that174* can be used by a native method to perform the same iteration175* cycle that the above methods provide while avoiding upcalls to176* the Java object.177* The definition of the structure whose pointer is returned by178* this method is defined in:179* <pre>180* src/share/native/sun/java2d/pipe/SpanIterator.h181* </pre>182*/183public native long getNativeIterator();184185/*186* Cleans out all internal data structures.187*/188public native void dispose();189190public native void moveTo(float x, float y);191public native void lineTo(float x, float y);192public native void quadTo(float x1, float y1,193float x2, float y2);194public native void curveTo(float x1, float y1,195float x2, float y2,196float x3, float y3);197public native void closePath();198public native void pathDone();199public native long getNativeConsumer();200}201202203