@open-pencil/scene-graph 0.17.0-beta.1 → 0.17.0-beta.3

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 (390) hide show
  1. package/dist/arrow-caps.d.ts.map +1 -1
  2. package/dist/arrow-caps.js +2 -5
  3. package/dist/arrow-caps.js.map +1 -1
  4. package/dist/asset-keys.d.ts +11 -1
  5. package/dist/asset-keys.d.ts.map +1 -1
  6. package/dist/asset-keys.js +16 -1
  7. package/dist/asset-keys.js.map +1 -1
  8. package/dist/behaviours/index.d.ts +2 -2
  9. package/dist/behaviours/index.js +2 -2
  10. package/dist/behaviours/kinds.d.ts +11 -5
  11. package/dist/behaviours/kinds.d.ts.map +1 -1
  12. package/dist/behaviours/kinds.js +76 -75
  13. package/dist/behaviours/kinds.js.map +1 -1
  14. package/dist/behaviours/layers.d.ts.map +1 -1
  15. package/dist/behaviours/layers.js +3 -2
  16. package/dist/behaviours/layers.js.map +1 -1
  17. package/dist/behaviours/model.d.ts +7 -4
  18. package/dist/behaviours/model.d.ts.map +1 -1
  19. package/dist/behaviours/model.js +16 -9
  20. package/dist/behaviours/model.js.map +1 -1
  21. package/dist/behaviours/schema.d.ts +118 -4
  22. package/dist/behaviours/schema.d.ts.map +1 -1
  23. package/dist/behaviours/schema.js +18 -3
  24. package/dist/behaviours/schema.js.map +1 -1
  25. package/dist/behaviours/spec.d.ts.map +1 -1
  26. package/dist/behaviours/spec.js +3 -2
  27. package/dist/behaviours/spec.js.map +1 -1
  28. package/dist/bindings.js +9 -1
  29. package/dist/bindings.js.map +1 -1
  30. package/dist/checkpoint.d.ts +31 -2
  31. package/dist/checkpoint.d.ts.map +1 -1
  32. package/dist/checkpoint.js +211 -10
  33. package/dist/checkpoint.js.map +1 -1
  34. package/dist/components/index.d.ts +3 -3
  35. package/dist/components/index.js +3 -3
  36. package/dist/components/properties.d.ts +18 -1
  37. package/dist/components/properties.d.ts.map +1 -1
  38. package/dist/components/properties.js +66 -3
  39. package/dist/components/properties.js.map +1 -1
  40. package/dist/components/variants.d.ts +16 -10
  41. package/dist/components/variants.d.ts.map +1 -1
  42. package/dist/components/variants.js +39 -13
  43. package/dist/components/variants.js.map +1 -1
  44. package/dist/connectors/hit.d.ts.map +1 -1
  45. package/dist/connectors/hit.js +1 -7
  46. package/dist/connectors/hit.js.map +1 -1
  47. package/dist/connectors/pieces.d.ts.map +1 -1
  48. package/dist/connectors/pieces.js +2 -5
  49. package/dist/connectors/pieces.js.map +1 -1
  50. package/dist/constants.d.ts +6 -1
  51. package/dist/constants.d.ts.map +1 -1
  52. package/dist/constants.js +17 -1
  53. package/dist/constants.js.map +1 -1
  54. package/dist/constraints.d.ts +25 -0
  55. package/dist/constraints.d.ts.map +1 -0
  56. package/dist/constraints.js +51 -0
  57. package/dist/constraints.js.map +1 -0
  58. package/dist/coordinate.d.ts +64 -1
  59. package/dist/coordinate.d.ts.map +1 -1
  60. package/dist/coordinate.js +217 -1
  61. package/dist/coordinate.js.map +1 -1
  62. package/dist/copy.d.ts.map +1 -1
  63. package/dist/copy.js +17 -16
  64. package/dist/copy.js.map +1 -1
  65. package/dist/css/blend.d.ts +11 -0
  66. package/dist/css/blend.d.ts.map +1 -0
  67. package/dist/css/blend.js +30 -0
  68. package/dist/css/blend.js.map +1 -0
  69. package/dist/css/index.d.ts +4 -2
  70. package/dist/css/index.js +4 -2
  71. package/dist/css/text.d.ts +28 -1
  72. package/dist/css/text.d.ts.map +1 -1
  73. package/dist/css/text.js +84 -17
  74. package/dist/css/text.js.map +1 -1
  75. package/dist/css/transform.d.ts +34 -0
  76. package/dist/css/transform.d.ts.map +1 -0
  77. package/dist/css/transform.js +320 -0
  78. package/dist/css/transform.js.map +1 -0
  79. package/dist/css/values.d.ts.map +1 -1
  80. package/dist/css/values.js +11 -1
  81. package/dist/css/values.js.map +1 -1
  82. package/dist/cubic.d.ts +7 -0
  83. package/dist/cubic.d.ts.map +1 -0
  84. package/dist/cubic.js +17 -0
  85. package/dist/cubic.js.map +1 -0
  86. package/dist/export-format.d.ts +14 -1
  87. package/dist/export-format.d.ts.map +1 -1
  88. package/dist/export-format.js +26 -1
  89. package/dist/export-format.js.map +1 -1
  90. package/dist/fields/text.d.ts +2 -2
  91. package/dist/fields/text.d.ts.map +1 -1
  92. package/dist/fields/text.js +2 -1
  93. package/dist/fields/text.js.map +1 -1
  94. package/dist/figjam/index.d.ts +2 -2
  95. package/dist/figjam/index.js +2 -2
  96. package/dist/figjam/palette.d.ts +13 -2
  97. package/dist/figjam/palette.d.ts.map +1 -1
  98. package/dist/figjam/palette.js +30 -2
  99. package/dist/figjam/palette.js.map +1 -1
  100. package/dist/generated/index.d.ts +2 -2
  101. package/dist/generated/index.js +2 -2
  102. package/dist/generated/shape-with-text.d.ts +7 -2
  103. package/dist/generated/shape-with-text.d.ts.map +1 -1
  104. package/dist/generated/shape-with-text.js +20 -3
  105. package/dist/generated/shape-with-text.js.map +1 -1
  106. package/dist/geometry.d.ts +37 -46
  107. package/dist/geometry.d.ts.map +1 -1
  108. package/dist/geometry.js +80 -137
  109. package/dist/geometry.js.map +1 -1
  110. package/dist/graph.d.ts +49 -12
  111. package/dist/graph.d.ts.map +1 -1
  112. package/dist/graph.js +125 -27
  113. package/dist/graph.js.map +1 -1
  114. package/dist/group-bounds.d.ts +14 -4
  115. package/dist/group-bounds.d.ts.map +1 -1
  116. package/dist/group-bounds.js +76 -11
  117. package/dist/group-bounds.js.map +1 -1
  118. package/dist/guides.d.ts +3 -1
  119. package/dist/guides.d.ts.map +1 -1
  120. package/dist/guides.js +5 -1
  121. package/dist/guides.js.map +1 -1
  122. package/dist/hit-test.d.ts +5 -0
  123. package/dist/hit-test.d.ts.map +1 -1
  124. package/dist/hit-test.js +57 -38
  125. package/dist/hit-test.js.map +1 -1
  126. package/dist/images.d.ts +27 -3
  127. package/dist/images.d.ts.map +1 -1
  128. package/dist/images.js +57 -3
  129. package/dist/images.js.map +1 -1
  130. package/dist/index.d.ts +2 -2
  131. package/dist/instances/fields.d.ts +2 -2
  132. package/dist/instances/fields.d.ts.map +1 -1
  133. package/dist/instances/fields.js +8 -0
  134. package/dist/instances/fields.js.map +1 -1
  135. package/dist/instances/index.d.ts +1 -3
  136. package/dist/instances/index.d.ts.map +1 -1
  137. package/dist/instances/index.js +39 -30
  138. package/dist/instances/index.js.map +1 -1
  139. package/dist/instances/kept-overrides.js +204 -0
  140. package/dist/instances/kept-overrides.js.map +1 -0
  141. package/dist/instances/main-component.d.ts +3 -1
  142. package/dist/instances/main-component.d.ts.map +1 -1
  143. package/dist/instances/main-component.js +10 -2
  144. package/dist/instances/main-component.js.map +1 -1
  145. package/dist/instances/overrides.d.ts +4 -1
  146. package/dist/instances/overrides.d.ts.map +1 -1
  147. package/dist/instances/overrides.js +18 -2
  148. package/dist/instances/overrides.js.map +1 -1
  149. package/dist/instances/placement.js +55 -12
  150. package/dist/instances/placement.js.map +1 -1
  151. package/dist/instances/swap.js +291 -0
  152. package/dist/instances/swap.js.map +1 -0
  153. package/dist/instances/sync.js +112 -39
  154. package/dist/instances/sync.js.map +1 -1
  155. package/dist/node-defaults.d.ts.map +1 -1
  156. package/dist/node-defaults.js +2 -1
  157. package/dist/node-defaults.js.map +1 -1
  158. package/dist/order-keys.d.ts +3 -1
  159. package/dist/order-keys.d.ts.map +1 -1
  160. package/dist/order-keys.js +6 -1
  161. package/dist/order-keys.js.map +1 -1
  162. package/dist/outline/flatten.d.ts +15 -0
  163. package/dist/outline/flatten.d.ts.map +1 -0
  164. package/dist/outline/flatten.js +189 -0
  165. package/dist/outline/flatten.js.map +1 -0
  166. package/dist/outline/heads.js +34 -0
  167. package/dist/outline/heads.js.map +1 -0
  168. package/dist/outline/hit.d.ts +21 -0
  169. package/dist/outline/hit.d.ts.map +1 -0
  170. package/dist/outline/hit.js +276 -0
  171. package/dist/outline/hit.js.map +1 -0
  172. package/dist/outline/index.d.ts +5 -0
  173. package/dist/outline/index.js +5 -0
  174. package/dist/outline/point.d.ts +16 -0
  175. package/dist/outline/point.d.ts.map +1 -0
  176. package/dist/outline/point.js +42 -0
  177. package/dist/outline/point.js.map +1 -0
  178. package/dist/outline/shapes.d.ts +35 -0
  179. package/dist/outline/shapes.d.ts.map +1 -0
  180. package/dist/outline/shapes.js +181 -0
  181. package/dist/outline/shapes.js.map +1 -0
  182. package/dist/plugin-data/fields.d.ts +4 -1
  183. package/dist/plugin-data/fields.d.ts.map +1 -1
  184. package/dist/plugin-data/fields.js +21 -4
  185. package/dist/plugin-data/fields.js.map +1 -1
  186. package/dist/resize.d.ts +22 -11
  187. package/dist/resize.d.ts.map +1 -1
  188. package/dist/resize.js +227 -93
  189. package/dist/resize.js.map +1 -1
  190. package/dist/scaling/index.d.ts +3 -2
  191. package/dist/scaling/index.js +3 -2
  192. package/dist/scaling/node.d.ts.map +1 -1
  193. package/dist/scaling/node.js +5 -1
  194. package/dist/scaling/node.js.map +1 -1
  195. package/dist/scaling/snapshot.d.ts +25 -0
  196. package/dist/scaling/snapshot.d.ts.map +1 -0
  197. package/dist/scaling/snapshot.js +39 -0
  198. package/dist/scaling/snapshot.js.map +1 -0
  199. package/dist/scaling/tree.d.ts +24 -2
  200. package/dist/scaling/tree.d.ts.map +1 -1
  201. package/dist/scaling/tree.js +38 -15
  202. package/dist/scaling/tree.js.map +1 -1
  203. package/dist/shaders/model.d.ts.map +1 -1
  204. package/dist/shaders/model.js +2 -1
  205. package/dist/shaders/model.js.map +1 -1
  206. package/dist/shared-styles.d.ts +16 -1
  207. package/dist/shared-styles.d.ts.map +1 -1
  208. package/dist/shared-styles.js +69 -2
  209. package/dist/shared-styles.js.map +1 -1
  210. package/dist/slots/authoring.d.ts +3 -2
  211. package/dist/slots/authoring.d.ts.map +1 -1
  212. package/dist/slots/authoring.js +5 -3
  213. package/dist/slots/authoring.js.map +1 -1
  214. package/dist/slots/content.d.ts.map +1 -1
  215. package/dist/slots/content.js +6 -1
  216. package/dist/slots/content.js.map +1 -1
  217. package/dist/slots/frames.d.ts +2 -0
  218. package/dist/slots/frames.d.ts.map +1 -1
  219. package/dist/slots/frames.js +7 -2
  220. package/dist/slots/frames.js.map +1 -1
  221. package/dist/text/direction.d.ts +3 -1
  222. package/dist/text/direction.d.ts.map +1 -1
  223. package/dist/text/direction.js +5 -1
  224. package/dist/text/direction.js.map +1 -1
  225. package/dist/text/index.d.ts +2 -2
  226. package/dist/text/index.js +3 -3
  227. package/dist/text/lengths.d.ts +6 -1
  228. package/dist/text/lengths.d.ts.map +1 -1
  229. package/dist/text/lengths.js +10 -1
  230. package/dist/text/lengths.js.map +1 -1
  231. package/dist/text/overflow.d.ts +2 -2
  232. package/dist/text/overflow.js.map +1 -1
  233. package/dist/text/picture.d.ts +1 -2
  234. package/dist/text/picture.d.ts.map +1 -1
  235. package/dist/text/picture.js +1 -4
  236. package/dist/text/picture.js.map +1 -1
  237. package/dist/transfer/apply.d.ts.map +1 -1
  238. package/dist/transfer/apply.js +1 -6
  239. package/dist/transfer/apply.js.map +1 -1
  240. package/dist/transfer/copied.d.ts +35 -3
  241. package/dist/transfer/copied.d.ts.map +1 -1
  242. package/dist/transfer/copied.js +44 -13
  243. package/dist/transfer/copied.js.map +1 -1
  244. package/dist/transfer/dependencies.d.ts +38 -0
  245. package/dist/transfer/dependencies.d.ts.map +1 -0
  246. package/dist/transfer/dependencies.js +100 -0
  247. package/dist/transfer/dependencies.js.map +1 -0
  248. package/dist/transfer/history.js +0 -1
  249. package/dist/transfer/history.js.map +1 -1
  250. package/dist/transfer/index.d.ts +5 -3
  251. package/dist/transfer/index.js +5 -3
  252. package/dist/transfer/nodes.d.ts.map +1 -1
  253. package/dist/transfer/nodes.js +16 -19
  254. package/dist/transfer/nodes.js.map +1 -1
  255. package/dist/transfer/plan.d.ts +0 -1
  256. package/dist/transfer/plan.js +3 -5
  257. package/dist/transfer/plan.js.map +1 -1
  258. package/dist/transfer/remove.js +5 -2
  259. package/dist/transfer/remove.js.map +1 -1
  260. package/dist/transfer/resources.d.ts +21 -0
  261. package/dist/transfer/resources.d.ts.map +1 -0
  262. package/dist/transfer/resources.js +38 -0
  263. package/dist/transfer/resources.js.map +1 -0
  264. package/dist/transfer/variables.d.ts +8 -1
  265. package/dist/transfer/variables.d.ts.map +1 -1
  266. package/dist/transfer/variables.js +24 -1
  267. package/dist/transfer/variables.js.map +1 -1
  268. package/dist/types.d.ts +73 -2
  269. package/dist/types.d.ts.map +1 -1
  270. package/dist/types.js.map +1 -1
  271. package/dist/undo.d.ts +28 -3
  272. package/dist/undo.d.ts.map +1 -1
  273. package/dist/undo.js +24 -7
  274. package/dist/undo.js.map +1 -1
  275. package/dist/variables/bindings.d.ts +37 -11
  276. package/dist/variables/bindings.d.ts.map +1 -1
  277. package/dist/variables/bindings.js +122 -74
  278. package/dist/variables/bindings.js.map +1 -1
  279. package/dist/variables/fields.d.ts +19 -1
  280. package/dist/variables/fields.d.ts.map +1 -1
  281. package/dist/variables/fields.js +36 -1
  282. package/dist/variables/fields.js.map +1 -1
  283. package/dist/variables/formula/check.d.ts +1 -1
  284. package/dist/variables/formula/index.d.ts +2 -2
  285. package/dist/variables/formula/index.js +1 -1
  286. package/dist/variables/formula/index.js.map +1 -1
  287. package/dist/variables/formula/print.d.ts +1 -1
  288. package/dist/variables/formula/signatures.js +1 -1
  289. package/dist/variables/index.d.ts +7 -7
  290. package/dist/variables/index.d.ts.map +1 -1
  291. package/dist/variables/index.js +57 -26
  292. package/dist/variables/index.js.map +1 -1
  293. package/dist/variables/paints.d.ts +57 -0
  294. package/dist/variables/paints.d.ts.map +1 -0
  295. package/dist/variables/paints.js +231 -0
  296. package/dist/variables/paints.js.map +1 -0
  297. package/dist/variables/resolution.d.ts +14 -11
  298. package/dist/variables/resolution.d.ts.map +1 -1
  299. package/dist/variables/resolution.js +36 -17
  300. package/dist/variables/resolution.js.map +1 -1
  301. package/dist/variables/subtree.js +17 -0
  302. package/dist/variables/subtree.js.map +1 -0
  303. package/dist/vector-network.d.ts.map +1 -1
  304. package/dist/vector-network.js +2 -8
  305. package/dist/vector-network.js.map +1 -1
  306. package/package.json +14 -1
  307. package/src/arrow-caps.ts +2 -5
  308. package/src/asset-keys.ts +20 -0
  309. package/src/behaviours/kinds.ts +51 -34
  310. package/src/behaviours/layers.ts +4 -2
  311. package/src/behaviours/model.ts +22 -11
  312. package/src/behaviours/schema.ts +20 -4
  313. package/src/behaviours/spec.ts +8 -2
  314. package/src/bindings.ts +11 -2
  315. package/src/checkpoint.ts +319 -17
  316. package/src/components/properties.ts +101 -2
  317. package/src/components/variants.ts +51 -30
  318. package/src/connectors/hit.ts +1 -12
  319. package/src/connectors/pieces.ts +2 -5
  320. package/src/constants.ts +17 -0
  321. package/src/constraints.ts +60 -0
  322. package/src/coordinate.ts +220 -0
  323. package/src/copy.ts +18 -19
  324. package/src/css/blend.ts +27 -0
  325. package/src/css/index.ts +18 -1
  326. package/src/css/text.ts +85 -17
  327. package/src/css/transform.ts +266 -0
  328. package/src/css/values.ts +8 -0
  329. package/src/cubic.ts +14 -0
  330. package/src/export-format.ts +29 -0
  331. package/src/fields/text.ts +3 -1
  332. package/src/figjam/palette.ts +29 -1
  333. package/src/generated/shape-with-text.ts +28 -2
  334. package/src/geometry.ts +97 -206
  335. package/src/graph.ts +155 -45
  336. package/src/group-bounds.ts +84 -17
  337. package/src/guides.ts +5 -0
  338. package/src/hit-test.ts +93 -64
  339. package/src/images.ts +71 -3
  340. package/src/instances/fields.ts +8 -0
  341. package/src/instances/index.ts +62 -31
  342. package/src/instances/kept-overrides.ts +280 -0
  343. package/src/instances/main-component.ts +12 -0
  344. package/src/instances/overrides.ts +27 -1
  345. package/src/instances/placement.ts +63 -10
  346. package/src/instances/swap.ts +456 -0
  347. package/src/instances/sync.ts +176 -50
  348. package/src/node-defaults.ts +3 -1
  349. package/src/order-keys.ts +6 -0
  350. package/src/outline/flatten.ts +190 -0
  351. package/src/outline/heads.ts +52 -0
  352. package/src/outline/hit.ts +369 -0
  353. package/src/outline/index.ts +4 -0
  354. package/src/outline/point.ts +59 -0
  355. package/src/outline/shapes.ts +207 -0
  356. package/src/plugin-data/fields.ts +21 -2
  357. package/src/resize.ts +267 -119
  358. package/src/scaling/index.ts +9 -1
  359. package/src/scaling/node.ts +9 -1
  360. package/src/scaling/snapshot.ts +51 -0
  361. package/src/scaling/tree.ts +63 -19
  362. package/src/shaders/model.ts +2 -3
  363. package/src/shared-styles.ts +90 -1
  364. package/src/slots/authoring.ts +5 -3
  365. package/src/slots/content.ts +6 -1
  366. package/src/slots/frames.ts +9 -7
  367. package/src/text/direction.ts +5 -0
  368. package/src/text/lengths.ts +10 -0
  369. package/src/text/overflow.ts +2 -2
  370. package/src/text/picture.ts +0 -4
  371. package/src/transfer/apply.ts +1 -13
  372. package/src/transfer/copied.ts +83 -15
  373. package/src/transfer/dependencies.ts +126 -0
  374. package/src/transfer/history.ts +0 -6
  375. package/src/transfer/index.ts +20 -2
  376. package/src/transfer/nodes.ts +17 -31
  377. package/src/transfer/plan.ts +3 -11
  378. package/src/transfer/remove.ts +5 -2
  379. package/src/transfer/resources.ts +54 -0
  380. package/src/transfer/variables.ts +34 -0
  381. package/src/types.ts +83 -1
  382. package/src/undo.ts +48 -10
  383. package/src/variables/bindings.ts +152 -88
  384. package/src/variables/fields.ts +46 -1
  385. package/src/variables/formula/index.ts +3 -1
  386. package/src/variables/index.ts +90 -39
  387. package/src/variables/paints.ts +296 -0
  388. package/src/variables/resolution.ts +39 -28
  389. package/src/variables/subtree.ts +15 -0
  390. package/src/vector-network.ts +4 -2
