@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
package/src/css/grid.ts CHANGED
@@ -1,5 +1,8 @@
1
1
  import valueParser, { type FunctionNode, type Node } from 'postcss-value-parser'
2
2
 
3
+ import { appendAll } from '@open-pencil/std/arrays'
4
+
5
+ import { MAX_GRID_TRACKS } from '../layout/limits'
3
6
  import type { GridTrack } from '../types'
4
7
  import { parseCSSNumber } from './values'
5
8
 
@@ -44,7 +47,14 @@ function functionTracks(node: FunctionNode): GridTrack[] {
44
47
  // `auto-fill` and `auto-fit` depend on the container, which a track list cannot know.
45
48
  if (!Number.isInteger(count) || count < 1) return [autoTrack()]
46
49
  const tracks = tracksOf([second, ...rest].flat())
47
- return Array.from({ length: Math.min(count, MAX_REPEATED_TRACKS) }, () =>
50
+ if (tracks.length === 0) return []
51
+ // Nested repeats multiply, so the count is bounded by the tracks a template may hold too.
52
+ const repeats = Math.min(
53
+ count,
54
+ MAX_REPEATED_TRACKS,
55
+ Math.floor(MAX_GRID_TRACKS / tracks.length)
56
+ )
57
+ return Array.from({ length: Math.max(1, repeats) }, () =>
48
58
  tracks.map((track) => ({ ...track }))
49
59
  ).flat()
50
60
  }
@@ -56,15 +66,47 @@ function functionTracks(node: FunctionNode): GridTrack[] {
56
66
  }
57
67
  }
58
68
 
69
+ /**
70
+ * A word's track sizes past the line names in it, such as `[start]200px[end]`, which the value
71
+ * parser keeps as one word; names may run across words, as in `[a b]`, so whether the word
72
+ * starts inside them comes in and whether it ends inside them goes out.
73
+ */
74
+ function sizesOutsideNames(word: string, inNames: boolean): { sizes: string[]; inNames: boolean } {
75
+ const sizes: string[] = []
76
+ let size = ''
77
+ let names = inNames
78
+ for (const char of word) {
79
+ if (char === '[' || char === ']') {
80
+ if (size) sizes.push(size)
81
+ size = ''
82
+ names = char === '['
83
+ } else if (!names) {
84
+ size += char
85
+ }
86
+ }
87
+ if (size) sizes.push(size)
88
+ return { sizes, inNames: names }
89
+ }
90
+
59
91
  function tracksOf(nodes: Node[]): GridTrack[] {
60
- return nodes.flatMap((node) => {
61
- if (node.type === 'word') return [wordTrack(node.value)]
62
- if (node.type === 'function') return functionTracks(node)
63
- return []
64
- })
92
+ const tracks: GridTrack[] = []
93
+ // Line names, such as `[start]` or `[a b]`, name the lines between tracks and size none.
94
+ let inNames = false
95
+ for (const node of nodes) {
96
+ if (tracks.length >= MAX_GRID_TRACKS) break
97
+ if (node.type === 'word') {
98
+ const word = sizesOutsideNames(node.value, inNames)
99
+ inNames = word.inNames
100
+ appendAll(tracks, word.sizes.map(wordTrack))
101
+ } else if (node.type === 'function') appendAll(tracks, functionTracks(node))
102
+ }
103
+ return tracks.slice(0, MAX_GRID_TRACKS)
65
104
  }
66
105
 
67
- /** The tracks of a CSS track list, such as `repeat(3, minmax(0, 1fr)) 120px auto`. */
106
+ /**
107
+ * The tracks of a CSS track list, such as `repeat(3, minmax(0, 1fr)) 120px auto`, at most
108
+ * `MAX_GRID_TRACKS` of them however the list nests `repeat()`.
109
+ */
68
110
  export function parseCSSGridTracks(value: string): GridTrack[] {
69
111
  return tracksOf(valueParser(value).nodes)
70
112
  }
package/src/css/index.ts CHANGED
@@ -3,8 +3,10 @@ export { parseCSSGridTracks } from './grid'
3
3
  export {
4
4
  parseCSSAlignItems,
5
5
  parseCSSAlignSelf,
6
+ parseCSSAspectRatio,
6
7
  parseCSSFlexDirection,
7
- parseCSSJustifyContent
8
+ parseCSSJustifyContent,
9
+ type CSSAspectRatio
8
10
  } from './layout'
