2 // System.Drawing.Drawing2D.GraphicsPathIterator.cs
5 // Bors Kirzner <boris@mainsoft.com>
7 // Copyright (C) 2005 Mainsoft Corporation, (http://www.mainsoft.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31 namespace System.Drawing.Drawing2D
33 public sealed class GraphicsPathIterator : MarshalByRefObject, IDisposable
37 private readonly GraphicsPath _path;
38 private int _marker = -1;
39 private int _subpath = -1;
45 public GraphicsPathIterator (GraphicsPath path)
50 #endregion // Constructors
56 get { return _path.NativeObject.PointCount; }
59 public int SubpathCount {
64 while (NextSubpath (out start, out end, out isClosed) != 0)
70 #endregion // Properties
74 public int CopyData (ref PointF [] points, ref byte [] types, int startIndex, int endIndex)
77 for (int i = startIndex; i <= endIndex && i < _path.PointCount; i++) {
78 points [j] = _path.PathPoints [i];
79 types [j++] = _path.PathTypes [i];
84 public void Dispose ()
88 public int Enumerate (ref PointF [] points, ref byte [] types)
90 return CopyData (ref points, ref types, 0, _path.PointCount);
93 public bool HasCurve ()
95 byte [] types = _path.PathTypes;
96 for (int i=0; i < types.Length; i++)
97 if ((types [i] & (byte)PathPointType.PathTypeMask) == (byte)PathPointType.Bezier3)
102 public int NextMarker (GraphicsPath path)
109 int count = NextMarker (out startIndex, out endIndex);
112 SetPath (_path, startIndex, count, path);
117 public int NextMarker (out int startIndex, out int endIndex)
119 if (_marker >= _path.PointCount) {
125 startIndex = ++_marker;
126 while ((_marker < _path.PointCount) && ((_path.PathTypes [_marker] & (byte)PathPointType.PathMarker) == 0))
129 endIndex = (_marker < _path.PointCount) ? _marker : _path.PointCount - 1;
130 return endIndex - startIndex + 1;
133 public int NextPathType (out byte pathType, out int startIndex, out int endIndex)
135 if ((_subpath >= _path.PointCount - 1) | (_subpath < 0)) {
138 pathType = (_subpath < 0) ? (byte)PathPointType.Start : _path.PathTypes [_path.PointCount - 1];
142 // .net acts different, but it seems to be a bug
143 if ((_path.PathTypes [_subpath + 1] & (byte)PathPointType.PathMarker) != 0) {
146 pathType = _path.PathTypes [_subpath];
150 startIndex = _subpath++;
151 endIndex = startIndex;
152 pathType = (byte)(_path.PathTypes [startIndex + 1] & (byte)PathPointType.PathTypeMask);
154 while (((_subpath) < _path.PointCount) && ((_path.PathTypes [_subpath] & (byte)PathPointType.PathTypeMask) == pathType))
157 endIndex = (_subpath < _path.PointCount) ? --_subpath : _path.PointCount - 1;
158 return endIndex - startIndex + 1;
161 public int NextSubpath (GraphicsPath path, out bool isClosed)
165 int count = NextSubpath (out startIndex, out endIndex, out isClosed);
167 if ((count != 0) && (path != null))
168 SetPath (_path, startIndex, count, path);
173 private void SetPath (GraphicsPath source, int start, int count, GraphicsPath target)
175 PointF [] points = new PointF [count];
176 byte [] types = new byte [count];
177 PointF [] pathPoints = _path.PathPoints;
178 byte [] pathTypes = _path.PathTypes;
180 for (int i = 0; i < count; i++) {
181 points [i] = pathPoints [start + i];
182 types [i] = pathTypes [start + i];
185 target.SetPath (points, types);
188 public int NextSubpath (out int startIndex, out int endIndex, out bool isClosed)
191 while (((_subpath) < _path.PointCount) && (_path.PathTypes [_subpath] != (byte)PathPointType.Start))
195 if (_subpath >= _path.PointCount - 1) {
202 startIndex = _subpath;
204 while (((_subpath + offset) < _path.PointCount) && (_path.PathTypes [_subpath + offset] != (byte)PathPointType.Start))
207 endIndex = ((_subpath + offset) < _path.PointCount) ? _subpath + offset - 1 : _path.PointCount - 1;
208 isClosed = (_path.PathTypes [endIndex] & (byte)PathPointType.CloseSubpath) != 0;
209 return endIndex - startIndex + 1;
212 public void Rewind ()
218 #endregion // Methods