@moondraft/import 0.0.0-stage → 0.1.0

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 (323) hide show
  1. package/CHANGELOG.md +7 -0
  2. package/LICENSE +34 -0
  3. package/README.md +36 -2
  4. package/SOURCE.md +169 -0
  5. package/dist/adapter/convert.d.ts +122 -0
  6. package/dist/adapter/convert.js +2302 -0
  7. package/dist/adapter/figma.d.ts +9 -0
  8. package/dist/adapter/figma.js +30 -0
  9. package/dist/adapter/file-components.d.ts +14 -0
  10. package/dist/adapter/file-components.js +262 -0
  11. package/dist/adapter/file-resources.d.ts +29 -0
  12. package/dist/adapter/file-resources.js +562 -0
  13. package/dist/adapter/index.d.ts +6 -0
  14. package/dist/adapter/index.js +9 -0
  15. package/dist/adapter/link.d.ts +45 -0
  16. package/dist/adapter/link.js +688 -0
  17. package/dist/adapter/payload.d.ts +31 -0
  18. package/dist/adapter/payload.js +78 -0
  19. package/dist/adapter/shapes.d.ts +31 -0
  20. package/dist/adapter/shapes.js +75 -0
  21. package/dist/adapter/variables.d.ts +44 -0
  22. package/dist/adapter/variables.js +103 -0
  23. package/dist/figma-file.d.ts +38 -0
  24. package/dist/figma-file.js +97 -0
  25. package/dist/figma-worker.d.ts +11 -0
  26. package/dist/figma-worker.js +12 -0
  27. package/dist/figma.d.ts +26 -0
  28. package/dist/figma.js +96 -0
  29. package/dist/index.d.ts +28 -0
  30. package/dist/index.js +49 -0
  31. package/dist/report.d.ts +15 -0
  32. package/dist/report.js +69 -0
  33. package/dist/svg.d.ts +33 -0
  34. package/dist/svg.js +44 -0
  35. package/dist/syntax.d.ts +8 -0
  36. package/dist/syntax.js +19 -0
  37. package/dist/taco/figma-import/clipboard.d.ts +25 -0
  38. package/dist/taco/figma-import/clipboard.js +87 -0
  39. package/dist/taco/figma-import/clipboardSize.d.ts +31 -0
  40. package/dist/taco/figma-import/clipboardSize.js +107 -0
  41. package/dist/taco/figma-import/container/compression.d.ts +9 -0
  42. package/dist/taco/figma-import/container/compression.js +174 -0
  43. package/dist/taco/figma-import/container/error.d.ts +15 -0
  44. package/dist/taco/figma-import/container/error.js +19 -0
  45. package/dist/taco/figma-import/container/kiwiContainer.d.ts +38 -0
  46. package/dist/taco/figma-import/container/kiwiContainer.js +61 -0
  47. package/dist/taco/figma-import/container/limits.d.ts +22 -0
  48. package/dist/taco/figma-import/container/limits.js +22 -0
  49. package/dist/taco/figma-import/container/zip.d.ts +14 -0
  50. package/dist/taco/figma-import/container/zip.js +154 -0
  51. package/dist/taco/figma-import/figFile.d.ts +34 -0
  52. package/dist/taco/figma-import/figFile.js +91 -0
  53. package/dist/taco/figma-import/figFileDocument.d.ts +25 -0
  54. package/dist/taco/figma-import/figFileDocument.js +100 -0
  55. package/dist/taco/figma-import/import/blobs.d.ts +10 -0
  56. package/dist/taco/figma-import/import/blobs.js +23 -0
  57. package/dist/taco/figma-import/import/carry.d.ts +11 -0
  58. package/dist/taco/figma-import/import/carry.js +33 -0
  59. package/dist/taco/figma-import/import/componentProps.d.ts +84 -0
  60. package/dist/taco/figma-import/import/componentProps.js +313 -0
  61. package/dist/taco/figma-import/import/componentSets.d.ts +83 -0
  62. package/dist/taco/figma-import/import/componentSets.js +647 -0
  63. package/dist/taco/figma-import/import/converters/effects.d.ts +31 -0
  64. package/dist/taco/figma-import/import/converters/effects.js +127 -0
  65. package/dist/taco/figma-import/import/converters/ellipse.d.ts +7 -0
  66. package/dist/taco/figma-import/import/converters/ellipse.js +54 -0
  67. package/dist/taco/figma-import/import/converters/frame.d.ts +21 -0
  68. package/dist/taco/figma-import/import/converters/frame.js +55 -0
  69. package/dist/taco/figma-import/import/converters/gradient.d.ts +72 -0
  70. package/dist/taco/figma-import/import/converters/gradient.js +106 -0
  71. package/dist/taco/figma-import/import/converters/group.d.ts +21 -0
  72. package/dist/taco/figma-import/import/converters/group.js +39 -0
  73. package/dist/taco/figma-import/import/converters/image.d.ts +13 -0
  74. package/dist/taco/figma-import/import/converters/image.js +83 -0
  75. package/dist/taco/figma-import/import/converters/layout.d.ts +34 -0
  76. package/dist/taco/figma-import/import/converters/layout.js +168 -0
  77. package/dist/taco/figma-import/import/converters/line.d.ts +11 -0
  78. package/dist/taco/figma-import/import/converters/line.js +40 -0
  79. package/dist/taco/figma-import/import/converters/rectangle.d.ts +3 -0
  80. package/dist/taco/figma-import/import/converters/rectangle.js +11 -0
  81. package/dist/taco/figma-import/import/converters/section.d.ts +9 -0
  82. package/dist/taco/figma-import/import/converters/section.js +35 -0
  83. package/dist/taco/figma-import/import/converters/shared.d.ts +68 -0
  84. package/dist/taco/figma-import/import/converters/shared.js +362 -0
  85. package/dist/taco/figma-import/import/converters/text.d.ts +41 -0
  86. package/dist/taco/figma-import/import/converters/text.js +576 -0
  87. package/dist/taco/figma-import/import/converters/vector.d.ts +84 -0
  88. package/dist/taco/figma-import/import/converters/vector.js +468 -0
  89. package/dist/taco/figma-import/import/converters/vectorNetwork.d.ts +98 -0
  90. package/dist/taco/figma-import/import/converters/vectorNetwork.js +385 -0
  91. package/dist/taco/figma-import/import/fields.d.ts +15 -0
  92. package/dist/taco/figma-import/import/fields.js +29 -0
  93. package/dist/taco/figma-import/import/instances.d.ts +163 -0
  94. package/dist/taco/figma-import/import/instances.js +1323 -0
  95. package/dist/taco/figma-import/import/nestingCycles.d.ts +86 -0
  96. package/dist/taco/figma-import/import/nestingCycles.js +488 -0
  97. package/dist/taco/figma-import/import/slots.d.ts +56 -0
  98. package/dist/taco/figma-import/import/slots.js +96 -0
  99. package/dist/taco/figma-import/import/styles.d.ts +46 -0
  100. package/dist/taco/figma-import/import/styles.js +42 -0
  101. package/dist/taco/figma-import/import/tree.d.ts +66 -0
  102. package/dist/taco/figma-import/import/tree.js +194 -0
  103. package/dist/taco/figma-import/import/types.d.ts +232 -0
  104. package/dist/taco/figma-import/import/types.js +33 -0
  105. package/dist/taco/figma-import/import/variables.d.ts +136 -0
  106. package/dist/taco/figma-import/import/variables.js +762 -0
  107. package/dist/taco/figma-import/import/variantTrees.d.ts +20 -0
  108. package/dist/taco/figma-import/import/variantTrees.js +480 -0
  109. package/dist/taco/figma-import/import/walk.d.ts +159 -0
  110. package/dist/taco/figma-import/import/walk.js +1297 -0
  111. package/dist/taco/figma-import/index.d.ts +22 -0
  112. package/dist/taco/figma-import/index.js +13 -0
  113. package/dist/taco/figma-import/kiwi/byteReader.d.ts +37 -0
  114. package/dist/taco/figma-import/kiwi/byteReader.js +151 -0
  115. package/dist/taco/figma-import/kiwi/error.d.ts +14 -0
  116. package/dist/taco/figma-import/kiwi/error.js +29 -0
  117. package/dist/taco/figma-import/kiwi/limits.d.ts +18 -0
  118. package/dist/taco/figma-import/kiwi/limits.js +18 -0
  119. package/dist/taco/figma-import/kiwi/message.d.ts +10 -0
  120. package/dist/taco/figma-import/kiwi/message.js +145 -0
  121. package/dist/taco/figma-import/kiwi/schema.d.ts +42 -0
  122. package/dist/taco/figma-import/kiwi/schema.js +206 -0
  123. package/dist/taco/figma-import/kiwi/types.d.ts +46 -0
  124. package/dist/taco/figma-import/kiwi/types.js +18 -0
  125. package/dist/taco/model/agent-hidden-fields.d.ts +20 -0
  126. package/dist/taco/model/agent-hidden-fields.js +29 -0
  127. package/dist/taco/model/asset-users.d.ts +23 -0
  128. package/dist/taco/model/asset-users.js +128 -0
  129. package/dist/taco/model/auto-layout-lines.d.ts +32 -0
  130. package/dist/taco/model/auto-layout-lines.js +70 -0
  131. package/dist/taco/model/codec.d.ts +444 -0
  132. package/dist/taco/model/codec.js +5635 -0
  133. package/dist/taco/model/color.d.ts +11 -0
  134. package/dist/taco/model/color.js +61 -0
  135. package/dist/taco/model/comment-operations.d.ts +102 -0
  136. package/dist/taco/model/comment-operations.js +227 -0
  137. package/dist/taco/model/component-evaluation.d.ts +94 -0
  138. package/dist/taco/model/component-evaluation.js +548 -0
  139. package/dist/taco/model/component-expressions.d.ts +242 -0
  140. package/dist/taco/model/component-expressions.js +1082 -0
  141. package/dist/taco/model/component-properties.d.ts +111 -0
  142. package/dist/taco/model/component-properties.js +339 -0
  143. package/dist/taco/model/component-samples.d.ts +83 -0
  144. package/dist/taco/model/component-samples.js +290 -0
  145. package/dist/taco/model/component-validity.d.ts +85 -0
  146. package/dist/taco/model/component-validity.js +397 -0
  147. package/dist/taco/model/components.d.ts +155 -0
  148. package/dist/taco/model/components.js +505 -0
  149. package/dist/taco/model/connector-ends.d.ts +20 -0
  150. package/dist/taco/model/connector-ends.js +188 -0
  151. package/dist/taco/model/connector-geometry.d.ts +74 -0
  152. package/dist/taco/model/connector-geometry.js +293 -0
  153. package/dist/taco/model/corner-radii.d.ts +26 -0
  154. package/dist/taco/model/corner-radii.js +47 -0
  155. package/dist/taco/model/derivations.d.ts +81 -0
  156. package/dist/taco/model/derivations.js +151 -0
  157. package/dist/taco/model/export-settings.d.ts +53 -0
  158. package/dist/taco/model/export-settings.js +156 -0
  159. package/dist/taco/model/expressions/ast.d.ts +153 -0
  160. package/dist/taco/model/expressions/ast.js +6 -0
  161. package/dist/taco/model/expressions/cache.d.ts +23 -0
  162. package/dist/taco/model/expressions/cache.js +43 -0
  163. package/dist/taco/model/expressions/check.d.ts +72 -0
  164. package/dist/taco/model/expressions/check.js +517 -0
  165. package/dist/taco/model/expressions/completion.d.ts +61 -0
  166. package/dist/taco/model/expressions/completion.js +421 -0
  167. package/dist/taco/model/expressions/evaluate.d.ts +32 -0
  168. package/dist/taco/model/expressions/evaluate.js +519 -0
  169. package/dist/taco/model/expressions/forms.d.ts +84 -0
  170. package/dist/taco/model/expressions/forms.js +200 -0
  171. package/dist/taco/model/expressions/index.d.ts +14 -0
  172. package/dist/taco/model/expressions/index.js +17 -0
  173. package/dist/taco/model/expressions/lexer.d.ts +42 -0
  174. package/dist/taco/model/expressions/lexer.js +213 -0
  175. package/dist/taco/model/expressions/library.d.ts +33 -0
  176. package/dist/taco/model/expressions/library.js +117 -0
  177. package/dist/taco/model/expressions/names.d.ts +43 -0
  178. package/dist/taco/model/expressions/names.js +99 -0
  179. package/dist/taco/model/expressions/parser.d.ts +15 -0
  180. package/dist/taco/model/expressions/parser.js +438 -0
  181. package/dist/taco/model/expressions/rename.d.ts +34 -0
  182. package/dist/taco/model/expressions/rename.js +46 -0
  183. package/dist/taco/model/expressions/source.d.ts +15 -0
  184. package/dist/taco/model/expressions/source.js +161 -0
  185. package/dist/taco/model/expressions/types.d.ts +119 -0
  186. package/dist/taco/model/expressions/types.js +102 -0
  187. package/dist/taco/model/expressions/values.d.ts +97 -0
  188. package/dist/taco/model/expressions/values.js +160 -0
  189. package/dist/taco/model/geometry-limits.d.ts +16 -0
  190. package/dist/taco/model/geometry-limits.js +67 -0
  191. package/dist/taco/model/geometry.d.ts +47 -0
  192. package/dist/taco/model/geometry.js +193 -0
  193. package/dist/taco/model/group-bounds.d.ts +38 -0
  194. package/dist/taco/model/group-bounds.js +144 -0
  195. package/dist/taco/model/id-table.d.ts +37 -0
  196. package/dist/taco/model/id-table.js +80 -0
  197. package/dist/taco/model/index.d.ts +25 -0
  198. package/dist/taco/model/index.js +27 -0
  199. package/dist/taco/model/instance-resolver.d.ts +372 -0
  200. package/dist/taco/model/instance-resolver.js +1748 -0
  201. package/dist/taco/model/layer-syntax.d.ts +340 -0
  202. package/dist/taco/model/layer-syntax.js +3506 -0
  203. package/dist/taco/model/layout/constraints.d.ts +84 -0
  204. package/dist/taco/model/layout/constraints.js +445 -0
  205. package/dist/taco/model/layout/frame-layout.d.ts +46 -0
  206. package/dist/taco/model/layout/frame-layout.js +618 -0
  207. package/dist/taco/model/layout/node-sizing.d.ts +163 -0
  208. package/dist/taco/model/layout/node-sizing.js +486 -0
  209. package/dist/taco/model/legacy-connector-caps.d.ts +15 -0
  210. package/dist/taco/model/legacy-connector-caps.js +70 -0
  211. package/dist/taco/model/linear-call.d.ts +48 -0
  212. package/dist/taco/model/linear-call.js +58 -0
  213. package/dist/taco/model/new-nodes.d.ts +145 -0
  214. package/dist/taco/model/new-nodes.js +277 -0
  215. package/dist/taco/model/node-builder.d.ts +449 -0
  216. package/dist/taco/model/node-builder.js +2294 -0
  217. package/dist/taco/model/node-table-watch.d.ts +36 -0
  218. package/dist/taco/model/node-table-watch.js +63 -0
  219. package/dist/taco/model/node-transform.d.ts +39 -0
  220. package/dist/taco/model/node-transform.js +79 -0
  221. package/dist/taco/model/node-users-index.d.ts +10 -0
  222. package/dist/taco/model/node-users-index.js +82 -0
  223. package/dist/taco/model/operation-references.d.ts +80 -0
  224. package/dist/taco/model/operation-references.js +399 -0
  225. package/dist/taco/model/operations.d.ts +610 -0
  226. package/dist/taco/model/operations.js +2882 -0
  227. package/dist/taco/model/overlay-table.d.ts +71 -0
  228. package/dist/taco/model/overlay-table.js +171 -0
  229. package/dist/taco/model/override-remap.d.ts +69 -0
  230. package/dist/taco/model/override-remap.js +230 -0
  231. package/dist/taco/model/parent-table.d.ts +53 -0
  232. package/dist/taco/model/parent-table.js +193 -0
  233. package/dist/taco/model/problem-collector.d.ts +27 -0
  234. package/dist/taco/model/problem-collector.js +101 -0
  235. package/dist/taco/model/sha256.d.ts +10 -0
  236. package/dist/taco/model/sha256.js +74 -0
  237. package/dist/taco/model/shape-geometry.d.ts +96 -0
  238. package/dist/taco/model/shape-geometry.js +368 -0
  239. package/dist/taco/model/shared-node-table.d.ts +97 -0
  240. package/dist/taco/model/shared-node-table.js +546 -0
  241. package/dist/taco/model/slots.d.ts +113 -0
  242. package/dist/taco/model/slots.js +281 -0
  243. package/dist/taco/model/stroke-settings.d.ts +37 -0
  244. package/dist/taco/model/stroke-settings.js +90 -0
  245. package/dist/taco/model/svg-path-import.d.ts +89 -0
  246. package/dist/taco/model/svg-path-import.js +219 -0
  247. package/dist/taco/model/text-measurement.d.ts +142 -0
  248. package/dist/taco/model/text-measurement.js +846 -0
  249. package/dist/taco/model/text-reads.d.ts +66 -0
  250. package/dist/taco/model/text-reads.js +128 -0
  251. package/dist/taco/model/text-sizer.d.ts +358 -0
  252. package/dist/taco/model/text-sizer.js +996 -0
  253. package/dist/taco/model/theme-users.d.ts +59 -0
  254. package/dist/taco/model/theme-users.js +312 -0
  255. package/dist/taco/model/thumbnail.d.ts +15 -0
  256. package/dist/taco/model/thumbnail.js +34 -0
  257. package/dist/taco/model/types.d.ts +2347 -0
  258. package/dist/taco/model/types.js +366 -0
  259. package/dist/taco/model/unknown-property.d.ts +5 -0
  260. package/dist/taco/model/unknown-property.js +7 -0
  261. package/dist/taco/model/validated-documents.d.ts +22 -0
  262. package/dist/taco/model/validated-documents.js +22 -0
  263. package/dist/taco/model/variable-modes.d.ts +61 -0
  264. package/dist/taco/model/variable-modes.js +438 -0
  265. package/dist/taco/model/variable-references.d.ts +54 -0
  266. package/dist/taco/model/variable-references.js +183 -0
  267. package/dist/taco/model/variables.d.ts +24 -0
  268. package/dist/taco/model/variables.js +151 -0
  269. package/dist/taco/model/vector-network.d.ts +34 -0
  270. package/dist/taco/model/vector-network.js +411 -0
  271. package/dist/taco/svg-import/import-svg.d.ts +36 -0
  272. package/dist/taco/svg-import/import-svg.js +729 -0
  273. package/dist/taco/svg-import/index.d.ts +3 -0
  274. package/dist/taco/svg-import/index.js +3 -0
  275. package/dist/taco/svg-import/limits/limits.d.ts +79 -0
  276. package/dist/taco/svg-import/limits/limits.js +160 -0
  277. package/dist/taco/svg-import/paint/gradients.d.ts +64 -0
  278. package/dist/taco/svg-import/paint/gradients.js +429 -0
  279. package/dist/taco/svg-import/paint/paint.d.ts +32 -0
  280. package/dist/taco/svg-import/paint/paint.js +162 -0
  281. package/dist/taco/svg-import/paths/paths.d.ts +58 -0
  282. package/dist/taco/svg-import/paths/paths.js +205 -0
  283. package/dist/taco/svg-import/public-types.d.ts +279 -0
  284. package/dist/taco/svg-import/public-types.js +56 -0
  285. package/dist/taco/svg-import/report/report.d.ts +44 -0
  286. package/dist/taco/svg-import/report/report.js +79 -0
  287. package/dist/taco/svg-import/reuse/reuse.d.ts +99 -0
  288. package/dist/taco/svg-import/reuse/reuse.js +840 -0
  289. package/dist/taco/svg-import/shapes/shapes.d.ts +35 -0
  290. package/dist/taco/svg-import/shapes/shapes.js +386 -0
  291. package/dist/taco/svg-import/structure/effects.d.ts +26 -0
  292. package/dist/taco/svg-import/structure/effects.js +618 -0
  293. package/dist/taco/svg-import/structure/image.d.ts +24 -0
  294. package/dist/taco/svg-import/structure/image.js +495 -0
  295. package/dist/taco/svg-import/structure/walk.d.ts +85 -0
  296. package/dist/taco/svg-import/structure/walk.js +544 -0
  297. package/dist/taco/svg-import/style/color.d.ts +40 -0
  298. package/dist/taco/svg-import/style/color.js +275 -0
  299. package/dist/taco/svg-import/style/computed.d.ts +214 -0
  300. package/dist/taco/svg-import/style/computed.js +1534 -0
  301. package/dist/taco/svg-import/style/css.d.ts +127 -0
  302. package/dist/taco/svg-import/style/css.js +541 -0
  303. package/dist/taco/svg-import/style/media.d.ts +25 -0
  304. package/dist/taco/svg-import/style/media.js +307 -0
  305. package/dist/taco/svg-import/style/properties.d.ts +43 -0
  306. package/dist/taco/svg-import/style/properties.js +135 -0
  307. package/dist/taco/svg-import/style/selector-reach.d.ts +30 -0
  308. package/dist/taco/svg-import/style/selector-reach.js +169 -0
  309. package/dist/taco/svg-import/style/selector-validity.d.ts +14 -0
  310. package/dist/taco/svg-import/style/selector-validity.js +307 -0
  311. package/dist/taco/svg-import/text/text.d.ts +29 -0
  312. package/dist/taco/svg-import/text/text.js +545 -0
  313. package/dist/taco/svg-import/transform/matrix.d.ts +111 -0
  314. package/dist/taco/svg-import/transform/matrix.js +229 -0
  315. package/dist/taco/svg-import/tree/element-data.d.ts +52 -0
  316. package/dist/taco/svg-import/tree/element-data.js +155 -0
  317. package/dist/taco/svg-import/tree/elements.d.ts +36 -0
  318. package/dist/taco/svg-import/tree/elements.js +121 -0
  319. package/dist/taco/svg-import/units/length.d.ts +43 -0
  320. package/dist/taco/svg-import/units/length.js +125 -0
  321. package/dist/taco/svg-import/units/viewbox.d.ts +35 -0
  322. package/dist/taco/svg-import/units/viewbox.js +77 -0
  323. package/package.json +54 -4
