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

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 (283) hide show
  1. package/dist/arrays.d.ts +17 -0
  2. package/dist/arrays.d.ts.map +1 -0
  3. package/dist/arrays.js +38 -0
  4. package/dist/arrays.js.map +1 -0
  5. package/dist/buffered-events.js +1 -1
  6. package/dist/buffered-events.js.map +1 -1
  7. package/dist/clipping.d.ts +7 -1
  8. package/dist/clipping.d.ts.map +1 -1
  9. package/dist/clipping.js +23 -19
  10. package/dist/clipping.js.map +1 -1
  11. package/dist/components/index.d.ts +2 -2
  12. package/dist/components/index.js +2 -2
  13. package/dist/components/properties.d.ts +1 -3
  14. package/dist/components/properties.d.ts.map +1 -1
  15. package/dist/components/properties.js +2 -9
  16. package/dist/components/properties.js.map +1 -1
  17. package/dist/corners.d.ts +22 -0
  18. package/dist/corners.d.ts.map +1 -0
  19. package/dist/corners.js +52 -0
  20. package/dist/corners.js.map +1 -0
  21. package/dist/css/grid.d.ts +4 -1
  22. package/dist/css/grid.d.ts.map +1 -1
  23. package/dist/css/grid.js +16 -7
  24. package/dist/css/grid.js.map +1 -1
  25. package/dist/css/index.d.ts +2 -2
  26. package/dist/css/index.js +2 -2
  27. package/dist/css/values.d.ts +6 -1
  28. package/dist/css/values.d.ts.map +1 -1
  29. package/dist/font-style.d.ts +2 -1
  30. package/dist/font-style.d.ts.map +1 -1
  31. package/dist/font-style.js +31 -6
  32. package/dist/font-style.js.map +1 -1
  33. package/dist/geometry.d.ts +68 -3
  34. package/dist/geometry.d.ts.map +1 -1
  35. package/dist/geometry.js +155 -5
  36. package/dist/geometry.js.map +1 -1
  37. package/dist/gradient.d.ts +9 -2
  38. package/dist/gradient.d.ts.map +1 -1
  39. package/dist/gradient.js +29 -1
  40. package/dist/gradient.js.map +1 -1
  41. package/dist/graph.d.ts +4 -0
  42. package/dist/graph.d.ts.map +1 -1
  43. package/dist/graph.js +30 -4
  44. package/dist/graph.js.map +1 -1
  45. package/dist/group-bounds.d.ts.map +1 -1
  46. package/dist/group-bounds.js +4 -3
  47. package/dist/group-bounds.js.map +1 -1
  48. package/dist/guides.d.ts +11 -2
  49. package/dist/guides.d.ts.map +1 -1
  50. package/dist/guides.js +30 -1
  51. package/dist/guides.js.map +1 -1
  52. package/dist/hit-test.d.ts.map +1 -1
  53. package/dist/hit-test.js +57 -5
  54. package/dist/hit-test.js.map +1 -1
  55. package/dist/icons/fit.d.ts +17 -0
  56. package/dist/icons/fit.d.ts.map +1 -0
  57. package/dist/icons/fit.js +21 -0
  58. package/dist/icons/fit.js.map +1 -0
  59. package/dist/icons/glyph.d.ts.map +1 -1
  60. package/dist/icons/glyph.js +5 -4
  61. package/dist/icons/glyph.js.map +1 -1
  62. package/dist/icons/index.d.ts +2 -1
  63. package/dist/icons/index.js +2 -1
  64. package/dist/images.d.ts +14 -4
  65. package/dist/images.d.ts.map +1 -1
  66. package/dist/images.js +45 -5
  67. package/dist/images.js.map +1 -1
  68. package/dist/instances/assignments.js +39 -6
  69. package/dist/instances/assignments.js.map +1 -1
  70. package/dist/instances/index.d.ts.map +1 -1
  71. package/dist/instances/index.js +39 -2
  72. package/dist/instances/index.js.map +1 -1
  73. package/dist/instances/kept-overrides.js +2 -2
  74. package/dist/instances/kept-overrides.js.map +1 -1
  75. package/dist/instances/matching.js +68 -0
  76. package/dist/instances/matching.js.map +1 -0
  77. package/dist/instances/overrides.d.ts +4 -1
  78. package/dist/instances/overrides.d.ts.map +1 -1
  79. package/dist/instances/overrides.js +13 -1
  80. package/dist/instances/overrides.js.map +1 -1
  81. package/dist/instances/protection.js +67 -0
  82. package/dist/instances/protection.js.map +1 -0
  83. package/dist/instances/swap.js +7 -5
  84. package/dist/instances/swap.js.map +1 -1
  85. package/dist/instances/sync.js +10 -116
  86. package/dist/instances/sync.js.map +1 -1
  87. package/dist/layout/guides.d.ts +17 -1
  88. package/dist/layout/guides.d.ts.map +1 -1
  89. package/dist/layout/guides.js +31 -11
  90. package/dist/layout/guides.js.map +1 -1
  91. package/dist/layout/index.d.ts +4 -2
  92. package/dist/layout/index.js +4 -2
  93. package/dist/layout/limits.d.ts +40 -0
  94. package/dist/layout/limits.d.ts.map +1 -0
  95. package/dist/layout/limits.js +54 -0
  96. package/dist/layout/limits.js.map +1 -0
  97. package/dist/layout/sizing.d.ts.map +1 -1
  98. package/dist/layout/sizing.js +0 -1
  99. package/dist/layout/sizing.js.map +1 -1
  100. package/dist/layout/spacing.d.ts +10 -0
  101. package/dist/layout/spacing.d.ts.map +1 -0
  102. package/dist/layout/spacing.js +12 -0
  103. package/dist/layout/spacing.js.map +1 -0
  104. package/dist/masks.d.ts +28 -0
  105. package/dist/masks.d.ts.map +1 -0
  106. package/dist/masks.js +53 -0
  107. package/dist/masks.js.map +1 -0
  108. package/dist/matrix.d.ts +2 -1
  109. package/dist/matrix.d.ts.map +1 -1
  110. package/dist/matrix.js +14 -1
  111. package/dist/matrix.js.map +1 -1
  112. package/dist/node-defaults.d.ts +9 -1
  113. package/dist/node-defaults.d.ts.map +1 -1
  114. package/dist/node-defaults.js +16 -1
  115. package/dist/node-defaults.js.map +1 -1
  116. package/dist/outline/flatten.d.ts.map +1 -1
  117. package/dist/outline/flatten.js +24 -17
  118. package/dist/outline/flatten.js.map +1 -1
  119. package/dist/outline/hit.d.ts +2 -8
  120. package/dist/outline/hit.d.ts.map +1 -1
  121. package/dist/outline/hit.js +16 -25
  122. package/dist/outline/hit.js.map +1 -1
  123. package/dist/outline/index.d.ts +3 -3
  124. package/dist/outline/index.js +3 -3
  125. package/dist/outline/shapes.d.ts +3 -1
  126. package/dist/outline/shapes.d.ts.map +1 -1
  127. package/dist/outline/shapes.js +18 -39
  128. package/dist/outline/shapes.js.map +1 -1
  129. package/dist/paints/index.d.ts +3 -1
  130. package/dist/paints/index.js +3 -1
  131. package/dist/paints/pattern.d.ts +13 -0
  132. package/dist/paints/pattern.d.ts.map +1 -0
  133. package/dist/paints/pattern.js +47 -0
  134. package/dist/paints/pattern.js.map +1 -0
  135. package/dist/paints/shows.d.ts +11 -0
  136. package/dist/paints/shows.d.ts.map +1 -0
  137. package/dist/paints/shows.js +16 -0
  138. package/dist/paints/shows.js.map +1 -0
  139. package/dist/paths/arc.d.ts +32 -0
  140. package/dist/paths/arc.d.ts.map +1 -0
  141. package/dist/paths/arc.js +49 -0
  142. package/dist/paths/arc.js.map +1 -0
  143. package/dist/paths/index.d.ts +5 -0
  144. package/dist/paths/index.js +5 -0
  145. package/dist/paths/network.d.ts +43 -0
  146. package/dist/paths/network.d.ts.map +1 -0
  147. package/dist/paths/network.js +249 -0
  148. package/dist/paths/network.js.map +1 -0
  149. package/dist/paths/sink.d.ts +20 -0
  150. package/dist/paths/sink.d.ts.map +1 -0
  151. package/dist/paths/sink.js +32 -0
  152. package/dist/paths/sink.js.map +1 -0
  153. package/dist/paths/smooth-corners.d.ts +25 -0
  154. package/dist/paths/smooth-corners.d.ts.map +1 -0
  155. package/dist/paths/smooth-corners.js +196 -0
  156. package/dist/paths/smooth-corners.js.map +1 -0
  157. package/dist/plugin-data/fields.d.ts +4 -0
  158. package/dist/plugin-data/fields.d.ts.map +1 -1
  159. package/dist/plugin-data/fields.js +2 -0
  160. package/dist/plugin-data/fields.js.map +1 -1
  161. package/dist/polygon.d.ts +1 -3
  162. package/dist/polygon.d.ts.map +1 -1
  163. package/dist/polygon.js +1 -16
  164. package/dist/polygon.js.map +1 -1
  165. package/dist/resize.d.ts.map +1 -1
  166. package/dist/resize.js +5 -5
  167. package/dist/resize.js.map +1 -1
  168. package/dist/scaling/node.d.ts.map +1 -1
  169. package/dist/scaling/node.js +25 -3
  170. package/dist/scaling/node.js.map +1 -1
  171. package/dist/text/auto-resize.d.ts +15 -0
  172. package/dist/text/auto-resize.d.ts.map +1 -0
  173. package/dist/text/auto-resize.js +23 -0
  174. package/dist/text/auto-resize.js.map +1 -0
  175. package/dist/text/index.d.ts +4 -3
  176. package/dist/text/index.js +4 -3
  177. package/dist/text/lengths.d.ts +6 -1
  178. package/dist/text/lengths.d.ts.map +1 -1
  179. package/dist/text/lengths.js +10 -1
  180. package/dist/text/lengths.js.map +1 -1
  181. package/dist/text/overflow.d.ts +14 -5
  182. package/dist/text/overflow.d.ts.map +1 -1
  183. package/dist/text/overflow.js +24 -6
  184. package/dist/text/overflow.js.map +1 -1
  185. package/dist/text/path.js +10 -0
  186. package/dist/text/path.js.map +1 -0
  187. package/dist/text/picture.js +5 -5
  188. package/dist/text/picture.js.map +1 -1
  189. package/dist/transfer/dependencies.d.ts.map +1 -1
  190. package/dist/transfer/dependencies.js +3 -2
  191. package/dist/transfer/dependencies.js.map +1 -1
  192. package/dist/transfer/nodes.js +2 -1
  193. package/dist/transfer/nodes.js.map +1 -1
  194. package/dist/types.d.ts +21 -8
  195. package/dist/types.d.ts.map +1 -1
  196. package/dist/types.js.map +1 -1
  197. package/dist/variables/index.d.ts +4 -3
  198. package/dist/variables/index.d.ts.map +1 -1
  199. package/dist/variables/index.js +4 -3
  200. package/dist/variables/index.js.map +1 -1
  201. package/dist/variables/lookup.d.ts +20 -0
  202. package/dist/variables/lookup.d.ts.map +1 -0
  203. package/dist/variables/lookup.js +34 -0
  204. package/dist/variables/lookup.js.map +1 -0
  205. package/dist/variables/paints.d.ts +35 -4
  206. package/dist/variables/paints.d.ts.map +1 -1
  207. package/dist/variables/paints.js +67 -16
  208. package/dist/variables/paints.js.map +1 -1
  209. package/dist/variables/resolution.d.ts +3 -1
  210. package/dist/variables/resolution.d.ts.map +1 -1
  211. package/dist/variables/resolution.js +8 -3
  212. package/dist/variables/resolution.js.map +1 -1
  213. package/dist/variables/subtree.js +1 -1
  214. package/dist/variables/subtree.js.map +1 -1
  215. package/dist/vector-network.d.ts.map +1 -1
  216. package/dist/vector-network.js +12 -76
  217. package/dist/vector-network.js.map +1 -1
  218. package/package.json +25 -1
  219. package/src/arrays.ts +37 -0
  220. package/src/buffered-events.ts +2 -1
  221. package/src/clipping.ts +11 -8
  222. package/src/components/properties.ts +1 -13
  223. package/src/corners.ts +93 -0
  224. package/src/css/grid.ts +21 -7
  225. package/src/css/index.ts +1 -1
  226. package/src/font-style.ts +38 -4
  227. package/src/geometry.ts +219 -6
  228. package/src/gradient.ts +23 -1
  229. package/src/graph.ts +22 -4
  230. package/src/group-bounds.ts +7 -3
  231. package/src/guides.ts +26 -1
  232. package/src/hit-test.ts +68 -10
  233. package/src/icons/fit.ts +24 -0
  234. package/src/icons/glyph.ts +5 -4
  235. package/src/icons/index.ts +1 -0
  236. package/src/images.ts +67 -7
  237. package/src/instances/assignments.ts +65 -7
  238. package/src/instances/index.ts +53 -4
  239. package/src/instances/kept-overrides.ts +2 -5
  240. package/src/instances/matching.ts +101 -0
  241. package/src/instances/overrides.ts +18 -0
  242. package/src/instances/protection.ts +103 -0
  243. package/src/instances/swap.ts +11 -9
  244. package/src/instances/sync.ts +11 -184
  245. package/src/layout/guides.ts +42 -9
  246. package/src/layout/index.ts +2 -0
  247. package/src/layout/limits.ts +64 -0
  248. package/src/layout/sizing.ts +0 -2
  249. package/src/layout/spacing.ts +11 -0
  250. package/src/masks.ts +51 -0
  251. package/src/matrix.ts +15 -2
  252. package/src/node-defaults.ts +23 -3
  253. package/src/outline/flatten.ts +30 -18
  254. package/src/outline/hit.ts +16 -33
  255. package/src/outline/index.ts +2 -2
  256. package/src/outline/shapes.ts +19 -36
  257. package/src/paints/index.ts +2 -0
  258. package/src/paints/pattern.ts +45 -0
  259. package/src/paints/shows.ts +13 -0
  260. package/src/paths/arc.ts +59 -0
  261. package/src/paths/index.ts +4 -0
  262. package/src/paths/network.ts +334 -0
  263. package/src/paths/sink.ts +50 -0
  264. package/src/paths/smooth-corners.ts +356 -0
  265. package/src/plugin-data/fields.ts +2 -0
  266. package/src/polygon.ts +0 -17
  267. package/src/resize.ts +6 -7
  268. package/src/scaling/node.ts +29 -3
  269. package/src/text/auto-resize.ts +25 -0
  270. package/src/text/index.ts +1 -0
  271. package/src/text/lengths.ts +11 -0
  272. package/src/text/overflow.ts +32 -6
  273. package/src/text/path.ts +13 -0
  274. package/src/text/picture.ts +5 -5
  275. package/src/transfer/dependencies.ts +3 -2
  276. package/src/transfer/nodes.ts +2 -1
  277. package/src/types.ts +25 -8
  278. package/src/variables/index.ts +1 -0
  279. package/src/variables/lookup.ts +31 -0
  280. package/src/variables/paints.ts +101 -20
  281. package/src/variables/resolution.ts +14 -2
  282. package/src/variables/subtree.ts +1 -1
  283. package/src/vector-network.ts +17 -138
