@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
@@ -1,12 +1,6 @@
1
1
  import { SceneGraph } from "../graph.js";
2
- import { Fill, SceneNode } from "../types.js";
2
+ import { SceneNode } from "../types.js";
3
3
  //#region src/outline/hit.d.ts
4
- /**
5
- * Whether a paint makes its layer clickable, as in Figma: a visible paint that shows something.
6
- * A paint at 0% opacity, or a gradient whose stops are all transparent, does not; 1% does, and
7
- * the layer's own opacity does not matter.
8
- */
9
- declare function isHitPaint(paint: Fill): boolean;
10
4
  /** The farthest a layer's clickable strokes reach outside its box, in its own units. */
11
5
  declare function strokeReach(node: SceneNode): number;
12
6
  /**
@@ -17,5 +11,5 @@ declare function strokeReach(node: SceneNode): number;
17
11
  */
18
12
  declare function hitsShape(graph: SceneGraph, node: SceneNode, px: number, py: number, pad?: number): boolean | null;
19
13
  //#endregion
20
- export { hitsShape, isHitPaint, strokeReach };
14
+ export { hitsShape, strokeReach };
21
15
  //# sourceMappingURL=hit.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"hit.d.ts","names":[],"sources":["../../src/outline/hit.ts"],"mappings":";;;;;;;;iBAuBgB,WAAW,OAAO;;iBAyBlB,YAAY,MAAM;;;;;;;iBAmTlB,UACd,OAAO,YACP,MAAM,WACN,YACA,YACA"}
1
+ {"version":3,"file":"hit.d.ts","names":[],"sources":["../../src/outline/hit.ts"],"mappings":";;;;iBA+BgB,YAAY,MAAM;;;;;;;iBAmTlB,UACd,OAAO,YACP,MAAM,WACN,YACA,YACA"}
@@ -1,7 +1,9 @@
1
1
  import Matrix from "../matrix.js";
2
2
  import { getWorldMatrix } from "../coordinate.js";
3
3
  import { arrowCapOverflow, isArrowCap, lineStrokeEnds } from "../arrow-caps.js";
4
+ import { strokeWeightOf } from "../geometry.js";
4
5
  import { containsByWinding, distanceToLines, distanceToSegment } from "./point.js";
6
+ import { paintShows } from "../paints/shows.js";
5
7
  import { hitsArrowHeads } from "./heads.js";
6
8
  import { shapeOutline } from "./shapes.js";
7
9
  import { compact } from "es-toolkit";
@@ -10,35 +12,24 @@ import { compact } from "es-toolkit";
10
12
  const EDGE_SAMPLES = 8;
11
13
  /** How far a flattened curve may stray from the shape when nothing tighter is asked for. */
12
14
  const DEFAULT_TOLERANCE = .25;
13
- /**
14
- * Whether a paint makes its layer clickable, as in Figma: a visible paint that shows something.
15
- * A paint at 0% opacity, or a gradient whose stops are all transparent, does not; 1% does, and
16
- * the layer's own opacity does not matter.
17
- */
18
- function isHitPaint(paint) {
19
- if (!paint.visible || !(paint.opacity > 0)) return false;
20
- if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0);
21
- if (paint.type === "SOLID") return paint.color.a > 0;
22
- return true;
23
- }
24
15
  function insideArea(area, point) {
25
16
  return containsByWinding(area.contours, area.windingRule, point);
26
17
  }
