@antv/util 2.0.15 → 3.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (348) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +15 -241
  3. package/dist/util.min.js +1 -0
  4. package/esm/color/arr2rgb.d.ts +12 -0
  5. package/esm/color/arr2rgb.js +18 -0
  6. package/esm/color/arr2rgb.js.map +1 -0
  7. package/esm/color/gradient.d.ts +6 -0
  8. package/esm/color/gradient.js +45 -0
  9. package/esm/color/gradient.js.map +1 -0
  10. package/esm/color/index.d.ts +4 -0
  11. package/esm/color/index.js +6 -0
  12. package/esm/color/index.js.map +1 -0
  13. package/esm/color/rgb2arr.d.ts +6 -0
  14. package/esm/color/rgb2arr.js +9 -0
  15. package/esm/color/rgb2arr.js.map +1 -0
  16. package/esm/color/tocssgradient.d.ts +1 -0
  17. package/esm/color/tocssgradient.js +37 -0
  18. package/esm/color/tocssgradient.js.map +1 -0
  19. package/esm/color/torgb.d.ts +6 -0
  20. package/esm/color/torgb.js +36 -0
  21. package/esm/color/torgb.js.map +1 -0
  22. package/esm/helper/index.d.ts +2 -0
  23. package/esm/helper/index.js +3 -0
  24. package/esm/helper/index.js.map +1 -0
  25. package/esm/helper/mod.d.ts +1 -0
  26. package/esm/helper/mod.js +4 -0
  27. package/esm/helper/mod.js.map +1 -0
  28. package/esm/helper/to-radian.d.ts +1 -0
  29. package/esm/helper/to-radian.js +5 -0
  30. package/esm/helper/to-radian.js.map +1 -0
  31. package/esm/index.d.ts +3 -0
  32. package/esm/index.js +4 -0
  33. package/esm/index.js.map +1 -0
  34. package/esm/matrix/index.d.ts +27 -0
  35. package/esm/matrix/index.js +97 -0
  36. package/esm/matrix/index.js.map +1 -0
  37. package/esm/path/catmull-rom-2-bezier.d.ts +10 -0
  38. package/esm/path/catmull-rom-2-bezier.js +115 -0
  39. package/esm/path/catmull-rom-2-bezier.js.map +1 -0
  40. package/esm/path/fill-path-by-diff.d.ts +5 -0
  41. package/esm/path/fill-path-by-diff.js +110 -0
  42. package/esm/path/fill-path-by-diff.js.map +1 -0
  43. package/esm/path/fill-path.d.ts +1 -0
  44. package/esm/path/fill-path.js +129 -0
  45. package/esm/path/fill-path.js.map +1 -0
  46. package/esm/path/format-path.d.ts +1 -0
  47. package/esm/path/format-path.js +139 -0
  48. package/esm/path/format-path.js.map +1 -0
  49. package/esm/path/get-arc-params.d.ts +17 -0
  50. package/esm/path/get-arc-params.js +90 -0
  51. package/esm/path/get-arc-params.js.map +1 -0
  52. package/esm/path/get-line-intersect.d.ts +14 -0
  53. package/esm/path/get-line-intersect.js +33 -0
  54. package/esm/path/get-line-intersect.js.map +1 -0
  55. package/esm/path/index.d.ts +16 -0
  56. package/esm/path/index.js +17 -0
  57. package/esm/path/index.js.map +1 -0
  58. package/esm/path/is-polygons-intersect.d.ts +1 -0
  59. package/esm/path/is-polygons-intersect.js +81 -0
  60. package/esm/path/is-polygons-intersect.js.map +1 -0
  61. package/esm/path/parse-path-array.d.ts +1 -0
  62. package/esm/path/parse-path-array.js +5 -0
  63. package/esm/path/parse-path-array.js.map +1 -0
  64. package/esm/path/parse-path-string.d.ts +1 -0
  65. package/esm/path/parse-path-string.js +57 -0
  66. package/esm/path/parse-path-string.js.map +1 -0
  67. package/esm/path/parse-path.d.ts +1 -0
  68. package/esm/path/parse-path.js +33 -0
  69. package/esm/path/parse-path.js.map +1 -0
  70. package/esm/path/path-2-absolute.d.ts +1 -0
  71. package/esm/path/path-2-absolute.js +124 -0
  72. package/esm/path/path-2-absolute.js.map +1 -0
  73. package/esm/path/path-2-curve.d.ts +2 -0
  74. package/esm/path/path-2-curve.js +63 -0
  75. package/esm/path/path-2-curve.js.map +1 -0
  76. package/esm/path/path-2-segments.d.ts +1 -0
  77. package/esm/path/path-2-segments.js +138 -0
  78. package/esm/path/path-2-segments.js.map +1 -0
  79. package/esm/path/path-intersection.d.ts +9 -0
  80. package/esm/path/path-intersection.js +366 -0
  81. package/esm/path/path-intersection.js.map +1 -0
  82. package/esm/path/point-in-polygon.d.ts +5 -0
  83. package/esm/path/point-in-polygon.js +49 -0
  84. package/esm/path/point-in-polygon.js.map +1 -0
  85. package/esm/path/process/arc-2-cubic.d.ts +1 -0
  86. package/esm/path/process/arc-2-cubic.js +140 -0
  87. package/esm/path/process/arc-2-cubic.js.map +1 -0
  88. package/esm/path/process/line-2-cubic.d.ts +1 -0
  89. package/esm/path/process/line-2-cubic.js +35 -0
  90. package/esm/path/process/line-2-cubic.js.map +1 -0
  91. package/esm/path/process/quad-2-cubic.d.ts +1 -0
  92. package/esm/path/process/quad-2-cubic.js +13 -0
  93. package/esm/path/process/quad-2-cubic.js.map +1 -0
  94. package/esm/path/process/segment-2-cubic.d.ts +2 -0
  95. package/esm/path/process/segment-2-cubic.js +37 -0
  96. package/esm/path/process/segment-2-cubic.js.map +1 -0
  97. package/esm/path/rect-path.d.ts +4 -0
  98. package/esm/path/rect-path.js +19 -0
  99. package/esm/path/rect-path.js.map +1 -0
  100. package/esm/path/types.d.ts +25 -0
  101. package/esm/path/types.js +2 -0
  102. package/esm/path/types.js.map +1 -0
  103. package/lib/color/arr2rgb.d.ts +12 -0
  104. package/lib/color/arr2rgb.js +23 -0
  105. package/lib/color/arr2rgb.js.map +1 -0
  106. package/lib/color/gradient.d.ts +6 -0
  107. package/lib/color/gradient.js +49 -0
  108. package/lib/color/gradient.js.map +1 -0
  109. package/lib/color/index.d.ts +4 -0
  110. package/lib/color/index.js +13 -0
  111. package/lib/color/index.js.map +1 -0
  112. package/lib/color/rgb2arr.d.ts +6 -0
  113. package/lib/color/rgb2arr.js +13 -0
  114. package/lib/color/rgb2arr.js.map +1 -0
  115. package/lib/color/tocssgradient.d.ts +1 -0
  116. package/lib/color/tocssgradient.js +41 -0
  117. package/lib/color/tocssgradient.js.map +1 -0
  118. package/lib/color/torgb.d.ts +6 -0
  119. package/lib/color/torgb.js +40 -0
  120. package/lib/color/torgb.js.map +1 -0
  121. package/lib/helper/index.d.ts +2 -0
  122. package/lib/helper/index.js +8 -0
  123. package/lib/helper/index.js.map +1 -0
  124. package/lib/helper/mod.d.ts +1 -0
  125. package/lib/helper/mod.js +8 -0
  126. package/lib/helper/mod.js.map +1 -0
  127. package/lib/helper/to-radian.d.ts +1 -0
  128. package/lib/helper/to-radian.js +9 -0
  129. package/lib/helper/to-radian.js.map +1 -0
  130. package/lib/index.d.ts +3 -0
  131. package/lib/index.js +16 -0
  132. package/lib/index.js.map +1 -0
  133. package/lib/matrix/index.d.ts +27 -0
  134. package/lib/matrix/index.js +104 -0
  135. package/lib/matrix/index.js.map +1 -0
  136. package/lib/path/catmull-rom-2-bezier.d.ts +10 -0
  137. package/lib/path/catmull-rom-2-bezier.js +119 -0
  138. package/lib/path/catmull-rom-2-bezier.js.map +1 -0
  139. package/lib/path/fill-path-by-diff.d.ts +5 -0
  140. package/lib/path/fill-path-by-diff.js +117 -0
  141. package/lib/path/fill-path-by-diff.js.map +1 -0
  142. package/lib/path/fill-path.d.ts +1 -0
  143. package/lib/path/fill-path.js +133 -0
  144. package/lib/path/fill-path.js.map +1 -0
  145. package/lib/path/format-path.d.ts +1 -0
  146. package/lib/path/format-path.js +143 -0
  147. package/lib/path/format-path.js.map +1 -0
  148. package/lib/path/get-arc-params.d.ts +17 -0
  149. package/lib/path/get-arc-params.js +95 -0
  150. package/lib/path/get-arc-params.js.map +1 -0
  151. package/lib/path/get-line-intersect.d.ts +14 -0
  152. package/lib/path/get-line-intersect.js +37 -0
  153. package/lib/path/get-line-intersect.js.map +1 -0
  154. package/lib/path/index.d.ts +16 -0
  155. package/lib/path/index.js +36 -0
  156. package/lib/path/index.js.map +1 -0
  157. package/lib/path/is-polygons-intersect.d.ts +1 -0
  158. package/lib/path/is-polygons-intersect.js +85 -0
  159. package/lib/path/is-polygons-intersect.js.map +1 -0
  160. package/lib/path/parse-path-array.d.ts +1 -0
  161. package/lib/path/parse-path-array.js +9 -0
  162. package/lib/path/parse-path-array.js.map +1 -0
  163. package/lib/path/parse-path-string.d.ts +1 -0
  164. package/lib/path/parse-path-string.js +61 -0
  165. package/lib/path/parse-path-string.js.map +1 -0
  166. package/lib/path/parse-path.d.ts +1 -0
  167. package/lib/path/parse-path.js +37 -0
  168. package/lib/path/parse-path.js.map +1 -0
  169. package/lib/path/path-2-absolute.d.ts +1 -0
  170. package/lib/path/path-2-absolute.js +128 -0
  171. package/lib/path/path-2-absolute.js.map +1 -0
  172. package/lib/path/path-2-curve.d.ts +2 -0
  173. package/lib/path/path-2-curve.js +67 -0
  174. package/lib/path/path-2-curve.js.map +1 -0
  175. package/lib/path/path-2-segments.d.ts +1 -0
  176. package/lib/path/path-2-segments.js +142 -0
  177. package/lib/path/path-2-segments.js.map +1 -0
  178. package/lib/path/path-intersection.d.ts +9 -0
  179. package/lib/path/path-intersection.js +370 -0
  180. package/lib/path/path-intersection.js.map +1 -0
  181. package/lib/path/point-in-polygon.d.ts +5 -0
  182. package/lib/path/point-in-polygon.js +53 -0
  183. package/lib/path/point-in-polygon.js.map +1 -0
  184. package/lib/path/process/arc-2-cubic.d.ts +1 -0
  185. package/lib/path/process/arc-2-cubic.js +144 -0
  186. package/lib/path/process/arc-2-cubic.js.map +1 -0
  187. package/lib/path/process/line-2-cubic.d.ts +1 -0
  188. package/lib/path/process/line-2-cubic.js +39 -0
  189. package/lib/path/process/line-2-cubic.js.map +1 -0
  190. package/lib/path/process/quad-2-cubic.d.ts +1 -0
  191. package/lib/path/process/quad-2-cubic.js +17 -0
  192. package/lib/path/process/quad-2-cubic.js.map +1 -0
  193. package/lib/path/process/segment-2-cubic.d.ts +2 -0
  194. package/lib/path/process/segment-2-cubic.js +41 -0
  195. package/lib/path/process/segment-2-cubic.js.map +1 -0
  196. package/lib/path/rect-path.d.ts +4 -0
  197. package/lib/path/rect-path.js +23 -0
  198. package/lib/path/rect-path.js.map +1 -0
  199. package/lib/path/types.d.ts +25 -0
  200. package/lib/path/types.js +3 -0
  201. package/lib/path/types.js.map +1 -0
  202. package/package.json +77 -36
  203. package/src/color/arr2rgb.ts +19 -0
  204. package/src/color/gradient.ts +55 -0
  205. package/src/color/index.ts +5 -0
  206. package/src/color/rgb2arr.ts +8 -0
  207. package/src/color/tocssgradient.ts +43 -0
  208. package/src/color/torgb.ts +45 -0
  209. package/src/helper/index.ts +2 -0
  210. package/src/helper/mod.ts +3 -0
  211. package/src/helper/to-radian.ts +5 -0
  212. package/src/index.ts +3 -130
  213. package/src/matrix/index.ts +108 -0
  214. package/src/path/catmull-rom-2-bezier.ts +141 -0
  215. package/src/path/fill-path-by-diff.ts +116 -0
  216. package/src/path/fill-path.ts +129 -0
  217. package/src/path/format-path.ts +142 -0
  218. package/src/path/get-arc-params.ts +102 -0
  219. package/src/path/get-line-intersect.ts +46 -0
  220. package/src/path/index.ts +16 -0
  221. package/src/path/is-polygons-intersect.ts +95 -0
  222. package/src/path/parse-path-array.ts +5 -0
  223. package/src/path/parse-path-string.ts +63 -0
  224. package/src/path/parse-path.ts +34 -0
  225. package/src/path/path-2-absolute.ts +132 -0
  226. package/src/path/path-2-curve.ts +69 -0
  227. package/src/path/path-2-segments.ts +142 -0
  228. package/src/path/path-intersection.ts +419 -0
  229. package/src/path/point-in-polygon.ts +56 -0
  230. package/src/path/process/arc-2-cubic.ts +221 -0
  231. package/src/path/process/line-2-cubic.ts +37 -0
  232. package/src/path/process/quad-2-cubic.ts +12 -0
  233. package/src/path/process/segment-2-cubic.ts +44 -0
  234. package/src/path/rect-path.ts +22 -0
  235. package/src/path/types.ts +25 -0
  236. package/src/augment.ts +0 -16
  237. package/src/cache.ts +0 -31
  238. package/src/clamp.ts +0 -10
  239. package/src/clear-animation-frame.ts +0 -11
  240. package/src/clone.ts +0 -31
  241. package/src/contains.ts +0 -10
  242. package/src/debounce.ts +0 -22
  243. package/src/deep-mix.ts +0 -39
  244. package/src/difference.ts +0 -17
  245. package/src/each.ts +0 -28
  246. package/src/ends-with.ts +0 -10
  247. package/src/every.ts +0 -14
  248. package/src/extend.ts +0 -36
  249. package/src/filter.ts +0 -18
  250. package/src/find-index.ts +0 -12
  251. package/src/find.ts +0 -29
  252. package/src/first-value.ts +0 -21
  253. package/src/fixed-base.ts +0 -14
  254. package/src/flatten-deep.ts +0 -24
  255. package/src/flatten.ts +0 -23
  256. package/src/for-in.ts +0 -3
  257. package/src/get-ellipsis-text.ts +0 -75
  258. package/src/get-range.ts +0 -35
  259. package/src/get-type.ts +0 -7
  260. package/src/get-wrap-behavior.ts +0 -13
  261. package/src/get.ts +0 -18
  262. package/src/group-by.ts +0 -35
  263. package/src/group-to-map.ts +0 -30
  264. package/src/group.ts +0 -14
  265. package/src/has-key.ts +0 -2
  266. package/src/has-value.ts +0 -4
  267. package/src/has.ts +0 -1
  268. package/src/head.ts +0 -8
  269. package/src/identity.ts +0 -1
  270. package/src/index-of.ts +0 -22
  271. package/src/is-arguments.ts +0 -13
  272. package/src/is-array-like.ts +0 -11
  273. package/src/is-array.ts +0 -7
  274. package/src/is-boolean.ts +0 -13
  275. package/src/is-date.ts +0 -7
  276. package/src/is-decimal.ts +0 -7
  277. package/src/is-element.ts +0 -8
  278. package/src/is-empty.ts +0 -39
  279. package/src/is-equal-with.ts +0 -31
  280. package/src/is-equal.ts +0 -46
  281. package/src/is-error.ts +0 -13
  282. package/src/is-even.ts +0 -7
  283. package/src/is-finite.ts +0 -11
  284. package/src/is-function.ts +0 -10
  285. package/src/is-integer.ts +0 -7
  286. package/src/is-match.ts +0 -17
  287. package/src/is-negative.ts +0 -7
  288. package/src/is-nil.ts +0 -10
  289. package/src/is-null.ts +0 -5
  290. package/src/is-number-equal.ts +0 -5
  291. package/src/is-number.ts +0 -10
  292. package/src/is-object-like.ts +0 -11
  293. package/src/is-object.ts +0 -10
  294. package/src/is-odd.ts +0 -7
  295. package/src/is-plain-object.ts +0 -24
  296. package/src/is-positive.ts +0 -7
  297. package/src/is-prototype.ts +0 -9
  298. package/src/is-reg-exp.ts +0 -7
  299. package/src/is-segment-equal.ts +0 -15
  300. package/src/is-string.ts +0 -5
  301. package/src/is-type.ts +0 -5
  302. package/src/is-undefined.ts +0 -5
  303. package/src/keys.ts +0 -14
  304. package/src/last.ts +0 -9
  305. package/src/lower-case.ts +0 -7
  306. package/src/lower-first.ts +0 -8
  307. package/src/map-values.ts +0 -19
  308. package/src/map.ts +0 -17
  309. package/src/max-by.ts +0 -37
  310. package/src/max.ts +0 -28
  311. package/src/measure-text-width.ts +0 -25
  312. package/src/memoize.ts +0 -31
  313. package/src/merge.ts +0 -12
  314. package/src/min-by.ts +0 -38
  315. package/src/min.ts +0 -28
  316. package/src/mix.ts +0 -15
  317. package/src/mod.ts +0 -5
  318. package/src/noop.ts +0 -1
  319. package/src/number2color.ts +0 -17
  320. package/src/omit.ts +0 -15
  321. package/src/parse-radius.ts +0 -45
  322. package/src/pick.ts +0 -32
  323. package/src/pull-at.ts +0 -23
  324. package/src/pull.ts +0 -16
  325. package/src/reduce.ts +0 -17
  326. package/src/remove.ts +0 -30
  327. package/src/request-animation-frame.ts +0 -13
  328. package/src/set.ts +0 -29
  329. package/src/size.ts +0 -12
  330. package/src/some.ts +0 -14
  331. package/src/sort-by.ts +0 -42
  332. package/src/starts-with.ts +0 -10
  333. package/src/substitute.ts +0 -17
  334. package/src/throttle.ts +0 -48
  335. package/src/to-array.ts +0 -5
  336. package/src/to-degree.ts +0 -7
  337. package/src/to-integer.ts +0 -1
  338. package/src/to-radian.ts +0 -7
  339. package/src/to-string.ts +0 -6
  340. package/src/types/index.ts +0 -3
  341. package/src/union.ts +0 -7
  342. package/src/uniq.ts +0 -15
  343. package/src/unique-id.ts +0 -11
  344. package/src/upper-case.ts +0 -7
  345. package/src/upper-first.ts +0 -8
  346. package/src/values-of-key.ts +0 -29
  347. package/src/values.ts +0 -15
  348. package/src/wrap-behavior.ts +0 -20