9
11
  export { parseCSSShadows } from './shadows'
10
12
  export {
@@ -24,4 +26,13 @@ export {
24
26
  type CSSTransformBox,
25
27
  type CSSTransformStyle
26
28
  } from './transform'
27
- export { parseCSSColor, parseCSSNumber, splitCSSValue } from './values'
29
+ export {
30
+ dimension,
31
+ parseCSSColor,
32
+ parseCSSNumber,
33
+ parseCSSURLReferences,
34
+ parseCSSURLs,
35
+ parsePaintFallback,
36
+ parseFragmentReference,
37
+ splitCSSValue
38
+ } from './values'
package/src/css/layout.ts CHANGED
@@ -51,3 +51,39 @@ export function parseCSSAlignSelf(value: string | undefined): LayoutAlignSelf |
51
51
  const keyword = alignmentKeyword(value)
52
52
  return keyword === 'normal' ? 'STRETCH' : parseLayoutAlignSelf(keyword)
53
53
  }
54
+
55
+ /** An `aspect-ratio`: width over height, and whether an `auto` stands beside it. */
56
+ export interface CSSAspectRatio {
57
+ ratio: number
58
+ /** With `auto`, the ratio sizes the content box, whatever `box-sizing` says. */
59
+ auto: boolean
60
+ }
61
+
62
+ /**
63
+ * The ratio an `aspect-ratio` value sizes a box by: a number, or a width and a height either
64
+ * side of a slash, with an `auto` on either side or none. Null for `auto` alone and for a ratio
65
+ * with a zero, negative, or infinite side.
66
+ */
67
+ export function parseCSSAspectRatio(value: string | undefined): CSSAspectRatio | null {
68
+ if (!value) return null
69
+ const isAuto = (node: valueParser.Node) =>
70
+ node.type === 'word' && node.value.toLowerCase() === 'auto'
71
+ const all = valueParser(value).nodes.filter(
72
+ (node) => node.type !== 'space' && node.type !== 'comment'
73
+ )
74
+ const nodes = all.filter((node) => !isAuto(node))
75
+ const auto = nodes.length < all.length
76
+ const [width, slash, height] = nodes
77
+ if (nodes.length === 0 || width.type !== 'word') return null
78
+ const across = Number(width.value)
79
+ if (nodes.length === 1) return positiveRatio(across, auto)
80
+ if (nodes.length !== 3 || slash.type !== 'div' || slash.value !== '/' || height.type !== 'word') {
81
+ return null
82
+ }
83
+ const down = Number(height.value)
84
+ return down > 0 ? positiveRatio(across / down, auto) : null
85
+ }
86
+
87
+ function positiveRatio(ratio: number, auto: boolean): CSSAspectRatio | null {
88
+ return Number.isFinite(ratio) && ratio > 0 ? { ratio, auto } : null
89
+ }
package/src/css/values.ts CHANGED
@@ -1,4 +1,6 @@
1
+ import { compact } from 'es-toolkit'
1
2
  import valueParser from 'postcss-value-parser'
3
+ import { decode, parsePath } from 'ufo'
2
4
 
3
5
  import { tryParseColor } from '../color'
4
6
  import type { Color } from '../primitives'
@@ -48,3 +50,50 @@ export function splitCSSValue(value: string): string[] {
48
50
  .nodes.filter((node) => node.type !== 'space' && node.type !== 'comment')
49
51
  .map((node) => valueParser.stringify(node))
50
52
  }