@@ -2,10 +2,12 @@ import { compact } from 'es-toolkit'
2
2
 
3
3
  import { arrowCapOverflow, isArrowCap, lineStrokeEnds } from '../arrow-caps'
4
4
  import { getWorldMatrix } from '../coordinate'
5
+ import { strokeWeightOf } from '../geometry'
5
6
  import type { SceneGraph } from '../graph'
6
7
  import Matrix from '../matrix'
8
+ import { paintShows } from '../paints/shows'
7
9
  import type { Vector } from '../primitives'
8
- import type { Fill, SceneNode, Stroke } from '../types'
10
+ import type { SceneNode, Stroke } from '../types'
9
11
  import { hitsArrowHeads } from './heads'
10
12
  import { containsByWinding, distanceToLines, distanceToSegment } from './point'
11
13
  import { shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'
@@ -16,47 +18,28 @@ const EDGE_SAMPLES = 8
16
18
  /** How far a flattened curve may stray from the shape when nothing tighter is asked for. */
17
19
  const DEFAULT_TOLERANCE = 0.25
18
20
 
19
- /**
20
- * Whether a paint makes its layer clickable, as in Figma: a visible paint that shows something.
21
- * A paint at 0% opacity, or a gradient whose stops are all transparent, does not; 1% does, and
22
- * the layer's own opacity does not matter.
23
- */
24
- export function isHitPaint(paint: Fill): boolean {
25
- if (!paint.visible || !(paint.opacity > 0)) return false
26
- if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0)
27
- if (paint.type === 'SOLID') return paint.color.a > 0
28
- return true
29
- }
30
-
31
21
  function insideArea(area: OutlineArea, point: Vector): boolean {
32
22
  return containsByWinding(area.contours, area.windingRule, point)
33
23
  }
