path.js 25 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992
  1. /*globals $, svgedit, svgroot*/
  2. /*jslint vars: true, eqeq: true, continue: true*/
  3. /**
  4. * Package: svgedit.path
  5. *
  6. * Licensed under the MIT License
  7. *
  8. * Copyright(c) 2011 Alexis Deveria
  9. * Copyright(c) 2011 Jeff Schiller
  10. */
  11. // Dependencies:
  12. // 1) jQuery
  13. // 2) browser.js
  14. // 3) math.js
  15. // 4) svgutils.js
  16. (function() {'use strict';
  17. if (!svgedit.path) {
  18. svgedit.path = {};
  19. }
  20. var NS = svgedit.NS;
  21. var uiStrings = {
  22. 'pathNodeTooltip': 'Drag node to move it. Double-click node to change segment type',
  23. 'pathCtrlPtTooltip': 'Drag control point to adjust curve properties'
  24. };
  25. var segData = {
  26. 2: ['x', 'y'],
  27. 4: ['x', 'y'],
  28. 6: ['x', 'y', 'x1', 'y1', 'x2', 'y2'],
  29. 8: ['x', 'y', 'x1', 'y1'],
  30. 10: ['x', 'y', 'r1', 'r2', 'angle', 'largeArcFlag', 'sweepFlag'],
  31. 12: ['x'],
  32. 14: ['y'],
  33. 16: ['x', 'y', 'x2', 'y2'],
  34. 18: ['x', 'y']
  35. };
  36. var pathFuncs = [];
  37. var link_control_pts = true;
  38. // Stores references to paths via IDs.
  39. // TODO: Make this cross-document happy.
  40. var pathData = {};
  41. svgedit.path.setLinkControlPoints = function(lcp) {
  42. link_control_pts = lcp;
  43. };
  44. svgedit.path.path = null;
  45. var editorContext_ = null;
  46. svgedit.path.init = function(editorContext) {
  47. editorContext_ = editorContext;
  48. pathFuncs = [0, 'ClosePath'];
  49. var pathFuncsStrs = ['Moveto', 'Lineto', 'CurvetoCubic', 'CurvetoQuadratic', 'Arc',
  50. 'LinetoHorizontal', 'LinetoVertical', 'CurvetoCubicSmooth', 'CurvetoQuadraticSmooth'];
  51. $.each(pathFuncsStrs, function(i, s) {
  52. pathFuncs.push(s+'Abs');
  53. pathFuncs.push(s+'Rel');
  54. });
  55. };
  56. svgedit.path.insertItemBefore = function(elem, newseg, index) {
  57. // Support insertItemBefore on paths for FF2
  58. var list = elem.pathSegList;
  59. if (svgedit.browser.supportsPathInsertItemBefore()) {
  60. list.insertItemBefore(newseg, index);
  61. return;
  62. }
  63. var len = list.numberOfItems;
  64. var arr = [];
  65. var i;
  66. for (i=0; i < len; i++) {
  67. var cur_seg = list.getItem(i);
  68. arr.push(cur_seg);
  69. }
  70. list.clear();
  71. for (i=0; i < len; i++) {
  72. if (i == index) { //index+1
  73. list.appendItem(newseg);
  74. }
  75. list.appendItem(arr[i]);
  76. }
  77. };
  78. // TODO: See if this should just live in replacePathSeg
  79. svgedit.path.ptObjToArr = function(type, seg_item) {
  80. var arr = segData[type], len = arr.length;
  81. var i, out = [];
  82. for (i = 0; i < len; i++) {
  83. out[i] = seg_item[arr[i]];
  84. }
  85. return out;
  86. };
  87. svgedit.path.getGripPt = function(seg, alt_pt) {
  88. var out = {
  89. x: alt_pt? alt_pt.x : seg.item.x,
  90. y: alt_pt? alt_pt.y : seg.item.y
  91. }, path = seg.path;
  92. if (path.matrix) {
  93. var pt = svgedit.math.transformPoint(out.x, out.y, path.matrix);
  94. out = pt;
  95. }
  96. out.x *= editorContext_.getCurrentZoom();
  97. out.y *= editorContext_.getCurrentZoom();
  98. return out;
  99. };
  100. svgedit.path.getPointFromGrip = function(pt, path) {
  101. var out = {
  102. x: pt.x,
  103. y: pt.y
  104. };
  105. if (path.matrix) {
  106. pt = svgedit.math.transformPoint(out.x, out.y, path.imatrix);
  107. out.x = pt.x;
  108. out.y = pt.y;
  109. }
  110. out.x /= editorContext_.getCurrentZoom();
  111. out.y /= editorContext_.getCurrentZoom();
  112. return out;
  113. };
  114. svgedit.path.addPointGrip = function(index, x, y) {
  115. // create the container of all the point grips
  116. var pointGripContainer = svgedit.path.getGripContainer();
  117. var pointGrip = svgedit.utilities.getElem('pathpointgrip_'+index);
  118. // create it
  119. if (!pointGrip) {
  120. pointGrip = document.createElementNS(NS.SVG, 'circle');
  121. svgedit.utilities.assignAttributes(pointGrip, {
  122. 'id': 'pathpointgrip_' + index,
  123. 'display': 'none',
  124. 'r': 4,
  125. 'fill': '#0FF',
  126. 'stroke': '#00F',
  127. 'stroke-width': 2,
  128. 'cursor': 'move',
  129. 'style': 'pointer-events:all',
  130. 'xlink:title': uiStrings.pathNodeTooltip
  131. });
  132. pointGrip = pointGripContainer.appendChild(pointGrip);
  133. var grip = $('#pathpointgrip_'+index);
  134. grip.dblclick(function() {
  135. if (svgedit.path.path) {
  136. svgedit.path.path.setSegType();
  137. }
  138. });
  139. }
  140. if (x && y) {
  141. // set up the point grip element and display it
  142. svgedit.utilities.assignAttributes(pointGrip, {
  143. 'cx': x,
  144. 'cy': y,
  145. 'display': 'inline'
  146. });
  147. }
  148. return pointGrip;
  149. };
  150. svgedit.path.getGripContainer = function() {
  151. var c = svgedit.utilities.getElem('pathpointgrip_container');
  152. if (!c) {
  153. var parent = svgedit.utilities.getElem('selectorParentGroup');
  154. c = parent.appendChild(document.createElementNS(NS.SVG, 'g'));
  155. c.id = 'pathpointgrip_container';
  156. }
  157. return c;
  158. };
  159. svgedit.path.addCtrlGrip = function(id) {
  160. var pointGrip = svgedit.utilities.getElem('ctrlpointgrip_'+id);
  161. if (pointGrip) {return pointGrip;}
  162. pointGrip = document.createElementNS(NS.SVG, 'circle');
  163. svgedit.utilities.assignAttributes(pointGrip, {
  164. 'id': 'ctrlpointgrip_' + id,
  165. 'display': 'none',
  166. 'r': 4,
  167. 'fill': '#0FF',
  168. 'stroke': '#55F',
  169. 'stroke-width': 1,
  170. 'cursor': 'move',
  171. 'style': 'pointer-events:all',
  172. 'xlink:title': uiStrings.pathCtrlPtTooltip
  173. });
  174. svgedit.path.getGripContainer().appendChild(pointGrip);
  175. return pointGrip;
  176. };
  177. svgedit.path.getCtrlLine = function(id) {
  178. var ctrlLine = svgedit.utilities.getElem('ctrlLine_'+id);
  179. if (ctrlLine) {return ctrlLine;}
  180. ctrlLine = document.createElementNS(NS.SVG, 'line');
  181. svgedit.utilities.assignAttributes(ctrlLine, {
  182. 'id': 'ctrlLine_'+id,
  183. 'stroke': '#555',
  184. 'stroke-width': 1,
  185. 'style': 'pointer-events:none'
  186. });
  187. svgedit.path.getGripContainer().appendChild(ctrlLine);
  188. return ctrlLine;
  189. };
  190. svgedit.path.getPointGrip = function(seg, update) {
  191. var index = seg.index;
  192. var pointGrip = svgedit.path.addPointGrip(index);
  193. if (update) {
  194. var pt = svgedit.path.getGripPt(seg);
  195. svgedit.utilities.assignAttributes(pointGrip, {
  196. 'cx': pt.x,
  197. 'cy': pt.y,
  198. 'display': 'inline'
  199. });
  200. }
  201. return pointGrip;
  202. };
  203. svgedit.path.getControlPoints = function(seg) {
  204. var item = seg.item;
  205. var index = seg.index;
  206. if (!('x1' in item) || !('x2' in item)) {return null;}
  207. var cpt = {};
  208. var pointGripContainer = svgedit.path.getGripContainer();
  209. // Note that this is intentionally not seg.prev.item
  210. var prev = svgedit.path.path.segs[index-1].item;
  211. var seg_items = [prev, item];
  212. var i;
  213. for (i = 1; i < 3; i++) {
  214. var id = index + 'c' + i;
  215. var ctrlLine = cpt['c' + i + '_line'] = svgedit.path.getCtrlLine(id);
  216. var pt = svgedit.path.getGripPt(seg, {x:item['x' + i], y:item['y' + i]});
  217. var gpt = svgedit.path.getGripPt(seg, {x:seg_items[i-1].x, y:seg_items[i-1].y});
  218. svgedit.utilities.assignAttributes(ctrlLine, {
  219. 'x1': pt.x,
  220. 'y1': pt.y,
  221. 'x2': gpt.x,
  222. 'y2': gpt.y,
  223. 'display': 'inline'
  224. });
  225. cpt['c' + i + '_line'] = ctrlLine;
  226. // create it
  227. var pointGrip = cpt['c' + i] = svgedit.path.addCtrlGrip(id);
  228. svgedit.utilities.assignAttributes(pointGrip, {
  229. 'cx': pt.x,
  230. 'cy': pt.y,
  231. 'display': 'inline'
  232. });
  233. cpt['c' + i] = pointGrip;
  234. }
  235. return cpt;
  236. };
  237. // This replaces the segment at the given index. Type is given as number.
  238. svgedit.path.replacePathSeg = function(type, index, pts, elem) {
  239. var path = elem || svgedit.path.path.elem;
  240. var func = 'createSVGPathSeg' + pathFuncs[type];
  241. var seg = path[func].apply(path, pts);
  242. if (svgedit.browser.supportsPathReplaceItem()) {
  243. path.pathSegList.replaceItem(seg, index);
  244. } else {
  245. var segList = path.pathSegList;
  246. var len = segList.numberOfItems;
  247. var arr = [];
  248. var i;
  249. for (i = 0; i < len; i++) {
  250. var cur_seg = segList.getItem(i);
  251. arr.push(cur_seg);
  252. }
  253. segList.clear();
  254. for (i = 0; i < len; i++) {
  255. if (i == index) {
  256. segList.appendItem(seg);
  257. } else {
  258. segList.appendItem(arr[i]);
  259. }
  260. }
  261. }
  262. };
  263. svgedit.path.getSegSelector = function(seg, update) {
  264. var index = seg.index;
  265. var segLine = svgedit.utilities.getElem('segline_' + index);
  266. if (!segLine) {
  267. var pointGripContainer = svgedit.path.getGripContainer();
  268. // create segline
  269. segLine = document.createElementNS(NS.SVG, 'path');
  270. svgedit.utilities.assignAttributes(segLine, {
  271. 'id': 'segline_' + index,
  272. 'display': 'none',
  273. 'fill': 'none',
  274. 'stroke': '#0FF',
  275. 'stroke-width': 2,
  276. 'style':'pointer-events:none',
  277. 'd': 'M0,0 0,0'
  278. });
  279. pointGripContainer.appendChild(segLine);
  280. }
  281. if (update) {
  282. var prev = seg.prev;
  283. if (!prev) {
  284. segLine.setAttribute('display', 'none');
  285. return segLine;
  286. }
  287. var pt = svgedit.path.getGripPt(prev);
  288. // Set start point
  289. svgedit.path.replacePathSeg(2, 0, [pt.x, pt.y], segLine);
  290. var pts = svgedit.path.ptObjToArr(seg.type, seg.item, true);
  291. var i;
  292. for (i = 0; i < pts.length; i += 2) {
  293. pt = svgedit.path.getGripPt(seg, {x:pts[i], y:pts[i+1]});
  294. pts[i] = pt.x;
  295. pts[i+1] = pt.y;
  296. }
  297. svgedit.path.replacePathSeg(seg.type, 1, pts, segLine);
  298. }
  299. return segLine;
  300. };
  301. // Function: smoothControlPoints
  302. // Takes three points and creates a smoother line based on them
  303. //
  304. // Parameters:
  305. // ct1 - Object with x and y values (first control point)
  306. // ct2 - Object with x and y values (second control point)
  307. // pt - Object with x and y values (third point)
  308. //
  309. // Returns:
  310. // Array of two "smoothed" point objects
  311. svgedit.path.smoothControlPoints = function(ct1, ct2, pt) {
  312. // each point must not be the origin
  313. var x1 = ct1.x - pt.x,
  314. y1 = ct1.y - pt.y,
  315. x2 = ct2.x - pt.x,
  316. y2 = ct2.y - pt.y;
  317. if ( (x1 != 0 || y1 != 0) && (x2 != 0 || y2 != 0) ) {
  318. var anglea = Math.atan2(y1, x1),
  319. angleb = Math.atan2(y2, x2),
  320. r1 = Math.sqrt(x1*x1+y1*y1),
  321. r2 = Math.sqrt(x2*x2+y2*y2),
  322. nct1 = editorContext_.getSVGRoot().createSVGPoint(),
  323. nct2 = editorContext_.getSVGRoot().createSVGPoint();
  324. if (anglea < 0) { anglea += 2*Math.PI; }
  325. if (angleb < 0) { angleb += 2*Math.PI; }
  326. var angleBetween = Math.abs(anglea - angleb),
  327. angleDiff = Math.abs(Math.PI - angleBetween)/2;
  328. var new_anglea, new_angleb;
  329. if (anglea - angleb > 0) {
  330. new_anglea = angleBetween < Math.PI ? (anglea + angleDiff) : (anglea - angleDiff);
  331. new_angleb = angleBetween < Math.PI ? (angleb - angleDiff) : (angleb + angleDiff);
  332. }
  333. else {
  334. new_anglea = angleBetween < Math.PI ? (anglea - angleDiff) : (anglea + angleDiff);
  335. new_angleb = angleBetween < Math.PI ? (angleb + angleDiff) : (angleb - angleDiff);
  336. }
  337. // rotate the points
  338. nct1.x = r1 * Math.cos(new_anglea) + pt.x;
  339. nct1.y = r1 * Math.sin(new_anglea) + pt.y;
  340. nct2.x = r2 * Math.cos(new_angleb) + pt.x;
  341. nct2.y = r2 * Math.sin(new_angleb) + pt.y;
  342. return [nct1, nct2];
  343. }
  344. return undefined;
  345. };
  346. svgedit.path.Segment = function(index, item) {
  347. this.selected = false;
  348. this.index = index;
  349. this.item = item;
  350. this.type = item.pathSegType;
  351. this.ctrlpts = [];
  352. this.ptgrip = null;
  353. this.segsel = null;
  354. };
  355. svgedit.path.Segment.prototype.showCtrlPts = function(y) {
  356. var i;
  357. for (i in this.ctrlpts) {
  358. if (this.ctrlpts.hasOwnProperty(i)) {
  359. this.ctrlpts[i].setAttribute('display', y ? 'inline' : 'none');
  360. }
  361. }
  362. };
  363. svgedit.path.Segment.prototype.selectCtrls = function(y) {
  364. $('#ctrlpointgrip_' + this.index + 'c1, #ctrlpointgrip_' + this.index + 'c2').
  365. attr('fill', y ? '#0FF' : '#EEE');
  366. };
  367. svgedit.path.Segment.prototype.show = function(y) {
  368. if (this.ptgrip) {
  369. this.ptgrip.setAttribute('display', y ? 'inline' : 'none');
  370. this.segsel.setAttribute('display', y ? 'inline' : 'none');
  371. // Show/hide all control points if available
  372. this.showCtrlPts(y);
  373. }
  374. };
  375. svgedit.path.Segment.prototype.select = function(y) {
  376. if (this.ptgrip) {
  377. this.ptgrip.setAttribute('stroke', y ? '#0FF' : '#00F');
  378. this.segsel.setAttribute('display', y ? 'inline' : 'none');
  379. if (this.ctrlpts) {
  380. this.selectCtrls(y);
  381. }
  382. this.selected = y;
  383. }
  384. };
  385. svgedit.path.Segment.prototype.addGrip = function() {
  386. this.ptgrip = svgedit.path.getPointGrip(this, true);
  387. this.ctrlpts = svgedit.path.getControlPoints(this, true);
  388. this.segsel = svgedit.path.getSegSelector(this, true);
  389. };
  390. svgedit.path.Segment.prototype.update = function(full) {
  391. if (this.ptgrip) {
  392. var pt = svgedit.path.getGripPt(this);
  393. svgedit.utilities.assignAttributes(this.ptgrip, {
  394. 'cx': pt.x,
  395. 'cy': pt.y
  396. });
  397. svgedit.path.getSegSelector(this, true);
  398. if (this.ctrlpts) {
  399. if (full) {
  400. this.item = svgedit.path.path.elem.pathSegList.getItem(this.index);
  401. this.type = this.item.pathSegType;
  402. }
  403. svgedit.path.getControlPoints(this);
  404. }
  405. // this.segsel.setAttribute('display', y?'inline':'none');
  406. }
  407. };
  408. svgedit.path.Segment.prototype.move = function(dx, dy) {
  409. var cur_pts, item = this.item;
  410. if (this.ctrlpts) {
  411. cur_pts = [item.x += dx, item.y += dy,
  412. item.x1, item.y1, item.x2 += dx, item.y2 += dy];
  413. } else {
  414. cur_pts = [item.x += dx, item.y += dy];
  415. }
  416. svgedit.path.replacePathSeg(this.type, this.index, cur_pts);
  417. if (this.next && this.next.ctrlpts) {
  418. var next = this.next.item;
  419. var next_pts = [next.x, next.y,
  420. next.x1 += dx, next.y1 += dy, next.x2, next.y2];
  421. svgedit.path.replacePathSeg(this.next.type, this.next.index, next_pts);
  422. }
  423. if (this.mate) {
  424. // The last point of a closed subpath has a 'mate',
  425. // which is the 'M' segment of the subpath
  426. item = this.mate.item;
  427. var pts = [item.x += dx, item.y += dy];
  428. svgedit.path.replacePathSeg(this.mate.type, this.mate.index, pts);
  429. // Has no grip, so does not need 'updating'?
  430. }
  431. this.update(true);
  432. if (this.next) {this.next.update(true);}
  433. };
  434. svgedit.path.Segment.prototype.setLinked = function(num) {
  435. var seg, anum, pt;
  436. if (num == 2) {
  437. anum = 1;
  438. seg = this.next;
  439. if (!seg) {return;}
  440. pt = this.item;
  441. } else {
  442. anum = 2;
  443. seg = this.prev;
  444. if (!seg) {return;}
  445. pt = seg.item;
  446. }
  447. var item = seg.item;
  448. item['x' + anum] = pt.x + (pt.x - this.item['x' + num]);
  449. item['y' + anum] = pt.y + (pt.y - this.item['y' + num]);
  450. var pts = [item.x, item.y,
  451. item.x1, item.y1,
  452. item.x2, item.y2];
  453. svgedit.path.replacePathSeg(seg.type, seg.index, pts);
  454. seg.update(true);
  455. };
  456. svgedit.path.Segment.prototype.moveCtrl = function(num, dx, dy) {
  457. var item = this.item;
  458. item['x' + num] += dx;
  459. item['y' + num] += dy;
  460. var pts = [item.x, item.y, item.x1, item.y1, item.x2, item.y2];
  461. svgedit.path.replacePathSeg(this.type, this.index, pts);
  462. this.update(true);
  463. };
  464. svgedit.path.Segment.prototype.setType = function(new_type, pts) {
  465. svgedit.path.replacePathSeg(new_type, this.index, pts);
  466. this.type = new_type;
  467. this.item = svgedit.path.path.elem.pathSegList.getItem(this.index);
  468. this.showCtrlPts(new_type === 6);
  469. this.ctrlpts = svgedit.path.getControlPoints(this);
  470. this.update(true);
  471. };
  472. svgedit.path.Path = function(elem) {
  473. if (!elem || elem.tagName !== 'path') {
  474. throw 'svgedit.path.Path constructed without a <path> element';
  475. }
  476. this.elem = elem;
  477. this.segs = [];
  478. this.selected_pts = [];
  479. svgedit.path.path = this;
  480. this.init();
  481. };
  482. // Reset path data
  483. svgedit.path.Path.prototype.init = function() {
  484. // Hide all grips, etc
  485. $(svgedit.path.getGripContainer()).find('*').attr('display', 'none');
  486. var segList = this.elem.pathSegList;
  487. var len = segList.numberOfItems;
  488. this.segs = [];
  489. this.selected_pts = [];
  490. this.first_seg = null;
  491. // Set up segs array
  492. var i;
  493. for (i = 0; i < len; i++) {
  494. var item = segList.getItem(i);
  495. var segment = new svgedit.path.Segment(i, item);
  496. segment.path = this;
  497. this.segs.push(segment);
  498. }
  499. var segs = this.segs;
  500. var start_i = null;
  501. for (i = 0; i < len; i++) {
  502. var seg = segs[i];
  503. var next_seg = (i+1) >= len ? null : segs[i+1];
  504. var prev_seg = (i-1) < 0 ? null : segs[i-1];
  505. var start_seg;
  506. if (seg.type === 2) {
  507. if (prev_seg && prev_seg.type !== 1) {
  508. // New sub-path, last one is open,
  509. // so add a grip to last sub-path's first point
  510. start_seg = segs[start_i];
  511. start_seg.next = segs[start_i+1];
  512. start_seg.next.prev = start_seg;
  513. start_seg.addGrip();
  514. }
  515. // Remember that this is a starter seg
  516. start_i = i;
  517. } else if (next_seg && next_seg.type === 1) {
  518. // This is the last real segment of a closed sub-path
  519. // Next is first seg after "M"
  520. seg.next = segs[start_i+1];
  521. // First seg after "M"'s prev is this
  522. seg.next.prev = seg;
  523. seg.mate = segs[start_i];
  524. seg.addGrip();
  525. if (this.first_seg == null) {
  526. this.first_seg = seg;
  527. }
  528. } else if (!next_seg) {
  529. if (seg.type !== 1) {
  530. // Last seg, doesn't close so add a grip
  531. // to last sub-path's first point
  532. start_seg = segs[start_i];
  533. start_seg.next = segs[start_i+1];
  534. start_seg.next.prev = start_seg;
  535. start_seg.addGrip();
  536. seg.addGrip();
  537. if (!this.first_seg) {
  538. // Open path, so set first as real first and add grip
  539. this.first_seg = segs[start_i];
  540. }
  541. }
  542. } else if (seg.type !== 1){
  543. // Regular segment, so add grip and its "next"
  544. seg.addGrip();
  545. // Don't set its "next" if it's an "M"
  546. if (next_seg && next_seg.type !== 2) {
  547. seg.next = next_seg;
  548. seg.next.prev = seg;
  549. }
  550. }
  551. }
  552. return this;
  553. };
  554. svgedit.path.Path.prototype.eachSeg = function(fn) {
  555. var i;
  556. var len = this.segs.length;
  557. for (i = 0; i < len; i++) {
  558. var ret = fn.call(this.segs[i], i);
  559. if (ret === false) {break;}
  560. }
  561. };
  562. svgedit.path.Path.prototype.addSeg = function(index) {
  563. // Adds a new segment
  564. var seg = this.segs[index];
  565. if (!seg.prev) {return;}
  566. var prev = seg.prev;
  567. var newseg, new_x, new_y;
  568. switch(seg.item.pathSegType) {
  569. case 4:
  570. new_x = (seg.item.x + prev.item.x) / 2;
  571. new_y = (seg.item.y + prev.item.y) / 2;
  572. newseg = this.elem.createSVGPathSegLinetoAbs(new_x, new_y);
  573. break;
  574. case 6: //make it a curved segment to preserve the shape (WRS)
  575. // http://en.wikipedia.org/wiki/De_Casteljau%27s_algorithm#Geometric_interpretation
  576. var p0_x = (prev.item.x + seg.item.x1)/2;
  577. var p1_x = (seg.item.x1 + seg.item.x2)/2;
  578. var p2_x = (seg.item.x2 + seg.item.x)/2;
  579. var p01_x = (p0_x + p1_x)/2;
  580. var p12_x = (p1_x + p2_x)/2;
  581. new_x = (p01_x + p12_x)/2;
  582. var p0_y = (prev.item.y + seg.item.y1)/2;
  583. var p1_y = (seg.item.y1 + seg.item.y2)/2;
  584. var p2_y = (seg.item.y2 + seg.item.y)/2;
  585. var p01_y = (p0_y + p1_y)/2;
  586. var p12_y = (p1_y + p2_y)/2;
  587. new_y = (p01_y + p12_y)/2;
  588. newseg = this.elem.createSVGPathSegCurvetoCubicAbs(new_x, new_y, p0_x, p0_y, p01_x, p01_y);
  589. var pts = [seg.item.x, seg.item.y, p12_x, p12_y, p2_x, p2_y];
  590. svgedit.path.replacePathSeg(seg.type, index, pts);
  591. break;
  592. }
  593. svgedit.path.insertItemBefore(this.elem, newseg, index);
  594. };
  595. svgedit.path.Path.prototype.deleteSeg = function(index) {
  596. var seg = this.segs[index];
  597. var list = this.elem.pathSegList;
  598. seg.show(false);
  599. var next = seg.next;
  600. var pt;
  601. if (seg.mate) {
  602. // Make the next point be the "M" point
  603. pt = [next.item.x, next.item.y];
  604. svgedit.path.replacePathSeg(2, next.index, pt);
  605. // Reposition last node
  606. svgedit.path.replacePathSeg(4, seg.index, pt);
  607. list.removeItem(seg.mate.index);
  608. } else if (!seg.prev) {
  609. // First node of open path, make next point the M
  610. var item = seg.item;
  611. pt = [next.item.x, next.item.y];
  612. svgedit.path.replacePathSeg(2, seg.next.index, pt);
  613. list.removeItem(index);
  614. } else {
  615. list.removeItem(index);
  616. }
  617. };
  618. svgedit.path.Path.prototype.subpathIsClosed = function(index) {
  619. var closed = false;
  620. // Check if subpath is already open
  621. svgedit.path.path.eachSeg(function(i) {
  622. if (i <= index) {return true;}
  623. if (this.type === 2) {
  624. // Found M first, so open
  625. return false;
  626. }
  627. if (this.type === 1) {
  628. // Found Z first, so closed
  629. closed = true;
  630. return false;
  631. }
  632. });
  633. return closed;
  634. };
  635. svgedit.path.Path.prototype.removePtFromSelection = function(index) {
  636. var pos = this.selected_pts.indexOf(index);
  637. if (pos == -1) {
  638. return;
  639. }
  640. this.segs[index].select(false);
  641. this.selected_pts.splice(pos, 1);
  642. };
  643. svgedit.path.Path.prototype.clearSelection = function() {
  644. this.eachSeg(function() {
  645. // 'this' is the segment here
  646. this.select(false);
  647. });
  648. this.selected_pts = [];
  649. };
  650. svgedit.path.Path.prototype.storeD = function() {
  651. this.last_d = this.elem.getAttribute('d');
  652. };
  653. svgedit.path.Path.prototype.show = function(y) {
  654. // Shows this path's segment grips
  655. this.eachSeg(function() {
  656. // 'this' is the segment here
  657. this.show(y);
  658. });
  659. if (y) {
  660. this.selectPt(this.first_seg.index);
  661. }
  662. return this;
  663. };
  664. // Move selected points
  665. svgedit.path.Path.prototype.movePts = function(d_x, d_y) {
  666. var i = this.selected_pts.length;
  667. while(i--) {
  668. var seg = this.segs[this.selected_pts[i]];
  669. seg.move(d_x, d_y);
  670. }
  671. };
  672. svgedit.path.Path.prototype.moveCtrl = function(d_x, d_y) {
  673. var seg = this.segs[this.selected_pts[0]];
  674. seg.moveCtrl(this.dragctrl, d_x, d_y);
  675. if (link_control_pts) {
  676. seg.setLinked(this.dragctrl);
  677. }
  678. };
  679. svgedit.path.Path.prototype.setSegType = function(new_type) {
  680. this.storeD();
  681. var i = this.selected_pts.length;
  682. var text;
  683. while(i--) {
  684. var sel_pt = this.selected_pts[i];
  685. // Selected seg
  686. var cur = this.segs[sel_pt];
  687. var prev = cur.prev;
  688. if (!prev) {continue;}
  689. if (!new_type) { // double-click, so just toggle
  690. text = 'Toggle Path Segment Type';
  691. // Toggle segment to curve/straight line
  692. var old_type = cur.type;
  693. new_type = (old_type == 6) ? 4 : 6;
  694. }
  695. new_type = Number(new_type);
  696. var cur_x = cur.item.x;
  697. var cur_y = cur.item.y;
  698. var prev_x = prev.item.x;
  699. var prev_y = prev.item.y;
  700. var points;
  701. switch ( new_type ) {
  702. case 6:
  703. if (cur.olditem) {
  704. var old = cur.olditem;
  705. points = [cur_x, cur_y, old.x1, old.y1, old.x2, old.y2];
  706. } else {
  707. var diff_x = cur_x - prev_x;
  708. var diff_y = cur_y - prev_y;
  709. // get control points from straight line segment
  710. /*
  711. var ct1_x = (prev_x + (diff_y/2));
  712. var ct1_y = (prev_y - (diff_x/2));
  713. var ct2_x = (cur_x + (diff_y/2));
  714. var ct2_y = (cur_y - (diff_x/2));
  715. */
  716. //create control points on the line to preserve the shape (WRS)
  717. var ct1_x = (prev_x + (diff_x/3));
  718. var ct1_y = (prev_y + (diff_y/3));
  719. var ct2_x = (cur_x - (diff_x/3));
  720. var ct2_y = (cur_y - (diff_y/3));
  721. points = [cur_x, cur_y, ct1_x, ct1_y, ct2_x, ct2_y];
  722. }
  723. break;
  724. case 4:
  725. points = [cur_x, cur_y];
  726. // Store original prevve segment nums
  727. cur.olditem = cur.item;
  728. break;
  729. }
  730. cur.setType(new_type, points);
  731. }
  732. svgedit.path.path.endChanges(text);
  733. };
  734. svgedit.path.Path.prototype.selectPt = function(pt, ctrl_num) {
  735. this.clearSelection();
  736. if (pt == null) {
  737. this.eachSeg(function(i) {
  738. // 'this' is the segment here.
  739. if (this.prev) {
  740. pt = i;
  741. }
  742. });
  743. }
  744. this.addPtsToSelection(pt);
  745. if (ctrl_num) {
  746. this.dragctrl = ctrl_num;
  747. if (link_control_pts) {
  748. this.segs[pt].setLinked(ctrl_num);
  749. }
  750. }
  751. };
  752. // Update position of all points
  753. svgedit.path.Path.prototype.update = function() {
  754. var elem = this.elem;
  755. if (svgedit.utilities.getRotationAngle(elem)) {
  756. this.matrix = svgedit.math.getMatrix(elem);
  757. this.imatrix = this.matrix.inverse();
  758. } else {
  759. this.matrix = null;
  760. this.imatrix = null;
  761. }
  762. this.eachSeg(function(i) {
  763. this.item = elem.pathSegList.getItem(i);
  764. this.update();
  765. });
  766. return this;
  767. };
  768. svgedit.path.getPath_ = function(elem) {
  769. var p = pathData[elem.id];
  770. if (!p) {
  771. p = pathData[elem.id] = new svgedit.path.Path(elem);
  772. }
  773. return p;
  774. };
  775. svgedit.path.removePath_ = function(id) {
  776. if (id in pathData) {delete pathData[id];}
  777. };
  778. var newcx, newcy, oldcx, oldcy, angle;
  779. var getRotVals = function(x, y) {
  780. var dx = x - oldcx;
  781. var dy = y - oldcy;
  782. // rotate the point around the old center
  783. var r = Math.sqrt(dx*dx + dy*dy);
  784. var theta = Math.atan2(dy, dx) + angle;
  785. dx = r * Math.cos(theta) + oldcx;
  786. dy = r * Math.sin(theta) + oldcy;
  787. // dx,dy should now hold the actual coordinates of each
  788. // point after being rotated
  789. // now we want to rotate them around the new center in the reverse direction
  790. dx -= newcx;
  791. dy -= newcy;
  792. r = Math.sqrt(dx*dx + dy*dy);
  793. theta = Math.atan2(dy, dx) - angle;
  794. return {'x': r * Math.cos(theta) + newcx,
  795. 'y': r * Math.sin(theta) + newcy};
  796. };
  797. // If the path was rotated, we must now pay the piper:
  798. // Every path point must be rotated into the rotated coordinate system of
  799. // its old center, then determine the new center, then rotate it back
  800. // This is because we want the path to remember its rotation
  801. // TODO: This is still using ye olde transform methods, can probably
  802. // be optimized or even taken care of by recalculateDimensions
  803. svgedit.path.recalcRotatedPath = function() {
  804. var current_path = svgedit.path.path.elem;
  805. angle = svgedit.utilities.getRotationAngle(current_path, true);
  806. if (!angle) {return;}
  807. // selectedBBoxes[0] = svgedit.path.path.oldbbox;
  808. var box = svgedit.utilities.getBBox(current_path),
  809. oldbox = svgedit.path.path.oldbbox; //selectedBBoxes[0],
  810. oldcx = oldbox.x + oldbox.width/2;
  811. oldcy = oldbox.y + oldbox.height/2;
  812. newcx = box.x + box.width/2;
  813. newcy = box.y + box.height/2;
  814. // un-rotate the new center to the proper position
  815. var dx = newcx - oldcx,
  816. dy = newcy - oldcy,
  817. r = Math.sqrt(dx*dx + dy*dy),
  818. theta = Math.atan2(dy, dx) + angle;
  819. newcx = r * Math.cos(theta) + oldcx;
  820. newcy = r * Math.sin(theta) + oldcy;
  821. var list = current_path.pathSegList,
  822. i = list.numberOfItems;
  823. while (i) {
  824. i -= 1;
  825. var seg = list.getItem(i),
  826. type = seg.pathSegType;
  827. if (type == 1) {continue;}
  828. var rvals = getRotVals(seg.x, seg.y),
  829. points = [rvals.x, rvals.y];
  830. if (seg.x1 != null && seg.x2 != null) {
  831. var c_vals1 = getRotVals(seg.x1, seg.y1);
  832. var c_vals2 = getRotVals(seg.x2, seg.y2);
  833. points.splice(points.length, 0, c_vals1.x , c_vals1.y, c_vals2.x, c_vals2.y);
  834. }
  835. svgedit.path.replacePathSeg(type, i, points);
  836. } // loop for each point
  837. box = svgedit.utilities.getBBox(current_path);
  838. // selectedBBoxes[0].x = box.x; selectedBBoxes[0].y = box.y;
  839. // selectedBBoxes[0].width = box.width; selectedBBoxes[0].height = box.height;
  840. // now we must set the new transform to be rotated around the new center
  841. var R_nc = svgroot.createSVGTransform(),
  842. tlist = svgedit.transformlist.getTransformList(current_path);
  843. R_nc.setRotate((angle * 180.0 / Math.PI), newcx, newcy);
  844. tlist.replaceItem(R_nc,0);
  845. };
  846. // ====================================
  847. // Public API starts here
  848. svgedit.path.clearData = function() {
  849. pathData = {};
  850. };
  851. }());