53
+
54
+ /**
55
+ * The id of the element a reference points to in its own document, as `href="#mark"` and
56
+ * `url(#clip)` point, decoded; null for a reference to another file or without a fragment.
57
+ */
58
+ export function parseFragmentReference(reference: string | null | undefined): string | null {
59
+ if (!reference) return null
60
+ const { pathname, search, hash } = parsePath(reference.trim())
61
+ if (pathname || search || hash.length < 2) return null
62
+ return decode(hash.slice(1))
63
+ }
64
+
65
+ /** What each `url()` in a CSS value points at, quoted or not, such as an image's data URL. */
66
+ export function parseCSSURLs(value: string | undefined): string[] {
67
+ const urls: string[] = []
68
+ if (!value) return urls
69
+ valueParser(value).walk((node) => {
70
+ if (node.type !== 'function' || node.value.toLowerCase() !== 'url') return true
71
+ const target = node.nodes.at(0)?.value
72
+ if (target) urls.push(target)
73
+ return false
74
+ })
75
+ return urls
76
+ }
77
+
78
+ /**
79
+ * What a paint written as `url(#id) fallback` falls back to when its reference cannot be used:
80
+ * the value without its `url()`, or undefined when nothing follows it, which paints nothing.
81
+ */
82
+ export function parsePaintFallback(value: string): string | undefined {
83
+ const rest = valueParser
84
+ .stringify(
85
+ valueParser(value).nodes.filter(
86
+ (node) => node.type !== 'function' || node.value.toLowerCase() !== 'url'
87
+ )
88
+ )
89
+ .trim()
90
+ return rest || undefined
91
+ }
92
+
93
+ /**
94
+ * The ids a CSS value points at with local `url(#id)` references, as `clip-path`, `mask`,
95
+ * `filter`, and SVG paints do, quoted or not.
96
+ */
97
+ export function parseCSSURLReferences(value: string | undefined): string[] {
98
+ return compact(parseCSSURLs(value).map(parseFragmentReference))
99
+ }
package/src/font-style.ts CHANGED
@@ -47,17 +47,51 @@ function normalizeFontStyleName(style: string): string {
47
47
  .replace(/[^a-z0-9]+/gu, '')
48
48
  }
49
49
 
50
+ const FONT_WIDTH_NAMES = [
51
+ 'ultracondensed',
52
+ 'ultracompressed',
53
+ 'extracompressed',
54
+ 'extracondensed',
55
+ 'compressed',
56
+ 'condensed',
57
+ 'narrow',
58
+ 'semicompressed',
59
+ 'semicondensed',
60
+ 'demicondensed',
61
+ 'semiexpanded',
62
+ 'semiextended',
63
+ 'expanded',
64
+ 'extended',
65
+ 'wide',
66
+ 'extraexpanded',
67
+ 'extraextended',
68
+ 'ultraexpanded',
69
+ 'ultraextended'
70
+ ]
71
+
72
+ const FONT_WIDTH_NAME = new RegExp(
73
+ FONT_WIDTH_NAMES.toSorted((a, b) => b.length - a.length).join('|'),
74
+ 'u'
75
+ )
76
+
77
+ function weightOfStyleName(name: string): number {
78
+ const numericWeight = name.match(/(?:^|[^0-9])([1-9]00)(?:[^0-9]|$)/u)?.[1]
79
+ return numericWeight ? Number(numericWeight) : (FONT_WEIGHT_BY_STYLE.get(name) ?? 400)
80
+ }
81
+
50
82
  export function parseFontStyle(style: string | undefined): ParsedFontStyle {
51
83
  const raw = style ?? ''
52
- const italic = /(?:italic|oblique)/iu.test(raw)
53
84
  const normalized = normalizeFontStyleName(raw)
54
- const numericWeight = normalized.match(/(?:^|[^0-9])([1-9]00)(?:[^0-9]|$)/u)?.[1]
55
85
  return {
56
- weight: numericWeight ? Number(numericWeight) : (FONT_WEIGHT_BY_STYLE.get(normalized) ?? 400),
57
- italic
86
+ weight: weightOfStyleName(normalized.replace(FONT_WIDTH_NAME, '')),
87
+ italic: /(?:italic|oblique)/iu.test(raw)
58
88
  }
59
89
  }
60
90
 
91
+ export function namesWidth(style: string): boolean {
92
+ return FONT_WIDTH_NAME.test(normalizeFontStyleName(style))
93
+ }
94
+
61
95
  export function styleToWeight(style: string | undefined): number {
62
96
  return parseFontStyle(style).weight
63
97
  }
package/src/geometry.ts CHANGED
@@ -1,6 +1,15 @@
1
1
  import { arrowCapOverflow } from './arrow-caps'
2
+ import { joinedSegmentPairs, leavingDirection } from './paths/network'
2
3
  import type { Rect, Vector } from './primitives'
