@open-pencil/scene-graph 0.17.0-beta.4 → 0.17.0-beta.6

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 (345) hide show
  1. package/dist/buffered-events.js +1 -1
  2. package/dist/buffered-events.js.map +1 -1
  3. package/dist/clipping.d.ts +7 -1
  4. package/dist/clipping.d.ts.map +1 -1
  5. package/dist/clipping.js +23 -19
  6. package/dist/clipping.js.map +1 -1
  7. package/dist/comments/schema.d.ts.map +1 -1
  8. package/dist/comments/schema.js +1 -0
  9. package/dist/comments/schema.js.map +1 -1
  10. package/dist/comments/threads.d.ts +2 -0
  11. package/dist/comments/threads.d.ts.map +1 -1
  12. package/dist/comments/threads.js +11 -5
  13. package/dist/comments/threads.js.map +1 -1
  14. package/dist/components/index.d.ts +2 -2
  15. package/dist/components/index.js +2 -2
  16. package/dist/components/properties.d.ts +1 -3
  17. package/dist/components/properties.d.ts.map +1 -1
  18. package/dist/components/properties.js +3 -10
  19. package/dist/components/properties.js.map +1 -1
  20. package/dist/constants.d.ts +12 -2
  21. package/dist/constants.d.ts.map +1 -1
  22. package/dist/constants.js +42 -1
  23. package/dist/constants.js.map +1 -1
  24. package/dist/copy.d.ts.map +1 -1
  25. package/dist/copy.js +2 -0
  26. package/dist/copy.js.map +1 -1
  27. package/dist/corners.d.ts +22 -0
  28. package/dist/corners.d.ts.map +1 -0
  29. package/dist/corners.js +52 -0
  30. package/dist/corners.js.map +1 -0
  31. package/dist/css/grid.d.ts +4 -1
  32. package/dist/css/grid.d.ts.map +1 -1
  33. package/dist/css/grid.js +40 -7
  34. package/dist/css/grid.js.map +1 -1
  35. package/dist/css/index.d.ts +3 -3
  36. package/dist/css/index.js +3 -3
  37. package/dist/css/layout.d.ts +13 -1
  38. package/dist/css/layout.d.ts.map +1 -1
  39. package/dist/css/layout.js +26 -1
  40. package/dist/css/layout.js.map +1 -1
  41. package/dist/css/values.d.ts +23 -1
  42. package/dist/css/values.d.ts.map +1 -1
  43. package/dist/css/values.js +39 -1
  44. package/dist/css/values.js.map +1 -1
  45. package/dist/font-style.d.ts +2 -1
  46. package/dist/font-style.d.ts.map +1 -1
  47. package/dist/font-style.js +31 -6
  48. package/dist/font-style.js.map +1 -1
  49. package/dist/geometry.d.ts +68 -3
  50. package/dist/geometry.d.ts.map +1 -1
  51. package/dist/geometry.js +159 -25
  52. package/dist/geometry.js.map +1 -1
  53. package/dist/gradient.d.ts +9 -2
  54. package/dist/gradient.d.ts.map +1 -1
  55. package/dist/gradient.js +29 -1
  56. package/dist/gradient.js.map +1 -1
  57. package/dist/graph.d.ts +9 -2
  58. package/dist/graph.d.ts.map +1 -1
  59. package/dist/graph.js +46 -10
  60. package/dist/graph.js.map +1 -1
  61. package/dist/group-bounds.d.ts.map +1 -1
  62. package/dist/group-bounds.js +4 -3
  63. package/dist/group-bounds.js.map +1 -1
  64. package/dist/guides.d.ts +11 -2
  65. package/dist/guides.d.ts.map +1 -1
  66. package/dist/guides.js +30 -1
  67. package/dist/guides.js.map +1 -1
  68. package/dist/hit-test.d.ts.map +1 -1
  69. package/dist/hit-test.js +57 -5
  70. package/dist/hit-test.js.map +1 -1
  71. package/dist/icons/fit.d.ts +17 -0
  72. package/dist/icons/fit.d.ts.map +1 -0
  73. package/dist/icons/fit.js +21 -0
  74. package/dist/icons/fit.js.map +1 -0
  75. package/dist/icons/glyph.d.ts.map +1 -1
  76. package/dist/icons/glyph.js +5 -4
  77. package/dist/icons/glyph.js.map +1 -1
  78. package/dist/icons/index.d.ts +2 -1
  79. package/dist/icons/index.js +2 -1
  80. package/dist/ids.js +1 -1
  81. package/dist/ids.js.map +1 -1
  82. package/dist/images.d.ts +14 -4
  83. package/dist/images.d.ts.map +1 -1
  84. package/dist/images.js +45 -5
  85. package/dist/images.js.map +1 -1
  86. package/dist/index.d.ts +2 -2
  87. package/dist/instances/assignments.js +39 -6
  88. package/dist/instances/assignments.js.map +1 -1
  89. package/dist/instances/index.d.ts +6 -1
  90. package/dist/instances/index.d.ts.map +1 -1
  91. package/dist/instances/index.js +58 -9
  92. package/dist/instances/index.js.map +1 -1
  93. package/dist/instances/kept-overrides.js +2 -2
  94. package/dist/instances/kept-overrides.js.map +1 -1
  95. package/dist/instances/matching.js +68 -0
  96. package/dist/instances/matching.js.map +1 -0
  97. package/dist/instances/overrides.d.ts +4 -1
  98. package/dist/instances/overrides.d.ts.map +1 -1
  99. package/dist/instances/overrides.js +13 -1
  100. package/dist/instances/overrides.js.map +1 -1
  101. package/dist/instances/protection.js +67 -0
  102. package/dist/instances/protection.js.map +1 -0
  103. package/dist/instances/swap.js +7 -5
  104. package/dist/instances/swap.js.map +1 -1
  105. package/dist/instances/sync.js +34 -116
  106. package/dist/instances/sync.js.map +1 -1
  107. package/dist/layout/guides.d.ts +17 -1
  108. package/dist/layout/guides.d.ts.map +1 -1
  109. package/dist/layout/guides.js +31 -11
  110. package/dist/layout/guides.js.map +1 -1
  111. package/dist/layout/index.d.ts +5 -2
  112. package/dist/layout/index.js +5 -2
  113. package/dist/layout/limits.d.ts +40 -0
  114. package/dist/layout/limits.d.ts.map +1 -0
  115. package/dist/layout/limits.js +54 -0
  116. package/dist/layout/limits.js.map +1 -0
  117. package/dist/layout/sizing.d.ts.map +1 -1
  118. package/dist/layout/sizing.js +0 -1
  119. package/dist/layout/sizing.js.map +1 -1
  120. package/dist/layout/spacing.d.ts +10 -0
  121. package/dist/layout/spacing.d.ts.map +1 -0
  122. package/dist/layout/spacing.js +12 -0
  123. package/dist/layout/spacing.js.map +1 -0
  124. package/dist/layout/strokes.d.ts +10 -0
  125. package/dist/layout/strokes.d.ts.map +1 -0
  126. package/dist/layout/strokes.js +30 -0
  127. package/dist/layout/strokes.js.map +1 -0
  128. package/dist/masks.d.ts +28 -0
  129. package/dist/masks.d.ts.map +1 -0
  130. package/dist/masks.js +53 -0
  131. package/dist/masks.js.map +1 -0
  132. package/dist/matrix.d.ts +2 -1
  133. package/dist/matrix.d.ts.map +1 -1
  134. package/dist/matrix.js +14 -1
  135. package/dist/matrix.js.map +1 -1
  136. package/dist/node-defaults.d.ts +9 -1
  137. package/dist/node-defaults.d.ts.map +1 -1
  138. package/dist/node-defaults.js +65 -38
  139. package/dist/node-defaults.js.map +1 -1
  140. package/dist/outline/flatten.d.ts.map +1 -1
  141. package/dist/outline/flatten.js +24 -17
  142. package/dist/outline/flatten.js.map +1 -1
  143. package/dist/outline/hit.d.ts +2 -8
  144. package/dist/outline/hit.d.ts.map +1 -1
  145. package/dist/outline/hit.js +16 -25
  146. package/dist/outline/hit.js.map +1 -1
  147. package/dist/outline/index.d.ts +3 -3
  148. package/dist/outline/index.js +3 -3
  149. package/dist/outline/shapes.d.ts +3 -1
  150. package/dist/outline/shapes.d.ts.map +1 -1
  151. package/dist/outline/shapes.js +18 -39
  152. package/dist/outline/shapes.js.map +1 -1
  153. package/dist/paints/index.d.ts +3 -1
  154. package/dist/paints/index.js +3 -1
  155. package/dist/paints/pattern.d.ts +13 -0
  156. package/dist/paints/pattern.d.ts.map +1 -0
  157. package/dist/paints/pattern.js +47 -0
  158. package/dist/paints/pattern.js.map +1 -0
  159. package/dist/paints/shows.d.ts +11 -0
  160. package/dist/paints/shows.d.ts.map +1 -0
  161. package/dist/paints/shows.js +16 -0
  162. package/dist/paints/shows.js.map +1 -0
  163. package/dist/parse-path.d.ts +4 -1
  164. package/dist/parse-path.d.ts.map +1 -1
  165. package/dist/parse-path.js +15 -1
  166. package/dist/parse-path.js.map +1 -1
  167. package/dist/paths/arc.d.ts +32 -0
  168. package/dist/paths/arc.d.ts.map +1 -0
  169. package/dist/paths/arc.js +49 -0
  170. package/dist/paths/arc.js.map +1 -0
  171. package/dist/paths/index.d.ts +5 -0
  172. package/dist/paths/index.js +5 -0
  173. package/dist/paths/network.d.ts +63 -0
  174. package/dist/paths/network.d.ts.map +1 -0
  175. package/dist/paths/network.js +315 -0
  176. package/dist/paths/network.js.map +1 -0
  177. package/dist/paths/sink.d.ts +20 -0
  178. package/dist/paths/sink.d.ts.map +1 -0
  179. package/dist/paths/sink.js +32 -0
  180. package/dist/paths/sink.js.map +1 -0
  181. package/dist/paths/smooth-corners.d.ts +25 -0
  182. package/dist/paths/smooth-corners.d.ts.map +1 -0
  183. package/dist/paths/smooth-corners.js +196 -0
  184. package/dist/paths/smooth-corners.js.map +1 -0
  185. package/dist/plugin-data/fields.d.ts +4 -0
  186. package/dist/plugin-data/fields.d.ts.map +1 -1
  187. package/dist/plugin-data/fields.js +2 -0
  188. package/dist/plugin-data/fields.js.map +1 -1
  189. package/dist/polygon.d.ts +1 -3
  190. package/dist/polygon.d.ts.map +1 -1
  191. package/dist/polygon.js +1 -16
  192. package/dist/polygon.js.map +1 -1
  193. package/dist/primitives/index.d.ts +1 -7
  194. package/dist/primitives/index.d.ts.map +1 -1
  195. package/dist/resize.d.ts.map +1 -1
  196. package/dist/resize.js +5 -5
  197. package/dist/resize.js.map +1 -1
  198. package/dist/scaling/node.d.ts.map +1 -1
  199. package/dist/scaling/node.js +25 -3
  200. package/dist/scaling/node.js.map +1 -1
  201. package/dist/shaders/model.d.ts.map +1 -1
  202. package/dist/shaders/model.js +1 -1
  203. package/dist/shaders/model.js.map +1 -1
  204. package/dist/shared-styles.d.ts.map +1 -1
  205. package/dist/shared-styles.js +2 -9
  206. package/dist/shared-styles.js.map +1 -1
  207. package/dist/strokes.d.ts +39 -0
  208. package/dist/strokes.d.ts.map +1 -0
  209. package/dist/strokes.js +104 -0
  210. package/dist/strokes.js.map +1 -0
  211. package/dist/text/auto-resize.d.ts +15 -0
  212. package/dist/text/auto-resize.d.ts.map +1 -0
  213. package/dist/text/auto-resize.js +23 -0
  214. package/dist/text/auto-resize.js.map +1 -0
  215. package/dist/text/index.d.ts +5 -3
  216. package/dist/text/index.js +5 -3
  217. package/dist/text/lengths.d.ts +6 -1
  218. package/dist/text/lengths.d.ts.map +1 -1
  219. package/dist/text/lengths.js +10 -1
  220. package/dist/text/lengths.js.map +1 -1
  221. package/dist/text/overflow.d.ts +14 -5
  222. package/dist/text/overflow.d.ts.map +1 -1
  223. package/dist/text/overflow.js +24 -6
  224. package/dist/text/overflow.js.map +1 -1
  225. package/dist/text/path.js +10 -0
  226. package/dist/text/path.js.map +1 -0
  227. package/dist/text/picture.js +5 -5
  228. package/dist/text/picture.js.map +1 -1
  229. package/dist/text/runs.d.ts +29 -0
  230. package/dist/text/runs.d.ts.map +1 -0
  231. package/dist/text/runs.js +72 -0
  232. package/dist/text/runs.js.map +1 -0
  233. package/dist/transfer/dependencies.d.ts.map +1 -1
  234. package/dist/transfer/dependencies.js +3 -2
  235. package/dist/transfer/dependencies.js.map +1 -1
  236. package/dist/transfer/nodes.js +2 -1
  237. package/dist/transfer/nodes.js.map +1 -1
  238. package/dist/types.d.ts +56 -10
  239. package/dist/types.d.ts.map +1 -1
  240. package/dist/types.js.map +1 -1
  241. package/dist/variables/index.d.ts +4 -3
  242. package/dist/variables/index.d.ts.map +1 -1
  243. package/dist/variables/index.js +4 -3
  244. package/dist/variables/index.js.map +1 -1
  245. package/dist/variables/lookup.d.ts +20 -0
  246. package/dist/variables/lookup.d.ts.map +1 -0
  247. package/dist/variables/lookup.js +34 -0
  248. package/dist/variables/lookup.js.map +1 -0
  249. package/dist/variables/paints.d.ts +35 -4
  250. package/dist/variables/paints.d.ts.map +1 -1
  251. package/dist/variables/paints.js +67 -16
  252. package/dist/variables/paints.js.map +1 -1
  253. package/dist/variables/resolution.d.ts +3 -1
  254. package/dist/variables/resolution.d.ts.map +1 -1
  255. package/dist/variables/resolution.js +8 -3
  256. package/dist/variables/resolution.js.map +1 -1
  257. package/dist/variables/subtree.js +1 -1
  258. package/dist/variables/subtree.js.map +1 -1
  259. package/dist/vector-network.d.ts.map +1 -1
  260. package/dist/vector-network.js +13 -79
  261. package/dist/vector-network.js.map +1 -1
  262. package/package.json +28 -7
  263. package/src/buffered-events.ts +2 -1
  264. package/src/clipping.ts +11 -8
  265. package/src/comments/schema.ts +1 -0
  266. package/src/comments/threads.ts +13 -5
  267. package/src/components/properties.ts +3 -14
  268. package/src/constants.ts +25 -1
  269. package/src/copy.ts +2 -0
  270. package/src/corners.ts +93 -0
  271. package/src/css/grid.ts +49 -7
  272. package/src/css/index.ts +13 -2
  273. package/src/css/layout.ts +36 -0
  274. package/src/css/values.ts +49 -0
  275. package/src/font-style.ts +38 -4
  276. package/src/geometry.ts +223 -28
  277. package/src/gradient.ts +23 -1
  278. package/src/graph.ts +35 -10
  279. package/src/group-bounds.ts +7 -3
  280. package/src/guides.ts +26 -1
  281. package/src/hit-test.ts +68 -10
  282. package/src/icons/fit.ts +24 -0
  283. package/src/icons/glyph.ts +6 -4
  284. package/src/icons/index.ts +1 -0
  285. package/src/ids.ts +1 -1
  286. package/src/images.ts +67 -7
  287. package/src/instances/assignments.ts +65 -7
  288. package/src/instances/index.ts +77 -13
  289. package/src/instances/kept-overrides.ts +2 -5
  290. package/src/instances/matching.ts +101 -0
  291. package/src/instances/overrides.ts +18 -0
  292. package/src/instances/protection.ts +103 -0
  293. package/src/instances/swap.ts +12 -9
  294. package/src/instances/sync.ts +52 -184
  295. package/src/layout/guides.ts +42 -9
  296. package/src/layout/index.ts +3 -0
  297. package/src/layout/limits.ts +64 -0
  298. package/src/layout/sizing.ts +0 -2
  299. package/src/layout/spacing.ts +11 -0
  300. package/src/layout/strokes.ts +23 -0
  301. package/src/masks.ts +51 -0
  302. package/src/matrix.ts +15 -2
  303. package/src/node-defaults.ts +71 -36
  304. package/src/outline/flatten.ts +31 -18
  305. package/src/outline/hit.ts +16 -33
  306. package/src/outline/index.ts +2 -2
  307. package/src/outline/shapes.ts +19 -36
  308. package/src/paints/index.ts +2 -0
  309. package/src/paints/pattern.ts +45 -0
  310. package/src/paints/shows.ts +13 -0
  311. package/src/parse-path.ts +14 -0
  312. package/src/paths/arc.ts +59 -0
  313. package/src/paths/index.ts +4 -0
  314. package/src/paths/network.ts +411 -0
  315. package/src/paths/sink.ts +50 -0
  316. package/src/paths/smooth-corners.ts +357 -0
  317. package/src/plugin-data/fields.ts +2 -0
  318. package/src/polygon.ts +0 -17
  319. package/src/primitives/index.ts +0 -5
  320. package/src/resize.ts +6 -7
  321. package/src/scaling/node.ts +29 -3
  322. package/src/shaders/model.ts +2 -1
  323. package/src/shared-styles.ts +3 -4
  324. package/src/strokes.ts +121 -0
  325. package/src/text/auto-resize.ts +25 -0
  326. package/src/text/index.ts +2 -0
  327. package/src/text/lengths.ts +11 -0
  328. package/src/text/overflow.ts +32 -6
  329. package/src/text/path.ts +13 -0
  330. package/src/text/picture.ts +5 -5
  331. package/src/text/runs.ts +80 -0
  332. package/src/transfer/dependencies.ts +4 -2
  333. package/src/transfer/nodes.ts +2 -1
  334. package/src/types.ts +62 -8
  335. package/src/variables/index.ts +1 -0
  336. package/src/variables/lookup.ts +31 -0
  337. package/src/variables/paints.ts +101 -20
  338. package/src/variables/resolution.ts +15 -2
  339. package/src/variables/subtree.ts +1 -1
  340. package/src/vector-network.ts +19 -142
  341. package/dist/random.d.ts +0 -7
  342. package/dist/random.d.ts.map +0 -1
  343. package/dist/random.js +0 -25
  344. package/dist/random.js.map +0 -1
  345. package/src/random.ts +0 -28