34
24
 
35
- /** A stroke's weight; with independent borders the widest side, for layers without per-side lines. */
36
- function strokeWeightOf(node: SceneNode, stroke: Stroke): number {
37
- if (node.independentStrokeWeights) {
38
- return Math.max(
39
- node.borderTopWeight,
40
- node.borderRightWeight,
41
- node.borderBottomWeight,
42
- node.borderLeftWeight
43
- )
44
- }
45
- return Number.isFinite(stroke.weight) ? stroke.weight : node.strokeWeight
25
+ /** A stroke's weight, the widest side with sides of their own, for layers without per-side lines. */
26
+ function hitWeight(node: SceneNode, stroke: Stroke): number {
27
+ const weight = strokeWeightOf(node, stroke)
28
+ return Number.isFinite(weight) ? weight : node.strokeWeight
46
29
  }
47
30
 
48
31
  /** The farthest a layer's clickable strokes reach outside its box, in its own units. */
49
32
  export function strokeReach(node: SceneNode): number {
50
33
  let reach = 0
51
34
  for (const stroke of node.strokes) {
52
- if (!isHitPaint(stroke)) continue
53
- const weight = strokeWeightOf(node, stroke)
35
+ if (!paintShows(stroke)) continue
36
+ const weight = hitWeight(node, stroke)
54
37
  // A line's stroke lies a full weight to one side of it.
55
38
  const side = node.type === 'LINE' || stroke.align === 'OUTSIDE'
56
39
  reach = Math.max(reach, side ? weight : weight / 2)
57
40
  }
58
41
  // Arrow heads reach past the stroke.
59
- const hitStrokes = node.strokes.filter(isHitPaint)
42
+ const hitStrokes = node.strokes.filter(paintShows)
60
43
  return Math.max(reach, arrowCapOverflow(hitStrokes, node.strokeCap, node.vectorNetwork))
61
44
  }
62
45
 
@@ -306,7 +289,7 @@ function strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number):
306
289
  if (probe.strokeAreaWithin) return probe.strokeAreaWithin(pad)