@@ -0,0 +1,132 @@
1
+ import { parsePathString } from './parse-path-string';
2
+
3
+ const REGEX_MD = /[a-z]/;
4
+
5
+ function toSymmetry(p, c) {
6
+ // 点对称
7
+ return [c[0] + (c[0] - p[0]), c[1] + (c[1] - p[1])];
8
+ }
9
+
10
+ export function path2Absolute(pathString: string) {
11
+ const pathArray = parsePathString(pathString);
12
+
13
+ if (!pathArray || !pathArray.length) {
14
+ return [['M', 0, 0]];
15
+ }
16
+ let needProcess = false; // 如果存在小写的命令或者 V,H,T,S 则需要处理
17
+ for (let i = 0; i < pathArray.length; i++) {
18
+ const cmd = pathArray[i][0];
19
+ // 如果存在相对位置的命令,则中断返回
20
+ if (REGEX_MD.test(cmd) || ['V', 'H', 'T', 'S'].indexOf(cmd) >= 0) {
21
+ needProcess = true;
22
+ break;
23
+ }
24
+ }
25
+ // 如果不存在相对命令,则直接返回
26
+ // 如果在业务上都写绝对路径,这种方式最快,仅做了一次检测
27
+ if (!needProcess) {
28
+ return pathArray;
29
+ }
30
+
31
+ const res = [];
32
+ let x = 0;
33
+ let y = 0;
34
+ let mx = 0;
35
+ let my = 0;
36
+ let start = 0;
37
+ const first = pathArray[0];
38
+ if (first[0] === 'M' || first[0] === 'm') {
39
+ x = +first[1];
40
+ y = +first[2];
41
+ mx = x;
42
+ my = y;
43
+ start++;
44
+ res[0] = ['M', x, y];
45
+ }
46
+
47
+ for (let i = start, ii = pathArray.length; i < ii; i++) {
48
+ const pa = pathArray[i];
49
+ const preParams = res[i - 1]; // 取前一个已经处理后的节点,否则会出现问题
50
+ let r = [];
51
+ const cmd = pa[0];
52
+ const upCmd = cmd.toUpperCase();
53
+ if (cmd !== upCmd) {
54
+ r[0] = upCmd;
55
+ switch (upCmd) {
56
+ case 'A':
57
+ r[1] = pa[1];
58
+ r[2] = pa[2];
59
+ r[3] = pa[3];
60
+ r[4] = pa[4];
61
+ r[5] = pa[5];
62
+ r[6] = +pa[6] + x;
63
+ r[7] = +pa[7] + y;
64
+ break;
65
+ case 'V':
66
+ r[1] = +pa[1] + y;
67
+ break;
68
+ case 'H':
69
+ r[1] = +pa[1] + x;
70
+ break;
71
+ case 'M':
72
+ mx = +pa[1] + x;
73
+ my = +pa[2] + y;
74
+ r[1] = mx;
75
+ r[2] = my;
76
+ break; // for lint
77
+ default:
78
+ for (let j = 1, jj = pa.length; j < jj; j++) {
79
+ r[j] = +pa[j] + (j % 2 ? x : y);
80
+ }
81
+ }
82
+ } else {
83
+ // 如果本来已经大写,则不处理
84
+ r = pathArray[i];
85
+ }
86
+ // 需要在外面统一做,同时处理 V,H,S,T 等特殊指令
87
+ switch (upCmd) {
88
+ case 'Z':
89
+ x = +mx;
90
+ y = +my;
91
+ break;
92
+ case 'H':
93
+ x = r[1];
94
+ r = ['L', x, y];
95
+ break;
96
+ case 'V':
97
+ y = r[1];
98
+ r = ['L', x, y];
99
+ break;
100
+ case 'T':
101
+ x = r[1];
102
+ y = r[2];
103
+ // 以 x, y 为中心的,上一个控制点的对称点
104
+ // 需要假设上一个节点的命令为 Q
105
+ const symetricT = toSymmetry([preParams[1], preParams[2]], [preParams[3], preParams[4]]);
106
+ r = ['Q', symetricT[0], symetricT[1], x, y];
107
+ break;
108
+ case 'S':
109
+ x = r[r.length - 2];
110
+ y = r[r.length - 1];
111
+ // 以 x,y 为中心,取上一个控制点,
112
+ // 需要假设上一个线段为 C 或者 S
113
+ const length = preParams.length;
114
+ const symetricS = toSymmetry(
115
+ [preParams[length - 4], preParams[length - 3]],
116
+ [preParams[length - 2], preParams[length - 1]],
117
+ );
118
+ r = ['C', symetricS[0], symetricS[1], r[1], r[2], x, y];
119
+ break;
120
+ case 'M':
121
+ mx = r[r.length - 2];
122
+ my = r[r.length - 1];
123
+ break; // for lint
124
+ default:
125
+ x = r[r.length - 2];
126
+ y = r[r.length - 1];
127
+ }
128
+ res.push(r);
129
+ }
130
+
131
+ return res;
132
+ }
@@ -0,0 +1,69 @@
1
+ import { path2Absolute } from './path-2-absolute';
2
+ import { segmentToCubic } from './process/segment-2-cubic';
3
+ import type { PathCommand, ProcessParams } from './types';
4
+
5
+ export function path2Curve(path: PathCommand[] | string, needZCommandIndexes = false) {
6
+ const pathArray = path2Absolute(path as string) as PathCommand[];
7
+
8
+ const params: ProcessParams = {
9
+ x1: 0,
10
+ y1: 0,
11
+ x2: 0,
12
+ y2: 0,
13
+ x: 0,
14
+ y: 0,
15
+ qx: null,
16
+ qy: null,
17
+ };
18
+ const allPathCommands = [];
19
+ let pathCommand = '';
20
+ let ii = pathArray.length;
21
+ let segment: PathCommand;
22
+ let seglen: number;
23
+ const zCommandIndexes: number[] = [];
24
+
25
+ for (let i = 0; i < ii; i += 1) {
26
+ if (pathArray[i]) [pathCommand] = pathArray[i];
27
+
28
+ allPathCommands[i] = pathCommand;
29
+ pathArray[i] = segmentToCubic(pathArray[i], params);
30
+
31
+ fixArc(pathArray, allPathCommands, i);
32
+ ii = pathArray.length; // solves curveArrays ending in Z
33
+
34
+ // keep Z command account for lineJoin
35
+ // @see https://github.com/antvis/util/issues/68
36
+ if (pathCommand === 'Z') {
37
+ zCommandIndexes.push(i);
38
+ }
39
+
40
+ segment = pathArray[i];
41
+ seglen = segment.length;
42
+
43
+ params.x1 = +segment[seglen - 2];
44
+ params.y1 = +segment[seglen - 1];
45
+ params.x2 = +segment[seglen - 4] || params.x1;
46
+ params.y2 = +segment[seglen - 3] || params.y1;
47
+ }
48
+ if (needZCommandIndexes) {
49
+ return [pathArray, zCommandIndexes];
50
+ } else {
51
+ return pathArray;
52
+ }
53
+ }
54
+
55
+ function fixArc(pathArray: PathCommand[], allPathCommands: string[], i: number) {
56
+ if (pathArray[i].length > 7) {
57
+ pathArray[i].shift();
58
+ const pi = pathArray[i];
59
+ // const ni = i + 1;
60
+ let ni = i;
61
+ while (pi.length) {
62
+ // if created multiple C:s, their original seg is saved
63
+ allPathCommands[i] = 'A';
64
+ // @ts-ignore
65
+ pathArray.splice((ni += 1), 0, ['C'].concat(pi.splice(0, 6)));
66
+ }
67
+ pathArray.splice(i, 1);
68
+ }
69
+ }
@@ -0,0 +1,142 @@
1
+ import { getArcParams, isSamePoint } from './get-arc-params';
2
+ import { parsePath } from './parse-path';
3
+
4
+ // 点对称
5
+ function toSymmetry(point, center) {
6
+ return [center[0] + (center[0] - point[0]), center[1] + (center[1] - point[1])];
7
+ }
8
+
9
+ export function path2Segments(path) {
10
+ path = parsePath(path);
11
+ const segments = [];
12
+ let currentPoint = null; // 当前图形
13
+ let nextParams = null; // 下一节点的 path 参数
14
+ let startMovePoint = null; // 开始 M 的点,可能会有多个
15
+ let lastStartMovePointIndex = 0; // 最近一个开始点 M 的索引
16
+ const count = path.length;
17
+ for (let i = 0; i < count; i++) {
18
+ const params = path[i];
19
+ nextParams = path[i + 1];
20
+ const command = params[0];
21
+ // 数学定义上的参数,便于后面的计算
22
+ const segment = {
23
+ command,
24
+ prePoint: currentPoint,
25
+ params,
26
+ startTangent: null,
27
+ endTangent: null,
28
+ };
29
+ switch (command) {
30
+ case 'M':
31
+ startMovePoint = [params[1], params[2]];
32
+ lastStartMovePointIndex = i;
33
+ break;
34
+ case 'A':
35
+ const arcParams = getArcParams(currentPoint, params);
36
+ segment['arcParams'] = arcParams;
37
+ break;
38
+ default:
39
+ break;
40
+ }
41
+ if (command === 'Z') {
42
+ // 有了 Z 后,当前节点从开始 M 的点开始
43
+ currentPoint = startMovePoint;
44
+ // 如果当前点的命令为 Z,相当于当前点为最近一个 M 点,则下一个点直接指向最近一个 M 点的下一个点
45
+ nextParams = path[lastStartMovePointIndex + 1];
46
+ } else {
47
+ const len = params.length;
48
+ currentPoint = [params[len - 2], params[len - 1]];
49
+ }
50
+ if (nextParams && nextParams[0] === 'Z') {
51
+ // 如果下一个点的命令为 Z,则下一个点直接指向最近一个 M 点
52
+ nextParams = path[lastStartMovePointIndex];
53
+ if (segments[lastStartMovePointIndex]) {
54
+ // 如果下一个点的命令为 Z,则最近一个 M 点的前一个点为当前点
55
+ segments[lastStartMovePointIndex].prePoint = currentPoint;
56
+ }
57
+ }
58
+ segment['currentPoint'] = currentPoint;
59
+ // 如果当前点与最近一个 M 点相同,则最近一个 M 点的前一个点为当前点的前一个点
60
+ if (
61
+ segments[lastStartMovePointIndex] &&
62
+ isSamePoint(currentPoint, segments[lastStartMovePointIndex].currentPoint)
63
+ ) {
64
+ segments[lastStartMovePointIndex].prePoint = segment.prePoint;
65
+ }
66
+ const nextPoint = nextParams ? [nextParams[nextParams.length - 2], nextParams[nextParams.length - 1]] : null;
67
+ segment['nextPoint'] = nextPoint;
68
+ // Add startTangent and endTangent
69
+ const { prePoint } = segment;
70
+ if (['L', 'H', 'V'].includes(command)) {
71
+ segment.startTangent = [prePoint[0] - currentPoint[0], prePoint[1] - currentPoint[1]];
72
+ segment.endTangent = [currentPoint[0] - prePoint[0], currentPoint[1] - prePoint[1]];
73
+ } else if (command === 'Q') {
74
+ // 二次贝塞尔曲线只有一个控制点
75
+ const cp = [params[1], params[2]];
76
+ // 二次贝塞尔曲线的终点为 currentPoint
77
+ segment.startTangent = [prePoint[0] - cp[0], prePoint[1] - cp[1]];
78
+ segment.endTangent = [currentPoint[0] - cp[0], currentPoint[1] - cp[1]];
79
+ } else if (command === 'T') {
80
+ const preSegment = segments[i - 1];
81
+ const cp = toSymmetry(preSegment.currentPoint, prePoint);
82
+ if (preSegment.command === 'Q') {
83
+ segment.command = 'Q';
84
+ segment.startTangent = [prePoint[0] - cp[0], prePoint[1] - cp[1]];
85
+ segment.endTangent = [currentPoint[0] - cp[0], currentPoint[1] - cp[1]];
86
+ } else {
87
+ segment.command = 'TL';
88
+ segment.startTangent = [prePoint[0] - currentPoint[0], prePoint[1] - currentPoint[1]];
89
+ segment.endTangent = [currentPoint[0] - prePoint[0], currentPoint[1] - prePoint[1]];
90
+ }
91
+ } else if (command === 'C') {
92
+ // 三次贝塞尔曲线有两个控制点
93
+ const cp1 = [params[1], params[2]];
94
+ const cp2 = [params[3], params[4]];
95
+ segment.startTangent = [prePoint[0] - cp1[0], prePoint[1] - cp1[1]];
96
+ segment.endTangent = [currentPoint[0] - cp2[0], currentPoint[1] - cp2[1]];
97
+
98
+ // horizontal line, eg. ['C', 100, 100, 100, 100, 200, 200]
99
+ if (segment.startTangent[0] === 0 && segment.startTangent[1] === 0) {
100
+ segment.startTangent = [cp1[0] - cp2[0], cp1[1] - cp2[1]];
101
+ }
102
+ if (segment.endTangent[0] === 0 && segment.endTangent[1] === 0) {
103
+ segment.endTangent = [cp2[0] - cp1[0], cp2[1] - cp1[1]];
104
+ }
105
+ } else if (command === 'S') {
106
+ const preSegment = segments[i - 1];
107
+ const cp1 = toSymmetry(preSegment.currentPoint, prePoint);
108
+ const cp2 = [params[1], params[2]];
109
+ if (preSegment.command === 'C') {
110
+ segment.command = 'C'; // 将 S 命令变换为 C 命令
111
+ segment.startTangent = [prePoint[0] - cp1[0], prePoint[1] - cp1[1]];
112
+ segment.endTangent = [currentPoint[0] - cp2[0], currentPoint[1] - cp2[1]];
113
+ } else {
114
+ segment.command = 'SQ'; // 将 S 命令变换为 SQ 命令
115
+ segment.startTangent = [prePoint[0] - cp2[0], prePoint[1] - cp2[1]];
116
+ segment.endTangent = [currentPoint[0] - cp2[0], currentPoint[1] - cp2[1]];
117
+ }
118
+ } else if (command === 'A') {
119
+ let d = 0.001;
120
+ const {
121
+ cx = 0,
122
+ cy = 0,
123
+ rx = 0,
124
+ ry = 0,
125
+ sweepFlag = 0,
126
+ startAngle = 0,
127
+ endAngle = 0,
128
+ } = segment['arcParams'] || {};
129
+ if (sweepFlag === 0) {
130
+ d *= -1;
131
+ }
132
+ const dx1 = rx * Math.cos(startAngle - d) + cx;
133
+ const dy1 = ry * Math.sin(startAngle - d) + cy;
134
+ segment.startTangent = [dx1 - startMovePoint[0], dy1 - startMovePoint[1]];
135
+ const dx2 = rx * Math.cos(startAngle + endAngle + d) + cx;
136
+ const dy2 = ry * Math.sin(startAngle + endAngle - d) + cy;
137
+ segment.endTangent = [prePoint[0] - dx2, prePoint[1] - dy2];
138
+ }
139
+ segments.push(segment);
140
+ }
141
+ return segments;
142
+ }