3
- import type { Effect, SceneNode, Stroke, StrokeCap, StrokeJoin, VectorNetwork } from './types'
4
+ import type {
5
+ ArcData,
6
+ Effect,
7
+ SceneNode,
8
+ Stroke,
9
+ StrokeCap,
10
+ StrokeJoin,
11
+ VectorNetwork
12
+ } from './types'
4
13
 
5
14
  export { cubicPoint } from './cubic'
6
15
 
@@ -12,6 +21,47 @@ export function radToDeg(radians: number): number {
12
21
  return (radians * 180) / Math.PI
13
22
  }
14
23
 
24
+ const FULL_TURN = 2 * Math.PI
25
+
26
+ /** Layers whose shadows Figma never spreads: a text, boolean, star, polygon or vector. */
27
+ const UNSPREAD_TYPES: ReadonlySet<SceneNode['type']> = new Set<SceneNode['type']>([
28
+ 'TEXT',
29
+ 'BOOLEAN_OPERATION',
30
+ 'STAR',
31
+ 'POLYGON',
32
+ 'VECTOR'
33
+ ])
34
+
35
+ /**
36
+ * Whether a layer's shadows take their spread. Figma spreads those of boxes and ellipses, arcs
37
+ * among them, and frames, and counts it in their render bounds; a text, boolean, star, polygon or
38
+ * vector never spreads (Figma desktop, live).
39
+ */
40
+ export function shadowSpreads(node: Pick<SceneNode, 'type'>): boolean {
41
+ return !UNSPREAD_TYPES.has(node.type)
42
+ }
43
+
44
+ /** The spread a shadow on `node` takes: its own, or none on a layer Figma never spreads. */
45
+ export function shadowSpreadOf(
46
+ node: Pick<SceneNode, 'type'>,
47
+ effect: Pick<Effect, 'spread'>
48
+ ): number {
49
+ return shadowSpreads(node) ? effect.spread : 0
50
+ }
51
+
52
+ /**
53
+ * Whether a sweep from `start` to `end` is a whole turn: less than 0.001 rad short of one draws the
54
+ * whole ellipse in Figma (desktop, live: 0.00095 short is whole, 0.001 short an arc).
55
+ */
56
+ export function sweepsWholeTurn(start: number, end: number): boolean {
57
+ return FULL_TURN - Math.abs(end - start) < 0.001
58
+ }
59
+
60
+ /** Whether an arc sweeps a whole turn, so it is drawn and edited as a whole ellipse or ring. */
61
+ export function isWholeArc(arc: Pick<ArcData, 'startingAngle' | 'endingAngle'>): boolean {
62
+ return sweepsWholeTurn(arc.startingAngle, arc.endingAngle)
63
+ }
64
+
15
65
  export function rotatePoint(px: number, py: number, cx: number, cy: number, rad: number): Vector {
16
66
  const cos = Math.cos(rad)
17
67
  const sin = Math.sin(rad)
@@ -143,16 +193,122 @@ export function polygonVertices(node: {
143
193
  })
144
194
  }
145
195
 