307
290
  const align = probe.closed && !probe.centered ? stroke.align : 'CENTER'
308
291
  if (probe.strokeOnEdge) {
309
- const weight = strokeWeightOf(node, stroke)
292
+ const weight = hitWeight(node, stroke)
310
293
  return probe.edgeWithin(strokeReachAt(align, weight, probe.inside) + pad)
311
294
  }
312
295
  // A flat cap ends the stroke at the line's end; round and square caps paint past it.
@@ -321,7 +304,7 @@ function strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number):
321
304
  }
322
305
  if (node.type === 'LINE' && probe.local) {
323
306
  // Figma strokes a line a full weight to one side, along the path the renderer draws.
324
- const weight = strokeWeightOf(node, stroke)
307
+ const weight = hitWeight(node, stroke)
325
308
  if (!(weight > 0)) return false
326
309
  const { start, end } = lineStrokeEnds(node.width, node.height, weight, cap)
327
310
  if (reaches(measureLine([start, end], [true, true], probe.local), weight / 2)) return true
@@ -336,11 +319,11 @@ function strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number):
336
319
  }
337
320
  const network = node.type === 'VECTOR' ? node.vectorNetwork : null
338
321
  if (network && probe.local) {
339
- const weight = strokeWeightOf(node, stroke)
322
+ const weight = hitWeight(node, stroke)
340
323
  if (weight > 0 && hitsArrowHeads(network, cap, weight, probe.local, pad)) return true
341
324
  }