@@ -0,0 +1,266 @@
1
+ import valueParser from 'postcss-value-parser'
2
+
3
+ import Matrix, { type Mat3 } from '../matrix'
4
+ import type { Vector } from '../primitives'
5
+ import { dimension, splitCSSValue } from './values'
6
+
7
+ /** The size percentages in a transform resolve against: the element's border box. */
8
+ export interface CSSTransformBox {
9
+ width: number
10
+ height: number
11
+ }
12
+
13
+ /** The properties that transform an element, as CSS applies them: translate, rotate, scale, transform. */
14
+ export interface CSSTransformStyle {
15
+ transform?: string
16
+ translate?: string
17
+ rotate?: string
18
+ scale?: string
19
+ }
20
+
21
+ const ANGLE_UNITS: Partial<Record<string, number>> = {
22
+ deg: Math.PI / 180,
23
+ rad: 1,
24
+ grad: Math.PI / 200,
25
+ turn: Math.PI * 2
26
+ }
27
+
28
+ /** An angle in radians; a bare zero counts, as CSS allows. Null for anything else. */
29
+ function angle(value: string): number | null {
30
+ const parsed = dimension(value.trim())
31
+ if (!parsed) return null
32
+ if (parsed.unit === '') return parsed.number === 0 ? 0 : null
33
+ const radians = ANGLE_UNITS[parsed.unit]
34
+ return radians === undefined ? null : parsed.number * radians
35
+ }
36
+
37
+ /** A length in pixels, a percentage of `extent`, or null for units this cannot resolve. */
38
+ function length(value: string, extent: number): number | null {
39
+ const parsed = dimension(value.trim())
40
+ if (!parsed) return null
41
+ if (parsed.unit === '%') return (parsed.number / 100) * extent
42
+ return parsed.unit === 'px' || (parsed.unit === '' && parsed.number === 0) ? parsed.number : null
43
+ }
44
+
45
+ /** A plain number, or a percentage as a fraction, as `scale` takes them. */
46
+ function factor(value: string): number | null {
47
+ const parsed = dimension(value.trim())
48
+ if (!parsed) return null
49
+ if (parsed.unit === '%') return parsed.number / 100
50
+ return parsed.unit === '' ? parsed.number : null
51
+ }
52
+
53
+ function skewed(x: number, y: number): Mat3 {
54
+ return [1, Math.tan(x), 0, Math.tan(y), 1, 0, 0, 0, 1]
55
+ }
56
+
57
+ /** The 2D matrix `matrix(a, b, c, d, e, f)` names. */
58
+ function affine([a, b, c, d, e, f]: number[]): Mat3 {
59
+ return [a, c, e, b, d, f, 0, 0, 1]
60
+ }
61
+
62
+ function translation(x: string, y: string | undefined, box: CSSTransformBox): Mat3 | null {
63
+ const dx = length(x, box.width)
64
+ const dy = y === undefined ? 0 : length(y, box.height)
65
+ return dx === null || dy === null ? null : Matrix.translated(dx, dy)
66
+ }
67
+
68
+ function scaling(x: string, y: string | undefined): Mat3 | null {
69
+ const sx = factor(x)
70
+ const sy = y === undefined ? sx : factor(y)
71
+ return sx === null || sy === null ? null : Matrix.scaled(sx, sy)
72
+ }
73
+
74
+ function numbers(values: string[]): number[] | null {
75
+ const parsed = values.map(Number)
76
+ return parsed.every(Number.isFinite) ? parsed : null
77
+ }
78
+
79
+ function rotation(value: string | undefined): Mat3 | null {
80
+ const radians = value === undefined ? null : angle(value)
81
+ return radians === null ? null : Matrix.rotated(radians)
82
+ }
83
+
84
+ function skewing(x: string | undefined, y: string | undefined): Mat3 | null {
85
+ const ax = x === undefined ? 0 : angle(x)
86
+ const ay = y === undefined ? 0 : angle(y)
87
+ return ax === null || ay === null ? null : skewed(ax, ay)
88
+ }
89
+
90
+ /** A transform function: how many arguments it takes, and the matrix they make. */
91
+ interface TransformFunction {
92
+ arity: readonly [min: number, max: number]
93
+ build: (args: string[], box: CSSTransformBox) => Mat3 | null
94
+ }
95
+
96
+ /** The 2D transform functions by lowercase name; 3D turns and perspective are left out. */
97
+ const TRANSFORM_FUNCTIONS: Partial<Record<string, TransformFunction>> = {
98
+ matrix: {
99
+ arity: [6, 6],
100
+ build: (args) => {
101
+ const m = numbers(args)
102
+ return m && affine(m)
103
+ }
104
+ },
105
+ matrix3d: {
106
+ arity: [16, 16],
107
+ build: (args) => {
108
+ const m = numbers(args)
109
+ return m && inPlane(m) ? affine([m[0], m[1], m[4], m[5], m[12], m[13]]) : null
110
+ }
111
+ },
112
+ translate: { arity: [1, 2], build: (args, box) => translation(args[0], args.at(1), box) },
113
+ translate3d: {
114
+ arity: [3, 3],
115
+ build: (args, box) => (depth(args[2]) ? translation(args[0], args[1], box) : null)
116
+ },
117
+ translatex: { arity: [1, 1], build: (args, box) => translation(args[0], undefined, box) },
118
+ translatey: { arity: [1, 1], build: (args, box) => translation('0', args[0], box) },
119
+ rotate: { arity: [1, 1], build: (args) => rotation(args[0]) },
120
+ rotatez: { arity: [1, 1], build: (args) => rotation(args[0]) },
121
+ scale: { arity: [1, 2], build: (args) => scaling(args[0], args.at(1)) },
122
+ scale3d: {
123
+ arity: [3, 3],
124
+ build: (args) => (factor(args[2]) === null ? null : scaling(args[0], args[1]))
125
+ },
126
+ scalex: { arity: [1, 1], build: (args) => scaling(args[0], '1') },
127
+ scaley: { arity: [1, 1], build: (args) => scaling('1', args[0]) },
128
+ skew: { arity: [1, 2], build: (args) => skewing(args[0], args.at(1)) },
129
+ skewx: { arity: [1, 1], build: (args) => skewing(args[0], undefined) },
130
+ skewy: { arity: [1, 1], build: (args) => skewing(undefined, args[0]) }
131
+ }
132
+
133
+ /**
134
+ * Whether a `matrix3d`, column by column, keeps the plane where it was: no turn out of it and no
135
+ * perspective. Depth, which moves nothing on the plane without perspective, may change.
136
+ */
137
+ function inPlane(m: number[]): boolean {
138
+ const outOfPlane = [m[2], m[3], m[6], m[7], m[8], m[9], m[11]]
139
+ return outOfPlane.every((value) => value === 0) && m[15] === 1
140
+ }
141
+
142
+ /**
143
+ * Whether a z offset is a length, which a percentage never is. It moves nothing on the plane without perspective, which a
144
+ * layer cannot hold, so it is read and dropped.
145
+ */
146
+ function depth(value: string): boolean {
147
+ return !value.trim().endsWith('%') && length(value, 0) !== null
148
+ }
149
+
150
+ /**
151
+ * A function's arguments, which CSS separates with commas: null when one is missing, or two
152
+ * stand without a comma between them.
153
+ */
154
+ function functionArguments(nodes: valueParser.Node[]): string[] | null {
155
+ const parts = nodes.filter((node) => node.type !== 'space' && node.type !== 'comment')
156
+ const args: string[] = []
157
+ for (const [index, node] of parts.entries()) {
158
+ const expectsArgument = index % 2 === 0
159
+ if (expectsArgument && node.type !== 'word') return null
160
+ if (!expectsArgument && (node.type !== 'div' || node.value !== ',')) return null
161
+ if (expectsArgument) args.push(node.value)
162
+ }
163
+ return parts.length % 2 === 1 ? args : null
164
+ }
165
+
166
+ /** One transform function as a matrix; null for one this does not know or cannot read. */
167
+ function transformFunction(name: string, args: string[], box: CSSTransformBox): Mat3 | null {
168
+ // Own names only, so `constructor` or `__proto__` reads as unknown instead of a prototype member.
169
+ const fn = Object.hasOwn(TRANSFORM_FUNCTIONS, name) ? TRANSFORM_FUNCTIONS[name] : undefined
170
+ if (!fn || args.length < fn.arity[0] || args.length > fn.arity[1]) return null
171
+ return fn.build(args, box)
172
+ }
173
+
174
+ /**
175
+ * A CSS `transform` as a 2D matrix about the transform origin, with percentages resolved against
176
+ * `box`. The identity for `none`; null when it turns out of the plane or cannot be read.
177
+ */
178
+ export function parseCSSTransform(value: string | undefined, box: CSSTransformBox): Mat3 | null {
179
+ const text = value?.trim()
180
+ if (!text || text === 'none') return Matrix.identity()
181
+ let matrix = Matrix.identity()
182
+ for (const node of valueParser(text).nodes) {
183
+ if (node.type === 'space' || node.type === 'comment') continue
184
+ if (node.type !== 'function') return null
185
+ const args = functionArguments(node.nodes)
186
+ const step = args && transformFunction(node.value.toLowerCase(), args, box)
187
+ if (!step) return null
188
+ matrix = Matrix.multiply(matrix, step)
189
+ }
190
+ return matrix
191
+ }
192
+
193
+ /** The `rotate` property: an angle, or one about the z axis. Null when it turns out of the plane. */
194
+ function rotateProperty(parts: string[]): Mat3 | null {
195
+ const last = parts.at(-1)
196
+ const axis = parts.slice(0, -1).join(' ').toLowerCase()
197
+ const radians = last === undefined ? null : angle(last)
198
+ if (radians === null || !['', 'z', '0 0 1'].includes(axis)) return null
199
+ return Matrix.rotated(radians)
200
+ }
201
+
202
+ /** A transform property's value as a matrix; the identity when it is unset or `none`. */
203
+ function property(value: string | undefined, parse: (parts: string[]) => Mat3 | null): Mat3 | null {
204
+ const text = value?.trim()
205
+ if (!text || text === 'none') return Matrix.identity()
206
+ return parse(splitCSSValue(text))
207
+ }
208
+
209
+ /**
210
+ * The 2D matrix about the transform origin that an element's `translate`, `rotate`, `scale`, and
211
+ * `transform` make together, applied in that order as CSS applies them. Null when one of them
212
+ * turns out of the plane or cannot be read.
213
+ */
214
+ export function parseCSSTransforms(style: CSSTransformStyle, box: CSSTransformBox): Mat3 | null {
215
+ const steps = [
216
+ property(style.translate, (parts) => {
217
+ const [x, y, z] = [parts.at(0), parts.at(1), parts.at(2)]
218
+ if (x === undefined || parts.length > 3 || (z !== undefined && !depth(z))) return null
219
+ return translation(x, y, box)
220
+ }),
221
+ property(style.rotate, rotateProperty),
222
+ property(style.scale, (parts) => {
223
+ const [x, y, z] = [parts.at(0), parts.at(1), parts.at(2)]
224
+ if (x === undefined || parts.length > 3 || (z !== undefined && factor(z) === null)) {
225
+ return null
226
+ }
227
+ return scaling(x, y)
228
+ }),
229
+ parseCSSTransform(style.transform, box)
230
+ ]
231
+ if (steps.some((step) => step === null)) return null
232
+ return Matrix.multiply(...steps.filter((step) => step !== null))
233
+ }
234
+
235
+ const ORIGIN_KEYWORDS: Partial<Record<string, { axis: 'x' | 'y' | 'either'; fraction: number }>> = {
236
+ left: { axis: 'x', fraction: 0 },
237
+ right: { axis: 'x', fraction: 1 },
238
+ top: { axis: 'y', fraction: 0 },
239
+ bottom: { axis: 'y', fraction: 1 },
240
+ center: { axis: 'either', fraction: 0.5 }
241
+ }
242
+
243
+ /**
244
+ * Where a CSS `transform-origin` puts the point an element transforms about, from its border
245
+ * box's top left corner. Its center when it is unset or cannot be read.
246
+ */
247
+ export function parseCSSTransformOrigin(value: string | undefined, box: CSSTransformBox): Vector {
248
+ const center = { x: box.width / 2, y: box.height / 2 }
249
+ const tokens = splitCSSValue(value?.trim() ?? '')
250
+ // A third value is the depth, which must be a length; anything past it is not an origin.
251
+ const depthOk = tokens.length < 3 || (tokens.length === 3 && depth(tokens[2]))
252
+ if (tokens.length === 0 || !depthOk) return center
253
+ const parts = tokens.slice(0, 2)
254
+ // A vertical keyword first, as in `top left`, names the y offset.
255
+ const [first = 'center', second = 'center'] = parts.map((part) => part.toLowerCase())
256
+ const swapped = ORIGIN_KEYWORDS[first]?.axis === 'y' || ORIGIN_KEYWORDS[second]?.axis === 'x'
257
+ const [x, y] = swapped ? [second, first] : [first, second]
258
+ const resolve = (part: string, extent: number, axis: 'x' | 'y') => {
259
+ const keyword = ORIGIN_KEYWORDS[part]
260
+ if (!keyword) return length(part, extent)
261
+ return keyword.axis === axis || keyword.axis === 'either' ? keyword.fraction * extent : null
262
+ }
263
+ const ox = resolve(x, box.width, 'x')
264
+ const oy = resolve(y, box.height, 'y')
265
+ return ox === null || oy === null ? center : { x: ox, y: oy }
266
+ }
package/src/css/values.ts CHANGED
@@ -3,6 +3,14 @@ import valueParser from 'postcss-value-parser'
3
3
  import { tryParseColor } from '../color'
