pathseg.js 54 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821
  1. // SVGPathSeg API polyfill
  2. // https://github.com/progers/pathseg
  3. //
  4. // This is a drop-in replacement for the SVGPathSeg and SVGPathSegList APIs that were removed from
  5. // SVG2 (https://lists.w3.org/Archives/Public/www-svg/2015Jun/0044.html), including the latest spec
  6. // changes which were implemented in Firefox 43 and Chrome 46.
  7. //
  8. // Example API usage:
  9. // var path = document.createElementNS("http://www.w3.org/2000/svg", "path");
  10. // var moveToSeg = path.createSVGPathSegMovetoRel(10, 10);
  11. // var lineToSeg = path.createSVGPathSegLinetoRel(100, 100);
  12. // path.pathSegList.appendItem(moveToSeg);
  13. // path.pathSegList.appendItem(lineToSeg);
  14. // console.log(path.getAttribute('d')); // m 10 10 l 100 100
  15. // moveToSeg.x += 200;
  16. // moveToSeg.y += 200;
  17. // console.log(path.getAttribute('d')); // m 210 210 l 100 100
  18. (function() { "use strict";
  19. if (!window.SVGPathSeg) {
  20. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSeg
  21. window.SVGPathSeg = function(type, typeAsLetter, owningPathSegList) {
  22. this.pathSegType = type;
  23. this.pathSegTypeAsLetter = typeAsLetter;
  24. this._owningPathSegList = owningPathSegList;
  25. }
  26. SVGPathSeg.PATHSEG_UNKNOWN = 0;
  27. SVGPathSeg.PATHSEG_CLOSEPATH = 1;
  28. SVGPathSeg.PATHSEG_MOVETO_ABS = 2;
  29. SVGPathSeg.PATHSEG_MOVETO_REL = 3;
  30. SVGPathSeg.PATHSEG_LINETO_ABS = 4;
  31. SVGPathSeg.PATHSEG_LINETO_REL = 5;
  32. SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS = 6;
  33. SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL = 7;
  34. SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS = 8;
  35. SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL = 9;
  36. SVGPathSeg.PATHSEG_ARC_ABS = 10;
  37. SVGPathSeg.PATHSEG_ARC_REL = 11;
  38. SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS = 12;
  39. SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL = 13;
  40. SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS = 14;
  41. SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL = 15;
  42. SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS = 16;
  43. SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL = 17;
  44. SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS = 18;
  45. SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL = 19;
  46. // Notify owning PathSegList on any changes so they can be synchronized back to the path element.
  47. SVGPathSeg.prototype._segmentChanged = function() {
  48. if (this._owningPathSegList)
  49. this._owningPathSegList.segmentChanged(this);
  50. }
  51. window.SVGPathSegClosePath = function(owningPathSegList) {
  52. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CLOSEPATH, "z", owningPathSegList);
  53. }
  54. SVGPathSegClosePath.prototype = Object.create(SVGPathSeg.prototype);
  55. SVGPathSegClosePath.prototype.toString = function() { return "[object SVGPathSegClosePath]"; }
  56. SVGPathSegClosePath.prototype._asPathString = function() { return this.pathSegTypeAsLetter; }
  57. SVGPathSegClosePath.prototype.clone = function() { return new SVGPathSegClosePath(undefined); }
  58. window.SVGPathSegMovetoAbs = function(owningPathSegList, x, y) {
  59. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_MOVETO_ABS, "M", owningPathSegList);
  60. this._x = x;
  61. this._y = y;
  62. }
  63. SVGPathSegMovetoAbs.prototype = Object.create(SVGPathSeg.prototype);
  64. SVGPathSegMovetoAbs.prototype.toString = function() { return "[object SVGPathSegMovetoAbs]"; }
  65. SVGPathSegMovetoAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  66. SVGPathSegMovetoAbs.prototype.clone = function() { return new SVGPathSegMovetoAbs(undefined, this._x, this._y); }
  67. Object.defineProperty(SVGPathSegMovetoAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  68. Object.defineProperty(SVGPathSegMovetoAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  69. window.SVGPathSegMovetoRel = function(owningPathSegList, x, y) {
  70. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_MOVETO_REL, "m", owningPathSegList);
  71. this._x = x;
  72. this._y = y;
  73. }
  74. SVGPathSegMovetoRel.prototype = Object.create(SVGPathSeg.prototype);
  75. SVGPathSegMovetoRel.prototype.toString = function() { return "[object SVGPathSegMovetoRel]"; }
  76. SVGPathSegMovetoRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  77. SVGPathSegMovetoRel.prototype.clone = function() { return new SVGPathSegMovetoRel(undefined, this._x, this._y); }
  78. Object.defineProperty(SVGPathSegMovetoRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  79. Object.defineProperty(SVGPathSegMovetoRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  80. window.SVGPathSegLinetoAbs = function(owningPathSegList, x, y) {
  81. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_LINETO_ABS, "L", owningPathSegList);
  82. this._x = x;
  83. this._y = y;
  84. }
  85. SVGPathSegLinetoAbs.prototype = Object.create(SVGPathSeg.prototype);
  86. SVGPathSegLinetoAbs.prototype.toString = function() { return "[object SVGPathSegLinetoAbs]"; }
  87. SVGPathSegLinetoAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  88. SVGPathSegLinetoAbs.prototype.clone = function() { return new SVGPathSegLinetoAbs(undefined, this._x, this._y); }
  89. Object.defineProperty(SVGPathSegLinetoAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  90. Object.defineProperty(SVGPathSegLinetoAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  91. window.SVGPathSegLinetoRel = function(owningPathSegList, x, y) {
  92. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_LINETO_REL, "l", owningPathSegList);
  93. this._x = x;
  94. this._y = y;
  95. }
  96. SVGPathSegLinetoRel.prototype = Object.create(SVGPathSeg.prototype);
  97. SVGPathSegLinetoRel.prototype.toString = function() { return "[object SVGPathSegLinetoRel]"; }
  98. SVGPathSegLinetoRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  99. SVGPathSegLinetoRel.prototype.clone = function() { return new SVGPathSegLinetoRel(undefined, this._x, this._y); }
  100. Object.defineProperty(SVGPathSegLinetoRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  101. Object.defineProperty(SVGPathSegLinetoRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  102. window.SVGPathSegCurvetoCubicAbs = function(owningPathSegList, x, y, x1, y1, x2, y2) {
  103. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS, "C", owningPathSegList);
  104. this._x = x;
  105. this._y = y;
  106. this._x1 = x1;
  107. this._y1 = y1;
  108. this._x2 = x2;
  109. this._y2 = y2;
  110. }
  111. SVGPathSegCurvetoCubicAbs.prototype = Object.create(SVGPathSeg.prototype);
  112. SVGPathSegCurvetoCubicAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicAbs]"; }
  113. SVGPathSegCurvetoCubicAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  114. SVGPathSegCurvetoCubicAbs.prototype.clone = function() { return new SVGPathSegCurvetoCubicAbs(undefined, this._x, this._y, this._x1, this._y1, this._x2, this._y2); }
  115. Object.defineProperty(SVGPathSegCurvetoCubicAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  116. Object.defineProperty(SVGPathSegCurvetoCubicAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  117. Object.defineProperty(SVGPathSegCurvetoCubicAbs.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  118. Object.defineProperty(SVGPathSegCurvetoCubicAbs.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  119. Object.defineProperty(SVGPathSegCurvetoCubicAbs.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  120. Object.defineProperty(SVGPathSegCurvetoCubicAbs.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  121. window.SVGPathSegCurvetoCubicRel = function(owningPathSegList, x, y, x1, y1, x2, y2) {
  122. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL, "c", owningPathSegList);
  123. this._x = x;
  124. this._y = y;
  125. this._x1 = x1;
  126. this._y1 = y1;
  127. this._x2 = x2;
  128. this._y2 = y2;
  129. }
  130. SVGPathSegCurvetoCubicRel.prototype = Object.create(SVGPathSeg.prototype);
  131. SVGPathSegCurvetoCubicRel.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicRel]"; }
  132. SVGPathSegCurvetoCubicRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  133. SVGPathSegCurvetoCubicRel.prototype.clone = function() { return new SVGPathSegCurvetoCubicRel(undefined, this._x, this._y, this._x1, this._y1, this._x2, this._y2); }
  134. Object.defineProperty(SVGPathSegCurvetoCubicRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  135. Object.defineProperty(SVGPathSegCurvetoCubicRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  136. Object.defineProperty(SVGPathSegCurvetoCubicRel.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  137. Object.defineProperty(SVGPathSegCurvetoCubicRel.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  138. Object.defineProperty(SVGPathSegCurvetoCubicRel.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  139. Object.defineProperty(SVGPathSegCurvetoCubicRel.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  140. window.SVGPathSegCurvetoQuadraticAbs = function(owningPathSegList, x, y, x1, y1) {
  141. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS, "Q", owningPathSegList);
  142. this._x = x;
  143. this._y = y;
  144. this._x1 = x1;
  145. this._y1 = y1;
  146. }
  147. SVGPathSegCurvetoQuadraticAbs.prototype = Object.create(SVGPathSeg.prototype);
  148. SVGPathSegCurvetoQuadraticAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticAbs]"; }
  149. SVGPathSegCurvetoQuadraticAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x + " " + this._y; }
  150. SVGPathSegCurvetoQuadraticAbs.prototype.clone = function() { return new SVGPathSegCurvetoQuadraticAbs(undefined, this._x, this._y, this._x1, this._y1); }
  151. Object.defineProperty(SVGPathSegCurvetoQuadraticAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  152. Object.defineProperty(SVGPathSegCurvetoQuadraticAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  153. Object.defineProperty(SVGPathSegCurvetoQuadraticAbs.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  154. Object.defineProperty(SVGPathSegCurvetoQuadraticAbs.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  155. window.SVGPathSegCurvetoQuadraticRel = function(owningPathSegList, x, y, x1, y1) {
  156. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL, "q", owningPathSegList);
  157. this._x = x;
  158. this._y = y;
  159. this._x1 = x1;
  160. this._y1 = y1;
  161. }
  162. SVGPathSegCurvetoQuadraticRel.prototype = Object.create(SVGPathSeg.prototype);
  163. SVGPathSegCurvetoQuadraticRel.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticRel]"; }
  164. SVGPathSegCurvetoQuadraticRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x1 + " " + this._y1 + " " + this._x + " " + this._y; }
  165. SVGPathSegCurvetoQuadraticRel.prototype.clone = function() { return new SVGPathSegCurvetoQuadraticRel(undefined, this._x, this._y, this._x1, this._y1); }
  166. Object.defineProperty(SVGPathSegCurvetoQuadraticRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  167. Object.defineProperty(SVGPathSegCurvetoQuadraticRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  168. Object.defineProperty(SVGPathSegCurvetoQuadraticRel.prototype, "x1", { get: function() { return this._x1; }, set: function(x1) { this._x1 = x1; this._segmentChanged(); }, enumerable: true });
  169. Object.defineProperty(SVGPathSegCurvetoQuadraticRel.prototype, "y1", { get: function() { return this._y1; }, set: function(y1) { this._y1 = y1; this._segmentChanged(); }, enumerable: true });
  170. window.SVGPathSegArcAbs = function(owningPathSegList, x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  171. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_ARC_ABS, "A", owningPathSegList);
  172. this._x = x;
  173. this._y = y;
  174. this._r1 = r1;
  175. this._r2 = r2;
  176. this._angle = angle;
  177. this._largeArcFlag = largeArcFlag;
  178. this._sweepFlag = sweepFlag;
  179. }
  180. SVGPathSegArcAbs.prototype = Object.create(SVGPathSeg.prototype);
  181. SVGPathSegArcAbs.prototype.toString = function() { return "[object SVGPathSegArcAbs]"; }
  182. SVGPathSegArcAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._r1 + " " + this._r2 + " " + this._angle + " " + (this._largeArcFlag ? "1" : "0") + " " + (this._sweepFlag ? "1" : "0") + " " + this._x + " " + this._y; }
  183. SVGPathSegArcAbs.prototype.clone = function() { return new SVGPathSegArcAbs(undefined, this._x, this._y, this._r1, this._r2, this._angle, this._largeArcFlag, this._sweepFlag); }
  184. Object.defineProperty(SVGPathSegArcAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  185. Object.defineProperty(SVGPathSegArcAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  186. Object.defineProperty(SVGPathSegArcAbs.prototype, "r1", { get: function() { return this._r1; }, set: function(r1) { this._r1 = r1; this._segmentChanged(); }, enumerable: true });
  187. Object.defineProperty(SVGPathSegArcAbs.prototype, "r2", { get: function() { return this._r2; }, set: function(r2) { this._r2 = r2; this._segmentChanged(); }, enumerable: true });
  188. Object.defineProperty(SVGPathSegArcAbs.prototype, "angle", { get: function() { return this._angle; }, set: function(angle) { this._angle = angle; this._segmentChanged(); }, enumerable: true });
  189. Object.defineProperty(SVGPathSegArcAbs.prototype, "largeArcFlag", { get: function() { return this._largeArcFlag; }, set: function(largeArcFlag) { this._largeArcFlag = largeArcFlag; this._segmentChanged(); }, enumerable: true });
  190. Object.defineProperty(SVGPathSegArcAbs.prototype, "sweepFlag", { get: function() { return this._sweepFlag; }, set: function(sweepFlag) { this._sweepFlag = sweepFlag; this._segmentChanged(); }, enumerable: true });
  191. window.SVGPathSegArcRel = function(owningPathSegList, x, y, r1, r2, angle, largeArcFlag, sweepFlag) {
  192. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_ARC_REL, "a", owningPathSegList);
  193. this._x = x;
  194. this._y = y;
  195. this._r1 = r1;
  196. this._r2 = r2;
  197. this._angle = angle;
  198. this._largeArcFlag = largeArcFlag;
  199. this._sweepFlag = sweepFlag;
  200. }
  201. SVGPathSegArcRel.prototype = Object.create(SVGPathSeg.prototype);
  202. SVGPathSegArcRel.prototype.toString = function() { return "[object SVGPathSegArcRel]"; }
  203. SVGPathSegArcRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._r1 + " " + this._r2 + " " + this._angle + " " + (this._largeArcFlag ? "1" : "0") + " " + (this._sweepFlag ? "1" : "0") + " " + this._x + " " + this._y; }
  204. SVGPathSegArcRel.prototype.clone = function() { return new SVGPathSegArcRel(undefined, this._x, this._y, this._r1, this._r2, this._angle, this._largeArcFlag, this._sweepFlag); }
  205. Object.defineProperty(SVGPathSegArcRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  206. Object.defineProperty(SVGPathSegArcRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  207. Object.defineProperty(SVGPathSegArcRel.prototype, "r1", { get: function() { return this._r1; }, set: function(r1) { this._r1 = r1; this._segmentChanged(); }, enumerable: true });
  208. Object.defineProperty(SVGPathSegArcRel.prototype, "r2", { get: function() { return this._r2; }, set: function(r2) { this._r2 = r2; this._segmentChanged(); }, enumerable: true });
  209. Object.defineProperty(SVGPathSegArcRel.prototype, "angle", { get: function() { return this._angle; }, set: function(angle) { this._angle = angle; this._segmentChanged(); }, enumerable: true });
  210. Object.defineProperty(SVGPathSegArcRel.prototype, "largeArcFlag", { get: function() { return this._largeArcFlag; }, set: function(largeArcFlag) { this._largeArcFlag = largeArcFlag; this._segmentChanged(); }, enumerable: true });
  211. Object.defineProperty(SVGPathSegArcRel.prototype, "sweepFlag", { get: function() { return this._sweepFlag; }, set: function(sweepFlag) { this._sweepFlag = sweepFlag; this._segmentChanged(); }, enumerable: true });
  212. window.SVGPathSegLinetoHorizontalAbs = function(owningPathSegList, x) {
  213. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS, "H", owningPathSegList);
  214. this._x = x;
  215. }
  216. SVGPathSegLinetoHorizontalAbs.prototype = Object.create(SVGPathSeg.prototype);
  217. SVGPathSegLinetoHorizontalAbs.prototype.toString = function() { return "[object SVGPathSegLinetoHorizontalAbs]"; }
  218. SVGPathSegLinetoHorizontalAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x; }
  219. SVGPathSegLinetoHorizontalAbs.prototype.clone = function() { return new SVGPathSegLinetoHorizontalAbs(undefined, this._x); }
  220. Object.defineProperty(SVGPathSegLinetoHorizontalAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  221. window.SVGPathSegLinetoHorizontalRel = function(owningPathSegList, x) {
  222. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL, "h", owningPathSegList);
  223. this._x = x;
  224. }
  225. SVGPathSegLinetoHorizontalRel.prototype = Object.create(SVGPathSeg.prototype);
  226. SVGPathSegLinetoHorizontalRel.prototype.toString = function() { return "[object SVGPathSegLinetoHorizontalRel]"; }
  227. SVGPathSegLinetoHorizontalRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x; }
  228. SVGPathSegLinetoHorizontalRel.prototype.clone = function() { return new SVGPathSegLinetoHorizontalRel(undefined, this._x); }
  229. Object.defineProperty(SVGPathSegLinetoHorizontalRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  230. window.SVGPathSegLinetoVerticalAbs = function(owningPathSegList, y) {
  231. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS, "V", owningPathSegList);
  232. this._y = y;
  233. }
  234. SVGPathSegLinetoVerticalAbs.prototype = Object.create(SVGPathSeg.prototype);
  235. SVGPathSegLinetoVerticalAbs.prototype.toString = function() { return "[object SVGPathSegLinetoVerticalAbs]"; }
  236. SVGPathSegLinetoVerticalAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._y; }
  237. SVGPathSegLinetoVerticalAbs.prototype.clone = function() { return new SVGPathSegLinetoVerticalAbs(undefined, this._y); }
  238. Object.defineProperty(SVGPathSegLinetoVerticalAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  239. window.SVGPathSegLinetoVerticalRel = function(owningPathSegList, y) {
  240. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL, "v", owningPathSegList);
  241. this._y = y;
  242. }
  243. SVGPathSegLinetoVerticalRel.prototype = Object.create(SVGPathSeg.prototype);
  244. SVGPathSegLinetoVerticalRel.prototype.toString = function() { return "[object SVGPathSegLinetoVerticalRel]"; }
  245. SVGPathSegLinetoVerticalRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._y; }
  246. SVGPathSegLinetoVerticalRel.prototype.clone = function() { return new SVGPathSegLinetoVerticalRel(undefined, this._y); }
  247. Object.defineProperty(SVGPathSegLinetoVerticalRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  248. window.SVGPathSegCurvetoCubicSmoothAbs = function(owningPathSegList, x, y, x2, y2) {
  249. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS, "S", owningPathSegList);
  250. this._x = x;
  251. this._y = y;
  252. this._x2 = x2;
  253. this._y2 = y2;
  254. }
  255. SVGPathSegCurvetoCubicSmoothAbs.prototype = Object.create(SVGPathSeg.prototype);
  256. SVGPathSegCurvetoCubicSmoothAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicSmoothAbs]"; }
  257. SVGPathSegCurvetoCubicSmoothAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  258. SVGPathSegCurvetoCubicSmoothAbs.prototype.clone = function() { return new SVGPathSegCurvetoCubicSmoothAbs(undefined, this._x, this._y, this._x2, this._y2); }
  259. Object.defineProperty(SVGPathSegCurvetoCubicSmoothAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  260. Object.defineProperty(SVGPathSegCurvetoCubicSmoothAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  261. Object.defineProperty(SVGPathSegCurvetoCubicSmoothAbs.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  262. Object.defineProperty(SVGPathSegCurvetoCubicSmoothAbs.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  263. window.SVGPathSegCurvetoCubicSmoothRel = function(owningPathSegList, x, y, x2, y2) {
  264. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL, "s", owningPathSegList);
  265. this._x = x;
  266. this._y = y;
  267. this._x2 = x2;
  268. this._y2 = y2;
  269. }
  270. SVGPathSegCurvetoCubicSmoothRel.prototype = Object.create(SVGPathSeg.prototype);
  271. SVGPathSegCurvetoCubicSmoothRel.prototype.toString = function() { return "[object SVGPathSegCurvetoCubicSmoothRel]"; }
  272. SVGPathSegCurvetoCubicSmoothRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x2 + " " + this._y2 + " " + this._x + " " + this._y; }
  273. SVGPathSegCurvetoCubicSmoothRel.prototype.clone = function() { return new SVGPathSegCurvetoCubicSmoothRel(undefined, this._x, this._y, this._x2, this._y2); }
  274. Object.defineProperty(SVGPathSegCurvetoCubicSmoothRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  275. Object.defineProperty(SVGPathSegCurvetoCubicSmoothRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  276. Object.defineProperty(SVGPathSegCurvetoCubicSmoothRel.prototype, "x2", { get: function() { return this._x2; }, set: function(x2) { this._x2 = x2; this._segmentChanged(); }, enumerable: true });
  277. Object.defineProperty(SVGPathSegCurvetoCubicSmoothRel.prototype, "y2", { get: function() { return this._y2; }, set: function(y2) { this._y2 = y2; this._segmentChanged(); }, enumerable: true });
  278. window.SVGPathSegCurvetoQuadraticSmoothAbs = function(owningPathSegList, x, y) {
  279. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS, "T", owningPathSegList);
  280. this._x = x;
  281. this._y = y;
  282. }
  283. SVGPathSegCurvetoQuadraticSmoothAbs.prototype = Object.create(SVGPathSeg.prototype);
  284. SVGPathSegCurvetoQuadraticSmoothAbs.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticSmoothAbs]"; }
  285. SVGPathSegCurvetoQuadraticSmoothAbs.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  286. SVGPathSegCurvetoQuadraticSmoothAbs.prototype.clone = function() { return new SVGPathSegCurvetoQuadraticSmoothAbs(undefined, this._x, this._y); }
  287. Object.defineProperty(SVGPathSegCurvetoQuadraticSmoothAbs.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  288. Object.defineProperty(SVGPathSegCurvetoQuadraticSmoothAbs.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  289. window.SVGPathSegCurvetoQuadraticSmoothRel = function(owningPathSegList, x, y) {
  290. SVGPathSeg.call(this, SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL, "t", owningPathSegList);
  291. this._x = x;
  292. this._y = y;
  293. }
  294. SVGPathSegCurvetoQuadraticSmoothRel.prototype = Object.create(SVGPathSeg.prototype);
  295. SVGPathSegCurvetoQuadraticSmoothRel.prototype.toString = function() { return "[object SVGPathSegCurvetoQuadraticSmoothRel]"; }
  296. SVGPathSegCurvetoQuadraticSmoothRel.prototype._asPathString = function() { return this.pathSegTypeAsLetter + " " + this._x + " " + this._y; }
  297. SVGPathSegCurvetoQuadraticSmoothRel.prototype.clone = function() { return new SVGPathSegCurvetoQuadraticSmoothRel(undefined, this._x, this._y); }
  298. Object.defineProperty(SVGPathSegCurvetoQuadraticSmoothRel.prototype, "x", { get: function() { return this._x; }, set: function(x) { this._x = x; this._segmentChanged(); }, enumerable: true });
  299. Object.defineProperty(SVGPathSegCurvetoQuadraticSmoothRel.prototype, "y", { get: function() { return this._y; }, set: function(y) { this._y = y; this._segmentChanged(); }, enumerable: true });
  300. // Add createSVGPathSeg* functions to SVGPathElement.
  301. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathElement.
  302. SVGPathElement.prototype.createSVGPathSegClosePath = function() { return new SVGPathSegClosePath(undefined); }
  303. SVGPathElement.prototype.createSVGPathSegMovetoAbs = function(x, y) { return new SVGPathSegMovetoAbs(undefined, x, y); }
  304. SVGPathElement.prototype.createSVGPathSegMovetoRel = function(x, y) { return new SVGPathSegMovetoRel(undefined, x, y); }
  305. SVGPathElement.prototype.createSVGPathSegLinetoAbs = function(x, y) { return new SVGPathSegLinetoAbs(undefined, x, y); }
  306. SVGPathElement.prototype.createSVGPathSegLinetoRel = function(x, y) { return new SVGPathSegLinetoRel(undefined, x, y); }
  307. SVGPathElement.prototype.createSVGPathSegCurvetoCubicAbs = function(x, y, x1, y1, x2, y2) { return new SVGPathSegCurvetoCubicAbs(undefined, x, y, x1, y1, x2, y2); }
  308. SVGPathElement.prototype.createSVGPathSegCurvetoCubicRel = function(x, y, x1, y1, x2, y2) { return new SVGPathSegCurvetoCubicRel(undefined, x, y, x1, y1, x2, y2); }
  309. SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticAbs = function(x, y, x1, y1) { return new SVGPathSegCurvetoQuadraticAbs(undefined, x, y, x1, y1); }
  310. SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticRel = function(x, y, x1, y1) { return new SVGPathSegCurvetoQuadraticRel(undefined, x, y, x1, y1); }
  311. SVGPathElement.prototype.createSVGPathSegArcAbs = function(x, y, r1, r2, angle, largeArcFlag, sweepFlag) { return new SVGPathSegArcAbs(undefined, x, y, r1, r2, angle, largeArcFlag, sweepFlag); }
  312. SVGPathElement.prototype.createSVGPathSegArcRel = function(x, y, r1, r2, angle, largeArcFlag, sweepFlag) { return new SVGPathSegArcRel(undefined, x, y, r1, r2, angle, largeArcFlag, sweepFlag); }
  313. SVGPathElement.prototype.createSVGPathSegLinetoHorizontalAbs = function(x) { return new SVGPathSegLinetoHorizontalAbs(undefined, x); }
  314. SVGPathElement.prototype.createSVGPathSegLinetoHorizontalRel = function(x) { return new SVGPathSegLinetoHorizontalRel(undefined, x); }
  315. SVGPathElement.prototype.createSVGPathSegLinetoVerticalAbs = function(y) { return new SVGPathSegLinetoVerticalAbs(undefined, y); }
  316. SVGPathElement.prototype.createSVGPathSegLinetoVerticalRel = function(y) { return new SVGPathSegLinetoVerticalRel(undefined, y); }
  317. SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothAbs = function(x, y, x2, y2) { return new SVGPathSegCurvetoCubicSmoothAbs(undefined, x, y, x2, y2); }
  318. SVGPathElement.prototype.createSVGPathSegCurvetoCubicSmoothRel = function(x, y, x2, y2) { return new SVGPathSegCurvetoCubicSmoothRel(undefined, x, y, x2, y2); }
  319. SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothAbs = function(x, y) { return new SVGPathSegCurvetoQuadraticSmoothAbs(undefined, x, y); }
  320. SVGPathElement.prototype.createSVGPathSegCurvetoQuadraticSmoothRel = function(x, y) { return new SVGPathSegCurvetoQuadraticSmoothRel(undefined, x, y); }
  321. }
  322. if (!window.SVGPathSegList) {
  323. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGPathSegList
  324. window.SVGPathSegList = function(pathElement) {
  325. this._pathElement = pathElement;
  326. this._list = this._parsePath(this._pathElement.getAttribute("d"));
  327. // Use a MutationObserver to catch changes to the path's "d" attribute.
  328. this._mutationObserverConfig = { "attributes": true, "attributeFilter": ["d"] };
  329. this._pathElementMutationObserver = new MutationObserver(this._updateListFromPathMutations.bind(this));
  330. this._pathElementMutationObserver.observe(this._pathElement, this._mutationObserverConfig);
  331. }
  332. Object.defineProperty(SVGPathSegList.prototype, "numberOfItems", {
  333. get: function() {
  334. this._checkPathSynchronizedToList();
  335. return this._list.length;
  336. }
  337. });
  338. // Process any pending mutations to the path element and update the list as needed.
  339. // This should be the first call of all public functions and is needed because
  340. // MutationObservers are not synchronous so we can have pending asynchronous mutations.
  341. SVGPathSegList.prototype._checkPathSynchronizedToList = function() {
  342. this._updateListFromPathMutations(this._pathElementMutationObserver.takeRecords());
  343. }
  344. SVGPathSegList.prototype._updateListFromPathMutations = function(mutationRecords) {
  345. if (!this._pathElement)
  346. return;
  347. var hasPathMutations = false;
  348. mutationRecords.forEach(function(record) {
  349. if (record.attributeName == "d")
  350. hasPathMutations = true;
  351. });
  352. if (hasPathMutations)
  353. this._list = this._parsePath(this._pathElement.getAttribute("d"));
  354. }
  355. // Serialize the list and update the path's 'd' attribute.
  356. SVGPathSegList.prototype._writeListToPath = function() {
  357. this._pathElementMutationObserver.disconnect();
  358. this._pathElement.setAttribute("d", SVGPathSegList._pathSegArrayAsString(this._list));
  359. this._pathElementMutationObserver.observe(this._pathElement, this._mutationObserverConfig);
  360. }
  361. // When a path segment changes the list needs to be synchronized back to the path element.
  362. SVGPathSegList.prototype.segmentChanged = function(pathSeg) {
  363. this._writeListToPath();
  364. }
  365. SVGPathSegList.prototype.clear = function() {
  366. this._checkPathSynchronizedToList();
  367. this._list.forEach(function(pathSeg) {
  368. pathSeg._owningPathSegList = null;
  369. });
  370. this._list = [];
  371. this._writeListToPath();
  372. }
  373. SVGPathSegList.prototype.initialize = function(newItem) {
  374. this._checkPathSynchronizedToList();
  375. this._list = [newItem];
  376. newItem._owningPathSegList = this;
  377. this._writeListToPath();
  378. return newItem;
  379. }
  380. SVGPathSegList.prototype._checkValidIndex = function(index) {
  381. if (isNaN(index) || index < 0 || index >= this.numberOfItems)
  382. throw "INDEX_SIZE_ERR";
  383. }
  384. SVGPathSegList.prototype.getItem = function(index) {
  385. this._checkPathSynchronizedToList();
  386. this._checkValidIndex(index);
  387. return this._list[index];
  388. }
  389. SVGPathSegList.prototype.insertItemBefore = function(newItem, index) {
  390. this._checkPathSynchronizedToList();
  391. // Spec: If the index is greater than or equal to numberOfItems, then the new item is appended to the end of the list.
  392. if (index > this.numberOfItems)
  393. index = this.numberOfItems;
  394. if (newItem._owningPathSegList) {
  395. // SVG2 spec says to make a copy.
  396. newItem = newItem.clone();
  397. }
  398. this._list.splice(index, 0, newItem);
  399. newItem._owningPathSegList = this;
  400. this._writeListToPath();
  401. return newItem;
  402. }
  403. SVGPathSegList.prototype.replaceItem = function(newItem, index) {
  404. this._checkPathSynchronizedToList();
  405. if (newItem._owningPathSegList) {
  406. // SVG2 spec says to make a copy.
  407. newItem = newItem.clone();
  408. }
  409. this._checkValidIndex(index);
  410. this._list[index] = newItem;
  411. newItem._owningPathSegList = this;
  412. this._writeListToPath();
  413. return newItem;
  414. }
  415. SVGPathSegList.prototype.removeItem = function(index) {
  416. this._checkPathSynchronizedToList();
  417. this._checkValidIndex(index);
  418. var item = this._list[index];
  419. this._list.splice(index, 1);
  420. this._writeListToPath();
  421. return item;
  422. }
  423. SVGPathSegList.prototype.appendItem = function(newItem) {
  424. this._checkPathSynchronizedToList();
  425. if (newItem._owningPathSegList) {
  426. // SVG2 spec says to make a copy.
  427. newItem = newItem.clone();
  428. }
  429. this._list.push(newItem);
  430. newItem._owningPathSegList = this;
  431. // TODO: Optimize this to just append to the existing attribute.
  432. this._writeListToPath();
  433. return newItem;
  434. }
  435. SVGPathSegList._pathSegArrayAsString = function(pathSegArray) {
  436. var string = "";
  437. var first = true;
  438. pathSegArray.forEach(function(pathSeg) {
  439. if (first) {
  440. first = false;
  441. string += pathSeg._asPathString();
  442. } else {
  443. string += " " + pathSeg._asPathString();
  444. }
  445. });
  446. return string;
  447. }
  448. // This closely follows SVGPathParser::parsePath from Source/core/svg/SVGPathParser.cpp.
  449. SVGPathSegList.prototype._parsePath = function(string) {
  450. if (!string || string.length == 0)
  451. return [];
  452. var owningPathSegList = this;
  453. var Builder = function() {
  454. this.pathSegList = [];
  455. }
  456. Builder.prototype.appendSegment = function(pathSeg) {
  457. this.pathSegList.push(pathSeg);
  458. }
  459. var Source = function(string) {
  460. this._string = string;
  461. this._currentIndex = 0;
  462. this._endIndex = this._string.length;
  463. this._previousCommand = SVGPathSeg.PATHSEG_UNKNOWN;
  464. this._skipOptionalSpaces();
  465. }
  466. Source.prototype._isCurrentSpace = function() {
  467. var character = this._string[this._currentIndex];
  468. return character <= " " && (character == " " || character == "\n" || character == "\t" || character == "\r" || character == "\f");
  469. }
  470. Source.prototype._skipOptionalSpaces = function() {
  471. while (this._currentIndex < this._endIndex && this._isCurrentSpace())
  472. this._currentIndex++;
  473. return this._currentIndex < this._endIndex;
  474. }
  475. Source.prototype._skipOptionalSpacesOrDelimiter = function() {
  476. if (this._currentIndex < this._endIndex && !this._isCurrentSpace() && this._string.charAt(this._currentIndex) != ",")
  477. return false;
  478. if (this._skipOptionalSpaces()) {
  479. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == ",") {
  480. this._currentIndex++;
  481. this._skipOptionalSpaces();
  482. }
  483. }
  484. return this._currentIndex < this._endIndex;
  485. }
  486. Source.prototype.hasMoreData = function() {
  487. return this._currentIndex < this._endIndex;
  488. }
  489. Source.prototype.peekSegmentType = function() {
  490. var lookahead = this._string[this._currentIndex];
  491. return this._pathSegTypeFromChar(lookahead);
  492. }
  493. Source.prototype._pathSegTypeFromChar = function(lookahead) {
  494. switch (lookahead) {
  495. case "Z":
  496. case "z":
  497. return SVGPathSeg.PATHSEG_CLOSEPATH;
  498. case "M":
  499. return SVGPathSeg.PATHSEG_MOVETO_ABS;
  500. case "m":
  501. return SVGPathSeg.PATHSEG_MOVETO_REL;
  502. case "L":
  503. return SVGPathSeg.PATHSEG_LINETO_ABS;
  504. case "l":
  505. return SVGPathSeg.PATHSEG_LINETO_REL;
  506. case "C":
  507. return SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS;
  508. case "c":
  509. return SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL;
  510. case "Q":
  511. return SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS;
  512. case "q":
  513. return SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL;
  514. case "A":
  515. return SVGPathSeg.PATHSEG_ARC_ABS;
  516. case "a":
  517. return SVGPathSeg.PATHSEG_ARC_REL;
  518. case "H":
  519. return SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS;
  520. case "h":
  521. return SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL;
  522. case "V":
  523. return SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS;
  524. case "v":
  525. return SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL;
  526. case "S":
  527. return SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS;
  528. case "s":
  529. return SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL;
  530. case "T":
  531. return SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS;
  532. case "t":
  533. return SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL;
  534. default:
  535. return SVGPathSeg.PATHSEG_UNKNOWN;
  536. }
  537. }
  538. Source.prototype._nextCommandHelper = function(lookahead, previousCommand) {
  539. // Check for remaining coordinates in the current command.
  540. if ((lookahead == "+" || lookahead == "-" || lookahead == "." || (lookahead >= "0" && lookahead <= "9")) && previousCommand != SVGPathSeg.PATHSEG_CLOSEPATH) {
  541. if (previousCommand == SVGPathSeg.PATHSEG_MOVETO_ABS)
  542. return SVGPathSeg.PATHSEG_LINETO_ABS;
  543. if (previousCommand == SVGPathSeg.PATHSEG_MOVETO_REL)
  544. return SVGPathSeg.PATHSEG_LINETO_REL;
  545. return previousCommand;
  546. }
  547. return SVGPathSeg.PATHSEG_UNKNOWN;
  548. }
  549. Source.prototype.initialCommandIsMoveTo = function() {
  550. // If the path is empty it is still valid, so return true.
  551. if (!this.hasMoreData())
  552. return true;
  553. var command = this.peekSegmentType();
  554. // Path must start with moveTo.
  555. return command == SVGPathSeg.PATHSEG_MOVETO_ABS || command == SVGPathSeg.PATHSEG_MOVETO_REL;
  556. }
  557. // Parse a number from an SVG path. This very closely follows genericParseNumber(...) from Source/core/svg/SVGParserUtilities.cpp.
  558. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-PathDataBNF
  559. Source.prototype._parseNumber = function() {
  560. var exponent = 0;
  561. var integer = 0;
  562. var frac = 1;
  563. var decimal = 0;
  564. var sign = 1;
  565. var expsign = 1;
  566. var startIndex = this._currentIndex;
  567. this._skipOptionalSpaces();
  568. // Read the sign.
  569. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == "+")
  570. this._currentIndex++;
  571. else if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == "-") {
  572. this._currentIndex++;
  573. sign = -1;
  574. }
  575. if (this._currentIndex == this._endIndex || ((this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9") && this._string.charAt(this._currentIndex) != "."))
  576. // The first character of a number must be one of [0-9+-.].
  577. return undefined;
  578. // Read the integer part, build right-to-left.
  579. var startIntPartIndex = this._currentIndex;
  580. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9")
  581. this._currentIndex++; // Advance to first non-digit.
  582. if (this._currentIndex != startIntPartIndex) {
  583. var scanIntPartIndex = this._currentIndex - 1;
  584. var multiplier = 1;
  585. while (scanIntPartIndex >= startIntPartIndex) {
  586. integer += multiplier * (this._string.charAt(scanIntPartIndex--) - "0");
  587. multiplier *= 10;
  588. }
  589. }
  590. // Read the decimals.
  591. if (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) == ".") {
  592. this._currentIndex++;
  593. // There must be a least one digit following the .
  594. if (this._currentIndex >= this._endIndex || this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9")
  595. return undefined;
  596. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9")
  597. decimal += (this._string.charAt(this._currentIndex++) - "0") * (frac *= 0.1);
  598. }
  599. // Read the exponent part.
  600. if (this._currentIndex != startIndex && this._currentIndex + 1 < this._endIndex && (this._string.charAt(this._currentIndex) == "e" || this._string.charAt(this._currentIndex) == "E") && (this._string.charAt(this._currentIndex + 1) != "x" && this._string.charAt(this._currentIndex + 1) != "m")) {
  601. this._currentIndex++;
  602. // Read the sign of the exponent.
  603. if (this._string.charAt(this._currentIndex) == "+") {
  604. this._currentIndex++;
  605. } else if (this._string.charAt(this._currentIndex) == "-") {
  606. this._currentIndex++;
  607. expsign = -1;
  608. }
  609. // There must be an exponent.
  610. if (this._currentIndex >= this._endIndex || this._string.charAt(this._currentIndex) < "0" || this._string.charAt(this._currentIndex) > "9")
  611. return undefined;
  612. while (this._currentIndex < this._endIndex && this._string.charAt(this._currentIndex) >= "0" && this._string.charAt(this._currentIndex) <= "9") {
  613. exponent *= 10;
  614. exponent += (this._string.charAt(this._currentIndex) - "0");
  615. this._currentIndex++;
  616. }
  617. }
  618. var number = integer + decimal;
  619. number *= sign;
  620. if (exponent)
  621. number *= Math.pow(10, expsign * exponent);
  622. if (startIndex == this._currentIndex)
  623. return undefined;
  624. this._skipOptionalSpacesOrDelimiter();
  625. return number;
  626. }
  627. Source.prototype._parseArcFlag = function() {
  628. if (this._currentIndex >= this._endIndex)
  629. return undefined;
  630. var flag = false;
  631. var flagChar = this._string.charAt(this._currentIndex++);
  632. if (flagChar == "0")
  633. flag = false;
  634. else if (flagChar == "1")
  635. flag = true;
  636. else
  637. return undefined;
  638. this._skipOptionalSpacesOrDelimiter();
  639. return flag;
  640. }
  641. Source.prototype.parseSegment = function() {
  642. var lookahead = this._string[this._currentIndex];
  643. var command = this._pathSegTypeFromChar(lookahead);
  644. if (command == SVGPathSeg.PATHSEG_UNKNOWN) {
  645. // Possibly an implicit command. Not allowed if this is the first command.
  646. if (this._previousCommand == SVGPathSeg.PATHSEG_UNKNOWN)
  647. return null;
  648. command = this._nextCommandHelper(lookahead, this._previousCommand);
  649. if (command == SVGPathSeg.PATHSEG_UNKNOWN)
  650. return null;
  651. } else {
  652. this._currentIndex++;
  653. }
  654. this._previousCommand = command;
  655. switch (command) {
  656. case SVGPathSeg.PATHSEG_MOVETO_REL:
  657. return new SVGPathSegMovetoRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  658. case SVGPathSeg.PATHSEG_MOVETO_ABS:
  659. return new SVGPathSegMovetoAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  660. case SVGPathSeg.PATHSEG_LINETO_REL:
  661. return new SVGPathSegLinetoRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  662. case SVGPathSeg.PATHSEG_LINETO_ABS:
  663. return new SVGPathSegLinetoAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  664. case SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_REL:
  665. return new SVGPathSegLinetoHorizontalRel(owningPathSegList, this._parseNumber());
  666. case SVGPathSeg.PATHSEG_LINETO_HORIZONTAL_ABS:
  667. return new SVGPathSegLinetoHorizontalAbs(owningPathSegList, this._parseNumber());
  668. case SVGPathSeg.PATHSEG_LINETO_VERTICAL_REL:
  669. return new SVGPathSegLinetoVerticalRel(owningPathSegList, this._parseNumber());
  670. case SVGPathSeg.PATHSEG_LINETO_VERTICAL_ABS:
  671. return new SVGPathSegLinetoVerticalAbs(owningPathSegList, this._parseNumber());
  672. case SVGPathSeg.PATHSEG_CLOSEPATH:
  673. this._skipOptionalSpaces();
  674. return new SVGPathSegClosePath(owningPathSegList);
  675. case SVGPathSeg.PATHSEG_CURVETO_CUBIC_REL:
  676. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  677. return new SVGPathSegCurvetoCubicRel(owningPathSegList, points.x, points.y, points.x1, points.y1, points.x2, points.y2);
  678. case SVGPathSeg.PATHSEG_CURVETO_CUBIC_ABS:
  679. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  680. return new SVGPathSegCurvetoCubicAbs(owningPathSegList, points.x, points.y, points.x1, points.y1, points.x2, points.y2);
  681. case SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_REL:
  682. var points = {x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  683. return new SVGPathSegCurvetoCubicSmoothRel(owningPathSegList, points.x, points.y, points.x2, points.y2);
  684. case SVGPathSeg.PATHSEG_CURVETO_CUBIC_SMOOTH_ABS:
  685. var points = {x2: this._parseNumber(), y2: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  686. return new SVGPathSegCurvetoCubicSmoothAbs(owningPathSegList, points.x, points.y, points.x2, points.y2);
  687. case SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_REL:
  688. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  689. return new SVGPathSegCurvetoQuadraticRel(owningPathSegList, points.x, points.y, points.x1, points.y1);
  690. case SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_ABS:
  691. var points = {x1: this._parseNumber(), y1: this._parseNumber(), x: this._parseNumber(), y: this._parseNumber()};
  692. return new SVGPathSegCurvetoQuadraticAbs(owningPathSegList, points.x, points.y, points.x1, points.y1);
  693. case SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_REL:
  694. return new SVGPathSegCurvetoQuadraticSmoothRel(owningPathSegList, this._parseNumber(), this._parseNumber());
  695. case SVGPathSeg.PATHSEG_CURVETO_QUADRATIC_SMOOTH_ABS:
  696. return new SVGPathSegCurvetoQuadraticSmoothAbs(owningPathSegList, this._parseNumber(), this._parseNumber());
  697. case SVGPathSeg.PATHSEG_ARC_REL:
  698. var points = {x1: this._parseNumber(), y1: this._parseNumber(), arcAngle: this._parseNumber(), arcLarge: this._parseArcFlag(), arcSweep: this._parseArcFlag(), x: this._parseNumber(), y: this._parseNumber()};
  699. return new SVGPathSegArcRel(owningPathSegList, points.x, points.y, points.x1, points.y1, points.arcAngle, points.arcLarge, points.arcSweep);
  700. case SVGPathSeg.PATHSEG_ARC_ABS:
  701. var points = {x1: this._parseNumber(), y1: this._parseNumber(), arcAngle: this._parseNumber(), arcLarge: this._parseArcFlag(), arcSweep: this._parseArcFlag(), x: this._parseNumber(), y: this._parseNumber()};
  702. return new SVGPathSegArcAbs(owningPathSegList, points.x, points.y, points.x1, points.y1, points.arcAngle, points.arcLarge, points.arcSweep);
  703. default:
  704. throw "Unknown path seg type."
  705. }
  706. }
  707. var builder = new Builder();
  708. var source = new Source(string);
  709. if (!source.initialCommandIsMoveTo())
  710. return [];
  711. while (source.hasMoreData()) {
  712. var pathSeg = source.parseSegment();
  713. if (!pathSeg)
  714. return [];
  715. builder.appendSegment(pathSeg);
  716. }
  717. return builder.pathSegList;
  718. }
  719. // Add the pathSegList accessors to SVGPathElement.
  720. // Spec: http://www.w3.org/TR/SVG11/single-page.html#paths-InterfaceSVGAnimatedPathData
  721. Object.defineProperty(SVGPathElement.prototype, "pathSegList", {
  722. get: function() {
  723. if (!this._pathSegList)
  724. this._pathSegList = new SVGPathSegList(this);
  725. return this._pathSegList;
  726. },
  727. enumerable: true
  728. });
  729. // FIXME: The following are not implemented and simply return SVGPathElement.pathSegList.
  730. Object.defineProperty(SVGPathElement.prototype, "normalizedPathSegList", { get: function() { return this.pathSegList; }, enumerable: true });
  731. Object.defineProperty(SVGPathElement.prototype, "animatedPathSegList", { get: function() { return this.pathSegList; }, enumerable: true });
  732. Object.defineProperty(SVGPathElement.prototype, "animatedNormalizedPathSegList", { get: function() { return this.pathSegList; }, enumerable: true });
  733. }
  734. }());