ExtendedGeneralPath.jvm.cs 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402
  1. using System;
  2. using java.awt;
  3. using java.awt.geom;
  4. using java.lang;
  5. namespace System.Drawing.Drawing2D
  6. {
  7. internal class ExtendedGeneralPath : Shape, ICloneable
  8. {
  9. #region Fields
  10. public const int WIND_EVEN_ODD = 0; //PathIterator__Finals.WIND_EVEN_ODD;
  11. public const int WIND_NON_ZERO = 1; //PathIterator__Finals.WIND_NON_ZERO;
  12. public const sbyte SEG_MOVETO = 0; //(byte) PathIterator__Finals.SEG_MOVETO;
  13. public const sbyte SEG_LINETO = 1; //(byte) PathIterator__Finals.SEG_LINETO;
  14. public const sbyte SEG_QUADTO = 2; //(byte) PathIterator__Finals.SEG_QUADTO;
  15. public const sbyte SEG_CUBICTO = 3; //(byte) PathIterator__Finals.SEG_CUBICTO;
  16. public const sbyte SEG_CLOSE = 4; //(byte) PathIterator__Finals.SEG_CLOSE;
  17. public const sbyte SEG_START = 16; // segment start
  18. public const sbyte SEG_MASK = SEG_MOVETO | SEG_LINETO | SEG_QUADTO | SEG_CUBICTO | SEG_CLOSE; // mask to eliminate SEG_CLOSE and SEG_MARKER
  19. private const sbyte SEG_MARKER = 32; // path marker
  20. private const sbyte SEG_UNMARK_MASK = 95; // path unmarker
  21. private sbyte [] _types;
  22. private float [] _coords;
  23. private int _typesCount;
  24. private int _coordsCount;
  25. private int _windingRule;
  26. const int INIT_SIZE = 20;
  27. const int EXPAND_MAX = 500;
  28. #endregion // Fileds
  29. #region Constructors
  30. public ExtendedGeneralPath() : this (WIND_NON_ZERO, INIT_SIZE, INIT_SIZE)
  31. {
  32. }
  33. public ExtendedGeneralPath(int rule) : this (rule, INIT_SIZE, INIT_SIZE)
  34. {
  35. }
  36. public ExtendedGeneralPath(int rule, int initialCapacity) : this (rule, initialCapacity, initialCapacity)
  37. {
  38. }
  39. public ExtendedGeneralPath(Shape s) : this(WIND_NON_ZERO, INIT_SIZE, INIT_SIZE)
  40. {
  41. PathIterator pi = s.getPathIterator (null);
  42. setWindingRule (pi.getWindingRule ());
  43. append (pi, false);
  44. }
  45. private ExtendedGeneralPath(int rule, int initialTypes, int initialCoords)
  46. {
  47. setWindingRule(rule);
  48. _types = new sbyte [initialTypes];
  49. _coords = new float [initialCoords * 2];
  50. }
  51. #endregion // Constructors
  52. #region Properties
  53. private GeneralPath GeneralPath
  54. {
  55. get {
  56. PathIterator iter = getPathIterator (null);
  57. GeneralPath path = new GeneralPath ();
  58. path.append (iter, false);
  59. return path;
  60. }
  61. }
  62. public sbyte [] Types
  63. {
  64. get { return _types; }
  65. }
  66. public float [] Coords
  67. {
  68. get { return _coords; }
  69. }
  70. public int TypesCount
  71. {
  72. get { return _typesCount; }
  73. }
  74. public int CoordsCount
  75. {
  76. get { return _coordsCount; }
  77. }
  78. public bool LastFigureClosed
  79. {
  80. get {
  81. return ((TypesCount == 0) ||
  82. ((Types [TypesCount - 1] & ExtendedGeneralPath.SEG_CLOSE) != 0) ||
  83. ((Types [TypesCount - 1] & ExtendedGeneralPath.SEG_START) != 0));
  84. }
  85. }
  86. #endregion // Properties
  87. #region Methods
  88. public void append(Shape s)
  89. {
  90. append (s, !LastFigureClosed);
  91. }
  92. #region GeneralPath
  93. public void append(PathIterator pi, bool connect)
  94. {
  95. float [] coords = new float [6];
  96. while (!pi.isDone ()) {
  97. switch (pi.currentSegment (coords)) {
  98. case SEG_MOVETO:
  99. if (!connect || _typesCount < 1 || _coordsCount < 2) {
  100. moveTo (coords [0], coords [1]);
  101. break;
  102. }
  103. if (_types [_typesCount - 1] != SEG_CLOSE &&
  104. _coords [_coordsCount - 2] == coords [0] &&
  105. _coords [_coordsCount - 1] == coords [1])
  106. break;
  107. goto case SEG_LINETO;
  108. case SEG_LINETO:
  109. lineTo (coords [0], coords [1]);
  110. break;
  111. case SEG_QUADTO:
  112. quadTo (coords [0], coords [1], coords [2], coords [3]);
  113. break;
  114. case SEG_CUBICTO:
  115. curveTo (coords [0], coords [1], coords [2], coords [3], coords [4], coords [5]);
  116. break;
  117. case SEG_CLOSE:
  118. closePath ();
  119. break;
  120. }
  121. pi.next ();
  122. connect = false;
  123. }
  124. }
  125. public void append(Shape s, bool connect)
  126. {
  127. PathIterator pi = s.getPathIterator (null);
  128. append (pi,connect);
  129. }
  130. public object Clone()
  131. {
  132. ExtendedGeneralPath copy = new ExtendedGeneralPath ();
  133. copy._types = (sbyte []) _types.Clone ();
  134. copy._coords = (float []) _coords.Clone ();
  135. return copy;
  136. }
  137. public void closePath()
  138. {
  139. if (_typesCount == 0 || _types[_typesCount - 1] != SEG_CLOSE) {
  140. needRoom (1, 0, true);
  141. _types [_typesCount++] = SEG_CLOSE;
  142. }
  143. }
  144. public bool contains(double x, double y)
  145. {
  146. return GeneralPath.contains (x, y);
  147. }
  148. public bool contains(double x, double y, double w, double h)
  149. {
  150. return GeneralPath.contains (x, y, w, h);
  151. }
  152. public bool contains(Point2D p)
  153. {
  154. return contains (p.getX (), p.getY ());
  155. }
  156. public bool contains(Rectangle2D r)
  157. {
  158. return contains (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
  159. }
  160. public Shape createTransformedShape(AffineTransform at)
  161. {
  162. ExtendedGeneralPath gp = (ExtendedGeneralPath) Clone ();
  163. if (at != null) {
  164. gp.transform (at);
  165. }
  166. return gp;
  167. }
  168. public void curveTo(float x1, float y1, float x2, float y2, float x3, float y3)
  169. {
  170. needRoom (1, 6, true);
  171. _types [_typesCount++] = SEG_CUBICTO;
  172. _coords [_coordsCount++] = x1;
  173. _coords [_coordsCount++] = y1;
  174. _coords [_coordsCount++] = x2;
  175. _coords [_coordsCount++] = y2;
  176. _coords [_coordsCount++] = x3;
  177. _coords [_coordsCount++] = y3;
  178. }
  179. public java.awt.Rectangle getBounds()
  180. {
  181. return getBounds2D ().getBounds ();
  182. }
  183. public Rectangle2D getBounds2D()
  184. {
  185. float x1, y1, x2, y2;
  186. int i = _coordsCount;
  187. if (i > 0) {
  188. y1 = y2 = _coords [--i];
  189. x1 = x2 = _coords [--i];
  190. while (i > 0) {
  191. float y = _coords [--i];
  192. float x = _coords [--i];
  193. if (x < x1) x1 = x;
  194. if (y < y1) y1 = y;
  195. if (x > x2) x2 = x;
  196. if (y > y2) y2 = y;
  197. }
  198. }
  199. else {
  200. x1 = y1 = x2 = y2 = 0f;
  201. }
  202. return new Rectangle2D.Float (x1, y1, x2 - x1, y2 - y1);
  203. }
  204. public Point2D getCurrentPoint()
  205. {
  206. if (_typesCount < 1 || _coordsCount < 2)
  207. return null;
  208. int index = _coordsCount;
  209. if (_types [_typesCount - 1] == SEG_CLOSE)
  210. for (int i = _typesCount - 2; i > 0; i--) {
  211. switch (_types [i]) {
  212. case SEG_MOVETO:
  213. //break loop;
  214. goto loopend;
  215. case SEG_LINETO:
  216. index -= 2;
  217. break;
  218. case SEG_QUADTO:
  219. index -= 4;
  220. break;
  221. case SEG_CUBICTO:
  222. index -= 6;
  223. break;
  224. case SEG_CLOSE:
  225. break;
  226. }
  227. }
  228. loopend:
  229. return new Point2D.Float (_coords [index - 2], _coords [index - 1]);
  230. }
  231. public PathIterator getPathIterator(AffineTransform at) {
  232. return new GeneralPathIterator (this, at);
  233. }
  234. public PathIterator getPathIterator(AffineTransform at, double flatness) {
  235. return new FlatteningPathIterator (getPathIterator (at), flatness);
  236. }
  237. public int getWindingRule()
  238. {
  239. return _windingRule;
  240. }
  241. public bool intersects(double x, double y, double w, double h)
  242. {
  243. return GeneralPath.intersects (x, y, w, h);
  244. }
  245. public bool intersects(Rectangle2D r)
  246. {
  247. return intersects (r.getX (), r.getY (), r.getWidth (), r.getHeight ());
  248. }
  249. public void lineTo(float x, float y)
  250. {
  251. needRoom (1, 2, true);
  252. _types [_typesCount++] = SEG_LINETO;
  253. _coords [_coordsCount++] = x;
  254. _coords [_coordsCount++] = y;
  255. }
  256. public void moveTo(float x, float y)
  257. {
  258. if (_typesCount > 0 && _types [_typesCount - 1] == SEG_MOVETO) {
  259. _coords [_coordsCount - 2] = x;
  260. _coords [_coordsCount - 1] = y;
  261. }
  262. else {
  263. needRoom (1, 2, false);
  264. _types [_typesCount++] = SEG_MOVETO;
  265. _coords [_coordsCount++] = x;
  266. _coords [_coordsCount++] = y;
  267. }
  268. }
  269. public void quadTo(float x1, float y1, float x2, float y2)
  270. {
  271. needRoom (1, 4, true);
  272. _types [_typesCount++] = SEG_QUADTO;
  273. _coords [_coordsCount++] = x1;
  274. _coords [_coordsCount++] = y1;
  275. _coords [_coordsCount++] = x2;
  276. _coords [_coordsCount++] = y2;
  277. }
  278. public void reset()
  279. {
  280. _typesCount = 0;
  281. _coordsCount = 0;
  282. }
  283. public void setWindingRule(int rule)
  284. {
  285. if (rule != WIND_EVEN_ODD && rule != WIND_NON_ZERO) {
  286. throw new IllegalArgumentException ("winding rule must be WIND_EVEN_ODD or WIND_NON_ZERO");
  287. }
  288. _windingRule = rule;
  289. }
  290. public void transform(AffineTransform at)
  291. {
  292. at.transform (_coords, 0, _coords, 0, _coordsCount/2);
  293. }
  294. private void needRoom(int newTypes, int newCoords, bool needMove)
  295. {
  296. if (needMove && _typesCount == 0)
  297. throw new IllegalPathStateException ("missing initial moveto in path definition");
  298. int size = _coords.Length;
  299. if (_coordsCount + newCoords > size) {
  300. int grow = size;
  301. if (grow > EXPAND_MAX * 2)
  302. grow = EXPAND_MAX * 2;
  303. if (grow < newCoords)
  304. grow = newCoords;
  305. float [] arr = new float [size + grow];
  306. Array.Copy (_coords, 0, arr, 0, _coordsCount);
  307. _coords = arr;
  308. }
  309. size = _types.Length;
  310. if (_typesCount + newTypes > size) {
  311. int grow = size;
  312. if (grow > EXPAND_MAX)
  313. grow = EXPAND_MAX;
  314. if (grow < newTypes)
  315. grow = newTypes;
  316. sbyte [] arr = new sbyte [size + grow];
  317. Array.Copy (_types, 0, arr, 0, _typesCount);
  318. _types = arr;
  319. }
  320. }
  321. #endregion // GeneralPath
  322. public void SetMarkers()
  323. {
  324. Types [ TypesCount - 1] |= SEG_MARKER;
  325. }
  326. public void ClearMarkers()
  327. {
  328. for (int i = 0; i < TypesCount; i++)
  329. Types [i] &= SEG_UNMARK_MASK;
  330. }
  331. #endregion //Methods
  332. }
  333. }