@despia-native/kernel 0.0.0-stage → 0.3.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.
- package/LICENSE +202 -0
- package/NOTICE +1414 -0
- package/README.md +17 -2
- package/dist/.dsx-build-stamp.json +7 -0
- package/dist/api-cohort.d.ts +27 -0
- package/dist/api-cohort.d.ts.map +1 -0
- package/dist/api-cohort.js +94 -0
- package/dist/api-cohort.js.map +1 -0
- package/dist/api.d.ts +454 -0
- package/dist/api.d.ts.map +1 -0
- package/dist/api.js +2442 -0
- package/dist/api.js.map +1 -0
- package/dist/app-consts.d.ts +10 -0
- package/dist/app-consts.d.ts.map +1 -0
- package/dist/app-consts.js +39 -0
- package/dist/app-consts.js.map +1 -0
- package/dist/app-update.d.ts +12 -0
- package/dist/app-update.d.ts.map +1 -0
- package/dist/app-update.js +32 -0
- package/dist/app-update.js.map +1 -0
- package/dist/aria.d.ts +152 -0
- package/dist/aria.d.ts.map +1 -0
- package/dist/aria.js +524 -0
- package/dist/aria.js.map +1 -0
- package/dist/audio-core.d.ts +260 -0
- package/dist/audio-core.d.ts.map +1 -0
- package/dist/audio-core.js +1440 -0
- package/dist/audio-core.js.map +1 -0
- package/dist/audio-edit.d.ts +100 -0
- package/dist/audio-edit.d.ts.map +1 -0
- package/dist/audio-edit.js +489 -0
- package/dist/audio-edit.js.map +1 -0
- package/dist/audio-mix.d.ts +84 -0
- package/dist/audio-mix.d.ts.map +1 -0
- package/dist/audio-mix.js +393 -0
- package/dist/audio-mix.js.map +1 -0
- package/dist/auth-proof.d.ts +145 -0
- package/dist/auth-proof.d.ts.map +1 -0
- package/dist/auth-proof.js +644 -0
- package/dist/auth-proof.js.map +1 -0
- package/dist/availability-explain.d.ts +3 -0
- package/dist/availability-explain.d.ts.map +1 -0
- package/dist/availability-explain.js +322 -0
- package/dist/availability-explain.js.map +1 -0
- package/dist/availability.d.ts +246 -0
- package/dist/availability.d.ts.map +1 -0
- package/dist/availability.js +615 -0
- package/dist/availability.js.map +1 -0
- package/dist/background.d.ts +116 -0
- package/dist/background.d.ts.map +1 -0
- package/dist/background.js +235 -0
- package/dist/background.js.map +1 -0
- package/dist/boot-tables-flag.d.ts +2 -0
- package/dist/boot-tables-flag.d.ts.map +1 -0
- package/dist/boot-tables-flag.js +16 -0
- package/dist/boot-tables-flag.js.map +1 -0
- package/dist/boot-tables.d.ts +40 -0
- package/dist/boot-tables.d.ts.map +1 -0
- package/dist/boot-tables.js +102 -0
- package/dist/boot-tables.js.map +1 -0
- package/dist/border-edges.d.ts +5 -0
- package/dist/border-edges.d.ts.map +1 -0
- package/dist/border-edges.js +47 -0
- package/dist/border-edges.js.map +1 -0
- package/dist/bus.d.ts +1203 -0
- package/dist/bus.d.ts.map +1 -0
- package/dist/bus.js +2647 -0
- package/dist/bus.js.map +1 -0
- package/dist/calendar-core.d.ts +143 -0
- package/dist/calendar-core.d.ts.map +1 -0
- package/dist/calendar-core.js +372 -0
- package/dist/calendar-core.js.map +1 -0
- package/dist/call-value.d.ts +16 -0
- package/dist/call-value.d.ts.map +1 -0
- package/dist/call-value.js +54 -0
- package/dist/call-value.js.map +1 -0
- package/dist/camera-core.d.ts +222 -0
- package/dist/camera-core.d.ts.map +1 -0
- package/dist/camera-core.js +980 -0
- package/dist/camera-core.js.map +1 -0
- package/dist/canvas-core.d.ts +404 -0
- package/dist/canvas-core.d.ts.map +1 -0
- package/dist/canvas-core.js +2186 -0
- package/dist/canvas-core.js.map +1 -0
- package/dist/capture-core.d.ts +74 -0
- package/dist/capture-core.d.ts.map +1 -0
- package/dist/capture-core.js +158 -0
- package/dist/capture-core.js.map +1 -0
- package/dist/compile/codegen.d.ts +146 -0
- package/dist/compile/codegen.d.ts.map +1 -0
- package/dist/compile/codegen.js +1949 -0
- package/dist/compile/codegen.js.map +1 -0
- package/dist/compile/jstier.d.ts +44 -0
- package/dist/compile/jstier.d.ts.map +1 -0
- package/dist/compile/jstier.js +181 -0
- package/dist/compile/jstier.js.map +1 -0
- package/dist/compile/tier-door.d.ts +41 -0
- package/dist/compile/tier-door.d.ts.map +1 -0
- package/dist/compile/tier-door.js +167 -0
- package/dist/compile/tier-door.js.map +1 -0
- package/dist/compile/tier-realm.d.ts +49 -0
- package/dist/compile/tier-realm.d.ts.map +1 -0
- package/dist/compile/tier-realm.js +368 -0
- package/dist/compile/tier-realm.js.map +1 -0
- package/dist/compile/tier.d.ts +23 -0
- package/dist/compile/tier.d.ts.map +1 -0
- package/dist/compile/tier.js +157 -0
- package/dist/compile/tier.js.map +1 -0
- package/dist/compose-core.d.ts +86 -0
- package/dist/compose-core.d.ts.map +1 -0
- package/dist/compose-core.js +112 -0
- package/dist/compose-core.js.map +1 -0
- package/dist/config-validate.d.ts +31 -0
- package/dist/config-validate.d.ts.map +1 -0
- package/dist/config-validate.js +147 -0
- package/dist/config-validate.js.map +1 -0
- package/dist/config-values.d.ts +106 -0
- package/dist/config-values.d.ts.map +1 -0
- package/dist/config-values.js +340 -0
- package/dist/config-values.js.map +1 -0
- package/dist/contacts-core.d.ts +127 -0
- package/dist/contacts-core.d.ts.map +1 -0
- package/dist/contacts-core.js +211 -0
- package/dist/contacts-core.js.map +1 -0
- package/dist/content-transition.d.ts +58 -0
- package/dist/content-transition.d.ts.map +1 -0
- package/dist/content-transition.js +94 -0
- package/dist/content-transition.js.map +1 -0
- package/dist/controls-core.d.ts +227 -0
- package/dist/controls-core.d.ts.map +1 -0
- package/dist/controls-core.js +652 -0
- package/dist/controls-core.js.map +1 -0
- package/dist/cookies.d.ts +78 -0
- package/dist/cookies.d.ts.map +1 -0
- package/dist/cookies.js +265 -0
- package/dist/cookies.js.map +1 -0
- package/dist/corner-shape-core.d.ts +69 -0
- package/dist/corner-shape-core.d.ts.map +1 -0
- package/dist/corner-shape-core.js +261 -0
- package/dist/corner-shape-core.js.map +1 -0
- package/dist/crypto-core.d.ts +57 -0
- package/dist/crypto-core.d.ts.map +1 -0
- package/dist/crypto-core.js +128 -0
- package/dist/crypto-core.js.map +1 -0
- package/dist/dataviz.d.ts +436 -0
- package/dist/dataviz.d.ts.map +1 -0
- package/dist/dataviz.js +956 -0
- package/dist/dataviz.js.map +1 -0
- package/dist/debug.d.ts +293 -0
- package/dist/debug.d.ts.map +1 -0
- package/dist/debug.js +767 -0
- package/dist/debug.js.map +1 -0
- package/dist/device-ownership.d.ts +50 -0
- package/dist/device-ownership.d.ts.map +1 -0
- package/dist/device-ownership.js +105 -0
- package/dist/device-ownership.js.map +1 -0
- package/dist/effects.d.ts +86 -0
- package/dist/effects.d.ts.map +1 -0
- package/dist/effects.js +98 -0
- package/dist/effects.js.map +1 -0
- package/dist/element.d.ts +108 -0
- package/dist/element.d.ts.map +1 -0
- package/dist/element.js +63 -0
- package/dist/element.js.map +1 -0
- package/dist/file-watch.d.ts +16 -0
- package/dist/file-watch.d.ts.map +1 -0
- package/dist/file-watch.js +49 -0
- package/dist/file-watch.js.map +1 -0
- package/dist/filepaths.d.ts +72 -0
- package/dist/filepaths.d.ts.map +1 -0
- package/dist/filepaths.js +271 -0
- package/dist/filepaths.js.map +1 -0
- package/dist/film.d.ts +264 -0
- package/dist/film.d.ts.map +1 -0
- package/dist/film.js +313 -0
- package/dist/film.js.map +1 -0
- package/dist/fonts.d.ts +66 -0
- package/dist/fonts.d.ts.map +1 -0
- package/dist/fonts.js +135 -0
- package/dist/fonts.js.map +1 -0
- package/dist/forms.d.ts +191 -0
- package/dist/forms.d.ts.map +1 -0
- package/dist/forms.js +663 -0
- package/dist/forms.js.map +1 -0
- package/dist/geo.d.ts +289 -0
- package/dist/geo.d.ts.map +1 -0
- package/dist/geo.js +460 -0
- package/dist/geo.js.map +1 -0
- package/dist/handler-announce.d.ts +46 -0
- package/dist/handler-announce.d.ts.map +1 -0
- package/dist/handler-announce.js +96 -0
- package/dist/handler-announce.js.map +1 -0
- package/dist/handoff.d.ts +71 -0
- package/dist/handoff.d.ts.map +1 -0
- package/dist/handoff.js +250 -0
- package/dist/handoff.js.map +1 -0
- package/dist/identity-durability.d.ts +76 -0
- package/dist/identity-durability.d.ts.map +1 -0
- package/dist/identity-durability.js +150 -0
- package/dist/identity-durability.js.map +1 -0
- package/dist/image-core.d.ts +240 -0
- package/dist/image-core.d.ts.map +1 -0
- package/dist/image-core.js +618 -0
- package/dist/image-core.js.map +1 -0
- package/dist/image-schemes.d.ts +10 -0
- package/dist/image-schemes.d.ts.map +1 -0
- package/dist/image-schemes.js +28 -0
- package/dist/image-schemes.js.map +1 -0
- package/dist/index.d.ts +143 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +299 -0
- package/dist/index.js.map +1 -0
- package/dist/ink-core.d.ts +53 -0
- package/dist/ink-core.d.ts.map +1 -0
- package/dist/ink-core.js +119 -0
- package/dist/ink-core.js.map +1 -0
- package/dist/input.d.ts +148 -0
- package/dist/input.d.ts.map +1 -0
- package/dist/input.js +358 -0
- package/dist/input.js.map +1 -0
- package/dist/integrity.d.ts +63 -0
- package/dist/integrity.d.ts.map +1 -0
- package/dist/integrity.js +150 -0
- package/dist/integrity.js.map +1 -0
- package/dist/intents.d.ts +95 -0
- package/dist/intents.d.ts.map +1 -0
- package/dist/intents.js +282 -0
- package/dist/intents.js.map +1 -0
- package/dist/interpolate-core.d.ts +37 -0
- package/dist/interpolate-core.d.ts.map +1 -0
- package/dist/interpolate-core.js +443 -0
- package/dist/interpolate-core.js.map +1 -0
- package/dist/jse/color.d.ts +12 -0
- package/dist/jse/color.d.ts.map +1 -0
- package/dist/jse/color.js +196 -0
- package/dist/jse/color.js.map +1 -0
- package/dist/jse/core.d.ts +54 -0
- package/dist/jse/core.d.ts.map +1 -0
- package/dist/jse/core.js +1519 -0
- package/dist/jse/core.js.map +1 -0
- package/dist/jse/dispatch.d.ts +28 -0
- package/dist/jse/dispatch.d.ts.map +1 -0
- package/dist/jse/dispatch.js +116 -0
- package/dist/jse/dispatch.js.map +1 -0
- package/dist/jse/highlight-grammar.d.ts +15 -0
- package/dist/jse/highlight-grammar.d.ts.map +1 -0
- package/dist/jse/highlight-grammar.js +161 -0
- package/dist/jse/highlight-grammar.js.map +1 -0
- package/dist/jse/highlight-grammars.generated.d.ts +2 -0
- package/dist/jse/highlight-grammars.generated.d.ts.map +1 -0
- package/dist/jse/highlight-grammars.generated.js +4 -0
- package/dist/jse/highlight-grammars.generated.js.map +1 -0
- package/dist/jse/highlight.d.ts +37 -0
- package/dist/jse/highlight.d.ts.map +1 -0
- package/dist/jse/highlight.js +323 -0
- package/dist/jse/highlight.js.map +1 -0
- package/dist/jse/jse.d.ts +674 -0
- package/dist/jse/jse.d.ts.map +1 -0
- package/dist/jse/jse.js +5841 -0
- package/dist/jse/jse.js.map +1 -0
- package/dist/jse/pathmatch.d.ts +10 -0
- package/dist/jse/pathmatch.d.ts.map +1 -0
- package/dist/jse/pathmatch.js +73 -0
- package/dist/jse/pathmatch.js.map +1 -0
- package/dist/jse/regex.d.ts +33 -0
- package/dist/jse/regex.d.ts.map +1 -0
- package/dist/jse/regex.js +375 -0
- package/dist/jse/regex.js.map +1 -0
- package/dist/jse/stdlib.d.ts +8 -0
- package/dist/jse/stdlib.d.ts.map +1 -0
- package/dist/jse/stdlib.js +765 -0
- package/dist/jse/stdlib.js.map +1 -0
- package/dist/jse/tokens.d.ts +102 -0
- package/dist/jse/tokens.d.ts.map +1 -0
- package/dist/jse/tokens.js +982 -0
- package/dist/jse/tokens.js.map +1 -0
- package/dist/jse/values.d.ts +206 -0
- package/dist/jse/values.d.ts.map +1 -0
- package/dist/jse/values.js +846 -0
- package/dist/jse/values.js.map +1 -0
- package/dist/keyboard.d.ts +196 -0
- package/dist/keyboard.d.ts.map +1 -0
- package/dist/keyboard.js +186 -0
- package/dist/keyboard.js.map +1 -0
- package/dist/label-choices.d.ts +3 -0
- package/dist/label-choices.d.ts.map +1 -0
- package/dist/label-choices.js +168 -0
- package/dist/label-choices.js.map +1 -0
- package/dist/language.d.ts +33 -0
- package/dist/language.d.ts.map +1 -0
- package/dist/language.js +50 -0
- package/dist/language.js.map +1 -0
- package/dist/length-units.d.ts +30 -0
- package/dist/length-units.d.ts.map +1 -0
- package/dist/length-units.js +83 -0
- package/dist/length-units.js.map +1 -0
- package/dist/lightsensor.d.ts +48 -0
- package/dist/lightsensor.d.ts.map +1 -0
- package/dist/lightsensor.js +97 -0
- package/dist/lightsensor.js.map +1 -0
- package/dist/link-refresh.d.ts +27 -0
- package/dist/link-refresh.d.ts.map +1 -0
- package/dist/link-refresh.js +166 -0
- package/dist/link-refresh.js.map +1 -0
- package/dist/link.d.ts +57 -0
- package/dist/link.d.ts.map +1 -0
- package/dist/link.js +333 -0
- package/dist/link.js.map +1 -0
- package/dist/linkpreview.d.ts +114 -0
- package/dist/linkpreview.d.ts.map +1 -0
- package/dist/linkpreview.js +439 -0
- package/dist/linkpreview.js.map +1 -0
- package/dist/list-presence.d.ts +32 -0
- package/dist/list-presence.d.ts.map +1 -0
- package/dist/list-presence.js +48 -0
- package/dist/list-presence.js.map +1 -0
- package/dist/live-stream.d.ts +27 -0
- package/dist/live-stream.d.ts.map +1 -0
- package/dist/live-stream.js +274 -0
- package/dist/live-stream.js.map +1 -0
- package/dist/livelogs.d.ts +134 -0
- package/dist/livelogs.d.ts.map +1 -0
- package/dist/livelogs.js +409 -0
- package/dist/livelogs.js.map +1 -0
- package/dist/logs.d.ts +80 -0
- package/dist/logs.d.ts.map +1 -0
- package/dist/logs.js +206 -0
- package/dist/logs.js.map +1 -0
- package/dist/mcp/apps.d.ts +82 -0
- package/dist/mcp/apps.d.ts.map +1 -0
- package/dist/mcp/apps.js +219 -0
- package/dist/mcp/apps.js.map +1 -0
- package/dist/mcp/entities.d.ts +25 -0
- package/dist/mcp/entities.d.ts.map +1 -0
- package/dist/mcp/entities.js +71 -0
- package/dist/mcp/entities.js.map +1 -0
- package/dist/mcp/result.d.ts +73 -0
- package/dist/mcp/result.d.ts.map +1 -0
- package/dist/mcp/result.js +119 -0
- package/dist/mcp/result.js.map +1 -0
- package/dist/mcp/webmcp.d.ts +162 -0
- package/dist/mcp/webmcp.d.ts.map +1 -0
- package/dist/mcp/webmcp.js +214 -0
- package/dist/mcp/webmcp.js.map +1 -0
- package/dist/mcp.d.ts +5 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +14 -0
- package/dist/mcp.js.map +1 -0
- package/dist/media-core.d.ts +191 -0
- package/dist/media-core.d.ts.map +1 -0
- package/dist/media-core.js +446 -0
- package/dist/media-core.js.map +1 -0
- package/dist/media-timeline-core.d.ts +94 -0
- package/dist/media-timeline-core.d.ts.map +1 -0
- package/dist/media-timeline-core.js +620 -0
- package/dist/media-timeline-core.js.map +1 -0
- package/dist/message.d.ts +42 -0
- package/dist/message.d.ts.map +1 -0
- package/dist/message.js +582 -0
- package/dist/message.js.map +1 -0
- package/dist/missing-element.d.ts +29 -0
- package/dist/missing-element.d.ts.map +1 -0
- package/dist/missing-element.js +76 -0
- package/dist/missing-element.js.map +1 -0
- package/dist/motion-core.d.ts +160 -0
- package/dist/motion-core.d.ts.map +1 -0
- package/dist/motion-core.js +752 -0
- package/dist/motion-core.js.map +1 -0
- package/dist/motion-pose.d.ts +65 -0
- package/dist/motion-pose.d.ts.map +1 -0
- package/dist/motion-pose.js +143 -0
- package/dist/motion-pose.js.map +1 -0
- package/dist/motion.d.ts +132 -0
- package/dist/motion.d.ts.map +1 -0
- package/dist/motion.js +260 -0
- package/dist/motion.js.map +1 -0
- package/dist/mount-core.d.ts +203 -0
- package/dist/mount-core.d.ts.map +1 -0
- package/dist/mount-core.js +437 -0
- package/dist/mount-core.js.map +1 -0
- package/dist/net-core.d.ts +110 -0
- package/dist/net-core.d.ts.map +1 -0
- package/dist/net-core.js +225 -0
- package/dist/net-core.js.map +1 -0
- package/dist/notify.d.ts +399 -0
- package/dist/notify.d.ts.map +1 -0
- package/dist/notify.js +964 -0
- package/dist/notify.js.map +1 -0
- package/dist/orientation-binding.d.ts +39 -0
- package/dist/orientation-binding.d.ts.map +1 -0
- package/dist/orientation-binding.js +64 -0
- package/dist/orientation-binding.js.map +1 -0
- package/dist/orientation.d.ts +55 -0
- package/dist/orientation.d.ts.map +1 -0
- package/dist/orientation.js +89 -0
- package/dist/orientation.js.map +1 -0
- package/dist/ota.d.ts +52 -0
- package/dist/ota.d.ts.map +1 -0
- package/dist/ota.js +122 -0
- package/dist/ota.js.map +1 -0
- package/dist/passkeys.d.ts +91 -0
- package/dist/passkeys.d.ts.map +1 -0
- package/dist/passkeys.js +307 -0
- package/dist/passkeys.js.map +1 -0
- package/dist/payment.d.ts +85 -0
- package/dist/payment.d.ts.map +1 -0
- package/dist/payment.js +270 -0
- package/dist/payment.js.map +1 -0
- package/dist/pending-presenter.d.ts +31 -0
- package/dist/pending-presenter.d.ts.map +1 -0
- package/dist/pending-presenter.js +119 -0
- package/dist/pending-presenter.js.map +1 -0
- package/dist/permission-core.d.ts +59 -0
- package/dist/permission-core.d.ts.map +1 -0
- package/dist/permission-core.js +102 -0
- package/dist/permission-core.js.map +1 -0
- package/dist/persist.d.ts +169 -0
- package/dist/persist.d.ts.map +1 -0
- package/dist/persist.js +361 -0
- package/dist/persist.js.map +1 -0
- package/dist/phase-plan.d.ts +114 -0
- package/dist/phase-plan.d.ts.map +1 -0
- package/dist/phase-plan.js +837 -0
- package/dist/phase-plan.js.map +1 -0
- package/dist/playback-core.d.ts +64 -0
- package/dist/playback-core.d.ts.map +1 -0
- package/dist/playback-core.js +176 -0
- package/dist/playback-core.js.map +1 -0
- package/dist/player-sources.d.ts +12 -0
- package/dist/player-sources.d.ts.map +1 -0
- package/dist/player-sources.js +44 -0
- package/dist/player-sources.js.map +1 -0
- package/dist/proxy-denylist.d.ts +11 -0
- package/dist/proxy-denylist.d.ts.map +1 -0
- package/dist/proxy-denylist.js +44 -0
- package/dist/proxy-denylist.js.map +1 -0
- package/dist/public-config.d.ts +20 -0
- package/dist/public-config.d.ts.map +1 -0
- package/dist/public-config.js +109 -0
- package/dist/public-config.js.map +1 -0
- package/dist/qoe-core.d.ts +72 -0
- package/dist/qoe-core.d.ts.map +1 -0
- package/dist/qoe-core.js +113 -0
- package/dist/qoe-core.js.map +1 -0
- package/dist/reactive-request.d.ts +80 -0
- package/dist/reactive-request.d.ts.map +1 -0
- package/dist/reactive-request.js +192 -0
- package/dist/reactive-request.js.map +1 -0
- package/dist/read-cache.d.ts +129 -0
- package/dist/read-cache.d.ts.map +1 -0
- package/dist/read-cache.js +566 -0
- package/dist/read-cache.js.map +1 -0
- package/dist/recorder-core.d.ts +57 -0
- package/dist/recorder-core.d.ts.map +1 -0
- package/dist/recorder-core.js +136 -0
- package/dist/recorder-core.js.map +1 -0
- package/dist/ref.d.ts +41 -0
- package/dist/ref.d.ts.map +1 -0
- package/dist/ref.js +98 -0
- package/dist/ref.js.map +1 -0
- package/dist/refusal.d.ts +82 -0
- package/dist/refusal.d.ts.map +1 -0
- package/dist/refusal.js +218 -0
- package/dist/refusal.js.map +1 -0
- package/dist/region-insets.d.ts +61 -0
- package/dist/region-insets.d.ts.map +1 -0
- package/dist/region-insets.js +67 -0
- package/dist/region-insets.js.map +1 -0
- package/dist/rive-core.d.ts +323 -0
- package/dist/rive-core.d.ts.map +1 -0
- package/dist/rive-core.js +677 -0
- package/dist/rive-core.js.map +1 -0
- package/dist/runner.d.ts +595 -0
- package/dist/runner.d.ts.map +1 -0
- package/dist/runner.js +4094 -0
- package/dist/runner.js.map +1 -0
- package/dist/scene/a11y.d.ts +11 -0
- package/dist/scene/a11y.d.ts.map +1 -0
- package/dist/scene/a11y.js +67 -0
- package/dist/scene/a11y.js.map +1 -0
- package/dist/scene/anim.d.ts +149 -0
- package/dist/scene/anim.d.ts.map +1 -0
- package/dist/scene/anim.js +396 -0
- package/dist/scene/anim.js.map +1 -0
- package/dist/scene/bind.d.ts +33 -0
- package/dist/scene/bind.d.ts.map +1 -0
- package/dist/scene/bind.js +66 -0
- package/dist/scene/bind.js.map +1 -0
- package/dist/scene/collide.d.ts +56 -0
- package/dist/scene/collide.d.ts.map +1 -0
- package/dist/scene/collide.js +146 -0
- package/dist/scene/collide.js.map +1 -0
- package/dist/scene/frame.d.ts +22 -0
- package/dist/scene/frame.d.ts.map +1 -0
- package/dist/scene/frame.js +57 -0
- package/dist/scene/frame.js.map +1 -0
- package/dist/scene/gltf.d.ts +79 -0
- package/dist/scene/gltf.d.ts.map +1 -0
- package/dist/scene/gltf.js +409 -0
- package/dist/scene/gltf.js.map +1 -0
- package/dist/scene/ir.d.ts +293 -0
- package/dist/scene/ir.d.ts.map +1 -0
- package/dist/scene/ir.js +692 -0
- package/dist/scene/ir.js.map +1 -0
- package/dist/scene/math.d.ts +50 -0
- package/dist/scene/math.d.ts.map +1 -0
- package/dist/scene/math.js +180 -0
- package/dist/scene/math.js.map +1 -0
- package/dist/scene/orbit.d.ts +19 -0
- package/dist/scene/orbit.d.ts.map +1 -0
- package/dist/scene/orbit.js +60 -0
- package/dist/scene/orbit.js.map +1 -0
- package/dist/scene/physics.d.ts +228 -0
- package/dist/scene/physics.d.ts.map +1 -0
- package/dist/scene/physics.js +1500 -0
- package/dist/scene/physics.js.map +1 -0
- package/dist/scene/registry.d.ts +85 -0
- package/dist/scene/registry.d.ts.map +1 -0
- package/dist/scene/registry.js +75 -0
- package/dist/scene/registry.js.map +1 -0
- package/dist/scene/skin.d.ts +82 -0
- package/dist/scene/skin.d.ts.map +1 -0
- package/dist/scene/skin.js +351 -0
- package/dist/scene/skin.js.map +1 -0
- package/dist/scene/sprite.d.ts +36 -0
- package/dist/scene/sprite.d.ts.map +1 -0
- package/dist/scene/sprite.js +107 -0
- package/dist/scene/sprite.js.map +1 -0
- package/dist/scene/viewer.d.ts +96 -0
- package/dist/scene/viewer.d.ts.map +1 -0
- package/dist/scene/viewer.js +235 -0
- package/dist/scene/viewer.js.map +1 -0
- package/dist/scoped-slots.d.ts +9 -0
- package/dist/scoped-slots.d.ts.map +1 -0
- package/dist/scoped-slots.js +42 -0
- package/dist/scoped-slots.js.map +1 -0
- package/dist/screen.d.ts +109 -0
- package/dist/screen.d.ts.map +1 -0
- package/dist/screen.js +309 -0
- package/dist/screen.js.map +1 -0
- package/dist/scroll.d.ts +525 -0
- package/dist/scroll.d.ts.map +1 -0
- package/dist/scroll.js +1226 -0
- package/dist/scroll.js.map +1 -0
- package/dist/settings-plane.d.ts +58 -0
- package/dist/settings-plane.d.ts.map +1 -0
- package/dist/settings-plane.js +86 -0
- package/dist/settings-plane.js.map +1 -0
- package/dist/share-inbox.d.ts +56 -0
- package/dist/share-inbox.d.ts.map +1 -0
- package/dist/share-inbox.js +114 -0
- package/dist/share-inbox.js.map +1 -0
- package/dist/shared-transition.d.ts +152 -0
- package/dist/shared-transition.d.ts.map +1 -0
- package/dist/shared-transition.js +257 -0
- package/dist/shared-transition.js.map +1 -0
- package/dist/sheet-content-padding.d.ts +24 -0
- package/dist/sheet-content-padding.d.ts.map +1 -0
- package/dist/sheet-content-padding.js +32 -0
- package/dist/sheet-content-padding.js.map +1 -0
- package/dist/shot-devices.d.ts +36 -0
- package/dist/shot-devices.d.ts.map +1 -0
- package/dist/shot-devices.js +90 -0
- package/dist/shot-devices.js.map +1 -0
- package/dist/shot-guards.d.ts +100 -0
- package/dist/shot-guards.d.ts.map +1 -0
- package/dist/shot-guards.js +260 -0
- package/dist/shot-guards.js.map +1 -0
- package/dist/shot-scope.d.ts +140 -0
- package/dist/shot-scope.d.ts.map +1 -0
- package/dist/shot-scope.js +302 -0
- package/dist/shot-scope.js.map +1 -0
- package/dist/slot-partition.d.ts +9 -0
- package/dist/slot-partition.d.ts.map +1 -0
- package/dist/slot-partition.js +38 -0
- package/dist/slot-partition.js.map +1 -0
- package/dist/snackbar.d.ts +165 -0
- package/dist/snackbar.d.ts.map +1 -0
- package/dist/snackbar.js +220 -0
- package/dist/snackbar.js.map +1 -0
- package/dist/socket.d.ts +96 -0
- package/dist/socket.d.ts.map +1 -0
- package/dist/socket.js +370 -0
- package/dist/socket.js.map +1 -0
- package/dist/spotlight.d.ts +77 -0
- package/dist/spotlight.d.ts.map +1 -0
- package/dist/spotlight.js +174 -0
- package/dist/spotlight.js.map +1 -0
- package/dist/state-primitive.d.ts +37 -0
- package/dist/state-primitive.d.ts.map +1 -0
- package/dist/state-primitive.js +91 -0
- package/dist/state-primitive.js.map +1 -0
- package/dist/store-query.d.ts +25 -0
- package/dist/store-query.d.ts.map +1 -0
- package/dist/store-query.js +88 -0
- package/dist/store-query.js.map +1 -0
- package/dist/store-value.d.ts +48 -0
- package/dist/store-value.d.ts.map +1 -0
- package/dist/store-value.js +264 -0
- package/dist/store-value.js.map +1 -0
- package/dist/store-versioning.d.ts +70 -0
- package/dist/store-versioning.d.ts.map +1 -0
- package/dist/store-versioning.js +188 -0
- package/dist/store-versioning.js.map +1 -0
- package/dist/store.d.ts +282 -0
- package/dist/store.d.ts.map +1 -0
- package/dist/store.js +1144 -0
- package/dist/store.js.map +1 -0
- package/dist/strings.d.ts +141 -0
- package/dist/strings.d.ts.map +1 -0
- package/dist/strings.js +581 -0
- package/dist/strings.js.map +1 -0
- package/dist/telemetry.d.ts +95 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +255 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/transform-core.d.ts +228 -0
- package/dist/transform-core.d.ts.map +1 -0
- package/dist/transform-core.js +921 -0
- package/dist/transform-core.js.map +1 -0
- package/dist/transition-core.d.ts +58 -0
- package/dist/transition-core.d.ts.map +1 -0
- package/dist/transition-core.js +352 -0
- package/dist/transition-core.js.map +1 -0
- package/dist/trim-trailing.d.ts +3 -0
- package/dist/trim-trailing.d.ts.map +1 -0
- package/dist/trim-trailing.js +14 -0
- package/dist/trim-trailing.js.map +1 -0
- package/dist/upload.d.ts +51 -0
- package/dist/upload.d.ts.map +1 -0
- package/dist/upload.js +68 -0
- package/dist/upload.js.map +1 -0
- package/dist/vendor-face.d.ts +44 -0
- package/dist/vendor-face.d.ts.map +1 -0
- package/dist/vendor-face.js +80 -0
- package/dist/vendor-face.js.map +1 -0
- package/dist/vendor-session.d.ts +166 -0
- package/dist/vendor-session.d.ts.map +1 -0
- package/dist/vendor-session.js +423 -0
- package/dist/vendor-session.js.map +1 -0
- package/dist/vendor-surface.d.ts +290 -0
- package/dist/vendor-surface.d.ts.map +1 -0
- package/dist/vendor-surface.js +439 -0
- package/dist/vendor-surface.js.map +1 -0
- package/dist/version-rules.generated.d.ts +17 -0
- package/dist/version-rules.generated.d.ts.map +1 -0
- package/dist/version-rules.generated.js +99 -0
- package/dist/version-rules.generated.js.map +1 -0
- package/dist/view-transition-core.d.ts +99 -0
- package/dist/view-transition-core.d.ts.map +1 -0
- package/dist/view-transition-core.js +127 -0
- package/dist/view-transition-core.js.map +1 -0
- package/dist/view-transition-door.d.ts +26 -0
- package/dist/view-transition-door.d.ts.map +1 -0
- package/dist/view-transition-door.js +88 -0
- package/dist/view-transition-door.js.map +1 -0
- package/dist/window-identity.d.ts +3 -0
- package/dist/window-identity.d.ts.map +1 -0
- package/dist/window-identity.js +27 -0
- package/dist/window-identity.js.map +1 -0
- package/package.json +139 -4
|
@@ -0,0 +1,2186 @@
|
|
|
1
|
+
//
|
|
2
|
+
// canvas-core.ts - the shared `<canvas>` core: the SVG path-data parser, the 2-D
|
|
3
|
+
// transform plane, fill-rule resolution, gradient normalisation, the tier-1 display
|
|
4
|
+
// list (+ its keyed diff and SVG serialisation), the tier-2 command recorder, the
|
|
5
|
+
// `on:frame` schedule and the accessibility fold. The law is the corpus,
|
|
6
|
+
// OpenSource/Conformance/canvas/ (parity/U04-canvas.md); the Kotlin twin is :core
|
|
7
|
+
// CanvasCore.kt and the Swift twin is Engine/Swift/CanvasCore.swift.
|
|
8
|
+
//
|
|
9
|
+
// WHY THIS IS A SHARED CORE AND NOT THREE RASTERISERS. Three rasterisers will never be
|
|
10
|
+
// bit-identical, so the determinism split is deliberate: PIXELS are toleranced and belong
|
|
11
|
+
// to the platform (GraphicsContext / DrawScope / canvas 2D), GEOMETRY is exact and lives
|
|
12
|
+
// here. A path's normalized segment list, a transform's composed matrix, a gradient's
|
|
13
|
+
// stop offsets, a display list's keys and diff, a tier-2 command stream and the frame
|
|
14
|
+
// budget are all pure data folds, and a renderer that disagrees with one of them draws a
|
|
15
|
+
// different picture no tolerance can excuse.
|
|
16
|
+
//
|
|
17
|
+
export const CANVAS_IDENTITY = [1, 0, 0, 1, 0, 0];
|
|
18
|
+
/** world = parentWorld · local, the same fold the scene kernel uses one dimension up */
|
|
19
|
+
export function canvasMatMul(a, b) {
|
|
20
|
+
return [
|
|
21
|
+
a[0] * b[0] + a[2] * b[1],
|
|
22
|
+
a[1] * b[0] + a[3] * b[1],
|
|
23
|
+
a[0] * b[2] + a[2] * b[3],
|
|
24
|
+
a[1] * b[2] + a[3] * b[3],
|
|
25
|
+
a[0] * b[4] + a[2] * b[5] + a[4],
|
|
26
|
+
a[1] * b[4] + a[3] * b[5] + a[5],
|
|
27
|
+
];
|
|
28
|
+
}
|
|
29
|
+
export function canvasApply(m, x, y) {
|
|
30
|
+
return [m[0] * x + m[2] * y + m[4], m[1] * x + m[3] * y + m[5]];
|
|
31
|
+
}
|
|
32
|
+
export function canvasMatrixIsIdentity(m) {
|
|
33
|
+
return m[0] === 1 && m[1] === 0 && m[2] === 0 && m[3] === 1 && m[4] === 0 && m[5] === 0;
|
|
34
|
+
}
|
|
35
|
+
const DEG = Math.PI / 180;
|
|
36
|
+
function num(value, fallback) {
|
|
37
|
+
if (typeof value === "number")
|
|
38
|
+
return Number.isFinite(value) ? value : fallback;
|
|
39
|
+
if (typeof value === "string") {
|
|
40
|
+
const parsed = Number(value.trim());
|
|
41
|
+
return value.trim() !== "" && Number.isFinite(parsed) ? parsed : fallback;
|
|
42
|
+
}
|
|
43
|
+
return fallback;
|
|
44
|
+
}
|
|
45
|
+
const COMMANDS = "MmLlHhVvCcSsQqTtAaZz";
|
|
46
|
+
class PathScanner {
|
|
47
|
+
i = 0;
|
|
48
|
+
s;
|
|
49
|
+
constructor(source) {
|
|
50
|
+
this.s = source;
|
|
51
|
+
}
|
|
52
|
+
isSep(c) {
|
|
53
|
+
return c === " " || c === "\t" || c === "\n" || c === "\r" || c === "\f" || c === ",";
|
|
54
|
+
}
|
|
55
|
+
skipSep() {
|
|
56
|
+
while (this.i < this.s.length && this.isSep(this.s[this.i]))
|
|
57
|
+
this.i += 1;
|
|
58
|
+
}
|
|
59
|
+
atEnd() {
|
|
60
|
+
this.skipSep();
|
|
61
|
+
return this.i >= this.s.length;
|
|
62
|
+
}
|
|
63
|
+
peek() {
|
|
64
|
+
return this.s[this.i] ?? "";
|
|
65
|
+
}
|
|
66
|
+
advance() {
|
|
67
|
+
this.i += 1;
|
|
68
|
+
}
|
|
69
|
+
/** the SVG number grammar: sign, digit runs, a bare leading '.', and exponents.
|
|
70
|
+
* '.5.5' is deliberately TWO numbers — a second '.' terminates the first. */
|
|
71
|
+
number() {
|
|
72
|
+
this.skipSep();
|
|
73
|
+
const start = this.i;
|
|
74
|
+
if (this.peek() === "+" || this.peek() === "-")
|
|
75
|
+
this.i += 1;
|
|
76
|
+
let digits = 0;
|
|
77
|
+
while (this.i < this.s.length && this.s[this.i] >= "0" && this.s[this.i] <= "9") {
|
|
78
|
+
this.i += 1;
|
|
79
|
+
digits += 1;
|
|
80
|
+
}
|
|
81
|
+
if (this.peek() === ".") {
|
|
82
|
+
this.i += 1;
|
|
83
|
+
while (this.i < this.s.length && this.s[this.i] >= "0" && this.s[this.i] <= "9") {
|
|
84
|
+
this.i += 1;
|
|
85
|
+
digits += 1;
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
if (digits === 0) {
|
|
89
|
+
this.i = start;
|
|
90
|
+
return null;
|
|
91
|
+
}
|
|
92
|
+
const e = this.peek();
|
|
93
|
+
if (e === "e" || e === "E") {
|
|
94
|
+
const mark = this.i;
|
|
95
|
+
this.i += 1;
|
|
96
|
+
if (this.peek() === "+" || this.peek() === "-")
|
|
97
|
+
this.i += 1;
|
|
98
|
+
let expDigits = 0;
|
|
99
|
+
while (this.i < this.s.length && this.s[this.i] >= "0" && this.s[this.i] <= "9") {
|
|
100
|
+
this.i += 1;
|
|
101
|
+
expDigits += 1;
|
|
102
|
+
}
|
|
103
|
+
if (expDigits === 0)
|
|
104
|
+
this.i = mark;
|
|
105
|
+
}
|
|
106
|
+
const value = Number(this.s.slice(start, this.i));
|
|
107
|
+
if (!Number.isFinite(value)) {
|
|
108
|
+
this.i = start;
|
|
109
|
+
return null;
|
|
110
|
+
}
|
|
111
|
+
return value;
|
|
112
|
+
}
|
|
113
|
+
/** the two arc flags are SINGLE characters, so `a5 5 0 0110 0` is fa=0 fs=1 x=10 y=0 */
|
|
114
|
+
flag() {
|
|
115
|
+
this.skipSep();
|
|
116
|
+
const c = this.peek();
|
|
117
|
+
if (c === "0" || c === "1") {
|
|
118
|
+
this.i += 1;
|
|
119
|
+
return c === "1" ? 1 : 0;
|
|
120
|
+
}
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
}
|
|
124
|
+
/** k for a cubic approximating a δ-radian elliptical sweep */
|
|
125
|
+
function arcK(delta) {
|
|
126
|
+
return (4 / 3) * Math.tan(delta / 4);
|
|
127
|
+
}
|
|
128
|
+
/** centre parameterisation → 1..4 cubics, split at ceil(|Δθ| / 90°) */
|
|
129
|
+
function arcCentreToCubics(cx, cy, rx, ry, phi, theta1, dtheta) {
|
|
130
|
+
const out = [];
|
|
131
|
+
const cosPhi = Math.cos(phi);
|
|
132
|
+
const sinPhi = Math.sin(phi);
|
|
133
|
+
const point = (t) => {
|
|
134
|
+
const x = rx * Math.cos(t);
|
|
135
|
+
const y = ry * Math.sin(t);
|
|
136
|
+
return [cx + cosPhi * x - sinPhi * y, cy + sinPhi * x + cosPhi * y];
|
|
137
|
+
};
|
|
138
|
+
const deriv = (t) => {
|
|
139
|
+
const x = -rx * Math.sin(t);
|
|
140
|
+
const y = ry * Math.cos(t);
|
|
141
|
+
return [cosPhi * x - sinPhi * y, sinPhi * x + cosPhi * y];
|
|
142
|
+
};
|
|
143
|
+
const count = Math.max(1, Math.ceil(Math.abs(dtheta) / (Math.PI / 2) - 1e-9));
|
|
144
|
+
const step = dtheta / count;
|
|
145
|
+
const k = arcK(step);
|
|
146
|
+
for (let i = 0; i < count; i += 1) {
|
|
147
|
+
const t1 = theta1 + step * i;
|
|
148
|
+
const t2 = t1 + step;
|
|
149
|
+
const p1 = point(t1);
|
|
150
|
+
const p2 = point(t2);
|
|
151
|
+
const d1 = deriv(t1);
|
|
152
|
+
const d2 = deriv(t2);
|
|
153
|
+
out.push(["C",
|
|
154
|
+
p1[0] + k * d1[0], p1[1] + k * d1[1],
|
|
155
|
+
p2[0] - k * d2[0], p2[1] - k * d2[1],
|
|
156
|
+
p2[0], p2[1],
|
|
157
|
+
]);
|
|
158
|
+
}
|
|
159
|
+
return out;
|
|
160
|
+
}
|
|
161
|
+
/** the F.6.5 endpoint→centre conversion plus the F.6.6 corrections: rx = |rx|, ry = |ry|;
|
|
162
|
+
* identical endpoints emit NOTHING; a zero radius degenerates to a line; radii too small
|
|
163
|
+
* are scaled by sqrt(Λ). */
|
|
164
|
+
export function canvasArcToCubics(x1, y1, rxIn, ryIn, phiDeg, largeArc, sweep, x2, y2) {
|
|
165
|
+
if (x1 === x2 && y1 === y2)
|
|
166
|
+
return [];
|
|
167
|
+
let rx = Math.abs(rxIn);
|
|
168
|
+
let ry = Math.abs(ryIn);
|
|
169
|
+
if (rx === 0 || ry === 0)
|
|
170
|
+
return [["L", x2, y2]];
|
|
171
|
+
const phi = ((phiDeg % 360) + 360) % 360 * DEG;
|
|
172
|
+
const cosPhi = Math.cos(phi);
|
|
173
|
+
const sinPhi = Math.sin(phi);
|
|
174
|
+
const dx = (x1 - x2) / 2;
|
|
175
|
+
const dy = (y1 - y2) / 2;
|
|
176
|
+
const x1p = cosPhi * dx + sinPhi * dy;
|
|
177
|
+
const y1p = -sinPhi * dx + cosPhi * dy;
|
|
178
|
+
const lambda = (x1p * x1p) / (rx * rx) + (y1p * y1p) / (ry * ry);
|
|
179
|
+
if (lambda > 1) {
|
|
180
|
+
const scale = Math.sqrt(lambda);
|
|
181
|
+
rx *= scale;
|
|
182
|
+
ry *= scale;
|
|
183
|
+
}
|
|
184
|
+
const rx2 = rx * rx;
|
|
185
|
+
const ry2 = ry * ry;
|
|
186
|
+
const numerator = Math.max(0, rx2 * ry2 - rx2 * y1p * y1p - ry2 * x1p * x1p);
|
|
187
|
+
const denominator = rx2 * y1p * y1p + ry2 * x1p * x1p;
|
|
188
|
+
const coefficient = denominator === 0 ? 0 : Math.sqrt(numerator / denominator);
|
|
189
|
+
const sign = largeArc === sweep ? -1 : 1;
|
|
190
|
+
const cxp = sign * coefficient * ((rx * y1p) / ry);
|
|
191
|
+
const cyp = sign * coefficient * (-(ry * x1p) / rx);
|
|
192
|
+
const cx = cosPhi * cxp - sinPhi * cyp + (x1 + x2) / 2;
|
|
193
|
+
const cy = sinPhi * cxp + cosPhi * cyp + (y1 + y2) / 2;
|
|
194
|
+
const theta1 = Math.atan2((y1p - cyp) / ry, (x1p - cxp) / rx);
|
|
195
|
+
const theta2 = Math.atan2((-y1p - cyp) / ry, (-x1p - cxp) / rx);
|
|
196
|
+
let dtheta = theta2 - theta1;
|
|
197
|
+
if (sweep === 0 && dtheta > 0)
|
|
198
|
+
dtheta -= 2 * Math.PI;
|
|
199
|
+
else if (sweep === 1 && dtheta < 0)
|
|
200
|
+
dtheta += 2 * Math.PI;
|
|
201
|
+
return arcCentreToCubics(cx, cy, rx, ry, phi, theta1, dtheta);
|
|
202
|
+
}
|
|
203
|
+
/**
|
|
204
|
+
* The SVG path-data mini-language → the normalized ABSOLUTE segment list.
|
|
205
|
+
*
|
|
206
|
+
* ARTICLE 7: a malformed token STOPS the parse, keeps every segment already produced and
|
|
207
|
+
* emits exactly ONE diagnostic — never a crash, never a partial number.
|
|
208
|
+
*/
|
|
209
|
+
export function parseCanvasPath(d) {
|
|
210
|
+
const segments = [];
|
|
211
|
+
const diagnostics = [];
|
|
212
|
+
const source = typeof d === "string" ? d : "";
|
|
213
|
+
const scan = new PathScanner(source);
|
|
214
|
+
let cmd = null;
|
|
215
|
+
let cx = 0;
|
|
216
|
+
let cy = 0;
|
|
217
|
+
let sx = 0;
|
|
218
|
+
let sy = 0;
|
|
219
|
+
let lastCubic = null;
|
|
220
|
+
let lastQuad = null;
|
|
221
|
+
let reopen = false;
|
|
222
|
+
const bad = (message) => {
|
|
223
|
+
diagnostics.push(message);
|
|
224
|
+
};
|
|
225
|
+
const openIfNeeded = () => {
|
|
226
|
+
if (!reopen)
|
|
227
|
+
return;
|
|
228
|
+
segments.push(["M", cx, cy]);
|
|
229
|
+
reopen = false;
|
|
230
|
+
};
|
|
231
|
+
while (!scan.atEnd()) {
|
|
232
|
+
const head = scan.peek();
|
|
233
|
+
if (/[A-Za-z]/.test(head)) {
|
|
234
|
+
if (!COMMANDS.includes(head)) {
|
|
235
|
+
bad(`canvas.path: unknown command '${head}'`);
|
|
236
|
+
break;
|
|
237
|
+
}
|
|
238
|
+
cmd = head;
|
|
239
|
+
scan.advance();
|
|
240
|
+
}
|
|
241
|
+
else {
|
|
242
|
+
if (cmd === null) {
|
|
243
|
+
bad("canvas.path: data does not start with a command");
|
|
244
|
+
break;
|
|
245
|
+
}
|
|
246
|
+
if (cmd === "M")
|
|
247
|
+
cmd = "L";
|
|
248
|
+
else if (cmd === "m")
|
|
249
|
+
cmd = "l";
|
|
250
|
+
else if (cmd === "Z" || cmd === "z") {
|
|
251
|
+
bad("canvas.path: an argument after closepath");
|
|
252
|
+
break;
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
const rel = cmd === cmd.toLowerCase();
|
|
256
|
+
const upper = cmd.toUpperCase();
|
|
257
|
+
let failed = false;
|
|
258
|
+
const need = () => {
|
|
259
|
+
const v = scan.number();
|
|
260
|
+
if (v === null) {
|
|
261
|
+
failed = true;
|
|
262
|
+
return 0;
|
|
263
|
+
}
|
|
264
|
+
return v;
|
|
265
|
+
};
|
|
266
|
+
if (upper === "Z") {
|
|
267
|
+
segments.push(["Z"]);
|
|
268
|
+
cx = sx;
|
|
269
|
+
cy = sy;
|
|
270
|
+
lastCubic = null;
|
|
271
|
+
lastQuad = null;
|
|
272
|
+
reopen = true;
|
|
273
|
+
}
|
|
274
|
+
else if (upper === "M") {
|
|
275
|
+
const x = need();
|
|
276
|
+
const y = need();
|
|
277
|
+
if (failed) {
|
|
278
|
+
bad("canvas.path: truncated moveto");
|
|
279
|
+
break;
|
|
280
|
+
}
|
|
281
|
+
cx = rel ? cx + x : x;
|
|
282
|
+
cy = rel ? cy + y : y;
|
|
283
|
+
sx = cx;
|
|
284
|
+
sy = cy;
|
|
285
|
+
segments.push(["M", cx, cy]);
|
|
286
|
+
lastCubic = null;
|
|
287
|
+
lastQuad = null;
|
|
288
|
+
reopen = false;
|
|
289
|
+
}
|
|
290
|
+
else if (upper === "L") {
|
|
291
|
+
const x = need();
|
|
292
|
+
const y = need();
|
|
293
|
+
if (failed) {
|
|
294
|
+
bad("canvas.path: truncated lineto");
|
|
295
|
+
break;
|
|
296
|
+
}
|
|
297
|
+
openIfNeeded();
|
|
298
|
+
cx = rel ? cx + x : x;
|
|
299
|
+
cy = rel ? cy + y : y;
|
|
300
|
+
segments.push(["L", cx, cy]);
|
|
301
|
+
lastCubic = null;
|
|
302
|
+
lastQuad = null;
|
|
303
|
+
}
|
|
304
|
+
else if (upper === "H") {
|
|
305
|
+
const x = need();
|
|
306
|
+
if (failed) {
|
|
307
|
+
bad("canvas.path: truncated horizontal lineto");
|
|
308
|
+
break;
|
|
309
|
+
}
|
|
310
|
+
openIfNeeded();
|
|
311
|
+
cx = rel ? cx + x : x;
|
|
312
|
+
segments.push(["L", cx, cy]);
|
|
313
|
+
lastCubic = null;
|
|
314
|
+
lastQuad = null;
|
|
315
|
+
}
|
|
316
|
+
else if (upper === "V") {
|
|
317
|
+
const y = need();
|
|
318
|
+
if (failed) {
|
|
319
|
+
bad("canvas.path: truncated vertical lineto");
|
|
320
|
+
break;
|
|
321
|
+
}
|
|
322
|
+
openIfNeeded();
|
|
323
|
+
cy = rel ? cy + y : y;
|
|
324
|
+
segments.push(["L", cx, cy]);
|
|
325
|
+
lastCubic = null;
|
|
326
|
+
lastQuad = null;
|
|
327
|
+
}
|
|
328
|
+
else if (upper === "C" || upper === "S") {
|
|
329
|
+
let c1x;
|
|
330
|
+
let c1y;
|
|
331
|
+
if (upper === "S") {
|
|
332
|
+
const reflected = lastCubic ?? [cx, cy];
|
|
333
|
+
c1x = 2 * cx - reflected[0];
|
|
334
|
+
c1y = 2 * cy - reflected[1];
|
|
335
|
+
}
|
|
336
|
+
else {
|
|
337
|
+
c1x = need();
|
|
338
|
+
c1y = need();
|
|
339
|
+
if (!failed) {
|
|
340
|
+
c1x = rel ? cx + c1x : c1x;
|
|
341
|
+
c1y = rel ? cy + c1y : c1y;
|
|
342
|
+
}
|
|
343
|
+
}
|
|
344
|
+
let c2x = need();
|
|
345
|
+
let c2y = need();
|
|
346
|
+
let x = need();
|
|
347
|
+
let y = need();
|
|
348
|
+
if (failed) {
|
|
349
|
+
bad("canvas.path: truncated cubic");
|
|
350
|
+
break;
|
|
351
|
+
}
|
|
352
|
+
c2x = rel ? cx + c2x : c2x;
|
|
353
|
+
c2y = rel ? cy + c2y : c2y;
|
|
354
|
+
x = rel ? cx + x : x;
|
|
355
|
+
y = rel ? cy + y : y;
|
|
356
|
+
openIfNeeded();
|
|
357
|
+
segments.push(["C", c1x, c1y, c2x, c2y, x, y]);
|
|
358
|
+
cx = x;
|
|
359
|
+
cy = y;
|
|
360
|
+
lastCubic = [c2x, c2y];
|
|
361
|
+
lastQuad = null;
|
|
362
|
+
}
|
|
363
|
+
else if (upper === "Q" || upper === "T") {
|
|
364
|
+
let qx;
|
|
365
|
+
let qy;
|
|
366
|
+
if (upper === "T") {
|
|
367
|
+
const reflected = lastQuad ?? [cx, cy];
|
|
368
|
+
qx = 2 * cx - reflected[0];
|
|
369
|
+
qy = 2 * cy - reflected[1];
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
qx = need();
|
|
373
|
+
qy = need();
|
|
374
|
+
if (!failed) {
|
|
375
|
+
qx = rel ? cx + qx : qx;
|
|
376
|
+
qy = rel ? cy + qy : qy;
|
|
377
|
+
}
|
|
378
|
+
}
|
|
379
|
+
let x = need();
|
|
380
|
+
let y = need();
|
|
381
|
+
if (failed) {
|
|
382
|
+
bad("canvas.path: truncated quadratic");
|
|
383
|
+
break;
|
|
384
|
+
}
|
|
385
|
+
x = rel ? cx + x : x;
|
|
386
|
+
y = rel ? cy + y : y;
|
|
387
|
+
openIfNeeded();
|
|
388
|
+
segments.push(["Q", qx, qy, x, y]);
|
|
389
|
+
cx = x;
|
|
390
|
+
cy = y;
|
|
391
|
+
lastQuad = [qx, qy];
|
|
392
|
+
lastCubic = null;
|
|
393
|
+
}
|
|
394
|
+
else if (upper === "A") {
|
|
395
|
+
const rx = need();
|
|
396
|
+
const ry = need();
|
|
397
|
+
const rot = need();
|
|
398
|
+
const fa = scan.flag();
|
|
399
|
+
const fs = scan.flag();
|
|
400
|
+
if (fa === null || fs === null)
|
|
401
|
+
failed = true;
|
|
402
|
+
let x = need();
|
|
403
|
+
let y = need();
|
|
404
|
+
if (failed) {
|
|
405
|
+
bad("canvas.path: truncated arc");
|
|
406
|
+
break;
|
|
407
|
+
}
|
|
408
|
+
x = rel ? cx + x : x;
|
|
409
|
+
y = rel ? cy + y : y;
|
|
410
|
+
const cubics = canvasArcToCubics(cx, cy, rx, ry, rot, fa, fs, x, y);
|
|
411
|
+
if (cubics.length > 0) {
|
|
412
|
+
openIfNeeded();
|
|
413
|
+
for (const seg of cubics)
|
|
414
|
+
segments.push(seg);
|
|
415
|
+
cx = x;
|
|
416
|
+
cy = y;
|
|
417
|
+
}
|
|
418
|
+
lastCubic = null;
|
|
419
|
+
lastQuad = null;
|
|
420
|
+
}
|
|
421
|
+
}
|
|
422
|
+
return { segments, diagnostics };
|
|
423
|
+
}
|
|
424
|
+
function cubicExtrema(p0, p1, p2, p3) {
|
|
425
|
+
const a = -p0 + 3 * p1 - 3 * p2 + p3;
|
|
426
|
+
const b = 2 * (p0 - 2 * p1 + p2);
|
|
427
|
+
const c = p1 - p0;
|
|
428
|
+
const roots = [];
|
|
429
|
+
if (Math.abs(a) < 1e-12) {
|
|
430
|
+
if (Math.abs(b) > 1e-12)
|
|
431
|
+
roots.push(-c / b);
|
|
432
|
+
}
|
|
433
|
+
else {
|
|
434
|
+
const disc = b * b - 4 * a * c;
|
|
435
|
+
if (disc >= 0) {
|
|
436
|
+
const s = Math.sqrt(disc);
|
|
437
|
+
roots.push((-b + s) / (2 * a), (-b - s) / (2 * a));
|
|
438
|
+
}
|
|
439
|
+
}
|
|
440
|
+
return roots.filter((t) => t > 0 && t < 1);
|
|
441
|
+
}
|
|
442
|
+
function cubicAt(p0, p1, p2, p3, t) {
|
|
443
|
+
const u = 1 - t;
|
|
444
|
+
return u * u * u * p0 + 3 * u * u * t * p1 + 3 * u * t * t * p2 + t * t * t * p3;
|
|
445
|
+
}
|
|
446
|
+
function quadAt(p0, p1, p2, t) {
|
|
447
|
+
const u = 1 - t;
|
|
448
|
+
return u * u * p0 + 2 * u * t * p1 + t * t * p2;
|
|
449
|
+
}
|
|
450
|
+
/** the TIGHT bounding box — the geometry half of the determinism split, exact on every
|
|
451
|
+
* renderer. null for an empty path. */
|
|
452
|
+
export function canvasPathBBox(segments) {
|
|
453
|
+
let minX = Infinity;
|
|
454
|
+
let minY = Infinity;
|
|
455
|
+
let maxX = -Infinity;
|
|
456
|
+
let maxY = -Infinity;
|
|
457
|
+
let seen = false;
|
|
458
|
+
let cx = 0;
|
|
459
|
+
let cy = 0;
|
|
460
|
+
let sx = 0;
|
|
461
|
+
let sy = 0;
|
|
462
|
+
const hit = (x, y) => {
|
|
463
|
+
seen = true;
|
|
464
|
+
if (x < minX)
|
|
465
|
+
minX = x;
|
|
466
|
+
if (y < minY)
|
|
467
|
+
minY = y;
|
|
468
|
+
if (x > maxX)
|
|
469
|
+
maxX = x;
|
|
470
|
+
if (y > maxY)
|
|
471
|
+
maxY = y;
|
|
472
|
+
};
|
|
473
|
+
for (const seg of segments) {
|
|
474
|
+
if (seg[0] === "M") {
|
|
475
|
+
cx = seg[1];
|
|
476
|
+
cy = seg[2];
|
|
477
|
+
sx = cx;
|
|
478
|
+
sy = cy;
|
|
479
|
+
hit(cx, cy);
|
|
480
|
+
}
|
|
481
|
+
else if (seg[0] === "L") {
|
|
482
|
+
cx = seg[1];
|
|
483
|
+
cy = seg[2];
|
|
484
|
+
hit(cx, cy);
|
|
485
|
+
}
|
|
486
|
+
else if (seg[0] === "Q") {
|
|
487
|
+
hit(seg[3], seg[4]);
|
|
488
|
+
for (const [p0, p1, p2, axis] of [
|
|
489
|
+
[cx, seg[1], seg[3], 0], [cy, seg[2], seg[4], 1],
|
|
490
|
+
]) {
|
|
491
|
+
const denominator = p0 - 2 * p1 + p2;
|
|
492
|
+
if (Math.abs(denominator) < 1e-12)
|
|
493
|
+
continue;
|
|
494
|
+
const t = (p0 - p1) / denominator;
|
|
495
|
+
if (t <= 0 || t >= 1)
|
|
496
|
+
continue;
|
|
497
|
+
const v = quadAt(p0, p1, p2, t);
|
|
498
|
+
if (axis === 0)
|
|
499
|
+
hit(v, cy);
|
|
500
|
+
else
|
|
501
|
+
hit(cx, v);
|
|
502
|
+
}
|
|
503
|
+
cx = seg[3];
|
|
504
|
+
cy = seg[4];
|
|
505
|
+
}
|
|
506
|
+
else if (seg[0] === "C") {
|
|
507
|
+
hit(seg[5], seg[6]);
|
|
508
|
+
for (const t of cubicExtrema(cx, seg[1], seg[3], seg[5])) {
|
|
509
|
+
hit(cubicAt(cx, seg[1], seg[3], seg[5], t), cy);
|
|
510
|
+
}
|
|
511
|
+
for (const t of cubicExtrema(cy, seg[2], seg[4], seg[6])) {
|
|
512
|
+
hit(cx, cubicAt(cy, seg[2], seg[4], seg[6], t));
|
|
513
|
+
}
|
|
514
|
+
cx = seg[5];
|
|
515
|
+
cy = seg[6];
|
|
516
|
+
}
|
|
517
|
+
else {
|
|
518
|
+
cx = sx;
|
|
519
|
+
cy = sy;
|
|
520
|
+
}
|
|
521
|
+
}
|
|
522
|
+
return seen ? [minX, minY, maxX, maxY] : null;
|
|
523
|
+
}
|
|
524
|
+
const TRANSFORM_ARITY = {
|
|
525
|
+
translate: [1, 2], scale: [1, 2], rotate: [1, 3], skewX: [1], skewY: [1], matrix: [6],
|
|
526
|
+
};
|
|
527
|
+
function transformFunction(name, a) {
|
|
528
|
+
switch (name) {
|
|
529
|
+
case "translate": return [1, 0, 0, 1, a[0], a.length > 1 ? a[1] : 0];
|
|
530
|
+
case "scale": return [a[0], 0, 0, a.length > 1 ? a[1] : a[0], 0, 0];
|
|
531
|
+
case "rotate": {
|
|
532
|
+
const r = a[0] * DEG;
|
|
533
|
+
const rot = [Math.cos(r), Math.sin(r), -Math.sin(r), Math.cos(r), 0, 0];
|
|
534
|
+
if (a.length === 1)
|
|
535
|
+
return rot;
|
|
536
|
+
const to = [1, 0, 0, 1, a[1], a[2]];
|
|
537
|
+
const back = [1, 0, 0, 1, -a[1], -a[2]];
|
|
538
|
+
return canvasMatMul(canvasMatMul(to, rot), back);
|
|
539
|
+
}
|
|
540
|
+
case "skewX": return [1, 0, Math.tan(a[0] * DEG), 1, 0, 0];
|
|
541
|
+
case "skewY": return [1, Math.tan(a[0] * DEG), 0, 1, 0, 0];
|
|
542
|
+
case "matrix": return [a[0], a[1], a[2], a[3], a[4], a[5]];
|
|
543
|
+
default: return null;
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
/** The next `name(args)` at or after `from`: the matches of
|
|
547
|
+
* `/([A-Za-z][A-Za-z0-9]*)\s*\(([\s\S]*?)\)/g`, found in one pass. A name runs to the end of its
|
|
548
|
+
* alphanumeric run, so a run either opens a call or no letter inside it does; and once no `)`
|
|
549
|
+
* follows an opener, no later call can close. The regex rediscovered both from every position. */
|
|
550
|
+
function nextTransformCall(source, from) {
|
|
551
|
+
let i = from;
|
|
552
|
+
while (i < source.length) {
|
|
553
|
+
if (!/[A-Za-z0-9]/.test(source[i])) {
|
|
554
|
+
i += 1;
|
|
555
|
+
continue;
|
|
556
|
+
}
|
|
557
|
+
let start = i;
|
|
558
|
+
while (i < source.length && /[A-Za-z0-9]/.test(source[i]))
|
|
559
|
+
i += 1;
|
|
560
|
+
while (start < i && /[0-9]/.test(source[start]))
|
|
561
|
+
start += 1;
|
|
562
|
+
if (start === i)
|
|
563
|
+
continue;
|
|
564
|
+
let open = i;
|
|
565
|
+
while (open < source.length && /\s/.test(source[open]))
|
|
566
|
+
open += 1;
|
|
567
|
+
if (source[open] !== "(")
|
|
568
|
+
continue;
|
|
569
|
+
const close = source.indexOf(")", open + 1);
|
|
570
|
+
if (close < 0)
|
|
571
|
+
return null;
|
|
572
|
+
return { index: start, end: close + 1, name: source.slice(start, i), args: source.slice(open + 1, close) };
|
|
573
|
+
}
|
|
574
|
+
return null;
|
|
575
|
+
}
|
|
576
|
+
/**
|
|
577
|
+
* The SVG function list, applied LEFT TO RIGHT: the leftmost function is outermost, so
|
|
578
|
+
* M = M1·M2·…·Mn and `translate(10,0) scale(2)` is NOT `scale(2) translate(10,0)`.
|
|
579
|
+
*
|
|
580
|
+
* ARTICLE 7: an unknown function, a wrong-arity function, a non-numeric argument or junk
|
|
581
|
+
* between functions costs exactly ONE diagnostic and is SKIPPED — every other function in
|
|
582
|
+
* the list still applies.
|
|
583
|
+
*/
|
|
584
|
+
export function parseCanvasTransform(input) {
|
|
585
|
+
const source = typeof input === "string" ? input : "";
|
|
586
|
+
const diagnostics = [];
|
|
587
|
+
let matrix = CANVAS_IDENTITY;
|
|
588
|
+
let cursor = 0;
|
|
589
|
+
const gap = (text) => {
|
|
590
|
+
if (text.replace(/[\s,]+/g, "") !== "")
|
|
591
|
+
diagnostics.push(`canvas.transform: junk '${text.trim()}'`);
|
|
592
|
+
};
|
|
593
|
+
for (;;) {
|
|
594
|
+
const match = nextTransformCall(source, cursor);
|
|
595
|
+
if (match === null)
|
|
596
|
+
break;
|
|
597
|
+
gap(source.slice(cursor, match.index));
|
|
598
|
+
cursor = match.end;
|
|
599
|
+
const name = match.name;
|
|
600
|
+
const arity = TRANSFORM_ARITY[name];
|
|
601
|
+
if (arity === undefined) {
|
|
602
|
+
diagnostics.push(`canvas.transform: unknown function '${name}'`);
|
|
603
|
+
continue;
|
|
604
|
+
}
|
|
605
|
+
const words = match.args.split(/[\s,]+/).filter((w) => w !== "");
|
|
606
|
+
if (!arity.includes(words.length)) {
|
|
607
|
+
diagnostics.push(`canvas.transform: ${name} takes ${arity.join(" or ")} arguments`);
|
|
608
|
+
continue;
|
|
609
|
+
}
|
|
610
|
+
const args = [];
|
|
611
|
+
let numeric = true;
|
|
612
|
+
for (const word of words) {
|
|
613
|
+
const value = Number(word);
|
|
614
|
+
if (!Number.isFinite(value)) {
|
|
615
|
+
numeric = false;
|
|
616
|
+
break;
|
|
617
|
+
}
|
|
618
|
+
args.push(value);
|
|
619
|
+
}
|
|
620
|
+
if (!numeric) {
|
|
621
|
+
diagnostics.push(`canvas.transform: ${name} has a non-numeric argument`);
|
|
622
|
+
continue;
|
|
623
|
+
}
|
|
624
|
+
const local = transformFunction(name, args);
|
|
625
|
+
if (local !== null)
|
|
626
|
+
matrix = canvasMatMul(matrix, local);
|
|
627
|
+
}
|
|
628
|
+
gap(source.slice(cursor));
|
|
629
|
+
return { matrix, diagnostics };
|
|
630
|
+
}
|
|
631
|
+
// ── flattening and the fill rules ────────────────────────────────────────────────────
|
|
632
|
+
/** uniform in t, never adaptive: adaptive subdivision is per-implementation and would
|
|
633
|
+
* make the fill-rule corpus unpinnable */
|
|
634
|
+
export const CANVAS_FLATTEN_SEGMENTS = 16;
|
|
635
|
+
/** every subpath, closed, as a point list */
|
|
636
|
+
export function canvasFlatten(segments) {
|
|
637
|
+
const out = [];
|
|
638
|
+
let current = null;
|
|
639
|
+
let cx = 0;
|
|
640
|
+
let cy = 0;
|
|
641
|
+
let sx = 0;
|
|
642
|
+
let sy = 0;
|
|
643
|
+
const push = (x, y) => {
|
|
644
|
+
if (current === null) {
|
|
645
|
+
current = [[x, y]];
|
|
646
|
+
out.push(current);
|
|
647
|
+
}
|
|
648
|
+
else
|
|
649
|
+
current.push([x, y]);
|
|
650
|
+
};
|
|
651
|
+
for (const seg of segments) {
|
|
652
|
+
if (seg[0] === "M") {
|
|
653
|
+
current = null;
|
|
654
|
+
cx = seg[1];
|
|
655
|
+
cy = seg[2];
|
|
656
|
+
sx = cx;
|
|
657
|
+
sy = cy;
|
|
658
|
+
push(cx, cy);
|
|
659
|
+
}
|
|
660
|
+
else if (seg[0] === "L") {
|
|
661
|
+
if (current === null)
|
|
662
|
+
push(cx, cy);
|
|
663
|
+
cx = seg[1];
|
|
664
|
+
cy = seg[2];
|
|
665
|
+
push(cx, cy);
|
|
666
|
+
}
|
|
667
|
+
else if (seg[0] === "Q") {
|
|
668
|
+
if (current === null)
|
|
669
|
+
push(cx, cy);
|
|
670
|
+
for (let i = 1; i <= CANVAS_FLATTEN_SEGMENTS; i += 1) {
|
|
671
|
+
const t = i / CANVAS_FLATTEN_SEGMENTS;
|
|
672
|
+
push(quadAt(cx, seg[1], seg[3], t), quadAt(cy, seg[2], seg[4], t));
|
|
673
|
+
}
|
|
674
|
+
cx = seg[3];
|
|
675
|
+
cy = seg[4];
|
|
676
|
+
}
|
|
677
|
+
else if (seg[0] === "C") {
|
|
678
|
+
if (current === null)
|
|
679
|
+
push(cx, cy);
|
|
680
|
+
for (let i = 1; i <= CANVAS_FLATTEN_SEGMENTS; i += 1) {
|
|
681
|
+
const t = i / CANVAS_FLATTEN_SEGMENTS;
|
|
682
|
+
push(cubicAt(cx, seg[1], seg[3], seg[5], t), cubicAt(cy, seg[2], seg[4], seg[6], t));
|
|
683
|
+
}
|
|
684
|
+
cx = seg[5];
|
|
685
|
+
cy = seg[6];
|
|
686
|
+
}
|
|
687
|
+
else {
|
|
688
|
+
cx = sx;
|
|
689
|
+
cy = sy;
|
|
690
|
+
current = null;
|
|
691
|
+
}
|
|
692
|
+
}
|
|
693
|
+
return out.filter((sub) => sub.length > 1);
|
|
694
|
+
}
|
|
695
|
+
/** the signed shoelace area of the FLATTENED path — positive is clockwise in the y-down
|
|
696
|
+
* canvas space. It pins the flattening itself, not just the inside/outside verdicts. */
|
|
697
|
+
export function canvasPathArea(segments) {
|
|
698
|
+
let total = 0;
|
|
699
|
+
for (const sub of canvasFlatten(segments)) {
|
|
700
|
+
for (let i = 0; i < sub.length; i += 1) {
|
|
701
|
+
const a = sub[i];
|
|
702
|
+
const b = sub[(i + 1) % sub.length];
|
|
703
|
+
total += a[0] * b[1] - b[0] * a[1];
|
|
704
|
+
}
|
|
705
|
+
}
|
|
706
|
+
return total / 2;
|
|
707
|
+
}
|
|
708
|
+
function crossingsAt(segments, px, py) {
|
|
709
|
+
let winding = 0;
|
|
710
|
+
let crossings = 0;
|
|
711
|
+
for (const sub of canvasFlatten(segments)) {
|
|
712
|
+
for (let i = 0; i < sub.length; i += 1) {
|
|
713
|
+
const a = sub[i];
|
|
714
|
+
const b = sub[(i + 1) % sub.length];
|
|
715
|
+
const up = a[1] <= py && py < b[1];
|
|
716
|
+
const down = b[1] <= py && py < a[1];
|
|
717
|
+
if (!up && !down)
|
|
718
|
+
continue;
|
|
719
|
+
const x = a[0] + ((py - a[1]) * (b[0] - a[0])) / (b[1] - a[1]);
|
|
720
|
+
if (!(x > px))
|
|
721
|
+
continue;
|
|
722
|
+
crossings += 1;
|
|
723
|
+
winding += up ? 1 : -1;
|
|
724
|
+
}
|
|
725
|
+
}
|
|
726
|
+
return { winding, crossings };
|
|
727
|
+
}
|
|
728
|
+
/** the winding number at a point (half-open in y, crossings strictly right of the point) */
|
|
729
|
+
export function canvasWindingAt(segments, x, y) {
|
|
730
|
+
return crossingsAt(segments, x, y).winding;
|
|
731
|
+
}
|
|
732
|
+
export function canvasCrossingsAt(segments, x, y) {
|
|
733
|
+
return crossingsAt(segments, x, y).crossings;
|
|
734
|
+
}
|
|
735
|
+
/** is this point inside, under `rule`? */
|
|
736
|
+
export function canvasContains(segments, x, y, rule) {
|
|
737
|
+
const c = crossingsAt(segments, x, y);
|
|
738
|
+
return rule === "evenodd" ? c.crossings % 2 === 1 : c.winding !== 0;
|
|
739
|
+
}
|
|
740
|
+
export const CANVAS_BLACK = { kind: "rgba", rgba: [0, 0, 0, 1] };
|
|
741
|
+
/** the semantic colour words (the StackStyle.color vocabulary) → their web custom
|
|
742
|
+
* property. `clear` is the one word with no token: it is literally transparent. */
|
|
743
|
+
export const CANVAS_COLOR_TOKENS = {
|
|
744
|
+
clear: "transparent",
|
|
745
|
+
label: "--dsx-label",
|
|
746
|
+
text: "--dsx-label",
|
|
747
|
+
secondary: "--dsx-secondary-label",
|
|
748
|
+
secondaryLabel: "--dsx-secondary-label",
|
|
749
|
+
tertiary: "--dsx-tertiary-label",
|
|
750
|
+
tertiaryLabel: "--dsx-tertiary-label",
|
|
751
|
+
quaternary: "--dsx-tertiary-label",
|
|
752
|
+
accent: "--dsx-accent",
|
|
753
|
+
destructive: "--dsx-destructive",
|
|
754
|
+
separator: "--dsx-separator",
|
|
755
|
+
fill: "--dsx-fill",
|
|
756
|
+
fillFaint: "--dsx-fill",
|
|
757
|
+
background: "--dsx-background",
|
|
758
|
+
systemBackground: "--dsx-background",
|
|
759
|
+
secondaryBackground: "--dsx-secondary-background",
|
|
760
|
+
tertiaryBackground: "--dsx-tertiary-background",
|
|
761
|
+
groupedBackground: "--dsx-grouped-background",
|
|
762
|
+
secondaryGroupedBackground: "--dsx-secondary-grouped-background",
|
|
763
|
+
};
|
|
764
|
+
const LITERAL_WORDS = {
|
|
765
|
+
white: [1, 1, 1, 1],
|
|
766
|
+
black: [0, 0, 0, 1],
|
|
767
|
+
};
|
|
768
|
+
export function parseCanvasPaint(input) {
|
|
769
|
+
const raw = typeof input === "string" ? input.trim() : "";
|
|
770
|
+
if (raw === "")
|
|
771
|
+
return { paint: null, ok: false };
|
|
772
|
+
if (raw === "none" || raw === "transparent")
|
|
773
|
+
return { paint: null, ok: true };
|
|
774
|
+
const url = /^url\(\s*#([^)\s]+)\s*\)$/.exec(raw);
|
|
775
|
+
if (url !== null)
|
|
776
|
+
return { paint: { kind: "gradient", id: url[1] }, ok: true };
|
|
777
|
+
if (Object.prototype.hasOwnProperty.call(LITERAL_WORDS, raw)) {
|
|
778
|
+
return { paint: { kind: "rgba", rgba: LITERAL_WORDS[raw] }, ok: true };
|
|
779
|
+
}
|
|
780
|
+
if (Object.prototype.hasOwnProperty.call(CANVAS_COLOR_TOKENS, raw)) {
|
|
781
|
+
return { paint: { kind: "token", token: raw }, ok: true };
|
|
782
|
+
}
|
|
783
|
+
const rgba = parseCanvasRgba(raw);
|
|
784
|
+
if (rgba !== null)
|
|
785
|
+
return { paint: { kind: "rgba", rgba }, ok: true };
|
|
786
|
+
return { paint: null, ok: false };
|
|
787
|
+
}
|
|
788
|
+
/** `#rgb` · `#rgba` · `#rrggbb` · `#rrggbbaa` · `rgb(r,g,b)` · `rgba(r,g,b,a)`, in
|
|
789
|
+
* STRAIGHT (non-premultiplied) sRGB, components 0..1 */
|
|
790
|
+
export function parseCanvasRgba(input) {
|
|
791
|
+
const raw = input.trim();
|
|
792
|
+
if (raw.startsWith("#")) {
|
|
793
|
+
const hex = raw.slice(1);
|
|
794
|
+
if (!/^[0-9a-fA-F]+$/.test(hex))
|
|
795
|
+
return null;
|
|
796
|
+
const wide = (n) => parseInt(hex[n] + hex[n], 16) / 255;
|
|
797
|
+
const pair = (n) => parseInt(hex.slice(n, n + 2), 16) / 255;
|
|
798
|
+
if (hex.length === 3)
|
|
799
|
+
return [wide(0), wide(1), wide(2), 1];
|
|
800
|
+
if (hex.length === 4)
|
|
801
|
+
return [wide(0), wide(1), wide(2), wide(3)];
|
|
802
|
+
if (hex.length === 6)
|
|
803
|
+
return [pair(0), pair(2), pair(4), 1];
|
|
804
|
+
if (hex.length === 8)
|
|
805
|
+
return [pair(0), pair(2), pair(4), pair(6)];
|
|
806
|
+
return null;
|
|
807
|
+
}
|
|
808
|
+
const call = /^rgba?\(([^)]*)\)$/i.exec(raw);
|
|
809
|
+
if (call === null)
|
|
810
|
+
return null;
|
|
811
|
+
const parts = call[1].split(/[,/\s]+/).filter((p) => p !== "");
|
|
812
|
+
if (parts.length < 3 || parts.length > 4)
|
|
813
|
+
return null;
|
|
814
|
+
const channel = (text) => {
|
|
815
|
+
const value = text.endsWith("%") ? Number(text.slice(0, -1)) * 2.55 : Number(text);
|
|
816
|
+
return Number.isFinite(value) ? Math.min(1, Math.max(0, value / 255)) : null;
|
|
817
|
+
};
|
|
818
|
+
const r = channel(parts[0]);
|
|
819
|
+
const g = channel(parts[1]);
|
|
820
|
+
const b = channel(parts[2]);
|
|
821
|
+
if (r === null || g === null || b === null)
|
|
822
|
+
return null;
|
|
823
|
+
let a = 1;
|
|
824
|
+
if (parts.length === 4) {
|
|
825
|
+
const text = parts[3];
|
|
826
|
+
const value = text.endsWith("%") ? Number(text.slice(0, -1)) / 100 : Number(text);
|
|
827
|
+
if (!Number.isFinite(value))
|
|
828
|
+
return null;
|
|
829
|
+
a = Math.min(1, Math.max(0, value));
|
|
830
|
+
}
|
|
831
|
+
return [r, g, b, a];
|
|
832
|
+
}
|
|
833
|
+
function parseOffset(value) {
|
|
834
|
+
if (value === null || value === undefined)
|
|
835
|
+
return null;
|
|
836
|
+
if (typeof value === "number")
|
|
837
|
+
return Number.isFinite(value) ? Math.min(1, Math.max(0, value)) : null;
|
|
838
|
+
const text = value.trim();
|
|
839
|
+
if (text === "")
|
|
840
|
+
return null;
|
|
841
|
+
const percent = text.endsWith("%");
|
|
842
|
+
const parsed = Number(percent ? text.slice(0, -1) : text);
|
|
843
|
+
if (!Number.isFinite(parsed))
|
|
844
|
+
return null;
|
|
845
|
+
return Math.min(1, Math.max(0, percent ? parsed / 100 : parsed));
|
|
846
|
+
}
|
|
847
|
+
/**
|
|
848
|
+
* THE LAW, in order: parse each offset (a number or a percentage) and CLAMP it into 0..1;
|
|
849
|
+
* a missing FIRST offset is 0 and a missing LAST is 1; a run of missing interior offsets
|
|
850
|
+
* spaces EVENLY between its two known neighbours; then walk left to right forcing
|
|
851
|
+
* MONOTONIC non-decreasing offsets (a decreasing offset is raised to its predecessor —
|
|
852
|
+
* never re-sorted, because author order is the paint order).
|
|
853
|
+
*/
|
|
854
|
+
export function normalizeCanvasStops(input) {
|
|
855
|
+
const diagnostics = [];
|
|
856
|
+
if (input.length === 0) {
|
|
857
|
+
diagnostics.push("canvas.gradient: no stops");
|
|
858
|
+
return { stops: [], diagnostics };
|
|
859
|
+
}
|
|
860
|
+
const offsets = input.map((s) => parseOffset(s.offset));
|
|
861
|
+
if (offsets[0] === null)
|
|
862
|
+
offsets[0] = 0;
|
|
863
|
+
if (offsets[offsets.length - 1] === null)
|
|
864
|
+
offsets[offsets.length - 1] = 1;
|
|
865
|
+
let i = 0;
|
|
866
|
+
while (i < offsets.length) {
|
|
867
|
+
if (offsets[i] !== null) {
|
|
868
|
+
i += 1;
|
|
869
|
+
continue;
|
|
870
|
+
}
|
|
871
|
+
let j = i;
|
|
872
|
+
while (j < offsets.length && offsets[j] === null)
|
|
873
|
+
j += 1;
|
|
874
|
+
const before = offsets[i - 1];
|
|
875
|
+
const after = offsets[j];
|
|
876
|
+
const span = j - i + 1;
|
|
877
|
+
for (let k = i; k < j; k += 1)
|
|
878
|
+
offsets[k] = before + ((after - before) * (k - i + 1)) / span;
|
|
879
|
+
i = j;
|
|
880
|
+
}
|
|
881
|
+
let previous = -Infinity;
|
|
882
|
+
const stops = [];
|
|
883
|
+
for (let k = 0; k < input.length; k += 1) {
|
|
884
|
+
const offset = Math.max(offsets[k], previous === -Infinity ? offsets[k] : previous);
|
|
885
|
+
previous = offset;
|
|
886
|
+
const raw = input[k];
|
|
887
|
+
const opacityValue = raw.opacity === null || raw.opacity === undefined ? 1 : num(raw.opacity, 1);
|
|
888
|
+
const opacity = Math.min(1, Math.max(0, opacityValue));
|
|
889
|
+
const parsed = parseCanvasPaint(raw.color);
|
|
890
|
+
if (parsed.paint !== null && parsed.paint.kind === "token") {
|
|
891
|
+
stops.push({ offset, token: parsed.paint.token, opacity });
|
|
892
|
+
continue;
|
|
893
|
+
}
|
|
894
|
+
if (parsed.paint !== null && parsed.paint.kind === "rgba") {
|
|
895
|
+
const c = parsed.paint.rgba;
|
|
896
|
+
stops.push({ offset, rgba: [c[0], c[1], c[2], c[3] * opacity] });
|
|
897
|
+
continue;
|
|
898
|
+
}
|
|
899
|
+
if (!parsed.ok)
|
|
900
|
+
diagnostics.push(`canvas.gradient: unparseable colour '${String(raw.color)}'`);
|
|
901
|
+
stops.push({ offset, rgba: [0, 0, 0, 1 * opacity] });
|
|
902
|
+
}
|
|
903
|
+
return { stops, diagnostics };
|
|
904
|
+
}
|
|
905
|
+
/** Sample the normalized stop list at t, interpolating componentwise in STRAIGHT
|
|
906
|
+
* (non-premultiplied) sRGB — not linear-light, not premultiplied, because that is what
|
|
907
|
+
* Canvas2D, CoreGraphics and Compose all do by default. Two stops at the SAME offset are
|
|
908
|
+
* a hard stop: the earlier stop owns the offset itself, the later one owns everything
|
|
909
|
+
* after it. A token stop cannot be sampled here — resolve the theme first. */
|
|
910
|
+
export function sampleCanvasStops(stops, t) {
|
|
911
|
+
if (stops.length === 0)
|
|
912
|
+
return null;
|
|
913
|
+
const rgbaOf = (s) => ("rgba" in s ? s.rgba : null);
|
|
914
|
+
if (t <= stops[0].offset)
|
|
915
|
+
return rgbaOf(stops[0]);
|
|
916
|
+
let index = -1;
|
|
917
|
+
for (let i = 1; i < stops.length; i += 1) {
|
|
918
|
+
if (t <= stops[i].offset) {
|
|
919
|
+
index = i;
|
|
920
|
+
break;
|
|
921
|
+
}
|
|
922
|
+
}
|
|
923
|
+
if (index === -1)
|
|
924
|
+
return rgbaOf(stops[stops.length - 1]);
|
|
925
|
+
const a = rgbaOf(stops[index - 1]);
|
|
926
|
+
const b = rgbaOf(stops[index]);
|
|
927
|
+
if (a === null || b === null)
|
|
928
|
+
return null;
|
|
929
|
+
const span = stops[index].offset - stops[index - 1].offset;
|
|
930
|
+
const u = span <= 0 ? 1 : (t - stops[index - 1].offset) / span;
|
|
931
|
+
return [
|
|
932
|
+
a[0] + (b[0] - a[0]) * u,
|
|
933
|
+
a[1] + (b[1] - a[1]) * u,
|
|
934
|
+
a[2] + (b[2] - a[2]) * u,
|
|
935
|
+
a[3] + (b[3] - a[3]) * u,
|
|
936
|
+
];
|
|
937
|
+
}
|
|
938
|
+
/** the 1-D paint coordinate for each gradient kind. linear is the CLAMPED projection onto
|
|
939
|
+
* the axis, radial the clamped distance ratio, angular the turn fraction from `start`,
|
|
940
|
+
* measured with atan2 in the y-down canvas space and wrapped into [0,1). */
|
|
941
|
+
export function canvasGradientT(kind, geom, x, y) {
|
|
942
|
+
if (kind === "linear") {
|
|
943
|
+
const [x1, y1, x2, y2] = [geom[0] ?? 0, geom[1] ?? 0, geom[2] ?? 0, geom[3] ?? 0];
|
|
944
|
+
const dx = x2 - x1;
|
|
945
|
+
const dy = y2 - y1;
|
|
946
|
+
const len2 = dx * dx + dy * dy;
|
|
947
|
+
if (len2 === 0)
|
|
948
|
+
return 0;
|
|
949
|
+
return Math.min(1, Math.max(0, ((x - x1) * dx + (y - y1) * dy) / len2));
|
|
950
|
+
}
|
|
951
|
+
const cx = geom[0] ?? 0;
|
|
952
|
+
const cy = geom[1] ?? 0;
|
|
953
|
+
if (kind === "radial") {
|
|
954
|
+
const r = geom[2] ?? 0;
|
|
955
|
+
if (r <= 0)
|
|
956
|
+
return 1;
|
|
957
|
+
return Math.min(1, Math.max(0, Math.hypot(x - cx, y - cy) / r));
|
|
958
|
+
}
|
|
959
|
+
const start = geom[2] ?? 0;
|
|
960
|
+
const degrees = Math.atan2(y - cy, x - cx) / DEG;
|
|
961
|
+
const turn = (degrees - start) / 360;
|
|
962
|
+
return turn - Math.floor(turn);
|
|
963
|
+
}
|
|
964
|
+
const SHAPE_KINDS = new Set(["path", "rect", "circle", "ellipse", "line", "polygon", "polyline"]);
|
|
965
|
+
const WRAPPER_KINDS = new Set(["group", "blur", "shadow", "blend"]);
|
|
966
|
+
/** the ONE kind whose unstyled default fill is absent rather than SVG black: a line has
|
|
967
|
+
* no interior to fill */
|
|
968
|
+
const UNFILLED_KINDS = new Set(["line"]);
|
|
969
|
+
function attr(node, name) {
|
|
970
|
+
return node.attrs === undefined ? undefined : node.attrs[name];
|
|
971
|
+
}
|
|
972
|
+
function attrText(node, name) {
|
|
973
|
+
const value = attr(node, name);
|
|
974
|
+
if (value === undefined || value === null)
|
|
975
|
+
return undefined;
|
|
976
|
+
return String(value);
|
|
977
|
+
}
|
|
978
|
+
function attrNum(node, name, fallback) {
|
|
979
|
+
const value = attr(node, name);
|
|
980
|
+
return value === undefined || value === null ? fallback : num(value, fallback);
|
|
981
|
+
}
|
|
982
|
+
function parsePoints(text) {
|
|
983
|
+
const words = text.split(/[\s,]+/).filter((w) => w !== "");
|
|
984
|
+
const out = [];
|
|
985
|
+
for (let i = 0; i + 1 < words.length; i += 2) {
|
|
986
|
+
const x = Number(words[i]);
|
|
987
|
+
const y = Number(words[i + 1]);
|
|
988
|
+
if (!Number.isFinite(x) || !Number.isFinite(y))
|
|
989
|
+
break;
|
|
990
|
+
out.push([x, y]);
|
|
991
|
+
}
|
|
992
|
+
return out;
|
|
993
|
+
}
|
|
994
|
+
/** rect/circle/ellipse/roundRect all become paths through the SAME arc→cubic converter as
|
|
995
|
+
* `d`, so a circle is four cubics with k = 4/3·tan(π/8) on every renderer */
|
|
996
|
+
export function canvasRectPath(x, y, w, h, rxIn, ryIn) {
|
|
997
|
+
const rx = Math.min(Math.abs(rxIn), Math.abs(w) / 2);
|
|
998
|
+
const ry = Math.min(Math.abs(ryIn), Math.abs(h) / 2);
|
|
999
|
+
if (rx <= 0 || ry <= 0) {
|
|
1000
|
+
return [["M", x, y], ["L", x + w, y], ["L", x + w, y + h], ["L", x, y + h], ["Z"]];
|
|
1001
|
+
}
|
|
1002
|
+
const out = [["M", x + rx, y], ["L", x + w - rx, y]];
|
|
1003
|
+
const corner = (x1, y1, x2, y2) => {
|
|
1004
|
+
for (const seg of canvasArcToCubics(x1, y1, rx, ry, 0, 0, 1, x2, y2))
|
|
1005
|
+
out.push(seg);
|
|
1006
|
+
};
|
|
1007
|
+
corner(x + w - rx, y, x + w, y + ry);
|
|
1008
|
+
out.push(["L", x + w, y + h - ry]);
|
|
1009
|
+
corner(x + w, y + h - ry, x + w - rx, y + h);
|
|
1010
|
+
out.push(["L", x + rx, y + h]);
|
|
1011
|
+
corner(x + rx, y + h, x, y + h - ry);
|
|
1012
|
+
out.push(["L", x, y + ry]);
|
|
1013
|
+
corner(x, y + ry, x + rx, y);
|
|
1014
|
+
out.push(["Z"]);
|
|
1015
|
+
return out;
|
|
1016
|
+
}
|
|
1017
|
+
export function canvasEllipsePath(cx, cy, rx, ry) {
|
|
1018
|
+
const out = [["M", cx + rx, cy]];
|
|
1019
|
+
for (const seg of arcCentreToCubics(cx, cy, rx, ry, 0, 0, 2 * Math.PI))
|
|
1020
|
+
out.push(seg);
|
|
1021
|
+
out.push(["Z"]);
|
|
1022
|
+
return out;
|
|
1023
|
+
}
|
|
1024
|
+
function shapePath(node, diagnostics) {
|
|
1025
|
+
switch (node.kind) {
|
|
1026
|
+
case "path": {
|
|
1027
|
+
const parsed = parseCanvasPath(attrText(node, "d"));
|
|
1028
|
+
for (const d of parsed.diagnostics)
|
|
1029
|
+
diagnostics.push(d);
|
|
1030
|
+
return parsed.segments;
|
|
1031
|
+
}
|
|
1032
|
+
case "rect": {
|
|
1033
|
+
const rxRaw = attr(node, "rx");
|
|
1034
|
+
const ryRaw = attr(node, "ry");
|
|
1035
|
+
const rx = rxRaw === undefined || rxRaw === null ? (ryRaw === undefined || ryRaw === null ? 0 : num(ryRaw, 0)) : num(rxRaw, 0);
|
|
1036
|
+
const ry = ryRaw === undefined || ryRaw === null ? rx : num(ryRaw, 0);
|
|
1037
|
+
return canvasRectPath(attrNum(node, "x", 0), attrNum(node, "y", 0), attrNum(node, "width", 0), attrNum(node, "height", 0), rx, ry);
|
|
1038
|
+
}
|
|
1039
|
+
case "circle": {
|
|
1040
|
+
const r = attrNum(node, "r", 0);
|
|
1041
|
+
return canvasEllipsePath(attrNum(node, "cx", 0), attrNum(node, "cy", 0), r, r);
|
|
1042
|
+
}
|
|
1043
|
+
case "ellipse":
|
|
1044
|
+
return canvasEllipsePath(attrNum(node, "cx", 0), attrNum(node, "cy", 0), attrNum(node, "rx", 0), attrNum(node, "ry", 0));
|
|
1045
|
+
case "line":
|
|
1046
|
+
return [
|
|
1047
|
+
["M", attrNum(node, "x1", 0), attrNum(node, "y1", 0)],
|
|
1048
|
+
["L", attrNum(node, "x2", 0), attrNum(node, "y2", 0)],
|
|
1049
|
+
];
|
|
1050
|
+
case "polygon":
|
|
1051
|
+
case "polyline": {
|
|
1052
|
+
const points = parsePoints(attrText(node, "points") ?? "");
|
|
1053
|
+
if (points.length === 0)
|
|
1054
|
+
return [];
|
|
1055
|
+
const out = [["M", points[0][0], points[0][1]]];
|
|
1056
|
+
for (let i = 1; i < points.length; i += 1)
|
|
1057
|
+
out.push(["L", points[i][0], points[i][1]]);
|
|
1058
|
+
if (node.kind === "polygon")
|
|
1059
|
+
out.push(["Z"]);
|
|
1060
|
+
return out;
|
|
1061
|
+
}
|
|
1062
|
+
default:
|
|
1063
|
+
return [];
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
function readInherited(node, parent, diagnostics) {
|
|
1067
|
+
const next = { ...parent };
|
|
1068
|
+
const paint = (name) => {
|
|
1069
|
+
const raw = attrText(node, name);
|
|
1070
|
+
if (raw === undefined)
|
|
1071
|
+
return undefined;
|
|
1072
|
+
const parsed = parseCanvasPaint(raw);
|
|
1073
|
+
if (!parsed.ok) {
|
|
1074
|
+
diagnostics.push(`canvas.${name}: unparseable colour '${raw}'`);
|
|
1075
|
+
return CANVAS_BLACK;
|
|
1076
|
+
}
|
|
1077
|
+
return parsed.paint;
|
|
1078
|
+
};
|
|
1079
|
+
const fill = paint("fill");
|
|
1080
|
+
if (fill !== undefined)
|
|
1081
|
+
next.fill = fill;
|
|
1082
|
+
const stroke = paint("stroke");
|
|
1083
|
+
if (stroke !== undefined)
|
|
1084
|
+
next.stroke = stroke;
|
|
1085
|
+
const width = attr(node, "strokeWidth");
|
|
1086
|
+
if (width !== undefined && width !== null)
|
|
1087
|
+
next.strokeWidth = num(width, 1);
|
|
1088
|
+
const rule = attrText(node, "fillRule");
|
|
1089
|
+
if (rule === "evenodd" || rule === "nonzero")
|
|
1090
|
+
next.fillRule = rule;
|
|
1091
|
+
const cap = attrText(node, "strokeLinecap");
|
|
1092
|
+
if (cap !== undefined)
|
|
1093
|
+
next.strokeLinecap = cap;
|
|
1094
|
+
const join = attrText(node, "strokeLinejoin");
|
|
1095
|
+
if (join !== undefined)
|
|
1096
|
+
next.strokeLinejoin = join;
|
|
1097
|
+
return next;
|
|
1098
|
+
}
|
|
1099
|
+
/**
|
|
1100
|
+
* THE BUILD LAW: walk the children in document order; a wrapper (group · blur · shadow ·
|
|
1101
|
+
* blend) emits NO op and instead folds into its descendants — transform composes,
|
|
1102
|
+
* opacity MULTIPLIES, paint attributes INHERIT, `clip` on a group rides every descendant
|
|
1103
|
+
* op with the matrix in force where the clip was authored, and each effect wrapper
|
|
1104
|
+
* APPENDS to the op's effect chain outermost-first. A `<gradient id>` child registers a
|
|
1105
|
+
* paint and emits no op. An unknown child tag is one diagnostic and no op — a canvas
|
|
1106
|
+
* draws, it does not lay out.
|
|
1107
|
+
*
|
|
1108
|
+
* THE KEY LAW (the `<list>` keying law verbatim): an authored `key` wins, an unkeyed node
|
|
1109
|
+
* keys on its KIND plus its position among same-kind siblings, a duplicate gets the `·n`
|
|
1110
|
+
* suffix in encounter order, and the key is PATH-PREFIXED by its ancestors' keys.
|
|
1111
|
+
*/
|
|
1112
|
+
export function buildCanvasDisplayList(tree) {
|
|
1113
|
+
const ops = [];
|
|
1114
|
+
const gradients = {};
|
|
1115
|
+
const diagnostics = [];
|
|
1116
|
+
const walk = (nodes, prefix, world, opacity, inherited, effects, clip) => {
|
|
1117
|
+
const used = new Set();
|
|
1118
|
+
const keyFor = (node) => {
|
|
1119
|
+
const authored = attrText(node, "key");
|
|
1120
|
+
const base = authored !== undefined && authored !== "" ? authored : node.kind;
|
|
1121
|
+
let key = base;
|
|
1122
|
+
let n = 1;
|
|
1123
|
+
while (used.has(key)) {
|
|
1124
|
+
key = `${base}·${n}`;
|
|
1125
|
+
n += 1;
|
|
1126
|
+
}
|
|
1127
|
+
used.add(key);
|
|
1128
|
+
return prefix === "" ? key : `${prefix}/${key}`;
|
|
1129
|
+
};
|
|
1130
|
+
for (const node of nodes) {
|
|
1131
|
+
if (node.kind === "gradient") {
|
|
1132
|
+
registerGradient(node, gradients, diagnostics);
|
|
1133
|
+
continue;
|
|
1134
|
+
}
|
|
1135
|
+
if (node.kind === "stop")
|
|
1136
|
+
continue;
|
|
1137
|
+
const isWrapper = WRAPPER_KINDS.has(node.kind);
|
|
1138
|
+
const isShape = SHAPE_KINDS.has(node.kind);
|
|
1139
|
+
const isText = node.kind === "text";
|
|
1140
|
+
const isImage = node.kind === "image";
|
|
1141
|
+
if (!isWrapper && !isShape && !isText && !isImage) {
|
|
1142
|
+
diagnostics.push(`canvas: <${node.kind}> is not a drawing primitive`);
|
|
1143
|
+
continue;
|
|
1144
|
+
}
|
|
1145
|
+
const key = keyFor(node);
|
|
1146
|
+
const local = parseCanvasTransform(attrText(node, "transform"));
|
|
1147
|
+
for (const d of local.diagnostics)
|
|
1148
|
+
diagnostics.push(d);
|
|
1149
|
+
const nodeWorld = canvasMatrixIsIdentity(local.matrix)
|
|
1150
|
+
? world : canvasMatMul(world, local.matrix);
|
|
1151
|
+
const nodeOpacity = opacity * clampUnit(attrNum(node, "opacity", 1));
|
|
1152
|
+
const nodeInherited = readInherited(node, inherited, diagnostics);
|
|
1153
|
+
if (isWrapper) {
|
|
1154
|
+
let nodeEffects = effects;
|
|
1155
|
+
if (node.kind === "blur") {
|
|
1156
|
+
nodeEffects = [...effects, { kind: "blur", radius: attrNum(node, "radius", 0) }];
|
|
1157
|
+
}
|
|
1158
|
+
else if (node.kind === "shadow") {
|
|
1159
|
+
const parsed = parseCanvasPaint(attrText(node, "color") ?? "#000000");
|
|
1160
|
+
nodeEffects = [...effects, {
|
|
1161
|
+
kind: "shadow",
|
|
1162
|
+
dx: attrNum(node, "dx", 0),
|
|
1163
|
+
dy: attrNum(node, "dy", 0),
|
|
1164
|
+
radius: attrNum(node, "radius", 0),
|
|
1165
|
+
color: parsed.paint ?? CANVAS_BLACK,
|
|
1166
|
+
}];
|
|
1167
|
+
}
|
|
1168
|
+
else if (node.kind === "blend") {
|
|
1169
|
+
nodeEffects = [...effects, { kind: "blend", mode: attrText(node, "mode") ?? "normal" }];
|
|
1170
|
+
}
|
|
1171
|
+
let nodeClip = clip;
|
|
1172
|
+
const clipData = attrText(node, "clip");
|
|
1173
|
+
if (clipData !== undefined) {
|
|
1174
|
+
const parsed = parseCanvasPath(clipData);
|
|
1175
|
+
for (const d of parsed.diagnostics)
|
|
1176
|
+
diagnostics.push(d);
|
|
1177
|
+
// Nested clips are NOT intersected here: the innermost authored clip wins. Path
|
|
1178
|
+
// intersection is a rasteriser job and no two of them agree on it.
|
|
1179
|
+
nodeClip = { path: parsed.segments, transform: nodeWorld };
|
|
1180
|
+
}
|
|
1181
|
+
walk(node.children ?? [], key, nodeWorld, nodeOpacity, nodeInherited, nodeEffects, nodeClip);
|
|
1182
|
+
continue;
|
|
1183
|
+
}
|
|
1184
|
+
const op = { key, kind: node.kind, transform: nodeWorld, opacity: nodeOpacity };
|
|
1185
|
+
if (isText) {
|
|
1186
|
+
op.text = attrText(node, "value") ?? "";
|
|
1187
|
+
op.x = attrNum(node, "x", 0);
|
|
1188
|
+
op.y = attrNum(node, "y", 0);
|
|
1189
|
+
op.fontSize = attrNum(node, "fontSize", 16);
|
|
1190
|
+
op.textAnchor = attrText(node, "textAnchor") ?? "start";
|
|
1191
|
+
op.fill = nodeInherited.fill !== undefined ? nodeInherited.fill : CANVAS_BLACK;
|
|
1192
|
+
}
|
|
1193
|
+
else if (isImage) {
|
|
1194
|
+
op.src = attrText(node, "src") ?? "";
|
|
1195
|
+
op.x = attrNum(node, "x", 0);
|
|
1196
|
+
op.y = attrNum(node, "y", 0);
|
|
1197
|
+
op.width = attr(node, "width") === undefined ? null : attrNum(node, "width", 0);
|
|
1198
|
+
op.height = attr(node, "height") === undefined ? null : attrNum(node, "height", 0);
|
|
1199
|
+
}
|
|
1200
|
+
else {
|
|
1201
|
+
op.path = shapePath(node, diagnostics);
|
|
1202
|
+
op.fill = nodeInherited.fill !== undefined
|
|
1203
|
+
? nodeInherited.fill
|
|
1204
|
+
: (UNFILLED_KINDS.has(node.kind) ? null : CANVAS_BLACK);
|
|
1205
|
+
op.stroke = nodeInherited.stroke !== undefined ? nodeInherited.stroke : null;
|
|
1206
|
+
op.strokeWidth = nodeInherited.strokeWidth ?? 1;
|
|
1207
|
+
op.fillRule = nodeInherited.fillRule ?? "nonzero";
|
|
1208
|
+
op.strokeLinecap = nodeInherited.strokeLinecap ?? "butt";
|
|
1209
|
+
op.strokeLinejoin = nodeInherited.strokeLinejoin ?? "miter";
|
|
1210
|
+
}
|
|
1211
|
+
if (clip !== undefined)
|
|
1212
|
+
op.clip = clip;
|
|
1213
|
+
if (effects.length > 0)
|
|
1214
|
+
op.effects = [...effects];
|
|
1215
|
+
ops.push(op);
|
|
1216
|
+
}
|
|
1217
|
+
};
|
|
1218
|
+
const root = {
|
|
1219
|
+
fill: undefined, stroke: undefined, strokeWidth: undefined,
|
|
1220
|
+
fillRule: undefined, strokeLinecap: undefined, strokeLinejoin: undefined,
|
|
1221
|
+
};
|
|
1222
|
+
walk(tree, "", CANVAS_IDENTITY, 1, root, [], undefined);
|
|
1223
|
+
return { ops, gradients, diagnostics };
|
|
1224
|
+
}
|
|
1225
|
+
function clampUnit(v) {
|
|
1226
|
+
return Math.min(1, Math.max(0, v));
|
|
1227
|
+
}
|
|
1228
|
+
function registerGradient(node, into, diagnostics) {
|
|
1229
|
+
const id = attrText(node, "id");
|
|
1230
|
+
if (id === undefined || id === "") {
|
|
1231
|
+
diagnostics.push("canvas.gradient: a gradient needs an id");
|
|
1232
|
+
return;
|
|
1233
|
+
}
|
|
1234
|
+
const word = attrText(node, "kind") ?? "linear";
|
|
1235
|
+
const kind = word === "radial" || word === "angular" ? word : "linear";
|
|
1236
|
+
const geom = kind === "linear"
|
|
1237
|
+
? [attrNum(node, "x1", 0), attrNum(node, "y1", 0), attrNum(node, "x2", 0), attrNum(node, "y2", 0)]
|
|
1238
|
+
: kind === "radial"
|
|
1239
|
+
? [attrNum(node, "cx", 0), attrNum(node, "cy", 0), attrNum(node, "r", 0)]
|
|
1240
|
+
: [attrNum(node, "cx", 0), attrNum(node, "cy", 0), attrNum(node, "start", 0)];
|
|
1241
|
+
const inputs = (node.children ?? [])
|
|
1242
|
+
.filter((c) => c.kind === "stop")
|
|
1243
|
+
.map((c) => ({
|
|
1244
|
+
offset: (attr(c, "offset") ?? null),
|
|
1245
|
+
color: attrText(c, "color") ?? null,
|
|
1246
|
+
opacity: (attr(c, "opacity") ?? null),
|
|
1247
|
+
}));
|
|
1248
|
+
const normalized = normalizeCanvasStops(inputs);
|
|
1249
|
+
for (const d of normalized.diagnostics)
|
|
1250
|
+
diagnostics.push(d);
|
|
1251
|
+
into[id] = { kind, geom, stops: normalized.stops };
|
|
1252
|
+
}
|
|
1253
|
+
function opContentEqual(a, b) {
|
|
1254
|
+
const strip = (op) => {
|
|
1255
|
+
const { key: _key, ...rest } = op;
|
|
1256
|
+
return rest;
|
|
1257
|
+
};
|
|
1258
|
+
return JSON.stringify(strip(a)) === JSON.stringify(strip(b));
|
|
1259
|
+
}
|
|
1260
|
+
/** the complement of a longest increasing subsequence of the retained previous indices,
|
|
1261
|
+
* so a single item moved to the front is ONE move, not n */
|
|
1262
|
+
function longestIncreasing(values) {
|
|
1263
|
+
if (values.length === 0)
|
|
1264
|
+
return [];
|
|
1265
|
+
const tails = [];
|
|
1266
|
+
const parents = new Array(values.length).fill(-1);
|
|
1267
|
+
for (let i = 0; i < values.length; i += 1) {
|
|
1268
|
+
let lo = 0;
|
|
1269
|
+
let hi = tails.length;
|
|
1270
|
+
while (lo < hi) {
|
|
1271
|
+
const mid = (lo + hi) >> 1;
|
|
1272
|
+
if (values[tails[mid]] < values[i])
|
|
1273
|
+
lo = mid + 1;
|
|
1274
|
+
else
|
|
1275
|
+
hi = mid;
|
|
1276
|
+
}
|
|
1277
|
+
if (lo > 0)
|
|
1278
|
+
parents[i] = tails[lo - 1];
|
|
1279
|
+
tails[lo] = i;
|
|
1280
|
+
}
|
|
1281
|
+
const out = [];
|
|
1282
|
+
let cursor = tails[tails.length - 1];
|
|
1283
|
+
while (cursor !== -1) {
|
|
1284
|
+
out.push(cursor);
|
|
1285
|
+
cursor = parents[cursor];
|
|
1286
|
+
}
|
|
1287
|
+
return out.reverse();
|
|
1288
|
+
}
|
|
1289
|
+
/** THE DIFF LAW: keys present on both sides KEEP their identity across reorder. */
|
|
1290
|
+
export function diffCanvasDisplayList(before, after) {
|
|
1291
|
+
const beforeIndex = new Map();
|
|
1292
|
+
before.forEach((op, i) => beforeIndex.set(op.key, i));
|
|
1293
|
+
const afterKeys = new Set(after.map((op) => op.key));
|
|
1294
|
+
const out = [];
|
|
1295
|
+
for (const op of before) {
|
|
1296
|
+
if (!afterKeys.has(op.key))
|
|
1297
|
+
out.push({ op: "remove", key: op.key });
|
|
1298
|
+
}
|
|
1299
|
+
const retained = [];
|
|
1300
|
+
const retainedAt = [];
|
|
1301
|
+
after.forEach((op, i) => {
|
|
1302
|
+
const previous = beforeIndex.get(op.key);
|
|
1303
|
+
if (previous !== undefined) {
|
|
1304
|
+
retained.push(previous);
|
|
1305
|
+
retainedAt.push(i);
|
|
1306
|
+
}
|
|
1307
|
+
});
|
|
1308
|
+
const stable = new Set(longestIncreasing(retained).map((i) => retainedAt[i]));
|
|
1309
|
+
after.forEach((op, i) => {
|
|
1310
|
+
const previous = beforeIndex.get(op.key);
|
|
1311
|
+
if (previous === undefined) {
|
|
1312
|
+
out.push({ op: "insert", key: op.key, to: i });
|
|
1313
|
+
return;
|
|
1314
|
+
}
|
|
1315
|
+
if (!stable.has(i)) {
|
|
1316
|
+
out.push({ op: "move", key: op.key, from: previous, to: i });
|
|
1317
|
+
return;
|
|
1318
|
+
}
|
|
1319
|
+
if (opContentEqual(before[previous], op))
|
|
1320
|
+
out.push({ op: "keep", key: op.key, to: i });
|
|
1321
|
+
else
|
|
1322
|
+
out.push({ op: "update", key: op.key, to: i });
|
|
1323
|
+
});
|
|
1324
|
+
return out;
|
|
1325
|
+
}
|
|
1326
|
+
// ── the SSR SVG serialisation ────────────────────────────────────────────────────────
|
|
1327
|
+
/** round to 6 decimals and trim: the ONE number spelling the SVG bytes are pinned to */
|
|
1328
|
+
export function canvasNumberText(value) {
|
|
1329
|
+
if (!Number.isFinite(value))
|
|
1330
|
+
return "0";
|
|
1331
|
+
const rounded = Math.round(value * 1e6) / 1e6;
|
|
1332
|
+
if (Object.is(rounded, -0))
|
|
1333
|
+
return "0";
|
|
1334
|
+
return String(rounded);
|
|
1335
|
+
}
|
|
1336
|
+
function hexByte(v) {
|
|
1337
|
+
return Math.max(0, Math.min(255, Math.round(v * 255))).toString(16).padStart(2, "0");
|
|
1338
|
+
}
|
|
1339
|
+
export function canvasRgbaHex(rgba) {
|
|
1340
|
+
return `#${hexByte(rgba[0])}${hexByte(rgba[1])}${hexByte(rgba[2])}`;
|
|
1341
|
+
}
|
|
1342
|
+
function paintText(paint) {
|
|
1343
|
+
if (paint === null)
|
|
1344
|
+
return "none";
|
|
1345
|
+
if (paint.kind === "rgba")
|
|
1346
|
+
return canvasRgbaHex(paint.rgba);
|
|
1347
|
+
if (paint.kind === "gradient")
|
|
1348
|
+
return `url(#${canvasEscapeAttr(paint.id)})`;
|
|
1349
|
+
const token = CANVAS_COLOR_TOKENS[paint.token];
|
|
1350
|
+
if (token === undefined || token === "transparent")
|
|
1351
|
+
return "transparent";
|
|
1352
|
+
return `var(${token})`;
|
|
1353
|
+
}
|
|
1354
|
+
function paintAlpha(paint) {
|
|
1355
|
+
return paint !== null && paint.kind === "rgba" ? paint.rgba[3] : 1;
|
|
1356
|
+
}
|
|
1357
|
+
export function canvasEscapeXml(text) {
|
|
1358
|
+
return text.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">");
|
|
1359
|
+
}
|
|
1360
|
+
/** An attribute value inside double quotes also needs its quotes escaped, or an authored `"`
|
|
1361
|
+
* closes the attribute and opens markup of its own. */
|
|
1362
|
+
function canvasEscapeAttr(text) {
|
|
1363
|
+
return canvasEscapeXml(text).replace(/"/g, """).replace(/'/g, "'");
|
|
1364
|
+
}
|
|
1365
|
+
function pathText(segments) {
|
|
1366
|
+
const parts = [];
|
|
1367
|
+
for (const seg of segments) {
|
|
1368
|
+
if (seg[0] === "Z") {
|
|
1369
|
+
parts.push("Z");
|
|
1370
|
+
continue;
|
|
1371
|
+
}
|
|
1372
|
+
parts.push(seg[0]);
|
|
1373
|
+
for (let i = 1; i < seg.length; i += 1)
|
|
1374
|
+
parts.push(canvasNumberText(seg[i]));
|
|
1375
|
+
}
|
|
1376
|
+
return parts.join(" ");
|
|
1377
|
+
}
|
|
1378
|
+
function matrixText(m) {
|
|
1379
|
+
return `matrix(${m.map(canvasNumberText).join(",")})`;
|
|
1380
|
+
}
|
|
1381
|
+
/**
|
|
1382
|
+
* The tier-1 tree renders SERVER-SIDE as SVG and hydrates onto a canvas with no repaint
|
|
1383
|
+
* flash: a capability other cross-platform frameworks lack, and it costs one serialiser.
|
|
1384
|
+
*
|
|
1385
|
+
* NAMED ABSENCE: SVG has no conic-gradient primitive, so an `angular` gradient serialises
|
|
1386
|
+
* as a radialGradient with the same stops — the SSR frame approximates and the live canvas
|
|
1387
|
+
* paints the real sweep. Everything else is byte-pinned by the corpus.
|
|
1388
|
+
*/
|
|
1389
|
+
export function canvasToSvg(list, width, height) {
|
|
1390
|
+
const defs = [];
|
|
1391
|
+
let fxCounter = 0;
|
|
1392
|
+
const fxId = () => { fxCounter += 1; return `dsx-fx-${fxCounter}`; };
|
|
1393
|
+
for (const [id, gradient] of Object.entries(list.gradients)) {
|
|
1394
|
+
const stops = gradient.stops.map((s) => {
|
|
1395
|
+
const bits = [`offset="${canvasNumberText(s.offset)}"`];
|
|
1396
|
+
if ("rgba" in s) {
|
|
1397
|
+
bits.push(`stop-color="${canvasRgbaHex(s.rgba)}"`);
|
|
1398
|
+
if (s.rgba[3] < 1)
|
|
1399
|
+
bits.push(`stop-opacity="${canvasNumberText(s.rgba[3])}"`);
|
|
1400
|
+
}
|
|
1401
|
+
else {
|
|
1402
|
+
const token = CANVAS_COLOR_TOKENS[s.token];
|
|
1403
|
+
bits.push(`stop-color="${token === undefined || token === "transparent" ? "transparent" : `var(${token})`}"`);
|
|
1404
|
+
if (s.opacity < 1)
|
|
1405
|
+
bits.push(`stop-opacity="${canvasNumberText(s.opacity)}"`);
|
|
1406
|
+
}
|
|
1407
|
+
return `<stop ${bits.join(" ")}/>`;
|
|
1408
|
+
}).join("");
|
|
1409
|
+
if (gradient.kind === "linear") {
|
|
1410
|
+
const g = gradient.geom;
|
|
1411
|
+
defs.push(`<linearGradient id="${canvasEscapeAttr(id)}" gradientUnits="userSpaceOnUse" x1="${canvasNumberText(g[0] ?? 0)}" y1="${canvasNumberText(g[1] ?? 0)}" x2="${canvasNumberText(g[2] ?? 0)}" y2="${canvasNumberText(g[3] ?? 0)}">${stops}</linearGradient>`);
|
|
1412
|
+
}
|
|
1413
|
+
else {
|
|
1414
|
+
const g = gradient.geom;
|
|
1415
|
+
const r = gradient.kind === "radial" ? (g[2] ?? 0) : Math.max(width, height) / 2;
|
|
1416
|
+
defs.push(`<radialGradient id="${canvasEscapeAttr(id)}" gradientUnits="userSpaceOnUse" cx="${canvasNumberText(g[0] ?? 0)}" cy="${canvasNumberText(g[1] ?? 0)}" r="${canvasNumberText(r)}">${stops}</radialGradient>`);
|
|
1417
|
+
}
|
|
1418
|
+
}
|
|
1419
|
+
const body = [];
|
|
1420
|
+
for (const op of list.ops) {
|
|
1421
|
+
let inner;
|
|
1422
|
+
if (op.kind === "text") {
|
|
1423
|
+
const bits = [
|
|
1424
|
+
`x="${canvasNumberText(op.x ?? 0)}"`,
|
|
1425
|
+
`y="${canvasNumberText(op.y ?? 0)}"`,
|
|
1426
|
+
`font-size="${canvasNumberText(op.fontSize ?? 16)}"`,
|
|
1427
|
+
`fill="${paintText(op.fill ?? null)}"`,
|
|
1428
|
+
];
|
|
1429
|
+
if (paintAlpha(op.fill ?? null) < 1)
|
|
1430
|
+
bits.push(`fill-opacity="${canvasNumberText(paintAlpha(op.fill ?? null))}"`);
|
|
1431
|
+
bits.push(`text-anchor="${canvasEscapeAttr(op.textAnchor ?? "start")}"`);
|
|
1432
|
+
inner = `<text ${bits.join(" ")}>${canvasEscapeXml(op.text ?? "")}</text>`;
|
|
1433
|
+
}
|
|
1434
|
+
else if (op.kind === "image") {
|
|
1435
|
+
const bits = [`x="${canvasNumberText(op.x ?? 0)}"`, `y="${canvasNumberText(op.y ?? 0)}"`];
|
|
1436
|
+
if (op.width !== null && op.width !== undefined)
|
|
1437
|
+
bits.push(`width="${canvasNumberText(op.width)}"`);
|
|
1438
|
+
if (op.height !== null && op.height !== undefined)
|
|
1439
|
+
bits.push(`height="${canvasNumberText(op.height)}"`);
|
|
1440
|
+
bits.push(`href="${canvasEscapeAttr(op.src ?? "")}"`);
|
|
1441
|
+
inner = `<image ${bits.join(" ")}/>`;
|
|
1442
|
+
}
|
|
1443
|
+
else {
|
|
1444
|
+
const bits = [`d="${pathText(op.path ?? [])}"`, `fill="${paintText(op.fill ?? null)}"`];
|
|
1445
|
+
if (paintAlpha(op.fill ?? null) < 1)
|
|
1446
|
+
bits.push(`fill-opacity="${canvasNumberText(paintAlpha(op.fill ?? null))}"`);
|
|
1447
|
+
if ((op.fillRule ?? "nonzero") !== "nonzero")
|
|
1448
|
+
bits.push(`fill-rule="${op.fillRule}"`);
|
|
1449
|
+
if (op.stroke !== null && op.stroke !== undefined) {
|
|
1450
|
+
bits.push(`stroke="${paintText(op.stroke)}"`);
|
|
1451
|
+
bits.push(`stroke-width="${canvasNumberText(op.strokeWidth ?? 1)}"`);
|
|
1452
|
+
if (paintAlpha(op.stroke) < 1)
|
|
1453
|
+
bits.push(`stroke-opacity="${canvasNumberText(paintAlpha(op.stroke))}"`);
|
|
1454
|
+
if ((op.strokeLinecap ?? "butt") !== "butt")
|
|
1455
|
+
bits.push(`stroke-linecap="${op.strokeLinecap}"`);
|
|
1456
|
+
if ((op.strokeLinejoin ?? "miter") !== "miter")
|
|
1457
|
+
bits.push(`stroke-linejoin="${op.strokeLinejoin}"`);
|
|
1458
|
+
}
|
|
1459
|
+
inner = `<path ${bits.join(" ")}/>`;
|
|
1460
|
+
}
|
|
1461
|
+
const wrap = [];
|
|
1462
|
+
if (!canvasMatrixIsIdentity(op.transform))
|
|
1463
|
+
wrap.push(`transform="${matrixText(op.transform)}"`);
|
|
1464
|
+
if (op.opacity !== 1)
|
|
1465
|
+
wrap.push(`opacity="${canvasNumberText(op.opacity)}"`);
|
|
1466
|
+
if (op.clip !== undefined) {
|
|
1467
|
+
const id = fxId();
|
|
1468
|
+
const clipTransform = canvasMatrixIsIdentity(op.clip.transform)
|
|
1469
|
+
? "" : ` transform="${matrixText(op.clip.transform)}"`;
|
|
1470
|
+
defs.push(`<clipPath id="${id}" clipPathUnits="userSpaceOnUse"><path d="${pathText(op.clip.path)}"${clipTransform}/></clipPath>`);
|
|
1471
|
+
wrap.push(`clip-path="url(#${id})"`);
|
|
1472
|
+
}
|
|
1473
|
+
if (wrap.length > 0)
|
|
1474
|
+
inner = `<g ${wrap.join(" ")}>${inner}</g>`;
|
|
1475
|
+
const effects = op.effects ?? [];
|
|
1476
|
+
for (let i = effects.length - 1; i >= 0; i -= 1) {
|
|
1477
|
+
const effect = effects[i];
|
|
1478
|
+
if (effect.kind === "blend") {
|
|
1479
|
+
inner = `<g style="mix-blend-mode:${effect.mode}">${inner}</g>`;
|
|
1480
|
+
}
|
|
1481
|
+
else if (effect.kind === "blur") {
|
|
1482
|
+
const id = fxId();
|
|
1483
|
+
defs.push(`<filter id="${id}"><feGaussianBlur stdDeviation="${canvasNumberText(effect.radius / 2)}"/></filter>`);
|
|
1484
|
+
inner = `<g filter="url(#${id})">${inner}</g>`;
|
|
1485
|
+
}
|
|
1486
|
+
else {
|
|
1487
|
+
const id = fxId();
|
|
1488
|
+
const rgba = effect.color.kind === "rgba" ? effect.color.rgba : [0, 0, 0, 1];
|
|
1489
|
+
defs.push(`<filter id="${id}"><feDropShadow dx="${canvasNumberText(effect.dx)}" dy="${canvasNumberText(effect.dy)}" stdDeviation="${canvasNumberText(effect.radius / 2)}" flood-color="${canvasRgbaHex(rgba)}" flood-opacity="${canvasNumberText(rgba[3])}"/></filter>`);
|
|
1490
|
+
inner = `<g filter="url(#${id})">${inner}</g>`;
|
|
1491
|
+
}
|
|
1492
|
+
}
|
|
1493
|
+
body.push(inner);
|
|
1494
|
+
}
|
|
1495
|
+
const defsText = defs.length === 0 ? "" : `<defs>${defs.join("")}</defs>`;
|
|
1496
|
+
return `<svg xmlns="http://www.w3.org/2000/svg" width="${canvasNumberText(width)}" height="${canvasNumberText(height)}" viewBox="0 0 ${canvasNumberText(width)} ${canvasNumberText(height)}">${defsText}${body.join("")}</svg>`;
|
|
1497
|
+
}
|
|
1498
|
+
/** the fallback text metrics, pinned so the fallback itself cannot drift. A platform WITH
|
|
1499
|
+
* real font metrics answers from them, and that divergence is documented, not silent. */
|
|
1500
|
+
export const CANVAS_FALLBACK_ADVANCE = 0.6;
|
|
1501
|
+
export const CANVAS_FALLBACK_ASCENT = 0.8;
|
|
1502
|
+
export const CANVAS_FALLBACK_DESCENT = 0.2;
|
|
1503
|
+
export function canvasFallbackMetrics(text, fontSize) {
|
|
1504
|
+
return {
|
|
1505
|
+
text,
|
|
1506
|
+
width: CANVAS_FALLBACK_ADVANCE * fontSize * text.length,
|
|
1507
|
+
ascent: CANVAS_FALLBACK_ASCENT * fontSize,
|
|
1508
|
+
descent: CANVAS_FALLBACK_DESCENT * fontSize,
|
|
1509
|
+
};
|
|
1510
|
+
}
|
|
1511
|
+
/**
|
|
1512
|
+
* The Canvas2D subset as a RECORDER: every call folds into a command stream a renderer
|
|
1513
|
+
* replays, which is why this corpus is exact on all three renderers — it asserts the
|
|
1514
|
+
* stream, never the pixels.
|
|
1515
|
+
*
|
|
1516
|
+
* (1) The CTM is baked into each path point AT POINT-ADD TIME — the Canvas2D rule, not the
|
|
1517
|
+
* SVG one. (2) save/restore push and pop the matrix AND the paint state as one unit.
|
|
1518
|
+
* (3) `arc` takes RADIANS (the Canvas2D signature authors know), `rotate` takes DEGREES
|
|
1519
|
+
* (every other DSX angle). (4) A draw with an EMPTY path emits nothing at all.
|
|
1520
|
+
*/
|
|
1521
|
+
export class CanvasRecorder {
|
|
1522
|
+
log = [];
|
|
1523
|
+
measurements = [];
|
|
1524
|
+
diagnostics = [];
|
|
1525
|
+
state = {
|
|
1526
|
+
ctm: CANVAS_IDENTITY,
|
|
1527
|
+
fill: CANVAS_BLACK,
|
|
1528
|
+
stroke: CANVAS_BLACK,
|
|
1529
|
+
lineWidth: 1,
|
|
1530
|
+
cap: "butt",
|
|
1531
|
+
join: "miter",
|
|
1532
|
+
alpha: 1,
|
|
1533
|
+
shadow: null,
|
|
1534
|
+
blend: null,
|
|
1535
|
+
clip: null,
|
|
1536
|
+
fontSize: 10,
|
|
1537
|
+
};
|
|
1538
|
+
stack = [];
|
|
1539
|
+
path = [];
|
|
1540
|
+
userX = 0;
|
|
1541
|
+
userY = 0;
|
|
1542
|
+
startX = 0;
|
|
1543
|
+
startY = 0;
|
|
1544
|
+
hasCurrent = false;
|
|
1545
|
+
run(script) {
|
|
1546
|
+
for (const command of script)
|
|
1547
|
+
this.call(command);
|
|
1548
|
+
return { log: this.log, measurements: this.measurements, diagnostics: this.diagnostics };
|
|
1549
|
+
}
|
|
1550
|
+
bad(message) {
|
|
1551
|
+
this.diagnostics.push(message);
|
|
1552
|
+
}
|
|
1553
|
+
baked(x, y) {
|
|
1554
|
+
return canvasApply(this.state.ctm, x, y);
|
|
1555
|
+
}
|
|
1556
|
+
moveTo(x, y) {
|
|
1557
|
+
const p = this.baked(x, y);
|
|
1558
|
+
this.path.push(["M", p[0], p[1]]);
|
|
1559
|
+
this.userX = x;
|
|
1560
|
+
this.userY = y;
|
|
1561
|
+
this.startX = x;
|
|
1562
|
+
this.startY = y;
|
|
1563
|
+
this.hasCurrent = true;
|
|
1564
|
+
}
|
|
1565
|
+
lineTo(x, y) {
|
|
1566
|
+
const p = this.baked(x, y);
|
|
1567
|
+
if (!this.hasCurrent) {
|
|
1568
|
+
this.moveTo(x, y);
|
|
1569
|
+
return;
|
|
1570
|
+
}
|
|
1571
|
+
this.path.push(["L", p[0], p[1]]);
|
|
1572
|
+
this.userX = x;
|
|
1573
|
+
this.userY = y;
|
|
1574
|
+
}
|
|
1575
|
+
entryHead() {
|
|
1576
|
+
const entry = { alpha: this.state.alpha, ctm: this.state.ctm, op: "" };
|
|
1577
|
+
if (this.state.clip !== null)
|
|
1578
|
+
entry.clip = this.state.clip;
|
|
1579
|
+
if (this.state.shadow !== null)
|
|
1580
|
+
entry.shadow = this.state.shadow;
|
|
1581
|
+
if (this.state.blend !== null)
|
|
1582
|
+
entry.blend = this.state.blend;
|
|
1583
|
+
return entry;
|
|
1584
|
+
}
|
|
1585
|
+
numbers(command, count, from = 1) {
|
|
1586
|
+
const out = [];
|
|
1587
|
+
for (let i = 0; i < count; i += 1) {
|
|
1588
|
+
const raw = command[from + i];
|
|
1589
|
+
if (raw === undefined || raw === null)
|
|
1590
|
+
return null;
|
|
1591
|
+
const value = typeof raw === "number" ? raw : Number(raw);
|
|
1592
|
+
if (!Number.isFinite(value))
|
|
1593
|
+
return null;
|
|
1594
|
+
out.push(value);
|
|
1595
|
+
}
|
|
1596
|
+
return out;
|
|
1597
|
+
}
|
|
1598
|
+
call(command) {
|
|
1599
|
+
const name = String(command[0] ?? "");
|
|
1600
|
+
const missing = () => this.bad(`canvas.ctx.${name}: missing argument`);
|
|
1601
|
+
switch (name) {
|
|
1602
|
+
case "beginPath":
|
|
1603
|
+
this.path = [];
|
|
1604
|
+
this.hasCurrent = false;
|
|
1605
|
+
return;
|
|
1606
|
+
case "closePath":
|
|
1607
|
+
if (this.path.length === 0)
|
|
1608
|
+
return;
|
|
1609
|
+
this.path.push(["Z"]);
|
|
1610
|
+
this.userX = this.startX;
|
|
1611
|
+
this.userY = this.startY;
|
|
1612
|
+
return;
|
|
1613
|
+
case "moveTo": {
|
|
1614
|
+
const a = this.numbers(command, 2);
|
|
1615
|
+
if (a === null) {
|
|
1616
|
+
missing();
|
|
1617
|
+
return;
|
|
1618
|
+
}
|
|
1619
|
+
this.moveTo(a[0], a[1]);
|
|
1620
|
+
return;
|
|
1621
|
+
}
|
|
1622
|
+
case "lineTo": {
|
|
1623
|
+
const a = this.numbers(command, 2);
|
|
1624
|
+
if (a === null) {
|
|
1625
|
+
missing();
|
|
1626
|
+
return;
|
|
1627
|
+
}
|
|
1628
|
+
this.lineTo(a[0], a[1]);
|
|
1629
|
+
return;
|
|
1630
|
+
}
|
|
1631
|
+
case "quadTo": {
|
|
1632
|
+
const a = this.numbers(command, 4);
|
|
1633
|
+
if (a === null) {
|
|
1634
|
+
missing();
|
|
1635
|
+
return;
|
|
1636
|
+
}
|
|
1637
|
+
if (!this.hasCurrent)
|
|
1638
|
+
this.moveTo(a[0], a[1]);
|
|
1639
|
+
const c = this.baked(a[0], a[1]);
|
|
1640
|
+
const p = this.baked(a[2], a[3]);
|
|
1641
|
+
this.path.push(["Q", c[0], c[1], p[0], p[1]]);
|
|
1642
|
+
this.userX = a[2];
|
|
1643
|
+
this.userY = a[3];
|
|
1644
|
+
return;
|
|
1645
|
+
}
|
|
1646
|
+
case "cubicTo": {
|
|
1647
|
+
const a = this.numbers(command, 6);
|
|
1648
|
+
if (a === null) {
|
|
1649
|
+
missing();
|
|
1650
|
+
return;
|
|
1651
|
+
}
|
|
1652
|
+
if (!this.hasCurrent)
|
|
1653
|
+
this.moveTo(a[0], a[1]);
|
|
1654
|
+
const c1 = this.baked(a[0], a[1]);
|
|
1655
|
+
const c2 = this.baked(a[2], a[3]);
|
|
1656
|
+
const p = this.baked(a[4], a[5]);
|
|
1657
|
+
this.path.push(["C", c1[0], c1[1], c2[0], c2[1], p[0], p[1]]);
|
|
1658
|
+
this.userX = a[4];
|
|
1659
|
+
this.userY = a[5];
|
|
1660
|
+
return;
|
|
1661
|
+
}
|
|
1662
|
+
case "arc": {
|
|
1663
|
+
const a = this.numbers(command, 5);
|
|
1664
|
+
if (a === null) {
|
|
1665
|
+
missing();
|
|
1666
|
+
return;
|
|
1667
|
+
}
|
|
1668
|
+
this.arc(a[0], a[1], a[2], a[3], a[4], command[6] === true);
|
|
1669
|
+
return;
|
|
1670
|
+
}
|
|
1671
|
+
case "arcTo": {
|
|
1672
|
+
const a = this.numbers(command, 5);
|
|
1673
|
+
if (a === null) {
|
|
1674
|
+
missing();
|
|
1675
|
+
return;
|
|
1676
|
+
}
|
|
1677
|
+
this.arcTo(a[0], a[1], a[2], a[3], a[4]);
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
case "rect": {
|
|
1681
|
+
const a = this.numbers(command, 4);
|
|
1682
|
+
if (a === null) {
|
|
1683
|
+
missing();
|
|
1684
|
+
return;
|
|
1685
|
+
}
|
|
1686
|
+
this.appendUserPath(canvasRectPath(a[0], a[1], a[2], a[3], 0, 0), a[0], a[1]);
|
|
1687
|
+
return;
|
|
1688
|
+
}
|
|
1689
|
+
case "roundRect": {
|
|
1690
|
+
const a = this.numbers(command, 5);
|
|
1691
|
+
if (a === null) {
|
|
1692
|
+
missing();
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
this.appendUserPath(canvasRectPath(a[0], a[1], a[2], a[3], a[4], a[4]), a[0], a[1]);
|
|
1696
|
+
return;
|
|
1697
|
+
}
|
|
1698
|
+
case "fill":
|
|
1699
|
+
case "stroke": {
|
|
1700
|
+
if (this.path.length === 0)
|
|
1701
|
+
return;
|
|
1702
|
+
const entry = this.entryHead();
|
|
1703
|
+
entry.op = name;
|
|
1704
|
+
entry.path = [...this.path];
|
|
1705
|
+
if (name === "fill") {
|
|
1706
|
+
entry.style = this.state.fill;
|
|
1707
|
+
entry.rule = command[1] === "evenodd" ? "evenodd" : "nonzero";
|
|
1708
|
+
}
|
|
1709
|
+
else {
|
|
1710
|
+
entry.style = this.state.stroke;
|
|
1711
|
+
entry.lineWidth = this.state.lineWidth;
|
|
1712
|
+
entry.cap = this.state.cap;
|
|
1713
|
+
entry.join = this.state.join;
|
|
1714
|
+
}
|
|
1715
|
+
this.log.push(entry);
|
|
1716
|
+
return;
|
|
1717
|
+
}
|
|
1718
|
+
case "clip": {
|
|
1719
|
+
if (this.path.length === 0)
|
|
1720
|
+
return;
|
|
1721
|
+
this.state = {
|
|
1722
|
+
...this.state,
|
|
1723
|
+
clip: { path: [...this.path], rule: command[1] === "evenodd" ? "evenodd" : "nonzero" },
|
|
1724
|
+
};
|
|
1725
|
+
return;
|
|
1726
|
+
}
|
|
1727
|
+
case "clear": {
|
|
1728
|
+
const entry = this.entryHead();
|
|
1729
|
+
entry.op = "clear";
|
|
1730
|
+
const a = this.numbers(command, 4);
|
|
1731
|
+
entry.rect = a === null ? null : [a[0], a[1], a[2], a[3]];
|
|
1732
|
+
this.log.push(entry);
|
|
1733
|
+
return;
|
|
1734
|
+
}
|
|
1735
|
+
case "save":
|
|
1736
|
+
this.stack.push({ ...this.state });
|
|
1737
|
+
return;
|
|
1738
|
+
case "restore": {
|
|
1739
|
+
const popped = this.stack.pop();
|
|
1740
|
+
if (popped === undefined) {
|
|
1741
|
+
this.bad("canvas.ctx.restore: the state stack is empty");
|
|
1742
|
+
return;
|
|
1743
|
+
}
|
|
1744
|
+
this.state = popped;
|
|
1745
|
+
return;
|
|
1746
|
+
}
|
|
1747
|
+
case "translate": {
|
|
1748
|
+
const a = this.numbers(command, 2);
|
|
1749
|
+
if (a === null) {
|
|
1750
|
+
missing();
|
|
1751
|
+
return;
|
|
1752
|
+
}
|
|
1753
|
+
this.compose([1, 0, 0, 1, a[0], a[1]]);
|
|
1754
|
+
return;
|
|
1755
|
+
}
|
|
1756
|
+
case "scale": {
|
|
1757
|
+
const a = this.numbers(command, 2);
|
|
1758
|
+
if (a === null) {
|
|
1759
|
+
missing();
|
|
1760
|
+
return;
|
|
1761
|
+
}
|
|
1762
|
+
this.compose([a[0], 0, 0, a[1], 0, 0]);
|
|
1763
|
+
return;
|
|
1764
|
+
}
|
|
1765
|
+
case "rotate": {
|
|
1766
|
+
const a = this.numbers(command, 1);
|
|
1767
|
+
if (a === null) {
|
|
1768
|
+
missing();
|
|
1769
|
+
return;
|
|
1770
|
+
}
|
|
1771
|
+
const r = a[0] * DEG;
|
|
1772
|
+
this.compose([Math.cos(r), Math.sin(r), -Math.sin(r), Math.cos(r), 0, 0]);
|
|
1773
|
+
return;
|
|
1774
|
+
}
|
|
1775
|
+
case "transform": {
|
|
1776
|
+
const a = this.numbers(command, 6);
|
|
1777
|
+
if (a === null) {
|
|
1778
|
+
missing();
|
|
1779
|
+
return;
|
|
1780
|
+
}
|
|
1781
|
+
this.compose([a[0], a[1], a[2], a[3], a[4], a[5]]);
|
|
1782
|
+
return;
|
|
1783
|
+
}
|
|
1784
|
+
case "drawImage": {
|
|
1785
|
+
const src = command[1];
|
|
1786
|
+
if (typeof src !== "string") {
|
|
1787
|
+
missing();
|
|
1788
|
+
return;
|
|
1789
|
+
}
|
|
1790
|
+
const a = this.numbers(command, 2, 2);
|
|
1791
|
+
if (a === null) {
|
|
1792
|
+
missing();
|
|
1793
|
+
return;
|
|
1794
|
+
}
|
|
1795
|
+
const size = this.numbers(command, 2, 4);
|
|
1796
|
+
const entry = this.entryHead();
|
|
1797
|
+
entry.op = "drawImage";
|
|
1798
|
+
entry.src = src;
|
|
1799
|
+
entry.x = a[0];
|
|
1800
|
+
entry.y = a[1];
|
|
1801
|
+
entry.width = size === null ? null : size[0];
|
|
1802
|
+
entry.height = size === null ? null : size[1];
|
|
1803
|
+
this.log.push(entry);
|
|
1804
|
+
return;
|
|
1805
|
+
}
|
|
1806
|
+
case "fillText":
|
|
1807
|
+
case "strokeText": {
|
|
1808
|
+
const text = command[1];
|
|
1809
|
+
if (typeof text !== "string") {
|
|
1810
|
+
missing();
|
|
1811
|
+
return;
|
|
1812
|
+
}
|
|
1813
|
+
const a = this.numbers(command, 2, 2);
|
|
1814
|
+
if (a === null) {
|
|
1815
|
+
missing();
|
|
1816
|
+
return;
|
|
1817
|
+
}
|
|
1818
|
+
const entry = this.entryHead();
|
|
1819
|
+
entry.op = name;
|
|
1820
|
+
entry.text = text;
|
|
1821
|
+
entry.x = a[0];
|
|
1822
|
+
entry.y = a[1];
|
|
1823
|
+
entry.style = name === "fillText" ? this.state.fill : this.state.stroke;
|
|
1824
|
+
entry.fontSize = this.state.fontSize;
|
|
1825
|
+
if (name === "strokeText")
|
|
1826
|
+
entry.lineWidth = this.state.lineWidth;
|
|
1827
|
+
this.log.push(entry);
|
|
1828
|
+
return;
|
|
1829
|
+
}
|
|
1830
|
+
case "measureText": {
|
|
1831
|
+
const text = command[1];
|
|
1832
|
+
if (typeof text !== "string") {
|
|
1833
|
+
missing();
|
|
1834
|
+
return;
|
|
1835
|
+
}
|
|
1836
|
+
this.measurements.push(canvasFallbackMetrics(text, this.state.fontSize));
|
|
1837
|
+
return;
|
|
1838
|
+
}
|
|
1839
|
+
case "setFillStyle":
|
|
1840
|
+
case "setStrokeStyle": {
|
|
1841
|
+
const raw = command[1];
|
|
1842
|
+
if (typeof raw !== "string") {
|
|
1843
|
+
missing();
|
|
1844
|
+
return;
|
|
1845
|
+
}
|
|
1846
|
+
const parsed = parseCanvasPaint(raw);
|
|
1847
|
+
if (parsed.paint === null) {
|
|
1848
|
+
if (!parsed.ok)
|
|
1849
|
+
this.bad(`canvas.ctx.${name}: unparseable colour '${raw}'`);
|
|
1850
|
+
return;
|
|
1851
|
+
}
|
|
1852
|
+
this.state = name === "setFillStyle"
|
|
1853
|
+
? { ...this.state, fill: parsed.paint }
|
|
1854
|
+
: { ...this.state, stroke: parsed.paint };
|
|
1855
|
+
return;
|
|
1856
|
+
}
|
|
1857
|
+
case "setLineWidth": {
|
|
1858
|
+
const a = this.numbers(command, 1);
|
|
1859
|
+
if (a === null) {
|
|
1860
|
+
missing();
|
|
1861
|
+
return;
|
|
1862
|
+
}
|
|
1863
|
+
this.state = { ...this.state, lineWidth: a[0] };
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1866
|
+
case "setLineCap": {
|
|
1867
|
+
const raw = command[1];
|
|
1868
|
+
if (typeof raw !== "string") {
|
|
1869
|
+
missing();
|
|
1870
|
+
return;
|
|
1871
|
+
}
|
|
1872
|
+
this.state = { ...this.state, cap: raw };
|
|
1873
|
+
return;
|
|
1874
|
+
}
|
|
1875
|
+
case "setLineJoin": {
|
|
1876
|
+
const raw = command[1];
|
|
1877
|
+
if (typeof raw !== "string") {
|
|
1878
|
+
missing();
|
|
1879
|
+
return;
|
|
1880
|
+
}
|
|
1881
|
+
this.state = { ...this.state, join: raw };
|
|
1882
|
+
return;
|
|
1883
|
+
}
|
|
1884
|
+
case "setShadow": {
|
|
1885
|
+
const a = this.numbers(command, 3);
|
|
1886
|
+
const raw = command[4];
|
|
1887
|
+
if (a === null || typeof raw !== "string") {
|
|
1888
|
+
missing();
|
|
1889
|
+
return;
|
|
1890
|
+
}
|
|
1891
|
+
const parsed = parseCanvasPaint(raw);
|
|
1892
|
+
this.state = {
|
|
1893
|
+
...this.state,
|
|
1894
|
+
shadow: { dx: a[0], dy: a[1], radius: a[2], color: parsed.paint ?? CANVAS_BLACK },
|
|
1895
|
+
};
|
|
1896
|
+
return;
|
|
1897
|
+
}
|
|
1898
|
+
case "setBlendMode": {
|
|
1899
|
+
const raw = command[1];
|
|
1900
|
+
if (typeof raw !== "string") {
|
|
1901
|
+
missing();
|
|
1902
|
+
return;
|
|
1903
|
+
}
|
|
1904
|
+
this.state = { ...this.state, blend: raw };
|
|
1905
|
+
return;
|
|
1906
|
+
}
|
|
1907
|
+
case "setGlobalAlpha": {
|
|
1908
|
+
const a = this.numbers(command, 1);
|
|
1909
|
+
if (a === null) {
|
|
1910
|
+
missing();
|
|
1911
|
+
return;
|
|
1912
|
+
}
|
|
1913
|
+
this.state = { ...this.state, alpha: clampUnit(a[0]) };
|
|
1914
|
+
return;
|
|
1915
|
+
}
|
|
1916
|
+
case "setFont": {
|
|
1917
|
+
const a = this.numbers(command, 1);
|
|
1918
|
+
if (a === null) {
|
|
1919
|
+
missing();
|
|
1920
|
+
return;
|
|
1921
|
+
}
|
|
1922
|
+
this.state = { ...this.state, fontSize: a[0] };
|
|
1923
|
+
return;
|
|
1924
|
+
}
|
|
1925
|
+
default:
|
|
1926
|
+
this.bad(`canvas.ctx: unknown method '${name}'`);
|
|
1927
|
+
}
|
|
1928
|
+
}
|
|
1929
|
+
compose(local) {
|
|
1930
|
+
this.state = { ...this.state, ctm: canvasMatMul(this.state.ctm, local) };
|
|
1931
|
+
}
|
|
1932
|
+
/** a user-space subpath, every point baked through the CTM as it is added */
|
|
1933
|
+
appendUserPath(segments, endX, endY) {
|
|
1934
|
+
for (const seg of segments) {
|
|
1935
|
+
if (seg[0] === "Z") {
|
|
1936
|
+
this.path.push(["Z"]);
|
|
1937
|
+
continue;
|
|
1938
|
+
}
|
|
1939
|
+
const baked = [];
|
|
1940
|
+
for (let i = 1; i < seg.length; i += 2) {
|
|
1941
|
+
const p = this.baked(seg[i], seg[i + 1]);
|
|
1942
|
+
baked.push(p[0], p[1]);
|
|
1943
|
+
}
|
|
1944
|
+
this.path.push([seg[0], ...baked]);
|
|
1945
|
+
}
|
|
1946
|
+
this.userX = endX;
|
|
1947
|
+
this.userY = endY;
|
|
1948
|
+
this.startX = endX;
|
|
1949
|
+
this.startY = endY;
|
|
1950
|
+
this.hasCurrent = true;
|
|
1951
|
+
}
|
|
1952
|
+
/** joins from the current point with a line, sweeps clockwise unless anticlockwise is
|
|
1953
|
+
* set, clamps at a full turn, and folds to cubics through the same converter as `d` */
|
|
1954
|
+
arc(cx, cy, r, a0, a1, ccw) {
|
|
1955
|
+
const start = [cx + r * Math.cos(a0), cy + r * Math.sin(a0)];
|
|
1956
|
+
if (this.hasCurrent)
|
|
1957
|
+
this.lineTo(start[0], start[1]);
|
|
1958
|
+
else
|
|
1959
|
+
this.moveTo(start[0], start[1]);
|
|
1960
|
+
const full = 2 * Math.PI;
|
|
1961
|
+
let delta = a1 - a0;
|
|
1962
|
+
if (!ccw) {
|
|
1963
|
+
if (delta >= full)
|
|
1964
|
+
delta = full;
|
|
1965
|
+
else {
|
|
1966
|
+
delta %= full;
|
|
1967
|
+
if (delta < 0)
|
|
1968
|
+
delta += full;
|
|
1969
|
+
}
|
|
1970
|
+
}
|
|
1971
|
+
else if (delta <= -full)
|
|
1972
|
+
delta = -full;
|
|
1973
|
+
else {
|
|
1974
|
+
delta %= full;
|
|
1975
|
+
if (delta > 0)
|
|
1976
|
+
delta -= full;
|
|
1977
|
+
}
|
|
1978
|
+
if (delta === 0)
|
|
1979
|
+
return;
|
|
1980
|
+
const cubics = arcCentreToCubics(cx, cy, r, r, 0, a0, delta);
|
|
1981
|
+
this.appendUserCurves(cubics);
|
|
1982
|
+
const end = [cx + r * Math.cos(a0 + delta), cy + r * Math.sin(a0 + delta)];
|
|
1983
|
+
this.userX = end[0];
|
|
1984
|
+
this.userY = end[1];
|
|
1985
|
+
}
|
|
1986
|
+
appendUserCurves(segments) {
|
|
1987
|
+
for (const seg of segments) {
|
|
1988
|
+
const baked = [];
|
|
1989
|
+
for (let i = 1; i < seg.length; i += 2) {
|
|
1990
|
+
const p = this.baked(seg[i], seg[i + 1]);
|
|
1991
|
+
baked.push(p[0], p[1]);
|
|
1992
|
+
}
|
|
1993
|
+
this.path.push([seg[0], ...baked]);
|
|
1994
|
+
}
|
|
1995
|
+
}
|
|
1996
|
+
arcTo(x1, y1, x2, y2, r) {
|
|
1997
|
+
if (!this.hasCurrent) {
|
|
1998
|
+
this.moveTo(x1, y1);
|
|
1999
|
+
return;
|
|
2000
|
+
}
|
|
2001
|
+
const x0 = this.userX;
|
|
2002
|
+
const y0 = this.userY;
|
|
2003
|
+
const v1x = x0 - x1;
|
|
2004
|
+
const v1y = y0 - y1;
|
|
2005
|
+
const v2x = x2 - x1;
|
|
2006
|
+
const v2y = y2 - y1;
|
|
2007
|
+
const l1 = Math.hypot(v1x, v1y);
|
|
2008
|
+
const l2 = Math.hypot(v2x, v2y);
|
|
2009
|
+
const cross = v1x * v2y - v1y * v2x;
|
|
2010
|
+
if (l1 === 0 || l2 === 0 || r <= 0 || Math.abs(cross) < 1e-12) {
|
|
2011
|
+
this.lineTo(x1, y1);
|
|
2012
|
+
return;
|
|
2013
|
+
}
|
|
2014
|
+
const u1x = v1x / l1;
|
|
2015
|
+
const u1y = v1y / l1;
|
|
2016
|
+
const u2x = v2x / l2;
|
|
2017
|
+
const u2y = v2y / l2;
|
|
2018
|
+
const angle = Math.acos(Math.min(1, Math.max(-1, u1x * u2x + u1y * u2y)));
|
|
2019
|
+
const d = r / Math.tan(angle / 2);
|
|
2020
|
+
const t1x = x1 + u1x * d;
|
|
2021
|
+
const t1y = y1 + u1y * d;
|
|
2022
|
+
const t2x = x1 + u2x * d;
|
|
2023
|
+
const t2y = y1 + u2y * d;
|
|
2024
|
+
let bx = u1x + u2x;
|
|
2025
|
+
let by = u1y + u2y;
|
|
2026
|
+
const bl = Math.hypot(bx, by);
|
|
2027
|
+
if (bl === 0) {
|
|
2028
|
+
this.lineTo(x1, y1);
|
|
2029
|
+
return;
|
|
2030
|
+
}
|
|
2031
|
+
bx /= bl;
|
|
2032
|
+
by /= bl;
|
|
2033
|
+
const centreDistance = r / Math.sin(angle / 2);
|
|
2034
|
+
const cx = x1 + bx * centreDistance;
|
|
2035
|
+
const cy = y1 + by * centreDistance;
|
|
2036
|
+
this.lineTo(t1x, t1y);
|
|
2037
|
+
const theta1 = Math.atan2(t1y - cy, t1x - cx);
|
|
2038
|
+
const theta2 = Math.atan2(t2y - cy, t2x - cx);
|
|
2039
|
+
let delta = theta2 - theta1;
|
|
2040
|
+
while (delta > Math.PI)
|
|
2041
|
+
delta -= 2 * Math.PI;
|
|
2042
|
+
while (delta < -Math.PI)
|
|
2043
|
+
delta += 2 * Math.PI;
|
|
2044
|
+
this.appendUserCurves(arcCentreToCubics(cx, cy, r, r, 0, theta1, delta));
|
|
2045
|
+
this.userX = t2x;
|
|
2046
|
+
this.userY = t2y;
|
|
2047
|
+
}
|
|
2048
|
+
}
|
|
2049
|
+
export function runCanvasScript(script) {
|
|
2050
|
+
return new CanvasRecorder().run(script);
|
|
2051
|
+
}
|
|
2052
|
+
// ── `on:frame`: the display-link contract ────────────────────────────────────────────
|
|
2053
|
+
/** the 60/s budget — the SAME constant the scene kernel uses, deliberately shared */
|
|
2054
|
+
export const CANVAS_FRAME_MIN_INTERVAL_MS = 1000 / 60;
|
|
2055
|
+
/**
|
|
2056
|
+
* An always-running frame callback is a BATTERY BUG, so the law is enforced by fixture,
|
|
2057
|
+
* not by intent. The loop is installed ONLY while an `on:frame` handler is bound AND the
|
|
2058
|
+
* canvas is mounted AND it is on screen; ticks arriving while the loop is not installed
|
|
2059
|
+
* are DROPPED, never queued; `delta` is 0 on the first tick after every install and `time`
|
|
2060
|
+
* is accumulated delta, so it EXCLUDES offscreen intervals; `frame` counts emissions.
|
|
2061
|
+
*/
|
|
2062
|
+
export class CanvasFrameLoop {
|
|
2063
|
+
mounted = false;
|
|
2064
|
+
visible = true;
|
|
2065
|
+
installedFlag = false;
|
|
2066
|
+
lastEmitted = null;
|
|
2067
|
+
time = 0;
|
|
2068
|
+
frame = 0;
|
|
2069
|
+
installs = 0;
|
|
2070
|
+
uninstalls = 0;
|
|
2071
|
+
bound;
|
|
2072
|
+
constructor(bound) {
|
|
2073
|
+
this.bound = bound;
|
|
2074
|
+
}
|
|
2075
|
+
get installed() {
|
|
2076
|
+
return this.installedFlag;
|
|
2077
|
+
}
|
|
2078
|
+
setBound(value) {
|
|
2079
|
+
this.bound = value;
|
|
2080
|
+
this.settle();
|
|
2081
|
+
}
|
|
2082
|
+
setMounted(value) {
|
|
2083
|
+
this.mounted = value;
|
|
2084
|
+
this.settle();
|
|
2085
|
+
}
|
|
2086
|
+
setVisible(value) {
|
|
2087
|
+
this.visible = value;
|
|
2088
|
+
this.settle();
|
|
2089
|
+
}
|
|
2090
|
+
settle() {
|
|
2091
|
+
const want = this.bound && this.mounted && this.visible;
|
|
2092
|
+
if (want && !this.installedFlag) {
|
|
2093
|
+
this.installedFlag = true;
|
|
2094
|
+
this.installs += 1;
|
|
2095
|
+
this.lastEmitted = null;
|
|
2096
|
+
}
|
|
2097
|
+
else if (!want && this.installedFlag) {
|
|
2098
|
+
this.installedFlag = false;
|
|
2099
|
+
this.uninstalls += 1;
|
|
2100
|
+
}
|
|
2101
|
+
}
|
|
2102
|
+
/** one raw platform tick (ms); the payload to emit, or null when dropped or coalesced */
|
|
2103
|
+
tick(nowMs) {
|
|
2104
|
+
if (!this.installedFlag)
|
|
2105
|
+
return null;
|
|
2106
|
+
if (this.lastEmitted === null) {
|
|
2107
|
+
this.lastEmitted = nowMs;
|
|
2108
|
+
const payload = { time: this.time, delta: 0, frame: this.frame };
|
|
2109
|
+
this.frame += 1;
|
|
2110
|
+
return payload;
|
|
2111
|
+
}
|
|
2112
|
+
const gap = nowMs - this.lastEmitted;
|
|
2113
|
+
if (gap < CANVAS_FRAME_MIN_INTERVAL_MS)
|
|
2114
|
+
return null;
|
|
2115
|
+
this.lastEmitted = nowMs;
|
|
2116
|
+
const delta = gap / 1000;
|
|
2117
|
+
this.time += delta;
|
|
2118
|
+
const payload = { time: this.time, delta, frame: this.frame };
|
|
2119
|
+
this.frame += 1;
|
|
2120
|
+
return payload;
|
|
2121
|
+
}
|
|
2122
|
+
}
|
|
2123
|
+
/** the pure fold the corpus pins: a bound flag plus a lifecycle/tick event list */
|
|
2124
|
+
export function canvasFrameSchedule(bound, events) {
|
|
2125
|
+
const loop = new CanvasFrameLoop(bound);
|
|
2126
|
+
const emitted = [];
|
|
2127
|
+
for (const event of events) {
|
|
2128
|
+
if (event.type === "mount")
|
|
2129
|
+
loop.setMounted(true);
|
|
2130
|
+
else if (event.type === "unmount")
|
|
2131
|
+
loop.setMounted(false);
|
|
2132
|
+
else if (event.type === "visible")
|
|
2133
|
+
loop.setVisible(event.value);
|
|
2134
|
+
else {
|
|
2135
|
+
const payload = loop.tick(event.at);
|
|
2136
|
+
if (payload !== null)
|
|
2137
|
+
emitted.push(payload);
|
|
2138
|
+
}
|
|
2139
|
+
}
|
|
2140
|
+
return { emitted, installs: loop.installs, uninstalls: loop.uninstalls, installed: loop.installed };
|
|
2141
|
+
}
|
|
2142
|
+
// ── accessibility ────────────────────────────────────────────────────────────────────
|
|
2143
|
+
/** the gesture words that make a canvas INTERACTIVE. `on:draw` and `on:frame` are
|
|
2144
|
+
* deliberately absent: a painted background is decorative. */
|
|
2145
|
+
export const CANVAS_GESTURE_HANDLERS = [
|
|
2146
|
+
"on:tap", "on:doubletap", "on:long-press", "on:long-press-end", "on:drag", "on:pan",
|
|
2147
|
+
"on:pinch", "on:rotate", "on:swipe", "on:press", "on:adjust",
|
|
2148
|
+
];
|
|
2149
|
+
export const CANVAS_A11Y_LINT_CODE = "canvas_a11y_label";
|
|
2150
|
+
export const CANVAS_A11Y_LINT_MESSAGE = "<canvas> with a gesture handler needs aria-label — a canvas is opaque to assistive tech";
|
|
2151
|
+
/**
|
|
2152
|
+
* A canvas is OPAQUE to assistive tech by construction — there is no view tree to inspect,
|
|
2153
|
+
* only pixels — so the semantics are declared or they do not exist. One fold, one rule,
|
|
2154
|
+
* three surfaces: this is what all three renderers apply AND what `lint_dsx.rb` enforces.
|
|
2155
|
+
*/
|
|
2156
|
+
export function canvasA11y(attrs, a11yChildren) {
|
|
2157
|
+
const interactive = CANVAS_GESTURE_HANDLERS.some((name) => {
|
|
2158
|
+
const raw = attrs[name];
|
|
2159
|
+
return raw !== undefined && raw !== null && String(raw).trim() !== "";
|
|
2160
|
+
});
|
|
2161
|
+
const rawLabel = attrs["aria-label"];
|
|
2162
|
+
const trimmed = rawLabel === undefined || rawLabel === null ? "" : String(rawLabel).trim();
|
|
2163
|
+
const label = trimmed === "" ? null : trimmed;
|
|
2164
|
+
const children = (a11yChildren ?? []).map((child) => ({
|
|
2165
|
+
role: child.role ?? "image",
|
|
2166
|
+
label: child.label ?? "",
|
|
2167
|
+
value: child.value ?? null,
|
|
2168
|
+
}));
|
|
2169
|
+
const declared = label !== null || children.length > 0;
|
|
2170
|
+
const role = children.length > 0
|
|
2171
|
+
? "group"
|
|
2172
|
+
: interactive
|
|
2173
|
+
? (label !== null ? "button" : "group")
|
|
2174
|
+
: (label !== null ? "image" : "none");
|
|
2175
|
+
return {
|
|
2176
|
+
interactive,
|
|
2177
|
+
label,
|
|
2178
|
+
children,
|
|
2179
|
+
role,
|
|
2180
|
+
hidden: !interactive && !declared,
|
|
2181
|
+
lint: interactive && !declared
|
|
2182
|
+
? { code: CANVAS_A11Y_LINT_CODE, level: "error", message: CANVAS_A11Y_LINT_MESSAGE }
|
|
2183
|
+
: null,
|
|
2184
|
+
};
|
|
2185
|
+
}
|
|
2186
|
+
//# sourceMappingURL=canvas-core.js.map
|