342
325
  return probe.lines.some((line, index) => {
343
- const weight = probe.lineWeights?.[index] ?? strokeWeightOf(node, stroke)
326
+ const weight = probe.lineWeights?.[index] ?? hitWeight(node, stroke)
344
327
  // A side with no weight paints no border.
345
328
  if (!(weight > 0)) return false
346
329
  return reaches(line, strokeReachAt(align, weight, probe.inside))
@@ -363,7 +346,7 @@ export function hitsShape(
363
346
  const tolerance = pad > 0 ? Math.min(DEFAULT_TOLERANCE, pad / 4) : DEFAULT_TOLERANCE
364
347
  const probe = probeNode(graph, node, px, py, tolerance)
365
348
  if (!probe) return null
366
- const fills = node.fills.filter(isHitPaint)
349
+ const fills = node.fills.filter(paintShows)
367
350
  if (fills.length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true
368
- return node.strokes.some((stroke) => isHitPaint(stroke) && strokeHits(node, stroke, probe, pad))
351
+ return node.strokes.some((stroke) => paintShows(stroke) && strokeHits(node, stroke, probe, pad))
369
352
  }
@@ -1,4 +1,4 @@
1
- export { hitsShape, isHitPaint, strokeReach } from './hit'
2
- export { shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'
1
+ export { hitsShape, strokeReach } from './hit'
2
+ export { isWholeEllipse, shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'
3
3
  export { blobContours, loopPoints, segmentPoints } from './flatten'
4
4
  export { containsByWinding, distanceToLines, distanceToSegment, windingNumber } from './point'
@@ -1,3 +1,6 @@
1
+ import { nodeCornerRadii } from '../corners'
2
+ import { borderWeights, sweepsWholeTurn } from '../geometry'
3
+ import { arcOutline } from '../paths/arc'
1
4
  import { polygonOutline } from '../polygon'
2
5
  import type { Vector } from '../primitives'
3
6
  import type { SceneNode, WindingRule } from '../types'
@@ -47,17 +50,9 @@ function fromContours(contours: Vector[][], windingRule: WindingRule = 'NONZERO'
47
50
  return { areas: [{ windingRule, contours }], lines: contours.map(closed) }
48
51
  }
49
52
 
50
- function cornerRadii(node: SceneNode): [number, number, number, number] {
51
- const radii: [number, number, number, number] = node.independentCorners
52
- ? [node.topLeftRadius, node.topRightRadius, node.bottomRightRadius, node.bottomLeftRadius]
53
- : [node.cornerRadius, node.cornerRadius, node.cornerRadius, node.cornerRadius]
54
- const limit = Math.min(node.width, node.height) / 2
55
- return radii.map((radius) => Math.max(0, Math.min(radius, limit))) as typeof radii
56
- }
57
-
58
53
  function rectangleContour(node: SceneNode, tolerance: number): Vector[] {
59
54
  const { width: w, height: h } = node
60
- const [tl, tr, br, bl] = cornerRadii(node)
55
+ const [tl, tr, br, bl] = nodeCornerRadii(node)
61
56
  const corner = (cx: number, cy: number, r: number, from: number): Vector[] =>
62
57
  r > 0 ? arcPoints(cx, cy, r, r, from, from + Math.PI / 2, tolerance) : [{ x: cx, y: cy }]
63
58
  return [
@@ -78,7 +73,7 @@ function rectangleSides(
78
73
  tolerance: number
79
74
  ): Pick<ShapeOutline, 'lines' | 'lineWeights'> {
80
75
  const { width: w, height: h } = node
81
- const [tl, tr, br, bl] = cornerRadii(node)
76
+ const [tl, tr, br, bl] = nodeCornerRadii(node)
82
77
  const quarter = Math.PI / 4
83
78
  const arc = (cx: number, cy: number, r: number, from: number): Vector[] =>
84
79
  r > 0 ? arcPoints(cx, cy, r, r, from, from + quarter, tolerance) : [{ x: cx, y: cy }]
@@ -89,38 +84,26 @@ function rectangleSides(
89
84
  [...arc(w - br, h - br, br, quarter), ...arc(bl, h - bl, bl, 2 * quarter)],
90
85
  [...arc(bl, h - bl, bl, 3 * quarter), ...arc(tl, tl, tl, 4 * quarter)]
91
86
  ],
92
- lineWeights: [
93
- node.borderTopWeight,
94
- node.borderRightWeight,
95
- node.borderBottomWeight,
96
- node.borderLeftWeight
97
- ]
87
+ lineWeights: [...borderWeights(node)]
98
88
  }
99
89
  }
100
90
 
101
- const FULL_TURN = Math.PI * 2
91
+ /** Whether a layer is an unbroken ellipse: an ELLIPSE whose arc, if any, is a whole turn with no hole. */
92
+ export function isWholeEllipse(node: Pick<SceneNode, 'type' | 'arcData'>): boolean {
93
+ if (node.type !== 'ELLIPSE') return false
94
+ const arc = node.arcData
95
+ return !arc || (arc.innerRadius <= 0 && sweepsWholeTurn(arc.startingAngle, arc.endingAngle))
96
+ }
102
97
 
103
98
  function ellipseContours(node: SceneNode, tolerance: number): Vector[][] {
104
- const rx = node.width / 2
105
- const ry = node.height / 2
106
- const arc = node.arcData
107
- const start = arc?.startingAngle ?? 0
108
- const end = arc?.endingAngle ?? FULL_TURN
109
- const inner = arc?.innerRadius ?? 0
110
- const whole = Math.abs(end - start) >= FULL_TURN - 1e-6
111
- const outer = arcPoints(rx, ry, rx, ry, start, whole ? start + FULL_TURN : end, tolerance)
112
- if (inner <= 0) return [whole ? outer : [{ x: rx, y: ry }, ...outer]]
113
- const hole = arcPoints(
114
- rx,
115
- ry,
116
- rx * inner,
117
- ry * inner,
118
- whole ? start + FULL_TURN : end,
119
- start,
120
- tolerance
99
+ const { cx, cy, contours } = arcOutline(node)
100
+ return contours.map((contour) =>
101
+ contour.flatMap((step) =>
102
+ step.type === 'centre'
103
+ ? [{ x: cx, y: cy }]
104
+ : arcPoints(cx, cy, step.rx, step.ry, step.start, step.start + step.sweep, tolerance)
105
+ )
121
106
  )
122
- // A whole ring is two loops wound opposite ways; a part ring is one outline across its ends.
123
- return whole ? [outer, hole] : [[...outer, ...hole]]
124
107
  }
125
108
 
126
109
  function vectorOutline(node: SceneNode, tolerance: number): ShapeOutline | null {
@@ -1 +1,3 @@
1
+ export * from './pattern'
1
2
  export * from './schema'
3
+ export * from './shows'
@@ -0,0 +1,45 @@
1
+ import type { Rect, Vector } from '../primitives'
2
+ import type { Fill, SceneNode } from '../types'
3
+
4
+ /** Where a pattern's source repeats in one tile: the tile, the source's scale, and its copies. */
5
+ export interface PatternTileLayout {
6
+ rect: Rect
7
+ scale: number
8
+ positions: Vector[]
9
+ }
10
+
11
+ function patternAlignmentOffset(
12
+ alignment: Fill['horizontalAlignment'],
13
+ gap: number,
14
+ sourceSize: number,
15
+ axis: 'x' | 'y'
16
+ ): number {
17
+ if (alignment === 'CENTER') return axis === 'x' ? -gap / 2 : -sourceSize / 2
18
+ if (alignment === 'END') return -gap
19
+ return 0
20
+ }
21
+
22
+ export function patternTileLayout(
23
+ source: Pick<SceneNode, 'width' | 'height'>,
24
+ fill: Fill
25
+ ): PatternTileLayout {
26
+ const scale = fill.scale && fill.scale > 0 ? fill.scale : 1
27
+ const spacing = fill.patternSpacing ?? { x: 0, y: 0 }
28
+ const scaledWidth = source.width * scale
29
+ const scaledHeight = source.height * scale
30
+ const gapX = scaledWidth * spacing.x
31
+ const gapY = scaledHeight * spacing.y
32
+ const width = scaledWidth + gapX
33
+ const height = scaledHeight + gapY
34
+ const x = patternAlignmentOffset(fill.horizontalAlignment, gapX, scaledWidth, 'x')
35
+ const y = patternAlignmentOffset(fill.verticalAlignment, gapY, scaledHeight, 'y')
36
+ const positions = [{ x, y }]
37
+
38
+ if (fill.patternTileType === 'HORIZONTAL_HEXAGONAL') {
39
+ positions.push({ x: x + width / 2, y: y + height / 2 })
40
+ } else if (fill.patternTileType === 'VERTICAL_HEXAGONAL') {
41
+ positions.push({ x: x + width / 2, y: y - height / 2 })
42
+ }
43
+
44
+ return { rect: { x: 0, y: 0, width, height }, scale, positions }
45
+ }
@@ -0,0 +1,13 @@
1
+ import type { Fill } from '../types'
2
+
3
+ /**
4
+ * Whether a paint shows anything: visible, above 0% opacity, and not wholly transparent in its own
5
+ * color or every gradient stop. Figma casts no shadow from a paint that does not, counts it in no
6
+ * render bounds, and does not click through it to its layer (Figma desktop, live).
7
+ */
8
+ export function paintShows(paint: Fill): boolean {
9
+ if (!paint.visible || !(paint.opacity > 0)) return false
10
+ if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0)
11
+ if (paint.type === 'SOLID') return paint.color.a > 0
12
+ return true
13
+ }
@@ -0,0 +1,59 @@
1
+ import { sweepsWholeTurn } from '../geometry'
2
+ import type { SceneNode } from '../types'
3
+
4
+ const FULL_TURN = Math.PI * 2
5
+
6
+ /** A sweep around the ellipse's centre, from `start` by `sweep` radians, negative to turn back. */
7
+ export interface ArcSweep {
8
+ type: 'arc'
9
+ rx: number
10
+ ry: number
11
+ start: number
12
+ sweep: number
13
+ }
14
+
15
+ /** A step to the ellipse's centre, as a pie slice's sides run. */
16
+ export interface ArcCentre {
17
+ type: 'centre'
18
+ }
19
+
20
+ export type ArcStep = ArcSweep | ArcCentre
21
+
22
+ /**
23
+ * An ellipse or arc as the canvas, SVG export and hit testing draw it: contours of sweeps around
24
+ * its centre, each closed. A whole turn is one sweep, and with a hole a second sweep wound back,
25
+ * so a nonzero fill leaves the hole empty; a part ring is one contour whose ends run straight
26
+ * across the ring; a pie slice runs its sweep, then to the centre.
27
+ */
28
+ export interface ArcOutline {
29
+ cx: number
30
+ cy: number
31
+ contours: ArcStep[][]
32
+ }
33
+
34
+ const WHOLE_ELLIPSE = { startingAngle: 0, endingAngle: FULL_TURN, innerRadius: 0 }
35
+
36
+ export function arcOutline(node: Pick<SceneNode, 'width' | 'height' | 'arcData'>): ArcOutline {
37
+ const { startingAngle: start, endingAngle: end, innerRadius } = node.arcData ?? WHOLE_ELLIPSE
38
+ const rx = node.width / 2
39
+ const ry = node.height / 2
40
+ const whole = sweepsWholeTurn(start, end)
41
+ const sweep = whole ? FULL_TURN : end - start
42
+ const outer: ArcSweep = { type: 'arc', rx, ry, start, sweep }
43
+ if (innerRadius > 0) {
44
+ const hole: ArcSweep = {
45
+ type: 'arc',
46
+ rx: rx * innerRadius,
47
+ ry: ry * innerRadius,
48
+ start: start + sweep,
49
+ sweep: -sweep
50
+ }
51
+ return { cx: rx, cy: ry, contours: whole ? [[outer], [hole]] : [[outer, hole]] }
52
+ }
53
+ return { cx: rx, cy: ry, contours: [whole ? [outer] : [outer, { type: 'centre' }]] }
54
+ }
55
+
56
+ /** Whether a sweep goes all the way round, drawn as a whole ellipse rather than an arc. */
57
+ export function sweepsWholeEllipse(step: ArcSweep): boolean {
58
+ return Math.abs(step.sweep) >= FULL_TURN
59
+ }
@@ -0,0 +1,4 @@
1
+ export * from './arc'
2
+ export * from './network'
3
+ export * from './sink'
4
+ export * from './smooth-corners'
@@ -0,0 +1,334 @@
1
+ import type { Vector } from '../primitives'
2
+ import type { 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
+ /**
268
+ * Segments chained into runs as Figma strokes a network: a run passes through every vertex
269
+ * exactly two segments meet at, so its corners take the stroke's join and only its open ends take
270
+ * caps. Runs stop at open ends and at junctions where three or more segments meet.
271
+ */
272
+ export function strokeRuns(segments: VectorSegment[]): StrokeRun[] {
273
+ const incident = new Map<number, number[]>()
274
+ const meet = (vertex: number, index: number) => {
275
+ const list = incident.get(vertex)
276
+ if (list) list.push(index)
277
+ else incident.set(vertex, [index])
278
+ }
279
+ for (const [index, segment] of segments.entries()) {
280
+ meet(segment.start, index)
281
+ if (segment.end !== segment.start) meet(segment.end, index)
282
+ }
283
+ const used = new Set<number>()
284
+ const across = (index: number, vertex: number) =>
285
+ segments[index].start === vertex ? segments[index].end : segments[index].start
286
+ /** Extends a run through `vertex` for as long as two segments meet at each vertex reached. */
287
+ const extend = (vertex: number, from: number, add: (index: number) => void) => {
288
+ for (;;) {
289
+ const meeting = incident.get(vertex)
290
+ if (meeting?.length !== 2) return vertex
291
+ const next = meeting[0] === from ? meeting[1] : meeting[0]
292
+ if (used.has(next)) return vertex
293
+ used.add(next)
294
+ add(next)
295
+ vertex = across(next, vertex)
296
+ from = next
297
+ }
298
+ }
299
+
300
+ const runs: StrokeRun[] = []
301
+ for (const [first, segment] of segments.entries()) {
302
+ if (used.has(first)) continue
303
+ used.add(first)
304
+ const run = [first]
305
+ const end = extend(segment.end, first, (index) => run.push(index))
306
+ if (end === segment.start) {
307
+ runs.push({ start: segment.start, segments: run, closed: true })
308
+ continue
309
+ }
310
+ const start = extend(segment.start, first, (index) => run.unshift(index))
311
+ // A loop through a junction stops at it from both sides; it still closes there, whichever
312
+ // of its segments is stored first.
313
+ runs.push({ start, segments: run, closed: start === end })
314
+ }
315
+ return runs
316
+ }
317
+
318
+ /** Emits a stroke run as one contour, closed when the run comes back to its start. */
319
+ export function addStrokeRunToPath(
320
+ path: PathSink,
321
+ run: StrokeRun,
322
+ segments: VectorSegment[],
323
+ vertices: VectorVertex[]
324
+ ): void {
325
+ let current = run.start
326
+ path.moveTo(vertices[current].x, vertices[current].y)
327
+ for (const index of run.segments) {
328
+ const segment = segments[index]
329
+ const forward = segment.start === current
330
+ addSegmentDirected(path, segment, vertices, forward)
331
+ current = forward ? segment.end : segment.start
332
+ }
333
+ if (run.closed) path.close()
334
+ }