GraphicsPathIterator.jvm.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. //
  2. // System.Drawing.Drawing2D.GraphicsPathIterator.cs
  3. //
  4. // Author:
  5. // Bors Kirzner <[email protected]>
  6. //
  7. // Copyright (C) 2005 Mainsoft Corporation, (http://www.mainsoft.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. using System;
  29. using System.Drawing;
  30. namespace System.Drawing.Drawing2D
  31. {
  32. public sealed class GraphicsPathIterator : MarshalByRefObject, IDisposable
  33. {
  34. #region Fields
  35. private readonly GraphicsPath _path;
  36. private int _marker = -1;
  37. private int _subpath = -1;
  38. #endregion // Fields
  39. #region Constructors
  40. public GraphicsPathIterator (GraphicsPath path)
  41. {
  42. _path = path;
  43. }
  44. #endregion // Constructors
  45. #region Properites
  46. public int Count
  47. {
  48. get { return _path.NativeObject.PointCount; }
  49. }
  50. public int SubpathCount {
  51. get {
  52. int count = 0;
  53. int start, end;
  54. bool isClosed;
  55. while (NextSubpath (out start, out end, out isClosed) != 0)
  56. count++;
  57. return count;
  58. }
  59. }
  60. #endregion // Properties
  61. #region Methods
  62. public int CopyData (ref PointF [] points, ref byte [] types, int startIndex, int endIndex)
  63. {
  64. int j = 0;
  65. for (int i = startIndex; i <= endIndex && i < _path.PointCount; i++) {
  66. points [j] = _path.PathPoints [i];
  67. types [j++] = _path.PathTypes [i];
  68. }
  69. return j;
  70. }
  71. public void Dispose ()
  72. {
  73. }
  74. public int Enumerate (ref PointF [] points, ref byte [] types)
  75. {
  76. return CopyData (ref points, ref types, 0, _path.PointCount);
  77. }
  78. public bool HasCurve ()
  79. {
  80. byte [] types = _path.PathTypes;
  81. for (int i=0; i < types.Length; i++)
  82. if ((types [i] & (byte)PathPointType.PathTypeMask) == (byte)PathPointType.Bezier3)
  83. return true;
  84. return false;
  85. }
  86. public int NextMarker (GraphicsPath path)
  87. {
  88. if (path == null)
  89. return 0;
  90. int startIndex;
  91. int endIndex;
  92. int count = NextMarker (out startIndex, out endIndex);
  93. if (count != 0)
  94. SetPath (_path, startIndex, count, path);
  95. return count;
  96. }
  97. public int NextMarker (out int startIndex, out int endIndex)
  98. {
  99. if (_marker >= _path.PointCount) {
  100. startIndex = 0;
  101. endIndex = 0;
  102. return 0;
  103. }
  104. startIndex = ++_marker;
  105. while ((_marker < _path.PointCount) && ((_path.PathTypes [_marker] & (byte)PathPointType.PathMarker) == 0))
  106. _marker++;
  107. endIndex = (_marker < _path.PointCount) ? _marker : _path.PointCount - 1;
  108. return endIndex - startIndex + 1;
  109. }
  110. public int NextPathType (out byte pathType, out int startIndex, out int endIndex)
  111. {
  112. if ((_subpath >= _path.PointCount - 1) | (_subpath < 0)) {
  113. startIndex = 0;
  114. endIndex = 0;
  115. pathType = (_subpath < 0) ? (byte)PathPointType.Start : _path.PathTypes [_path.PointCount - 1];
  116. return 0;
  117. }
  118. // .net acts different, but it seems to be a bug
  119. if ((_path.PathTypes [_subpath + 1] & (byte)PathPointType.PathMarker) != 0) {
  120. startIndex = 0;
  121. endIndex = 0;
  122. pathType = _path.PathTypes [_subpath];
  123. return 0;
  124. }
  125. startIndex = _subpath++;
  126. endIndex = startIndex;
  127. pathType = (byte)(_path.PathTypes [startIndex + 1] & (byte)PathPointType.PathTypeMask);
  128. while (((_subpath) < _path.PointCount) && ((_path.PathTypes [_subpath] & (byte)PathPointType.PathTypeMask) == pathType))
  129. _subpath++;
  130. endIndex = (_subpath < _path.PointCount) ? --_subpath : _path.PointCount - 1;
  131. return endIndex - startIndex + 1;
  132. }
  133. public int NextSubpath (GraphicsPath path, out bool isClosed)
  134. {
  135. int startIndex;
  136. int endIndex;
  137. int count = NextSubpath (out startIndex, out endIndex, out isClosed);
  138. if ((count != 0) && (path != null))
  139. SetPath (_path, startIndex, count, path);
  140. return count;
  141. }
  142. private void SetPath (GraphicsPath source, int start, int count, GraphicsPath target)
  143. {
  144. PointF [] points = new PointF [count];
  145. byte [] types = new byte [count];
  146. PointF [] pathPoints = _path.PathPoints;
  147. byte [] pathTypes = _path.PathTypes;
  148. for (int i = 0; i < count; i++) {
  149. points [i] = pathPoints [start + i];
  150. types [i] = pathTypes [start + i];
  151. }
  152. target.SetPath (points, types);
  153. }
  154. public int NextSubpath (out int startIndex, out int endIndex, out bool isClosed)
  155. {
  156. _subpath++;
  157. while (((_subpath) < _path.PointCount) && (_path.PathTypes [_subpath] != (byte)PathPointType.Start))
  158. _subpath++;
  159. if (_subpath >= _path.PointCount - 1) {
  160. startIndex = 0;
  161. endIndex = 0;
  162. isClosed = true;
  163. return 0;
  164. }
  165. startIndex = _subpath;
  166. int offset = 1;
  167. while (((_subpath + offset) < _path.PointCount) && (_path.PathTypes [_subpath + offset] != (byte)PathPointType.Start))
  168. offset++;
  169. endIndex = ((_subpath + offset) < _path.PointCount) ? _subpath + offset - 1 : _path.PointCount - 1;
  170. isClosed = (_path.PathTypes [endIndex] & (byte)PathPointType.CloseSubpath) != 0;
  171. return endIndex - startIndex + 1;
  172. }
  173. public void Rewind ()
  174. {
  175. _marker = -1;
  176. _subpath = -1;
  177. }
  178. #endregion // Methods
  179. }
  180. }