196
+ /** A rectangle's stroke weights, side by side: top, right, bottom, left. */
197
+ export type SideWeights = readonly [top: number, right: number, bottom: number, left: number]
198
+
199
+ type SideStrokeNode = Pick<
200
+ SceneNode,
201
+ | 'independentStrokeWeights'
202
+ | 'borderTopWeight'
203
+ | 'borderRightWeight'
204
+ | 'borderBottomWeight'
205
+ | 'borderLeftWeight'
206
+ >
207
+
208
+ /** What deciding on per-side strokes reads: the side weights, and the node's type when known. */
209
+ type StrokeSidesInput = Partial<SideStrokeNode> & { type?: string }
210
+
211
+ /** The layers the canvas strokes side by side when their sides have weights of their own. */
212
+ const SIDE_STROKE_TYPES: ReadonlySet<string> = new Set<SceneNode['type']>([
213
+ 'FRAME',
214
+ 'RECTANGLE',
215
+ 'ROUNDED_RECTANGLE',
216
+ 'COMPONENT',
217
+ 'INSTANCE',
218
+ 'SECTION',
219
+ 'GROUP'
220
+ ])
221
+
222
+ /**
223
+ * Whether `node` strokes each side by its own weight. A line or vector keeps one weight whatever
224
+ * the flag says, since its outline has no sides.
225
+ */
226
+ export function drawsStrokeSides(node: StrokeSidesInput): boolean {
227
+ return node.independentStrokeWeights === true && (!node.type || SIDE_STROKE_TYPES.has(node.type))
228
+ }
229
+
230
+ /** The share of a stroke's weight that lies outside the shape it outlines. */
231
+ export function strokeAlignShare(align: Stroke['align']): number {
232
+ if (align === 'OUTSIDE') return 1
233
+ if (align === 'CENTER') return 0.5
234
+ return 0
235
+ }
236
+
237
+ /** A layer's own side weights: top, right, bottom, left. */
238
+ export function borderWeights(node: SideStrokeNode): SideWeights {
239
+ return [
240
+ node.borderTopWeight,
241
+ node.borderRightWeight,
242
+ node.borderBottomWeight,
243
+ node.borderLeftWeight
244
+ ]
245
+ }
246
+
247
+ /** The weight of each side of `node` under `stroke`, its own when its sides have their own. */
248
+ export function strokeSideWeights(
249
+ node: SideStrokeNode & { type?: string },
250
+ stroke: Pick<Stroke, 'weight'>
251
+ ): SideWeights {
252
+ if (!drawsStrokeSides(node)) return [stroke.weight, stroke.weight, stroke.weight, stroke.weight]
253
+ return borderWeights(node)
254
+ }
255
+
256
+ /** A stroke's weight, or with sides of their own the widest side, for what is not drawn per side. */
257
+ export function strokeWeightOf(node: StrokeSidesInput, stroke: Pick<Stroke, 'weight'>): number {
258
+ if (!drawsStrokeSides(node)) return stroke.weight
259
+ return Math.max(
260
+ node.borderTopWeight ?? 0,
261
+ node.borderRightWeight ?? 0,
262
+ node.borderBottomWeight ?? 0,
263
+ node.borderLeftWeight ?? 0
264
+ )
265
+ }
266
+
267
+ /**
268
+ * How far a layer's visible strokes reach past each side of its box: top, right, bottom, left.
269
+ * With sides of their own each side reaches by its own weight (Figma desktop, live: a 1px outside
270
+ * stroke with a 20px right side bounds a 100 × 100 rectangle at 121 × 102).
271
+ */
272
+ export function strokeSideReach(
273
+ node: SideStrokeNode & Pick<SceneNode, 'strokes'> & { type?: string }
274
+ ): SideWeights {
275
+ let reach: SideWeights = [0, 0, 0, 0]
276
+ for (const stroke of node.strokes) {
277
+ if (!stroke.visible) continue
278
+ const share = strokeAlignShare(stroke.align)
279
+ const sides = strokeSideWeights(node, stroke)
280
+ reach = [
281
+ Math.max(reach[0], sides[0] * share),
282
+ Math.max(reach[1], sides[1] * share),
283
+ Math.max(reach[2], sides[2] * share),
284
+ Math.max(reach[3], sides[3] * share)
285
+ ]
286
+ }
287
+ return reach
288
+ }
289
+
146
290
  /**
147
291
  * What shapes how far a layer's stroke reaches past its box: its type, since a line is stroked to
148
292
  * one side, and its join and miter limit, for the tips mitered path corners reach.
149
293
  */