4
4
  import type { Color } from '../primitives'
5
5
 
6
+ /** A CSS dimension's number and lowercase unit, or null. */
7
+ export function dimension(value: string): { number: number; unit: string } | null {
8
+ const parsed = valueParser.unit(value)
9
+ if (!parsed) return null
10
+ const number = Number(parsed.number)
11
+ return Number.isFinite(number) ? { number, unit: parsed.unit.toLowerCase() } : null
12
+ }
13
+
6
14
  /** Pixels in a rem, as browsers use by default. */
7
15
  const ROOT_FONT_SIZE = 16
8
16
 
package/src/cubic.ts ADDED
@@ -0,0 +1,14 @@
1
+ import type { Vector } from './primitives'
2
+
3
+ /** The point at `t` along the cubic from `p0` to `p3` with control points `p1` and `p2`. */
4
+ export function cubicPoint(p0: Vector, p1: Vector, p2: Vector, p3: Vector, t: number): Vector {
5
+ const u = 1 - t
6
+ const a = u * u * u
7
+ const b = 3 * u * u * t
8
+ const c = 3 * u * t * t
9
+ const d = t * t * t
10
+ return {
11
+ x: a * p0.x + b * p1.x + c * p2.x + d * p3.x,
12
+ y: a * p0.y + b * p1.y + c * p2.y + d * p3.y
13
+ }
14
+ }
@@ -1,3 +1,7 @@
1
+ import { omitBy } from 'es-toolkit/object'
2
+
3
+ import type { ExportSetting } from './types'
4
+
1
5
  /**
2
6
  * Formats a node's export settings may persist. Each id must name a Core IO
3
7
  * export adapter that supports node export; labels and options come from that
@@ -10,3 +14,28 @@ export type ExportFormatId = (typeof EXPORT_FORMAT_IDS)[number]
10
14
  export function isExportFormatId(value: unknown): value is ExportFormatId {
11
15
  return EXPORT_FORMAT_IDS.some((id) => id === value)
12
16
  }
17
+
18
+ /** What each of Figma's export options reads as when an export leaves it out. */
19
+ export const EXPORT_OPTION_DEFAULTS = {
20
+ suffix: '',
21
+ contentsOnly: true,
22
+ useAbsoluteBounds: false,
23
+ colorProfile: 'DOCUMENT',
24
+ svgOutlineText: true,
25
+ svgIdAttribute: false,
26
+ svgSimplifyStroke: true
27
+ } as const satisfies Required<Omit<ExportSetting, 'scale' | 'format' | 'size'>>
28
+
29
+ /** An export as the model keeps it: options that are unset or at Figma's default left out. */
30
+ export function compactExportSetting({
31
+ scale,
32
+ format,
33
+ size,
34
+ ...options
35
+ }: ExportSetting): ExportSetting {
36
+ const changed = omitBy(
37
+ options,
38
+ (value, key) => value === undefined || value === EXPORT_OPTION_DEFAULTS[key]
39
+ )
40
+ return size ? { scale, format, size, ...changed } : { scale, format, ...changed }
41
+ }
@@ -16,7 +16,9 @@ export const TEXT_METRIC_FIELDS = [
16
16
  'listSpacing',
17
17
  'paragraphSpacing',
18
18
  'paragraphIndent',
19
- 'hangingList'
19
+ 'hangingList',
20
+ // A cap-height trim drops the space above and below the glyphs, so the text measures shorter.
21
+ 'leadingTrim'
20
22
  ] as const satisfies readonly (keyof SceneNode)[]