@@ -0,0 +1,219 @@
1
+ import { ModelError } from "./codec.js";
2
+ import { getNodeBounds } from "./geometry.js";
3
+ import { transformIsInvertible } from "./node-transform.js";
4
+ import { openPathChain } from "./stroke-settings.js";
5
+ import { vectorNetworkFromSvgPathData } from "./vector-network.js";
6
+ /** The values of SVG's `fill-rule` property. */
7
+ export const SVG_FILL_RULES = ["nonzero", "evenodd"];
8
+ /** The values of SVG's `stroke-linecap` property. */
9
+ export const SVG_STROKE_CAPS = ["butt", "round", "square"];
10
+ /** The values of SVG's `stroke-linejoin` property that the model can draw. */
11
+ export const SVG_STROKE_JOINS = ["miter", "round", "bevel"];
12
+ const OPTION_KEYS = new Set([
13
+ "id", "name", "pathData", "style", "transform", "fillRule", "strokeCap",
14
+ "strokeJoin", "visible", "locked", "opacity",
15
+ ]);
16
+ const CAP_FROM_SVG = {
17
+ butt: "none",
18
+ round: "round",
19
+ square: "square",
20
+ };
21
+ const ROOT = "$svgPath";
22
+ function refuse(message, path, cause) {
23
+ throw new ModelError("INVALID_SCHEMA", message, path, cause === undefined ? undefined : { cause });
24
+ }
25
+ /** A refused value for a message: strings are quoted, and nothing here can throw, unlike `JSON.stringify`. */
26
+ function shown(value) {
27
+ return typeof value === "string" ? JSON.stringify(value) : `a value of type ${typeof value}`;
28
+ }
29
+ function oneOf(value, allowed, fallback, path) {
30
+ if (value === undefined)
31
+ return fallback;
32
+ if (!allowed.includes(value)) {
33
+ refuse(`Expected one of ${allowed.join(", ")} but got ${shown(value)}`, path);
34
+ }
35
+ return value;
36
+ }
37
+ function importedTransform(value, path) {
38
+ if (value === undefined)
39
+ return [1, 0, 0, 1, 0, 0];
40
+ if (!Array.isArray(value) || value.length !== 6) {
41
+ refuse("A transform matrix must contain six numbers", path);
42
+ }
43
+ value.forEach((entry, index) => {
44
+ if (typeof entry !== "number" || !Number.isFinite(entry)) {
45
+ refuse("Expected a finite number", `${path}[${String(index)}]`);
46
+ }
47
+ });
48
+ const [a, b, c, d, e, f] = value;
49
+ const transform = [a, b, c, d, e, f];
50
+ if (!transformIsInvertible(transform)) {
51
+ refuse("A transform matrix must be invertible, so its determinant cannot be zero or too small to divide by", path);
52
+ }
53
+ const determinant = a * d - b * c;
54
+ const inverse = [d / determinant, -b / determinant, -c / determinant, a / determinant,
55
+ (c * f - d * e) / determinant, (b * e - a * f) / determinant];
56
+ if (!inverse.every(Number.isFinite)) {
57
+ refuse("A transform matrix must have an inverse of finite numbers", path);
58
+ }
59
+ return transform;
60
+ }
61
+ function importedNetwork(pathData, fillRule) {
62
+ const path = `${ROOT}.pathData`;
63
+ if (typeof pathData !== "string")
64
+ refuse("Expected a string of SVG path data", path);
65
+ if (pathData.trim().length === 0)
66
+ refuse("SVG path data is empty", path);
67
+ let network;
68
+ try {
69
+ network = vectorNetworkFromSvgPathData(pathData, fillRule);
70
+ }
71
+ catch (error) {
72
+ refuse(`SVG path data cannot be read: ${error instanceof Error ? error.message : String(error)}`, path, error);
73
+ }
74
+ if (network.segments.length === 0) {
75
+ refuse("SVG path data draws nothing: it has no line, curve or arc", path);
76
+ }
77
+ const finite = (value) => typeof value === "number"
78
+ ? Number.isFinite(value)
79
+ : typeof value !== "object" || value === null || Object.values(value).every(finite);
80
+ if (!finite(network.vertices) || !finite(network.segments)) {
81
+ refuse("SVG path data has a number that is not finite", path);
82
+ }
83
+ return network;
84
+ }
85
+ /**
86
+ * SVG's `stroke-linecap` ends every dash as well as the path, and the
87
+ * renderer draws a stroke's open ends with its dash ends wherever the path's
88
+ * own end points are not used (see `drawsEndsWithDashEnds`). So every stroke
89
+ * takes the cap as its dash ends; one that names its own keeps them (the
90
+ * caller spreads the stroke after this).
91
+ */
92
+ function dashEnds(stroke, cap) {
93
+ return typeof stroke === "object" && stroke !== null ? { dashCap: cap } : {};
94
+ }
95
+ function isCentered(stroke) {
96
+ if (typeof stroke !== "object" || stroke === null)
97
+ return true;
98
+ return (stroke.position ?? "center") === "center";
99
+ }
100
+ /**
101
+ * Whether the open ends of the path are drawn with each stroke's dash ends
102
+ * instead of the path's own end points (`startCap`, `endCap`). The end points
103
+ * are used only on one open chain whose strokes are all centered (see
104
+ * `PathNode.startCap`). So this is true for a path with open ends that has
105
+ * several open parts, closed and open parts together, or a stroke that is
106
+ * inside or outside. A closed path has no open end to draw.
107
+ */
108
+ function drawsEndsWithDashEnds(network, strokes) {
109
+ if (openPathChain(network) !== null)
110
+ return !strokes.every(isCentered);
111
+ const uses = new Map();
112
+ for (const segment of network.segments) {
113
+ uses.set(segment.start, (uses.get(segment.start) ?? 0) + 1);
114
+ uses.set(segment.end, (uses.get(segment.end) ?? 0) + 1);
115
+ }
116
+ return [...uses.values()].some((count) => count === 1);
117
+ }
118
+ /** The dash ends a stroke names itself, if it does. */
119
+ function ownDashEnds(stroke) {
120
+ return typeof stroke === "object" && stroke !== null ? stroke.dashCap : undefined;
121
+ }
122
+ /**
123
+ * Builds a complete, editable path layer from one SVG `<path>`: its `d`
124
+ * attribute, its `fill-rule`, `stroke-linecap` and `stroke-linejoin`, and the
125
+ * matrix that places it in its parent. Relative, shorthand and quadratic
126
+ * commands are normalized and arcs stay arcs (see
127
+ * `vectorNetworkFromSvgPathData`). A path layer has no frame: its bounds come
128
+ * from the network under `transform` (`getNodeBounds`).
129
+ *
130
+ * Nothing is guessed. Each of these throws a `ModelError` with code
131
+ * `INVALID_SCHEMA` whose `path` names the option, such as
132
+ * `$svgPath.pathData`: path data that is not a string, is empty, cannot be
133
+ * read, draws no segment or holds a number that is not finite; a `transform`
134
+ * that is not six finite numbers, cannot be inverted (see
135
+ * `transformIsInvertible`) or puts the path beyond finite numbers; a `fillRule`,
136
+ * `strokeCap` or `strokeJoin` outside SVG's values; a `strokeJoin` for a style
137
+ * without strokes; a style that names a shared style (`styleId`) and no
138
+ * `strokes` of its own, whose strokes would be the shared style's; a stroke
139
+ * whose own `dashCap` differs from `strokeCap` on a path whose open ends are
140
+ * drawn with the dash ends (several open parts, or a stroke that is inside
141
+ * or outside), because one cap draws both there; and an option this function
142
+ * does not know.
143
+ *
144
+ * The rest of the layer (`id`, `name`, `style`, `opacity`) is checked when
145
+ * the layer enters a document, as for any other layer.
146
+ */
147
+ export function pathNodeFromSvgPath(svgPath) {
148
+ if (typeof svgPath !== "object" || svgPath === null || Array.isArray(svgPath)) {
149
+ refuse("Expected an object that describes an SVG path", ROOT);
150
+ }
151
+ for (const key of Object.keys(svgPath)) {
152
+ if (!OPTION_KEYS.has(key))
153
+ refuse("Unknown option", `${ROOT}.${key}`);
154
+ }
155
+ const fillRule = oneOf(svgPath.fillRule, SVG_FILL_RULES, "nonzero", `${ROOT}.fillRule`);
156
+ const svgCap = oneOf(svgPath.strokeCap, SVG_STROKE_CAPS, "butt", `${ROOT}.strokeCap`);
157
+ const cap = CAP_FROM_SVG[svgCap];
158
+ const join = oneOf(svgPath.strokeJoin, SVG_STROKE_JOINS, "miter", `${ROOT}.strokeJoin`);
159
+ const transform = importedTransform(svgPath.transform, `${ROOT}.transform`);
160
+ const vectorNetwork = importedNetwork(svgPath.pathData, fillRule);
161
+ if (typeof svgPath.style !== "object" || svgPath.style === null || Array.isArray(svgPath.style)) {
162
+ refuse("Expected the layer's style", `${ROOT}.style`);
163
+ }
164
+ const strokes = svgPath.style.strokes;
165
+ if (strokes !== undefined && !Array.isArray(strokes)) {
166
+ refuse("Expected a list of strokes", `${ROOT}.style.strokes`);
167
+ }
168
+ const sharedStyleId = svgPath.style.styleId;
169
+ if (sharedStyleId !== undefined && typeof sharedStyleId !== "string") {
170
+ refuse(`Expected a shared style ID but got ${shown(sharedStyleId)}`, `${ROOT}.style.styleId`);
171
+ }
172
+ if (strokes === undefined && sharedStyleId !== undefined) {
173
+ // The layer would draw the shared style's strokes, which this function
174
+ // cannot see or change: SVG's join and cap would not be on them.
175
+ refuse(`The strokes would come from the shared style ${JSON.stringify(sharedStyleId)}, which cannot take SVG's join and cap; `
176
+ + "give the strokes in style.strokes (an empty list for no stroke)", `${ROOT}.style.strokes`);
177
+ }
178
+ if (strokes !== undefined && drawsEndsWithDashEnds(vectorNetwork, strokes)) {
179
+ strokes.forEach((stroke, index) => {
180
+ const own = ownDashEnds(stroke);
181
+ if (own === undefined || own === cap)
182
+ return;
183
+ refuse("SVG has one line cap, and this path's open ends are drawn with each stroke's dash ends "
184
+ + "(it has several open parts, or a stroke that is inside or outside), "
185
+ + `so the dash ends ${shown(own)} cannot differ from the strokeCap ${JSON.stringify(svgCap)}`, `${ROOT}.style.strokes[${index}].dashCap`);
186
+ });
187
+ }
188
+ if (svgPath.strokeJoin !== undefined && (strokes === undefined || strokes.length === 0)) {
189
+ refuse("A stroke join needs a stroke in style.strokes to be stored on", `${ROOT}.strokeJoin`);
190
+ }
191
+ const node = {
192
+ id: svgPath.id,
193
+ name: svgPath.name,
194
+ visible: svgPath.visible ?? true,
195
+ locked: svgPath.locked ?? false,
196
+ opacity: svgPath.opacity ?? 1,
197
+ type: "shape",
198
+ shape: "path",
199
+ vectorNetwork,
200
+ transform,
201
+ style: strokes === undefined
202
+ ? svgPath.style
203
+ : { ...svgPath.style, strokes: strokes.map((stroke) => ({ join, ...dashEnds(stroke, cap), ...stroke })) },
204
+ startCap: cap,
205
+ endCap: cap,
206
+ };
207
+ // A large scale can carry finite path data past the largest number. The
208
+ // corners of the path's own box go farthest, so they are what is checked.
209
+ const box = getNodeBounds({ ...node, transform: [1, 0, 0, 1, 0, 0] });
210
+ const [a, b, c, d, e, f] = transform;
211
+ for (const x of [box.x, box.x + box.width]) {
212
+ for (const y of [box.y, box.y + box.height]) {
213
+ if (!Number.isFinite(a * x + c * y + e) || !Number.isFinite(b * x + d * y + f)) {
214
+ refuse("The transform puts the path where its coordinates are not finite numbers", `${ROOT}.transform`);
215
+ }
216
+ }
217
+ }
218
+ return node;
219
+ }
@@ -0,0 +1,142 @@
1
+ import type { AssetId, DesignDocument, Rect, TextParagraph, TextStyle, TextStyleOverrides, TextualNode } from "./types.ts";
2
+ export interface TextLineMetrics {
3
+ readonly width: number;
4
+ readonly ascent: number;
5
+ readonly descent: number;
6
+ readonly height: number;
7
+ }
8
+ export interface TextMeasurement {
9
+ readonly width: number;
10
+ readonly height: number;
11
+ readonly lineCount: number;
12
+ readonly lines: readonly TextLineMetrics[];
13
+ }
14
+ export interface TextFontRequest {
15
+ readonly font: string;
16
+ readonly text: string;
17
+ }
18
+ export interface TextListLayout {
19
+ readonly text: string;
20
+ readonly style: TextStyle;
21
+ readonly gap: number;
22
+ /** Additional body and marker inset contributed by the authored list level. */
23
+ readonly nestingOffset: number;
24
+ readonly left: number;
25
+ readonly right: number;
26
+ readonly firstLine: number;
27
+ readonly markerWidth: number;
28
+ readonly markerBoxWidth: number;
29
+ }
30
+ export declare const textMeasurementStats: {
31
+ layoutComputations: number;
32
+ fontRequestTextRebuilds: number;
33
+ resolvedStyleCopies: number;
34
+ };
35
+ export declare function resetTextMeasurementStats(): void;
36
+ export declare function resolveTextStyle(document: DesignDocument, styleId: string | undefined, overrides: TextStyleOverrides | undefined): TextStyle;
37
+ export declare function splitCssFontFamilies(fontFamily: string): readonly string[];
38
+ /** Returns the stable CSS family used for one document font asset identity. */
39
+ export declare function documentFontFamilyAlias(assetId: string): string;
40
+ /**
41
+ * Resolves embedded font identities into an asset-specific CSS family stack.
42
+ * The primary embedded face replaces the authored primary family, ordered
43
+ * embedded fallbacks follow it, and the authored CSS fallback tail remains
44
+ * last. Asset aliases keep same-family faces distinct and deterministic.
45
+ */
46
+ export declare function resolveTextFontFamily(document: DesignDocument, style: TextStyle): string;
47
+ export declare function textStyleToCanvasFont(style: TextStyle, document?: DesignDocument): string;
48
+ export declare function clearTextMeasurementCache(): void;
49
+ /**
50
+ * The style a list marker of the paragraph is drawn in. A marker takes the
51
+ * shared style and the overrides it does not name itself from the first run
52
+ * of its paragraph, each on its own: a marker with a style and no overrides
53
+ * still takes the first run's overrides. A marker the renderer writes itself
54
+ * (a bullet or a number) takes both. The page compiler, the canvas estimate
55
+ * and `textLayoutKey` all resolve markers here, so they cannot disagree.
56
+ */
57
+ export declare function resolveListMarkerStyle(document: DesignDocument, paragraph: Pick<TextParagraph, "runs" | "list">): TextStyle;
58
+ export declare function resolveTextListLayout(document: DesignDocument, paragraph: TextParagraph, paragraphIndex: number): TextListLayout | null;
59
+ /**
60
+ * The space above the paragraph at `index` of a text: its own `spacingBefore`
61
+ * where set, otherwise the text's `paragraphSpacing` for every paragraph but
62
+ * the first.
63
+ */
64
+ export declare function paragraphSpaceAbove(node: Pick<TextualNode, "paragraphSpacing">, paragraph: Pick<TextParagraph, "spacingBefore">, index: number): number;
65
+ export declare function measureTextNode(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): TextMeasurement;
66
+ export declare function getTextNodeBaselines(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): readonly number[];
67
+ /** One laid-out line of a text, in the text layer's own coordinates (the top-left of its box at 0, 0). */
68
+ export interface TextLineBox {
69
+ readonly x: number;
70
+ readonly y: number;
71
+ readonly width: number;
72
+ readonly height: number;
73
+ /** The y of the line's baseline. */
74
+ readonly baseline: number;
75
+ }
76
+ /**
77
+ * Where each line of a text sits in its box, from the same layout that gives
78
+ * `measureTextNode` and `getTextNodeBaselines`: each line moved down by the
79
+ * text's vertical alignment and across by its paragraph's alignment (a
80
+ * justified line spans the box only where the renderer stretches it: not the
81
+ * last line before a hard break or the paragraph's end, and not a line with no
82
+ * gap between words; in a right-to-left paragraph such a line keeps to the
83
+ * right). Each line is placed between its paragraph's left and
84
+ * right indents (the first-line indent on a paragraph's first line), as the
85
+ * renderer paints it; a list's first line also includes its marker. It only reads the layout; it
86
+ * draws nothing and changes no text.
87
+ */
88
+ export declare function getTextNodeLineBoxes(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): readonly TextLineBox[];
89
+ export declare function getTextNodeFontRequests(document: DesignDocument, node: TextualNode, paragraphs?: readonly TextParagraph[]): readonly TextFontRequest[];
90
+ /**
91
+ * The point of a text box that stays in place on the page when its content
92
+ * resizes it, as fractions of the frame's local width and height. As in Figma,
93
+ * the top edge stays put, and auto-width text keeps its alignment edge: the
94
+ * left edge for left-aligned or justified text, the center for centered text
95
+ * and the right edge for right-aligned text. When paragraphs disagree, the left
96
+ * edge stays put.
97
+ */
98
+ export declare function textAutoResizeAnchor(paragraphs: readonly TextParagraph[]): {
99
+ readonly x: number;
100
+ readonly y: number;
101
+ };
102
+ /**
103
+ * The frame of an auto-sized text box after its content changed its size.
104
+ * The box turns about its center, so keeping `frame.x` and `frame.y` would move
105
+ * rotated text across the page as it grows; this keeps the anchor from
106
+ * `textAutoResizeAnchor` fixed in page space instead.
107
+ */
108
+ export declare function autoResizedTextFrame(node: Pick<TextualNode, "frame" | "rotation">, paragraphs: readonly TextParagraph[], size: {
109
+ readonly width: number;
110
+ readonly height: number;
111
+ }): Rect;
112
+ /**
113
+ * `textLayoutKey`, and the IDs of the font assets the key names
114
+ * (`fontAssetFingerprint`), a missing one included: the key of the text
115
+ * changes only when the entry of one of these assets does, or the text or
116
+ * the theme.
117
+ */
118
+ export declare function textLayoutKeyAndFontAssets(document: DesignDocument, node: Pick<TextualNode, "autoResize" | "wrap" | "frame" | "paragraphs" | "paragraphSpacing">): {
119
+ readonly key: string;
120
+ readonly fontAssetIds: readonly AssetId[];
121
+ };
122
+ /**
123
+ * Names everything in the document that decides the size of an auto-sized
124
+ * text: its words, the styles they resolve to in this document (so a theme
125
+ * or variable mode that changes a style changes the key), the font file each
126
+ * of their font assets stands for, the paragraph settings, how the box sizes
127
+ * itself, and its width when only its height follows the text. A list marker
128
+ * is named by the style the renderer draws it in (`resolveListMarkerStyle`),
129
+ * which can come from the first run of its paragraph. Two texts
130
+ * of one document with the same key are laid out alike by the renderer (the
131
+ * order in which the fields of a style or a paragraph were written is not
132
+ * part of the key), as
133
+ * long as it draws with the same font files; the files that are not in the
134
+ * document (which parts of a Google Fonts family have arrived, the fallback
135
+ * fonts the renderer picks itself) are told apart by `setDrawnTextFontSet`.
136
+ * The drawn sizes are kept by this key
137
+ * (`recordDrawnTextSize`), and anything else that remembers or counts
138
+ * something about one layout of a text uses it too, so a change of font,
139
+ * size, wrap width or sizing mode is never taken for the layout before it.
140
+ */
141
+ export declare function textLayoutKey(document: DesignDocument, node: Pick<TextualNode, "autoResize" | "wrap" | "frame" | "paragraphs" | "paragraphSpacing">, paragraphs?: readonly TextParagraph[]): string;
142
+ export * from "./text-sizer.ts";