ExtendedGeneralPath.jvm.cs 9.4 KB

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