27
- /** A stroke's weight; with independent borders the widest side, for layers without per-side lines. */
28
- function strokeWeightOf(node, stroke) {
29
- if (node.independentStrokeWeights) return Math.max(node.borderTopWeight, node.borderRightWeight, node.borderBottomWeight, node.borderLeftWeight);
30
- return Number.isFinite(stroke.weight) ? stroke.weight : node.strokeWeight;
18
+ /** A stroke's weight, the widest side with sides of their own, for layers without per-side lines. */
19
+ function hitWeight(node, stroke) {
20
+ const weight = strokeWeightOf(node, stroke);
21
+ return Number.isFinite(weight) ? weight : node.strokeWeight;
31
22
  }
32
23
  /** The farthest a layer's clickable strokes reach outside its box, in its own units. */
33
24
  function strokeReach(node) {
34
25
  let reach = 0;
35
26
  for (const stroke of node.strokes) {
36
- if (!isHitPaint(stroke)) continue;
37
- const weight = strokeWeightOf(node, stroke);
27
+ if (!paintShows(stroke)) continue;
28
+ const weight = hitWeight(node, stroke);
38
29
  const side = node.type === "LINE" || stroke.align === "OUTSIDE";
39
30
  reach = Math.max(reach, side ? weight : weight / 2);
40
31
  }
41
- const hitStrokes = node.strokes.filter(isHitPaint);
32
+ const hitStrokes = node.strokes.filter(paintShows);
42
33
  return Math.max(reach, arrowCapOverflow(hitStrokes, node.strokeCap, node.vectorNetwork));
43
34
  }
44
35
  function localPoint(graph, node, px, py) {
@@ -217,7 +208,7 @@ function strokeHits(node, stroke, probe, pad) {
217
208
  if (probe.strokeAreaWithin) return probe.strokeAreaWithin(pad);
218
209
  const align = probe.closed && !probe.centered ? stroke.align : "CENTER";
219
210
  if (probe.strokeOnEdge) {
220
- const weight = strokeWeightOf(node, stroke);
211
+ const weight = hitWeight(node, stroke);
221
212
  return probe.edgeWithin(strokeReachAt(align, weight, probe.inside) + pad);
222
213
  }
223
214
  const cap = stroke.cap ?? node.strokeCap;
@@ -227,7 +218,7 @@ function strokeHits(node, stroke, probe, pad) {
227
218
  return line.distance <= band + pad;
228
219
  };
229
220
  if (node.type === "LINE" && probe.local) {
230
- const weight = strokeWeightOf(node, stroke);
221
+ const weight = hitWeight(node, stroke);
231
222
  if (!(weight > 0)) return false;
232
223
  const { start, end } = lineStrokeEnds(node.width, node.height, weight, cap);
233
224
  if (reaches(measureLine([start, end], [true, true], probe.local), weight / 2)) return true;
@@ -249,11 +240,11 @@ function strokeHits(node, stroke, probe, pad) {
249
240
  }
250
241
  const network = node.type === "VECTOR" ? node.vectorNetwork : null;
251
242
  if (network && probe.local) {
252
- const weight = strokeWeightOf(node, stroke);
243
+ const weight = hitWeight(node, stroke);
253
244
  if (weight > 0 && hitsArrowHeads(network, cap, weight, probe.local, pad)) return true;
254
245
  }
255
246
  return probe.lines.some((line, index) => {
256
- const weight = probe.lineWeights?.[index] ?? strokeWeightOf(node, stroke);
247
+ const weight = probe.lineWeights?.[index] ?? hitWeight(node, stroke);
257
248
  if (!(weight > 0)) return false;
258
249
  return reaches(line, strokeReachAt(align, weight, probe.inside));
259
250
  });
@@ -267,10 +258,10 @@ function strokeHits(node, stroke, probe, pad) {
267
258
  function hitsShape(graph, node, px, py, pad = 0) {
268
259
  const probe = probeNode(graph, node, px, py, pad > 0 ? Math.min(DEFAULT_TOLERANCE, pad / 4) : DEFAULT_TOLERANCE);
269
260
  if (!probe) return null;
270
- if (node.fills.filter(isHitPaint).length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true;
271
- return node.strokes.some((stroke) => isHitPaint(stroke) && strokeHits(node, stroke, probe, pad));
261
+ if (node.fills.filter(paintShows).length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true;
262
+ return node.strokes.some((stroke) => paintShows(stroke) && strokeHits(node, stroke, probe, pad));
272
263
  }
273
264
  //#endregion
274
- export { hitsShape, isHitPaint, strokeReach };
265
+ export { hitsShape, strokeReach };
275
266
 
276
267
  //# sourceMappingURL=hit.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"hit.js","names":[],"sources":["../../src/outline/hit.ts"],"sourcesContent":["import { compact } from 'es-toolkit'\n\nimport { arrowCapOverflow, isArrowCap, lineStrokeEnds } from '../arrow-caps'\nimport { getWorldMatrix } from '../coordinate'\nimport type { SceneGraph } from '../graph'\nimport Matrix from '../matrix'\nimport type { Vector } from '../primitives'\nimport type { Fill, SceneNode, Stroke } from '../types'\nimport { hitsArrowHeads } from './heads'\nimport { containsByWinding, distanceToLines, distanceToSegment } from './point'\nimport { shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'\n\n/** Points tested along each edge near the point for whether the filled result changes there. */\nconst EDGE_SAMPLES = 8\n\n/** How far a flattened curve may stray from the shape when nothing tighter is asked for. */\nconst DEFAULT_TOLERANCE = 0.25\n\n/**\n * Whether a paint makes its layer clickable, as in Figma: a visible paint that shows something.\n * A paint at 0% opacity, or a gradient whose stops are all transparent, does not; 1% does, and\n * the layer's own opacity does not matter.\n */\nexport function isHitPaint(paint: Fill): boolean {\n if (!paint.visible || !(paint.opacity > 0)) return false\n if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0)\n if (paint.type === 'SOLID') return paint.color.a > 0\n return true\n}\n\nfunction insideArea(area: OutlineArea, point: Vector): boolean {\n return containsByWinding(area.contours, area.windingRule, point)\n}\n\n/** A stroke's weight; with independent borders the widest side, for layers without per-side lines. */\nfunction strokeWeightOf(node: SceneNode, stroke: Stroke): number {\n if (node.independentStrokeWeights) {\n return Math.max(\n node.borderTopWeight,\n node.borderRightWeight,\n node.borderBottomWeight,\n node.borderLeftWeight\n )\n }\n return Number.isFinite(stroke.weight) ? stroke.weight : node.strokeWeight\n}\n\n/** The farthest a layer's clickable strokes reach outside its box, in its own units. */\nexport function strokeReach(node: SceneNode): number {\n let reach = 0\n for (const stroke of node.strokes) {\n if (!isHitPaint(stroke)) continue\n const weight = strokeWeightOf(node, stroke)\n // A line's stroke lies a full weight to one side of it.\n const side = node.type === 'LINE' || stroke.align === 'OUTSIDE'\n reach = Math.max(reach, side ? weight : weight / 2)\n }\n // Arrow heads reach past the stroke.\n const hitStrokes = node.strokes.filter(isHitPaint)\n return Math.max(reach, arrowCapOverflow(hitStrokes, node.strokeCap, node.vectorNetwork))\n}\n\nfunction localPoint(graph: SceneGraph, node: SceneNode, px: number, py: number): Vector | null {\n const inverse = Matrix.invert(getWorldMatrix(node, graph))\n if (!inverse) return null\n const [x, y] = Matrix.mapPoints(inverse, [px, py])\n return { x, y }\n}\n\ninterface Probe {\n inside: boolean\n /** The point in the layer's own coordinates, where its outline has one. */\n local?: Vector\n /**\n * Whether the outline of the filled area passes within `radius` of the point, counting only\n * edges where the filled result changes from one side to the other.\n */\n edgeWithin: (radius: number) => boolean\n /** Each line a stroke follows, measured from the point in canvas units. */\n lines: LineReach[]\n /** Each line's own stroke weight, where sides differ. */\n lineWeights?: number[]\n /** Whether the layer has an area of its own; strokes on lines are always centred. */\n closed: boolean\n /** Strokes centre on the lines whatever their alignment. */\n centered?: boolean\n /** Strokes follow the filled area's outline, for a shape combined from operands. */\n strokeOnEdge?: boolean\n /** Whether the point lies on or within `radius` of the saved outline a stroke fills. */\n strokeAreaWithin?: (radius: number) => boolean\n}\n\n/**\n * Whether an outline made of `edges` passes within `radius` of the point, counting a stretch of\n * edge only where `insideAt` changes from one side of it to the other: edges that cancel or that\n * lie hidden inside or outside the result count for nothing.\n */\nfunction changingEdgeWithin(\n edges: Vector[][],\n insideAt: (x: number, y: number) => boolean | null,\n point: Vector,\n radius: number,\n tolerance: number\n): boolean {\n const step = Math.max(1e-6, Math.min(tolerance, radius / 4) / 4)\n for (const edge of edges) {\n for (let i = 0; i + 1 < edge.length; i++) {\n const a = edge[i]\n const b = edge[i + 1]\n if (a.x === b.x && a.y === b.y) continue\n if (distanceToSegment(point, a, b) > radius) continue\n const dx = b.x - a.x\n const dy = b.y - a.y\n const length = Math.hypot(dx, dy)\n const nx = -dy / length\n const ny = dx / length\n // The part of the edge inside the circle, sampled evenly.\n const centre = ((point.x - a.x) * dx + (point.y - a.y) * dy) / (length * length)\n const offset = Math.abs((point.x - a.x) * nx + (point.y - a.y) * ny)\n const half = Math.sqrt(Math.max(0, radius * radius - offset * offset)) / length\n const from = Math.max(0, centre - half)\n const to = Math.min(1, centre + half)\n for (let k = 0; k <= EDGE_SAMPLES; k++) {\n const t = from + ((to - from) * k) / EDGE_SAMPLES\n const qx = a.x + dx * t\n const qy = a.y + dy * t\n const left = insideAt(qx + nx * step, qy + ny * step)\n const right = insideAt(qx - nx * step, qy - ny * step)\n if (left !== null && right !== null && left !== right) return true\n }\n }\n }\n return false\n}\n\n/**\n * How a point lies from a line: its distance, and past an open end how far beyond the end it is\n * along the line and how far across it, for a flat cap that paints nothing past the end.\n */\ninterface LineReach {\n distance: number\n beyond: number\n across: number\n}\n\nfunction measureLine(\n line: Vector[],\n ends: [boolean, boolean] | undefined,\n point: Vector\n): LineReach {\n const reach: LineReach = { distance: distanceToLines([line], point), beyond: 0, across: 0 }\n if (!ends || line.length < 2) return reach\n // Past an open end, the point projects beyond the first or last segment.\n const tips: Array<[boolean, Vector, Vector]> = [\n [ends[0], line[0], line[1]],\n [ends[1], line[line.length - 1], line[line.length - 2]]\n ]\n for (const [open, tip, next] of tips) {\n if (!open) continue\n const dx = tip.x - next.x\n const dy = tip.y - next.y\n const length = Math.hypot(dx, dy)\n if (length === 0) continue\n const along = ((point.x - tip.x) * dx + (point.y - tip.y) * dy) / length\n if (along <= 0 || Math.hypot(point.x - tip.x, point.y - tip.y) > reach.distance + 1e-9) continue\n reach.beyond = along\n reach.across = Math.abs((point.x - tip.x) * dy - (point.y - tip.y) * dx) / length\n }\n return reach\n}\n\nfunction probeOutline(outline: ShapeOutline, point: Vector, tolerance: number): Probe {\n const insideAt = (x: number, y: number) =>\n outline.areas.some((area) => insideArea(area, { x, y }))\n const edges = outline.areas.flatMap((area) => area.contours.map(closedLoop))\n const { strokeAreas } = outline\n return {\n inside: insideAt(point.x, point.y),\n local: point,\n edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),\n lines: outline.lines.map((line, index) => measureLine(line, outline.lineEnds?.[index], point)),\n lineWeights: outline.lineWeights,\n closed: outline.areas.some((area) => area.contours.length > 0),\n centered: outline.strokeCentered,\n strokeAreaWithin: strokeAreas\n ? (radius) =>\n strokeAreas.some((area) => insideArea(area, point)) ||\n changingEdgeWithin(\n strokeAreas.flatMap((area) => area.contours.map(closedLoop)),\n (x, y) => strokeAreas.some((area) => insideArea(area, { x, y })),\n point,\n radius,\n tolerance\n )\n : undefined\n }\n}\n\nfunction closedLoop(contour: Vector[]): Vector[] {\n const first = contour.at(0)\n return first ? [...contour, first] : contour\n}\n\nconst BOOLEAN_COMBINE: Record<\n NonNullable<SceneNode['booleanOperation']>,\n (hits: boolean[]) => boolean\n> = {\n UNION: (hits) => hits.some(Boolean),\n INTERSECT: (hits) => hits.length > 0 && hits.every(Boolean),\n SUBTRACT: ([first = false, ...rest]) => first && !rest.some(Boolean),\n EXCLUDE: (hits) => compact(hits).length % 2 === 1\n}\n\n/** The lines an operand draws its outline along, in canvas coordinates. */\nfunction operandEdges(graph: SceneGraph, node: SceneNode, tolerance: number): Vector[][] {\n if (node.type === 'BOOLEAN_OPERATION' && node.fillGeometry.length === 0) {\n return graph\n .getChildren(node.id)\n .filter((child) => child.visible)\n .flatMap((child) => operandEdges(graph, child, tolerance))\n }\n const outline =\n node.type === 'BOOLEAN_OPERATION'\n ? shapeOutline({ ...node, type: 'VECTOR', vectorNetwork: null }, tolerance)\n : shapeOutline(node, tolerance)\n if (!outline) return []\n const world = getWorldMatrix(node, graph)\n const local = [\n ...outline.lines,\n ...outline.areas.flatMap((area) => area.contours.map(closedLoop))\n ]\n return local.map((line) =>\n line.map((point) => {\n const [x, y] = Matrix.mapPoints(world, [point.x, point.y])\n return { x, y }\n })\n )\n}\n\n/**\n * A boolean operation without saved geometry, combined from its operands as it draws. Its outline\n * runs along the operand edges where the combined result changes from one side to the other, so\n * edges hidden inside or outside the result count for nothing. Each side is probed at most a\n * quarter of a screen pixel out, so a result thinner than that, which draws no visible pixel, is\n * not seen.\n */\nfunction probeBoolean(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n tolerance: number\n): Probe | null {\n const operands = graph.getChildren(node.id).filter((child) => child.visible)\n if (operands.length === 0) return null\n const combine = BOOLEAN_COMBINE[node.booleanOperation ?? 'UNION']\n const insideAt = (x: number, y: number): boolean | null => {\n const hits: boolean[] = []\n for (const operand of operands) {\n const probe = probeNode(graph, operand, x, y, tolerance)\n if (!probe) return null\n hits.push(probe.inside)\n }\n return combine(hits)\n }\n const inside = insideAt(px, py)\n if (inside === null) return null\n const point = { x: px, y: py }\n const edges = operands.flatMap((operand) => operandEdges(graph, operand, tolerance))\n return {\n inside,\n edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),\n lines: [],\n closed: true,\n strokeOnEdge: true\n }\n}\n\nfunction probeNode(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n tolerance: number\n): Probe | null {\n if (node.type === 'BOOLEAN_OPERATION' && node.fillGeometry.length === 0) {\n return probeBoolean(graph, node, px, py, tolerance)\n }\n const outline =\n node.type === 'BOOLEAN_OPERATION'\n ? shapeOutline({ ...node, type: 'VECTOR', vectorNetwork: null }, tolerance)\n : shapeOutline(node, tolerance)\n if (!outline) return null\n const point = localPoint(graph, node, px, py)\n return point ? probeOutline(outline, point, tolerance) : null\n}\n\n/** How far from a line a stroke's band reaches toward the point, beyond which it misses. */\nfunction strokeReachAt(align: Stroke['align'], weight: number, inside: boolean): number {\n if (align === 'INSIDE') return inside ? weight : 0\n if (align === 'OUTSIDE') return inside ? 0 : weight\n return weight / 2\n}\n\nfunction strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number): boolean {\n if (probe.strokeAreaWithin) return probe.strokeAreaWithin(pad)\n const align = probe.closed && !probe.centered ? stroke.align : 'CENTER'\n if (probe.strokeOnEdge) {\n const weight = strokeWeightOf(node, stroke)\n return probe.edgeWithin(strokeReachAt(align, weight, probe.inside) + pad)\n }\n // A flat cap ends the stroke at the line's end; round and square caps paint past it.\n const cap = stroke.cap ?? node.strokeCap\n const flatEnds = cap === 'NONE'\n const reaches = (line: LineReach, band: number): boolean => {\n if (flatEnds && line.beyond > 0) {\n // Past a flat end, the distance to the painted band's square end.\n return Math.hypot(line.beyond, Math.max(0, line.across - band)) <= pad\n }\n return line.distance <= band + pad\n }\n if (node.type === 'LINE' && probe.local) {\n // Figma strokes a line a full weight to one side, along the path the renderer draws.\n const weight = strokeWeightOf(node, stroke)\n if (!(weight > 0)) return false\n const { start, end } = lineStrokeEnds(node.width, node.height, weight, cap)\n if (reaches(measureLine([start, end], [true, true], probe.local), weight / 2)) return true\n if (!isArrowCap(cap)) return false\n const still = { x: 0, y: 0 }\n const shaft = {\n vertices: [start, end],\n segments: [{ start: 0, end: 1, tangentStart: still, tangentEnd: still }],\n regions: []\n }\n return hitsArrowHeads(shaft, cap, weight, probe.local, pad)\n }\n const network = node.type === 'VECTOR' ? node.vectorNetwork : null\n if (network && probe.local) {\n const weight = strokeWeightOf(node, stroke)\n if (weight > 0 && hitsArrowHeads(network, cap, weight, probe.local, pad)) return true\n }\n return probe.lines.some((line, index) => {\n const weight = probe.lineWeights?.[index] ?? strokeWeightOf(node, stroke)\n // A side with no weight paints no border.\n if (!(weight > 0)) return false\n return reaches(line, strokeReachAt(align, weight, probe.inside))\n })\n}\n\n/**\n * Whether a click at the canvas point lands on the shape, as in Figma: inside the area a\n * clickable fill covers, or on the band a clickable stroke draws, either grown by `pad` canvas\n * units. A shape whose paints show nothing is never hit. Null for a layer without an outline to\n * test, which is hit by its box.\n */\nexport function hitsShape(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n pad = 0\n): boolean | null {\n const tolerance = pad > 0 ? Math.min(DEFAULT_TOLERANCE, pad / 4) : DEFAULT_TOLERANCE\n const probe = probeNode(graph, node, px, py, tolerance)\n if (!probe) return null\n const fills = node.fills.filter(isHitPaint)\n if (fills.length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true\n return node.strokes.some((stroke) => isHitPaint(stroke) && strokeHits(node, stroke, probe, pad))\n}\n"],"mappings":";;;;;;;;;AAaA,MAAM,eAAe;;AAGrB,MAA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AG;GAAG,GAAG;EAAE;EAM3B,OAAO,eAAe;GAJpB,UAAU,CAAC,OAAO,GAAG;GACrB,UAAU,CAAC;IAAE,OAAO;IAAG,KAAK;IAAG,cAAc;IAAO,YAAY;GAAM,CAAC;GACvE,SAAS,CAAC;EAEc,GAAG,KAAK,QAAQ,MAAM,OAAO,GAAG;CAC5D;CACA,MAAM,UAAU,KAAK,SAAS,WAAW,KAAK,gBAAgB;CAC9D,IAAI,WAAW,MAAM,OAAO;EAC1B,MAAM,SAAS,eAAe,MAAM,MAAM;EAC1C,IAAI,SAAS,KAAK,eAAe,SAAS,KAAK,QAAQ,MAAM,OAAO,GAAG,GAAG,OAAO;CACnF;CACA,OAAO,MAAM,MAAM,MAAM,MAAM,UAAU;EACvC,MAAM,SAAS,MAAM,cAAc,UAAU,eAAe,MAAM,MAAM;EAExE,IAAI,EAAE,SAAS,IAAI,OAAO;EAC1B,OAAO,QAAQ,MAAM,cAAc,OAAO,QAAQ,MAAM,MAAM,CAAC;CACjE,CAAC;AACH;;;;;;;AAQA,SAAgB,UACd,OACA,MACA,IACA,IACA,MAAM,GACU;CAEhB,MAAM,QAAQ,UAAU,OAAO,MAAM,IAAI,IADvB,MAAM,IAAI,KAAK,IAAI,mBAAmB,MAAM,CAAC,IAAI,iBACb;CACtD,IAAI,CAAC,OAAO,OAAO;CAEnB,IADc,KAAK,MAAM,OAAO,UACxB,CAAC,CAAC,SAAS,KAAK,MAAM,WAAW,MAAM,UAAU,MAAM,WAAW,GAAG,IAAI,OAAO;CACxF,OAAO,KAAK,QAAQ,MAAM,WAAW,WAAW,MAAM,KAAK,WAAW,MAAM,QAAQ,OAAO,GAAG,CAAC;AACjG"}
1
+ {"version":3,"file":"hit.js","names":[],"sources":["../../src/outline/hit.ts"],"sourcesContent":["import { compact } from 'es-toolkit'\n\nimport { arrowCapOverflow, isArrowCap, lineStrokeEnds } from '../arrow-caps'\nimport { getWorldMatrix } from '../coordinate'\nimport { strokeWeightOf } from '../geometry'\nimport type { SceneGraph } from '../graph'\nimport Matrix from '../matrix'\nimport { paintShows } from '../paints/shows'\nimport type { Vector } from '../primitives'\nimport type { SceneNode, Stroke } from '../types'\nimport { hitsArrowHeads } from './heads'\nimport { containsByWinding, distanceToLines, distanceToSegment } from './point'\nimport { shapeOutline, type OutlineArea, type ShapeOutline } from './shapes'\n\n/** Points tested along each edge near the point for whether the filled result changes there. */\nconst EDGE_SAMPLES = 8\n\n/** How far a flattened curve may stray from the shape when nothing tighter is asked for. */\nconst DEFAULT_TOLERANCE = 0.25\n\nfunction insideArea(area: OutlineArea, point: Vector): boolean {\n return containsByWinding(area.contours, area.windingRule, point)\n}\n\n/** A stroke's weight, the widest side with sides of their own, for layers without per-side lines. */\nfunction hitWeight(node: SceneNode, stroke: Stroke): number {\n const weight = strokeWeightOf(node, stroke)\n return Number.isFinite(weight) ? weight : node.strokeWeight\n}\n\n/** The farthest a layer's clickable strokes reach outside its box, in its own units. */\nexport function strokeReach(node: SceneNode): number {\n let reach = 0\n for (const stroke of node.strokes) {\n if (!paintShows(stroke)) continue\n const weight = hitWeight(node, stroke)\n // A line's stroke lies a full weight to one side of it.\n const side = node.type === 'LINE' || stroke.align === 'OUTSIDE'\n reach = Math.max(reach, side ? weight : weight / 2)\n }\n // Arrow heads reach past the stroke.\n const hitStrokes = node.strokes.filter(paintShows)\n return Math.max(reach, arrowCapOverflow(hitStrokes, node.strokeCap, node.vectorNetwork))\n}\n\nfunction localPoint(graph: SceneGraph, node: SceneNode, px: number, py: number): Vector | null {\n const inverse = Matrix.invert(getWorldMatrix(node, graph))\n if (!inverse) return null\n const [x, y] = Matrix.mapPoints(inverse, [px, py])\n return { x, y }\n}\n\ninterface Probe {\n inside: boolean\n /** The point in the layer's own coordinates, where its outline has one. */\n local?: Vector\n /**\n * Whether the outline of the filled area passes within `radius` of the point, counting only\n * edges where the filled result changes from one side to the other.\n */\n edgeWithin: (radius: number) => boolean\n /** Each line a stroke follows, measured from the point in canvas units. */\n lines: LineReach[]\n /** Each line's own stroke weight, where sides differ. */\n lineWeights?: number[]\n /** Whether the layer has an area of its own; strokes on lines are always centred. */\n closed: boolean\n /** Strokes centre on the lines whatever their alignment. */\n centered?: boolean\n /** Strokes follow the filled area's outline, for a shape combined from operands. */\n strokeOnEdge?: boolean\n /** Whether the point lies on or within `radius` of the saved outline a stroke fills. */\n strokeAreaWithin?: (radius: number) => boolean\n}\n\n/**\n * Whether an outline made of `edges` passes within `radius` of the point, counting a stretch of\n * edge only where `insideAt` changes from one side of it to the other: edges that cancel or that\n * lie hidden inside or outside the result count for nothing.\n */\nfunction changingEdgeWithin(\n edges: Vector[][],\n insideAt: (x: number, y: number) => boolean | null,\n point: Vector,\n radius: number,\n tolerance: number\n): boolean {\n const step = Math.max(1e-6, Math.min(tolerance, radius / 4) / 4)\n for (const edge of edges) {\n for (let i = 0; i + 1 < edge.length; i++) {\n const a = edge[i]\n const b = edge[i + 1]\n if (a.x === b.x && a.y === b.y) continue\n if (distanceToSegment(point, a, b) > radius) continue\n const dx = b.x - a.x\n const dy = b.y - a.y\n const length = Math.hypot(dx, dy)\n const nx = -dy / length\n const ny = dx / length\n // The part of the edge inside the circle, sampled evenly.\n const centre = ((point.x - a.x) * dx + (point.y - a.y) * dy) / (length * length)\n const offset = Math.abs((point.x - a.x) * nx + (point.y - a.y) * ny)\n const half = Math.sqrt(Math.max(0, radius * radius - offset * offset)) / length\n const from = Math.max(0, centre - half)\n const to = Math.min(1, centre + half)\n for (let k = 0; k <= EDGE_SAMPLES; k++) {\n const t = from + ((to - from) * k) / EDGE_SAMPLES\n const qx = a.x + dx * t\n const qy = a.y + dy * t\n const left = insideAt(qx + nx * step, qy + ny * step)\n const right = insideAt(qx - nx * step, qy - ny * step)\n if (left !== null && right !== null && left !== right) return true\n }\n }\n }\n return false\n}\n\n/**\n * How a point lies from a line: its distance, and past an open end how far beyond the end it is\n * along the line and how far across it, for a flat cap that paints nothing past the end.\n */\ninterface LineReach {\n distance: number\n beyond: number\n across: number\n}\n\nfunction measureLine(\n line: Vector[],\n ends: [boolean, boolean] | undefined,\n point: Vector\n): LineReach {\n const reach: LineReach = { distance: distanceToLines([line], point), beyond: 0, across: 0 }\n if (!ends || line.length < 2) return reach\n // Past an open end, the point projects beyond the first or last segment.\n const tips: Array<[boolean, Vector, Vector]> = [\n [ends[0], line[0], line[1]],\n [ends[1], line[line.length - 1], line[line.length - 2]]\n ]\n for (const [open, tip, next] of tips) {\n if (!open) continue\n const dx = tip.x - next.x\n const dy = tip.y - next.y\n const length = Math.hypot(dx, dy)\n if (length === 0) continue\n const along = ((point.x - tip.x) * dx + (point.y - tip.y) * dy) / length\n if (along <= 0 || Math.hypot(point.x - tip.x, point.y - tip.y) > reach.distance + 1e-9) continue\n reach.beyond = along\n reach.across = Math.abs((point.x - tip.x) * dy - (point.y - tip.y) * dx) / length\n }\n return reach\n}\n\nfunction probeOutline(outline: ShapeOutline, point: Vector, tolerance: number): Probe {\n const insideAt = (x: number, y: number) =>\n outline.areas.some((area) => insideArea(area, { x, y }))\n const edges = outline.areas.flatMap((area) => area.contours.map(closedLoop))\n const { strokeAreas } = outline\n return {\n inside: insideAt(point.x, point.y),\n local: point,\n edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),\n lines: outline.lines.map((line, index) => measureLine(line, outline.lineEnds?.[index], point)),\n lineWeights: outline.lineWeights,\n closed: outline.areas.some((area) => area.contours.length > 0),\n centered: outline.strokeCentered,\n strokeAreaWithin: strokeAreas\n ? (radius) =>\n strokeAreas.some((area) => insideArea(area, point)) ||\n changingEdgeWithin(\n strokeAreas.flatMap((area) => area.contours.map(closedLoop)),\n (x, y) => strokeAreas.some((area) => insideArea(area, { x, y })),\n point,\n radius,\n tolerance\n )\n : undefined\n }\n}\n\nfunction closedLoop(contour: Vector[]): Vector[] {\n const first = contour.at(0)\n return first ? [...contour, first] : contour\n}\n\nconst BOOLEAN_COMBINE: Record<\n NonNullable<SceneNode['booleanOperation']>,\n (hits: boolean[]) => boolean\n> = {\n UNION: (hits) => hits.some(Boolean),\n INTERSECT: (hits) => hits.length > 0 && hits.every(Boolean),\n SUBTRACT: ([first = false, ...rest]) => first && !rest.some(Boolean),\n EXCLUDE: (hits) => compact(hits).length % 2 === 1\n}\n\n/** The lines an operand draws its outline along, in canvas coordinates. */\nfunction operandEdges(graph: SceneGraph, node: SceneNode, tolerance: number): Vector[][] {\n if (node.type === 'BOOLEAN_OPERATION' && node.fillGeometry.length === 0) {\n return graph\n .getChildren(node.id)\n .filter((child) => child.visible)\n .flatMap((child) => operandEdges(graph, child, tolerance))\n }\n const outline =\n node.type === 'BOOLEAN_OPERATION'\n ? shapeOutline({ ...node, type: 'VECTOR', vectorNetwork: null }, tolerance)\n : shapeOutline(node, tolerance)\n if (!outline) return []\n const world = getWorldMatrix(node, graph)\n const local = [\n ...outline.lines,\n ...outline.areas.flatMap((area) => area.contours.map(closedLoop))\n ]\n return local.map((line) =>\n line.map((point) => {\n const [x, y] = Matrix.mapPoints(world, [point.x, point.y])\n return { x, y }\n })\n )\n}\n\n/**\n * A boolean operation without saved geometry, combined from its operands as it draws. Its outline\n * runs along the operand edges where the combined result changes from one side to the other, so\n * edges hidden inside or outside the result count for nothing. Each side is probed at most a\n * quarter of a screen pixel out, so a result thinner than that, which draws no visible pixel, is\n * not seen.\n */\nfunction probeBoolean(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n tolerance: number\n): Probe | null {\n const operands = graph.getChildren(node.id).filter((child) => child.visible)\n if (operands.length === 0) return null\n const combine = BOOLEAN_COMBINE[node.booleanOperation ?? 'UNION']\n const insideAt = (x: number, y: number): boolean | null => {\n const hits: boolean[] = []\n for (const operand of operands) {\n const probe = probeNode(graph, operand, x, y, tolerance)\n if (!probe) return null\n hits.push(probe.inside)\n }\n return combine(hits)\n }\n const inside = insideAt(px, py)\n if (inside === null) return null\n const point = { x: px, y: py }\n const edges = operands.flatMap((operand) => operandEdges(graph, operand, tolerance))\n return {\n inside,\n edgeWithin: (radius) => changingEdgeWithin(edges, insideAt, point, radius, tolerance),\n lines: [],\n closed: true,\n strokeOnEdge: true\n }\n}\n\nfunction probeNode(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n tolerance: number\n): Probe | null {\n if (node.type === 'BOOLEAN_OPERATION' && node.fillGeometry.length === 0) {\n return probeBoolean(graph, node, px, py, tolerance)\n }\n const outline =\n node.type === 'BOOLEAN_OPERATION'\n ? shapeOutline({ ...node, type: 'VECTOR', vectorNetwork: null }, tolerance)\n : shapeOutline(node, tolerance)\n if (!outline) return null\n const point = localPoint(graph, node, px, py)\n return point ? probeOutline(outline, point, tolerance) : null\n}\n\n/** How far from a line a stroke's band reaches toward the point, beyond which it misses. */\nfunction strokeReachAt(align: Stroke['align'], weight: number, inside: boolean): number {\n if (align === 'INSIDE') return inside ? weight : 0\n if (align === 'OUTSIDE') return inside ? 0 : weight\n return weight / 2\n}\n\nfunction strokeHits(node: SceneNode, stroke: Stroke, probe: Probe, pad: number): boolean {\n if (probe.strokeAreaWithin) return probe.strokeAreaWithin(pad)\n const align = probe.closed && !probe.centered ? stroke.align : 'CENTER'\n if (probe.strokeOnEdge) {\n const weight = hitWeight(node, stroke)\n return probe.edgeWithin(strokeReachAt(align, weight, probe.inside) + pad)\n }\n // A flat cap ends the stroke at the line's end; round and square caps paint past it.\n const cap = stroke.cap ?? node.strokeCap\n const flatEnds = cap === 'NONE'\n const reaches = (line: LineReach, band: number): boolean => {\n if (flatEnds && line.beyond > 0) {\n // Past a flat end, the distance to the painted band's square end.\n return Math.hypot(line.beyond, Math.max(0, line.across - band)) <= pad\n }\n return line.distance <= band + pad\n }\n if (node.type === 'LINE' && probe.local) {\n // Figma strokes a line a full weight to one side, along the path the renderer draws.\n const weight = hitWeight(node, stroke)\n if (!(weight > 0)) return false\n const { start, end } = lineStrokeEnds(node.width, node.height, weight, cap)\n if (reaches(measureLine([start, end], [true, true], probe.local), weight / 2)) return true\n if (!isArrowCap(cap)) return false\n const still = { x: 0, y: 0 }\n const shaft = {\n vertices: [start, end],\n segments: [{ start: 0, end: 1, tangentStart: still, tangentEnd: still }],\n regions: []\n }\n return hitsArrowHeads(shaft, cap, weight, probe.local, pad)\n }\n const network = node.type === 'VECTOR' ? node.vectorNetwork : null\n if (network && probe.local) {\n const weight = hitWeight(node, stroke)\n if (weight > 0 && hitsArrowHeads(network, cap, weight, probe.local, pad)) return true\n }\n return probe.lines.some((line, index) => {\n const weight = probe.lineWeights?.[index] ?? hitWeight(node, stroke)\n // A side with no weight paints no border.\n if (!(weight > 0)) return false\n return reaches(line, strokeReachAt(align, weight, probe.inside))\n })\n}\n\n/**\n * Whether a click at the canvas point lands on the shape, as in Figma: inside the area a\n * clickable fill covers, or on the band a clickable stroke draws, either grown by `pad` canvas\n * units. A shape whose paints show nothing is never hit. Null for a layer without an outline to\n * test, which is hit by its box.\n */\nexport function hitsShape(\n graph: SceneGraph,\n node: SceneNode,\n px: number,\n py: number,\n pad = 0\n): boolean | null {\n const tolerance = pad > 0 ? Math.min(DEFAULT_TOLERANCE, pad / 4) : DEFAULT_TOLERANCE\n const probe = probeNode(graph, node, px, py, tolerance)\n if (!probe) return null\n const fills = node.fills.filter(paintShows)\n if (fills.length > 0 && probe.closed && (probe.inside || probe.edgeWithin(pad))) return true\n return node.strokes.some((stroke) => paintShows(stroke) && strokeHits(node, stroke, probe, pad))\n}\n"],"mappings":";;;;;;;;;;;AAeA,MAAM,eAAe;;AAGrB,MAAM,oBAAoB;AAE1B,SAAS,WAAW,MAAmB,OAAwB;CAC7D,OAAO,kBAAkB,KAAK,UAAU,KAAK,aAAa,KAAK;AACjE;;AAGA,SAAS,UAAU,MAAiB,QAAwB;CAC1D,MAAM,SAAS,eAAe,MAAM,MAAM;CAC1C,OAAO,OAAO,SAAS,MAAM,IAAI,SAAS,KAAK;AACjD;;AAGA,SAAgB,YAAY,MAAyB;CACnD,IAAI,QAAQ;CACZ,KAAK,MAAM,UAAU,KAAK,SAAS;EACjC,IAAI,CAAC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;;AAQA,SAAgB,UACd,OACA,MACA,IACA,IACA,MAAM,GACU;CAEhB,MAAM,QAAQ,UAAU,OAAO,MAAM,IAAI,IADvB,MAAM,IAAI,KAAK,IAAI,mBAAmB,MAAM,CAAC,IAAI,iBACb;CACtD,IAAI,CAAC,OAAO,OAAO;CAEnB,IADc,KAAK,MAAM,OAAO,UACxB,CAAC,CAAC,SAAS,KAAK,MAAM,WAAW,MAAM,UAAU,MAAM,WAAW,GAAG,IAAI,OAAO;CACxF,OAAO,KAAK,QAAQ,MAAM,WAAW,WAAW,MAAM,KAAK,WAAW,MAAM,QAAQ,OAAO,GAAG,CAAC;AACjG"}
@@ -1,5 +1,5 @@
1
- import { hitsShape, isHitPaint, strokeReach } from "./hit.js";
2
- import { OutlineArea, ShapeOutline, shapeOutline } from "./shapes.js";
1
+ import { hitsShape, strokeReach } from "./hit.js";
2
+ import { OutlineArea, ShapeOutline, isWholeEllipse, shapeOutline } from "./shapes.js";
3
3
  import { blobContours, loopPoints, segmentPoints } from "./flatten.js";
4
4
  import { containsByWinding, distanceToLines, distanceToSegment, windingNumber } from "./point.js";
5
- export { type OutlineArea, type ShapeOutline, blobContours, containsByWinding, distanceToLines, distanceToSegment, hitsShape, isHitPaint, loopPoints, segmentPoints, shapeOutline, strokeReach, windingNumber };
5
+ export { type OutlineArea, type ShapeOutline, blobContours, containsByWinding, distanceToLines, distanceToSegment, hitsShape, isWholeEllipse, loopPoints, segmentPoints, shapeOutline, strokeReach, windingNumber };
@@ -1,5 +1,5 @@
1
1
  import { containsByWinding, distanceToLines, distanceToSegment, windingNumber } from "./point.js";
2
2
  import { blobContours, loopPoints, segmentPoints } from "./flatten.js";
3
- import { shapeOutline } from "./shapes.js";
4
- import { hitsShape, isHitPaint, strokeReach } from "./hit.js";
5
- export { blobContours, containsByWinding, distanceToLines, distanceToSegment, hitsShape, isHitPaint, loopPoints, segmentPoints, shapeOutline, strokeReach, windingNumber };
3
+ import { isWholeEllipse, shapeOutline } from "./shapes.js";
4
+ import { hitsShape, strokeReach } from "./hit.js";
5
+ export { blobContours, containsByWinding, distanceToLines, distanceToSegment, hitsShape, isWholeEllipse, loopPoints, segmentPoints, shapeOutline, strokeReach, windingNumber };
@@ -25,11 +25,13 @@ interface ShapeOutline {
25
25
  /** The saved outline a stroke fills, for a shape whose stroke has no lines to follow. */
26
26
  strokeAreas?: OutlineArea[];
27
27
  }
28
+ /** Whether a layer is an unbroken ellipse: an ELLIPSE whose arc, if any, is a whole turn with no hole. */
29
+ declare function isWholeEllipse(node: Pick<SceneNode, 'type' | 'arcData'>): boolean;
28
30
  /**
29
31
  * The outline a click is tested against, in the node's own coordinates, with curves within
30
32
  * `tolerance` of the shape; null for layers tested by their box, such as text.
31
33
  */
32
34
  declare function shapeOutline(node: SceneNode, tolerance: number): ShapeOutline | null;
33
35
  //#endregion
34
- export { OutlineArea, ShapeOutline, shapeOutline };
36
+ export { OutlineArea, ShapeOutline, isWholeEllipse, shapeOutline };
35
37
  //# sourceMappingURL=shapes.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"shapes.d.ts","names":[],"sources":["../../src/outline/shapes.ts"],"mappings":";;;;UAaiB;EACf,aAAa;EACb,UAAU;;;;;;UAOK;EACf,OAAO;EACP,OAAO;;EAEP;;;;;EAKA;;EAEA,WAAW;;EAEX,cAAc;;;;;;iBA2IA,aAAa,MAAM,WAAW,oBAAoB"}
1
+ {"version":3,"file":"shapes.d.ts","names":[],"sources":["../../src/outline/shapes.ts"],"mappings":";;;;UAgBiB;EACf,aAAa;EACb,UAAU;;;;;;UAOK;EACf,OAAO;EACP,OAAO;;EAEP;;;;;EAKA;;EAEA,WAAW;;EAEX,cAAc;;;iBAqDA,eAAe,MAAM,KAAK;;;;;iBAkE1B,aAAa,MAAM,WAAW,oBAAoB"}
@@ -1,3 +1,6 @@
1
+ import { borderWeights, sweepsWholeTurn } from "../geometry.js";
2
+ import { nodeCornerRadii } from "../corners.js";
3
+ import { arcOutline } from "../paths/arc.js";
1
4
  import { polygonOutline } from "../polygon.js";
2
5
  import { arcPoints, blobContours, commandContours, loopPoints, segmentPoints, segmentRuns } from "./flatten.js";
3
6
  //#region src/outline/shapes.ts
@@ -16,24 +19,9 @@ function fromContours(contours, windingRule = "NONZERO") {
16
19
  lines: contours.map(closed)
17
20
  };
18
21
  }
19
- function cornerRadii(node) {
20
- const radii = node.independentCorners ? [
21
- node.topLeftRadius,
22
- node.topRightRadius,
23
- node.bottomRightRadius,
24
- node.bottomLeftRadius
25
- ] : [
26
- node.cornerRadius,
27
- node.cornerRadius,
28
- node.cornerRadius,
29
- node.cornerRadius
30
- ];
31
- const limit = Math.min(node.width, node.height) / 2;
32
- return radii.map((radius) => Math.max(0, Math.min(radius, limit)));
33
- }
34
22
  function rectangleContour(node, tolerance) {
35
23
  const { width: w, height: h } = node;
36
- const [tl, tr, br, bl] = cornerRadii(node);
24
+ const [tl, tr, br, bl] = nodeCornerRadii(node);
37
25
  const corner = (cx, cy, r, from) => r > 0 ? arcPoints(cx, cy, r, r, from, from + Math.PI / 2, tolerance) : [{
38
26
  x: cx,
39
27
  y: cy
@@ -52,7 +40,7 @@ function rectangleContour(node, tolerance) {
52
40
  */
53
41
  function rectangleSides(node, tolerance) {
54
42
  const { width: w, height: h } = node;
55
- const [tl, tr, br, bl] = cornerRadii(node);
43
+ const [tl, tr, br, bl] = nodeCornerRadii(node);
56
44
  const quarter = Math.PI / 4;
57
45
  const arc = (cx, cy, r, from) => r > 0 ? arcPoints(cx, cy, r, r, from, from + quarter, tolerance) : [{
58
46
  x: cx,
@@ -65,30 +53,21 @@ function rectangleSides(node, tolerance) {
65
53
  [...arc(w - br, h - br, br, quarter), ...arc(bl, h - bl, bl, 2 * quarter)],
66
54
  [...arc(bl, h - bl, bl, 3 * quarter), ...arc(tl, tl, tl, 4 * quarter)]
67
55
  ],
68
- lineWeights: [
69
- node.borderTopWeight,
70
- node.borderRightWeight,
71
- node.borderBottomWeight,
72
- node.borderLeftWeight
73
- ]
56
+ lineWeights: [...borderWeights(node)]
74
57
  };
75
58
  }
76
- const FULL_TURN = Math.PI * 2;
77
- function ellipseContours(node, tolerance) {
78
- const rx = node.width / 2;
79
- const ry = node.height / 2;
59
+ /** Whether a layer is an unbroken ellipse: an ELLIPSE whose arc, if any, is a whole turn with no hole. */
60
+ function isWholeEllipse(node) {
61
+ if (node.type !== "ELLIPSE") return false;
80
62
  const arc = node.arcData;
81
- const start = arc?.startingAngle ?? 0;
82
- const end = arc?.endingAngle ?? FULL_TURN;
83
- const inner = arc?.innerRadius ?? 0;
84
- const whole = Math.abs(end - start) >= FULL_TURN - 1e-6;
85
- const outer = arcPoints(rx, ry, rx, ry, start, whole ? start + FULL_TURN : end, tolerance);
86
- if (inner <= 0) return [whole ? outer : [{
87
- x: rx,
88
- y: ry
89
- }, ...outer]];
90
- const hole = arcPoints(rx, ry, rx * inner, ry * inner, whole ? start + FULL_TURN : end, start, tolerance);
91
- return whole ? [outer, hole] : [[...outer, ...hole]];
63
+ return !arc || arc.innerRadius <= 0 && sweepsWholeTurn(arc.startingAngle, arc.endingAngle);
64
+ }
65
+ function ellipseContours(node, tolerance) {
66
+ const { cx, cy, contours } = arcOutline(node);
67
+ return contours.map((contour) => contour.flatMap((step) => step.type === "centre" ? [{
68
+ x: cx,
69
+ y: cy
70
+ }] : arcPoints(cx, cy, step.rx, step.ry, step.start, step.start + step.sweep, tolerance)));
92
71
  }
93
72
  function vectorOutline(node, tolerance) {
94
73
  const network = node.vectorNetwork;
@@ -176,6 +155,6 @@ function shapeOutline(node, tolerance) {
176
155
  }
177
156
  }
178
157
  //#endregion
179
- export { shapeOutline };
158
+ export { isWholeEllipse, shapeOutline };
180
159
 
181
160
  //# sourceMappingURL=shapes.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"shapes.js","names":[],"sources":["../../src/outline/shapes.ts"],"sourcesContent":["import { polygonOutline } from '../polygon'\nimport type { Vector } from '../primitives'\nimport type { SceneNode, WindingRule } from '../types'\nimport {\n arcPoints,\n blobContours,\n commandContours,\n loopPoints,\n segmentPoints,\n segmentRuns\n} from './flatten'\n\n/** Contours filled together under one winding rule. */\nexport interface OutlineArea {\n windingRule: WindingRule\n contours: Vector[][]\n}\n\n/**\n * A shape's geometry in its own coordinates, curves flattened: the areas its fills cover and the\n * lines its strokes follow, closed contours ending where they began.\n */\nexport interface ShapeOutline {\n areas: OutlineArea[]\n lines: Vector[][]\n /** Each line's own stroke weight, where sides differ, as a rectangle's independent borders. */\n lineWeights?: number[]\n /**\n * Strokes centre on the lines whatever their alignment, as on a network without regions that is\n * open or has no saved stroke geometry.\n */\n strokeCentered?: boolean\n /** For each line, whether its start and its end are open ends, where a stroke's cap is drawn. */\n lineEnds?: Array<[boolean, boolean]>\n /** The saved outline a stroke fills, for a shape whose stroke has no lines to follow. */\n strokeAreas?: OutlineArea[]\n}\n\nfunction closed(contour: Vector[]): Vector[] {\n const first = contour.at(0)\n const last = contour.at(-1)\n if (!first || !last || (first.x === last.x && first.y === last.y)) return contour\n return [...contour, first]\n}\n\nfunction fromContours(contours: Vector[][], windingRule: WindingRule = 'NONZERO'): ShapeOutline {\n return { areas: [{ windingRule, contours }], lines: contours.map(closed) }\n}\n\nfunction cornerRadii(node: SceneNode): [number, number, number, number] {\n const radii: [number, number, number, number] = node.independentCorners\n ? [node.topLeftRadius, node.topRightRadius, node.bottomRightRadius, node.bottomLeftRadius]\n : [node.cornerRadius, node.cornerRadius, node.cornerRadius, node.cornerRadius]\n const limit = Math.min(node.width, node.height) / 2\n return radii.map((radius) => Math.max(0, Math.min(radius, limit))) as typeof radii\n}\n\nfunction rectangleContour(node: SceneNode, tolerance: number): Vector[] {\n const { width: w, height: h } = node\n const [tl, tr, br, bl] = cornerRadii(node)\n const corner = (cx: number, cy: number, r: number, from: number): Vector[] =>\n r > 0 ? arcPoints(cx, cy, r, r, from, from + Math.PI / 2, tolerance) : [{ x: cx, y: cy }]\n return [\n ...corner(w - tr, tr, tr, -Math.PI / 2),\n ...corner(w - br, h - br, br, 0),\n ...corner(bl, h - bl, bl, Math.PI / 2),\n ...corner(tl, tl, tl, Math.PI)\n ]\n}\n\n/**\n * A rectangle's four sides with the weight each draws its border at: top, right, bottom, left.\n * Each side runs from the middle of one rounded corner to the middle of the next, as its border\n * is drawn round them.\n */\nfunction rectangleSides(\n node: SceneNode,\n tolerance: number\n): Pick<ShapeOutline, 'lines' | 'lineWeights'> {\n const { width: w, height: h } = node\n const [tl, tr, br, bl] = cornerRadii(node)\n const quarter = Math.PI / 4\n const arc = (cx: number, cy: number, r: number, from: number): Vector[] =>\n r > 0 ? arcPoints(cx, cy, r, r, from, from + quarter, tolerance) : [{ x: cx, y: cy }]\n return {\n lines: [\n [...arc(tl, tl, tl, 5 * quarter), ...arc(w - tr, tr, tr, -2 * quarter)],\n [...arc(w - tr, tr, tr, -quarter), ...arc(w - br, h - br, br, 0)],\n [...arc(w - br, h - br, br, quarter), ...arc(bl, h - bl, bl, 2 * quarter)],\n [...arc(bl, h - bl, bl, 3 * quarter), ...arc(tl, tl, tl, 4 * quarter)]\n ],\n lineWeights: [\n node.borderTopWeight,\n node.borderRightWeight,\n node.borderBottomWeight,\n node.borderLeftWeight\n ]\n }\n}\n\nconst FULL_TURN = Math.PI * 2\n\nfunction ellipseContours(node: SceneNode, tolerance: number): Vector[][] {\n const rx = node.width / 2\n const ry = node.height / 2\n const arc = node.arcData\n const start = arc?.startingAngle ?? 0\n const end = arc?.endingAngle ?? FULL_TURN\n const inner = arc?.innerRadius ?? 0\n const whole = Math.abs(end - start) >= FULL_TURN - 1e-6\n const outer = arcPoints(rx, ry, rx, ry, start, whole ? start + FULL_TURN : end, tolerance)\n if (inner <= 0) return [whole ? outer : [{ x: rx, y: ry }, ...outer]]\n const hole = arcPoints(\n rx,\n ry,\n rx * inner,\n ry * inner,\n whole ? start + FULL_TURN : end,\n start,\n tolerance\n )\n // A whole ring is two loops wound opposite ways; a part ring is one outline across its ends.\n return whole ? [outer, hole] : [[...outer, ...hole]]\n}\n\nfunction vectorOutline(node: SceneNode, tolerance: number): ShapeOutline | null {\n const network = node.vectorNetwork\n const geometry = node.fillGeometry.map((path) => ({\n windingRule: path.windingRule,\n contours: path.commandsBlob.length > 0 ? blobContours(path.commandsBlob, tolerance) : []\n }))\n if (!network) {\n if (geometry.length === 0 && node.strokeGeometry.length === 0) return null\n // Without a network the renderer fills the saved stroke geometry, or draws no stroke.\n const strokeAreas = node.strokeGeometry.map((path) => ({\n windingRule: path.windingRule,\n contours: path.commandsBlob.length > 0 ? blobContours(path.commandsBlob, tolerance) : []\n }))\n return { areas: geometry, lines: [], strokeAreas }\n }\n // A vertex on one segment only is an open end.\n const degree = new Map<number, number>()\n for (const segment of network.segments) {\n degree.set(segment.start, (degree.get(segment.start) ?? 0) + 1)\n degree.set(segment.end, (degree.get(segment.end) ?? 0) + 1)\n }\n const lines: Vector[][] = []\n const lineEnds: Array<[boolean, boolean]> = []\n for (const [index, segment] of network.segments.entries()) {\n const points = segmentPoints(network, index, tolerance)\n if (!points) continue\n lines.push(points)\n lineEnds.push([degree.get(segment.start) === 1, degree.get(segment.end) === 1])\n }\n // Without regions the stroke draws centred on each segment, unless a closed network carries\n // saved stroke geometry, which the renderer draws with its alignment.\n const open = lineEnds.some(([start, end]) => start || end)\n const strokeCentered = network.regions.length === 0 && (open || node.strokeGeometry.length === 0)\n if (geometry.length > 0) return { areas: geometry, lines, lineEnds, strokeCentered }\n const areas =\n network.regions.length > 0\n ? network.regions.map((region) => ({\n windingRule: region.windingRule,\n contours: region.loops.map((loop) => loopPoints(network, loop, tolerance))\n }))\n : // Without regions a fill closes each run of segments, as a path fill does.\n [{ windingRule: 'NONZERO' as const, contours: segmentRuns(network, tolerance) }]\n return { areas, lines, lineEnds, strokeCentered }\n}\n\n/**\n * The outline a click is tested against, in the node's own coordinates, with curves within\n * `tolerance` of the shape; null for layers tested by their box, such as text.\n */\nexport function shapeOutline(node: SceneNode, tolerance: number): ShapeOutline | null {\n switch (node.type) {\n case 'RECTANGLE':\n case 'ROUNDED_RECTANGLE': {\n const outline = fromContours([rectangleContour(node, tolerance)])\n return node.independentStrokeWeights\n ? { ...outline, ...rectangleSides(node, tolerance) }\n : outline\n }\n case 'ELLIPSE': {\n if (node.fillGeometry.length > 0) return vectorOutline(node, tolerance)\n return fromContours(ellipseContours(node, tolerance))\n }\n case 'POLYGON':\n case 'STAR':\n return fromContours(commandContours(polygonOutline(node), tolerance))\n case 'LINE':\n return {\n areas: [],\n lines: [\n [\n { x: 0, y: 0 },\n { x: node.width, y: node.height }\n ]\n ],\n lineEnds: [[true, true]]\n }\n case 'VECTOR':\n return vectorOutline(node, tolerance)\n default:\n return null\n }\n}\n"],"mappings":";;;AAsCA,SAAS,OAAO,SAA6B;CAC3C,MAAM,QAAQ,QAAQ,GAAG,CAAC;CAC1B,MAAM,OAAO,QAAQ,GAAG,EAAE;CAC1B,IAAI,CAAC,SAAS,CAAC,QAAS,MAAM,MAAM,KAAK,KAAK,MAAM,MAAM,KAAK,GAAI,OAAO;CAC1E,OAAO,CAAC,GAAG,SAAS,KAAK;AAC3B;AAEA,SAAS,aAAa,UAAsB,cAA2B,WAAyB;CAC9F,OAAO;EAAE,OAAO,CAAC;GAAE;GAAa;EAAS,CAAC;EAAG,OAAO,SAAS,IAAI,MAAM;CAAE;AAC3E;AAEA,SAAS,YAAY,MAAmD;CACtE,MAAM,QAA0C,KAAK,qBACjD;EAAC,KAAK;EAAe,KAAK;EAAgB,KAAK;EAAmB,KAAK;CAAgB,IACvF;EAAC,KAAK;EAAc,KAAK;EAAc,KAAK;EAAc,KAAK;CAAY;CAC/E,MAAM,QAAQ,KAAK,IAAI,KAAK,OAAO,KAAK,MAAM,IAAI;CAClD,OAAO,MAAM,KAAK,WAAW,KAAK,IAAI,GAAG,KAAK,IAAI,QAAQ,KAAK,CAAC,CAAC;AACnE;AAEA,SAAS,iBAAiB,MAAiB,WAA6B;CACtE,MAAM,EAAE,OAAO,GAAG,QAAQ,MAAM;CAChC,MAAM,CAAC,IAAI,IAAI,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2BACR;IAAE,GAAG;IAAS,GAAG,eAAe,MAAM,SAAS;GAAE,IACjD;EACN;EACA,KAAK;GACH,IAAI,KAAK,aAAa,SAAS,GAAG,OAAO,cAAc,MAAM,SAAS;GACtE,OAAO,aAAa,gBAAgB,MAAM,SAAS,CAAC;EAEtD,KAAK;EACL,KAAK,QACH,OAAO,aAAa,gBAAgB,eAAe,IAAI,GAAG,SAAS,CAAC;EACtE,KAAK,QACH,OAAO;GACL,OAAO,CAAC;GACR,OAAO,CACL,CACE;IAAE,GAAG;IAAG,GAAG;GAAE,GACb;IAAE,GAAG,KAAK;IAAO,GAAG,KAAK;GAAO,CAClC,CACF;GACA,UAAU,CAAC,CAAC,MAAM,IAAI,CAAC;EACzB;EACF,KAAK,UACH,OAAO,cAAc,MAAM,SAAS;EACtC,SACE,OAAO;CACX;AACF"}
1
+ {"version":3,"file":"shapes.js","names":[],"sources":["../../src/outline/shapes.ts"],"sourcesContent":["import { nodeCornerRadii } from '../corners'\nimport { borderWeights, sweepsWholeTurn } from '../geometry'\nimport { arcOutline } from '../paths/arc'\nimport { polygonOutline } from '../polygon'\nimport type { Vector } from '../primitives'\nimport type { SceneNode, WindingRule } from '../types'\nimport {\n arcPoints,\n blobContours,\n commandContours,\n loopPoints,\n segmentPoints,\n segmentRuns\n} from './flatten'\n\n/** Contours filled together under one winding rule. */\nexport interface OutlineArea {\n windingRule: WindingRule\n contours: Vector[][]\n}\n\n/**\n * A shape's geometry in its own coordinates, curves flattened: the areas its fills cover and the\n * lines its strokes follow, closed contours ending where they began.\n */\nexport interface ShapeOutline {\n areas: OutlineArea[]\n lines: Vector[][]\n /** Each line's own stroke weight, where sides differ, as a rectangle's independent borders. */\n lineWeights?: number[]\n /**\n * Strokes centre on the lines whatever their alignment, as on a network without regions that is\n * open or has no saved stroke geometry.\n */\n strokeCentered?: boolean\n /** For each line, whether its start and its end are open ends, where a stroke's cap is drawn. */\n lineEnds?: Array<[boolean, boolean]>\n /** The saved outline a stroke fills, for a shape whose stroke has no lines to follow. */\n strokeAreas?: OutlineArea[]\n}\n\nfunction closed(contour: Vector[]): Vector[] {\n const first = contour.at(0)\n const last = contour.at(-1)\n if (!first || !last || (first.x === last.x && first.y === last.y)) return contour\n return [...contour, first]\n}\n\nfunction fromContours(contours: Vector[][], windingRule: WindingRule = 'NONZERO'): ShapeOutline {\n return { areas: [{ windingRule, contours }], lines: contours.map(closed) }\n}\n\nfunction rectangleContour(node: SceneNode, tolerance: number): Vector[] {\n const { width: w, height: h } = node\n const [tl, tr, br, bl] = nodeCornerRadii(node)\n const corner = (cx: number, cy: number, r: number, from: number): Vector[] =>\n r > 0 ? arcPoints(cx, cy, r, r, from, from + Math.PI / 2, tolerance) : [{ x: cx, y: cy }]\n return [\n ...corner(w - tr, tr, tr, -Math.PI / 2),\n ...corner(w - br, h - br, br, 0),\n ...corner(bl, h - bl, bl, Math.PI / 2),\n ...corner(tl, tl, tl, Math.PI)\n ]\n}\n\n/**\n * A rectangle's four sides with the weight each draws its border at: top, right, bottom, left.\n * Each side runs from the middle of one rounded corner to the middle of the next, as its border\n * is drawn round them.\n */\nfunction rectangleSides(\n node: SceneNode,\n tolerance: number\n): Pick<ShapeOutline, 'lines' | 'lineWeights'> {\n const { width: w, height: h } = node\n const [tl, tr, br, bl] = nodeCornerRadii(node)\n const quarter = Math.PI / 4\n const arc = (cx: number, cy: number, r: number, from: number): Vector[] =>\n r > 0 ? arcPoints(cx, cy, r, r, from, from + quarter, tolerance) : [{ x: cx, y: cy }]\n return {\n lines: [\n [...arc(tl, tl, tl, 5 * quarter), ...arc(w - tr, tr, tr, -2 * quarter)],\n [...arc(w - tr, tr, tr, -quarter), ...arc(w - br, h - br, br, 0)],\n [...arc(w - br, h - br, br, quarter), ...arc(bl, h - bl, bl, 2 * quarter)],\n [...arc(bl, h - bl, bl, 3 * quarter), ...arc(tl, tl, tl, 4 * quarter)]\n ],\n lineWeights: [...borderWeights(node)]\n }\n}\n\n/** Whether a layer is an unbroken ellipse: an ELLIPSE whose arc, if any, is a whole turn with no hole. */\nexport function isWholeEllipse(node: Pick<SceneNode, 'type' | 'arcData'>): boolean {\n if (node.type !== 'ELLIPSE') return false\n const arc = node.arcData\n return !arc || (arc.innerRadius <= 0 && sweepsWholeTurn(arc.startingAngle, arc.endingAngle))\n}\n\nfunction ellipseContours(node: SceneNode, tolerance: number): Vector[][] {\n const { cx, cy, contours } = arcOutline(node)\n return contours.map((contour) =>\n contour.flatMap((step) =>\n step.type === 'centre'\n ? [{ x: cx, y: cy }]\n : arcPoints(cx, cy, step.rx, step.ry, step.start, step.start + step.sweep, tolerance)\n )\n )\n}\n\nfunction vectorOutline(node: SceneNode, tolerance: number): ShapeOutline | null {\n const network = node.vectorNetwork\n const geometry = node.fillGeometry.map((path) => ({\n windingRule: path.windingRule,\n contours: path.commandsBlob.length > 0 ? blobContours(path.commandsBlob, tolerance) : []\n }))\n if (!network) {\n if (geometry.length === 0 && node.strokeGeometry.length === 0) return null\n // Without a network the renderer fills the saved stroke geometry, or draws no stroke.\n const strokeAreas = node.strokeGeometry.map((path) => ({\n windingRule: path.windingRule,\n contours: path.commandsBlob.length > 0 ? blobContours(path.commandsBlob, tolerance) : []\n }))\n return { areas: geometry, lines: [], strokeAreas }\n }\n // A vertex on one segment only is an open end.\n const degree = new Map<number, number>()\n for (const segment of network.segments) {\n degree.set(segment.start, (degree.get(segment.start) ?? 0) + 1)\n degree.set(segment.end, (degree.get(segment.end) ?? 0) + 1)\n }\n const lines: Vector[][] = []\n const lineEnds: Array<[boolean, boolean]> = []\n for (const [index, segment] of network.segments.entries()) {\n const points = segmentPoints(network, index, tolerance)\n if (!points) continue\n lines.push(points)\n lineEnds.push([degree.get(segment.start) === 1, degree.get(segment.end) === 1])\n }\n // Without regions the stroke draws centred on each segment, unless a closed network carries\n // saved stroke geometry, which the renderer draws with its alignment.\n const open = lineEnds.some(([start, end]) => start || end)\n const strokeCentered = network.regions.length === 0 && (open || node.strokeGeometry.length === 0)\n if (geometry.length > 0) return { areas: geometry, lines, lineEnds, strokeCentered }\n const areas =\n network.regions.length > 0\n ? network.regions.map((region) => ({\n windingRule: region.windingRule,\n contours: region.loops.map((loop) => loopPoints(network, loop, tolerance))\n }))\n : // Without regions a fill closes each run of segments, as a path fill does.\n [{ windingRule: 'NONZERO' as const, contours: segmentRuns(network, tolerance) }]\n return { areas, lines, lineEnds, strokeCentered }\n}\n\n/**\n * The outline a click is tested against, in the node's own coordinates, with curves within\n * `tolerance` of the shape; null for layers tested by their box, such as text.\n */\nexport function shapeOutline(node: SceneNode, tolerance: number): ShapeOutline | null {\n switch (node.type) {\n case 'RECTANGLE':\n case 'ROUNDED_RECTANGLE': {\n const outline = fromContours([rectangleContour(node, tolerance)])\n return node.independentStrokeWeights\n ? { ...outline, ...rectangleSides(node, tolerance) }\n : outline\n }\n case 'ELLIPSE': {\n if (node.fillGeometry.length > 0) return vectorOutline(node, tolerance)\n return fromContours(ellipseContours(node, tolerance))\n }\n case 'POLYGON':\n case 'STAR':\n return fromContours(commandContours(polygonOutline(node), tolerance))\n case 'LINE':\n return {\n areas: [],\n lines: [\n [\n { x: 0, y: 0 },\n { x: node.width, y: node.height }\n ]\n ],\n lineEnds: [[true, true]]\n }\n case 'VECTOR':\n return vectorOutline(node, tolerance)\n default:\n return null\n }\n}\n"],"mappings":";;;;;;AAyCA,SAAS,OAAO,SAA6B;CAC3C,MAAM,QAAQ,QAAQ,GAAG,CAAC;CAC1B,MAAM,OAAO,QAAQ,GAAG,EAAE;CAC1B,IAAI,CAAC,SAAS,CAAC,QAAS,MAAM,MAAM,KAAK,KAAK,MAAM,MAAM,KAAK,GAAI,OAAO;CAC1E,OAAO,CAAC,GAAG,SAAS,KAAK;AAC3B;AAEA,SAAS,aAAa,UAAsB,cAA2B,WAAyB;CAC9F,OAAO;EAAE,OAAO,CAAC;GAAE;GAAa;EAAS,CAAC;EAAG,OAAO,SAAS,IAAI,MAAM;CAAE;AAC3E;AAEA,SAAS,iBAAiB,MAAiB,WAA6B;C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,gBAAgB,MAAM,SAAS,CAAC;EAEtD,KAAK;EACL,KAAK,QACH,OAAO,aAAa,gBAAgB,eAAe,IAAI,GAAG,SAAS,CAAC;EACtE,KAAK,QACH,OAAO;GACL,OAAO,CAAC;GACR,OAAO,CACL,CACE;IAAE,GAAG;IAAG,GAAG;GAAE,GACb;IAAE,GAAG,KAAK;IAAO,GAAG,KAAK;GAAO,CAClC,CACF;GACA,UAAU,CAAC,CAAC,MAAM,IAAI,CAAC;EACzB;EACF,KAAK,UACH,OAAO,cAAc,MAAM,SAAS;EACtC,SACE,OAAO;CACX;AACF"}
@@ -1,2 +1,4 @@
1
+ import { PatternTileLayout, patternTileLayout } from "./pattern.js";
1
2
  import { fillSchema, gradientStopSchema, paintTransformSchema } from "./schema.js";
2
- export { fillSchema, gradientStopSchema, paintTransformSchema };
3
+ import { paintShows } from "./shows.js";
4
+ export { PatternTileLayout, fillSchema, gradientStopSchema, paintShows, paintTransformSchema, patternTileLayout };
@@ -1,2 +1,4 @@
1
+ import { paintShows } from "./shows.js";
2
+ import { patternTileLayout } from "./pattern.js";
1
3
  import { fillSchema, gradientStopSchema, paintTransformSchema } from "./schema.js";
2
- export { fillSchema, gradientStopSchema, paintTransformSchema };
4
+ export { fillSchema, gradientStopSchema, paintShows, paintTransformSchema, patternTileLayout };
@@ -0,0 +1,13 @@
1
+ import { Rect, Vector } from "../primitives/index.js";
2
+ import { Fill, SceneNode } from "../types.js";
3
+ //#region src/paints/pattern.d.ts
4
+ /** Where a pattern's source repeats in one tile: the tile, the source's scale, and its copies. */
5
+ interface PatternTileLayout {
6
+ rect: Rect;
7
+ scale: number;
8
+ positions: Vector[];
9
+ }
10
+ declare function patternTileLayout(source: Pick<SceneNode, 'width' | 'height'>, fill: Fill): PatternTileLayout;
11
+ //#endregion
12
+ export { PatternTileLayout, patternTileLayout };
13
+ //# sourceMappingURL=pattern.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pattern.d.ts","names":[],"sources":["../../src/paints/pattern.ts"],"mappings":";;;;UAIiB;EACf,MAAM;EACN;EACA,WAAW;;iBAcG,kBACd,QAAQ,KAAK,gCACb,MAAM,OACL"}
@@ -0,0 +1,47 @@
1
+ //#region src/paints/pattern.ts
2
+ function patternAlignmentOffset(alignment, gap, sourceSize, axis) {
3
+ if (alignment === "CENTER") return axis === "x" ? -gap / 2 : -sourceSize / 2;
4
+ if (alignment === "END") return -gap;
5
+ return 0;
6
+ }
7
+ function patternTileLayout(source, fill) {
8
+ const scale = fill.scale && fill.scale > 0 ? fill.scale : 1;
9
+ const spacing = fill.patternSpacing ?? {
10
+ x: 0,
11
+ y: 0
12
+ };
13
+ const scaledWidth = source.width * scale;
14
+ const scaledHeight = source.height * scale;
15
+ const gapX = scaledWidth * spacing.x;
16
+ const gapY = scaledHeight * spacing.y;
17
+ const width = scaledWidth + gapX;
18
+ const height = scaledHeight + gapY;
19
+ const x = patternAlignmentOffset(fill.horizontalAlignment, gapX, scaledWidth, "x");
20
+ const y = patternAlignmentOffset(fill.verticalAlignment, gapY, scaledHeight, "y");
21
+ const positions = [{
22
+ x,
23
+ y
24
+ }];
25
+ if (fill.patternTileType === "HORIZONTAL_HEXAGONAL") positions.push({
26
+ x: x + width / 2,
27
+ y: y + height / 2
28
+ });
29
+ else if (fill.patternTileType === "VERTICAL_HEXAGONAL") positions.push({
30
+ x: x + width / 2,
31
+ y: y - height / 2
32
+ });
33
+ return {
34
+ rect: {
35
+ x: 0,
36
+ y: 0,
37
+ width,
38
+ height
39
+ },
40
+ scale,
41
+ positions
42
+ };
43
+ }
44
+ //#endregion
45
+ export { patternTileLayout };
46
+
47
+ //# sourceMappingURL=pattern.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"pattern.js","names":[],"sources":["../../src/paints/pattern.ts"],"sourcesContent":["import type { Rect, Vector } from '../primitives'\nimport type { Fill, SceneNode } from '../types'\n\n/** Where a pattern's source repeats in one tile: the tile, the source's scale, and its copies. */\nexport interface PatternTileLayout {\n rect: Rect\n scale: number\n positions: Vector[]\n}\n\nfunction patternAlignmentOffset(\n alignment: Fill['horizontalAlignment'],\n gap: number,\n sourceSize: number,\n axis: 'x' | 'y'\n): number {\n if (alignment === 'CENTER') return axis === 'x' ? -gap / 2 : -sourceSize / 2\n if (alignment === 'END') return -gap\n return 0\n}\n\nexport function patternTileLayout(\n source: Pick<SceneNode, 'width' | 'height'>,\n fill: Fill\n): PatternTileLayout {\n const scale = fill.scale && fill.scale > 0 ? fill.scale : 1\n const spacing = fill.patternSpacing ?? { x: 0, y: 0 }\n const scaledWidth = source.width * scale\n const scaledHeight = source.height * scale\n const gapX = scaledWidth * spacing.x\n const gapY = scaledHeight * spacing.y\n const width = scaledWidth + gapX\n const height = scaledHeight + gapY\n const x = patternAlignmentOffset(fill.horizontalAlignment, gapX, scaledWidth, 'x')\n const y = patternAlignmentOffset(fill.verticalAlignment, gapY, scaledHeight, 'y')\n const positions = [{ x, y }]\n\n if (fill.patternTileType === 'HORIZONTAL_HEXAGONAL') {\n positions.push({ x: x + width / 2, y: y + height / 2 })\n } else if (fill.patternTileType === 'VERTICAL_HEXAGONAL') {\n positions.push({ x: x + width / 2, y: y - height / 2 })\n }\n\n return { rect: { x: 0, y: 0, width, height }, scale, positions }\n}\n"],"mappings":";AAUA,SAAS,uBACP,WACA,KACA,YACA,MACQ;CACR,IAAI,cAAc,UAAU,OAAO,SAAS,MAAM,CAAC,MAAM,IAAI,CAAC,aAAa;CAC3E,IAAI,cAAc,OAAO,OAAO,CAAC;CACjC,OAAO;AACT;AAEA,SAAgB,kBACd,QACA,MACmB;CACnB,MAAM,QAAQ,KAAK,SAAS,KAAK,QAAQ,IAAI,KAAK,QAAQ;CAC1D,MAAM,UAAU,KAAK,kBAAkB;EAAE,GAAG;EAAG,GAAG;CAAE;CACpD,MAAM,cAAc,OAAO,QAAQ;CACnC,MAAM,eAAe,OAAO,SAAS;CACrC,MAAM,OAAO,cAAc,QAAQ;CACnC,MAAM,OAAO,eAAe,QAAQ;CACpC,MAAM,QAAQ,cAAc;CAC5B,MAAM,SAAS,eAAe;CAC9B,MAAM,IAAI,uBAAuB,KAAK,qBAAqB,MAAM,aAAa,GAAG;CACjF,MAAM,IAAI,uBAAuB,KAAK,mBAAmB,MAAM,cAAc,GAAG;CAChF,MAAM,YAAY,CAAC;EAAE;EAAG;CAAE,CAAC;CAE3B,IAAI,KAAK,oBAAoB,wBAC3B,UAAU,KAAK;EAAE,GAAG,IAAI,QAAQ;EAAG,GAAG,IAAI,SAAS;CAAE,CAAC;MACjD,IAAI,KAAK,oBAAoB,sBAClC,UAAU,KAAK;EAAE,GAAG,IAAI,QAAQ;EAAG,GAAG,IAAI,SAAS;CAAE,CAAC;CAGxD,OAAO;EAAE,MAAM;GAAE,GAAG;GAAG,GAAG;GAAG;GAAO;EAAO;EAAG;EAAO;CAAU;AACjE"}
@@ -0,0 +1,11 @@
1
+ import { Fill } from "../types.js";
2
+ //#region src/paints/shows.d.ts
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
+ declare function paintShows(paint: Fill): boolean;
9
+ //#endregion
10
+ export { paintShows };
11
+ //# sourceMappingURL=shows.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shows.d.ts","names":[],"sources":["../../src/paints/shows.ts"],"mappings":";;;;;;;iBAOgB,WAAW,OAAO"}
@@ -0,0 +1,16 @@
1
+ //#region src/paints/shows.ts
2
+ /**
3
+ * Whether a paint shows anything: visible, above 0% opacity, and not wholly transparent in its own
4
+ * color or every gradient stop. Figma casts no shadow from a paint that does not, counts it in no
5
+ * render bounds, and does not click through it to its layer (Figma desktop, live).
6
+ */
7
+ function paintShows(paint) {
8
+ if (!paint.visible || !(paint.opacity > 0)) return false;
9
+ if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0);
10
+ if (paint.type === "SOLID") return paint.color.a > 0;
11
+ return true;
12
+ }
13
+ //#endregion
14
+ export { paintShows };
15
+
16
+ //# sourceMappingURL=shows.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"shows.js","names":[],"sources":["../../src/paints/shows.ts"],"sourcesContent":["import type { Fill } from '../types'\n\n/**\n * Whether a paint shows anything: visible, above 0% opacity, and not wholly transparent in its own\n * color or every gradient stop. Figma casts no shadow from a paint that does not, counts it in no\n * render bounds, and does not click through it to its layer (Figma desktop, live).\n */\nexport function paintShows(paint: Fill): boolean {\n if (!paint.visible || !(paint.opacity > 0)) return false\n if (paint.gradientStops) return paint.gradientStops.some((stop) => stop.color.a > 0)\n if (paint.type === 'SOLID') return paint.color.a > 0\n return true\n}\n"],"mappings":";;;;;;AAOA,SAAgB,WAAW,OAAsB;CAC/C,IAAI,CAAC,MAAM,WAAW,EAAE,MAAM,UAAU,IAAI,OAAO;CACnD,IAAI,MAAM,eAAe,OAAO,MAAM,cAAc,MAAM,SAAS,KAAK,MAAM,IAAI,CAAC;CACnF,IAAI,MAAM,SAAS,SAAS,OAAO,MAAM,MAAM,IAAI;CACnD,OAAO;AACT"}
@@ -1,3 +1,4 @@
1
+ import { Rect } from "./primitives/index.js";
1
2
  import { VectorNetwork, WindingRule } from "./types.js";
2
3
  //#region src/parse-path.d.ts
3
4
  type VectorPathWindingRule = WindingRule | 'NONE';
@@ -31,6 +32,8 @@ declare function parseSVGPathData(d: string, windingRule: WindingRule, options:
31
32
  }): SVGPathParseResult;
32
33
  /** Parse the strict absolute command subset accepted by Figma's VectorPath API. */
33
34
  declare function parsePluginVectorPath(d: string, windingRule: VectorPathWindingRule): SVGPathParseResult;
35
+ /** A `viewBox` value as a rect, or null when it is malformed or does not establish a viewport. */
36
+ declare function parseSVGViewBoxValue(value: string | null | undefined): Rect | null;
34
37
  //#endregion
35
- export { SVGPathParseResult, VectorPathWindingRule, parsePluginVectorPath, parseSVGPath, parseSVGPathData };
38
+ export { SVGPathParseResult, VectorPathWindingRule, parsePluginVectorPath, parseSVGPath, parseSVGPathData, parseSVGViewBoxValue };
36
39
  //# sourceMappingURL=parse-path.d.ts.map
@@ -1 +1 @@
1
- {"version":3,"file":"parse-path.d.ts","names":[],"sources":["../src/parse-path.ts"],"mappings":";;KAUY,wBAAwB;KAIxB;EAAuB;EAAU,SAAS;;EAAoB;EAAW;;;;;;;;;;;;iBAiBrE,aACd,WACA,cAAa,aACb;EAAW;IACV;;;;;;iBAaa,iBACd,WACA,aAAa,aACb;EAAW;IACV;;iBAKa,sBACd,WACA,aAAa,wBACZ"}
1
+ {"version":3,"file":"parse-path.d.ts","names":[],"sources":["../src/parse-path.ts"],"mappings":";;;KAWY,wBAAwB;KAIxB;EAAuB;EAAU,SAAS;;EAAoB;EAAW;;;;;;;;;;;;iBAiBrE,aACd,WACA,cAAa,aACb;EAAW;IACV;;;;;;iBAaa,iBACd,WACA,aAAa,aACb;EAAW;IACV;;iBAKa,sBACd,WACA,aAAa,wBACZ;;iBA0La,qBAAqB,mCAAmC"}
@@ -194,7 +194,21 @@ function parsePath(d, windingRule, options) {
194
194
  }
195
195
  };
196
196
  }
197
+ /** A `viewBox` value as a rect, or null when it is malformed or does not establish a viewport. */
198
+ function parseSVGViewBoxValue(value) {
199
+ if (!value) return null;
200
+ const values = value.trim().split(/[\s,]+/).map(Number);
201
+ if (values.length !== 4 || values.some((entry) => !Number.isFinite(entry))) return null;
202
+ const [x = 0, y = 0, width = 0, height = 0] = values;
203
+ if (width <= 0 || height <= 0) return null;
204
+ return {
205
+ x,
206
+ y,
207
+ width,
208
+ height
209
+ };
210
+ }
197
211
  //#endregion
198
- export { parsePluginVectorPath, parseSVGPath, parseSVGPathData };
212
+ export { parsePluginVectorPath, parseSVGPath, parseSVGPathData, parseSVGViewBoxValue };
199
213
 
200
214
  //# sourceMappingURL=parse-path.js.map