path.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. if (!dojo._hasResource["dojox.gfx.path"]) { // _hasResource checks added by
  2. // build. Do not use _hasResource
  3. // directly in your code.
  4. dojo._hasResource["dojox.gfx.path"] = true;
  5. dojo.provide("dojox.gfx.path");
  6. dojo.require("dojox.gfx.shape");
  7. dojo.declare("dojox.gfx.path.Path", dojox.gfx.Shape, {
  8. // summary: a generalized path shape
  9. constructor : function(rawNode) {
  10. // summary: a path constructor
  11. // rawNode: Node: a DOM node to be used by this path object
  12. this.shape = dojo.clone(dojox.gfx.defaultPath);
  13. this.segments = [];
  14. this.absolute = true;
  15. this.last = {};
  16. this.rawNode = rawNode;
  17. },
  18. // mode manipulations
  19. setAbsoluteMode : function(mode) {
  20. // summary: sets an absolute or relative mode for path
  21. // points
  22. // mode: Boolean: true/false or "absolute"/"relative" to
  23. // specify the mode
  24. this.absolute = typeof mode == "string"
  25. ? (mode == "absolute")
  26. : mode;
  27. return this; // self
  28. },
  29. getAbsoluteMode : function() {
  30. // summary: returns a current value of the absolute mode
  31. return this.absolute; // Boolean
  32. },
  33. getBoundingBox : function() {
  34. // summary: returns the bounding box {x, y, width, height}
  35. // or null
  36. return (this.bbox && ("l" in this.bbox)) ? {
  37. x : this.bbox.l,
  38. y : this.bbox.t,
  39. width : this.bbox.r - this.bbox.l,
  40. height : this.bbox.b - this.bbox.t
  41. } : null; // dojox.gfx.Rectangle
  42. },
  43. getLastPosition : function() {
  44. // summary: returns the last point in the path, or null
  45. return "x" in this.last ? this.last : null; // Object
  46. },
  47. // segment interpretation
  48. _updateBBox : function(x, y) {
  49. // summary: updates the bounding box of path with new point
  50. // x: Number: an x coordinate
  51. // y: Number: a y coordinate
  52. // we use {l, b, r, t} representation of a bbox
  53. if (this.bbox && ("l" in this.bbox)) {
  54. if (this.bbox.l > x)
  55. this.bbox.l = x;
  56. if (this.bbox.r < x)
  57. this.bbox.r = x;
  58. if (this.bbox.t > y)
  59. this.bbox.t = y;
  60. if (this.bbox.b < y)
  61. this.bbox.b = y;
  62. } else {
  63. this.bbox = {
  64. l : x,
  65. b : y,
  66. r : x,
  67. t : y
  68. };
  69. }
  70. },
  71. _updateWithSegment : function(segment) {
  72. // summary: updates the bounding box of path with new
  73. // segment
  74. // segment: Object: a segment
  75. var n = segment.args, l = n.length;
  76. // update internal variables: bbox, absolute, last
  77. switch (segment.action) {
  78. case "M" :
  79. case "L" :
  80. case "C" :
  81. case "S" :
  82. case "Q" :
  83. case "T" :
  84. for (var i = 0; i < l; i += 2) {
  85. this._updateBBox(n[i], n[i + 1]);
  86. }
  87. this.last.x = n[l - 2];
  88. this.last.y = n[l - 1];
  89. this.absolute = true;
  90. break;
  91. case "H" :
  92. for (var i = 0; i < l; ++i) {
  93. this._updateBBox(n[i], this.last.y);
  94. }
  95. this.last.x = n[l - 1];
  96. this.absolute = true;
  97. break;
  98. case "V" :
  99. for (var i = 0; i < l; ++i) {
  100. this._updateBBox(this.last.x, n[i]);
  101. }
  102. this.last.y = n[l - 1];
  103. this.absolute = true;
  104. break;
  105. case "m" :
  106. var start = 0;
  107. if (!("x" in this.last)) {
  108. this._updateBBox(this.last.x = n[0],
  109. this.last.y = n[1]);
  110. start = 2;
  111. }
  112. for (var i = start; i < l; i += 2) {
  113. this._updateBBox(this.last.x += n[i],
  114. this.last.y += n[i + 1]);
  115. }
  116. this.absolute = false;
  117. break;
  118. case "l" :
  119. case "t" :
  120. for (var i = 0; i < l; i += 2) {
  121. this._updateBBox(this.last.x += n[i],
  122. this.last.y += n[i + 1]);
  123. }
  124. this.absolute = false;
  125. break;
  126. case "h" :
  127. for (var i = 0; i < l; ++i) {
  128. this._updateBBox(this.last.x += n[i],
  129. this.last.y);
  130. }
  131. this.absolute = false;
  132. break;
  133. case "v" :
  134. for (var i = 0; i < l; ++i) {
  135. this._updateBBox(this.last.x,
  136. this.last.y += n[i]);
  137. }
  138. this.absolute = false;
  139. break;
  140. case "c" :
  141. for (var i = 0; i < l; i += 6) {
  142. this._updateBBox(this.last.x + n[i],
  143. this.last.y + n[i + 1]);
  144. this._updateBBox(this.last.x + n[i + 2],
  145. this.last.y + n[i + 3]);
  146. this._updateBBox(this.last.x += n[i + 4],
  147. this.last.y += n[i + 5]);
  148. }
  149. this.absolute = false;
  150. break;
  151. case "s" :
  152. case "q" :
  153. for (var i = 0; i < l; i += 4) {
  154. this._updateBBox(this.last.x + n[i],
  155. this.last.y + n[i + 1]);
  156. this._updateBBox(this.last.x += n[i + 2],
  157. this.last.y += n[i + 3]);
  158. }
  159. this.absolute = false;
  160. break;
  161. case "A" :
  162. for (var i = 0; i < l; i += 7) {
  163. this._updateBBox(n[i + 5], n[i + 6]);
  164. }
  165. this.last.x = n[l - 2];
  166. this.last.y = n[l - 1];
  167. this.absolute = true;
  168. break;
  169. case "a" :
  170. for (var i = 0; i < l; i += 7) {
  171. this._updateBBox(this.last.x += n[i + 5],
  172. this.last.y += n[i + 6]);
  173. }
  174. this.absolute = false;
  175. break;
  176. }
  177. // add an SVG path segment
  178. var path = [segment.action];
  179. for (var i = 0; i < l; ++i) {
  180. path.push(dojox.gfx.formatNumber(n[i], true));
  181. }
  182. if (typeof this.shape.path == "string") {
  183. this.shape.path += path.join("");
  184. } else {
  185. var l = path.length, a = this.shape.path;
  186. for (var i = 0; i < l; ++i) {
  187. a.push(path[i]);
  188. }
  189. }
  190. },
  191. // a dictionary, which maps segment type codes to a number of
  192. // their argemnts
  193. _validSegments : {
  194. m : 2,
  195. l : 2,
  196. h : 1,
  197. v : 1,
  198. c : 6,
  199. s : 4,
  200. q : 4,
  201. t : 2,
  202. a : 7,
  203. z : 0
  204. },
  205. _pushSegment : function(action, args) {
  206. // summary: adds a segment
  207. // action: String: valid SVG code for a segment's type
  208. // args: Array: a list of parameters for this segment
  209. var group = this._validSegments[action.toLowerCase()];
  210. if (typeof group == "number") {
  211. if (group) {
  212. if (args.length >= group) {
  213. var segment = {
  214. action : action,
  215. args : args.slice(0, args.length
  216. - args.length % group)
  217. };
  218. this.segments.push(segment);
  219. this._updateWithSegment(segment);
  220. }
  221. } else {
  222. var segment = {
  223. action : action,
  224. args : []
  225. };
  226. this.segments.push(segment);
  227. this._updateWithSegment(segment);
  228. }
  229. }
  230. },
  231. _collectArgs : function(array, args) {
  232. // summary: converts an array of arguments to plain numeric
  233. // values
  234. // array: Array: an output argument (array of numbers)
  235. // args: Array: an input argument (can be values of Boolean,
  236. // Number, dojox.gfx.Point, or an embedded array of them)
  237. for (var i = 0; i < args.length; ++i) {
  238. var t = args[i];
  239. if (typeof t == "boolean") {
  240. array.push(t ? 1 : 0);
  241. } else if (typeof t == "number") {
  242. array.push(t);
  243. } else if (t instanceof Array) {
  244. this._collectArgs(array, t);
  245. } else if ("x" in t && "y" in t) {
  246. array.push(t.x, t.y);
  247. }
  248. }
  249. },
  250. // segments
  251. moveTo : function() {
  252. // summary: formes a move segment
  253. var args = [];
  254. this._collectArgs(args, arguments);
  255. this._pushSegment(this.absolute ? "M" : "m", args);
  256. return this; // self
  257. },
  258. lineTo : function() {
  259. // summary: formes a line segment
  260. var args = [];
  261. this._collectArgs(args, arguments);
  262. this._pushSegment(this.absolute ? "L" : "l", args);
  263. return this; // self
  264. },
  265. hLineTo : function() {
  266. // summary: formes a horizontal line segment
  267. var args = [];
  268. this._collectArgs(args, arguments);
  269. this._pushSegment(this.absolute ? "H" : "h", args);
  270. return this; // self
  271. },
  272. vLineTo : function() {
  273. // summary: formes a vertical line segment
  274. var args = [];
  275. this._collectArgs(args, arguments);
  276. this._pushSegment(this.absolute ? "V" : "v", args);
  277. return this; // self
  278. },
  279. curveTo : function() {
  280. // summary: formes a curve segment
  281. var args = [];
  282. this._collectArgs(args, arguments);
  283. this._pushSegment(this.absolute ? "C" : "c", args);
  284. return this; // self
  285. },
  286. smoothCurveTo : function() {
  287. // summary: formes a smooth curve segment
  288. var args = [];
  289. this._collectArgs(args, arguments);
  290. this._pushSegment(this.absolute ? "S" : "s", args);
  291. return this; // self
  292. },
  293. qCurveTo : function() {
  294. // summary: formes a quadratic curve segment
  295. var args = [];
  296. this._collectArgs(args, arguments);
  297. this._pushSegment(this.absolute ? "Q" : "q", args);
  298. return this; // self
  299. },
  300. qSmoothCurveTo : function() {
  301. // summary: formes a quadratic smooth curve segment
  302. var args = [];
  303. this._collectArgs(args, arguments);
  304. this._pushSegment(this.absolute ? "T" : "t", args);
  305. return this; // self
  306. },
  307. arcTo : function() {
  308. // summary: formes an elliptic arc segment
  309. var args = [];
  310. this._collectArgs(args, arguments);
  311. this._pushSegment(this.absolute ? "A" : "a", args);
  312. return this; // self
  313. },
  314. closePath : function() {
  315. // summary: closes a path
  316. this._pushSegment("Z", []);
  317. return this; // self
  318. },
  319. // setShape
  320. _setPath : function(path) {
  321. // summary: forms a path using an SVG path string
  322. // path: String: an SVG path string
  323. var p = dojo.isArray(path) ? path : path
  324. .match(dojox.gfx.pathSvgRegExp);
  325. this.segments = [];
  326. this.absolute = true;
  327. this.bbox = {};
  328. this.last = {};
  329. if (!p)
  330. return;
  331. // create segments
  332. var action = "", // current action
  333. args = [], // current arguments
  334. l = p.length;
  335. for (var i = 0; i < l; ++i) {
  336. var t = p[i], x = parseFloat(t);
  337. if (isNaN(x)) {
  338. if (action) {
  339. this._pushSegment(action, args);
  340. }
  341. args = [];
  342. action = t;
  343. } else {
  344. args.push(x);
  345. }
  346. }
  347. this._pushSegment(action, args);
  348. },
  349. setShape : function(newShape) {
  350. // summary: forms a path using a shape
  351. // newShape: Object: an SVG path string or a path object
  352. // (see dojox.gfx.defaultPath)
  353. dojox.gfx.Shape.prototype.setShape.call(this,
  354. typeof newShape == "string" ? {
  355. path : newShape
  356. } : newShape);
  357. var path = this.shape.path;
  358. // switch to non-updating version of path building
  359. this.shape.path = [];
  360. this._setPath(path);
  361. // switch back to the string path
  362. this.shape.path = this.shape.path.join("");
  363. return this; // self
  364. },
  365. // useful constant for descendants
  366. _2PI : Math.PI * 2
  367. });
  368. dojo.declare("dojox.gfx.path.TextPath", dojox.gfx.path.Path, {
  369. // summary: a generalized TextPath shape
  370. constructor : function(rawNode) {
  371. // summary: a TextPath shape constructor
  372. // rawNode: Node: a DOM node to be used by this TextPath
  373. // object
  374. if (!("text" in this)) {
  375. this.text = dojo.clone(dojox.gfx.defaultTextPath);
  376. }
  377. if (!("fontStyle" in this)) {
  378. this.fontStyle = dojo.clone(dojox.gfx.defaultFont);
  379. }
  380. },
  381. setText : function(newText) {
  382. // summary: sets a text to be drawn along the path
  383. this.text = dojox.gfx.makeParameters(this.text,
  384. typeof newText == "string" ? {
  385. text : newText
  386. } : newText);
  387. this._setText();
  388. return this; // self
  389. },
  390. setFont : function(newFont) {
  391. // summary: sets a font for text
  392. this.fontStyle = typeof newFont == "string" ? dojox.gfx
  393. .splitFontString(newFont) : dojox.gfx
  394. .makeParameters(dojox.gfx.defaultFont, newFont);
  395. this._setFont();
  396. return this; // self
  397. }
  398. });
  399. }