@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,618 @@
1
+ import { DEFAULT_STROKE_MITER_ANGLE, getNodeBounds, multiplyTransforms, } from "../../model/index.js";
2
+ import { attr, childElements, elementId, isSvgElement, urlReferenceId } from "../tree/elements.js";
3
+ import { initialComputedStyle } from "../style/computed.js";
4
+ import { parseSvgLength, parseSvgNumber, parseSvgNumberList } from "../units/length.js";
5
+ import { hexColor } from "../style/color.js";
6
+ import { applyMatrixToPoint, invertMatrix, negligibleNextTo, sameScale, parseTransform, remainderIsUniformScale, } from "../transform/matrix.js";
7
+ import { layerPlacement, pathLayerFromData } from "../paths/paths.js";
8
+ import { buildChildren, unionBoxes, wrapInGroup } from "./walk.js";
9
+ const FILTER_NOTE = { what: "Filters", kept: "The layer is kept without the filter" };
10
+ const CLIP_NOTE = { what: "Clip paths", kept: "The layer is kept without the clip" };
11
+ const MASK_NOTE = { what: "Masks", kept: "The layer is kept unmasked" };
12
+ /** A clip, mask or filter that lets nothing through: the layer stays, hidden. */
13
+ const HIDDEN_NOTE = { what: "Effects that hide everything", kept: "The layer is kept, hidden" };
14
+ const REGION_NOTE = { what: "Effect regions", kept: "The effect is applied without its region" };
15
+ /** A `url(#id)` value's id, "" for an external url, or null when the value is not a url. */
16
+ function urlId(value) {
17
+ if (value === null)
18
+ return null;
19
+ const id = urlReferenceId(value);
20
+ if (id !== null)
21
+ return id;
22
+ return value.trim().startsWith("url(") ? "" : null;
23
+ }
24
+ /** The attributes that give a filter primitive its own subregion (Filter Effects §8.5). */
25
+ const PRIMITIVE_REGION = ["x", "y", "width", "height"];
26
+ /** Numbers of one filter primitive attribute. */
27
+ function primitiveNumbers(element, name) {
28
+ const text = attr(element, name);
29
+ if (text === null)
30
+ return [];
31
+ return parseSvgNumberList(text) ?? [];
32
+ }
33
+ /**
34
+ * A `stdDeviation`: one value, or x and y separately. Null when the pair
35
+ * differs, when there are more than two values or when any is negative: the
36
+ * model has one blur value and refuses a negative one, so the caller leaves
37
+ * the filter out and reports it.
38
+ */
39
+ function stdDeviation(element, initial) {
40
+ const values = primitiveNumbers(element, "stdDeviation");
41
+ if (values.length > 2 || values.some((value) => value < 0))
42
+ return null;
43
+ if (values.length === 2 && values[0] !== values[1])
44
+ return null;
45
+ return values[0] ?? initial;
46
+ }
47
+ /**
48
+ * A flood color with its opacity folded in, as `#RRGGBBAA`, from the
49
+ * primitive's computed style: `flood-color` and `flood-opacity` are CSS
50
+ * properties, so a style sheet or inline style beats the attribute, and
51
+ * `currentColor` reads the primitive's `color`.
52
+ */
53
+ function floodColor(element, styles) {
54
+ const style = styles.get(element) ?? initialComputedStyle();
55
+ const rgba = { ...style.floodColor, a: style.floodColor.a * style.floodOpacity };
56
+ return hexColor(rgba);
57
+ }
58
+ /**
59
+ * Maps a `<filter>` to a drop shadow or a layer blur, exactly as the spec
60
+ * allows: `feDropShadow`, the `feGaussianBlur` + `feOffset` + `feFlood` +
61
+ * `feComposite` + `feMerge` chain exporters write for a drop shadow, or a
62
+ * lone `feGaussianBlur` on SourceGraphic. Anything else returns null and
63
+ * the caller reports it.
64
+ */
65
+ export function mapFilter(filterElement, styles) {
66
+ const primitives = childElements(filterElement);
67
+ if (primitives.length === 1) {
68
+ const first = primitives[0];
69
+ if (isSvgElement(first, "feDropShadow")) {
70
+ // feDropShadow's stdDeviation starts at 2 (Filter Effects §15.7).
71
+ const deviation = stdDeviation(first, 2);
72
+ if (deviation === null)
73
+ return null;
74
+ return {
75
+ shadows: [{
76
+ kind: "drop",
77
+ x: parseSvgNumber(attr(first, "dx") ?? "2") ?? 2,
78
+ y: parseSvgNumber(attr(first, "dy") ?? "2") ?? 2,
79
+ blur: deviation * 2,
80
+ spread: 0,
81
+ color: floodColor(first, styles),
82
+ }],
83
+ layerBlur: null,
84
+ };
85
+ }
86
+ if (isSvgElement(first, "feGaussianBlur")) {
87
+ const input = attr(first, "in") ?? "SourceGraphic";
88
+ if (input !== "SourceGraphic")
89
+ return null;
90
+ const deviation = stdDeviation(first, 0);
91
+ if (deviation === null)
92
+ return null;
93
+ return { shadows: [], layerBlur: deviation * 2 };
94
+ }
95
+ return null;
96
+ }
97
+ return mapShadowChain(primitives, styles);
98
+ }
99
+ /**
100
+ * The exporter drop-shadow chain, checked link by link: a blur of the
101
+ * source, an offset of that blur (named by its `result`), a flood, the flood
102
+ * composited `in` the offset, and a merge of that composite under
103
+ * SourceGraphic. An input names the latest earlier primitive with that
104
+ * `result`, or a standard input; a missing `in` is the previous primitive's
105
+ * output (SourceGraphic for the first). Any other wiring draws something
106
+ * else, so it returns null.
107
+ */
108
+ function mapShadowChain(primitives, styles) {
109
+ if (primitives.length !== 5)
110
+ return null;
111
+ const [blur, offset, flood, composite, merge] = primitives;
112
+ if (!isSvgElement(blur, "feGaussianBlur") || !isSvgElement(offset, "feOffset") || !isSvgElement(flood, "feFlood")
113
+ || !isSvgElement(composite, "feComposite") || !isSvgElement(merge, "feMerge")) {
114
+ return null;
115
+ }
116
+ // What an input refers to at primitive `index`: the index of the primitive it reads, or a standard input's name.
117
+ const source = (index, name) => {
118
+ if (name === null)
119
+ return index === 0 ? "SourceGraphic" : index - 1;
120
+ if (name === "SourceGraphic" || name === "SourceAlpha")
121
+ return name;
122
+ for (let earlier = index - 1; earlier >= 0; earlier -= 1) {
123
+ if (attr(primitives[earlier], "result") === name)
124
+ return earlier;
125
+ }
126
+ return null;
127
+ };
128
+ const blurInput = source(0, attr(blur, "in"));
129
+ // Composited `in` the flood, only the blur's alpha counts, so SourceGraphic and SourceAlpha draw the same.
130
+ if (blurInput !== "SourceAlpha" && blurInput !== "SourceGraphic")
131
+ return null;
132
+ if (source(1, attr(offset, "in")) !== 0)
133
+ return null;
134
+ if (source(3, attr(composite, "in")) !== 2)
135
+ return null;
136
+ const in2 = attr(composite, "in2");
137
+ if (in2 === null || source(3, in2) !== 1)
138
+ return null;
139
+ if ((attr(composite, "operator") ?? "over") !== "in")
140
+ return null;
141
+ const mergeNodes = childElements(merge);
142
+ if (mergeNodes.length !== 2)
143
+ return null;
144
+ if (!isSvgElement(mergeNodes[0], "feMergeNode") || !isSvgElement(mergeNodes[1], "feMergeNode")) {
145
+ return null;
146
+ }
147
+ if (source(4, attr(mergeNodes[0], "in")) !== 3)
148
+ return null;
149
+ if (source(4, attr(mergeNodes[1], "in")) !== "SourceGraphic")
150
+ return null;
151
+ const deviation = stdDeviation(blur, 0);
152
+ if (deviation === null)
153
+ return null;
154
+ return {
155
+ shadows: [{
156
+ kind: "drop",
157
+ x: parseSvgNumber(attr(offset, "dx") ?? "0") ?? 0,
158
+ y: parseSvgNumber(attr(offset, "dy") ?? "0") ?? 0,
159
+ blur: deviation * 2,
160
+ spread: 0,
161
+ color: floodColor(flood, styles),
162
+ }],
163
+ layerBlur: null,
164
+ };
165
+ }
166
+ /**
167
+ * Whether a built layer paints only white, for the all-white mask rule. Its
168
+ * shadows paint too, so they must be white as well; a blur keeps the layer's
169
+ * own colors.
170
+ */
171
+ function layerIsAllWhite(node, nodesById) {
172
+ // A container paints only through its visible layers (and, for a frame,
173
+ // its own style), so each of them must be white too.
174
+ if ("children" in node) {
175
+ const childrenWhite = node.children.every((id) => {
176
+ const child = nodesById.get(id);
177
+ return child !== undefined && (child.visible === false || layerIsAllWhite(child, nodesById));
178
+ });
179
+ if (!childrenWhite)
180
+ return false;
181
+ if (!("style" in node) || node.style === undefined)
182
+ return true;
183
+ }
184
+ if (!("style" in node) || node.style === undefined)
185
+ return false;
186
+ const style = node.style;
187
+ const paintWhite = (paint) => {
188
+ if (paint.kind === "solid")
189
+ return paint.color === "#FFFFFF";
190
+ if (paint.kind === "linear-gradient" || paint.kind === "radial-gradient") {
191
+ return (paint.stops ?? []).every((stop) => stop.color.startsWith("#FFFFFF"));
192
+ }
193
+ return false;
194
+ };
195
+ return (style.fills ?? []).every(paintWhite) &&
196
+ (style.strokes ?? []).every((stroke) => paintWhite(stroke.paint)) &&
197
+ (style.shadows ?? []).every((shadow) => shadow.color.startsWith("#FFFFFF"));
198
+ }
199
+ /**
200
+ * Forces every layer opaque and every styled layer black, as clip shapes
201
+ * paint for their geometry only: a container's opacity is cleared too.
202
+ */
203
+ function forceOpaque(nodes) {
204
+ return nodes.map((node) => {
205
+ if (!("style" in node) || node.style === undefined) {
206
+ return node.opacity === undefined || node.opacity === 1 ? node : { ...node, opacity: 1 };
207
+ }
208
+ return {
209
+ ...node,
210
+ opacity: 1,
211
+ style: { fills: [{ kind: "solid", color: "#000000" }] },
212
+ };
213
+ });
214
+ }
215
+ /** Builds the content of a clipPath or mask as layers in the referencing element's space. */
216
+ function buildDefinitionContent(definition, parentStyle, matrix, elementPath, ctx) {
217
+ const { children, nodes } = buildChildren(definition, parentStyle, matrix, elementPath, ctx);
218
+ return { children, nodes };
219
+ }
220
+ /**
221
+ * Wraps one built element in its clip and mask groups and merges its filter
222
+ * effects. The order matches SVG: the filter goes on the layer, then the
223
+ * clip group wraps it, then the mask group wraps that. The computed style
224
+ * carries the references already; one that does not start with `#` is
225
+ * external and is never fetched.
226
+ */
227
+ export function applyClipMaskFilter(element, computed, matrix, elementPath, built, ctx) {
228
+ if (built.top === null)
229
+ return built;
230
+ let top = built.top;
231
+ const nodes = [...built.nodes];
232
+ const placement = layerPlacement(matrix);
233
+ const effectScale = placement?.strokeScale ?? 1;
234
+ if (computed.filter !== null) {
235
+ if (computed.filter.external) {
236
+ ctx.report(elementPath, element.localName, "left-out", "the external filter is never fetched", FILTER_NOTE);
237
+ }
238
+ else {
239
+ const filterId = computed.filter.id;
240
+ const definition = ctx.ids.get(filterId);
241
+ if (definition === undefined) {
242
+ ctx.report(elementPath, element.localName, "left-out", `the filter #${filterId} is missing`, FILTER_NOTE);
243
+ }
244
+ else if (!isSvgElement(definition, "filter")) {
245
+ ctx.report(elementPath, element.localName, "left-out", `#${filterId} is not a filter`, FILTER_NOTE);
246
+ }
247
+ else if (attr(definition, "primitiveUnits")?.trim() === "objectBoundingBox") {
248
+ // In objectBoundingBox units the offsets and deviations are fractions of the layer's box, which the mapping does not convert.
249
+ ctx.report(elementPath, element.localName, "left-out", "a filter in objectBoundingBox primitiveUnits is not supported; the layer comes in without it", FILTER_NOTE);
250
+ }
251
+ else if (childElements(definition).some((primitive) => PRIMITIVE_REGION.some((name) => attr(primitive, name) !== null))) {
252
+ // A primitive subregion clips that primitive's output, which the model's effects cannot do.
253
+ ctx.report(elementPath, element.localName, "left-out", "a filter primitive with its own region (x, y, width or height) is not supported; the layer comes in without the filter", FILTER_NOTE);
254
+ }
255
+ else {
256
+ const effects = mapFilter(definition, ctx.styles);
257
+ if (effects === null) {
258
+ ctx.report(elementPath, element.localName, "left-out", "the filter is not a drop shadow or a plain blur", FILTER_NOTE);
259
+ }
260
+ else if (!effectsFollowScale(effects, matrix)) {
261
+ ctx.report(elementPath, element.localName, "left-out", "the filter's effect cannot follow the layer's non-uniform scale, skew or mirror, so the layer comes in without it", FILTER_NOTE);
262
+ }
263
+ else if ("style" in top) {
264
+ const styled = top;
265
+ const style = styled.style ?? {};
266
+ const shadows = effects.shadows.map((shadow) => ({
267
+ ...shadow,
268
+ x: shadow.x * effectScale,
269
+ y: shadow.y * effectScale,
270
+ blur: shadow.blur * effectScale,
271
+ }));
272
+ top = {
273
+ ...styled,
274
+ style: {
275
+ ...style,
276
+ ...(shadows.length > 0 ? { shadows: [...(style.shadows ?? []), ...shadows] } : {}),
277
+ ...(effects.layerBlur === null ? {} : { layerBlur: effects.layerBlur * effectScale }),
278
+ },
279
+ };
280
+ const index = nodes.findIndex((node) => node.id === top.id);
281
+ if (index >= 0)
282
+ nodes[index] = top;
283
+ const region = effectRegion(definition, "filter", element, matrix, getNodeBounds(top), elementPath, ctx);
284
+ if (region === "empty") {
285
+ // A filter region with no area disables the element's rendering (Filter Effects §6).
286
+ ctx.report(elementPath, element.localName, "left-out", "the filter region has no area, so nothing of the layer shows; it comes in hidden", HIDDEN_NOTE);
287
+ top = { ...top, visible: false };
288
+ if (index >= 0)
289
+ nodes[index] = top;
290
+ }
291
+ else if (region !== null && !filterRegionHoldsPaint(region, matrix, top)) {
292
+ const clipped = clipToRegion(element, matrix, elementPath, region, top, "Filter region", ctx);
293
+ if (clipped !== null) {
294
+ top = clipped.group;
295
+ mergeNodes(nodes, clipped.nodes);
296
+ }
297
+ }
298
+ }
299
+ else {
300
+ // A group (or any layer without a style) has no effects of its own.
301
+ const article = /^[aeiou]/.test(top.type) ? "an" : "a";
302
+ ctx.report(elementPath, element.localName, "left-out", `a filter on ${article} ${top.type} cannot map, so the ${top.type} comes in without it`, FILTER_NOTE);
303
+ }
304
+ }
305
+ }
306
+ }
307
+ if (computed.clipPath !== null) {
308
+ if (computed.clipPath.external) {
309
+ ctx.report(elementPath, element.localName, "left-out", "the external clip-path is never fetched", CLIP_NOTE);
310
+ }
311
+ else {
312
+ const wrapped = wrapClip(element, matrix, elementPath, computed.clipPath.id, top, ctx);
313
+ if (wrapped !== null) {
314
+ top = wrapped.group;
315
+ mergeNodes(nodes, wrapped.nodes);
316
+ }
317
+ }
318
+ }
319
+ if (computed.mask !== null) {
320
+ if (computed.mask.external) {
321
+ ctx.report(elementPath, element.localName, "left-out", "the external mask is never fetched", MASK_NOTE);
322
+ }
323
+ else {
324
+ const wrapped = wrapMask(element, matrix, elementPath, computed.mask.id, top, built.top, nodes, ctx);
325
+ if (wrapped !== null) {
326
+ top = wrapped.group;
327
+ mergeNodes(nodes, wrapped.nodes);
328
+ }
329
+ }
330
+ }
331
+ return { top, nodes };
332
+ }
333
+ /** Adds a wrap's nodes, replacing the earlier copy of a layer the wrap shifted rather than repeating its id. */
334
+ function mergeNodes(nodes, added) {
335
+ for (const node of added) {
336
+ const index = nodes.findIndex((entry) => entry.id === node.id);
337
+ if (index >= 0)
338
+ nodes[index] = node;
339
+ else
340
+ nodes.push(node);
341
+ }
342
+ }
343
+ /** Wraps one layer in a clip group: the clip shapes painted opaque black, marked as the mask. */
344
+ function wrapClip(element, matrix, elementPath, clipId, top, ctx) {
345
+ const definition = ctx.ids.get(clipId);
346
+ if (definition === undefined) {
347
+ ctx.report(elementPath, element.localName, "left-out", `the clip-path #${clipId} is missing`, CLIP_NOTE);
348
+ return null;
349
+ }
350
+ if (!isSvgElement(definition, "clipPath")) {
351
+ ctx.report(elementPath, element.localName, "left-out", `#${clipId} is not a clip-path`, CLIP_NOTE);
352
+ return null;
353
+ }
354
+ const units = attr(definition, "clipPathUnits") ?? "userSpaceOnUse";
355
+ if (units !== "userSpaceOnUse") {
356
+ ctx.report(elementPath, element.localName, "left-out", "a clip-path in objectBoundingBox units is not supported", CLIP_NOTE);
357
+ return null;
358
+ }
359
+ const definitionStyle = ctx.styles.get(definition);
360
+ if (definitionStyle === undefined)
361
+ return null;
362
+ // The clipPath's own transform applies inside the referencing element's,
363
+ // unless a winning CSS transform overrides the attribute.
364
+ let clipMatrix = matrix;
365
+ const transformText = definitionStyle.cssTransformWins ? null : attr(definition, "transform");
366
+ if (transformText !== null) {
367
+ const parsed = parseTransform(transformText);
368
+ if (parsed === null) {
369
+ // An invalid transform attribute is ignored, as a browser ignores it:
370
+ // the clip still applies, untransformed.
371
+ ctx.report(elementPath, element.localName, "left-out", `the transform "${transformText}" of the clip-path #${clipId} cannot be read, so it is ignored`, { what: "Transforms that cannot be read", kept: "Drawn without the transform" });
372
+ }
373
+ else {
374
+ clipMatrix = multiplyTransforms(matrix, parsed);
375
+ }
376
+ }
377
+ const content = buildDefinitionContent(definition, definitionStyle, clipMatrix, elementPath, { ...ctx, insideClipPath: true });
378
+ const forced = forceOpaque(content.nodes);
379
+ const forcedById = new Map(forced.map((node) => [node.id, node]));
380
+ const descendants = descendantsOf(forced, content.children);
381
+ const shapes = content.children
382
+ .map((node) => forcedById.get(node.id) ?? node)
383
+ .filter((node) => node.visible !== false);
384
+ if (shapes.length === 0) {
385
+ // An empty clipping path clips everything away (CSS Masking §6.1): the layer stays, hidden, and the report says why.
386
+ ctx.report(elementPath, element.localName, "left-out", "the clip-path has no shapes, so nothing of the layer shows; it comes in hidden", HIDDEN_NOTE);
387
+ const hidden = { ...top, visible: false };
388
+ return { group: hidden, nodes: [hidden] };
389
+ }
390
+ let maskLayer;
391
+ let maskSubtree = [];
392
+ if (shapes.length === 1) {
393
+ maskLayer = { ...shapes[0], mask: true };
394
+ maskSubtree = [maskLayer];
395
+ }
396
+ else {
397
+ const inner = wrapInGroup(element, shapes, "Clip shapes", ctx);
398
+ maskLayer = { ...inner.group, mask: true };
399
+ maskSubtree = [maskLayer, ...inner.shifted];
400
+ }
401
+ const wrapped = wrapInGroup(element, [maskLayer, top], elementId(element) ?? "Clip group", ctx);
402
+ return { group: wrapped.group, nodes: [wrapped.group, ...wrapped.shifted, ...maskSubtree.slice(1), ...descendants] };
403
+ }
404
+ /** Wraps one layer in a mask group when the mask is exact, per the spec's alpha rules. */
405
+ function wrapMask(element, matrix, elementPath, maskId, top, layer, layerNodes, ctx) {
406
+ const definition = ctx.ids.get(maskId);
407
+ if (definition === undefined) {
408
+ ctx.report(elementPath, element.localName, "left-out", `the mask #${maskId} is missing`, MASK_NOTE);
409
+ return null;
410
+ }
411
+ if (!isSvgElement(definition, "mask")) {
412
+ ctx.report(elementPath, element.localName, "left-out", `#${maskId} is not a mask`, MASK_NOTE);
413
+ return null;
414
+ }
415
+ const contentUnits = attr(definition, "maskContentUnits") ?? "userSpaceOnUse";
416
+ if (contentUnits !== "userSpaceOnUse") {
417
+ ctx.report(elementPath, element.localName, "left-out", "a mask in objectBoundingBox units is not supported; its content comes in unmasked", MASK_NOTE);
418
+ return null;
419
+ }
420
+ const definitionStyle = ctx.styles.get(definition);
421
+ if (definitionStyle === undefined)
422
+ return null;
423
+ const maskType = definitionStyle.maskType;
424
+ // A mask that lets nothing through hides the element: the layer stays, hidden, and the report says why.
425
+ const hide = (reason) => {
426
+ ctx.report(elementPath, element.localName, "left-out", `${reason}, so nothing of the layer shows; it comes in hidden`, HIDDEN_NOTE);
427
+ const hidden = { ...top, visible: false };
428
+ return { group: hidden, nodes: [hidden] };
429
+ };
430
+ const region = effectRegion(definition, "mask", element, matrix, getNodeBounds(layer), elementPath, ctx);
431
+ if (region === "empty")
432
+ return hide("the mask region has no area");
433
+ const content = buildDefinitionContent(definition, definitionStyle, matrix, elementPath, ctx);
434
+ const visible = content.children.filter((node) => node.visible !== false);
435
+ if (visible.length === 0)
436
+ return hide("the mask has no content");
437
+ const contentById = new Map(content.nodes.map((node) => [node.id, node]));
438
+ if (maskType !== "alpha" && !visible.every((node) => layerIsAllWhite(node, contentById))) {
439
+ ctx.report(elementPath, element.localName, "left-out", "the mask is not mask-type alpha and is not all white, so its luminance cannot map; its content comes in unmasked", MASK_NOTE);
440
+ return null;
441
+ }
442
+ const inner = wrapInGroup(element, visible, "Mask", ctx);
443
+ const maskLayer = { ...inner.group, mask: true };
444
+ const wrapped = wrapInGroup(element, [maskLayer, top], elementId(element) ?? "Mask group", ctx);
445
+ const descendants = descendantsOf(content.nodes, content.children);
446
+ const maskNodes = [...wrapped.shifted, ...inner.shifted, ...descendants];
447
+ if (region === null || regionHoldsPaint(region, matrix, visible, content.nodes, layer, layerNodes)) {
448
+ return { group: wrapped.group, nodes: [wrapped.group, ...maskNodes] };
449
+ }
450
+ const clipped = clipToRegion(element, matrix, elementPath, region, wrapped.group, "Mask region", ctx);
451
+ if (clipped === null)
452
+ return { group: wrapped.group, nodes: [wrapped.group, ...maskNodes] };
453
+ return { group: clipped.group, nodes: [...clipped.nodes, ...maskNodes] };
454
+ }
455
+ /**
456
+ * Wraps a layer in a clip group whose clip is the region, a rectangle in
457
+ * the element's user space. Null when the matrix collapses the region.
458
+ */
459
+ function clipToRegion(element, matrix, elementPath, region, layer, name, ctx) {
460
+ const placement = layerPlacement(matrix);
461
+ if (placement === null)
462
+ return null;
463
+ const regionPath = `M${String(region.x)} ${String(region.y)}H${String(region.x + region.width)}`
464
+ + `V${String(region.y + region.height)}H${String(region.x)}Z`;
465
+ const built = pathLayerFromData(ctx.idSource(), "Region", regionPath, initialComputedStyle(), placement, elementPath, element, ctx);
466
+ if (built === null)
467
+ return null;
468
+ const clipped = wrapInGroup(element, [{ ...built.node, mask: true }, layer], name, ctx);
469
+ return { group: clipped.group, nodes: [clipped.group, ...clipped.shifted] };
470
+ }
471
+ /**
472
+ * Whether a filter region surely holds everything the filtered layer paints,
473
+ * so clipping to it changes nothing: under a transform with no rotation or
474
+ * skew, the region contains the layer's bounds grown by its strokes' reach
475
+ * (the width times the miter limit, which covers joins and caps), each
476
+ * shadow's offset and spread, and each blur's reach. A Gaussian blur of
477
+ * deviation σ (a model blur of 2σ) is negligible past 3σ. Text and
478
+ * instances can paint past their bounds, so then the region is applied.
479
+ */
480
+ function filterRegionHoldsPaint(region, matrix, layer) {
481
+ if (!negligibleNextTo(matrix[1], matrix[0]) || !negligibleNextTo(matrix[2], matrix[3]))
482
+ return false;
483
+ if (layer.type === "text" || layer.type === "instance")
484
+ return false;
485
+ const style = ("style" in layer ? layer.style : undefined);
486
+ const strokeReach = Math.max(0, ...(style?.strokes ?? []).map((stroke) => {
487
+ const angle = (stroke.miterAngle ?? DEFAULT_STROKE_MITER_ANGLE) * Math.PI / 180;
488
+ return stroke.width * Math.max(1, 1 / Math.sin(angle / 2));
489
+ }));
490
+ const blurReach = (blur) => 1.5 * blur;
491
+ const bounds = getNodeBounds(layer);
492
+ const grown = (dx, dy, by) => ({
493
+ x: bounds.x + dx - by, y: bounds.y + dy - by, width: bounds.width + 2 * by, height: bounds.height + 2 * by,
494
+ });
495
+ const boxes = [
496
+ grown(0, 0, strokeReach + blurReach(style?.layerBlur ?? 0)),
497
+ ...(style?.shadows ?? []).map((shadow) => grown(shadow.x, shadow.y, strokeReach + Math.max(0, shadow.spread) + blurReach(shadow.blur))),
498
+ ];
499
+ const from = applyMatrixToPoint(matrix, region.x, region.y);
500
+ const to = applyMatrixToPoint(matrix, region.x + region.width, region.y + region.height);
501
+ const left = Math.min(from.x, to.x);
502
+ const top = Math.min(from.y, to.y);
503
+ const right = Math.max(from.x, to.x);
504
+ const bottom = Math.max(from.y, to.y);
505
+ return boxes.every((box) => box.x >= left && box.y >= top && box.x + box.width <= right && box.y + box.height <= bottom);
506
+ }
507
+ /**
508
+ * Whether the layer's effects follow the filter exactly under the baked
509
+ * remainder of its transform: a uniform positive scale scales offsets and
510
+ * blurs by one factor; a mirror with equal axis scales keeps a plain blur,
511
+ * which has no direction. Anything else changes the effect's shape.
512
+ */
513
+ function effectsFollowScale(effects, matrix) {
514
+ const placement = layerPlacement(matrix);
515
+ if (placement === null)
516
+ return true;
517
+ const parts = placement.parts;
518
+ if (remainderIsUniformScale(parts))
519
+ return true;
520
+ return effects.shadows.length === 0 && negligibleNextTo(parts.uy, parts.uz) && sameScale(Math.abs(parts.ux), Math.abs(parts.uz));
521
+ }
522
+ /**
523
+ * A mask's or filter's region in the element's user space (SVG 1.1 §14.4,
524
+ * Filter Effects §6): `maskUnits` or `filterUnits` (objectBoundingBox by
525
+ * default) with `x`, `y`, `width` and `height`, by default -10%, -10%, 120%
526
+ * and 120%. A bounding box region
527
+ * needs the layer's box in user space, which its bounds give exactly only
528
+ * when the transform has no rotation or skew; otherwise the region is
529
+ * reported and not applied. Null when the region is not applied; "empty"
530
+ * when it has no area, which per CSS Masking hides the element.
531
+ */
532
+ function effectRegion(definition, kind, element, matrix, layerBounds, elementPath, ctx) {
533
+ const userSpace = attr(definition, `${kind}Units`) === "userSpaceOnUse";
534
+ const fontSize = ctx.styles.get(definition)?.fontSize ?? initialComputedStyle().fontSize;
535
+ let box = null;
536
+ if (!userSpace) {
537
+ const inverse = invertMatrix(matrix);
538
+ if (inverse === null || !negligibleNextTo(matrix[1], matrix[0]) || !negligibleNextTo(matrix[2], matrix[3])) {
539
+ ctx.report(elementPath, element.localName, "approximated", `the ${kind} region (by default 10% around the layer) is not applied to a rotated or skewed layer`, REGION_NOTE);
540
+ return null;
541
+ }
542
+ const corners = [
543
+ applyMatrixToPoint(inverse, layerBounds.x, layerBounds.y),
544
+ applyMatrixToPoint(inverse, layerBounds.x + layerBounds.width, layerBounds.y + layerBounds.height),
545
+ ];
546
+ const xs = corners.map((point) => point.x);
547
+ const ys = corners.map((point) => point.y);
548
+ box = { x: Math.min(...xs), y: Math.min(...ys), width: Math.abs(xs[1] - xs[0]), height: Math.abs(ys[1] - ys[0]) };
549
+ }
550
+ const value = (name, fallback) => {
551
+ const text = attr(definition, name) ?? fallback;
552
+ let parsed = parseSvgLength(text, { fontSize, rootFontSize: ctx.rules.rootFontSize });
553
+ if (parsed === null) {
554
+ ctx.report(elementPath, element.localName, "approximated", `the ${kind}'s ${name} "${text}" cannot be read, so ${fallback} is used`, { what: "Effect regions that cannot be read", kept: "A default region is used" });
555
+ parsed = parseSvgLength(fallback, { fontSize, rootFontSize: ctx.rules.rootFontSize });
556
+ }
557
+ const horizontal = name === "x" || name === "width";
558
+ if (box !== null) {
559
+ const fraction = parsed.kind === "percent" ? parsed.fraction : parsed.value;
560
+ const origin = name === "x" ? box.x : name === "y" ? box.y : 0;
561
+ return origin + fraction * (horizontal ? box.width : box.height);
562
+ }
563
+ if (parsed.kind === "absolute")
564
+ return parsed.value;
565
+ if (ctx.viewport === null) {
566
+ ctx.report(elementPath, element.localName, "approximated", `the ${kind}'s ${name} "${text}" has no viewport to resolve against, so the ${kind} region is not applied`, REGION_NOTE);
567
+ return null;
568
+ }
569
+ return parsed.fraction * (horizontal ? ctx.viewport.width : ctx.viewport.height);
570
+ };
571
+ const x = value("x", "-10%");
572
+ const y = value("y", "-10%");
573
+ const width = value("width", "120%");
574
+ const height = value("height", "120%");
575
+ if (x === null || y === null || width === null || height === null)
576
+ return null;
577
+ if (width <= 0 || height <= 0)
578
+ return "empty";
579
+ return { x, y, width, height };
580
+ }
581
+ /**
582
+ * Whether a mask region surely holds everything the mask shows, so clipping
583
+ * to it changes nothing: under a transform with no rotation or skew, the
584
+ * region contains the mask content's or the layer's bounds, and those
585
+ * layers paint only fills (a stroke, an effect, text or an instance can
586
+ * paint past its bounds, so then the region is applied).
587
+ */
588
+ function regionHoldsPaint(region, matrix, maskLayers, maskNodes, layer, layerNodes) {
589
+ if (!negligibleNextTo(matrix[1], matrix[0]) || !negligibleNextTo(matrix[2], matrix[3]))
590
+ return false;
591
+ const from = applyMatrixToPoint(matrix, region.x, region.y);
592
+ const to = applyMatrixToPoint(matrix, region.x + region.width, region.y + region.height);
593
+ const left = Math.min(from.x, to.x);
594
+ const top = Math.min(from.y, to.y);
595
+ const right = Math.max(from.x, to.x);
596
+ const bottom = Math.max(from.y, to.y);
597
+ const holds = (box) => box !== null
598
+ && box.x >= left && box.y >= top && box.x + box.width <= right && box.y + box.height <= bottom;
599
+ const fillsOnly = (nodes) => nodes.every((node) => {
600
+ if (node.type === "text" || node.type === "instance")
601
+ return false;
602
+ if (!("style" in node) || node.style === undefined)
603
+ return true;
604
+ const style = node.style;
605
+ return (style.strokes?.length ?? 0) === 0 && (style.shadows?.length ?? 0) === 0 && (style.layerBlur ?? 0) === 0;
606
+ });
607
+ if (fillsOnly(maskNodes) && holds(unionBoxes(maskLayers.map((node) => getNodeBounds(node)))))
608
+ return true;
609
+ return fillsOnly(layerNodes) && holds(getNodeBounds(layer));
610
+ }
611
+ /**
612
+ * The nodes built from a clip-path or mask below its top-level layers: the
613
+ * layers inside its groups, which the wrapped group names through them.
614
+ */
615
+ function descendantsOf(nodes, children) {
616
+ const topIds = new Set(children.map((node) => node.id));
617
+ return nodes.filter((node) => !topIds.has(node.id));
618
+ }
@@ -0,0 +1,24 @@
1
+ import type { SvgElementLike } from "../public-types.ts";
2
+ import type { ComputedStyle } from "../style/computed.ts";
3
+ import { type Matrix } from "../transform/matrix.ts";
4
+ import type { BuiltElement, WalkContext } from "./walk.ts";
5
+ /** A decoded data URI: its mime type and bytes. */
6
+ export interface DecodedDataUri {
7
+ readonly mimeType: string;
8
+ readonly data: Uint8Array;
9
+ }
10
+ /** Whether a URI is a `data:` URL; the scheme is ASCII case-insensitive. */
11
+ export declare function isDataUri(uri: string): boolean;
12
+ /** The lower-case mime type of a `data:` URL, or null for anything else. */
13
+ export declare function dataUriMimeType(uri: string): string | null;
14
+ /** Decodes a `data:` URI: base64 or percent-encoded. Returns null for anything else. */
15
+ export declare function decodeDataUri(uri: string): DecodedDataUri | null;
16
+ /** Decodes a text data URI (for SVG images). */
17
+ export declare function decodeTextDataUri(uri: string): string | null;
18
+ /** The intrinsic size of a raster image, read from its header, or null. */
19
+ export declare function intrinsicSize(mimeType: string, data: Uint8Array): {
20
+ readonly width: number;
21
+ readonly height: number;
22
+ } | null;
23
+ /** Builds the layer of one `<image>` element. */
24
+ export declare function imageLayer(element: SvgElementLike, computed: ComputedStyle, matrix: Matrix, elementPath: string, ctx: WalkContext): BuiltElement;