@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,429 @@
1
+ import { multiplyTransforms, } from "../../model/index.js";
2
+ import { attr, childElements, hrefValue, isSvgElement, } from "../tree/elements.js";
3
+ import { applyMatrixToPoint, applyMatrixToVector, invertMatrix, matrixIsDegenerate, parseTransform, } from "../transform/matrix.js";
4
+ import { parseSvgLength, parseSvgNumber } from "../units/length.js";
5
+ import { IterationGuard, ITERATION_LIMIT } from "../limits/limits.js";
6
+ import { hexColor } from "../style/color.js";
7
+ const GRADIENT_ATTRIBUTES = [
8
+ "x1", "y1", "x2", "y2", "cx", "cy", "r", "fx", "fy", "fr",
9
+ "gradientUnits", "spreadMethod", "gradientTransform",
10
+ ];
11
+ /**
12
+ * Follows a gradient's `href` chain: attributes come from the nearest
13
+ * gradient in the chain that sets them, stops from the nearest that has
14
+ * `<stop>` children. A cycle, a dangling href or an href to something that
15
+ * is not a gradient ends the chain with what was found; the caller's report covers any real loss.
16
+ * A link to another file also ends it, and is reported, as it is never fetched.
17
+ */
18
+ function resolveGradient(id, ctx) {
19
+ const guard = new IterationGuard("gradient href resolution", ITERATION_LIMIT);
20
+ const attributes = {};
21
+ let stops = null;
22
+ let kind = null;
23
+ let current = ctx.ids.get(id);
24
+ if (current === undefined)
25
+ return { kind: "missing" };
26
+ const seen = new Set();
27
+ let first = null;
28
+ while (current !== undefined) {
29
+ guard.step();
30
+ ctx.limits.countReferenceStep();
31
+ if (isSvgElement(current, "linearGradient")) {
32
+ kind ??= "linear";
33
+ }
34
+ else if (isSvgElement(current, "radialGradient")) {
35
+ kind ??= "radial";
36
+ }
37
+ else if (first !== null) {
38
+ // A template link to anything but a gradient is invalid, so the
39
+ // gradient keeps what the chain gave so far (SVG 2 §14.3.4).
40
+ break;
41
+ }
42
+ else if (isSvgElement(current, "pattern")) {
43
+ return { kind: "pattern" };
44
+ }
45
+ else {
46
+ return { kind: "not-paint-server", localName: current.localName };
47
+ }
48
+ first ??= current;
49
+ const currentId = attr(current, "id");
50
+ if (currentId !== null) {
51
+ if (seen.has(currentId))
52
+ break;
53
+ seen.add(currentId);
54
+ }
55
+ for (const name of GRADIENT_ATTRIBUTES) {
56
+ if (attributes[name] !== undefined)
57
+ continue;
58
+ const value = attr(current, name);
59
+ if (value !== null)
60
+ attributes[name] = value;
61
+ }
62
+ if (stops === null) {
63
+ const stopElements = childElements(current).filter((child) => isSvgElement(child, "stop"));
64
+ if (stopElements.length > 0)
65
+ stops = readStops(stopElements, ctx);
66
+ }
67
+ const href = hrefValue(current);
68
+ if (href === null)
69
+ break;
70
+ if (!href.startsWith("#")) {
71
+ // Opening never fetches, so a template in another file is left out;
72
+ // a browser could fetch it and draw this gradient differently.
73
+ ctx.report(current.localName, `the template gradient "${href}" is in another file, which is never fetched; only this file's gradient attributes are used`, { what: "Gradients based on another file", kept: "Only this file's gradient settings are used" });
74
+ break;
75
+ }
76
+ current = ctx.ids.get(href.slice(1));
77
+ }
78
+ if (kind === null || first === null)
79
+ return { kind: "missing" };
80
+ if (stops === null || stops.length === 0)
81
+ return null;
82
+ return { element: first, kind, attributes, stops };
83
+ }
84
+ /** Reads `<stop>` elements into model stops, folding `stop-opacity` into the color's alpha. */
85
+ function readStops(stopElements, ctx) {
86
+ const stops = [];
87
+ let previous = 0;
88
+ for (const stop of stopElements) {
89
+ const offsetText = attr(stop, "offset") ?? "0";
90
+ let offset;
91
+ if (offsetText.endsWith("%")) {
92
+ // The "%" follows the number directly: "50 %" is not a percentage.
93
+ const number = offsetText.slice(0, -1);
94
+ offset = number === number.trimEnd() ? (parseSvgNumber(number) ?? Number.NaN) / 100 : Number.NaN;
95
+ }
96
+ else {
97
+ offset = parseSvgNumber(offsetText) ?? Number.NaN;
98
+ }
99
+ if (!Number.isFinite(offset)) {
100
+ // An offset that cannot be read takes its initial value, 0.
101
+ ctx.report("stop", `the stop offset "${offsetText}" cannot be read, so 0 is used`, { what: "Gradient stops that cannot be read", kept: "The stop is placed at the start" });
102
+ offset = 0;
103
+ }
104
+ offset = Math.min(1, Math.max(0, offset));
105
+ previous = Math.max(previous, offset);
106
+ // A stop inherits through its own ancestors (the gradient, defs, the
107
+ // root), never through the element that references the gradient.
108
+ const style = ctx.styles.get(stop);
109
+ if (style === undefined)
110
+ throw new Error("a <stop> has no computed style from the pre-pass");
111
+ const color = { ...style.stopColor, a: style.stopColor.a * style.stopOpacity };
112
+ stops.push({ offset: previous, color: hexColor(color) });
113
+ }
114
+ return stops;
115
+ }
116
+ /**
117
+ * The viewport length a user-space percentage is a fraction of: its width,
118
+ * its height, or for a radius its normalized diagonal, sqrt((w² + h²) / 2).
119
+ */
120
+ function viewportLength(viewport, axis) {
121
+ if (axis === "x")
122
+ return viewport.width;
123
+ if (axis === "y")
124
+ return viewport.height;
125
+ return Math.sqrt((viewport.width ** 2 + viewport.height ** 2) / 2);
126
+ }
127
+ /**
128
+ * A number that may be a fraction (`0.5`), a percentage (`50%`) or a user
129
+ * length (`24`, `1in`). Callers pass a missing attribute as its SVG default,
130
+ * which is a percentage, so in user space it resolves against the viewport.
131
+ * For `objectBoundingBox` a length with units is read into user units and
132
+ * the number is the fraction of the bounding box, as Chromium draws it:
133
+ * `x1="1em"` at a 100px font is `x1="100"`.
134
+ */
135
+ function gradientNumber(text, fallback, unitIsFraction, viewport, axis, fonts) {
136
+ const trimmed = text.trim();
137
+ if (trimmed.endsWith("%")) {
138
+ const number = parseSvgNumber(trimmed.slice(0, -1));
139
+ if (number === null)
140
+ return fallback;
141
+ const fraction = number / 100;
142
+ if (unitIsFraction)
143
+ return fraction;
144
+ if (viewport === null)
145
+ return fallback;
146
+ return fraction * viewportLength(viewport, axis);
147
+ }
148
+ if (unitIsFraction) {
149
+ const length = parseSvgLength(trimmed, fonts);
150
+ if (length === null)
151
+ return fallback;
152
+ return length.kind === "absolute" ? length.value : length.fraction;
153
+ }
154
+ const length = parseSvgLength(trimmed, fonts);
155
+ if (length === null)
156
+ return fallback;
157
+ if (length.kind === "absolute")
158
+ return length.value;
159
+ if (viewport === null)
160
+ return fallback;
161
+ return length.fraction * viewportLength(viewport, axis);
162
+ }
163
+ /**
164
+ * The font sizes for a gradient's coordinates. Chromium reads `em` and `ex`
165
+ * against the computed font size of the gradient the paint names, also for
166
+ * a coordinate taken from a template gradient; the painted element's font
167
+ * size does not count.
168
+ */
169
+ function gradientFonts(gradient, ctx) {
170
+ const style = ctx.styles.get(gradient.element);
171
+ if (style === undefined)
172
+ throw new Error("a gradient has no computed style from the pre-pass");
173
+ return { fontSize: style.fontSize, rootFontSize: ctx.rootFontSize };
174
+ }
175
+ /**
176
+ * Resolves a `url(#id)` paint reference to a model paint.
177
+ * `localFromElement` maps the referencing element's local user space into
178
+ * the layer's local geometry space, and `localBounds` is the layer's own
179
+ * bounding box in that space (the box the model's gradient coordinates are
180
+ * fractions of). Approximations are reported through `ctx.report`.
181
+ */
182
+ export function resolvePaintServer(id, localFromElement, localBounds, ctx) {
183
+ const resolved = resolveGradient(id, ctx);
184
+ if (resolved === null)
185
+ return null;
186
+ switch (resolved.kind) {
187
+ case "linear":
188
+ case "radial":
189
+ break;
190
+ default:
191
+ return resolved;
192
+ }
193
+ const gradientTransform = resolved.attributes.gradientTransform === undefined
194
+ ? null
195
+ : parseTransform(resolved.attributes.gradientTransform);
196
+ const userSpace = resolved.attributes.gradientUnits === "userSpaceOnUse";
197
+ // An objectBoundingBox gradient on a bounding box with no width or height
198
+ // paints nothing, and the reference counts as invalid. The box is measured
199
+ // in the element's user space: a layer can be flat where the element's box
200
+ // is not, as a diagonal line's box has width and height.
201
+ if (!userSpace && boxIsFlat(localFromElement, localBounds)) {
202
+ return { kind: "paints-nothing" };
203
+ }
204
+ const spread = resolved.attributes.spreadMethod ?? "pad";
205
+ if (spread === "reflect" || spread === "repeat") {
206
+ ctx.report(resolved.element.localName, `spreadMethod "${spread}" is drawn as pad`, { what: "Repeating or mirrored gradients", kept: "The end colors extend instead" });
207
+ }
208
+ // SVG paints a gradient with one stop as that stop's color; the model
209
+ // needs at least two stops.
210
+ if (resolved.stops.length === 1)
211
+ return lastStopSolid(resolved);
212
+ if (resolved.kind === "linear") {
213
+ return linearPaint(resolved, gradientTransform, userSpace, localFromElement, localBounds, ctx);
214
+ }
215
+ return radialPaint(resolved, gradientTransform, userSpace, localFromElement, localBounds, ctx);
216
+ }
217
+ /**
218
+ * Whether the element's bounding box — the layer's box measured in the
219
+ * element's user space — has no width or height, so an objectBoundingBox
220
+ * gradient paints nothing on it.
221
+ */
222
+ function boxIsFlat(localFromElement, localBounds) {
223
+ if (localBounds.width >= 1e-9 && localBounds.height >= 1e-9)
224
+ return false;
225
+ const inverse = invertMatrix(localFromElement);
226
+ if (inverse === null)
227
+ return true;
228
+ const corners = [
229
+ applyMatrixToPoint(inverse, localBounds.x, localBounds.y),
230
+ applyMatrixToPoint(inverse, localBounds.x + localBounds.width, localBounds.y),
231
+ applyMatrixToPoint(inverse, localBounds.x, localBounds.y + localBounds.height),
232
+ applyMatrixToPoint(inverse, localBounds.x + localBounds.width, localBounds.y + localBounds.height),
233
+ ];
234
+ const xs = corners.map((corner) => corner.x);
235
+ const ys = corners.map((corner) => corner.y);
236
+ return Math.max(...xs) - Math.min(...xs) < 1e-9 || Math.max(...ys) - Math.min(...ys) < 1e-9;
237
+ }
238
+ /**
239
+ * The map from gradient-space points to the layer's local space. For
240
+ * `objectBoundingBox` the gradient space is the bounds' unit square; for
241
+ * `userSpaceOnUse` it is the element's user space. `gradientTransform`
242
+ * composes inside the gradient space in both cases.
243
+ */
244
+ function gradientToLocal(gradientTransform, userSpace, localFromElement, localBounds) {
245
+ const bboxMap = [
246
+ localBounds.width, 0, 0, localBounds.height, localBounds.x, localBounds.y,
247
+ ];
248
+ const base = userSpace ? localFromElement : bboxMap;
249
+ return gradientTransform === null ? base : multiplyTransforms(base, gradientTransform);
250
+ }
251
+ function linearPaint(gradient, gradientTransform, userSpace, localFromElement, localBounds, ctx) {
252
+ const viewport = ctx.viewport;
253
+ const unitIsFraction = !userSpace;
254
+ const fonts = gradientFonts(gradient, ctx);
255
+ const x1 = gradientNumber(gradient.attributes.x1 ?? "0%", 0, unitIsFraction, viewport, "x", fonts);
256
+ const y1 = gradientNumber(gradient.attributes.y1 ?? "0%", 0, unitIsFraction, viewport, "y", fonts);
257
+ const x2 = gradientNumber(gradient.attributes.x2 ?? "100%", 1, unitIsFraction, viewport, "x", fonts);
258
+ const y2 = gradientNumber(gradient.attributes.y2 ?? "0%", 0, unitIsFraction, viewport, "y", fonts);
259
+ const map = gradientToLocal(gradientTransform, userSpace, localFromElement, localBounds);
260
+ const p1 = applyMatrixToPoint(map, x1, y1);
261
+ const p2 = applyMatrixToPoint(map, x2, y2);
262
+ const dx = p2.x - p1.x;
263
+ const dy = p2.y - p1.y;
264
+ const length = Math.hypot(dx, dy);
265
+ if (length < 1e-9 || !Number.isFinite(length))
266
+ return lastStopSolid(gradient);
267
+ // The color changes along the gradient's normal, which the map carries by
268
+ // its inverse transpose (exact for a scale or a skew, where the end points
269
+ // alone would tilt the lines of equal color). Normalize before applying
270
+ // the map to avoid squaring a very long axis.
271
+ const nx = x2 - x1;
272
+ const ny = y2 - y1;
273
+ const sourceLength = Math.hypot(nx, ny);
274
+ const ux = nx / sourceLength, uy = ny / sourceLength;
275
+ const [a, b, c, d] = map;
276
+ const determinant = a * d - b * c;
277
+ const normal = matrixIsDegenerate(map)
278
+ ? { x: dx / length / length, y: dy / length / length }
279
+ : {
280
+ x: (d * ux - b * uy) / determinant / sourceLength,
281
+ y: (a * uy - c * ux) / determinant / sourceLength,
282
+ };
283
+ const perUnit = Math.hypot(normal.x, normal.y);
284
+ if (!perUnit || !Number.isFinite(perUnit))
285
+ return lastStopSolid(gradient);
286
+ const lineLength = Math.abs(localBounds.width * (normal.x / perUnit))
287
+ + Math.abs(localBounds.height * (normal.y / perUnit));
288
+ const centerX = localBounds.x + localBounds.width / 2;
289
+ const centerY = localBounds.y + localBounds.height / 2;
290
+ if (lineLength < 1e-9) {
291
+ // A flat shape perpendicular to the gradient sits on one line of equal
292
+ // color: sample the gradient at the shape's position, padded past the
293
+ // ends, as a browser paints it (a zero-length gradient, above, is the
294
+ // only case that takes the last stop).
295
+ const t = (centerX - p1.x) * normal.x + (centerY - p1.y) * normal.y;
296
+ const sampled = sampleStops(gradient.stops, t);
297
+ return sampled === null ? null : { kind: "solid", color: sampled.slice(0, 7), opacity: alphaOf(sampled) };
298
+ }
299
+ // Restrict the offset function t = normal · (point - p1) to the box.
300
+ // A flat box loses one component of its slope but keeps its offset at the origin.
301
+ const gx = localBounds.width ? normal.x : 0;
302
+ const gy = localBounds.height ? normal.y : 0;
303
+ const slope = Math.hypot(gx, gy);
304
+ const spanX = localBounds.width ? (gx / slope) / localBounds.width / slope : 0;
305
+ const spanY = localBounds.height ? (gy / slope) / localBounds.height / slope : 0;
306
+ const offset = (localBounds.width ? 0 : (localBounds.x - p1.x) * normal.x)
307
+ + (localBounds.height ? 0 : (localBounds.y - p1.y) * normal.y);
308
+ const from = {
309
+ x: localBounds.width ? (p1.x - localBounds.x) / localBounds.width - offset * spanX : 0,
310
+ y: localBounds.height ? (p1.y - localBounds.y) / localBounds.height - offset * spanY : 0,
311
+ };
312
+ const to = { x: from.x + spanX, y: from.y + spanY };
313
+ if (![from.x, from.y, to.x, to.y].every(Number.isFinite))
314
+ return lastStopSolid(gradient);
315
+ const paint = { kind: "linear-gradient", from, to, stops: gradient.stops };
316
+ return { kind: "gradient", paint };
317
+ }
318
+ /**
319
+ * The color a gradient paints at offset `t`: padded to the first or last
320
+ * stop outside the stops' span, interpolated between the neighbouring stops
321
+ * inside it. Offsets must be in non-decreasing order, as `readStops` leaves
322
+ * them. Null only for a gradient with no stops.
323
+ */
324
+ function sampleStops(stops, t) {
325
+ const first = stops[0];
326
+ const last = stops[stops.length - 1];
327
+ if (first === undefined || last === undefined)
328
+ return null;
329
+ if (t <= first.offset)
330
+ return first.color;
331
+ if (t >= last.offset)
332
+ return last.color;
333
+ for (let i = 0; i + 1 < stops.length; i++) {
334
+ const a = stops[i];
335
+ const b = stops[i + 1];
336
+ if (t >= a.offset && t <= b.offset && b.offset > a.offset) {
337
+ return mixColors(a.color, b.color, (t - a.offset) / (b.offset - a.offset));
338
+ }
339
+ }
340
+ return last.color;
341
+ }
342
+ /** Mixes two `#RRGGBB[AA]` colors channel by channel, `t` of the way from `a` to `b`. */
343
+ function mixColors(a, b, t) {
344
+ const channels = (color) => [1, 3, 5, 7].map((at) => at === 7 && color.length !== 9 ? 255 : Number.parseInt(color.slice(at, at + 2), 16));
345
+ const from = channels(a);
346
+ const to = channels(b);
347
+ const mix = (i) => from[i] + (to[i] - from[i]) * t;
348
+ return hexColor({ r: mix(0), g: mix(1), b: mix(2), a: mix(3) / 255 });
349
+ }
350
+ /** The alpha of a `#RRGGBBAA` stop color as a separate opacity, undefined for opaque. */
351
+ function alphaOf(color) {
352
+ if (color.length !== 9)
353
+ return undefined;
354
+ const alpha = Number.parseInt(color.slice(7), 16) / 255;
355
+ return alpha >= 1 ? undefined : alpha;
356
+ }
357
+ /** The solid paint a gradient draws when it paints one color: its last stop's. */
358
+ function lastStopSolid(gradient) {
359
+ const lastStop = gradient.stops[gradient.stops.length - 1];
360
+ return lastStop === undefined
361
+ ? null
362
+ : { kind: "solid", color: lastStop.color.slice(0, 7), opacity: alphaOf(lastStop.color) };
363
+ }
364
+ function radialPaint(gradient, gradientTransform, userSpace, localFromElement, localBounds, ctx) {
365
+ const viewport = ctx.viewport;
366
+ const unitIsFraction = !userSpace;
367
+ const fonts = gradientFonts(gradient, ctx);
368
+ const cx = gradientNumber(gradient.attributes.cx ?? "50%", 0.5, unitIsFraction, viewport, "x", fonts);
369
+ const cy = gradientNumber(gradient.attributes.cy ?? "50%", 0.5, unitIsFraction, viewport, "y", fonts);
370
+ let r = gradientNumber(gradient.attributes.r ?? "50%", 0.5, unitIsFraction, viewport, "diagonal", fonts);
371
+ if (r < 0) {
372
+ // SVG makes a negative radius an error; browsers use the initial 50%.
373
+ ctx.report(gradient.element.localName, `the radial gradient radius "${gradient.attributes.r?.trim() ?? ""}" is negative, so 50% is used`, { what: "Negative gradient radii", kept: "A radius of 50% is used" });
374
+ r = gradientNumber("50%", 0.5, unitIsFraction, viewport, "diagonal", fonts);
375
+ }
376
+ const fx = gradient.attributes.fx === undefined
377
+ ? cx
378
+ : gradientNumber(gradient.attributes.fx, cx, unitIsFraction, viewport, "x", fonts);
379
+ const fy = gradient.attributes.fy === undefined
380
+ ? cy
381
+ : gradientNumber(gradient.attributes.fy, cy, unitIsFraction, viewport, "y", fonts);
382
+ const fr = gradientNumber(gradient.attributes.fr ?? "0%", 0, unitIsFraction, viewport, "diagonal", fonts);
383
+ const map = gradientToLocal(gradientTransform, userSpace, localFromElement, localBounds);
384
+ const center = applyMatrixToPoint(map, cx, cy);
385
+ const focal = applyMatrixToPoint(map, fx, fy);
386
+ if (Math.hypot(focal.x - center.x, focal.y - center.y) > 1e-6 || fr > 1e-9) {
387
+ ctx.report(gradient.element.localName, "the radial gradient's focal point is dropped; it draws centered", { what: "Radial gradient focal points", kept: "The gradient is drawn centered" });
388
+ }
389
+ // A bbox gradient's radius r spans r of the width and r of the height; a
390
+ // user-space gradient's radius spans r units on both axes. The mapped
391
+ // radial is exact while the radius vectors stay a circle: orthogonal and
392
+ // equal length (rotation is harmless). Anything else draws axis-aligned
393
+ // and is reported.
394
+ const radiusX = applyMatrixToVector(map, userSpace ? r : r, 0);
395
+ const radiusY = applyMatrixToVector(map, 0, userSpace ? r : r);
396
+ const lengthX = Math.hypot(radiusX.dx, radiusX.dy);
397
+ const lengthY = Math.hypot(radiusY.dx, radiusY.dy);
398
+ const dot = radiusX.dx * radiusY.dx + radiusX.dy * radiusY.dy;
399
+ const approximated = Math.abs(dot) > 1e-9 * lengthX * lengthY
400
+ || Math.abs(lengthX - lengthY) > 1e-9 * Math.max(lengthX, lengthY);
401
+ if (approximated) {
402
+ ctx.report(gradient.element.localName, "the radial gradient is skewed or non-uniformly scaled; it draws as an axis-aligned ellipse", { what: "Skewed radial gradients", kept: "Drawn as an upright ellipse" });
403
+ }
404
+ // SVG paints a zero radius as the last stop's color; the model needs a
405
+ // positive radius.
406
+ if (lengthX < 1e-9 || lengthY < 1e-9)
407
+ return lastStopSolid(gradient);
408
+ // A browser paints a user-space radial gradient on a flat layer (a line's
409
+ // stroke, say), but the model's center and radius are fractions of the
410
+ // layer's box, so a box without width or height cannot place it. Dropping
411
+ // the paint is a real loss, so it is reported as itself, not mistaken for
412
+ // an empty gradient.
413
+ if (localBounds.width < 1e-9 || localBounds.height < 1e-9) {
414
+ return { kind: "unsupported", reason: "a radial gradient cannot be placed on a zero-width or zero-height box" };
415
+ }
416
+ const paint = {
417
+ kind: "radial-gradient",
418
+ center: {
419
+ x: (center.x - localBounds.x) / localBounds.width,
420
+ y: (center.y - localBounds.y) / localBounds.height,
421
+ },
422
+ radius: {
423
+ x: lengthX / localBounds.width,
424
+ y: lengthY / localBounds.height,
425
+ },
426
+ stops: gradient.stops,
427
+ };
428
+ return { kind: "gradient", paint };
429
+ }
@@ -0,0 +1,32 @@
1
+ import { type Rect, type Stroke, type VisualStyle } from "../../model/index.ts";
2
+ import type { SvgElementLike } from "../public-types.ts";
3
+ import type { Matrix } from "../transform/matrix.ts";
4
+ import type { ComputedStyle } from "../style/computed.ts";
5
+ import type { StyleRules, StyleViewport } from "../style/computed.ts";
6
+ import type { ReportNote } from "../report/report.ts";
7
+ /** Whether a stroke came from a non-scaling-stroke element. */
8
+ export declare function isNonScalingStroke(stroke: Stroke): boolean;
9
+ export interface PaintContext {
10
+ readonly ids: ReadonlyMap<string, SvgElementLike>;
11
+ /** The import-wide work caps that following references, reading path data and style work count against. */
12
+ readonly limits: {
13
+ readonly countReferenceStep: () => void;
14
+ readonly countPathNumbers: (count: number) => void;
15
+ readonly countStyleWork: (units: number) => void;
16
+ };
17
+ readonly rules: StyleRules;
18
+ /** Every element's computed style at its own place in the tree, from the pre-pass. */
19
+ readonly styles: ReadonlyMap<SvgElementLike, ComputedStyle>;
20
+ readonly viewport: StyleViewport | null;
21
+ readonly report: (elementPath: string, element: string, kind: "left-out" | "approximated", reason: string, note: ReportNote) => void;
22
+ }
23
+ /**
24
+ * Builds the model style of one layer from its computed style: solid fills
25
+ * and strokes, gradients, opacity, dashes, caps, joins and the miter limit.
26
+ * `strokeScale` is the factor the baked transform scales stroke geometry
27
+ * by; when it is not exact, the caller passes `strokeScaledApproximately`
28
+ * and a stroked layer is reported. A non-scaling stroke ignores both: its
29
+ * width and dashes stay in the outer viewport's units, which are the
30
+ * layer's baked units, so it maps exactly.
31
+ */
32
+ export declare function visualStyleFromComputed(computed: ComputedStyle, localFromElement: Matrix, localBounds: Rect, strokeScale: number, strokeScaledApproximately: boolean, elementPath: string, element: SvgElementLike, ctx: PaintContext): VisualStyle;
@@ -0,0 +1,162 @@
1
+ import { miterAngleFromLimit, } from "../../model/index.js";
2
+ import { hexColorAndOpacity } from "../style/color.js";
3
+ import { resolvePaintServer } from "./gradients.js";
4
+ /** What the paint module needs of the import beyond the element itself. */
5
+ /**
6
+ * The strokes built from a `vector-effect: non-scaling-stroke` element. The
7
+ * model has no such flag, so an instance's scale overrides look them up
8
+ * here and leave their width and dashes as they are.
9
+ */
10
+ const nonScalingStrokes = new WeakSet();
11
+ /** Whether a stroke came from a non-scaling-stroke element. */
12
+ export function isNonScalingStroke(stroke) {
13
+ return nonScalingStrokes.has(stroke);
14
+ }
15
+ /** Maps SVG's `stroke-linecap` to the model's dash-cap names. */
16
+ function dashCapOf(linecap) {
17
+ return linecap === "butt" ? "none" : linecap;
18
+ }
19
+ /**
20
+ * Turns a computed `fill` or `stroke` value into model paints.
21
+ * `localFromElement` and `localBounds` place gradients in the layer's own
22
+ * geometry space, and `opacity` folds the matching `fill-opacity` or
23
+ * `stroke-opacity` in. A paint server that cannot map exactly is reported
24
+ * and the paint is dropped; nothing is guessed.
25
+ */
26
+ function paintsFromValue(value, opacity, localFromElement, localBounds, elementPath, element, property, ctx) {
27
+ switch (value.kind) {
28
+ case "none":
29
+ return [];
30
+ case "color": {
31
+ const { color, opacity: alpha } = hexColorAndOpacity(value.color);
32
+ const combined = (alpha ?? 1) * opacity;
33
+ return [{
34
+ kind: "solid",
35
+ color,
36
+ ...(combined >= 1 ? {} : { opacity: combined }),
37
+ }];
38
+ }
39
+ case "ref": {
40
+ // Opening never fetches, so a paint server in another file is an
41
+ // invalid reference, like a missing one.
42
+ const server = value.external ? { kind: "external" } : resolvePaintServer(value.id, localFromElement, localBounds, {
43
+ ids: ctx.ids,
44
+ limits: ctx.limits,
45
+ styles: ctx.styles,
46
+ viewport: ctx.viewport,
47
+ rootFontSize: ctx.rules.rootFontSize,
48
+ report: (_localName, reason, note) => {
49
+ ctx.report(elementPath, element.localName, "approximated", reason, note);
50
+ },
51
+ });
52
+ if (server === null) {
53
+ ctx.report(elementPath, element.localName, "left-out", `the ${property} gradient has no stops`, { what: "Gradients with no stops", kept: "Not painted" });
54
+ return [];
55
+ }
56
+ switch (server.kind) {
57
+ case "solid": {
58
+ const combined = (server.opacity ?? 1) * opacity;
59
+ return [{
60
+ kind: "solid",
61
+ color: server.color,
62
+ ...(combined >= 1 ? {} : { opacity: combined }),
63
+ }];
64
+ }
65
+ case "gradient": {
66
+ const paint = server.paint;
67
+ return [opacity >= 1 ? paint : { ...paint, opacity }];
68
+ }
69
+ case "pattern":
70
+ ctx.report(elementPath, element.localName, "left-out", `pattern ${property} is not supported`, { what: "Pattern fills and strokes", kept: "Not painted" });
71
+ return [];
72
+ case "unsupported":
73
+ ctx.report(elementPath, element.localName, "left-out", server.reason, { what: "Unsupported fills and strokes", kept: "Not painted" });
74
+ return [];
75
+ case "external":
76
+ case "missing":
77
+ case "not-paint-server":
78
+ case "paints-nothing": {
79
+ // Each makes the reference invalid, so SVG paints the fallback color when one is given.
80
+ const problem = server.kind === "external"
81
+ ? `the ${property} reference ${value.id} is in another file, which is never fetched`
82
+ : server.kind === "missing"
83
+ ? `the ${property} reference #${value.id} is missing`
84
+ : server.kind === "paints-nothing"
85
+ ? `an objectBoundingBox ${property} gradient paints nothing on a zero-width or zero-height box`
86
+ : `#${value.id} is a ${server.localName}, not a gradient or pattern`;
87
+ if (value.fallback !== null) {
88
+ const { color, opacity: alpha } = hexColorAndOpacity(value.fallback);
89
+ const combined = (alpha ?? 1) * opacity;
90
+ ctx.report(elementPath, element.localName, "approximated", `${problem}; its fallback color is used`, { what: "Fills and strokes that refer to nothing usable", kept: "Painted in their fallback color" });
91
+ return [{ kind: "solid", color, ...(combined >= 1 ? {} : { opacity: combined }) }];
92
+ }
93
+ ctx.report(elementPath, element.localName, "left-out", problem, { what: "Fills and strokes that refer to nothing usable", kept: "Not painted" });
94
+ return [];
95
+ }
96
+ }
97
+ }
98
+ }
99
+ }
100
+ /**
101
+ * Whether the element has a `pathLength` a browser applies: a number that is
102
+ * not negative. It scales the dash lengths by the path's real length over
103
+ * it (SVG 2, the pathLength attribute); an unreadable or negative value is
104
+ * ignored.
105
+ */
106
+ function hasReadablePathLength(element) {
107
+ const value = element.getAttribute("pathLength")?.trim() ?? "";
108
+ return /^[+]?(\d+\.?\d*|\.\d+)([eE][+-]?\d+)?$/.test(value);
109
+ }
110
+ /**
111
+ * Builds the model style of one layer from its computed style: solid fills
112
+ * and strokes, gradients, opacity, dashes, caps, joins and the miter limit.
113
+ * `strokeScale` is the factor the baked transform scales stroke geometry
114
+ * by; when it is not exact, the caller passes `strokeScaledApproximately`
115
+ * and a stroked layer is reported. A non-scaling stroke ignores both: its
116
+ * width and dashes stay in the outer viewport's units, which are the
117
+ * layer's baked units, so it maps exactly.
118
+ */
119
+ export function visualStyleFromComputed(computed, localFromElement, localBounds, strokeScale, strokeScaledApproximately, elementPath, element, ctx) {
120
+ const fills = paintsFromValue(computed.fill, computed.fillOpacity, localFromElement, localBounds, elementPath, element, "fill", ctx);
121
+ const strokePaints = paintsFromValue(computed.stroke, computed.strokeOpacity, localFromElement, localBounds, elementPath, element, "stroke", ctx);
122
+ let strokes = [];
123
+ if (computed.nonScalingStroke) {
124
+ strokeScale = 1;
125
+ strokeScaledApproximately = false;
126
+ }
127
+ if (strokePaints.length > 0 && computed.strokeWidth > 0) {
128
+ if (strokeScaledApproximately) {
129
+ ctx.report(elementPath, element.localName, "approximated", "the stroke width scales with the geometric mean of the transform's axis scales", { what: "Strokes under an uneven scale", kept: "One even stroke width is used" });
130
+ }
131
+ const width = computed.strokeWidth * strokeScale;
132
+ let dash = computed.strokeDasharray;
133
+ // A pattern whose dashes sum to zero draws a solid stroke (SVG 2 §13.5.6).
134
+ if (dash !== null && dash.every((entry) => entry === 0))
135
+ dash = null;
136
+ if (dash !== null && dash.length % 2 === 1)
137
+ dash = [...dash, ...dash];
138
+ if (computed.strokeDashoffset !== 0) {
139
+ ctx.report(elementPath, element.localName, "approximated", "stroke-dashoffset is dropped; dashes start at the path start", { what: "Dash offsets", kept: "Dashes start at the start of the path" });
140
+ }
141
+ if (dash !== null && hasReadablePathLength(element)) {
142
+ ctx.report(elementPath, element.localName, "approximated", "pathLength is not applied; the dash lengths stay in user units instead of being scaled to the path's length", { what: "Path lengths (pathLength)", kept: "Dash lengths are kept unscaled" });
143
+ }
144
+ strokes = strokePaints.map((paint) => ({
145
+ paint,
146
+ width,
147
+ ...(dash === null ? {} : { dash: dash.map((entry) => entry * strokeScale) }),
148
+ join: computed.strokeLinejoin,
149
+ ...(computed.strokeLinejoin === "miter"
150
+ ? { miterAngle: miterAngleFromLimit(computed.strokeMiterlimit) }
151
+ : {}),
152
+ dashCap: dashCapOf(computed.strokeLinecap),
153
+ }));
154
+ if (computed.nonScalingStroke)
155
+ for (const stroke of strokes)
156
+ nonScalingStrokes.add(stroke);
157
+ }
158
+ return {
159
+ ...(fills.length > 0 ? { fills } : {}),
160
+ ...(strokes.length > 0 ? { strokes } : {}),
161
+ };
162
+ }