150
- export interface StrokeShape {
294
+ export interface StrokeShape extends Partial<SideStrokeNode> {
151
295
  type?: string
152
296
  strokeJoin?: StrokeJoin
153
297
  strokeMiterLimit?: number
154
298
  }
155
299
 
300
+ /** How far past a path a stroke of `weight` reaches, before mitered corners: a line's stroke lies wholly to one side. */
301
+ function strokeExtent(
302
+ weight: number,
303
+ align: Stroke['align'],
304
+ line: boolean,
305
+ open: boolean
306
+ ): number {
307
+ if (line || align === 'OUTSIDE') return weight
308
+ if (align === 'CENTER' || open) return weight / 2
309
+ return 0
310
+ }
311
+
156
312
  export function strokeOverflow(
157
313
  strokes?: Stroke[],
158
314
  fallbackCap?: StrokeCap,
@@ -168,10 +324,8 @@ export function strokeOverflow(
168
324
  let overflow = 0
169
325
  for (const stroke of strokes ?? []) {
170
326
  if (!stroke.visible) continue
171
- const align = sided ? stroke.align : 'CENTER'
172
- let extra = open ? stroke.weight / 2 : 0
173
- if (line || align === 'OUTSIDE') extra = stroke.weight
174
- else if (align === 'CENTER') extra = stroke.weight / 2
327
+ const weight = strokeWeightOf(shape ?? {}, stroke)
328
+ const extra = strokeExtent(weight, sided ? stroke.align : 'CENTER', line, open)
175
329
  const miter = network
176
330
  ? miterRatio(network, stroke.join ?? shape?.strokeJoin, shape?.strokeMiterLimit)
177
331
  : 1
@@ -182,35 +336,16 @@ export function strokeOverflow(
182
336
  return Math.max(overflow, heads > 0 && line ? heads + overflow / 2 : heads)
183
337
  }
184
338
 
185
- /** The direction a segment leaves `vertex` in, along its handle when it has one. */
186
- function leaving(network: VectorNetwork, index: number, vertex: number): Vector {
187
- const segment = network.segments[index]
188
- const atStart = segment.start === vertex
189
- const handle = atStart ? segment.tangentStart : segment.tangentEnd
190
- if (handle.x !== 0 || handle.y !== 0) return handle
191
- const from = network.vertices[vertex]
192
- const to = network.vertices[atStart ? segment.end : segment.start]
193
- return { x: to.x - from.x, y: to.y - from.y }
194
- }
195
-
196
339
  /**
197
340
  * How far past half the stroke the sharpest mitered corner reaches, as a multiple: a corner of
198
341
  * angle θ mitres out to 1/sin(θ/2), unless that passes the miter limit and it bevels instead.
199
- * Corners are the vertices exactly two segments meet at, as the stroke joins them.
200
342
  */
201
343
  function miterRatio(network: VectorNetwork, join: StrokeJoin | undefined, limit = 4): number {
202
- const meeting = new Map<number, number[]>()
203
- for (const [index, segment] of network.segments.entries()) {
204
- for (const vertex of new Set([segment.start, segment.end])) {
205
- meeting.set(vertex, [...(meeting.get(vertex) ?? []), index])
206
- }
207
- }
208
344
  let ratio = 1
209
- for (const [vertex, indices] of meeting) {
210
- if (indices.length !== 2) continue
345
+ for (const [vertex, first, second] of joinedSegmentPairs(network)) {
211
346
  if ((network.vertices[vertex].strokeJoin ?? join ?? 'MITER') !== 'MITER') continue
212
- const a = leaving(network, indices[0], vertex)
213
- const b = leaving(network, indices[1], vertex)
347
+ const a = leavingDirection(network, first, vertex)
348
+ const b = leavingDirection(network, second, vertex)
214
349
  const lengths = Math.hypot(a.x, a.y) * Math.hypot(b.x, b.y)
215
350
  if (lengths === 0) continue
216
351
  const angle = Math.acos(Math.max(-1, Math.min(1, (a.x * b.x + a.y * b.y) / lengths)))
@@ -247,11 +382,71 @@ export function blurSigma(radius: number): number {
247
382
  return drawnBlurRadius(radius) * BLUR_SIGMA_PER_RADIUS
248
383
  }
249
384
 
385
+ /** Skia's Gaussian kernels end at 3 sigma. */
386
+ const KERNEL_SIGMAS = 3
387
+
388
+ /**
389
+ * How far past what it blurs a Gaussian of `sigma` leaves ink on screen: where Skia's kernel
390
+ * ends, and one more pixel holds its rounding.
391
+ */
392
+ export function kernelReach(sigma: number): number {
393
+ return sigma > 0 ? Math.ceil(KERNEL_SIGMAS * sigma) + 1 : 0
394
+ }
395
+
396
+ /** How far past what it blurs a Figma blur of `radius` leaves ink on screen (`kernelReach`). */
397
+ export function blurReach(radius: number): number {
398
+ return kernelReach(blurSigma(radius))
399
+ }
400
+
401
+ /**
402
+ * The distance, in sigmas, past a straight edge blurred by a Gaussian at which the edge's share
403
+ * drops to `share`: the inverse of the normal tail Q, by Abramowitz and Stegun 26.2.23, which is
404
+ * within 0.00045 of it.
405
+ */
406
+ function gaussianTailDistance(share: number): number {
407
+ if (share <= 0) {
408
+ return Number.POSITIVE_INFINITY
409
+ }
410
+ if (share >= 0.5) {
411
+ return 0
412
+ }
413
+ const s = Math.sqrt(-2 * Math.log(share))
414
+ return (
415
+ s -
416
+ (2.515517 + 0.802853 * s + 0.010328 * s * s) /
417
+ (1 + 1.432788 * s + 0.189269 * s * s + 0.001308 * s * s * s)
418
+ )
419
+ }
420
+
421
+ const COLOR_LEVELS = 255
422
+
423
+ /**
424
+ * How far past its shape a drop shadow of `radius` and colour alpha `alpha` leaves ink on
425
+ * screen. Skia blurs the shape into an 8-bit mask and scales it by `alpha`, so a mask level shows
426
+ * once `alpha` times it rounds to a level: an opaque or half-opaque shadow shows its faintest
427
+ * level, out to where the kernel ends, and a fainter one ends sooner. Measured on CPU raster, the
428
+ * ink ends at 3.0 sigma for alpha 1 and 0.5, 2.4 for 0.25, and 2.1 for 0.1.
429
+ */
430
+ export function shadowReach(radius: number, alpha: number): number {
431
+ const sigma = blurSigma(radius)
432
+ const faintestShownLevel = Math.ceil(0.5 / Math.max(alpha, 1 / COLOR_LEVELS))
433
+ const sigmas = Math.min(
434
+ KERNEL_SIGMAS,
435
+ gaussianTailDistance((faintestShownLevel - 1) / COLOR_LEVELS)
436
+ )
437
+ return Math.ceil(sigmas * sigma) + 1
438
+ }
439
+
250
440
  /** The Figma blur radius that draws a Gaussian of `sigma`, the inverse of `blurSigma`. */
251
441
  export function blurRadiusForSigma(sigma: number): number {
252
442
  return Math.max(0, sigma) / BLUR_SIGMA_PER_RADIUS
253
443
  }
254
444
 
445
+ /**
446
+ * How far a layer's shadows and blurs reach past each side as Figma's render bounds count them:
447
+ * a blur by its radius, a shadow by its radius and spread, moved by its offset. Ink on screen
448
+ * reaches further, to `blurReach`.
449
+ */
255
450
  export function effectOverflow(effects?: Effect[]) {
256
451
  let left = 0
257
452
  let right = 0
package/src/gradient.ts CHANGED
@@ -1,4 +1,5 @@
1
- import type { Color, Vector } from './primitives'
1
+ import Matrix from './matrix'
2
+ import type { Color, Rect, Vector } from './primitives'
2
3
  import type { Fill, GradientStop, GradientTransform } from './types'
3
4
 
4
5
  /**
@@ -178,3 +179,24 @@ export function ellipticalGradientTransform(
178
179
  m12: center.y - (xAxis.y + yAxis.y) / 2
179
180
  })
180
181
  }
182
+
183
+ /**
184
+ * The transform that keeps a gradient where it was on the canvas when its layer's box becomes
185
+ * `box`, given in the old box's units, as Figma rewrites it when editing a path resizes the layer
186
+ * (Figma desktop 126: a 200 × 120 box grown to 380 × 160 and moved up 40 gives
187
+ * `[[1.9, 0, 0], [0, 1.333, -0.333]]` from the identity).
188
+ */
189
+ export function gradientTransformInBox(t: GradientTransform, box: Rect): GradientTransform {
190
+ const [m00, m01, m02, m10, m11, m12] = Matrix.multiply(Matrix.fromAffine(t), [
191
+ box.width,
192
+ 0,
193
+ box.x,
194
+ 0,
195
+ box.height,
196
+ box.y,
197
+ 0,
198
+ 0,
199
+ 1
200
+ ])
201
+ return { m00, m01, m02, m10, m11, m12 }
202
+ }