21
23
 
22
24
  export const TEXT_SHAPING_FIELDS = [
@@ -244,6 +244,27 @@ export function figJamOutlineColorName(color: Color | undefined): FigJamColorNam
244
244
  )
245
245
  }
246
246
 
247
+ /**
248
+ * How FigJam draws a section's title on a board, measured in FigJam: a pill in the colour of the
249
+ * outline drawn on top, with white text on the palette's darkest colours and black on the rest;
250
+ * null for a section without an outline from the palette. `colorOf` reads an outline's colour as
251
+ * drawn, such as a variable's value in the section's mode.
252
+ */
253
+ export function figJamSectionTitleColors(
254
+ section: Pick<SceneNode, 'strokes'>,
255
+ colorOf: (stroke: Stroke, index: number) => Color = (stroke) => stroke.color
256
+ ): { strokeIndex: number; background: Color; lightText: boolean } | null {
257
+ const strokeIndex = section.strokes.findLastIndex(
258
+ (stroke) => stroke.visible && stroke.opacity > 0
259
+ )
260
+ const outline = section.strokes.at(strokeIndex)
261
+ if (strokeIndex === -1 || !outline) return null
262
+ const color = colorOf(outline, strokeIndex)
263
+ const name = figJamOutlineColorName(color)
264
+ if (!name) return null
265
+ return { strokeIndex, background: { ...color, a: 1 }, lightText: PALETTE[name].lightLabel }
266
+ }
267
+
247
268
  /** The fill mode a shape's first fill shows. */
