swf2js 0.1.2 → 0.4.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 (416) hide show
  1. package/README.md +81 -14
  2. package/package.json +11 -7
  3. package/studio/actionscript-roundtrip.test.js +54 -0
  4. package/studio/actionscript-scripts.test.js +169 -0
  5. package/studio/animated-image.test.js +137 -0
  6. package/studio/arithmetic.test.js +16 -0
  7. package/studio/as3-import.test.js +110 -0
  8. package/studio/audio-info.test.js +44 -0
  9. package/studio/components.test.js +152 -0
  10. package/studio/document-editing.test.js +92 -0
  11. package/studio/examples/01-space-mission.json +1628 -0
  12. package/studio/examples/02-clockwork-vault.json +1079 -0
  13. package/studio/examples/03-neon-synth.json +1364 -0
  14. package/studio/examples/04-kinetic-poster.json +923 -0
  15. package/studio/examples/05-signal-relay.json +1131 -0
  16. package/studio/examples/06-calculator.json +564 -0
  17. package/studio/frame-editing.test.js +129 -0
  18. package/studio/frame-events.test.js +117 -0
  19. package/studio/groups.test.js +88 -0
  20. package/studio/index.html +962 -0
  21. package/studio/interaction-triggers.test.js +37 -0
  22. package/studio/linked-actions.test.js +48 -0
  23. package/studio/lottie.test.js +208 -0
  24. package/studio/masks.test.js +43 -0
  25. package/studio/media-assets.test.js +62 -0
  26. package/studio/paint-tools.test.js +60 -0
  27. package/studio/playback.test.js +17 -0
  28. package/studio/project-io.test.js +405 -0
  29. package/studio/project-preview.test.js +475 -0
  30. package/studio/scene-transition.test.js +32 -0
  31. package/studio/server/archive-companions.js +27 -0
  32. package/studio/server/as3-library.js +91 -0
  33. package/studio/server/assets.js +162 -0
  34. package/studio/server/audio-info.js +82 -0
  35. package/studio/server/font-files.js +332 -0
  36. package/studio/server/http.js +135 -0
  37. package/studio/server/linked-actions.js +93 -0
  38. package/studio/server/png-decode.js +1 -0
  39. package/studio/server/project-movie.js +1164 -0
  40. package/studio/server/project-preview.js +152 -0
  41. package/studio/server/projects.js +370 -0
  42. package/studio/server/report.js +29 -0
  43. package/studio/server/routes.js +93 -0
  44. package/studio/server/swf-authoring.js +90 -0
  45. package/studio/server/swf-control-runtime.as +513 -0
  46. package/studio/server/swf-controls.js +119 -0
  47. package/studio/server/swf-export.js +293 -0
  48. package/studio/server/swf-import.js +1675 -0
  49. package/studio/server/swf-morphs.js +64 -0
  50. package/studio/server/swf-routes.js +42 -0
  51. package/studio/server/swf-streams.js +80 -0
  52. package/studio/server/swf-transition-runtime.as +52 -0
  53. package/studio/server/tween-recovery.js +106 -0
  54. package/studio/server-api.test.js +283 -0
  55. package/studio/server.js +320 -0
  56. package/studio/src/animated-image.js +423 -0
  57. package/studio/src/appearance-inspector.js +63 -0
  58. package/studio/src/arithmetic.js +50 -0
  59. package/studio/src/authored-controls.js +42 -0
  60. package/studio/src/busy.js +41 -0
  61. package/studio/src/canvas.js +430 -0
  62. package/studio/src/clipper-browser.js +2 -0
  63. package/studio/src/color-transform.js +38 -0
  64. package/studio/src/component-inspector.js +126 -0
  65. package/studio/src/components.js +268 -0
  66. package/studio/src/display-frame.js +262 -0
  67. package/studio/src/document-editing.js +69 -0
  68. package/studio/src/event-editor.js +150 -0
  69. package/studio/src/frame-editing.js +150 -0
  70. package/studio/src/frame-events.js +128 -0
  71. package/studio/src/frame-image.js +28 -0
  72. package/studio/src/groups.js +125 -0
  73. package/studio/src/handler-editor.js +122 -0
  74. package/studio/src/history.js +83 -0
  75. package/studio/src/imported-paths.js +99 -0
  76. package/studio/src/index.js +3582 -0
  77. package/studio/src/interaction-triggers.js +42 -0
  78. package/studio/src/json-patch.js +77 -0
  79. package/studio/src/layer-icons.js +25 -0
  80. package/studio/src/lottie-export.js +948 -0
  81. package/studio/src/lottie-import.js +1138 -0
  82. package/studio/src/masks.js +69 -0
  83. package/studio/src/media-assets.js +188 -0
  84. package/studio/src/media-library.js +193 -0
  85. package/studio/src/model.js +431 -0
  86. package/studio/src/numeric-fields.js +46 -0
  87. package/studio/src/object-list.js +57 -0
  88. package/studio/src/paint-tools.js +76 -0
  89. package/studio/src/placement-effects.js +74 -0
  90. package/studio/src/playback.js +10 -0
  91. package/studio/src/preview-audio.js +64 -0
  92. package/studio/src/preview-interactions.js +106 -0
  93. package/studio/src/preview-recorder.js +335 -0
  94. package/studio/src/preview-surface.js +338 -0
  95. package/studio/src/project-io.js +905 -0
  96. package/studio/src/project-session.js +375 -0
  97. package/studio/src/projects-dialog.js +319 -0
  98. package/studio/src/scene-transition.js +129 -0
  99. package/studio/src/script-editor.js +175 -0
  100. package/studio/src/script-references.js +74 -0
  101. package/studio/src/scripted-controls.js +146 -0
  102. package/studio/src/scripts.js +185 -0
  103. package/studio/src/stage-guides.js +101 -0
  104. package/studio/src/svg-animation.js +416 -0
  105. package/studio/src/svg-context.js +522 -0
  106. package/studio/src/svg-frame.js +413 -0
  107. package/studio/src/svg-import.js +1208 -0
  108. package/studio/src/swf-recording.js +104 -0
  109. package/studio/src/symbol-library.js +39 -0
  110. package/studio/src/symbols.js +62 -0
  111. package/studio/src/text-fields.js +58 -0
  112. package/studio/src/theme.js +43 -0
  113. package/studio/src/timeline.js +321 -0
  114. package/studio/src/vector-cut.js +103 -0
  115. package/studio/src/vector-geometry.js +254 -0
  116. package/studio/src/vector-style.js +146 -0
  117. package/studio/src/video-encode.js +84 -0
  118. package/studio/src/video-export.js +331 -0
  119. package/studio/src/video-mux.js +339 -0
  120. package/studio/src/video-playback.js +281 -0
  121. package/studio/src/workspace.js +344 -0
  122. package/studio/stroke-style.test.js +119 -0
  123. package/studio/styles.css +1547 -0
  124. package/studio/svg-export.test.js +162 -0
  125. package/studio/svg-import.test.js +318 -0
  126. package/studio/swf-authoring.test.js +140 -0
  127. package/studio/swf-export.test.js +324 -0
  128. package/studio/swf-import.test.js +489 -0
  129. package/studio/symbols.test.js +68 -0
  130. package/studio/tests/accessibility-interactions.mjs +138 -0
  131. package/studio/tests/appearance-interactions.mjs +103 -0
  132. package/studio/tests/control-interactions.mjs +60 -0
  133. package/studio/tests/event-interactions.mjs +117 -0
  134. package/studio/tests/external-resources.mjs +174 -0
  135. package/studio/tests/frame-image.mjs +71 -0
  136. package/studio/tests/html-export.mjs +68 -0
  137. package/studio/tests/import-files.mjs +131 -0
  138. package/studio/tests/lottie.mjs +125 -0
  139. package/studio/tests/mask-interactions.mjs +148 -0
  140. package/studio/tests/media-interactions.mjs +109 -0
  141. package/studio/tests/paint-interactions.mjs +64 -0
  142. package/studio/tests/project-interactions.mjs +244 -0
  143. package/studio/tests/script-interactions.mjs +178 -0
  144. package/studio/tests/scripted-control-interactions.mjs +107 -0
  145. package/studio/tests/svg-export.mjs +149 -0
  146. package/studio/tests/text-field-interactions.mjs +64 -0
  147. package/studio/tests/theme-interactions.mjs +53 -0
  148. package/studio/tests/timeline-browser.mjs +507 -0
  149. package/studio/tests/transition-interactions.mjs +94 -0
  150. package/studio/tests/trigger-interactions.mjs +93 -0
  151. package/studio/tests/video-export.mjs +243 -0
  152. package/studio/tests/workspace-interactions.mjs +226 -0
  153. package/studio/tween-recovery.test.js +31 -0
  154. package/studio/vector-style.test.js +56 -0
  155. package/studio/video-export.test.js +326 -0
  156. package/studiospec/actionscript.md +88 -0
  157. package/studiospec/architecture.md +378 -0
  158. package/studiospec/controls.md +100 -0
  159. package/studiospec/file-support.md +268 -0
  160. package/studiospec/lottie.md +69 -0
  161. package/studiospec/requirements.md +135 -0
  162. package/studiospec/stroke-style.md +95 -0
  163. package/studiospec/svg-export.md +83 -0
  164. package/studiospec/svg-import.md +139 -0
  165. package/studiospec/swooflib-integration.md +115 -0
  166. package/studiospec/tasks.md +573 -0
  167. package/studiospec/video-export.md +127 -0
  168. package/swf2js/__tests__/actionscript.test.js +1609 -0
  169. package/swf2js/__tests__/adpcm-audio.test.js +43 -0
  170. package/swf2js/__tests__/as2-compiler.test.js +153 -0
  171. package/swf2js/__tests__/as3-mixed-depth.test.js +498 -0
  172. package/swf2js/__tests__/authored-sprite-hit.test.js +118 -0
  173. package/swf2js/__tests__/authored-sprite-sizing.test.js +164 -0
  174. package/swf2js/__tests__/avm1-action-prefix.test.js +134 -0
  175. package/swf2js/__tests__/avm1-attach-movie.test.js +1151 -0
  176. package/swf2js/__tests__/avm1-boxhead-budget.test.js +73 -0
  177. package/swf2js/__tests__/avm1-cfg-bubble-tanks.test.js +101 -0
  178. package/swf2js/__tests__/avm1-cfg-mario-kart.test.js +184 -0
  179. package/swf2js/__tests__/avm1-cfg-mario.test.js +31 -0
  180. package/swf2js/__tests__/avm1-cfg-pacman.test.js +49 -0
  181. package/swf2js/__tests__/avm1-checkers-gameplay.test.js +122 -0
  182. package/swf2js/__tests__/avm1-clip-actions.test.js +2002 -0
  183. package/swf2js/__tests__/avm1-clone-drag.test.js +626 -0
  184. package/swf2js/__tests__/avm1-decompiler.test.js +102 -0
  185. package/swf2js/__tests__/avm1-dynamic-locals.test.js +159 -0
  186. package/swf2js/__tests__/avm1-formal-parameters.test.js +71 -0
  187. package/swf2js/__tests__/avm1-key-events.test.js +52 -0
  188. package/swf2js/__tests__/avm1-load-bitmap.test.js +75 -0
  189. package/swf2js/__tests__/avm1-loading-host-actions.test.js +764 -0
  190. package/swf2js/__tests__/avm1-multibyte-strings.test.js +63 -0
  191. package/swf2js/__tests__/avm1-pacman-gameplay.test.js +115 -0
  192. package/swf2js/__tests__/avm1-shape-hittest.test.js +311 -0
  193. package/swf2js/__tests__/avm1-shared-bodies.test.js +57 -0
  194. package/swf2js/__tests__/avm1-shared-runtime.test.js +1364 -0
  195. package/swf2js/__tests__/avm1-snake-gameplay.test.js +139 -0
  196. package/swf2js/__tests__/avm1-super.test.js +70 -0
  197. package/swf2js/__tests__/avm1-target-path.test.js +325 -0
  198. package/swf2js/__tests__/avm1-try.test.js +229 -0
  199. package/swf2js/__tests__/avm2-activation-closures.test.js +65 -0
  200. package/swf2js/__tests__/avm2-applytype.test.js +180 -0
  201. package/swf2js/__tests__/avm2-call-ops.test.js +232 -0
  202. package/swf2js/__tests__/avm2-callsuperid.test.js +231 -0
  203. package/swf2js/__tests__/avm2-class-initializer.test.js +219 -0
  204. package/swf2js/__tests__/avm2-custom-namespace.test.js +139 -0
  205. package/swf2js/__tests__/avm2-describe-type.test.js +96 -0
  206. package/swf2js/__tests__/avm2-dictionary.test.js +96 -0
  207. package/swf2js/__tests__/avm2-domain-memory.test.js +332 -0
  208. package/swf2js/__tests__/avm2-e4x.test.js +326 -0
  209. package/swf2js/__tests__/avm2-exceptions.test.js +81 -0
  210. package/swf2js/__tests__/avm2-findprop.test.js +110 -0
  211. package/swf2js/__tests__/avm2-getlex.test.js +166 -0
  212. package/swf2js/__tests__/avm2-global-slot.test.js +106 -0
  213. package/swf2js/__tests__/avm2-interface.test.js +95 -0
  214. package/swf2js/__tests__/avm2-multiname-precedence.test.js +148 -0
  215. package/swf2js/__tests__/avm2-named-namespace-types.test.js +106 -0
  216. package/swf2js/__tests__/avm2-optional-args.test.js +100 -0
  217. package/swf2js/__tests__/avm2-outer-scopes.test.js +101 -0
  218. package/swf2js/__tests__/avm2-package-internal-class.test.js +106 -0
  219. package/swf2js/__tests__/avm2-protected-namespace.test.js +105 -0
  220. package/swf2js/__tests__/avm2-protected.test.js +214 -0
  221. package/swf2js/__tests__/avm2-qwop-gameplay.test.js +116 -0
  222. package/swf2js/__tests__/avm2-runtime-namespaces.test.js +159 -0
  223. package/swf2js/__tests__/avm2-script-traits.test.js +89 -0
  224. package/swf2js/__tests__/avm2-shared-runtime.test.js +52 -0
  225. package/swf2js/__tests__/avm2-slot-ops.test.js +117 -0
  226. package/swf2js/__tests__/avm2-type-ops.test.js +224 -0
  227. package/swf2js/__tests__/bevel-filter.test.js +116 -0
  228. package/swf2js/__tests__/bitmap-button-mask.test.js +106 -0
  229. package/swf2js/__tests__/bitmap-data-draw.test.js +388 -0
  230. package/swf2js/__tests__/bitmap-decoder.test.js +108 -0
  231. package/swf2js/__tests__/bitmapdata-color-bounds.test.js +70 -0
  232. package/swf2js/__tests__/bitmapdata-copy-channel.test.js +91 -0
  233. package/swf2js/__tests__/bitmapdata-copy-pixels.test.js +136 -0
  234. package/swf2js/__tests__/bitmapdata-fill-compare.test.js +241 -0
  235. package/swf2js/__tests__/bitmapdata-filter.test.js +232 -0
  236. package/swf2js/__tests__/bitmapdata-hit-test.test.js +71 -0
  237. package/swf2js/__tests__/bitmapdata-merge.test.js +84 -0
  238. package/swf2js/__tests__/bitmapdata-palette-map.test.js +92 -0
  239. package/swf2js/__tests__/bitmapdata-pixels-io.test.js +198 -0
  240. package/swf2js/__tests__/bitmapdata-threshold.test.js +97 -0
  241. package/swf2js/__tests__/bitmapdata-vector.test.js +82 -0
  242. package/swf2js/__tests__/blur-filter.test.js +48 -0
  243. package/swf2js/__tests__/bundle-emitter.test.js +394 -0
  244. package/swf2js/__tests__/button-actions.test.js +370 -0
  245. package/swf2js/__tests__/button-color-transform.test.js +48 -0
  246. package/swf2js/__tests__/button-decoder.test.js +106 -0
  247. package/swf2js/__tests__/button-hit-test.test.js +825 -0
  248. package/swf2js/__tests__/button-scaling-grid.test.js +124 -0
  249. package/swf2js/__tests__/button-sound.test.js +105 -0
  250. package/swf2js/__tests__/bytearray-coercion.test.js +23 -0
  251. package/swf2js/__tests__/bytearray-object-serialization.test.js +380 -0
  252. package/swf2js/__tests__/cfg-switch-loop.test.js +103 -0
  253. package/swf2js/__tests__/clip-depth-mask.test.js +119 -0
  254. package/swf2js/__tests__/clipped-bitmap-fill.test.js +118 -0
  255. package/swf2js/__tests__/companion-swf.test.js +638 -0
  256. package/swf2js/__tests__/context-menu.test.js +149 -0
  257. package/swf2js/__tests__/convolution-filter.test.js +74 -0
  258. package/swf2js/__tests__/definefont4.test.js +280 -0
  259. package/swf2js/__tests__/display-displacement.test.js +161 -0
  260. package/swf2js/__tests__/display-list.test.js +80 -0
  261. package/swf2js/__tests__/display-mask-render.test.js +656 -0
  262. package/swf2js/__tests__/displayobject-stage.test.js +320 -0
  263. package/swf2js/__tests__/embedded-script-audio.test.js +131 -0
  264. package/swf2js/__tests__/embedded-swf.test.js +202 -0
  265. package/swf2js/__tests__/embedded-textfield.test.js +107 -0
  266. package/swf2js/__tests__/embedded-timeline-audio.test.js +182 -0
  267. package/swf2js/__tests__/embedded-timeline-input.test.js +420 -0
  268. package/swf2js/__tests__/embedded-timer-lifecycle.test.js +145 -0
  269. package/swf2js/__tests__/event-priority-order.test.js +125 -0
  270. package/swf2js/__tests__/event-weak-listeners.test.js +53 -0
  271. package/swf2js/__tests__/event-will-trigger.test.js +43 -0
  272. package/swf2js/__tests__/flash-text-engine-content.test.js +115 -0
  273. package/swf2js/__tests__/flash-text-engine-format.test.js +95 -0
  274. package/swf2js/__tests__/flash-text-engine-line.test.js +314 -0
  275. package/swf2js/__tests__/font-encoder.test.js +95 -0
  276. package/swf2js/__tests__/font-outline-text.test.js +659 -0
  277. package/swf2js/__tests__/glow-shadow-filter.test.js +68 -0
  278. package/swf2js/__tests__/graphics-bitmap-smoothing.test.js +32 -0
  279. package/swf2js/__tests__/graphics-fill-lifetime.test.js +36 -0
  280. package/swf2js/__tests__/graphics-gradient-matrix.test.js +53 -0
  281. package/swf2js/__tests__/graphics-stroke-hit-test.test.js +457 -0
  282. package/swf2js/__tests__/hit-test-primitives.test.js +414 -0
  283. package/swf2js/__tests__/input-events.test.js +714 -0
  284. package/swf2js/__tests__/integration/corpus.test.js +41 -0
  285. package/swf2js/__tests__/loader-embedded-swf.test.js +210 -0
  286. package/swf2js/__tests__/loader-info.test.js +81 -0
  287. package/swf2js/__tests__/local-connection.test.js +122 -0
  288. package/swf2js/__tests__/morph-focal-gradient.test.js +51 -0
  289. package/swf2js/__tests__/morph-topology.test.js +267 -0
  290. package/swf2js/__tests__/movie-data-codec.test.js +128 -0
  291. package/swf2js/__tests__/nellymoser-audio.test.js +84 -0
  292. package/swf2js/__tests__/opaque-background-cache.test.js +352 -0
  293. package/swf2js/__tests__/path-stream.test.js +81 -0
  294. package/swf2js/__tests__/place-object-filters.test.js +88 -0
  295. package/swf2js/__tests__/place-object3-decoder.test.js +116 -0
  296. package/swf2js/__tests__/place-object3-image.test.js +85 -0
  297. package/swf2js/__tests__/player-version.test.js +46 -0
  298. package/swf2js/__tests__/root-textfield.test.js +287 -0
  299. package/swf2js/__tests__/scaling-grid.test.js +183 -0
  300. package/swf2js/__tests__/shape-decoder.test.js +361 -0
  301. package/swf2js/__tests__/shape-line-style2.test.js +138 -0
  302. package/swf2js/__tests__/shape-nonscaling-stroke.test.js +131 -0
  303. package/swf2js/__tests__/shape-pixel-hint.test.js +101 -0
  304. package/swf2js/__tests__/simplebutton-pointer.test.js +1477 -0
  305. package/swf2js/__tests__/socket-offline.test.js +67 -0
  306. package/swf2js/__tests__/sound-avm1-stop.test.js +99 -0
  307. package/swf2js/__tests__/sound-envelope.test.js +110 -0
  308. package/swf2js/__tests__/sound-extract.test.js +168 -0
  309. package/swf2js/__tests__/sound-id3.test.js +283 -0
  310. package/swf2js/__tests__/sound-load.test.js +259 -0
  311. package/swf2js/__tests__/sound-mixer-spectrum.test.js +261 -0
  312. package/swf2js/__tests__/sound-stream-sprite.test.js +624 -0
  313. package/swf2js/__tests__/sound-transform.test.js +104 -0
  314. package/swf2js/__tests__/speex-audio.test.js +77 -0
  315. package/swf2js/__tests__/stage-noscale.test.js +45 -0
  316. package/swf2js/__tests__/swf-encoder.test.js +134 -0
  317. package/swf2js/__tests__/swflib-assemble.test.js +20 -0
  318. package/swf2js/__tests__/textfield-html-text.test.js +368 -0
  319. package/swf2js/__tests__/textfield-line-layout.test.js +339 -0
  320. package/swf2js/__tests__/textfield-programmatic-overlay.test.js +2506 -0
  321. package/swf2js/__tests__/textfield-scroll.test.js +79 -0
  322. package/swf2js/__tests__/textfield-selection.test.js +383 -0
  323. package/swf2js/__tests__/timeline-effect-surfaces.test.js +295 -0
  324. package/swf2js/__tests__/timeline-named-anchor.test.js +139 -0
  325. package/swf2js/__tests__/timeline-scenes.test.js +1723 -0
  326. package/swf2js/__tests__/urlloader-http-status.test.js +236 -0
  327. package/{src → swf2js}/cli.js +43 -11
  328. package/swf2js/compiler/as2-classes.js +156 -0
  329. package/swf2js/compiler/as2-parser.js +598 -0
  330. package/swf2js/compiler/avm1-codegen.js +897 -0
  331. package/swf2js/compiler/index.js +28 -0
  332. package/{src → swf2js}/decoder/button-decoder.js +3 -1
  333. package/swf2js/decoder/png.js +114 -0
  334. package/{src → swf2js}/decoder/shape-decoder.js +20 -10
  335. package/{src → swf2js}/decoder/tag-decoder.js +5 -3
  336. package/{src → swf2js}/decoder/tag-types.js +4 -0
  337. package/swf2js/decompiler/avm1-decompiler.js +1138 -0
  338. package/{src → swf2js}/emitter/asset-encoder.js +66 -113
  339. package/{src → swf2js}/emitter/bundle-emitter.js +209 -78
  340. package/swf2js/emitter/external-resources.js +39 -0
  341. package/swf2js/emitter/movie-data-codec.js +91 -0
  342. package/swf2js/encoder/bit-writer.js +182 -0
  343. package/swf2js/encoder/shape-encoder.js +310 -0
  344. package/swf2js/encoder/swf-builder.js +639 -0
  345. package/swf2js/encoder/swf-writer.js +55 -0
  346. package/{src/runtime/swflib/assemble.js → swf2js/runtime/assemble-swooflib.js} +4 -3
  347. package/{src → swf2js}/runtime/avm1-runtime.js +102 -63
  348. package/{src → swf2js}/runtime/index.js +1 -1
  349. package/{src → swf2js}/runtime/movie-runtime.js +28 -2
  350. package/{src → swf2js}/transpiler/actionscript.js +82 -19
  351. package/{src → swf2js}/transpiler/avm1/avm1-opcodes.js +5 -3
  352. package/{src → swf2js}/transpiler/avm1/avm1-transpiler.js +69 -6
  353. package/{src → swf2js}/transpiler/avm2/avm2-transpiler.js +1 -1
  354. package/{src → swf2js}/transpiler/timeline/display-list.js +41 -22
  355. package/{src → swf2js}/transpiler/timeline/timeline-codegen.js +97 -36
  356. package/swooflib/README.md +569 -0
  357. package/swooflib/adpcm.js +147 -0
  358. package/{src/runtime/swflib → swooflib}/builtins.js +6 -0
  359. package/{src/runtime/swflib → swooflib}/context-menu.js +7 -1
  360. package/swooflib/control-definitions.js +42 -0
  361. package/swooflib/control-widget.js +763 -0
  362. package/swooflib/controls.js +300 -0
  363. package/{src/runtime/swflib → swooflib}/display.js +16 -10
  364. package/{src/runtime/swflib → swooflib}/events.js +11 -0
  365. package/{src/runtime/swflib → swooflib}/flash.js +6 -5
  366. package/{src/runtime/swflib → swooflib}/graphics.js +81 -19
  367. package/{src/runtime/swflib → swooflib}/input.js +12 -14
  368. package/{src/runtime/swflib → swooflib}/loader.js +52 -22
  369. package/swooflib/movie-network.js +86 -0
  370. package/swooflib/path-stream.js +169 -0
  371. package/{src/runtime/swflib → swooflib}/render.js +3 -13
  372. package/{src/runtime/swflib → swooflib}/sound.js +25 -2
  373. package/{src/runtime/swflib → swooflib}/stage.js +3 -3
  374. package/swooflib/stream-time.js +21 -0
  375. package/{src/runtime/swflib → swooflib}/system.js +88 -37
  376. package/{src/runtime/swflib → swooflib}/text-paint.js +14 -3
  377. package/{src/runtime/swflib → swooflib}/text.js +8 -0
  378. package/swooflib/xml-node.js +53 -0
  379. package/src/emitter/timeline-data-codec.js +0 -58
  380. /package/{src → swf2js}/decoder/symbol-table.js +0 -0
  381. /package/{src → swf2js}/emitter/embedded-swf.js +0 -0
  382. /package/{src → swf2js}/emitter/font-encoder.js +0 -0
  383. /package/{src → swf2js}/model/swf-document.js +0 -0
  384. /package/{src → swf2js}/model/transpile-result.js +0 -0
  385. /package/{src → swf2js}/parser/bit-reader.js +0 -0
  386. /package/{src → swf2js}/parser/decompressor.js +0 -0
  387. /package/{src → swf2js}/parser/swf-parser.js +0 -0
  388. /package/{src → swf2js}/pretty-printer/pretty-printer.js +0 -0
  389. /package/{src → swf2js}/runtime/avm2-runtime.js +0 -0
  390. /package/{src → swf2js}/runtime/embedded-movie-runtime.js +0 -0
  391. /package/{src → swf2js}/transpiler/avm1/avm1-ir.js +0 -0
  392. /package/{src → swf2js}/transpiler/avm1/avm1-rewrite.js +0 -0
  393. /package/{src → swf2js}/transpiler/avm2/abc-reader.js +0 -0
  394. /package/{src → swf2js}/transpiler/cfg-structurer.js +0 -0
  395. /package/{src → swf2js}/util/diagnostics.js +0 -0
  396. /package/{src → swf2js}/util/logger.js +0 -0
  397. /package/{src/runtime/swflib → swooflib}/bitmap-image.js +0 -0
  398. /package/{src/runtime/swflib → swooflib}/bitmap.js +0 -0
  399. /package/{src/runtime/swflib → swooflib}/bytearray.js +0 -0
  400. /package/{src/runtime/swflib → swooflib}/compiled-swf.js +0 -0
  401. /package/{src/runtime/swflib → swooflib}/cursor-flag.js +0 -0
  402. /package/{src/runtime/swflib → swooflib}/dictionary.js +0 -0
  403. /package/{src/runtime/swflib → swooflib}/filters.js +0 -0
  404. /package/{src/runtime/swflib → swooflib}/geom.js +0 -0
  405. /package/{src/runtime/swflib → swooflib}/glyph-metrics.js +0 -0
  406. /package/{src/runtime/swflib → swooflib}/host.js +0 -0
  407. /package/{src/runtime/swflib → swooflib}/inherit.js +0 -0
  408. /package/{src/runtime/swflib → swooflib}/marks.js +0 -0
  409. /package/{src/runtime/swflib → swooflib}/matrix3d.js +0 -0
  410. /package/{src/runtime/swflib → swooflib}/pointer-bridge.js +0 -0
  411. /package/{src/runtime/swflib → swooflib}/projection.js +0 -0
  412. /package/{src/runtime/swflib → swooflib}/sprite-drag.js +0 -0
  413. /package/{src/runtime/swflib → swooflib}/stage-ref.js +0 -0
  414. /package/{src/runtime/swflib → swooflib}/text-field.js +0 -0
  415. /package/{src/runtime/swflib → swooflib}/transform.js +0 -0
  416. /package/{src/runtime/swflib → swooflib}/xml.js +0 -0