@@ -0,0 +1,411 @@
1
+ import type { Vector } from '../primitives'
2
+ import type { VectorNetwork, VectorRegion, VectorSegment, VectorVertex } from '../types'
3
+ import type { PathSink } from './sink'
4
+
5
+ export function addSegmentDirected(
6
+ path: PathSink,
7
+ seg: VectorSegment,
8
+ vertices: VectorVertex[],
9
+ forward: boolean
10
+ ): void {
11
+ const p0 = forward ? vertices[seg.start] : vertices[seg.end]
12
+ const p3 = forward ? vertices[seg.end] : vertices[seg.start]
13
+ const ts = seg.tangentStart
14
+ const te = seg.tangentEnd
15
+
16
+ const isLine = ts.x === 0 && ts.y === 0 && te.x === 0 && te.y === 0
17
+ if (isLine) {
18
+ path.lineTo(p3.x, p3.y)
19
+ } else if (forward) {
20
+ path.cubicTo(p0.x + ts.x, p0.y + ts.y, p3.x + te.x, p3.y + te.y, p3.x, p3.y)
21
+ } else {
22
+ path.cubicTo(p0.x + te.x, p0.y + te.y, p3.x + ts.x, p3.y + ts.y, p3.x, p3.y)
23
+ }
24
+ }
25
+
26
+ /**
27
+ * Draws one region loop. It starts at the end of its first segment away from the second, as a
28
+ * loop's stored order runs; a segment that does not continue from the last starts a new subpath,
29
+ * and the loop closes only when it runs unbroken back to where it started.
30
+ */
31
+ export function addLoopToPath(
32
+ path: PathSink,
33
+ loop: number[],
34
+ segments: VectorSegment[],
35
+ vertices: VectorVertex[]
36
+ ): void {
37
+ if (loop.length === 0) return
38
+ const first = segments[loop[0]]
39
+ const second = loop.length > 1 ? segments[loop[1]] : null
40
+ const secondMeetsStart = second?.start === first.start || second?.end === first.start
41
+ const secondMeetsEnd = second?.start === first.end || second?.end === first.end
42
+ const start = !second || secondMeetsEnd || !secondMeetsStart ? first.start : first.end
43
+ let current = start
44
+ let broken = false
45
+ path.moveTo(vertices[start].x, vertices[start].y)
46
+ for (const index of loop) {
47
+ const segment = segments[index]
48
+ const connected = segment.start === current || segment.end === current
49
+ const forward = segment.start === current || !connected
50
+ if (!connected) {
51
+ broken = true
52
+ const from = vertices[segment.start]
53
+ path.moveTo(from.x, from.y)
54
+ }
55
+ addSegmentDirected(path, segment, vertices, forward)
56
+ current = forward ? segment.end : segment.start
57
+ }
58
+ if (!broken && current === start) path.close()
59
+ }
60
+
61
+ /** A region's loops, one after another: the outline its fill rule fills. */
62
+ export function addRegionToPath(
63
+ path: PathSink,
64
+ region: Pick<VectorRegion, 'loops'>,
65
+ segments: VectorSegment[],
66
+ vertices: VectorVertex[]
67
+ ): void {
68
+ for (const loop of region.loops) addLoopToPath(path, loop, segments, vertices)
69
+ }
70
+
71
+ function isStraight(seg: VectorSegment): boolean {
72
+ const { tangentStart: ts, tangentEnd: te } = seg
73
+ return ts.x === 0 && ts.y === 0 && te.x === 0 && te.y === 0
74
+ }
75
+
76
+ /** Cubic handle length, as a fraction of the radius, of a circular arc turning by `angle`. */
77
+ function arcHandle(angle: number): number {
78
+ return (4 / 3) * Math.tan(angle / 4)
79
+ }
80
+
81
+ interface RoundedCorner {
82
+ /** Where the arc leaves the incoming segment and joins the outgoing one. */
83
+ start: Vector
84
+ end: Vector
85
+ handleIn: Vector
86
+ handleOut: Vector
87
+ }
88
+
89
+ /**
90
+ * The arc rounding `corner` between straight runs from `from` and to `after`, or null when the
91
+ * runs do not turn or have no length. A radius never takes more than half of either run.
92
+ */
93
+ function roundedCorner(
94
+ from: Vector,
95
+ corner: Vector,
96
+ after: Vector,
97
+ radius: number
98
+ ): RoundedCorner | null {
99
+ const inLength = Math.hypot(corner.x - from.x, corner.y - from.y)
100
+ const outLength = Math.hypot(after.x - corner.x, after.y - corner.y)
101
+ if (radius <= 0 || inLength === 0 || outLength === 0) return null
102
+ const inDir = { x: (corner.x - from.x) / inLength, y: (corner.y - from.y) / inLength }
103
+ const outDir = { x: (after.x - corner.x) / outLength, y: (after.y - corner.y) / outLength }
104
+ const turn = Math.acos(Math.max(-1, Math.min(1, inDir.x * outDir.x + inDir.y * outDir.y)))
105
+ if (turn <= 1e-6) return null
106
+ const reach = Math.min(radius * Math.tan(turn / 2), inLength / 2, outLength / 2)
107
+ const handle = arcHandle(turn) * (reach / Math.tan(turn / 2))
108
+ const start = { x: corner.x - inDir.x * reach, y: corner.y - inDir.y * reach }
109
+ const end = { x: corner.x + outDir.x * reach, y: corner.y + outDir.y * reach }
110
+ return {
111
+ start,
112
+ end,
113
+ handleIn: { x: start.x + inDir.x * handle, y: start.y + inDir.y * handle },
114
+ handleOut: { x: end.x - outDir.x * handle, y: end.y - outDir.y * handle }
115
+ }
116
+ }
117
+
118
+ function drawCorner(path: PathSink, corner: RoundedCorner) {
119
+ path.lineTo(corner.start.x, corner.start.y)
120
+ path.cubicTo(
121
+ corner.handleIn.x,
122
+ corner.handleIn.y,
123
+ corner.handleOut.x,
124
+ corner.handleOut.y,
125
+ corner.end.x,
126
+ corner.end.y
127
+ )
128
+ }
129
+
130
+ /**
131
+ * Draws one chain, rounding each vertex where two straight segments meet by its corner radius, or
132
+ * `cornerRadius` when it has none, as Figma rounds a vector's corners. A chain that comes back to
133
+ * where it started is closed, its last corner rounded too.
134
+ */
135
+ function addChainToPath(
136
+ path: PathSink,
137
+ { start: first, segments: chain }: Chain,
138
+ segments: VectorSegment[],
139
+ vertices: VectorVertex[],
140
+ cornerRadius: number
141
+ ): void {
142
+ // The chain's vertices in drawing order; a closed chain ends where it started.
143
+ const order = [first]
144
+ for (const segIdx of chain) {
145
+ const seg = segments[segIdx]
146
+ order.push(seg.start === order[order.length - 1] ? seg.end : seg.start)
147
+ }
148
+ const closed = chain.length > 2 && order[order.length - 1] === first
149
+ const cornerAt = (index: number): RoundedCorner | null => {
150
+ const incoming = segments[chain[index - 1]]
151
+ const outgoing = segments[chain[index % chain.length]]
152
+ if (!isStraight(incoming) || !isStraight(outgoing)) return null
153
+ const corner = vertices[order[index]]
154
+ return roundedCorner(
155
+ vertices[order[index - 1]],
156
+ corner,
157
+ vertices[order[(index % chain.length) + 1]],
158
+ corner.cornerRadius ?? cornerRadius
159
+ )
160
+ }
161
+ const wrap = closed ? cornerAt(chain.length) : null
162
+ const begin = wrap?.end ?? vertices[first]
163
+ path.moveTo(begin.x, begin.y)
164
+ for (let index = 0; index < chain.length; index++) {
165
+ const last = index === chain.length - 1
166
+ const corner = last ? wrap : cornerAt(index + 1)
167
+ if (corner) {
168
+ drawCorner(path, corner)
169
+ continue
170
+ }
171
+ const seg = segments[chain[index]]
172
+ addSegmentDirected(path, seg, vertices, seg.start === order[index])
173
+ }
174
+ if (closed) path.close()
175
+ }
176
+
177
+ export function addOpenSegmentsToPath(
178
+ path: PathSink,
179
+ segments: VectorSegment[],
180
+ vertices: VectorVertex[],
181
+ cornerRadius = 0
182
+ ): void {
183
+ const visited = new Set<number>()
184
+ const chains = buildChains(segments)
185
+
186
+ for (const chain of chains) {
187
+ for (const segIdx of chain.segments) visited.add(segIdx)
188
+ addChainToPath(path, chain, segments, vertices, cornerRadius)
189
+ }
190
+
191
+ for (let i = 0; i < segments.length; i++) {
192
+ if (visited.has(i)) continue
193
+ const seg = segments[i]
194
+ path.moveTo(vertices[seg.start].x, vertices[seg.start].y)
195
+ addSegmentDirected(path, seg, vertices, true)
196
+ }
197
+ }
198
+
199
+ /**
200
+ * A run of connected segments in the order a walk took them, from the vertex it started at, or a
201
+ * lone segment's own start. The start is kept rather than worked out again from the first two
202
+ * segments, which two segments between the same vertices leave ambiguous.
203
+ */
204
+ export interface Chain {
205
+ start: number
206
+ segments: number[]
207
+ }
208
+
209
+ export function buildChains(segments: VectorSegment[]): Chain[] {
210
+ if (segments.length === 0) return []
211
+
212
+ const adj = new Map<number, number[]>()
213
+ for (let i = 0; i < segments.length; i++) {
214
+ const s = segments[i]
215
+ const startList = adj.get(s.start) ?? []
216
+ startList.push(i)
217
+ adj.set(s.start, startList)
218
+
219
+ const endList = adj.get(s.end) ?? []
220
+ endList.push(i)
221
+ adj.set(s.end, endList)
222
+ }
223
+
224
+ const visited = new Set<number>()
225
+ const chains: Chain[] = []
226
+ // Open runs start at their ends; closed loops left over start wherever their first segment does,
227
+ // so every loop is one contour, not one per segment.
228
+ const degree1 = [...adj.entries()].filter(([, segs]) => segs.length === 1).map(([v]) => v)
229
+ const startVertices = [...degree1, ...segments.map((segment) => segment.start)]
230
+
231
+ for (const startVertex of startVertices) {
232
+ let current = startVertex
233
+ const chain: number[] = []
234
+
235
+ for (;;) {
236
+ const segs = adj.get(current)
237
+ if (!segs) break
238
+
239
+ const nextSeg = segs.find((s) => !visited.has(s))
240
+ if (nextSeg === undefined) break
241
+
242
+ visited.add(nextSeg)
243
+ chain.push(nextSeg)
244
+
245
+ const seg = segments[nextSeg]
246
+ current = seg.start === current ? seg.end : seg.start
247
+ }
248
+
249
+ // A lone segment keeps its stored direction, as a dash pattern runs from its start.
250
+ const start = chain.length === 1 ? segments[chain[0]].start : startVertex
251
+ if (chain.length > 0) chains.push({ start, segments: chain })
252
+ }
253
+
254
+ return chains
255
+ }
256
+
257
+ /** A stretch of a network's segments stroked as one contour, so its corners join. */
258
+ export interface StrokeRun {
259
+ /** The vertex the run starts at. */
260
+ start: number
261
+ /** Segment indices in drawing order. */
262
+ segments: number[]
263
+ /** Whether the run comes back to its start, joining there instead of taking caps. */
264
+ closed: boolean
265
+ }
266
+
267
+ export type RunContinuation = (vertex: number, from: number) => number | undefined
268
+
269
+ /**
270
+ * The indices of the segments meeting at each vertex, in segment order; a segment from a vertex to
271
+ * itself is listed there once. `include` leaves out the segments it returns false for.
272
+ */
273
+ export function segmentsByVertex(
274
+ segments: readonly VectorSegment[],
275
+ include?: (index: number) => boolean
276
+ ): Map<number, number[]> {
277
+ const byVertex = new Map<number, number[]>()
278
+ const meet = (vertex: number, index: number) => {
279
+ const list = byVertex.get(vertex)
280
+ if (list) list.push(index)
281
+ else byVertex.set(vertex, [index])
282
+ }
283
+ for (const [index, segment] of segments.entries()) {
284
+ if (include && !include(index)) continue
285
+ meet(segment.start, index)
286
+ if (segment.end !== segment.start) meet(segment.end, index)
287
+ }
288
+ return byVertex
289
+ }
290
+
291
+ /**
292
+ * The direction segment `index` leaves `vertex` in: along its handle there, or with that handle
293
+ * collapsed, toward the far end's control point, as a cubic leaves toward its next distinct
294
+ * control point; a straight segment leaves along its chord.
295
+ */
296
+ export function leavingDirection(
297
+ network: Pick<VectorNetwork, 'vertices' | 'segments'>,
298
+ index: number,
299
+ vertex: number
300
+ ): Vector {
301
+ const segment = network.segments[index]
302
+ const atStart = segment.start === vertex
303
+ const handle = atStart ? segment.tangentStart : segment.tangentEnd
304
+ if (handle.x !== 0 || handle.y !== 0) return handle
305
+ const from = network.vertices[vertex]
306
+ const to = network.vertices[atStart ? segment.end : segment.start]
307
+ const farHandle = atStart ? segment.tangentEnd : segment.tangentStart
308
+ const control = { x: to.x + farHandle.x - from.x, y: to.y + farHandle.y - from.y }
309
+ if (control.x !== 0 || control.y !== 0) return control
310
+ return { x: to.x - from.x, y: to.y - from.y }
311
+ }
312
+
313
+ /**
314
+ * The pairs of segments a stroke may join at each vertex: the two at a vertex exactly two meet at,
315
+ * and with several regions, also each two region edges next to each other around a vertex,
316
+ * which region strokes join where the gap between them is unfilled, open segments aside.
317
+ */
318
+ export function joinedSegmentPairs(
319
+ network: VectorNetwork
320
+ ): [vertex: number, a: number, b: number][] {
321
+ const pairs: [number, number, number][] = []
322
+ for (const [vertex, indices] of segmentsByVertex(network.segments)) {
323
+ if (indices.length === 2) pairs.push([vertex, indices[0], indices[1]])
324
+ }
325
+ if (network.regions.length < 2) return pairs
326
+ const bounding = new Set(network.regions.flatMap((region) => region.loops.flat()))
327
+ const edges = segmentsByVertex(
328
+ network.segments,
329
+ (index) => bounding.has(index) && network.segments[index].start !== network.segments[index].end
330
+ )
331
+ for (const [vertex, indices] of edges) {
332
+ if (indices.length < 2) continue
333
+ const angle = (index: number) => {
334
+ const direction = leavingDirection(network, index, vertex)
335
+ return Math.atan2(direction.y, direction.x)
336
+ }
337
+ const around = indices.toSorted((a, b) => angle(a) - angle(b))
338
+ for (const [position, index] of around.entries()) {
339
+ pairs.push([vertex, index, around[(position + 1) % around.length]])
340
+ }
341
+ }
342
+ return pairs
343
+ }
344
+
345
+ /**
346
+ * Segments chained into runs as Figma strokes a network: by default a run passes through every
347
+ * vertex exactly two segments meet at, so its corners take the stroke's join and only its open
348
+ * ends take caps. Runs stop at open ends and at junctions where three or more segments meet, unless
349
+ * `continuation` names a segment to go on with there.
350
+ */
351
+ export function strokeRuns(segments: VectorSegment[], continuation?: RunContinuation): StrokeRun[] {
352
+ const incident = segmentsByVertex(segments)
353
+ const used = new Set<number>()
354
+ const across = (index: number, vertex: number) =>
355
+ segments[index].start === vertex ? segments[index].end : segments[index].start
356
+ const throughPairs: RunContinuation = (vertex, from) => {
357
+ const meeting = incident.get(vertex)
358
+ if (meeting?.length !== 2) {
359
+ return undefined
360
+ }
361
+ return meeting[0] === from ? meeting[1] : meeting[0]
362
+ }
363
+ const continueAt = continuation ?? throughPairs
364
+ const extend = (vertex: number, from: number, add: (index: number) => void) => {
365
+ for (;;) {
366
+ const next = continueAt(vertex, from)
367
+ if (next === undefined || used.has(next)) {
368
+ return vertex
369
+ }
370
+ used.add(next)
371
+ add(next)
372
+ vertex = across(next, vertex)
373
+ from = next
374
+ }
375
+ }
376
+
377
+ const runs: StrokeRun[] = []
378
+ for (const [first, segment] of segments.entries()) {
379
+ if (used.has(first)) continue
380
+ used.add(first)
381
+ const run = [first]
382
+ const end = extend(segment.end, first, (index) => run.push(index))
383
+ if (end === segment.start) {
384
+ runs.push({ start: segment.start, segments: run, closed: true })
385
+ continue
386
+ }
387
+ const start = extend(segment.start, first, (index) => run.unshift(index))
388
+ // A loop through a junction stops at it from both sides; it still closes there, whichever
389
+ // of its segments is stored first.
390
+ runs.push({ start, segments: run, closed: start === end })
391
+ }
392
+ return runs
393
+ }
394
+
395
+ /** Emits a stroke run as one contour, closed when the run comes back to its start. */
396
+ export function addStrokeRunToPath(
397
+ path: PathSink,
398
+ run: StrokeRun,
399
+ segments: VectorSegment[],
400
+ vertices: VectorVertex[]
401
+ ): void {
402
+ let current = run.start
403
+ path.moveTo(vertices[current].x, vertices[current].y)
404
+ for (const index of run.segments) {
405
+ const segment = segments[index]
406
+ const forward = segment.start === current
407
+ addSegmentDirected(path, segment, vertices, forward)
408
+ current = forward ? segment.end : segment.start
409
+ }
410
+ if (run.closed) path.close()
411
+ }
@@ -0,0 +1,50 @@
1
+ import type { PolygonOutlineCommand } from '../polygon'
2
+
3
+ /** Anything path commands can be written into: a CanvasKit path, SVG path data, a blob encoder. */
4
+ export interface PathSink {
5
+ moveTo(x: number, y: number): unknown
6
+ lineTo(x: number, y: number): unknown
7
+ cubicTo(x1: number, y1: number, x2: number, y2: number, x3: number, y3: number): unknown
8
+ close(): unknown
9
+ }
10
+
11
+ /** A coordinate rounded to two decimals, as exported path data writes it. */
12
+ export function roundCoordinate(value: number): number {
13
+ return Math.round(value * 100) / 100
14
+ }
15
+
16
+ /** Path commands written as SVG path data into `parts`, each coordinate written by `format`. */
17
+ export function svgPathSink(
18
+ parts: string[],
19
+ format: (value: number) => number = roundCoordinate
20
+ ): PathSink {
21
+ const point = (x: number, y: number) => `${format(x)} ${format(y)}`
22
+ return {
23
+ moveTo: (x, y) => parts.push(`M${point(x, y)}`),
24
+ lineTo: (x, y) => parts.push(`L${point(x, y)}`),
25
+ cubicTo: (x1, y1, x2, y2, x, y) =>
26
+ parts.push(`C${point(x1, y1)} ${point(x2, y2)} ${point(x, y)}`),
27
+ close: () => parts.push('Z')
28
+ }
29
+ }
30
+
31
+ /** Outline commands written into `sink`. */
32
+ export function replayOutline(commands: readonly PolygonOutlineCommand[], sink: PathSink): void {
33
+ for (const command of commands) {
34
+ if (command.type === 'M') sink.moveTo(command.x, command.y)
35
+ else if (command.type === 'L') sink.lineTo(command.x, command.y)
36
+ else if (command.type === 'C')
37
+ sink.cubicTo(command.x1, command.y1, command.x2, command.y2, command.x, command.y)
38
+ else sink.close()
39
+ }
40
+ }
41
+
42
+ /** Outline commands as SVG path data, each coordinate written by `format`. */
43
+ export function outlineSVGPath(
44
+ commands: readonly PolygonOutlineCommand[],
45
+ format: (value: number) => number = roundCoordinate
46
+ ): string {
47
+ const parts: string[] = []
48
+ replayOutline(commands, svgPathSink(parts, format))
49
+ return parts.join('')
50
+ }