248
269
  export function figJamFillMode(fill: Fill | undefined): FigJamFillMode {
249
270
  if (!fill || !fill.visible || fill.opacity === 0) return 'none'
@@ -380,9 +401,16 @@ export function figJamSectionColorFields(
380
401
  name: FigJamColorName
381
402
  ): Partial<SceneNode> {
382
403
  const { sectionFill, sectionStroke } = PALETTE[name]
404
+ // The outline shows in full at FigJam's 2, as its menu draws it, unless the line menu hid it.
405
+ const strokes = strokesIn(section.strokes, parseColor(sectionStroke), SECTION_STROKE_WEIGHT).map(
406
+ (stroke): Stroke =>
407
+ stroke.opacity === 0
408
+ ? stroke
409
+ : { ...stroke, opacity: 1, visible: true, weight: SECTION_STROKE_WEIGHT }
410
+ )
383
411
  return {
384
412
  fills: [{ type: 'SOLID', color: parseColor(sectionFill), opacity: 1, visible: true }],
385
- strokes: strokesIn(section.strokes, parseColor(sectionStroke), SECTION_STROKE_WEIGHT),
413
+ strokes,
386
414
  strokeWeight: SECTION_STROKE_WEIGHT
387
415
  }
388
416
  }
@@ -9,9 +9,15 @@ import type { GeneratedFrameKind } from './kind'
9
9
 
10
10
  /** FigJam makes a shape with text 176 px square, from its tool and from its plugin API. */
11
11
  export const SHAPE_WITH_TEXT_SIZE = 176
12
- /** FigJam rounds every corner of a shape by 6 px. */
12
+ /** FigJam rounds every corner of a shape by 6 px, but a rounded rectangle's. */
13
13
  export const SHAPE_WITH_TEXT_CORNER_RADIUS = 6
14
14
 
15
+ /**
16
+ * FigJam keeps a rounded rectangle's corner radius at 0.4 of its shorter side, through resizes,
17
+ * measured in FigJam; Figma draws the shape from that radius.
18
+ */
19
+ const ROUNDED_RECTANGLE_RADIUS = 0.4
20
+
15
21
  const LABEL_FILL: Fill = { type: 'SOLID', color: { ...BLACK }, opacity: 0.8, visible: true }
16
22
 
17
23
  /** How the label sits in its shape: as wide as the label box, as tall as its text. */
@@ -42,7 +48,27 @@ export function shapeWithTextFrameFields(
42
48
  paddingRight: Math.max(0, width - box.x - box.width),
43
49
  paddingBottom: Math.max(0, height - box.y - box.height),
44
50
  paddingLeft: box.x,
45
- clipsContent: false
51
+ clipsContent: false,
52
+ ...(kind === 'ROUNDED_RECTANGLE'
53
+ ? { cornerRadius: ROUNDED_RECTANGLE_RADIUS * Math.min(width, height) }
54
+ : {})
55
+ }
56
+ }
57
+
58
+ /**
59
+ * What turning a shape into `kind` changes, keeping its size, as FigJam's shape switcher does: the
60
+ * label area, and the corners a rounded rectangle leaves go back to FigJam's 6.
61
+ */
62
+ export function shapeWithTextKindFields(
63
+ shape: Pick<SceneNode, 'shapeWithTextType' | 'width' | 'height'>,
64
+ kind: ShapeWithTextType
65
+ ): Partial<SceneNode> {
66
+ const leavesRounded =
67
+ shape.shapeWithTextType === 'ROUNDED_RECTANGLE' && kind !== shape.shapeWithTextType
68
+ return {
69
+ shapeWithTextType: kind,
70
+ ...(leavesRounded ? { cornerRadius: SHAPE_WITH_TEXT_CORNER_RADIUS } : {}),
71
+ ...shapeWithTextFrameFields(kind, shape.width, shape.height)
46
72
  }
47
73
  }
48
74