@astryxdesign/cli 0.7.18-canary.gfacee17 → 0.7.18
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/CHANGELOG.md +185 -19
- package/README.md +19 -19
- package/api/component/_adapter.d.mts +2 -1
- package/api/component/_adapter.mjs +2 -1
- package/api/docs/_adapter.d.mts +6 -5
- package/api/docs/_adapter.mjs +36 -5
- package/api/doctor/_adapter.d.mts +6 -3
- package/api/doctor/_adapter.mjs +13 -5
- package/api/doctor/doctor.mjs +5 -5
- package/api/doctor/font-packages.d.mts +36 -0
- package/api/doctor/font-packages.mjs +124 -0
- package/api/doctor/theme-checks.mjs +110 -49
- package/api/experimental.d.mts +0 -2
- package/api/experimental.mjs +0 -12
- package/api/icon/_adapter.d.mts +2 -2
- package/api/icon/_adapter.mjs +6 -2
- package/api/icon/_authoring.mjs +22 -13
- package/api/icon/_default-look.mjs +15 -15
- package/api/icon/add/add.mjs +14 -9
- package/api/icon/icon.doc.mjs +7 -7
- package/api/icon/icon.type.d.mts +33 -33
- package/api/icon/icon.type.mjs +45 -45
- package/api/icon/iconAdd.doc.mjs +9 -10
- package/api/icon/iconMap.doc.mjs +11 -12
- package/api/icon/iconNewDefinition.doc.mjs +8 -8
- package/api/icon/iconNewSet.doc.mjs +5 -5
- package/api/icon/map/map.mjs +7 -7
- package/api/icon/use/use.mjs +4 -4
- package/api/init/run/run.mjs +0 -1
- package/api/integration/add-helpers.mjs +1 -1
- package/api/integration/validate-integration.type.d.mts +6 -6
- package/api/integration/validate-integration.type.mjs +6 -6
- package/api/render/_adapter.d.mts +5 -3
- package/api/render/_adapter.mjs +64 -4
- package/api/render/render.doc.mjs +9 -2
- package/api/render/render.mjs +5 -2
- package/api/render/render.type.d.mts +17 -0
- package/api/render/render.type.mjs +10 -0
- package/api/search/ORDERING.md +1 -1
- package/api/search/_candidate-score.mjs +20 -0
- package/api/search/_words.d.mts +8 -0
- package/api/search/_words.mjs +15 -0
- package/api/template/template.type.d.mts +2 -2
- package/api/template/template.type.mjs +2 -2
- package/api/theme/_adapter.mjs +19 -11
- package/api/theme/build/build.d.mts +19 -51
- package/api/theme/build/build.mjs +195 -189
- package/api/theme/build/font-warning.d.mts +19 -2
- package/api/theme/build/font-warning.mjs +46 -5
- package/api/theme/generateTonalPalette.doc.mjs +1 -1
- package/api/theme/palette/generate/derive-look.mjs +108 -37
- package/api/theme/palette/generate/mapping.mjs +2 -0
- package/api/theme/template/template.d.mts +0 -17
- package/api/theme/template/template.mjs +1 -42
- package/api/theme/theme.type.d.mts +5 -4
- package/api/theme/theme.type.mjs +5 -4
- package/api/theme/themePaletteGenerate.doc.mjs +1 -1
- package/api/theme/themeRemove.doc.mjs +1 -1
- package/assets/codemods/transforms/v0.7.0/preserve-bottom-sheet-content-padding.mjs +3 -1
- package/assets/component-list.generated.json +1 -1
- package/assets/discover/official-packages.json +26 -2
- package/assets/docs/author-a-theme.doc.mjs +6 -5
- package/assets/docs/browser-support.doc.mjs +1 -0
- package/assets/docs/icons.doc.mjs +8 -7
- package/assets/docs/motion.doc.mjs +46 -0
- package/assets/docs/theme.doc.dense.mjs +6 -17
- package/assets/docs/theme.doc.mjs +6 -16
- package/assets/docs/theme.doc.zh.mjs +6 -16
- package/assets/docs/tree/ci-and-containers.doc.mjs +1 -1
- package/assets/docs/tree/data-colors-in-renderers.doc.mjs +400 -0
- package/assets/docs/tree/font-setup.doc.mjs +30 -0
- package/assets/docs/tree/icon-sets.doc.mjs +2 -2
- package/assets/docs/tree/share-a-page.doc.mjs +1 -1
- package/assets/docs/tree/template-icons.doc.mjs +3 -3
- package/assets/docs/tree/tokens-color.doc.mjs +5 -0
- package/assets/docs/tree/tokens.doc.mjs +4 -3
- package/assets/docs/tree/updates.doc.mjs +1 -1
- package/assets/docs/tree/visualization.doc.mjs +30 -0
- package/assets/docs-routes.generated.json +1 -1
- package/assets/docs.generated.json +2 -2
- package/assets/icons/definitions/drag.doc.mjs +1 -0
- package/assets/search-catalog.generated.json +2 -2
- package/assets/self-audit.generated.json +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardElevated.doc.mjs +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardElevated.tsx +20 -10
- package/assets/templates/blocks/components/Card/SelectableCardShowcase.doc.mjs +1 -1
- package/assets/templates/blocks/components/Card/SelectableCardShowcase.tsx +36 -16
- package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMentionTrigger.tsx +6 -0
- package/assets/templates/blocks/components/ChatComposerInput/ChatComposerInputMultipleTriggers.tsx +6 -0
- package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.doc.mjs +14 -0
- package/assets/templates/blocks/components/Hooks/useIndicatorFocusRingHookUsage.tsx +63 -0
- package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.doc.mjs +29 -0
- package/assets/templates/blocks/components/Hooks/useLongPressHookUsage.tsx +237 -0
- package/assets/templates/blocks/components/SideNav/SideNavShowcase.tsx +10 -72
- package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.doc.mjs +1 -1
- package/assets/templates/blocks/components/SideNav/SideNavWithHeaderMenu.tsx +7 -7
- package/assets/templates/blocks/components/Table/TableCustomRowTable.doc.mjs +14 -0
- package/assets/templates/blocks/components/Table/TableCustomRowTable.tsx +96 -0
- package/assets/templates/components.generated.json +1 -1
- package/assets/templates/pages/ai-chat-landing/page.tsx +4 -0
- package/assets/templates/pages/dashboard-scorecard/page.tsx +26 -7
- package/assets/templates/pages/editor/page.tsx +13 -3
- package/assets/templates/themes/butter/butterPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/butter/butterTheme.ts +12 -10
- package/assets/templates/themes/butter/icons.tsx +2 -0
- package/assets/templates/themes/chocolate/chocolateTheme.ts +1 -0
- package/assets/templates/themes/chocolate/icons.tsx +2 -0
- package/assets/templates/themes/gothic/gothicPaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/gothic/gothicTheme.ts +1 -0
- package/assets/templates/themes/gothic/icons.tsx +2 -0
- package/assets/templates/themes/matcha/icons.tsx +2 -0
- package/assets/templates/themes/matcha/matchaPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/matcha/matchaTheme.ts +1 -0
- package/assets/templates/themes/neutral/icons.tsx +2 -0
- package/assets/templates/themes/neutral/neutralPaletteRefs.generated.ts +2 -2
- package/assets/templates/themes/neutral/neutralTheme.ts +1 -0
- package/assets/templates/themes/stone/icons.tsx +2 -0
- package/assets/templates/themes/stone/stonePaletteRefs.generated.ts +1 -1
- package/assets/templates/themes/stone/stoneTheme.ts +1 -0
- package/assets/templates/themes/y2k/icons.tsx +2 -0
- package/assets/templates/themes/y2k/y2kTheme.ts +1 -0
- package/assets/theme.template.ts +9 -5
- package/authoring/config/parse.mjs +1 -1
- package/authoring/doctypes/base/type.ts +2 -3
- package/authoring/doctypes/component/component.doc.mjs +1 -1
- package/authoring/doctypes/icon-set/icon-names.mjs +1 -0
- package/authoring/integration/integration.doc.mjs +1 -1
- package/authoring/integration/type.ts +1 -1
- package/clients/cli/build-id.generated.json +1 -1
- package/clients/cli/commands/doctor.mjs +2 -2
- package/clients/cli/commands/experimental/export.mjs +2 -1
- package/clients/cli/commands/experimental/icon.mjs +17 -17
- package/clients/cli/commands/experimental/index.mjs +0 -5
- package/clients/cli/commands/experimental/render-output.mjs +9 -0
- package/clients/cli/commands/experimental-export.doc.mjs +6 -0
- package/clients/cli/commands/experimental-icon-add.doc.mjs +8 -8
- package/clients/cli/commands/experimental-icon-map.doc.mjs +6 -6
- package/clients/cli/commands/experimental-icon-new-definition.doc.mjs +6 -6
- package/clients/cli/commands/experimental-icon-new-set.doc.mjs +3 -3
- package/clients/cli/commands/experimental-icon-use.doc.mjs +1 -1
- package/clients/cli/commands/experimental-icon.doc.mjs +6 -6
- package/clients/cli/commands/experimental.doc.mjs +2 -6
- package/clients/cli/commands/template.mjs +1 -1
- package/clients/cli/commands/theme-build.doc.mjs +8 -1
- package/clients/cli/commands/theme-palette-generate.doc.mjs +1 -1
- package/clients/cli/commands/theme-remove.doc.mjs +1 -1
- package/clients/cli/commands/theme.doc.mjs +1 -11
- package/clients/cli/commands/theme.mjs +110 -475
- package/clients/cli/commands.generated.json +1 -1
- package/clients/cli/index.mjs +4 -34
- package/clients/cli/lib/base-gate.mjs +2 -3
- package/clients/cli/lib/cli-error.mjs +8 -72
- package/clients/cli/lib/define-command.mjs +0 -19
- package/clients/cli/lib/json-shim.mjs +11 -633
- package/clients/cli/lib/manifest.mjs +0 -4
- package/clients/cli/lib/stray-option.mjs +1 -2
- package/clients/cli/manifest.generated.json +1 -1
- package/foundation/agent-docs/agent-docs.mjs +1 -1
- package/foundation/color/css-color.mjs +2 -3
- package/foundation/discovery/app-layout.d.mts +13 -0
- package/foundation/discovery/app-layout.mjs +35 -0
- package/foundation/discovery/cli-self-docs.mjs +4 -4
- package/foundation/discovery/fixture-refs.d.mts +51 -0
- package/foundation/discovery/fixture-refs.mjs +351 -0
- package/foundation/discovery/icon-definition-discovery.mjs +7 -7
- package/foundation/discovery/icon-ranking.mjs +2 -2
- package/foundation/discovery/icon-set-checks.mjs +6 -6
- package/foundation/discovery/icon-set-discovery.mjs +2 -2
- package/foundation/discovery/icon-set-writer.d.mts +24 -7
- package/foundation/discovery/icon-set-writer.mjs +75 -31
- package/foundation/discovery/static-descriptor.d.mts +49 -0
- package/foundation/discovery/static-descriptor.mjs +548 -0
- package/foundation/discovery/template-adapter.d.mts +0 -47
- package/foundation/discovery/template-adapter.mjs +1 -333
- package/foundation/discovery/theme-discovery.d.mts +6 -47
- package/foundation/discovery/theme-discovery.mjs +15 -537
- package/foundation/doc-compiler/bundle.mjs +2 -2
- package/foundation/doc-compiler/import.mjs +1 -1
- package/foundation/env/user-dirs.d.mts +5 -2
- package/foundation/env/user-dirs.mjs +12 -5
- package/foundation/fonts/next-font.d.mts +26 -0
- package/foundation/fonts/next-font.mjs +149 -0
- package/foundation/fs/empty-folder.mjs +2 -2
- package/foundation/fs/read-record.mjs +11 -4
- package/foundation/pictures/pictures.d.mts +152 -0
- package/foundation/pictures/pictures.mjs +565 -0
- package/foundation/release/folder-lock.mjs +3 -2
- package/foundation/render/cut-short.d.mts +2 -2
- package/foundation/render/cut-short.mjs +2 -2
- package/foundation/render/drawn-contrast.d.mts +2 -2
- package/foundation/render/drawn-contrast.mjs +2 -2
- package/foundation/render/font-probe.d.mts +2 -2
- package/foundation/render/font-probe.mjs +2 -2
- package/foundation/render/layers.mjs +1 -2
- package/foundation/render/package-resolve-hooks.d.mts +25 -5
- package/foundation/render/package-resolve-hooks.mjs +77 -14
- package/foundation/render/page-worker.mjs +24 -12
- package/foundation/render/part-boxes.d.mts +2 -3
- package/foundation/render/part-boxes.mjs +2 -3
- package/foundation/render/picture-chrome.mjs +18 -15
- package/foundation/render/picture-playwright.mjs +10 -7
- package/foundation/render/picture-window.d.mts +13 -0
- package/foundation/render/picture-window.mjs +31 -0
- package/foundation/render/render-page.d.mts +1 -1
- package/foundation/render/render-page.mjs +4 -5
- package/foundation/render/single-copy-hooks.d.mts +2 -0
- package/foundation/render/single-copy-hooks.mjs +2 -0
- package/foundation/response/error-codes.doc.mjs +14 -14
- package/foundation/response/error-codes.mjs +13 -13
- package/foundation/response/response-types.doc.mjs +8 -69
- package/foundation/runtime/css-tree.mjs +2 -2
- package/foundation/runtime/word-table.d.mts +1 -1
- package/foundation/runtime/word-table.mjs +1 -1
- package/foundation/suggest/other-command.d.mts +52 -15
- package/foundation/suggest/other-command.mjs +168 -45
- package/foundation/text/suggest-order.d.mts +7 -0
- package/foundation/text/suggest-order.mjs +1 -1
- package/package.json +7 -8
- package/api/theme-edit/_adapter.d.mts +0 -228
- package/api/theme-edit/_adapter.mjs +0 -2025
- package/api/theme-edit/_catalog.d.mts +0 -172
- package/api/theme-edit/_catalog.mjs +0 -806
- package/api/theme-edit/_check.d.mts +0 -391
- package/api/theme-edit/_check.mjs +0 -9370
- package/api/theme-edit/_color.d.mts +0 -135
- package/api/theme-edit/_color.mjs +0 -480
- package/api/theme-edit/_compiler.d.mts +0 -77
- package/api/theme-edit/_compiler.mjs +0 -1053
- package/api/theme-edit/_context.d.mts +0 -399
- package/api/theme-edit/_context.mjs +0 -1778
- package/api/theme-edit/_css.d.mts +0 -169
- package/api/theme-edit/_css.mjs +0 -1167
- package/api/theme-edit/_edit.d.mts +0 -167
- package/api/theme-edit/_edit.mjs +0 -11220
- package/api/theme-edit/_how-commands.d.mts +0 -315
- package/api/theme-edit/_how-commands.mjs +0 -1664
- package/api/theme-edit/_how-question.d.mts +0 -341
- package/api/theme-edit/_how-question.mjs +0 -33291
- package/api/theme-edit/_how-sides.d.mts +0 -22
- package/api/theme-edit/_how-sides.mjs +0 -173
- package/api/theme-edit/_how-steps.d.mts +0 -42
- package/api/theme-edit/_how-steps.mjs +0 -208
- package/api/theme-edit/_how-values.d.mts +0 -29
- package/api/theme-edit/_how-values.mjs +0 -111
- package/api/theme-edit/_how-words.d.mts +0 -15
- package/api/theme-edit/_how-words.mjs +0 -239
- package/api/theme-edit/_how.d.mts +0 -55
- package/api/theme-edit/_how.mjs +0 -2388
- package/api/theme-edit/_importers.d.mts +0 -38
- package/api/theme-edit/_importers.mjs +0 -188
- package/api/theme-edit/_marks.d.mts +0 -39
- package/api/theme-edit/_marks.mjs +0 -67
- package/api/theme-edit/_missed.d.mts +0 -56
- package/api/theme-edit/_missed.mjs +0 -1128
- package/api/theme-edit/_name-rule.d.mts +0 -11
- package/api/theme-edit/_name-rule.mjs +0 -12
- package/api/theme-edit/_normal.d.mts +0 -365
- package/api/theme-edit/_normal.mjs +0 -1978
- package/api/theme-edit/_overview.d.mts +0 -9
- package/api/theme-edit/_overview.mjs +0 -293
- package/api/theme-edit/_palette.d.mts +0 -616
- package/api/theme-edit/_palette.mjs +0 -2776
- package/api/theme-edit/_part-pieces.d.mts +0 -54
- package/api/theme-edit/_part-pieces.mjs +0 -179
- package/api/theme-edit/_part-words.d.mts +0 -39
- package/api/theme-edit/_part-words.mjs +0 -242
- package/api/theme-edit/_parts.d.mts +0 -402
- package/api/theme-edit/_parts.mjs +0 -2294
- package/api/theme-edit/_picture-traits.d.mts +0 -259
- package/api/theme-edit/_picture-traits.mjs +0 -1030
- package/api/theme-edit/_png.d.mts +0 -94
- package/api/theme-edit/_png.mjs +0 -479
- package/api/theme-edit/_preview-app.d.mts +0 -172
- package/api/theme-edit/_preview-app.mjs +0 -6430
- package/api/theme-edit/_preview.d.mts +0 -549
- package/api/theme-edit/_preview.mjs +0 -3264
- package/api/theme-edit/_project.d.mts +0 -207
- package/api/theme-edit/_project.mjs +0 -643
- package/api/theme-edit/_resolved.d.mts +0 -46
- package/api/theme-edit/_resolved.mjs +0 -800
- package/api/theme-edit/_set-line.d.mts +0 -29
- package/api/theme-edit/_set-line.mjs +0 -100
- package/api/theme-edit/_settings.d.mts +0 -2426
- package/api/theme-edit/_settings.mjs +0 -18435
- package/api/theme-edit/_tables.d.mts +0 -227
- package/api/theme-edit/_tables.mjs +0 -1128
- package/api/theme-edit/_text-looks.d.mts +0 -78
- package/api/theme-edit/_text-looks.mjs +0 -376
- package/api/theme-edit/_vet.d.mts +0 -50
- package/api/theme-edit/_vet.mjs +0 -166
- package/api/theme-edit/how/how.d.mts +0 -17
- package/api/theme-edit/how/how.mjs +0 -28
- package/api/theme-edit/new/new.d.mts +0 -11
- package/api/theme-edit/new/new.mjs +0 -22
- package/api/theme-edit/preview-add/preview-add.d.mts +0 -14
- package/api/theme-edit/preview-add/preview-add.mjs +0 -25
- package/api/theme-edit/preview-app/preview-app.d.mts +0 -13
- package/api/theme-edit/preview-app/preview-app.mjs +0 -24
- package/api/theme-edit/preview-list/preview-list.d.mts +0 -10
- package/api/theme-edit/preview-list/preview-list.mjs +0 -21
- package/api/theme-edit/preview-remove/preview-remove.d.mts +0 -12
- package/api/theme-edit/preview-remove/preview-remove.mjs +0 -23
- package/api/theme-edit/set/set.d.mts +0 -13
- package/api/theme-edit/set/set.mjs +0 -24
- package/api/theme-edit/show/show.d.mts +0 -13
- package/api/theme-edit/show/show.mjs +0 -24
- package/api/theme-edit/theme-edit.d.mts +0 -12
- package/api/theme-edit/theme-edit.mjs +0 -36
- package/api/theme-edit/theme-edit.type.d.mts +0 -730
- package/api/theme-edit/theme-edit.type.mjs +0 -342
- package/api/theme-edit/themeEditHow.doc.d.mts +0 -6
- package/api/theme-edit/themeEditHow.doc.mjs +0 -105
- package/api/theme-edit/themeEditNew.doc.d.mts +0 -5
- package/api/theme-edit/themeEditNew.doc.mjs +0 -112
- package/api/theme-edit/themeEditPreviewAdd.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewAdd.doc.mjs +0 -121
- package/api/theme-edit/themeEditPreviewApp.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewApp.doc.mjs +0 -87
- package/api/theme-edit/themeEditPreviewList.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewList.doc.mjs +0 -62
- package/api/theme-edit/themeEditPreviewRemove.doc.d.mts +0 -6
- package/api/theme-edit/themeEditPreviewRemove.doc.mjs +0 -96
- package/api/theme-edit/themeEditSet.doc.d.mts +0 -6
- package/api/theme-edit/themeEditSet.doc.mjs +0 -175
- package/api/theme-edit/themeEditShow.doc.d.mts +0 -6
- package/api/theme-edit/themeEditShow.doc.mjs +0 -118
- package/api/theme-edit/themeEditUndo.doc.d.mts +0 -6
- package/api/theme-edit/themeEditUndo.doc.mjs +0 -133
- package/api/theme-edit/undo/undo.d.mts +0 -13
- package/api/theme-edit/undo/undo.mjs +0 -24
- package/assets/preview-app/README.md +0 -6
- package/assets/preview-app/css.d.ts +0 -5
- package/assets/preview-app/gitignore +0 -4
- package/assets/preview-app/next.config.mjs +0 -12
- package/assets/preview-app/package.json +0 -24
- package/assets/preview-app/src/app/globals.css +0 -6
- package/assets/preview-app/src/app/layout.tsx +0 -21
- package/assets/preview-app/src/app/page.tsx +0 -94
- package/assets/preview-app/src/app/providers.tsx +0 -16
- package/assets/preview-app/tsconfig.json +0 -27
- package/clients/cli/commands/experimental/theme.mjs +0 -2990
- package/clients/cli/commands/experimental-theme-add.doc.mjs +0 -80
- package/clients/cli/commands/experimental-theme-compile.doc.mjs +0 -98
- package/clients/cli/commands/experimental-theme-editor-how.doc.mjs +0 -196
- package/clients/cli/commands/experimental-theme-editor-new.doc.mjs +0 -174
- package/clients/cli/commands/experimental-theme-editor-set.doc.mjs +0 -139
- package/clients/cli/commands/experimental-theme-editor-show.doc.mjs +0 -77
- package/clients/cli/commands/experimental-theme-editor-undo.doc.mjs +0 -105
- package/clients/cli/commands/experimental-theme-editor.doc.mjs +0 -49
- package/clients/cli/commands/experimental-theme-eject.doc.mjs +0 -72
- package/clients/cli/commands/experimental-theme-list.doc.mjs +0 -49
- package/clients/cli/commands/experimental-theme-preview-add.doc.mjs +0 -88
- package/clients/cli/commands/experimental-theme-preview-app.doc.mjs +0 -61
- package/clients/cli/commands/experimental-theme-preview-list.doc.mjs +0 -48
- package/clients/cli/commands/experimental-theme-preview-remove.doc.mjs +0 -59
- package/clients/cli/commands/experimental-theme-preview.doc.mjs +0 -41
- package/clients/cli/commands/experimental-theme-remove.doc.mjs +0 -57
- package/clients/cli/commands/experimental-theme-targets.doc.mjs +0 -62
- package/clients/cli/commands/experimental-theme-template.doc.mjs +0 -63
- package/clients/cli/commands/experimental-theme-use.doc.mjs +0 -57
- package/clients/cli/commands/experimental-theme.doc.mjs +0 -60
- package/foundation/fonts/fontsource-families.generated.d.mts +0 -5
- package/foundation/fonts/fontsource-families.generated.mjs +0 -2110
|
@@ -1,94 +0,0 @@
|
|
|
1
|
-
// @generated by scripts/sync-api-types.mjs from the JSDoc in api/**/*.mjs.
|
|
2
|
-
// DO NOT EDIT — run `pnpm sync:api-types` to regenerate.
|
|
3
|
-
|
|
4
|
-
/** @typedef {{width: number, height: number, rgba: Uint8Array}} Picture */
|
|
5
|
-
/**
|
|
6
|
-
* Decode a PNG file's bytes. Interlaced pictures are refused, and so is
|
|
7
|
-
* any file whose header, chunks or pixel data do not match the PNG format.
|
|
8
|
-
* @param {Buffer} bytes
|
|
9
|
-
* @returns {Picture}
|
|
10
|
-
*/
|
|
11
|
-
export function decodePng(bytes: Buffer): Picture;
|
|
12
|
-
/**
|
|
13
|
-
* The exact color at each point, with its alpha (0 is fully see-through,
|
|
14
|
-
* 1 fully opaque). A point outside the picture has no color.
|
|
15
|
-
* @param {Picture} pic
|
|
16
|
-
* @param {Array<{x: number, y: number}>} points
|
|
17
|
-
* @returns {Array<{at: string, color: string | null, alpha: number | null}>}
|
|
18
|
-
*/
|
|
19
|
-
export function colorsAt(pic: Picture, points: Array<{
|
|
20
|
-
x: number;
|
|
21
|
-
y: number;
|
|
22
|
-
}>): Array<{
|
|
23
|
-
at: string;
|
|
24
|
-
color: string | null;
|
|
25
|
-
alpha: number | null;
|
|
26
|
-
}>;
|
|
27
|
-
/**
|
|
28
|
-
* The main colors of a picture: every pixel (on a grid past 4 million)
|
|
29
|
-
* bucketed to 5 bits per channel, biggest share first, each with where it sits. A fully
|
|
30
|
-
* see-through pixel shows no color, so it is skipped; a partly see-through
|
|
31
|
-
* one counts as it looks over white.
|
|
32
|
-
* @param {Picture} pic
|
|
33
|
-
* @param {number} count
|
|
34
|
-
* @returns {Array<{color: string, share: number, where: string}>}
|
|
35
|
-
*/
|
|
36
|
-
export function mainColors(pic: Picture, count: number): Array<{
|
|
37
|
-
color: string;
|
|
38
|
-
share: number;
|
|
39
|
-
where: string;
|
|
40
|
-
}>;
|
|
41
|
-
/**
|
|
42
|
-
* The `tEXt` chunks of a PNG, keyword to text; empty for a file that is not
|
|
43
|
-
* a PNG or cannot be read whole.
|
|
44
|
-
* @param {Buffer} bytes
|
|
45
|
-
* @returns {Record<string, string>}
|
|
46
|
-
*/
|
|
47
|
-
export function pngTexts(bytes: Buffer): Record<string, string>;
|
|
48
|
-
/**
|
|
49
|
-
* A PNG with a `tEXt` chunk added right after its header (any chunk with the
|
|
50
|
-
* same keyword replaced), or the bytes as they were when they are not a PNG.
|
|
51
|
-
* @param {Buffer} bytes
|
|
52
|
-
* @param {string} keyword
|
|
53
|
-
* @param {string} text
|
|
54
|
-
* @returns {Buffer}
|
|
55
|
-
*/
|
|
56
|
-
export function withPngText(bytes: Buffer, keyword: string, text: string): Buffer;
|
|
57
|
-
/**
|
|
58
|
-
* The bytes of a PNG holding a picture's RGBA pixels, 8 bits a channel, each
|
|
59
|
-
* row unfiltered.
|
|
60
|
-
* @param {Picture} pic
|
|
61
|
-
* @returns {Buffer}
|
|
62
|
-
*/
|
|
63
|
-
export function encodePng({ width, height, rgba }: Picture): Buffer;
|
|
64
|
-
/**
|
|
65
|
-
* A tall picture cut into pictures, in order, each cut only at the top of a
|
|
66
|
-
* row of parts: the end of a run of at least `gap` rows of one color (the
|
|
67
|
-
* page between two rows of cells; a cell's dashed edge breaks every run
|
|
68
|
-
* inside it). A piece ends at the lowest row top within `most` pixels; a
|
|
69
|
-
* row taller than that is a piece of its own, never cut through. A last
|
|
70
|
-
* piece shorter than `least` joins the one before it. A picture no taller
|
|
71
|
-
* than `most` is itself alone.
|
|
72
|
-
* @param {Picture} pic
|
|
73
|
-
* @param {number} most
|
|
74
|
-
* @param {{gap?: number, least?: number}} [options]
|
|
75
|
-
* @returns {Picture[]}
|
|
76
|
-
*/
|
|
77
|
-
export function cutTall(pic: Picture, most: number, { gap, least }?: {
|
|
78
|
-
gap?: number;
|
|
79
|
-
least?: number;
|
|
80
|
-
}): Picture[];
|
|
81
|
-
/**
|
|
82
|
-
* The most pixels a picture may have: about a 6K screen. A bigger header is
|
|
83
|
-
* refused before any memory is taken for it.
|
|
84
|
-
*/
|
|
85
|
-
export const MAX_PIXELS: 40000000;
|
|
86
|
-
/** A 16-bit picture takes twice the memory per pixel, so half as many. */
|
|
87
|
-
export const MAX_PIXELS_16: 20000000;
|
|
88
|
-
/** The keyword of the text chunk the theme editor stamps its pictures with. */
|
|
89
|
-
export const EDITOR_STAMP: "astryx-theme-editor";
|
|
90
|
-
export type Picture = {
|
|
91
|
-
width: number;
|
|
92
|
-
height: number;
|
|
93
|
-
rgba: Uint8Array;
|
|
94
|
-
};
|
package/api/theme-edit/_png.mjs
DELETED
|
@@ -1,479 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Meta Platforms, Inc. and affiliates.
|
|
2
|
-
|
|
3
|
-
/**
|
|
4
|
-
* @file Read a PNG in plain Node: check the header and size before taking
|
|
5
|
-
* memory, decompress with zlib to exactly the size the header names, undo
|
|
6
|
-
* the row filters, and give RGBA pixels. Then the main colors of a picture,
|
|
7
|
-
* or the exact color and alpha at given points.
|
|
8
|
-
* @position api/theme-edit — pure helpers for `experimental theme editor how <picture.png>`
|
|
9
|
-
*/
|
|
10
|
-
|
|
11
|
-
import * as zlib from 'node:zlib';
|
|
12
|
-
import {wordTable} from './_tables.mjs';
|
|
13
|
-
|
|
14
|
-
const SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
15
|
-
|
|
16
|
-
/**
|
|
17
|
-
* The most pixels a picture may have: about a 6K screen. A bigger header is
|
|
18
|
-
* refused before any memory is taken for it.
|
|
19
|
-
*/
|
|
20
|
-
export const MAX_PIXELS = 40_000_000;
|
|
21
|
-
|
|
22
|
-
/** A 16-bit picture takes twice the memory per pixel, so half as many. */
|
|
23
|
-
export const MAX_PIXELS_16 = 20_000_000;
|
|
24
|
-
|
|
25
|
-
/** The most pixels the main colors read one by one; past it they stride. */
|
|
26
|
-
const SAMPLE_BUDGET = 4_000_000;
|
|
27
|
-
|
|
28
|
-
/** The bit depths each PNG color type allows. */
|
|
29
|
-
const DEPTHS = wordTable(
|
|
30
|
-
/** @type {Record<number, number[]>} */ ({
|
|
31
|
-
0: [1, 2, 4, 8, 16],
|
|
32
|
-
2: [8, 16],
|
|
33
|
-
3: [1, 2, 4, 8],
|
|
34
|
-
4: [8, 16],
|
|
35
|
-
6: [8, 16],
|
|
36
|
-
}),
|
|
37
|
-
);
|
|
38
|
-
const CHANNELS = wordTable(
|
|
39
|
-
/** @type {Record<number, number>} */ ({
|
|
40
|
-
0: 1,
|
|
41
|
-
2: 3,
|
|
42
|
-
3: 1,
|
|
43
|
-
4: 2,
|
|
44
|
-
6: 4,
|
|
45
|
-
}),
|
|
46
|
-
);
|
|
47
|
-
|
|
48
|
-
/** @typedef {{width: number, height: number, rgba: Uint8Array}} Picture */
|
|
49
|
-
|
|
50
|
-
/**
|
|
51
|
-
* Decode a PNG file's bytes. Interlaced pictures are refused, and so is
|
|
52
|
-
* any file whose header, chunks or pixel data do not match the PNG format.
|
|
53
|
-
* @param {Buffer} bytes
|
|
54
|
-
* @returns {Picture}
|
|
55
|
-
*/
|
|
56
|
-
export function decodePng(bytes) {
|
|
57
|
-
if (bytes.length < 8 || !bytes.subarray(0, 8).equals(SIGNATURE))
|
|
58
|
-
throw new Error('not a PNG file');
|
|
59
|
-
let pos = 8;
|
|
60
|
-
let width = 0;
|
|
61
|
-
let height = 0;
|
|
62
|
-
let depth = 0;
|
|
63
|
-
let colorType = 0;
|
|
64
|
-
let interlace = 0;
|
|
65
|
-
let header = false;
|
|
66
|
-
/** @type {Buffer | null} */
|
|
67
|
-
let palette = null;
|
|
68
|
-
/** @type {Buffer | null} */
|
|
69
|
-
let alpha = null;
|
|
70
|
-
/** @type {Buffer[]} */
|
|
71
|
-
const data = [];
|
|
72
|
-
while (pos + 8 <= bytes.length) {
|
|
73
|
-
const length = bytes.readUInt32BE(pos);
|
|
74
|
-
const type = bytes.toString('latin1', pos + 4, pos + 8);
|
|
75
|
-
if (pos + 12 + length > bytes.length)
|
|
76
|
-
throw new Error(`the ${type} chunk is cut off`);
|
|
77
|
-
const body = bytes.subarray(pos + 8, pos + 8 + length);
|
|
78
|
-
pos += 12 + length;
|
|
79
|
-
if (type === 'IHDR') {
|
|
80
|
-
if (length !== 13) throw new Error('the PNG header is damaged');
|
|
81
|
-
width = body.readUInt32BE(0);
|
|
82
|
-
height = body.readUInt32BE(4);
|
|
83
|
-
depth = body[8];
|
|
84
|
-
colorType = body[9];
|
|
85
|
-
interlace = body[12];
|
|
86
|
-
header = true;
|
|
87
|
-
} else if (type === 'PLTE') palette = body;
|
|
88
|
-
else if (type === 'tRNS') alpha = body;
|
|
89
|
-
else if (type === 'IDAT') data.push(body);
|
|
90
|
-
else if (type === 'IEND') break;
|
|
91
|
-
}
|
|
92
|
-
if (!header) throw new Error('the PNG has no header');
|
|
93
|
-
if (!width || !height)
|
|
94
|
-
throw new Error(
|
|
95
|
-
`the picture is ${width}x${height} pixels, so it has no colors`,
|
|
96
|
-
);
|
|
97
|
-
if (interlace) throw new Error('interlaced PNG files are not supported');
|
|
98
|
-
if (!DEPTHS[colorType]?.includes(depth))
|
|
99
|
-
throw new Error(
|
|
100
|
-
`color type ${colorType} at bit depth ${depth} is not a valid PNG format`,
|
|
101
|
-
);
|
|
102
|
-
const cap = depth === 16 ? MAX_PIXELS_16 : MAX_PIXELS;
|
|
103
|
-
if (width * height > cap)
|
|
104
|
-
throw new Error(
|
|
105
|
-
`the picture is ${width}x${height}${depth === 16 ? ' at 16 bits per channel' : ''}, more than ${cap / 1e6} million pixels; crop or scale it down${depth === 16 ? ', or save it at 8 bits' : ''}`,
|
|
106
|
-
);
|
|
107
|
-
if (colorType === 3 && !palette)
|
|
108
|
-
throw new Error('the PNG uses a palette but has none');
|
|
109
|
-
if (!data.length) throw new Error('the PNG has no pixel data');
|
|
110
|
-
const channels = CHANNELS[colorType];
|
|
111
|
-
const bitsPerPixel = channels * depth;
|
|
112
|
-
const stride = Math.ceil((width * bitsPerPixel) / 8);
|
|
113
|
-
const expected = (stride + 1) * height;
|
|
114
|
-
/** @type {Buffer} */
|
|
115
|
-
let raw;
|
|
116
|
-
try {
|
|
117
|
-
raw = zlib.inflateSync(Buffer.concat(data), {maxOutputLength: expected});
|
|
118
|
-
} catch (e) {
|
|
119
|
-
// Node reports output past the limit as a RangeError.
|
|
120
|
-
throw new Error(
|
|
121
|
-
e instanceof RangeError
|
|
122
|
-
? 'the pixel data is longer than the header says'
|
|
123
|
-
: 'the pixel data is damaged',
|
|
124
|
-
{cause: e},
|
|
125
|
-
);
|
|
126
|
-
}
|
|
127
|
-
if (raw.length < expected)
|
|
128
|
-
throw new Error(
|
|
129
|
-
`the pixel data is cut off: ${raw.length} of ${expected} bytes`,
|
|
130
|
-
);
|
|
131
|
-
const bpp = Math.max(1, bitsPerPixel >> 3);
|
|
132
|
-
const rows = Buffer.alloc(stride * height);
|
|
133
|
-
for (let y = 0; y < height; y++) {
|
|
134
|
-
const filter = raw[y * (stride + 1)];
|
|
135
|
-
if (filter > 4)
|
|
136
|
-
throw new Error(`row ${y} has an unknown filter type ${filter}`);
|
|
137
|
-
const line = raw.subarray(y * (stride + 1) + 1, (y + 1) * (stride + 1));
|
|
138
|
-
const out = rows.subarray(y * stride, (y + 1) * stride);
|
|
139
|
-
const prev = y > 0 ? rows.subarray((y - 1) * stride, y * stride) : null;
|
|
140
|
-
for (let x = 0; x < stride; x++) {
|
|
141
|
-
const a = x >= bpp ? out[x - bpp] : 0;
|
|
142
|
-
const b = prev ? prev[x] : 0;
|
|
143
|
-
const c = prev && x >= bpp ? prev[x - bpp] : 0;
|
|
144
|
-
let v = line[x];
|
|
145
|
-
if (filter === 1) v += a;
|
|
146
|
-
else if (filter === 2) v += b;
|
|
147
|
-
else if (filter === 3) v += (a + b) >> 1;
|
|
148
|
-
else if (filter === 4) {
|
|
149
|
-
const p = a + b - c;
|
|
150
|
-
const pa = Math.abs(p - a);
|
|
151
|
-
const pb = Math.abs(p - b);
|
|
152
|
-
const pc = Math.abs(p - c);
|
|
153
|
-
v += pa <= pb && pa <= pc ? a : pb <= pc ? b : c;
|
|
154
|
-
}
|
|
155
|
-
out[x] = v & 255;
|
|
156
|
-
}
|
|
157
|
-
}
|
|
158
|
-
const rgba = new Uint8Array(width * height * 4);
|
|
159
|
-
const max = (1 << depth) - 1;
|
|
160
|
-
/** @param {number} y @param {number} i sample index in the row */
|
|
161
|
-
const sample = (y, i) => {
|
|
162
|
-
const row = y * stride;
|
|
163
|
-
if (depth === 8) return rows[row + i];
|
|
164
|
-
if (depth === 16) return rows[row + i * 2];
|
|
165
|
-
const bit = i * depth;
|
|
166
|
-
const byte = rows[row + (bit >> 3)];
|
|
167
|
-
const v = (byte >> (8 - depth - (bit & 7))) & max;
|
|
168
|
-
return colorType === 3 ? v : Math.round((v * 255) / max);
|
|
169
|
-
};
|
|
170
|
-
const entries = palette ? Math.floor(palette.length / 3) : 0;
|
|
171
|
-
/** A sample as stored, before scaling to 0-255. @param {number} y @param {number} i */
|
|
172
|
-
const raw16 = (y, i) => {
|
|
173
|
-
const row = y * stride;
|
|
174
|
-
if (depth === 16) return (rows[row + i * 2] << 8) | rows[row + i * 2 + 1];
|
|
175
|
-
if (depth === 8) return rows[row + i];
|
|
176
|
-
const bit = i * depth;
|
|
177
|
-
return (rows[row + (bit >> 3)] >> (8 - depth - (bit & 7))) & max;
|
|
178
|
-
};
|
|
179
|
-
// tRNS for gray and RGB pictures names one color that is fully
|
|
180
|
-
// see-through.
|
|
181
|
-
const key =
|
|
182
|
-
alpha && colorType === 0 && alpha.length >= 2
|
|
183
|
-
? [alpha.readUInt16BE(0)]
|
|
184
|
-
: alpha && colorType === 2 && alpha.length >= 6
|
|
185
|
-
? [alpha.readUInt16BE(0), alpha.readUInt16BE(2), alpha.readUInt16BE(4)]
|
|
186
|
-
: null;
|
|
187
|
-
for (let y = 0; y < height; y++) {
|
|
188
|
-
for (let x = 0; x < width; x++) {
|
|
189
|
-
const o = (y * width + x) * 4;
|
|
190
|
-
const s = x * channels;
|
|
191
|
-
if (colorType === 3) {
|
|
192
|
-
const idx = sample(y, s);
|
|
193
|
-
if (idx >= entries)
|
|
194
|
-
throw new Error(`a pixel names palette entry ${idx} of ${entries}`);
|
|
195
|
-
const pal = /** @type {Buffer} */ (palette);
|
|
196
|
-
rgba[o] = pal[idx * 3];
|
|
197
|
-
rgba[o + 1] = pal[idx * 3 + 1];
|
|
198
|
-
rgba[o + 2] = pal[idx * 3 + 2];
|
|
199
|
-
rgba[o + 3] = alpha && idx < alpha.length ? alpha[idx] : 255;
|
|
200
|
-
} else if (colorType === 0 || colorType === 4) {
|
|
201
|
-
const g = sample(y, s);
|
|
202
|
-
rgba[o] = rgba[o + 1] = rgba[o + 2] = g;
|
|
203
|
-
rgba[o + 3] =
|
|
204
|
-
colorType === 4
|
|
205
|
-
? sample(y, s + 1)
|
|
206
|
-
: key && raw16(y, s) === key[0]
|
|
207
|
-
? 0
|
|
208
|
-
: 255;
|
|
209
|
-
} else {
|
|
210
|
-
rgba[o] = sample(y, s);
|
|
211
|
-
rgba[o + 1] = sample(y, s + 1);
|
|
212
|
-
rgba[o + 2] = sample(y, s + 2);
|
|
213
|
-
rgba[o + 3] =
|
|
214
|
-
colorType === 6
|
|
215
|
-
? sample(y, s + 3)
|
|
216
|
-
: key && key.every((k, n) => raw16(y, s + n) === k)
|
|
217
|
-
? 0
|
|
218
|
-
: 255;
|
|
219
|
-
}
|
|
220
|
-
}
|
|
221
|
-
}
|
|
222
|
-
return {width, height, rgba};
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
/** @param {number} v */
|
|
226
|
-
const hx = v => Math.round(v).toString(16).padStart(2, '0');
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* The exact color at each point, with its alpha (0 is fully see-through,
|
|
230
|
-
* 1 fully opaque). A point outside the picture has no color.
|
|
231
|
-
* @param {Picture} pic
|
|
232
|
-
* @param {Array<{x: number, y: number}>} points
|
|
233
|
-
* @returns {Array<{at: string, color: string | null, alpha: number | null}>}
|
|
234
|
-
*/
|
|
235
|
-
export function colorsAt(pic, points) {
|
|
236
|
-
return points.map(({x, y}) => {
|
|
237
|
-
if (x < 0 || y < 0 || x >= pic.width || y >= pic.height)
|
|
238
|
-
return {at: `${x},${y}`, color: null, alpha: null};
|
|
239
|
-
const i = (y * pic.width + x) * 4;
|
|
240
|
-
return {
|
|
241
|
-
at: `${x},${y}`,
|
|
242
|
-
color: `#${hx(pic.rgba[i])}${hx(pic.rgba[i + 1])}${hx(pic.rgba[i + 2])}`,
|
|
243
|
-
alpha: Number((pic.rgba[i + 3] / 255).toFixed(2)),
|
|
244
|
-
};
|
|
245
|
-
});
|
|
246
|
-
}
|
|
247
|
-
|
|
248
|
-
/**
|
|
249
|
-
* The main colors of a picture: every pixel (on a grid past 4 million)
|
|
250
|
-
* bucketed to 5 bits per channel, biggest share first, each with where it sits. A fully
|
|
251
|
-
* see-through pixel shows no color, so it is skipped; a partly see-through
|
|
252
|
-
* one counts as it looks over white.
|
|
253
|
-
* @param {Picture} pic
|
|
254
|
-
* @param {number} count
|
|
255
|
-
* @returns {Array<{color: string, share: number, where: string}>}
|
|
256
|
-
*/
|
|
257
|
-
export function mainColors(pic, count) {
|
|
258
|
-
/** @type {Map<number, {n: number, r: number, g: number, b: number, x: number, y: number}>} */
|
|
259
|
-
const buckets = new Map();
|
|
260
|
-
// Every pixel up to a budget, so a 1-pixel line is never skipped; a
|
|
261
|
-
// bigger picture is read on an even grid.
|
|
262
|
-
const step = Math.max(
|
|
263
|
-
1,
|
|
264
|
-
Math.ceil(Math.sqrt((pic.width * pic.height) / SAMPLE_BUDGET)),
|
|
265
|
-
);
|
|
266
|
-
for (let y = 0; y < pic.height; y += step) {
|
|
267
|
-
for (let x = 0; x < pic.width; x += step) {
|
|
268
|
-
const i = (y * pic.width + x) * 4;
|
|
269
|
-
const a = pic.rgba[i + 3] / 255;
|
|
270
|
-
if (a === 0) continue;
|
|
271
|
-
const over = (/** @type {number} */ c) =>
|
|
272
|
-
Math.round(c * a + 255 * (1 - a));
|
|
273
|
-
const r = over(pic.rgba[i]);
|
|
274
|
-
const g = over(pic.rgba[i + 1]);
|
|
275
|
-
const b = over(pic.rgba[i + 2]);
|
|
276
|
-
const k = ((r >> 3) << 10) | ((g >> 3) << 5) | (b >> 3);
|
|
277
|
-
let e = buckets.get(k);
|
|
278
|
-
if (!e) {
|
|
279
|
-
e = {n: 0, r: 0, g: 0, b: 0, x: 0, y: 0};
|
|
280
|
-
buckets.set(k, e);
|
|
281
|
-
}
|
|
282
|
-
e.n++;
|
|
283
|
-
e.r += r;
|
|
284
|
-
e.g += g;
|
|
285
|
-
e.b += b;
|
|
286
|
-
e.x += x;
|
|
287
|
-
e.y += y;
|
|
288
|
-
}
|
|
289
|
-
}
|
|
290
|
-
const total = [...buckets.values()].reduce((a, e) => a + e.n, 0) || 1;
|
|
291
|
-
return [...buckets.values()]
|
|
292
|
-
.sort((a, b) => b.n - a.n)
|
|
293
|
-
.slice(0, count)
|
|
294
|
-
.map(e => {
|
|
295
|
-
const cx = e.x / e.n / pic.width;
|
|
296
|
-
const cy = e.y / e.n / pic.height;
|
|
297
|
-
const where = `${cy < 0.15 ? 'top' : cy > 0.8 ? 'bottom' : 'middle'}${cx < 0.25 ? '-left' : cx > 0.75 ? '-right' : ''}`;
|
|
298
|
-
return {
|
|
299
|
-
color: `#${hx(e.r / e.n)}${hx(e.g / e.n)}${hx(e.b / e.n)}`,
|
|
300
|
-
share: Number(((100 * e.n) / total).toFixed(1)),
|
|
301
|
-
where,
|
|
302
|
-
};
|
|
303
|
-
});
|
|
304
|
-
}
|
|
305
|
-
|
|
306
|
-
/** The keyword of the text chunk the theme editor stamps its pictures with. */
|
|
307
|
-
export const EDITOR_STAMP = 'astryx-theme-editor';
|
|
308
|
-
|
|
309
|
-
const PNG_SIGNATURE = Buffer.from([137, 80, 78, 71, 13, 10, 26, 10]);
|
|
310
|
-
|
|
311
|
-
/**
|
|
312
|
-
* The `tEXt` chunks of a PNG, keyword to text; empty for a file that is not
|
|
313
|
-
* a PNG or cannot be read whole.
|
|
314
|
-
* @param {Buffer} bytes
|
|
315
|
-
* @returns {Record<string, string>}
|
|
316
|
-
*/
|
|
317
|
-
export function pngTexts(bytes) {
|
|
318
|
-
/** @type {Record<string, string>} */
|
|
319
|
-
const out = {};
|
|
320
|
-
if (bytes.length < 8 || !bytes.subarray(0, 8).equals(PNG_SIGNATURE))
|
|
321
|
-
return out;
|
|
322
|
-
for (let at = 8; at + 8 <= bytes.length;) {
|
|
323
|
-
const len = bytes.readUInt32BE(at);
|
|
324
|
-
const type = bytes.toString('latin1', at + 4, at + 8);
|
|
325
|
-
const end = at + 12 + len;
|
|
326
|
-
if (end > bytes.length) break;
|
|
327
|
-
if (type === 'tEXt') {
|
|
328
|
-
const data = bytes.subarray(at + 8, at + 8 + len);
|
|
329
|
-
const nul = data.indexOf(0);
|
|
330
|
-
if (nul > 0)
|
|
331
|
-
out[data.toString('latin1', 0, nul)] = data.toString('latin1', nul + 1);
|
|
332
|
-
}
|
|
333
|
-
if (type === 'IEND') break;
|
|
334
|
-
at = end;
|
|
335
|
-
}
|
|
336
|
-
return out;
|
|
337
|
-
}
|
|
338
|
-
|
|
339
|
-
/**
|
|
340
|
-
* A PNG with a `tEXt` chunk added right after its header (any chunk with the
|
|
341
|
-
* same keyword replaced), or the bytes as they were when they are not a PNG.
|
|
342
|
-
* @param {Buffer} bytes
|
|
343
|
-
* @param {string} keyword
|
|
344
|
-
* @param {string} text
|
|
345
|
-
* @returns {Buffer}
|
|
346
|
-
*/
|
|
347
|
-
export function withPngText(bytes, keyword, text) {
|
|
348
|
-
if (bytes.length < 33 || !bytes.subarray(0, 8).equals(PNG_SIGNATURE))
|
|
349
|
-
return bytes;
|
|
350
|
-
/** @type {Buffer[]} */
|
|
351
|
-
const chunks = [];
|
|
352
|
-
for (let at = 8; at + 8 <= bytes.length;) {
|
|
353
|
-
const len = bytes.readUInt32BE(at);
|
|
354
|
-
const type = bytes.toString('latin1', at + 4, at + 8);
|
|
355
|
-
const end = at + 12 + len;
|
|
356
|
-
if (end > bytes.length) return bytes;
|
|
357
|
-
const data = bytes.subarray(at + 8, at + 8 + len);
|
|
358
|
-
const same =
|
|
359
|
-
type === 'tEXt' &&
|
|
360
|
-
data.indexOf(0) > 0 &&
|
|
361
|
-
data.toString('latin1', 0, data.indexOf(0)) === keyword;
|
|
362
|
-
if (!same) chunks.push(bytes.subarray(at, end));
|
|
363
|
-
if (type === 'IHDR') {
|
|
364
|
-
const body = Buffer.concat([
|
|
365
|
-
Buffer.from('tEXt', 'latin1'),
|
|
366
|
-
Buffer.from(keyword, 'latin1'),
|
|
367
|
-
Buffer.from([0]),
|
|
368
|
-
Buffer.from(text, 'latin1'),
|
|
369
|
-
]);
|
|
370
|
-
const head = Buffer.alloc(4);
|
|
371
|
-
head.writeUInt32BE(body.length - 4);
|
|
372
|
-
const crc = Buffer.alloc(4);
|
|
373
|
-
crc.writeUInt32BE(zlib.crc32(body) >>> 0);
|
|
374
|
-
chunks.push(Buffer.concat([head, body, crc]));
|
|
375
|
-
}
|
|
376
|
-
if (type === 'IEND') break;
|
|
377
|
-
at = end;
|
|
378
|
-
}
|
|
379
|
-
return Buffer.concat([PNG_SIGNATURE, ...chunks]);
|
|
380
|
-
}
|
|
381
|
-
|
|
382
|
-
/**
|
|
383
|
-
* The bytes of a PNG holding a picture's RGBA pixels, 8 bits a channel, each
|
|
384
|
-
* row unfiltered.
|
|
385
|
-
* @param {Picture} pic
|
|
386
|
-
* @returns {Buffer}
|
|
387
|
-
*/
|
|
388
|
-
export function encodePng({width, height, rgba}) {
|
|
389
|
-
/** @param {string} type @param {Buffer} data */
|
|
390
|
-
const chunk = (type, data) => {
|
|
391
|
-
const body = Buffer.concat([Buffer.from(type, 'latin1'), data]);
|
|
392
|
-
const head = Buffer.alloc(4);
|
|
393
|
-
head.writeUInt32BE(data.length);
|
|
394
|
-
const crc = Buffer.alloc(4);
|
|
395
|
-
crc.writeUInt32BE(zlib.crc32(body) >>> 0);
|
|
396
|
-
return Buffer.concat([head, body, crc]);
|
|
397
|
-
};
|
|
398
|
-
const ihdr = Buffer.alloc(13);
|
|
399
|
-
ihdr.writeUInt32BE(width, 0);
|
|
400
|
-
ihdr.writeUInt32BE(height, 4);
|
|
401
|
-
ihdr[8] = 8;
|
|
402
|
-
ihdr[9] = 6;
|
|
403
|
-
const stride = width * 4;
|
|
404
|
-
const rows = Buffer.alloc((stride + 1) * height);
|
|
405
|
-
for (let y = 0; y < height; y++)
|
|
406
|
-
rows.set(rgba.subarray(y * stride, (y + 1) * stride), y * (stride + 1) + 1);
|
|
407
|
-
return Buffer.concat([
|
|
408
|
-
PNG_SIGNATURE,
|
|
409
|
-
chunk('IHDR', ihdr),
|
|
410
|
-
chunk('IDAT', zlib.deflateSync(rows)),
|
|
411
|
-
chunk('IEND', Buffer.alloc(0)),
|
|
412
|
-
]);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
/**
|
|
416
|
-
* A tall picture cut into pictures, in order, each cut only at the top of a
|
|
417
|
-
* row of parts: the end of a run of at least `gap` rows of one color (the
|
|
418
|
-
* page between two rows of cells; a cell's dashed edge breaks every run
|
|
419
|
-
* inside it). A piece ends at the lowest row top within `most` pixels; a
|
|
420
|
-
* row taller than that is a piece of its own, never cut through. A last
|
|
421
|
-
* piece shorter than `least` joins the one before it. A picture no taller
|
|
422
|
-
* than `most` is itself alone.
|
|
423
|
-
* @param {Picture} pic
|
|
424
|
-
* @param {number} most
|
|
425
|
-
* @param {{gap?: number, least?: number}} [options]
|
|
426
|
-
* @returns {Picture[]}
|
|
427
|
-
*/
|
|
428
|
-
export function cutTall(pic, most, {gap = 10, least = 300} = {}) {
|
|
429
|
-
const {width, height, rgba} = pic;
|
|
430
|
-
if (height <= most) return [pic];
|
|
431
|
-
const stride = width * 4;
|
|
432
|
-
/** @param {number} y */
|
|
433
|
-
const plain = y => {
|
|
434
|
-
const row = new Uint32Array(
|
|
435
|
-
rgba.buffer,
|
|
436
|
-
rgba.byteOffset + y * stride,
|
|
437
|
-
width,
|
|
438
|
-
);
|
|
439
|
-
const first = row[0];
|
|
440
|
-
for (let x = 1; x < width; x++) if (row[x] !== first) return false;
|
|
441
|
-
return true;
|
|
442
|
-
};
|
|
443
|
-
// Where a row of parts starts: the end of each run of `gap` plain rows.
|
|
444
|
-
/** @type {number[]} */
|
|
445
|
-
const rowTops = [];
|
|
446
|
-
for (let y = 0, run = 0; y < height; y++) {
|
|
447
|
-
if (plain(y)) run++;
|
|
448
|
-
else {
|
|
449
|
-
if (run >= gap && y - run > 0) rowTops.push(y);
|
|
450
|
-
run = 0;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
/** Where each piece ends. @type {number[]} */
|
|
454
|
-
const ends = [];
|
|
455
|
-
for (let top = 0; top < height;) {
|
|
456
|
-
const limit = top + most;
|
|
457
|
-
let end = height;
|
|
458
|
-
if (limit < height) {
|
|
459
|
-
// The lowest row top the piece reaches; a row taller than `most`
|
|
460
|
-
// is a piece of its own, never cut.
|
|
461
|
-
const within = rowTops.filter(y => y > top && y <= limit);
|
|
462
|
-
end = within.length
|
|
463
|
-
? within[within.length - 1]
|
|
464
|
-
: (rowTops.find(y => y > limit) ?? height);
|
|
465
|
-
}
|
|
466
|
-
ends.push(end);
|
|
467
|
-
top = end;
|
|
468
|
-
}
|
|
469
|
-
if (ends.length > 1 && height - ends[ends.length - 2] < least)
|
|
470
|
-
ends.splice(ends.length - 2, 1);
|
|
471
|
-
return ends.map((end, i) => {
|
|
472
|
-
const top = i ? ends[i - 1] : 0;
|
|
473
|
-
return {
|
|
474
|
-
width,
|
|
475
|
-
height: end - top,
|
|
476
|
-
rgba: rgba.slice(top * stride, end * stride),
|
|
477
|
-
};
|
|
478
|
-
});
|
|
479
|
-
}
|