@@ -0,0 +1,162 @@
1
+ // SVG export (studiospec/svg-export.md): path and drawing recording, transform decomposition,
2
+ // keyframe reduction, and merging frames into SMIL animations.
3
+ import { describe, it } from 'node:test';
4
+ import assert from 'node:assert/strict';
5
+ import { SvgPath, SvgRecordingContext, parseColor } from './src/svg-context.js';
6
+ import { compose, createMerge, decompose, mergeFrame, reduceTrack, renameIds, writeMerged } from './src/svg-animation.js';
7
+
8
+ const numbers = text => text.match(/-?\d*\.?\d+/g).map(Number);
9
+ const near = (actual, expected, epsilon, label) => actual.forEach((value, i) =>
10
+ assert.ok(Math.abs(value - expected[i]) <= epsilon, `${label}: ${actual} is near ${expected}`));
11
+
12
+ describe('SVG paths', () => {
13
+ it('records lines, rectangles and curves as absolute commands', () => {
14
+ const path = new SvgPath();
15
+ path.moveTo(1, 2);
16
+ path.lineTo(3, 4);
17
+ path.quadraticCurveTo(5, 6, 7, 8);
18
+ path.bezierCurveTo(1, 1, 2, 2, 3, 3);
19
+ path.closePath();
20
+ path.rect(10, 10, 5, 5);
21
+ assert.equal(path.toString(), 'M1 2L3 4Q5 6 7 8C1 1 2 2 3 3ZM10 10L15 10L15 15L10 15Z');
22
+ });
23
+
24
+ it('turns arcs into Béziers that end on the circle', () => {
25
+ const path = new SvgPath();
26
+ path.arc(0, 0, 10, 0, Math.PI * 2);
27
+ const curves = path.cmds.filter(c => c[0] === 'C');
28
+ assert.equal(curves.length, 4);
29
+ for (const c of curves) assert.ok(Math.abs(Math.hypot(c[5], c[6]) - 10) < 1e-9);
30
+ // The midpoint of a quarter Bézier stays within 0.03% of the radius.
31
+ const [, x1, y1, x2, y2, x3, y3] = curves[0];
32
+ const mid = [(10 + 3 * x1 + 3 * x2 + x3) / 8, (0 + 3 * y1 + 3 * y2 + y3) / 8];
33
+ assert.ok(Math.abs(Math.hypot(...mid) - 10) < 0.003);
34
+ });
35
+
36
+ it('parses path data, relative commands and arcs included, and maps it by a matrix', () => {
37
+ const path = new SvgPath('M10 10h5v5l-5 0z m20 0 a5 5 0 1 0 10 0');
38
+ assert.match(path.toString(), /^M10 10L15 10L15 15L10 15ZM30 10C/);
39
+ assert.deepEqual(path.cmds.at(-1).slice(-2).map(Math.round), [40, 10]);
40
+ const moved = new SvgPath();
41
+ moved.addPath(new SvgPath('M1 0L2 0'), { a: 0, b: 1, c: -1, d: 0, e: 10, f: 20 });
42
+ assert.equal(moved.toString(), 'M10 21L10 22');
43
+ });
44
+
45
+ it('reads colours as rgb() and opacity', () => {
46
+ assert.deepEqual(parseColor('#ff8000'), { rgb: 'rgb(255,128,0)', alpha: 1 });
47
+ assert.deepEqual(parseColor('rgba(10, 20, 30, 0.5)'), { rgb: 'rgb(10,20,30)', alpha: 0.5 });
48
+ assert.deepEqual(parseColor('#0f08'), { rgb: 'rgb(0,255,0)', alpha: 0x88 / 255 });
49
+ });
50
+ });
51
+
52
+ describe('SVG recording context', () => {
53
+ it('writes a gradient fill in the space it is painted in', () => {
54
+ const ctx = new SvgRecordingContext({ base: [2, 0, 0, 2, 0, 0], prefix: 'n/' });
55
+ ctx.scale(2, 2);
56
+ const gradient = ctx.createLinearGradient(-1, 0, 1, 0);
57
+ gradient.addColorStop(1, '#ffffff');
58
+ gradient.addColorStop(0, 'rgba(0,0,0,0.5)');
59
+ ctx.fillStyle = gradient;
60
+ ctx.fillRect(0, 0, 4, 4);
61
+ const [paint, shape] = ctx.elements;
62
+ assert.equal(paint.tag, 'linearGradient');
63
+ assert.deepEqual(paint.children.map(stop => [stop.attrs.offset, stop.attrs['stop-opacity']]), [['0', '0.5'], ['1', '1']]);
64
+ assert.equal(shape.attrs.fill, `url(#${paint.attrs.id})`);
65
+ // Elements are written in the base space: the shape carries only the context's own scale.
66
+ assert.equal(shape.attrs.transform, 'matrix(2,0,0,2,0,0)');
67
+ assert.equal(shape.attrs.d, 'M0 0L4 0L4 4L0 4Z');
68
+ });
69
+
70
+ it('keeps the current path in device space and wraps clipped drawing', () => {
71
+ const ctx = new SvgRecordingContext();
72
+ ctx.translate(10, 0);
73
+ ctx.beginPath();
74
+ ctx.rect(0, 0, 5, 5);
75
+ ctx.clip();
76
+ ctx.translate(10, 0);
77
+ ctx.fillStyle = 'red';
78
+ ctx.fillRect(0, 0, 1, 1);
79
+ const [clip, wrapped] = ctx.elements;
80
+ assert.equal(clip.tag, 'clipPath');
81
+ assert.equal(clip.children[0].attrs.d, 'M0 0L5 0L5 5L0 5Z');
82
+ assert.equal(clip.children[0].attrs.transform, 'matrix(1,0,0,1,10,0)');
83
+ assert.equal(wrapped.attrs['clip-path'], `url(#${clip.attrs.id})`);
84
+ assert.equal(wrapped.children[0].attrs.transform, 'matrix(1,0,0,1,20,0)');
85
+ assert.equal(wrapped.children[0].attrs.fill, 'rgb(255,0,0)');
86
+ });
87
+
88
+ it('places text on its alphabetic baseline and crops drawn images', () => {
89
+ const measure = (font, text, spacing, kerning, baseline) => ({ actualBoundingBoxAscent: baseline === 'top' ? 2 : 14, width: 10 });
90
+ const ctx = new SvgRecordingContext({ measure, image: () => ({ id: 'img0', width: 100, height: 50 }) });
91
+ ctx.font = 'bold 16px Georgia';
92
+ ctx.textBaseline = 'top';
93
+ ctx.fillText('Boo', 5, 10);
94
+ const text = ctx.elements[0];
95
+ assert.equal(text.tag, 'text');
96
+ assert.equal(text.attrs.y, '22');
97
+ assert.equal(text.text, 'Boo');
98
+ assert.match(text.attrs.style, /font:bold 16px Georgia/);
99
+ ctx.drawImage({}, 10, 0, 20, 50, 0, 0, 40, 100);
100
+ const [clip, wrapped] = ctx.elements.slice(1);
101
+ assert.equal(clip.tag, 'clipPath');
102
+ assert.equal(wrapped.children[0].attrs.transform, 'matrix(2,0,0,2,-20,0)');
103
+ });
104
+ });
105
+
106
+ describe('Animated SVG', () => {
107
+ it('decomposes matrices into translate, rotate, skew and scale', () => {
108
+ for (const parts of [
109
+ { tx: 3, ty: 4, rotation: 30, skew: 0, sx: 2, sy: 0.5 },
110
+ { tx: 0, ty: 0, rotation: -120, skew: 20, sx: 1, sy: 1 },
111
+ { tx: 1, ty: 1, rotation: 0, skew: 0, sx: 1, sy: -1 },
112
+ ]) near(compose(decompose(compose(parts))), compose(parts), 1e-9, JSON.stringify(parts));
113
+ // Turning past 180° keeps counting, and a scale of zero keeps the last angle.
114
+ const previous = decompose(compose({ tx: 0, ty: 0, rotation: 170, skew: 0, sx: 1, sy: 1 }));
115
+ assert.ok(Math.abs(decompose(compose({ tx: 0, ty: 0, rotation: 190, skew: 0, sx: 1, sy: 1 }), previous).rotation - 190) < 1e-9);
116
+ assert.equal(decompose([0, 0, 0, 0, 5, 5], { rotation: 42, skew: 7 }).rotation, 42);
117
+ });
118
+
119
+ it('reduces a tween to its corners within the tolerance and keeps jumps as steps', () => {
120
+ const linear = Array.from({ length: 11 }, (_, i) => [i * 2]);
121
+ assert.deepEqual(reduceTrack(linear, [0.05]).map(key => key.frame), [0, 10]);
122
+ const eased = Array.from({ length: 25 }, (_, i) => [100 * (i / 24) ** 2]);
123
+ const keys = reduceTrack(eased, [0.05]);
124
+ for (let f = 0; f < eased.length; f++) {
125
+ const after = keys.findIndex(key => key.frame >= f), b = keys[after], a = keys[Math.max(0, after - 1)];
126
+ const value = b.frame === a.frame ? b.value[0] : a.value[0] + (b.value[0] - a.value[0]) * (f - a.frame) / (b.frame - a.frame);
127
+ assert.ok(Math.abs(value - eased[f][0]) <= 0.05, `frame ${f}`);
128
+ }
129
+ // An ease-in starts slowly and has no jump in it.
130
+ assert.ok(reduceTrack(eased, [0.05]).every(key => !key.step));
131
+ // A clip that loops: it slides right, then starts again on the left.
132
+ const looping = [0, 10, 20, 30, 0, 10, 20, 30].map(x => [x]);
133
+ const loopKeys = reduceTrack(looping, [0.05]);
134
+ assert.deepEqual(loopKeys.map(key => [key.frame, key.step]), [[0, false], [3, false], [4, true], [7, false]]);
135
+ });
136
+
137
+ it('merges frames by key: appearing objects, order swaps, variants and loops', () => {
138
+ const g = (key, attrs, children = [], text) => ({ tag: text == null ? 'g' : 'text', key, attrs, children, text });
139
+ const merged = createMerge();
140
+ const frames = [
141
+ g('stage', {}, [g('a', { transform: 'matrix(1,0,0,1,0,0)' }), g('b', { opacity: '1' })]),
142
+ g('stage', {}, [g('a', { transform: 'matrix(1,0,0,1,10,0)' }), g('b', { opacity: '0.5' }), g('c', {}, [], 'hi')]),
143
+ g('stage', {}, [g('b', { opacity: '0' }), g('a', { transform: 'matrix(1,0,0,1,20,0)' }), g('c', {}, [], 'bye')]),
144
+ ];
145
+ frames.forEach((tree, i) => mergeFrame(merged, tree, i));
146
+ const stage = merged.root.byKey.get('stage');
147
+ // c's second text is a variant placed where that frame had it; the two never show together.
148
+ assert.deepEqual(stage.order, ['a', 'b', 'a^1', 'c~1', 'c']);
149
+ const svg = writeMerged(merged, { frames: 3, fps: 10, loopFrom: 1 });
150
+ // a holds its attribute in the intro, so only the loop part, from 0.1 s, is written; its copy
151
+ // a^1 starts there.
152
+ assert.doesNotMatch(svg, /begin="0s"/);
153
+ assert.match(svg, /<g transform="matrix\(1,0,0,1,0,0\)"><animate attributeName="display" calcMode="discrete" begin="0.1s" dur="0.2s" repeatCount="indefinite" values="inline;none" keyTimes="0;0.5"\/><animate attributeName="transform" calcMode="discrete" begin="0.1s" dur="0.2s" repeatCount="indefinite" values="matrix\(1,0,0,1,10,0\)"/);
154
+ assert.match(svg, /<g transform="matrix\(1,0,0,1,20,0\)" display="none">/);
155
+ assert.match(svg, /<text display="none">.*>hi<\/text>/);
156
+ // A lone change between two frames is a step: 0.5 holds, then 0 from the second frame.
157
+ assert.match(svg, /attributeName="opacity" begin="0.1s" dur="0.2s" repeatCount="indefinite" values="0.5;0.5;0;0" keyTimes="0;0.5;0.5;1"/);
158
+ const renamed = renameIds({ tag: 'g', key: 'k', attrs: { filter: 'url(#f1)' }, children: [{ tag: 'filter', key: 'f', attrs: { id: 'f1' }, children: [] }] }, 'v1');
159
+ assert.equal(renamed.attrs.filter, 'url(#f1v1)');
160
+ assert.equal(renamed.children[0].attrs.id, 'f1v1');
161
+ });
162
+ });
@@ -0,0 +1,318 @@
1
+ // SVG import (studiospec/svg-import.md): the XML reader, values, path data, shapes, text, images,
2
+ // clip paths, filters, and files that are not to be trusted.
3
+ import { describe, it } from 'node:test';
4
+ import assert from 'node:assert/strict';
5
+ import { importSvg, isSvgText, parseColor, parsePathData, parseTransform, parseXml } from './src/svg-import.js';
6
+ import { parseProjectJson, serializeProject } from './src/project-io.js';
7
+ import { withStudioData } from './src/svg-animation.js';
8
+
9
+ const close = (actual, expected, epsilon, label) => assert.ok(Math.abs(actual - expected) <= epsilon, `${label}: ${actual} is near ${expected}`);
10
+ const svg = (body, attrs = 'width="400" height="300" viewBox="0 0 400 300"') => `<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" ${attrs}>${body}</svg>`;
11
+ const load = async (text, options) => {
12
+ const result = await importSvg(text, options);
13
+ const project = parseProjectJson(result.json);
14
+ return { ...result, project, elements: project.scenes[0].elements };
15
+ };
16
+ const PNG = 'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8z8BQDwAEhQGAhKmMIQAAAABJRU5ErkJggg==';
17
+
18
+ describe('SVG reading', () => {
19
+ it('recognises SVG text, with an XML declaration, comments and a DOCTYPE with entities', async () => {
20
+ assert.ok(isSvgText('<?xml version="1.0"?>\n<!-- made by hand --><!DOCTYPE svg [<!ENTITY a "b">]><svg></svg>'));
21
+ assert.ok(!isSvgText('{"format":"swoof-studio"}'));
22
+ assert.ok(!isSvgText('<html><svg></svg></html>'));
23
+ });
24
+
25
+ it('reads elements, attributes and text, and never expands DOCTYPE entities', async () => {
26
+ const root = parseXml('<!DOCTYPE svg [<!ENTITY boom "BOOM">]><svg a="1" b=\'x &amp; y\'><text>&boom; &lt;ok&gt; &#65;&#x42;<![CDATA[<raw>]]></text><g/></svg>');
27
+ const node = root.children.find(child => child.name === 'svg');
28
+ assert.deepEqual(node.attrs, { a: '1', b: 'x & y' });
29
+ assert.equal(node.children[0].children.map(child => child.text).join(''), ' <ok> AB<raw>');
30
+ assert.equal(node.children[1].name, 'g');
31
+ });
32
+
33
+ it('reads colours in every CSS form', async () => {
34
+ assert.deepEqual(parseColor('#abc'), { color: '#aabbcc', alpha: 1 });
35
+ assert.deepEqual(parseColor('#11223380'), { color: '#112233', alpha: 128 / 255 });
36
+ assert.deepEqual(parseColor('rgb(255, 0, 10)'), { color: '#ff000a', alpha: 1 });
37
+ assert.deepEqual(parseColor('rgba(100%, 0%, 0%, 0.5)'), { color: '#ff0000', alpha: 0.5 });
38
+ assert.deepEqual(parseColor('navy'), { color: '#000080', alpha: 1 });
39
+ assert.equal(parseColor('none'), null);
40
+ assert.equal(parseColor('hsl(0, 100%, 50%)'), undefined);
41
+ assert.deepEqual(parseColor('hsl(0, 100%, 50%)', () => '#ff0000'), { color: '#ff0000', alpha: 1 });
42
+ });
43
+
44
+ it('reads transform lists in order', async () => {
45
+ const m = parseTransform('translate(10 20) rotate(90) scale(2)');
46
+ [0, 2, -2, 0, 10, 20].forEach((value, k) => close(m[k], value, 1e-9, `matrix[${k}]`));
47
+ const about = parseTransform('rotate(180, 50, 50)');
48
+ close(about[4], 100, 1e-9, 'rotation about a point');
49
+ });
50
+
51
+ it('reads path data: relative commands, smooth curves, quadratics, arcs and packed flags', async () => {
52
+ const [square, curve] = parsePathData('M0 0h10v10H0z m20 0 q5 -10 10 0 t10 0 a5 5 0 0110 0');
53
+ assert.equal(square.closed, true);
54
+ assert.deepEqual(square.points.map(p => [p.x, p.y]), [[0, 0], [10, 0], [10, 10], [0, 10]]);
55
+ assert.equal(curve.closed, false);
56
+ assert.deepEqual(curve.points.map(p => [Math.round(p.x) + 0, Math.round(p.y) + 0]).at(-1), [50, 0]);
57
+ assert.ok(curve.points.length >= 5, 'the arc becomes cubic segments');
58
+ });
59
+ });
60
+
61
+ describe('SVG import', () => {
62
+ it('sizes the stage from the viewBox and scales tiny or huge images to fit', async () => {
63
+ assert.deepEqual((await load(svg(""))).project.stage, { width: 400, height: 300, frameRate: 24, background: '#ffffff' });
64
+ const small = await load(svg('', 'viewBox="0 0 48 48"'));
65
+ assert.deepEqual([small.project.stage.width, small.project.stage.height], [160, 160]);
66
+ assert.match(small.notes.join(), /48 × 48 image \(scaled to 160 × 160\)/);
67
+ });
68
+
69
+ it('keeps rectangles and ellipses as such, and turns other shapes into paths', async () => {
70
+ const { elements, notes } = await load(svg(`
71
+ <rect x="10" y="20" width="100" height="50" fill="#ff0000"/>
72
+ <rect x="10" y="100" width="100" height="50" rx="8" fill="blue" stroke="black" stroke-width="3"/>
73
+ <circle cx="200" cy="150" r="40" fill="green"/>
74
+ <polygon points="300,10 350,60 250,60" fill="#123456"/>
75
+ <polyline points="10,200 50,250 90,200" fill="none" stroke="purple" stroke-width="4"/>`));
76
+ assert.deepEqual(elements.map(e => e.type), ['rectangle', 'roundedRectangle', 'ellipse', 'path', 'path']);
77
+ assert.deepEqual([elements[0].x, elements[0].y, elements[0].width, elements[0].height, elements[0].fill], [60, 45, 100, 50, '#ff0000']);
78
+ assert.deepEqual([elements[1].cornerRadius, elements[1].stroke, elements[1].strokeWidth], [8, '#000000', 3]);
79
+ assert.deepEqual([elements[2].x, elements[2].y, elements[2].width], [200, 150, 80]);
80
+ assert.equal(elements[3].closed, true);
81
+ assert.deepEqual([elements[4].closed, elements[4].stroke, elements[4].strokeWidth], [false, '#800080', 4]);
82
+ assert.deepEqual(notes, []);
83
+ });
84
+
85
+ it('applies group transforms, inherits styles and multiplies opacity', async () => {
86
+ const { elements } = await load(svg('<g fill="#00ff00" opacity="0.5" transform="translate(100 50) rotate(30)"><ellipse rx="40" ry="20" style="opacity:0.5"/></g>'));
87
+ const [ellipse] = elements;
88
+ assert.deepEqual([ellipse.fill, ellipse.x, ellipse.y, ellipse.opacity], ['#00ff00', 100, 50, 0.25]);
89
+ close(ellipse.rotation, 30, 1e-6, 'rotation');
90
+ });
91
+
92
+ it('bakes transforms a pose cannot hold into the points of a path', async () => {
93
+ const { elements } = await load(svg('<rect width="1" height="10" fill="red" transform="matrix(200 0 0 1 0 0)"/>'));
94
+ assert.equal(elements[0].type, 'path', 'a scale of 200 is more than a pose holds');
95
+ assert.deepEqual(elements[0].points.map(p => [Math.round(p.x + elements[0].x), Math.round(p.y + elements[0].y)]), [[0, 0], [200, 0], [200, 10], [0, 10]]);
96
+ });
97
+
98
+ it('gives a fill and the stroke drawn over the same outline back as one object', async () => {
99
+ const d = 'M10 10 L90 10 L90 90 Z';
100
+ const { elements } = await load(svg(`<path d="${d}" fill="#ff0000"/><path d="${d}" fill="none" stroke="#0000ff" stroke-width="2"/><path d="${d}" fill="#00ff00"/>`));
101
+ assert.equal(elements.length, 2, 'the stroke joins the fill under it; a second fill stays');
102
+ assert.deepEqual([elements[0].fill, elements[0].stroke, elements[0].strokeWidth, elements[1].fill], ['#ff0000', '#0000ff', 2, '#00ff00']);
103
+ });
104
+
105
+ it('maps linear and radial gradients with their stops and opacity', async () => {
106
+ const { elements } = await load(svg(`<defs>
107
+ <linearGradient id="a"><stop offset="0" stop-color="red"/><stop offset="1" stop-color="blue" stop-opacity="0.5"/></linearGradient>
108
+ <radialGradient id="b" href="#a" cx="0.5" cy="0.5" r="0.5"/></defs>
109
+ <rect width="200" height="100" fill="url(#a)" fill-opacity="0.5"/><circle cx="300" cy="200" r="50" fill="url(#b)"/>`));
110
+ const [linear, radial] = elements;
111
+ assert.equal(linear.gradient.type, 'linear');
112
+ assert.deepEqual(linear.gradient.stops, [{ ratio: 0, color: '#ff0000', alpha: 0.5 }, { ratio: 1, color: '#0000ff', alpha: 0.25 }]);
113
+ assert.equal(linear.opacity, 1, 'fill opacity is in the stops, not applied twice');
114
+ close(linear.gradient.matrix.a, 0.5, 1e-3, 'the gradient square spans the box (Flash convention, studio/src/vector-style.js)');
115
+ assert.equal(radial.gradient.type, 'radial');
116
+ assert.equal(radial.gradient.stops.length, 2, 'stops come through href');
117
+ });
118
+
119
+ it('reads text with its font, lines, anchor and colour', async () => {
120
+ const { elements, notes } = await load(svg('<text x="200" y="100" text-anchor="middle" style="font: italic bold 24px Georgia, serif" fill="navy">Hello <tspan x="200" dy="1.2em">world</tspan></text>'));
121
+ const [text] = elements;
122
+ assert.deepEqual([text.type, text.text, text.fontFamily, text.fontSize, text.bold, text.textAlign, text.textColor], ['text', 'Hello\nworld', 'Georgia', 24, true, 'center', '#000080']);
123
+ close(text.x, 200, 1, 'centred on its anchor');
124
+ assert.equal(text.italic, true);
125
+ assert.doesNotMatch(notes.join(), /Italic/);
126
+ });
127
+
128
+ it('embeds data URI images, and leaves out linked ones', async () => {
129
+ const { elements, project, notes } = await load(svg(`<image x="10" y="10" width="40" height="20" href="${PNG}"/><image width="10" height="10" xlink:href="https://example.com/a.png"/>`));
130
+ assert.equal(project.assets.length, 1);
131
+ assert.deepEqual([elements.length, elements[0].type, elements[0].x, elements[0].y, elements[0].width], [1, 'bitmap', 30, 20, 40]);
132
+ assert.match(notes.join(), /Images linked from outside the file/);
133
+ });
134
+
135
+ it('makes a clip path a mask, and reuses <use> and <symbol> content', async () => {
136
+ const { elements } = await load(svg(`<defs><clipPath id="c"><circle cx="100" cy="100" r="50"/></clipPath><symbol id="s"><rect width="10" height="10"/></symbol></defs>
137
+ <rect width="200" height="200" fill="teal" clip-path="url(#c)"/><use href="#s" x="300" y="20" fill="gold"/>`));
138
+ const mask = elements.find(e => e.type === 'ellipse');
139
+ const clipped = elements.find(e => e.fill === '#008080');
140
+ assert.equal(clipped.maskId, mask.id);
141
+ assert.ok(elements.indexOf(mask) < elements.indexOf(clipped), 'a mask comes before what it masks');
142
+ assert.deepEqual([elements.at(-1).fill, elements.at(-1).x, elements.at(-1).y], ['#ffd700', 305, 25]);
143
+ });
144
+
145
+ it('reads blur, drop shadow and glow filters as effects', async () => {
146
+ const { elements, notes } = await load(svg(`<defs>
147
+ <filter id="blur"><feGaussianBlur stdDeviation="4"/></filter>
148
+ <filter id="shadow"><feGaussianBlur in="SourceAlpha" stdDeviation="2"/><feOffset dx="0" dy="5"/><feFlood flood-color="#000" flood-opacity="0.5"/><feComposite operator="in" in2="SourceAlpha"/><feMerge><feMergeNode/><feMergeNode in="SourceGraphic"/></feMerge></filter>
149
+ <filter id="glow"><feGaussianBlur stdDeviation="3 3"/><feColorMatrix values="0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 2 0"/><feComponentTransfer><feFuncA type="linear" slope="0.6"/></feComponentTransfer><feComposite in2="SourceGraphic" operator="out"/></filter>
150
+ <filter id="odd"><feTurbulence/></filter></defs>
151
+ <rect width="10" height="10" filter="url(#blur)"/><rect width="10" height="10" filter="url(#shadow)"/><rect width="10" height="10" filter="url(#glow)"/><rect width="10" height="10" filter="url(#odd)"/>`));
152
+ const sigma = blur => Math.sqrt((blur * blur - 1) / 12);
153
+ assert.equal(elements[0].effect.type, 'blur');
154
+ close(sigma(elements[0].effect.blurX), 4, 1e-3, 'the blur keeps its deviation');
155
+ assert.deepEqual([elements[1].effect.type, elements[1].effect.distance, elements[1].effect.angle, elements[1].effect.alpha], ['shadow', 5, 90, 0.5]);
156
+ assert.deepEqual([elements[2].effect.type, elements[2].effect.color, elements[2].effect.alpha], ['glow', '#ff0000', 0.6]);
157
+ assert.equal(elements[3].effect, undefined);
158
+ assert.match(notes.join(), /Filters other than blurs, shadows and glows/);
159
+ });
160
+
161
+ it('reports what Studio cannot hold, and skips hidden and editor-only elements', async () => {
162
+ const { elements, notes } = await load(svg(`<style>g > .a{fill:red}</style><script>alert(1)</script>
163
+ <rect width="10" height="10"/><rect width="10" height="10" display="none"/><sodipodi:namedview><x/></sodipodi:namedview>
164
+ <path d="M0 0 L10 10" stroke="black" stroke-dasharray="2 2" marker-end="url(#missing)"/>
165
+ <rect width="10" height="10"><animate attributeName="x" to="100"/></rect><foreignObject><p>hi</p></foreignObject>`));
166
+ assert.equal(elements.length, 3);
167
+ const text = notes.join('; ');
168
+ for (const note of ['Style sheet rules with combinators', 'Scripts', 'SVG animation', '<foreignObject>']) assert.ok(text.includes(note), note);
169
+ assert.ok(!text.includes('Dashed'), 'dashes are kept');
170
+ assert.ok(!text.includes('sodipodi'));
171
+ });
172
+
173
+ it('applies style sheet rules by specificity, before the style attribute', async () => {
174
+ const { elements, notes } = await load(svg(`<defs><style>/* Illustrator */ .a{fill:#ff0000;opacity:.5} rect.a{fill:#00ff00} #b{fill:#0000ff} .c,.d{stroke:#000;stroke-width:3px}</style></defs>
175
+ <rect class="a" width="10" height="10"/><rect id="b" class="a" width="10" height="10"/><rect class="a c" width="10" height="10" style="fill:#123456"/><circle class="d" r="5" fill="none"/>`));
176
+ assert.deepEqual(elements.map(e => e.fill ?? e.stroke).slice(0, 3), ['#00ff00', '#0000ff', '#123456']);
177
+ assert.equal(elements[0].opacity, 0.5, 'non-inherited properties from rules');
178
+ assert.deepEqual([elements[2].stroke, elements[2].strokeWidth, elements[3].stroke, elements[3].strokeWidth], ['#000000', 3, '#000000', 3]);
179
+ assert.deepEqual(notes, []);
180
+ });
181
+
182
+ it('keeps gradient spread and gradient strokes', async () => {
183
+ const { elements, notes } = await load(svg(`<defs><linearGradient id="g" spreadMethod="reflect" x2="0.5"><stop offset="0" stop-color="red"/><stop offset="1" stop-color="blue"/></linearGradient></defs>
184
+ <rect width="100" height="50" fill="url(#g)" stroke="url(#g)" stroke-width="4"/><path d="M0 100 L100 100" stroke="url(#g)" stroke-width="2"/>`));
185
+ assert.equal(elements[0].gradient.spreadMethod, 'reflect');
186
+ assert.equal(elements[0].strokeGradient.type, 'linear');
187
+ assert.equal(elements[0].stroke, '#ff0000');
188
+ assert.ok(elements[1].strokeGradient, 'an open stroke keeps its gradient');
189
+ assert.deepEqual(notes, []);
190
+ });
191
+
192
+ it('draws line markers at the vertices, turned with the line and sized by its stroke', async () => {
193
+ const { elements } = await load(svg(`<defs><marker id="arrow" viewBox="0 0 10 10" refX="10" refY="5" markerWidth="4" markerHeight="4" orient="auto">
194
+ <path d="M0 0 L10 5 L0 10 z" fill="#ff0000"/></marker><marker id="dot" markerWidth="2" markerHeight="2"><circle r="1" fill="#0000ff"/></marker></defs>
195
+ <polyline points="0,0 100,0 100,100" fill="none" stroke="black" stroke-width="2" marker-end="url(#arrow)" marker-mid="url(#dot)"/>`));
196
+ const arrow = elements.find(e => e.fill === '#ff0000');
197
+ const dot = elements.find(e => e.fill === '#0000ff');
198
+ // The arrow tip (refX 10) sits on the end point, pointing down the last segment: 8 units long.
199
+ assert.deepEqual([Math.round(arrow.x), Math.round(arrow.y), Math.round(arrow.width), Math.round(arrow.height)], [100, 96, 8, 8]);
200
+ assert.deepEqual([dot.x, dot.y, dot.width * dot.scaleX], [100, 0, 4], 'a circle of radius 1, at twice the size for a stroke of 2');
201
+ });
202
+
203
+ it('makes an SVG mask a mask of its first shape, and draws a switch\'s first child', async () => {
204
+ const { elements, notes } = await load(svg(`<defs><mask id="m"><circle cx="50" cy="50" r="40" fill="white"/></mask></defs>
205
+ <rect width="100" height="100" fill="teal" mask="url(#m)"/><switch><g systemLanguage="fr"><rect width="5" height="5" fill="#111111"/></g><rect width="5" height="5" fill="#222222"/></switch>`));
206
+ const mask = elements.find(e => e.type === 'ellipse');
207
+ assert.equal(elements.find(e => e.fill === '#008080').maskId, mask.id);
208
+ assert.ok(elements.some(e => e.fill === '#111111') && !elements.some(e => e.fill === '#222222'));
209
+ assert.match(notes.join(), /SVG masks \(as the outline of their shapes/);
210
+ });
211
+
212
+ it('fills a self-crossing outline even-odd when the SVG says so', async () => {
213
+ const star = 'M50 0 L80 100 L0 35 L100 35 L20 100 Z';
214
+ const { elements } = await load(svg(`<path d="${star}" fill="red" fill-rule="evenodd"/><path d="${star}" fill="blue"/><path d="M0 0 H10 V10 Z" fill="green" fill-rule="evenodd"/>`));
215
+ assert.equal(elements[0].fillRule, 'evenodd', 'even-odd: the star has a hole');
216
+ assert.equal(elements[1].fillRule, undefined, 'one outline fills nonzero, as Studio does');
217
+ assert.equal(elements[2].fillRule, undefined, 'an outline that does not cross itself stays simple');
218
+ });
219
+
220
+ it('keeps nonzero fills of several contours, line ends, corners and dashes', async () => {
221
+ const { elements, project } = await load(svg(`<path d="M0 0 H100 V100 H0 Z M20 20 H80 V80 H20 Z" fill="red"/>
222
+ <path d="M0 0 H100 V100 H0 Z M20 20 V80 H80 V20 Z" fill="blue" fill-rule="evenodd"/>
223
+ <path d="M10 10 L90 10 L90 90" fill="none" stroke="black" stroke-width="4" stroke-dasharray="6,3" stroke-dashoffset="2" stroke-miterlimit="8"/>
224
+ <rect x="5" y="5" width="50" height="40" fill="green" stroke="black" stroke-linejoin="round" stroke-linecap="square" transform="scale(2)"/>`));
225
+ assert.equal(elements[0].fillRule, 'nonzero', 'SVG fills nonzero by default; the hole is covered');
226
+ assert.equal(elements[1].fillRule, undefined, 'even-odd compound paths are Studio\'s default');
227
+ assert.deepEqual([elements[2].lineCap, elements[2].lineJoin, elements[2].miterLimit, elements[2].strokeDash, elements[2].strokeDashOffset], ['butt', 'miter', 8, [6, 3], 2]);
228
+ assert.deepEqual([elements[3].type, elements[3].lineCap, elements[3].lineJoin, elements[3].strokeDash], ['rectangle', 'square', undefined, undefined]);
229
+ const again = parseProjectJson(JSON.stringify(project)).scenes[0].elements;
230
+ assert.deepEqual(again[2].strokeDash, [6, 3], 'the project keeps them');
231
+ });
232
+
233
+ it('lays text on a path out letter by letter, turned with the path', async () => {
234
+ const { elements, notes } = await load(svg(`<path id="arc" d="M20 150 Q150 0 280 150" fill="none"/>
235
+ <text font-size="20" fill="red" font-style="italic"><textPath xlink:href="#arc" startOffset="50%" text-anchor="middle">Hi there</textPath></text>`));
236
+ const letters = elements.filter(e => e.type === 'text');
237
+ assert.deepEqual(letters.map(e => e.text).join(''), 'Hithere', 'one object per letter; spaces only advance');
238
+ assert.ok(letters.every(e => e.groupId === letters[0].groupId && e.italic && e.textColor === '#ff0000'));
239
+ const rotation = e => e.keyframes[0].rotation ?? e.rotation ?? 0;
240
+ assert.ok(rotation(letters[0]) < -5 && rotation(letters.at(-1)) > 5, 'turned with the arc');
241
+ close(letters[0].x + letters.at(-1).x, 300, 6, 'centred on the middle of the path');
242
+ assert.doesNotMatch(notes.join(), /path/);
243
+ });
244
+
245
+ it('fills with patterns as repeating bitmaps, and draws soft masks and other filters as pictures', async () => {
246
+ const calls = [];
247
+ const rasterize = async (text, width, height, { crop = false } = {}) => {
248
+ calls.push({ text, width, height, crop });
249
+ return crop ? { data: PNG, x: 20, y: 40, width: 60, height: 80 } : { data: PNG, x: 0, y: 0, width, height };
250
+ };
251
+ const { elements, project, notes } = await load(svg(`<defs><pattern id="p" width="10" height="10" patternUnits="userSpaceOnUse"><rect width="5" height="5" fill="red"/></pattern>
252
+ <linearGradient id="g"><stop offset="0" stop-color="white"/><stop offset="1" stop-color="black"/></linearGradient><mask id="m"><rect width="100" height="100" fill="url(#g)"/></mask>
253
+ <linearGradient id="unused"><stop offset="0" stop-color="red"/></linearGradient></defs>
254
+ <rect x="10" y="10" width="40" height="40" fill="url(#p)"/><circle cx="50" cy="50" r="20" fill="blue" mask="url(#m)"/>`), { rasterize });
255
+ const tile = elements.find(e => e.bitmapFill);
256
+ assert.equal(tile.type, 'path', 'a pattern-filled rectangle becomes a path');
257
+ assert.equal(tile.bitmapFill.repeat, true);
258
+ assert.ok(project.assets.some(asset => asset.id === tile.assetId));
259
+ const picture = elements.find(e => e.type === 'bitmap');
260
+ assert.deepEqual([picture.width, picture.height, picture.x, picture.y], [30, 40, 25, 40], 'cropped, at half the drawing resolution');
261
+ const pictureText = calls.find(call => call.crop).text;
262
+ assert.match(pictureText, /<mask id="m"/);
263
+ assert.match(pictureText, /id="g"/, 'the gradient the mask needs');
264
+ assert.doesNotMatch(pictureText, /id="unused"/, 'nothing else');
265
+ assert.match(notes.join(), /Soft SVG masks \(drawn as pictures\)/);
266
+
267
+ // At most 64 pictures for a file; the rest keep the plain fallback.
268
+ calls.length = 0;
269
+ const many = Array.from({ length: 70 }, (_, k) => `<rect x="${k}" width="5" height="5" filter="url(#t)"/>`).join('');
270
+ const capped = await load(svg(`<defs><filter id="t"><feTurbulence baseFrequency="0.1"/></filter></defs>${many}`), { rasterize });
271
+ assert.equal(calls.length, 64);
272
+ assert.match(capped.notes.join(), /More than 64 pictures/);
273
+ // Without a browser, the old fallbacks.
274
+ const plain = await load(svg('<defs><pattern id="p" width="10" height="10"><rect width="5" height="5"/></pattern></defs><rect width="40" height="40" fill="url(#p)"/>'));
275
+ assert.equal(plain.elements[0].bitmapFill, undefined);
276
+ assert.match(plain.notes.join(), /Pattern fills \(drawn in a plain colour\)/);
277
+ });
278
+
279
+ it('reads inner shadows from Inkscape, Figma and Studio filters', async () => {
280
+ const { elements } = await load(svg(`<defs>
281
+ <filter id="ink"><feFlood flood-color="#0000ff" flood-opacity="0.5" result="f"/><feComposite in="f" in2="SourceGraphic" operator="out" result="c"/>
282
+ <feGaussianBlur in="c" stdDeviation="2" result="b"/><feOffset dx="3" dy="3" result="o"/><feComposite in="o" in2="SourceGraphic" operator="atop"/></filter>
283
+ <filter id="figma"><feFlood flood-opacity="0" result="BackgroundImageFix"/><feBlend mode="normal" in="SourceGraphic" in2="BackgroundImageFix" result="shape"/>
284
+ <feColorMatrix in="SourceAlpha" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0" result="hardAlpha"/><feOffset dy="4"/><feGaussianBlur stdDeviation="2"/>
285
+ <feComposite in2="hardAlpha" operator="arithmetic" k2="-1" k3="1"/><feColorMatrix type="matrix" values="0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0.25 0"/><feBlend mode="normal" in2="shape"/></filter>
286
+ <filter id="studio"><feColorMatrix in="SourceGraphic" type="matrix" values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 -1 1" result="i"/><feGaussianBlur in="i" stdDeviation="3"/>
287
+ <feColorMatrix type="matrix" values="0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 0 0 1 0" result="e"/><feComposite in="e" in2="SourceGraphic" operator="in"/></filter></defs>
288
+ <rect width="50" height="50" fill="#ccc" filter="url(#ink)"/><rect width="50" height="50" fill="#ccc" filter="url(#figma)"/><rect width="50" height="50" fill="#ccc" filter="url(#studio)"/>`));
289
+ assert.deepEqual([elements[0].effect.type, elements[0].effect.inner, elements[0].effect.color, elements[0].effect.alpha], ['shadow', true, '#0000ff', 0.5]);
290
+ assert.deepEqual([elements[1].effect.type, elements[1].effect.inner, elements[1].effect.color, elements[1].effect.alpha], ['shadow', true, '#ff0000', 0.25]);
291
+ assert.deepEqual([elements[2].effect.type, elements[2].effect.inner, elements[2].effect.color], ['glow', true, '#00ff00']);
292
+ });
293
+
294
+ it('opens a Studio SVG with its project exactly, and reads it as drawn once edited elsewhere', async () => {
295
+ const project = parseProjectJson(JSON.stringify({ stage: { width: 200, height: 150, frameRate: 12 }, scenes: [{ id: 's', name: 'Moving', durationFrames: 6, elements: [
296
+ { id: 'r', name: 'r', type: 'rectangle', x: 50, y: 50, width: 40, height: 20, fill: '#ff0000', keyframes: [{ frame: 1, x: 50, tween: 'easeIn' }, { frame: 6, x: 150 }] }] }] }));
297
+ const drawn = svg('<rect x="30" y="40" width="40" height="20" fill="#ff0000"><animate attributeName="x" values="30;130" dur="0.5s"/></rect>', 'viewBox="0 0 200 100"');
298
+ const text = await withStudioData(drawn, project);
299
+ assert.ok(text.startsWith(drawn.slice(0, drawn.indexOf('>') + 1) + '<metadata id="swoof-studio"'));
300
+ const exact = await importSvg(text);
301
+ assert.equal(exact.exact, true);
302
+ assert.equal(serializeProject(parseProjectJson(exact.json)), serializeProject(project));
303
+ const edited = await load(text.replace('fill="#ff0000"', 'fill="#00ff00"'));
304
+ assert.equal(edited.exact, false);
305
+ assert.equal(edited.elements[0].fill, '#00ff00');
306
+ assert.match(edited.notes.join(), /changed after Studio exported it/);
307
+ });
308
+
309
+ it('treats the file as untrusted: deep nesting, reference cycles and huge values', async () => {
310
+ await assert.rejects(importSvg(svg('<g>'.repeat(300) + '</g>'.repeat(300))), /nested too deeply/);
311
+ const cycle = await load(svg('<g id="a"><use href="#a"/></g><rect width="5" height="5"/>'));
312
+ assert.equal(cycle.elements.length, 1);
313
+ assert.match(cycle.notes.join(), /<use> of something missing or nested too deep/);
314
+ const huge = await load(svg('<path d="M0 0 L1e30 0 L0 1e30 Z"/><rect width="1e9" height="5"/>'));
315
+ assert.ok(huge.elements.every(e => Math.abs(e.x) <= 100000 && e.width <= 10000));
316
+ await assert.rejects(importSvg('<html></html>'), /not an SVG/);
317
+ });
318
+ });
@@ -0,0 +1,140 @@
1
+ import test from 'node:test';
2
+ import assert from 'node:assert/strict';
3
+ import { authoringMetadata, readAuthoringMetadata } from './server/swf-authoring.js';
4
+ import { convertSwf } from './server/swf-import.js';
5
+ import { exportProjectSwf } from './server/swf-export.js';
6
+ import { parse } from '../swf2js/parser/swf-parser.js';
7
+ import { decode } from '../swf2js/decoder/tag-decoder.js';
8
+ import { TAG } from '../swf2js/decoder/tag-types.js';
9
+ import { encodeTag, writeSwf } from '../swf2js/encoder/swf-writer.js';
10
+ import { DiagnosticsCollector } from '../swf2js/util/diagnostics.js';
11
+
12
+ function project(version, loop) {
13
+ const art = { type: 'rectangle', width: 40, height: 30, x: 50, y: 60, fill: '#ff0000', strokeWidth: 0 };
14
+ const instance = (id, symbolId, props = {}) => ({ id, name: id, type: 'symbol', symbolId, width: 200, height: 160, ...props });
15
+ const click = { trigger: 'click', type: 'stop' };
16
+ const scripts = [
17
+ { id: 'init', code: 'function BadgeClass(){ this.fromClass = 81; } BadgeClass.prototype = new MovieClip(); Object.registerClass("Badge", BadgeClass);' },
18
+ { id: 'attach', code: 'var spawned = this.attachMovie("Badge", "spawned", 200); trace("ATTACHED "+spawned.fromClass);' },
19
+ { id: 'over', code: 'trace("over");' },
20
+ ].map(script => ({ ...script, swfVersion: version, language: 'actionscript' }));
21
+ return { stage: { width: 300, height: 200, frameRate: 12 }, scripts,
22
+ symbols: [{ id: 'badge', name: 'Badge', width: 200, height: 160, durationFrames: 3, loop, linkage: 'Badge', initScript: 'init',
23
+ elements: [{ ...art, id: 'badge-art' }] },
24
+ { id: 'btn', name: 'Clicker', width: 200, height: 160, button: true, durationFrames: 4,
25
+ elements: [{ ...art, id: 'button-art' }], scriptEvents: [{ on: ['rollOver'], script: 'over' }] }],
26
+ scenes: [{ id: 'scene', durationFrames: 2,
27
+ events: [{ trigger: 'frameEnter', frame: 1, actions: [{ type: 'script', script: 'attach' }] }],
28
+ elements: [{ ...art, id: 'paint', name: 'Paint', buttonAction: click }, instance('standard', 'badge'),
29
+ instance('override', 'badge', { symbolMode: loop ? 'once' : 'loop' }), instance('button', 'btn', { buttonAction: click })] }] };
30
+ }
31
+
32
+ async function inspect(bytes) {
33
+ const { doc } = await parse(bytes);
34
+ const { tags } = decode(doc, new DiagnosticsCollector());
35
+ const linkedId = tags.find(tag => tag.type === 'ExportAssets').assets.find(asset => asset.name === 'Badge').id;
36
+ const init = tags.find(tag => tag.type === 'DoInitAction' && tag.spriteId === linkedId);
37
+ const attach = tags.find(tag => tag.type === 'DoAction' && tag.bytecode.length > 10);
38
+ assert.ok(init && attach);
39
+ const positions = Object.fromEntries(tags.filter(tag => tag.type.startsWith('PlaceObject') && tag.name).map(tag => [tag.name, tag.characterId]));
40
+ assert.equal(positions.standard, linkedId);
41
+ assert.notEqual(positions.override, linkedId);
42
+ return { doc, init: Buffer.from(init.bytecode), attach: Buffer.from(attach.bytecode) };
43
+ }
44
+
45
+ for (const loop of [false, true]) {
46
+ test(`reimports generated artwork, button and ${loop ? 'once' : 'loop'} clips, preserving AVM1 linkage and handlers`, async () => {
47
+ const first = await exportProjectSwf(project(8, loop));
48
+ const original = await inspect(first.bytes);
49
+ assert.equal(readAuthoringMetadata(original.doc.tags).length, 3);
50
+ assert.ok(original.doc.tags[1].length < 400);
51
+ let bytes = first.bytes;
52
+ for (let pass = 0; pass < 2; pass++) {
53
+ const { project: stored } = await convertSwf(bytes, { name: 'Authored' });
54
+ assert.equal(stored.symbols.length, 2);
55
+ const elements = Object.fromEntries(stored.scenes[0].elements.map(e => [e.name, e]));
56
+ const badge = stored.symbols.find(symbol => symbol.linkage === 'Badge');
57
+ assert.equal(badge.loop, loop);
58
+ assert.ok(badge.initScript);
59
+ assert.equal(elements.standard.symbolId, badge.id);
60
+ assert.equal(elements.override.symbolId, badge.id);
61
+ assert.equal(elements.override.symbolMode, loop ? 'once' : 'loop');
62
+ assert.equal(elements.Paint.type, 'path');
63
+ assert.equal(elements.Paint.buttonAction.type, 'stop');
64
+ assert.equal(elements.button.buttonAction.type, 'stop');
65
+ assert.deepEqual(stored.symbols.find(s => s.button).scriptEvents.map(e => e.on), [['rollOver']]);
66
+ ({ bytes } = await exportProjectSwf(stored));
67
+ const next = await inspect(bytes);
68
+ assert.deepEqual(next.init, original.init);
69
+ assert.deepEqual(next.attach, original.attach);
70
+ }
71
+ });
72
+ }
73
+
74
+ test('malformed and stale XMP leave the native definitions available', async () => {
75
+ const { bytes } = await exportProjectSwf(project(8, false));
76
+ const { doc } = await parse(bytes);
77
+ for (const damaged of [TAG.Metadata, TAG.SetBackgroundColor]) {
78
+ const tags = doc.tags.map(tag => {
79
+ const data = tag.data.slice();
80
+ if (tag.code === damaged) data[0] ^= 1;
81
+ return encodeTag(tag.code, data);
82
+ });
83
+ const edited = writeSwf({ ...doc.header, tags });
84
+ assert.deepEqual(readAuthoringMetadata((await parse(edited)).doc.tags), []);
85
+ const { project: stored } = await convertSwf(edited, { name: 'Edited' });
86
+ assert.equal(stored.scenes[0].elements.find(e => e.name === 'Paint').type, 'symbol');
87
+ const badge = stored.symbols.find(s => s.linkage === 'Badge');
88
+ assert.notEqual(stored.scenes[0].elements.find(e => e.name === 'override').symbolId, badge.id);
89
+ assert.ok(badge.initScript);
90
+ }
91
+ });
92
+
93
+ test('legacy targets retain the SWF 6 publishing floor and ordinary native definitions', async () => {
94
+ for (const version of [5, 6, 7]) {
95
+ const { bytes } = await exportProjectSwf(project(version, false));
96
+ const { doc } = await inspect(bytes);
97
+ assert.equal(doc.header.version, Math.max(6, version));
98
+ assert.ok(!doc.tags.some(tag => tag.code === TAG.Metadata || tag.code === TAG.FileAttributes));
99
+ }
100
+ });
101
+
102
+ test('forged but correctly checksummed hints cannot replace a linked clip or unrelated artwork', async () => {
103
+ const { bytes } = await exportProjectSwf(project(8, false));
104
+ const original = await inspect(bytes);
105
+ const rows = readAuthoringMetadata(original.doc.tags);
106
+ const [sprite] = rows.filter(row => row[0] === 's');
107
+ const [button] = rows.filter(row => row[0] === 'b');
108
+ const [wrapper] = rows.filter(row => row[0] === 'w');
109
+ // Claim the linked base is disposable and the button draws a different character.
110
+ const forged = [['s', sprite[2], sprite[1], 1, 0], ['w', wrapper[1], button[2]]];
111
+ const tags = original.doc.tags.filter(tag => tag.code !== TAG.Metadata).map(tag => encodeTag(tag.code, tag.data));
112
+ const metadata = authoringMetadata(forged)(tags);
113
+ tags.splice(1, 0, encodeTag(TAG.Metadata, Buffer.from(metadata + '\0')));
114
+ const edited = writeSwf({ ...original.doc.header, tags });
115
+ const { project: stored } = await convertSwf(edited, { name: 'Forged' });
116
+ const elements = Object.fromEntries(stored.scenes[0].elements.map(e => [e.name, e]));
117
+ assert.equal(elements.Paint.type, 'symbol');
118
+ assert.notEqual(elements.standard.symbolId, elements.override.symbolId);
119
+ assert.equal(stored.symbols.find(s => s.linkage === 'Badge').id, elements.standard.symbolId);
120
+ });
121
+
122
+ test('an actual SWF 5 movie keeps its attachMovie call and library linkage on republish', async () => {
123
+ const source = project(5, false);
124
+ delete source.symbols[0].initScript;
125
+ source.scripts = source.scripts.filter(s => s.id !== 'init');
126
+ source.scripts.find(s => s.id === 'attach').code = 'var spawned = this.attachMovie("Badge","spawned",200); trace(spawned._name);';
127
+ const { doc } = await parse((await exportProjectSwf(source)).bytes);
128
+ const swf5 = writeSwf({ ...doc.header, version: 5, tags: doc.tags.map(tag => encodeTag(tag.code, tag.data)) });
129
+ async function attachment(bytes) {
130
+ const { doc } = await parse(bytes);
131
+ const { tags } = decode(doc, new DiagnosticsCollector());
132
+ assert.ok(tags.some(tag => tag.type === 'ExportAssets' && tag.assets.some(a => a.name === 'Badge')));
133
+ return Buffer.from(tags.find(tag => tag.type === 'DoAction' && tag.bytecode.length > 10).bytecode);
134
+ }
135
+ assert.equal((await parse(swf5)).doc.header.version, 5);
136
+ const { project: stored } = await convertSwf(swf5, { name: 'Legacy' });
137
+ const republished = await exportProjectSwf(stored);
138
+ assert.equal(republished.version, 6);
139
+ assert.deepEqual(await attachment(republished.bytes), await attachment(swf5));
140
+ });