@kurot/atlas 0.0.0-stage → 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +21 -0
- package/LICENSE +21 -0
- package/README.md +151 -2
- package/dist/AtlasError.d.ts +9 -0
- package/dist/AtlasError.d.ts.map +1 -0
- package/dist/AtlasError.js +12 -0
- package/dist/AtlasError.js.map +1 -0
- package/dist/AtlasTypes.d.ts +76 -0
- package/dist/AtlasTypes.d.ts.map +1 -0
- package/dist/AtlasTypes.js +2 -0
- package/dist/AtlasTypes.js.map +1 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/layout.d.ts +10 -0
- package/dist/layout.d.ts.map +1 -0
- package/dist/layout.js +39 -0
- package/dist/layout.js.map +1 -0
- package/dist/max-rects.d.ts +17 -0
- package/dist/max-rects.d.ts.map +1 -0
- package/dist/max-rects.js +67 -0
- package/dist/max-rects.js.map +1 -0
- package/dist/options.d.ts +5 -0
- package/dist/options.d.ts.map +1 -0
- package/dist/options.js +64 -0
- package/dist/options.js.map +1 -0
- package/dist/pack-atlas.d.ts +7 -0
- package/dist/pack-atlas.d.ts.map +1 -0
- package/dist/pack-atlas.js +16 -0
- package/dist/pack-atlas.js.map +1 -0
- package/dist/png/index.d.ts +26 -0
- package/dist/png/index.d.ts.map +1 -0
- package/dist/png/index.js +89 -0
- package/dist/png/index.js.map +1 -0
- package/dist/render.d.ts +6 -0
- package/dist/render.d.ts.map +1 -0
- package/dist/render.js +40 -0
- package/dist/render.js.map +1 -0
- package/dist/trim.d.ts +12 -0
- package/dist/trim.d.ts.map +1 -0
- package/dist/trim.js +29 -0
- package/dist/trim.js.map +1 -0
- package/docs/ai-context.md +76 -0
- package/docs/cli-integration.md +108 -0
- package/docs/reference-validation.md +96 -0
- package/package.json +40 -5
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
# Changelog
|
|
2
|
+
|
|
3
|
+
## 0.1.0 — 2026-10-07
|
|
4
|
+
|
|
5
|
+
Initial local implementation; not published.
|
|
6
|
+
|
|
7
|
+
- Add independent portable RGBA atlas packing with deterministic nonrotated
|
|
8
|
+
MaxRects placement and single-sheet power-of-two size selection.
|
|
9
|
+
- Add alpha cropping, retained trim margins, straight-alpha composition,
|
|
10
|
+
transparent RGB clearing and edge/corner extrusion.
|
|
11
|
+
- Emit flat Kurot-compatible file/frames JSON with trim offsets and logical size.
|
|
12
|
+
- Add a separate Node PNG entry point using pngjs; bound decoded pixel counts,
|
|
13
|
+
validate PNG input and reject APNG.
|
|
14
|
+
- Add synthetic pixel, packing, deterministic-output and PNG regression tests.
|
|
15
|
+
- Add repeatable read-only local basis/basis_icon validation for frame schemas,
|
|
16
|
+
RGBA pixels, cropping, extrusion, overlap and deterministic output; record
|
|
17
|
+
existing CLI development/release compatibility and future integration proposals.
|
|
18
|
+
|
|
19
|
+
No Core, CLI, Editor, Reskin or game project behavior changes. Filesystem writes,
|
|
20
|
+
skin edits, manifest updates, rotation, deduplication and multipack are outside
|
|
21
|
+
this release.
|
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Kelvin
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,152 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @kurot/atlas
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Version **0.1.0** — initial local implementation, not published.
|
|
4
|
+
|
|
5
|
+
Independent, deterministic sprite-atlas tooling. Packs straight-alpha RGBA pixels
|
|
6
|
+
and outputs the flat `file`/`frames` sheet format consumed by Kurot. No dependency
|
|
7
|
+
on Core, UI, ui-document, ui-runtime or CLI; none of those packages depend on atlas.
|
|
8
|
+
It can be called by Reskin, Editor, a separate atlas tool or build automation.
|
|
9
|
+
|
|
10
|
+
## Entry points
|
|
11
|
+
|
|
12
|
+
- `@kurot/atlas`: pixel-only packing, portable ES2022, no Node or PNG imports.
|
|
13
|
+
- `@kurot/atlas/png`: synchronous Node.js >=20 PNG decoding/encoding through
|
|
14
|
+
[pngjs](https://github.com/pngjs/pngjs), without native binaries.
|
|
15
|
+
|
|
16
|
+
The package performs no filesystem I/O. Callers own reading, source-image storage,
|
|
17
|
+
project-path validation, resource manifests, worker scheduling and atomic saves.
|
|
18
|
+
The PNG functions are synchronous; run larger batches in your tool's worker.
|
|
19
|
+
|
|
20
|
+
## PNG example
|
|
21
|
+
|
|
22
|
+
After installing/linking the package into a tool:
|
|
23
|
+
|
|
24
|
+
```ts
|
|
25
|
+
import { readFile, writeFile } from 'node:fs/promises';
|
|
26
|
+
import { packPNGAtlas } from '@kurot/atlas/png';
|
|
27
|
+
|
|
28
|
+
const result = packPNGAtlas([
|
|
29
|
+
{ name: 'r_btn_bet', png: await readFile('src/basis/r_btn_bet.png') },
|
|
30
|
+
{ name: 'r_prg_thumb', png: await readFile('src/basis/r_prg_thumb.png') },
|
|
31
|
+
], { file: 'r_basis.png' });
|
|
32
|
+
|
|
33
|
+
// Demonstration only: a project editor must stage and commit the pair together.
|
|
34
|
+
await writeFile('export/r_basis.png', result.png);
|
|
35
|
+
await writeFile('export/r_basis.json', JSON.stringify(result.data, undefined, 2) + '\n');
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Frame names are supplied explicitly and remain stable; the library does not
|
|
39
|
+
derive names from filesystem paths. Names start with an ASCII letter or digit,
|
|
40
|
+
followed by letters, digits, underscores, hyphens or dots. Duplicates are errors.
|
|
41
|
+
The `file` option is a PNG basename, resolved relative to the JSON by the consumer.
|
|
42
|
+
|
|
43
|
+
## Pixel example
|
|
44
|
+
|
|
45
|
+
```ts
|
|
46
|
+
import { packAtlas } from '@kurot/atlas';
|
|
47
|
+
|
|
48
|
+
const result = packAtlas([
|
|
49
|
+
{ name: 'red', width: 1, height: 1, pixels: new Uint8Array([255, 0, 0, 255]) },
|
|
50
|
+
]);
|
|
51
|
+
// result.image contains width, height and RGBA pixels; result.data contains the sheet JSON.
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Pixel input is row-major, four bytes per pixel, straight alpha, 8-bit channels.
|
|
55
|
+
Uint8Array and Uint8ClampedArray are accepted. Source arrays and buffers are not
|
|
56
|
+
modified. RGB in fully transparent output pixels is cleared; semitransparent
|
|
57
|
+
colors and alpha are retained without premultiplication or gamma correction.
|
|
58
|
+
|
|
59
|
+
## Packing contract
|
|
60
|
+
|
|
61
|
+
One sheet, no rotation, power-of-two dimensions, not necessarily square.
|
|
62
|
+
MaxRects placement uses a stable area/name sort and three heuristics. Candidate
|
|
63
|
+
sizes are tried by ascending area, then closeness to square, then width.
|
|
64
|
+
This is heuristic packing, not proof of an optimal layout; an overflow means
|
|
65
|
+
these searches did not fit within the requested limits. It does not reproduce
|
|
66
|
+
TexturePacker's exact placement or compressed PNG bytes.
|
|
67
|
+
|
|
68
|
+
| Option | Default | Contract |
|
|
69
|
+
| --- | --- | --- |
|
|
70
|
+
| `file` | `atlas.png` | PNG basename |
|
|
71
|
+
| `maxWidth`, `maxHeight` | 2048 | Integer 1–16384; output POT axes stay within bounds |
|
|
72
|
+
| `trim` | true | Crop transparent borders |
|
|
73
|
+
| `alphaThreshold` | 1 | Content alpha threshold, integer 1–255 |
|
|
74
|
+
| `trimMargin` | 1 | Retained source-pixel margin, clamped to original bounds |
|
|
75
|
+
| `extrude` | 1 | Copies nearest edge pixels, including corners, outside the frame |
|
|
76
|
+
| `shapePadding` | 0 | Empty right/bottom gap after each extruded rectangle |
|
|
77
|
+
| `borderPadding` | 0 | Empty outer sheet margin |
|
|
78
|
+
| `maxPixels` | 16777216 | Limits total decoded source area and output area |
|
|
79
|
+
|
|
80
|
+
Margin, extrusion and padding are integers 0–1024. maxPixels is an integer
|
|
81
|
+
1–67108864. Batches contain 1–4096 sources. PNG input has a 32 MiB per-file limit;
|
|
82
|
+
dimensions are checked before decoding a batch. Static PNG input is normalized to
|
|
83
|
+
RGBA8; APNG is rejected. The input buffers, decoder intermediates and output may
|
|
84
|
+
coexist, so maxPixels is not a process-wide memory quota.
|
|
85
|
+
|
|
86
|
+
Trimming tests alpha >= alphaThreshold, retains trimMargin around that bounding
|
|
87
|
+
box, then stores source offsets and original dimensions. Pixels below the
|
|
88
|
+
threshold inside the retained rectangle are preserved. An empty cropped source
|
|
89
|
+
becomes a clear 1×1 frame at source offset 0, retaining original logical dimensions.
|
|
90
|
+
Set trim=false when retaining the original canvas is required.
|
|
91
|
+
|
|
92
|
+
```json
|
|
93
|
+
{
|
|
94
|
+
"file": "r_basis_icon.png",
|
|
95
|
+
"frames": {
|
|
96
|
+
"r_icon_bars": {
|
|
97
|
+
"x": 1, "y": 1, "w": 56, "h": 43,
|
|
98
|
+
"offX": 6, "offY": 11, "sourceW": 64, "sourceH": 64
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
```
|
|
103
|
+
|
|
104
|
+
Reported rectangles exclude extrusion and padding. Uncropped frames omit offset
|
|
105
|
+
and original-size fields. Sheet metadata does not include TexturePacker ownership,
|
|
106
|
+
smartupdate, pivots, rotation or multipack links.
|
|
107
|
+
|
|
108
|
+
## Errors and integration boundary
|
|
109
|
+
|
|
110
|
+
`AtlasError.code` is `invalid-input`, `invalid-options`, `invalid-png` or
|
|
111
|
+
`atlas-overflow`. Decode failures retain their cause. Overflow never produces a
|
|
112
|
+
partial result or automatically enables rotation/multipack.
|
|
113
|
+
|
|
114
|
+
Resource keys and logical sprite sizes, rather than packed coordinates, connect
|
|
115
|
+
the output to skins. Keep source files as project-owned authoring assets. Preserve
|
|
116
|
+
manifest object-form `subkeys`, groups and resource-default nine-slice metadata
|
|
117
|
+
when replacing a sheet; validate grids against original dimensions. The library
|
|
118
|
+
does not edit these records, KUI XML, style.json or game code. A caller must commit
|
|
119
|
+
PNG/JSON and any changed resource metadata consistently and refresh its caches.
|
|
120
|
+
|
|
121
|
+
Not included: source-folder scanning, `.tps` parsing, GUI, filesystem transactions,
|
|
122
|
+
sprite deduplication, multiple pages, rotation, JPEG/WebP output, polygon meshes,
|
|
123
|
+
automatic scaling or CLI integration.
|
|
124
|
+
|
|
125
|
+
## Development and validation
|
|
126
|
+
|
|
127
|
+
```sh
|
|
128
|
+
pnpm --dir packages/atlas install
|
|
129
|
+
pnpm --dir packages/atlas build
|
|
130
|
+
pnpm --dir packages/atlas test
|
|
131
|
+
```
|
|
132
|
+
|
|
133
|
+
Unit tests use synthetic images and do not require the local game checkout.
|
|
134
|
+
Initial local validation against the updated KUI sample on 2026-10-07 packed
|
|
135
|
+
all 29 basis and 32 basis_icon sources to 1024×1024 and 256×512 respectively.
|
|
136
|
+
All 61 frame field shapes and crop geometries match the existing export. Retained
|
|
137
|
+
RGBA pixels and edge/corner extrusion match the sources, and reversed source
|
|
138
|
+
order produces identical JSON and PNG bytes. CLI 3.3.0 development/release builds
|
|
139
|
+
accept the new resources without authored skin, manifest or stylesheet changes.
|
|
140
|
+
That validation does not promise identical layout to TexturePacker or replace
|
|
141
|
+
future Editor/Reskin integration and rendered-game acceptance.
|
|
142
|
+
|
|
143
|
+
To repeat local reference verification, provide the sample project root and a new
|
|
144
|
+
output directory outside that project:
|
|
145
|
+
|
|
146
|
+
```sh
|
|
147
|
+
pnpm --dir packages/atlas verify:reference /path/to/kurot-project-kui /tmp/atlas-new
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
See [reference results and checks](docs/reference-validation.md),
|
|
151
|
+
[CLI integration and optimization proposals](docs/cli-integration.md), and
|
|
152
|
+
[architecture and API context](docs/ai-context.md).
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type AtlasErrorCode = 'invalid-input' | 'invalid-options' | 'atlas-overflow' | 'invalid-png';
|
|
2
|
+
/**
|
|
3
|
+
* Stable failure category for tooling; no input files or output files are changed.
|
|
4
|
+
*/
|
|
5
|
+
export declare class AtlasError extends Error {
|
|
6
|
+
readonly code: AtlasErrorCode;
|
|
7
|
+
constructor(code: AtlasErrorCode, message: string, options?: ErrorOptions);
|
|
8
|
+
}
|
|
9
|
+
//# sourceMappingURL=AtlasError.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AtlasError.d.ts","sourceRoot":"","sources":["../src/AtlasError.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,cAAc,GAAG,eAAe,GAAG,iBAAiB,GAAG,gBAAgB,GAAG,aAAa,CAAC;AAEpG;;GAEG;AACH,qBAAa,UAAW,SAAQ,KAAK;IACpC,SAAgB,IAAI,EAAE,cAAc,CAAC;gBAElB,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,MAAM,EAAE,OAAO,CAAC,EAAE,YAAY;CAKhF"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Stable failure category for tooling; no input files or output files are changed.
|
|
3
|
+
*/
|
|
4
|
+
export class AtlasError extends Error {
|
|
5
|
+
code;
|
|
6
|
+
constructor(code, message, options) {
|
|
7
|
+
super(message, options);
|
|
8
|
+
this.name = 'AtlasError';
|
|
9
|
+
this.code = code;
|
|
10
|
+
}
|
|
11
|
+
}
|
|
12
|
+
//# sourceMappingURL=AtlasError.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AtlasError.js","sourceRoot":"","sources":["../src/AtlasError.ts"],"names":[],"mappings":"AAEA;;GAEG;AACH,MAAM,OAAO,UAAW,SAAQ,KAAK;IACpB,IAAI,CAAiB;IAErC,YAAmB,IAAoB,EAAE,OAAe,EAAE,OAAsB;QAC/E,KAAK,CAAC,OAAO,EAAE,OAAO,CAAC,CAAC;QACxB,IAAI,CAAC,IAAI,GAAG,YAAY,CAAC;QACzB,IAAI,CAAC,IAAI,GAAG,IAAI,CAAC;IAClB,CAAC;CACD"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Straight-alpha, 8-bit RGBA pixels in row-major order. Input buffers are never mutated.
|
|
3
|
+
*/
|
|
4
|
+
export interface AtlasImage {
|
|
5
|
+
readonly width: number;
|
|
6
|
+
readonly height: number;
|
|
7
|
+
readonly pixels: Uint8Array | Uint8ClampedArray;
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* Name is the stable resource key; callers remove extensions or assign names explicitly.
|
|
11
|
+
*/
|
|
12
|
+
export interface AtlasSource extends AtlasImage {
|
|
13
|
+
readonly name: string;
|
|
14
|
+
}
|
|
15
|
+
export interface AtlasOptions {
|
|
16
|
+
/**
|
|
17
|
+
* Output PNG basename, default atlas.png. No directories or URLs.
|
|
18
|
+
*/
|
|
19
|
+
readonly file?: string;
|
|
20
|
+
/**
|
|
21
|
+
* Upper dimension bounds, default 2048 each. Output axes are powers of two.
|
|
22
|
+
*/
|
|
23
|
+
readonly maxWidth?: number;
|
|
24
|
+
readonly maxHeight?: number;
|
|
25
|
+
/**
|
|
26
|
+
* Crop transparent borders, default true.
|
|
27
|
+
*/
|
|
28
|
+
readonly trim?: boolean;
|
|
29
|
+
/**
|
|
30
|
+
* Alpha values at or above this byte value define content, default 1, range 1–255.
|
|
31
|
+
*/
|
|
32
|
+
readonly alphaThreshold?: number;
|
|
33
|
+
/**
|
|
34
|
+
* Source pixels retained around the content bounding box, default 1.
|
|
35
|
+
*/
|
|
36
|
+
readonly trimMargin?: number;
|
|
37
|
+
/**
|
|
38
|
+
* Repeated edge pixels outside each reported frame, default 1, including corners.
|
|
39
|
+
*/
|
|
40
|
+
readonly extrude?: number;
|
|
41
|
+
/**
|
|
42
|
+
* Additional empty pixels after the extruded rectangle on its right and bottom, default 0.
|
|
43
|
+
*/
|
|
44
|
+
readonly shapePadding?: number;
|
|
45
|
+
/**
|
|
46
|
+
* Empty margin around the packed content, default 0.
|
|
47
|
+
*/
|
|
48
|
+
readonly borderPadding?: number;
|
|
49
|
+
/**
|
|
50
|
+
* Limit for total source pixels and output pixels, default 16,777,216.
|
|
51
|
+
*/
|
|
52
|
+
readonly maxPixels?: number;
|
|
53
|
+
}
|
|
54
|
+
/**
|
|
55
|
+
* Frame coordinates exclude extrusion. Offsets place cropped pixels in the original canvas.
|
|
56
|
+
* Uncropped frames omit offX/offY/sourceW/sourceH. No rotated frames are emitted.
|
|
57
|
+
*/
|
|
58
|
+
export interface AtlasFrame {
|
|
59
|
+
x: number;
|
|
60
|
+
y: number;
|
|
61
|
+
w: number;
|
|
62
|
+
h: number;
|
|
63
|
+
offX?: number;
|
|
64
|
+
offY?: number;
|
|
65
|
+
sourceW?: number;
|
|
66
|
+
sourceH?: number;
|
|
67
|
+
}
|
|
68
|
+
export interface AtlasData {
|
|
69
|
+
file: string;
|
|
70
|
+
frames: Record<string, AtlasFrame>;
|
|
71
|
+
}
|
|
72
|
+
export interface AtlasResult {
|
|
73
|
+
image: AtlasImage;
|
|
74
|
+
data: AtlasData;
|
|
75
|
+
}
|
|
76
|
+
//# sourceMappingURL=AtlasTypes.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AtlasTypes.d.ts","sourceRoot":"","sources":["../src/AtlasTypes.ts"],"names":[],"mappings":"AAAA;;GAEG;AACH,MAAM,WAAW,UAAU;IAC1B,QAAQ,CAAC,KAAK,EAAE,MAAM,CAAC;IACvB,QAAQ,CAAC,MAAM,EAAE,MAAM,CAAC;IACxB,QAAQ,CAAC,MAAM,EAAE,UAAU,GAAG,iBAAiB,CAAC;CAChD;AAED;;GAEG;AACH,MAAM,WAAW,WAAY,SAAQ,UAAU;IAC9C,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;CACtB;AAED,MAAM,WAAW,YAAY;IAC5B;;OAEG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,MAAM,CAAC;IACvB;;OAEG;IACH,QAAQ,CAAC,QAAQ,CAAC,EAAE,MAAM,CAAC;IAC3B,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;IAC5B;;OAEG;IACH,QAAQ,CAAC,IAAI,CAAC,EAAE,OAAO,CAAC;IACxB;;OAEG;IACH,QAAQ,CAAC,cAAc,CAAC,EAAE,MAAM,CAAC;IACjC;;OAEG;IACH,QAAQ,CAAC,UAAU,CAAC,EAAE,MAAM,CAAC;IAC7B;;OAEG;IACH,QAAQ,CAAC,OAAO,CAAC,EAAE,MAAM,CAAC;IAC1B;;OAEG;IACH,QAAQ,CAAC,YAAY,CAAC,EAAE,MAAM,CAAC;IAC/B;;OAEG;IACH,QAAQ,CAAC,aAAa,CAAC,EAAE,MAAM,CAAC;IAChC;;OAEG;IACH,QAAQ,CAAC,SAAS,CAAC,EAAE,MAAM,CAAC;CAC5B;AAED;;;GAGG;AACH,MAAM,WAAW,UAAU;IAC1B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,IAAI,CAAC,EAAE,MAAM,CAAC;IACd,OAAO,CAAC,EAAE,MAAM,CAAC;IACjB,OAAO,CAAC,EAAE,MAAM,CAAC;CACjB;AAED,MAAM,WAAW,SAAS;IACzB,IAAI,EAAE,MAAM,CAAC;IACb,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CACnC;AAED,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,UAAU,CAAC;IAClB,IAAI,EAAE,SAAS,CAAC;CAChB"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"AtlasTypes.js","sourceRoot":"","sources":["../src/AtlasTypes.ts"],"names":[],"mappings":""}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
export { AtlasError } from './AtlasError.js';
|
|
2
|
+
export type { AtlasErrorCode } from './AtlasError.js';
|
|
3
|
+
export type { AtlasData, AtlasFrame, AtlasImage, AtlasOptions, AtlasResult, AtlasSource } from './AtlasTypes.js';
|
|
4
|
+
export { packAtlas } from './pack-atlas.js';
|
|
5
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAC7C,YAAY,EAAE,cAAc,EAAE,MAAM,iBAAiB,CAAC;AACtD,YAAY,EAAE,SAAS,EAAE,UAAU,EAAE,UAAU,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACjH,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/index.js
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAG7C,OAAO,EAAE,SAAS,EAAE,MAAM,iBAAiB,CAAC"}
|
package/dist/layout.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { Placement } from './max-rects.js';
|
|
2
|
+
import type { PackingOptions } from './options.js';
|
|
3
|
+
import type { PreparedSource } from './trim.js';
|
|
4
|
+
export interface AtlasLayout {
|
|
5
|
+
width: number;
|
|
6
|
+
height: number;
|
|
7
|
+
placements: Placement[];
|
|
8
|
+
}
|
|
9
|
+
export declare function createLayout(sources: readonly PreparedSource[], options: PackingOptions): AtlasLayout;
|
|
10
|
+
//# sourceMappingURL=layout.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"layout.d.ts","sourceRoot":"","sources":["../src/layout.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,EAAoB,SAAS,EAAE,MAAM,gBAAgB,CAAC;AAClE,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,MAAM,WAAW,WAAW;IAC3B,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,UAAU,EAAE,SAAS,EAAE,CAAC;CACxB;AAED,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,EAAE,OAAO,EAAE,cAAc,GAAG,WAAW,CA+BrG"}
|
package/dist/layout.js
ADDED
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
import { AtlasError } from './AtlasError.js';
|
|
2
|
+
import { placeRectangles } from './max-rects.js';
|
|
3
|
+
export function createLayout(sources, options) {
|
|
4
|
+
const items = sources.map(item => ({
|
|
5
|
+
name: item.source.name,
|
|
6
|
+
width: item.width + options.extrude * 2 + options.shapePadding,
|
|
7
|
+
height: item.height + options.extrude * 2 + options.shapePadding,
|
|
8
|
+
})).sort((a, b) => b.width * b.height - a.width * a.height || Math.max(b.width, b.height) - Math.max(a.width, a.height) ||
|
|
9
|
+
(a.name < b.name ? -1 : a.name > b.name ? 1 : 0));
|
|
10
|
+
const margin = options.borderPadding * 2;
|
|
11
|
+
const minWidth = Math.max(...items.map(item => item.width)) + margin;
|
|
12
|
+
const minHeight = Math.max(...items.map(item => item.height)) + margin;
|
|
13
|
+
const area = items.reduce((sum, item) => sum + item.width * item.height, 0);
|
|
14
|
+
const candidates = powersOfTwo(minWidth, options.maxWidth).flatMap(width => powersOfTwo(minHeight, options.maxHeight).map(height => ({ width, height }))).filter(size => (size.width - margin) * (size.height - margin) >= area &&
|
|
15
|
+
size.width * size.height <= options.maxPixels).sort((a, b) => a.width * a.height - b.width * b.height ||
|
|
16
|
+
Math.abs(a.width - a.height) - Math.abs(b.width - b.height) || a.width - b.width);
|
|
17
|
+
const heuristics = ['short-side', 'area', 'long-side'];
|
|
18
|
+
for (const size of candidates) {
|
|
19
|
+
for (const heuristic of heuristics) {
|
|
20
|
+
const placements = placeRectangles(items, size.width - margin, size.height - margin, heuristic);
|
|
21
|
+
if (!placements)
|
|
22
|
+
continue;
|
|
23
|
+
return { ...size, placements: placements.map(rect => ({
|
|
24
|
+
...rect, x: rect.x + options.borderPadding, y: rect.y + options.borderPadding,
|
|
25
|
+
})) };
|
|
26
|
+
}
|
|
27
|
+
}
|
|
28
|
+
throw new AtlasError('atlas-overflow', `Images do not fit a single power-of-two atlas within ${options.maxWidth}×${options.maxHeight} and maxPixels.`);
|
|
29
|
+
}
|
|
30
|
+
function powersOfTwo(min, max) {
|
|
31
|
+
const values = [];
|
|
32
|
+
for (let value = 1; value <= max; value *= 2) {
|
|
33
|
+
if (value >= min) {
|
|
34
|
+
values.push(value);
|
|
35
|
+
}
|
|
36
|
+
}
|
|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{
|
|
2
|
+
x: number;
|
|
3
|
+
y: number;
|
|
4
|
+
width: number;
|
|
5
|
+
height: number;
|
|
6
|
+
}
|
|
7
|
+
export interface PackingItem {
|
|
8
|
+
name: string;
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
}
|
|
12
|
+
export interface Placement extends PackingRect {
|
|
13
|
+
name: string;
|
|
14
|
+
}
|
|
15
|
+
export type PackingHeuristic = 'short-side' | 'area' | 'long-side';
|
|
16
|
+
export declare function placeRectangles(items: readonly PackingItem[], width: number, height: number, heuristic: PackingHeuristic): Placement[] | undefined;
|
|
17
|
+
//# sourceMappingURL=max-rects.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"max-rects.d.ts","sourceRoot":"","sources":["../src/max-rects.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,WAAW;IAC3B,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,WAAW;IAC3B,IAAI,EAAE,MAAM,CAAC;IACb,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CACf;AAED,MAAM,WAAW,SAAU,SAAQ,WAAW;IAC7C,IAAI,EAAE,MAAM,CAAC;CACb;AAED,MAAM,MAAM,gBAAgB,GAAG,YAAY,GAAG,MAAM,GAAG,WAAW,CAAC;AAEnE,wBAAgB,eAAe,CAC9B,KAAK,EAAE,SAAS,WAAW,EAAE,EAAE,KAAK,EAAE,MAAM,EAAE,MAAM,EAAE,MAAM,EAAE,SAAS,EAAE,gBAAgB,GACvF,SAAS,EAAE,GAAG,SAAS,CA4BzB"}
|
|
@@ -0,0 +1,67 @@
|
|
|
1
|
+
export function placeRectangles(items, width, height, heuristic) {
|
|
2
|
+
let free = [{ x: 0, y: 0, width, height }];
|
|
3
|
+
const placements = [];
|
|
4
|
+
for (const item of items) {
|
|
5
|
+
let best;
|
|
6
|
+
let bestPrimary = Infinity;
|
|
7
|
+
let bestSecondary = Infinity;
|
|
8
|
+
for (const rect of free) {
|
|
9
|
+
if (item.width > rect.width || item.height > rect.height)
|
|
10
|
+
continue;
|
|
11
|
+
const short = Math.min(rect.width - item.width, rect.height - item.height);
|
|
12
|
+
const long = Math.max(rect.width - item.width, rect.height - item.height);
|
|
13
|
+
const primary = heuristic === 'area' ? rect.width * rect.height - item.width * item.height
|
|
14
|
+
: heuristic === 'long-side' ? long : short;
|
|
15
|
+
const secondary = heuristic === 'area' ? short : heuristic === 'long-side' ? short : long;
|
|
16
|
+
if (primary < bestPrimary || (primary === bestPrimary && secondary < bestSecondary) ||
|
|
17
|
+
(primary === bestPrimary && secondary === bestSecondary &&
|
|
18
|
+
(!best || rect.y < best.y || (rect.y === best.y && rect.x < best.x)))) {
|
|
19
|
+
best = rect;
|
|
20
|
+
bestPrimary = primary;
|
|
21
|
+
bestSecondary = secondary;
|
|
22
|
+
}
|
|
23
|
+
}
|
|
24
|
+
if (!best)
|
|
25
|
+
return undefined;
|
|
26
|
+
const placed = { name: item.name, x: best.x, y: best.y, width: item.width, height: item.height };
|
|
27
|
+
placements.push(placed);
|
|
28
|
+
free = pruneRectangles(free.flatMap(rect => splitRect(rect, placed)));
|
|
29
|
+
}
|
|
30
|
+
return placements;
|
|
31
|
+
}
|
|
32
|
+
function splitRect(free, used) {
|
|
33
|
+
const right = free.x + free.width;
|
|
34
|
+
const bottom = free.y + free.height;
|
|
35
|
+
const usedRight = used.x + used.width;
|
|
36
|
+
const usedBottom = used.y + used.height;
|
|
37
|
+
if (used.x >= right || usedRight <= free.x || used.y >= bottom || usedBottom <= free.y) {
|
|
38
|
+
return [free];
|
|
39
|
+
}
|
|
40
|
+
const parts = [];
|
|
41
|
+
if (used.x > free.x) {
|
|
42
|
+
parts.push({ x: free.x, y: free.y, width: used.x - free.x, height: free.height });
|
|
43
|
+
}
|
|
44
|
+
if (usedRight < right) {
|
|
45
|
+
parts.push({ x: usedRight, y: free.y, width: right - usedRight, height: free.height });
|
|
46
|
+
}
|
|
47
|
+
if (used.y > free.y) {
|
|
48
|
+
parts.push({ x: free.x, y: free.y, width: free.width, height: used.y - free.y });
|
|
49
|
+
}
|
|
50
|
+
if (usedBottom < bottom) {
|
|
51
|
+
parts.push({ x: free.x, y: usedBottom, width: free.width, height: bottom - usedBottom });
|
|
52
|
+
}
|
|
53
|
+
return parts;
|
|
54
|
+
}
|
|
55
|
+
function pruneRectangles(rects) {
|
|
56
|
+
return rects.filter((rect, index) => !rects.some((other, otherIndex) => {
|
|
57
|
+
if (index === otherIndex)
|
|
58
|
+
return false;
|
|
59
|
+
const contained = rect.x >= other.x && rect.y >= other.y &&
|
|
60
|
+
rect.x + rect.width <= other.x + other.width && rect.y + rect.height <= other.y + other.height;
|
|
61
|
+
if (!contained)
|
|
62
|
+
return false;
|
|
63
|
+
const equal = rect.x === other.x && rect.y === other.y && rect.width === other.width && rect.height === other.height;
|
|
64
|
+
return !equal || otherIndex < index;
|
|
65
|
+
}));
|
|
66
|
+
}
|
|
67
|
+
//# sourceMappingURL=max-rects.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"max-rects.js","sourceRoot":"","sources":["../src/max-rects.ts"],"names":[],"mappings":"AAmBA,MAAM,UAAU,eAAe,CAC9B,KAA6B,EAAE,KAAa,EAAE,MAAc,EAAE,SAA2B;IAEzF,IAAI,IAAI,GAAkB,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,MAAM,EAAE,CAAC,CAAC;IAC1D,MAAM,UAAU,GAAgB,EAAE,CAAC;IACnC,KAAK,MAAM,IAAI,IAAI,KAAK,EAAE,CAAC;QAC1B,IAAI,IAA6B,CAAC;QAClC,IAAI,WAAW,GAAG,QAAQ,CAAC;QAC3B,IAAI,aAAa,GAAG,QAAQ,CAAC;QAC7B,KAAK,MAAM,IAAI,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AAC;QACvC,MAAM,SAAS,GAAG,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,IAAI,KAAK,CAAC,CAAC;YACvD,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,KAAK,IAAI,KAAK,CAAC,CAAC,GAAG,KAAK,CAAC,KAAK,IAAI,IAAI,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,IAAI,KAAK,CAAC,CAAC,GAAG,KAAK,CAAC,MAAM,CAAC;QAChG,IAAI,CAAC,SAAS;YAAE,OAAO,KAAK,CAAC;QAC7B,MAAM,KAAK,GAAG,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,IAAI,IAAI,CAAC,CAAC,KAAK,KAAK,CAAC,CAAC,IAAI,IAAI,CAAC,KAAK,KAAK,KAAK,CAAC,KAAK,IAAI,IAAI,CAAC,MAAM,KAAK,KAAK,CAAC,MAAM,CAAC;QACrH,OAAO,CAAC,KAAK,IAAI,UAAU,GAAG,KAAK,CAAC;IACrC,CAAC,CAAC,CAAC,CAAC;AACL,CAAC"}
|
|
@@ -0,0 +1,5 @@
|
|
|
1
|
+
import type { AtlasOptions, AtlasSource } from './AtlasTypes.js';
|
|
2
|
+
export type PackingOptions = Required<AtlasOptions>;
|
|
3
|
+
export declare function resolveOptions(options: AtlasOptions): PackingOptions;
|
|
4
|
+
export declare function validateSources(sources: readonly AtlasSource[], options: PackingOptions): void;
|
|
5
|
+
//# sourceMappingURL=options.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.d.ts","sourceRoot":"","sources":["../src/options.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAEjE,MAAM,MAAM,cAAc,GAAG,QAAQ,CAAC,YAAY,CAAC,CAAC;AAEpD,wBAAgB,cAAc,CAAC,OAAO,EAAE,YAAY,GAAG,cAAc,CA4BpE;AAED,wBAAgB,eAAe,CAAC,OAAO,EAAE,SAAS,WAAW,EAAE,EAAE,OAAO,EAAE,cAAc,GAAG,IAAI,CA2B9F"}
|
package/dist/options.js
ADDED
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
import { AtlasError } from './AtlasError.js';
|
|
2
|
+
export function resolveOptions(options) {
|
|
3
|
+
const resolved = {
|
|
4
|
+
file: options.file ?? 'atlas.png',
|
|
5
|
+
maxWidth: options.maxWidth ?? 2048,
|
|
6
|
+
maxHeight: options.maxHeight ?? 2048,
|
|
7
|
+
trim: options.trim ?? true,
|
|
8
|
+
alphaThreshold: options.alphaThreshold ?? 1,
|
|
9
|
+
trimMargin: options.trimMargin ?? 1,
|
|
10
|
+
extrude: options.extrude ?? 1,
|
|
11
|
+
shapePadding: options.shapePadding ?? 0,
|
|
12
|
+
borderPadding: options.borderPadding ?? 0,
|
|
13
|
+
maxPixels: options.maxPixels ?? 16_777_216,
|
|
14
|
+
};
|
|
15
|
+
if (typeof resolved.file !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9_.-]*\.png$/.test(resolved.file)) {
|
|
16
|
+
throw new AtlasError('invalid-options', 'file must be a PNG basename without directories.');
|
|
17
|
+
}
|
|
18
|
+
if (typeof resolved.trim !== 'boolean') {
|
|
19
|
+
throw new AtlasError('invalid-options', 'trim must be boolean.');
|
|
20
|
+
}
|
|
21
|
+
for (const key of ['maxWidth', 'maxHeight']) {
|
|
22
|
+
checkInteger(key, resolved[key], 1, 16_384);
|
|
23
|
+
}
|
|
24
|
+
checkInteger('alphaThreshold', resolved.alphaThreshold, 1, 255);
|
|
25
|
+
for (const key of ['trimMargin', 'extrude', 'shapePadding', 'borderPadding']) {
|
|
26
|
+
checkInteger(key, resolved[key], 0, 1024);
|
|
27
|
+
}
|
|
28
|
+
checkInteger('maxPixels', resolved.maxPixels, 1, 67_108_864);
|
|
29
|
+
return resolved;
|
|
30
|
+
}
|
|
31
|
+
export function validateSources(sources, options) {
|
|
32
|
+
if (!Array.isArray(sources) || sources.length === 0 || sources.length > 4096) {
|
|
33
|
+
throw new AtlasError('invalid-input', 'Provide between 1 and 4096 source images.');
|
|
34
|
+
}
|
|
35
|
+
const names = new Set();
|
|
36
|
+
let pixels = 0;
|
|
37
|
+
for (const source of sources) {
|
|
38
|
+
if (typeof source.name !== 'string' || !/^[A-Za-z0-9][A-Za-z0-9_.-]*$/.test(source.name)) {
|
|
39
|
+
throw new AtlasError('invalid-input', 'Source names must be nonempty resource keys without paths or whitespace.');
|
|
40
|
+
}
|
|
41
|
+
if (names.has(source.name)) {
|
|
42
|
+
throw new AtlasError('invalid-input', `Duplicate source name: ${source.name}.`);
|
|
43
|
+
}
|
|
44
|
+
names.add(source.name);
|
|
45
|
+
if (!Number.isSafeInteger(source.width) || source.width < 1 ||
|
|
46
|
+
!Number.isSafeInteger(source.height) || source.height < 1) {
|
|
47
|
+
throw new AtlasError('invalid-input', `Invalid source dimensions: ${source.name}.`);
|
|
48
|
+
}
|
|
49
|
+
pixels += source.width * source.height;
|
|
50
|
+
if (!Number.isSafeInteger(pixels) || pixels > options.maxPixels) {
|
|
51
|
+
throw new AtlasError('invalid-input', 'Total source pixels exceed maxPixels.');
|
|
52
|
+
}
|
|
53
|
+
if (!(source.pixels instanceof Uint8Array || source.pixels instanceof Uint8ClampedArray) ||
|
|
54
|
+
source.pixels.length !== source.width * source.height * 4) {
|
|
55
|
+
throw new AtlasError('invalid-input', `Expected width × height × 4 RGBA bytes: ${source.name}.`);
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
function checkInteger(key, value, min, max) {
|
|
60
|
+
if (!Number.isSafeInteger(value) || value < min || value > max) {
|
|
61
|
+
throw new AtlasError('invalid-options', `${key} must be an integer in ${min}–${max}.`);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
//# sourceMappingURL=options.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"options.js","sourceRoot":"","sources":["../src/options.ts"],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terministic for the same named pixels and options,
|
|
4
|
+
* independent of input order. Performs no I/O and never mutates source buffers.
|
|
5
|
+
*/
|
|
6
|
+
export declare function packAtlas(sources: readonly AtlasSource[], options?: AtlasOptions): AtlasResult;
|
|
7
|
+
//# sourceMappingURL=pack-atlas.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pack-atlas.d.ts","sourceRoot":"","sources":["../src/pack-atlas.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,YAAY,EAAE,WAAW,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAM9E;;;GAGG;AACH,wBAAgB,SAAS,CAAC,OAAO,EAAE,SAAS,WAAW,EAAE,EAAE,OAAO,GAAE,YAAiB,GAAG,WAAW,CAMlG"}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import { createLayout } from './layout.js';
|
|
2
|
+
import { resolveOptions, validateSources } from './options.js';
|
|
3
|
+
import { renderAtlas } from './render.js';
|
|
4
|
+
import { prepareSource } from './trim.js';
|
|
5
|
+
/**
|
|
6
|
+
* Packs one nonrotated atlas. Deterministic for the same named pixels and options,
|
|
7
|
+
* independent of input order. Performs no I/O and never mutates source buffers.
|
|
8
|
+
*/
|
|
9
|
+
export function packAtlas(sources, options = {}) {
|
|
10
|
+
const settings = resolveOptions(options);
|
|
11
|
+
validateSources(sources, settings);
|
|
12
|
+
const prepared = sources.map(source => prepareSource(source, settings))
|
|
13
|
+
.sort((a, b) => a.source.name < b.source.name ? -1 : a.source.name > b.source.name ? 1 : 0);
|
|
14
|
+
return renderAtlas(prepared, createLayout(prepared, settings), settings);
|
|
15
|
+
}
|
|
16
|
+
//# sourceMappingURL=pack-atlas.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"pack-atlas.js","sourceRoot":"","sources":["../src/pack-atlas.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,YAAY,EAAE,MAAM,aAAa,CAAC;AAC3C,OAAO,EAAE,cAAc,EAAE,eAAe,EAAE,MAAM,cAAc,CAAC;AAC/D,OAAO,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC1C,OAAO,EAAE,aAAa,EAAE,MAAM,WAAW,CAAC;AAE1C;;;GAGG;AACH,MAAM,UAAU,SAAS,CAAC,OAA+B,EAAE,UAAwB,EAAE;IACpF,MAAM,QAAQ,GAAG,cAAc,CAAC,OAAO,CAAC,CAAC;IACzC,eAAe,CAAC,OAAO,EAAE,QAAQ,CAAC,CAAC;IACnC,MAAM,QAAQ,GAAG,OAAO,CAAC,GAAG,CAAC,MAAM,CAAC,EAAE,CAAC,aAAa,CAAC,MAAM,EAAE,QAAQ,CAAC,CAAC;SACrE,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,MAAM,CAAC,IAAI,GAAG,CAAC,CAAC,MAAM,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;IAC7F,OAAO,WAAW,CAAC,QAAQ,EAAE,YAAY,CAAC,QAAQ,EAAE,QAAQ,CAAC,EAAE,QAAQ,CAAC,CAAC;AAC1E,CAAC"}
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { AtlasData, AtlasImage, AtlasOptions } from '../AtlasTypes.js';
|
|
2
|
+
export interface PNGAtlasSource {
|
|
3
|
+
readonly name: string;
|
|
4
|
+
readonly png: Uint8Array;
|
|
5
|
+
}
|
|
6
|
+
export interface PNGAtlasResult {
|
|
7
|
+
png: Uint8Array;
|
|
8
|
+
data: AtlasData;
|
|
9
|
+
width: number;
|
|
10
|
+
height: number;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Node.js PNG adapter. Decodes all sources, packs once and encodes RGBA8 PNG.
|
|
14
|
+
* Performs no filesystem writes. Run large jobs in a caller-owned worker.
|
|
15
|
+
*/
|
|
16
|
+
export declare function packPNGAtlas(sources: readonly PNGAtlasSource[], options?: AtlasOptions): PNGAtlasResult;
|
|
17
|
+
/**
|
|
18
|
+
* Decodes static PNG to straight-alpha RGBA8 without gamma correction.
|
|
19
|
+
* maxPixels defaults to 16,777,216; input is limited to 32 MiB. APNG is rejected.
|
|
20
|
+
*/
|
|
21
|
+
export declare function decodePNG(bytes: Uint8Array, maxPixels?: number): AtlasImage;
|
|
22
|
+
/**
|
|
23
|
+
* Encodes straight-alpha RGBA as a static 8-bit PNG; leaves the image buffer unchanged.
|
|
24
|
+
*/
|
|
25
|
+
export declare function encodePNG(image: AtlasImage): Uint8Array;
|
|
26
|
+
//# sourceMappingURL=index.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../../src/png/index.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,SAAS,EAAE,UAAU,EAAE,YAAY,EAAE,MAAM,kBAAkB,CAAC;AAI5E,MAAM,WAAW,cAAc;IAC9B,QAAQ,CAAC,IAAI,EAAE,MAAM,CAAC;IACtB,QAAQ,CAAC,GAAG,EAAE,UAAU,CAAC;CACzB;AAED,MAAM,WAAW,cAAc;IAC9B,GAAG,EAAE,UAAU,CAAC;IAChB,IAAI,EAAE,SAAS,CAAC;IAChB,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;CACf;AAED;;;GAGG;AACH,wBAAgB,YAAY,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,EAAE,OAAO,GAAE,YAAiB,GAAG,cAAc,CAgB3G;AAED;;;GAGG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,EAAE,SAAS,SAAa,GAAG,UAAU,CAa/E;AAED;;GAEG;AACH,wBAAgB,SAAS,CAAC,KAAK,EAAE,UAAU,GAAG,UAAU,CAWvD"}
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
import { Buffer } from 'node:buffer';
|
|
2
|
+
import { PNG } from 'pngjs';
|
|
3
|
+
import { AtlasError } from '../AtlasError.js';
|
|
4
|
+
import { packAtlas } from '../pack-atlas.js';
|
|
5
|
+
import { resolveOptions } from '../options.js';
|
|
6
|
+
/**
|
|
7
|
+
* Node.js PNG adapter. Decodes all sources, packs once and encodes RGBA8 PNG.
|
|
8
|
+
* Performs no filesystem writes. Run large jobs in a caller-owned worker.
|
|
9
|
+
*/
|
|
10
|
+
export function packPNGAtlas(sources, options = {}) {
|
|
11
|
+
const settings = resolveOptions(options);
|
|
12
|
+
if (!Array.isArray(sources) || sources.length === 0 || sources.length > 4096) {
|
|
13
|
+
throw new AtlasError('invalid-input', 'Provide between 1 and 4096 PNG sources.');
|
|
14
|
+
}
|
|
15
|
+
let pixelCount = 0;
|
|
16
|
+
// Read every header before decoding so an oversized batch cannot allocate all decoded images first.
|
|
17
|
+
for (const source of sources) {
|
|
18
|
+
pixelCount += readPixelCount(source.png);
|
|
19
|
+
if (!Number.isSafeInteger(pixelCount) || pixelCount > settings.maxPixels) {
|
|
20
|
+
throw new AtlasError('invalid-input', 'Total source pixels exceed maxPixels.');
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
const decoded = sources.map(source => ({ name: source.name, ...decodePNG(source.png, settings.maxPixels) }));
|
|
24
|
+
const result = packAtlas(decoded, settings);
|
|
25
|
+
return { png: encodePNG(result.image), data: result.data, width: result.image.width, height: result.image.height };
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Decodes static PNG to straight-alpha RGBA8 without gamma correction.
|
|
29
|
+
* maxPixels defaults to 16,777,216; input is limited to 32 MiB. APNG is rejected.
|
|
30
|
+
*/
|
|
31
|
+
export function decodePNG(bytes, maxPixels = 16_777_216) {
|
|
32
|
+
if (!Number.isSafeInteger(maxPixels) || maxPixels < 1 || maxPixels > 67_108_864) {
|
|
33
|
+
throw new AtlasError('invalid-options', 'Invalid maxPixels limit.');
|
|
34
|
+
}
|
|
35
|
+
if (readPixelCount(bytes) > maxPixels) {
|
|
36
|
+
throw new AtlasError('invalid-input', 'PNG pixels exceed maxPixels.');
|
|
37
|
+
}
|
|
38
|
+
try {
|
|
39
|
+
const png = PNG.sync.read(Buffer.from(bytes), { checkCRC: true });
|
|
40
|
+
return { width: png.width, height: png.height, pixels: new Uint8Array(png.data) };
|
|
41
|
+
}
|
|
42
|
+
catch (cause) {
|
|
43
|
+
throw new AtlasError('invalid-png', 'Unable to decode PNG.', { cause });
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
/**
|
|
47
|
+
* Encodes straight-alpha RGBA as a static 8-bit PNG; leaves the image buffer unchanged.
|
|
48
|
+
*/
|
|
49
|
+
export function encodePNG(image) {
|
|
50
|
+
if (!Number.isSafeInteger(image.width) || image.width < 1 || !Number.isSafeInteger(image.height) || image.height < 1 ||
|
|
51
|
+
image.width * image.height > 67_108_864 ||
|
|
52
|
+
!(image.pixels instanceof Uint8Array || image.pixels instanceof Uint8ClampedArray) ||
|
|
53
|
+
image.pixels.length !== image.width * image.height * 4) {
|
|
54
|
+
throw new AtlasError('invalid-input', 'Invalid RGBA image dimensions or pixels.');
|
|
55
|
+
}
|
|
56
|
+
const png = new PNG({ width: image.width, height: image.height });
|
|
57
|
+
png.data.set(image.pixels);
|
|
58
|
+
return new Uint8Array(PNG.sync.write(png, { colorType: 6, inputColorType: 6, bitDepth: 8, deflateLevel: 9, deflateStrategy: 3, filterType: -1 }));
|
|
59
|
+
}
|
|
60
|
+
function readPixelCount(bytes) {
|
|
61
|
+
if (!(bytes instanceof Uint8Array) || bytes.length < 33 || bytes.length > 33_554_432) {
|
|
62
|
+
throw new AtlasError('invalid-png', 'PNG input must contain 33 bytes to 32 MiB.');
|
|
63
|
+
}
|
|
64
|
+
const signature = [137, 80, 78, 71, 13, 10, 26, 10];
|
|
65
|
+
if (!signature.every((byte, index) => bytes[index] === byte)) {
|
|
66
|
+
throw new AtlasError('invalid-png', 'Invalid PNG signature.');
|
|
67
|
+
}
|
|
68
|
+
const buffer = Buffer.from(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
69
|
+
if (buffer.readUInt32BE(8) !== 13 || buffer.toString('ascii', 12, 16) !== 'IHDR') {
|
|
70
|
+
throw new AtlasError('invalid-png', 'PNG must start with an IHDR chunk.');
|
|
71
|
+
}
|
|
72
|
+
const width = buffer.readUInt32BE(16);
|
|
73
|
+
const height = buffer.readUInt32BE(20);
|
|
74
|
+
if (width === 0 || height === 0 || !Number.isSafeInteger(width * height)) {
|
|
75
|
+
throw new AtlasError('invalid-png', 'Invalid PNG dimensions.');
|
|
76
|
+
}
|
|
77
|
+
for (let offset = 8; offset + 12 <= buffer.length;) {
|
|
78
|
+
const length = buffer.readUInt32BE(offset);
|
|
79
|
+
if (length > buffer.length - offset - 12) {
|
|
80
|
+
throw new AtlasError('invalid-png', 'Truncated PNG chunk.');
|
|
81
|
+
}
|
|
82
|
+
if (buffer.toString('ascii', offset + 4, offset + 8) === 'acTL') {
|
|
83
|
+
throw new AtlasError('invalid-png', 'Animated PNG is not supported.');
|
|
84
|
+
}
|
|
85
|
+
offset += length + 12;
|
|
86
|
+
}
|
|
87
|
+
return width * height;
|
|
88
|
+
}
|
|
89
|
+
//# sourceMappingURL=index.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/png/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,MAAM,EAAE,MAAM,aAAa,CAAC;AACrC,OAAO,EAAE,GAAG,EAAE,MAAM,OAAO,CAAC;AAC5B,OAAO,EAAE,UAAU,EAAE,MAAM,kBAAkB,CAAC;AAE9C,OAAO,EAAE,SAAS,EAAE,MAAM,kBAAkB,CAAC;AAC7C,OAAO,EAAE,cAAc,EAAE,MAAM,eAAe,CAAC;AAc/C;;;GAGG;AACH,MAAM,UAAU,YAAY,CAAC,OAAk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AAM,CAAC;AACvB,CAAC"}
|
package/dist/render.d.ts
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { AtlasResult } from './AtlasTypes.js';
|
|
2
|
+
import type { AtlasLayout } from './layout.js';
|
|
3
|
+
import type { PackingOptions } from './options.js';
|
|
4
|
+
import type { PreparedSource } from './trim.js';
|
|
5
|
+
export declare function renderAtlas(sources: readonly PreparedSource[], layout: AtlasLayout, options: PackingOptions): AtlasResult;
|
|
6
|
+
//# sourceMappingURL=render.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.d.ts","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAc,WAAW,EAAE,MAAM,iBAAiB,CAAC;AAC/D,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,aAAa,CAAC;AAC/C,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,WAAW,CAAC;AAEhD,wBAAgB,WAAW,CAAC,OAAO,EAAE,SAAS,cAAc,EAAE,EAAE,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,GAAG,WAAW,CAoBzH"}
|
package/dist/render.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export function renderAtlas(sources, layout, options) {
|
|
2
|
+
const pixels = new Uint8Array(layout.width * layout.height * 4);
|
|
3
|
+
const positions = new Map(layout.placements.map(rect => [rect.name, rect]));
|
|
4
|
+
const frames = sources.map(item => {
|
|
5
|
+
const rect = positions.get(item.source.name);
|
|
6
|
+
const x = rect.x + options.extrude;
|
|
7
|
+
const y = rect.y + options.extrude;
|
|
8
|
+
const frame = { x, y, w: item.width, h: item.height };
|
|
9
|
+
if (item.x !== 0 || item.y !== 0 || item.width !== item.source.width || item.height !== item.source.height) {
|
|
10
|
+
frame.offX = item.x;
|
|
11
|
+
frame.offY = item.y;
|
|
12
|
+
frame.sourceW = item.source.width;
|
|
13
|
+
frame.sourceH = item.source.height;
|
|
14
|
+
}
|
|
15
|
+
if (!item.empty) {
|
|
16
|
+
blit(item, pixels, layout.width, x, y, options.extrude);
|
|
17
|
+
}
|
|
18
|
+
return [item.source.name, frame];
|
|
19
|
+
});
|
|
20
|
+
return { image: { width: layout.width, height: layout.height, pixels }, data: { file: options.file, frames: Object.fromEntries(frames) } };
|
|
21
|
+
}
|
|
22
|
+
function blit(item, target, targetWidth, x, y, extrude) {
|
|
23
|
+
for (let dy = -extrude; dy < item.height + extrude; dy++) {
|
|
24
|
+
const sy = item.y + Math.max(0, Math.min(item.height - 1, dy));
|
|
25
|
+
for (let dx = -extrude; dx < item.width + extrude; dx++) {
|
|
26
|
+
const sx = item.x + Math.max(0, Math.min(item.width - 1, dx));
|
|
27
|
+
const sourceOffset = (sy * item.source.width + sx) * 4;
|
|
28
|
+
const targetOffset = ((y + dy) * targetWidth + x + dx) * 4;
|
|
29
|
+
const alpha = item.source.pixels[sourceOffset + 3];
|
|
30
|
+
// Straight alpha is retained; hidden RGB is canonicalized to avoid stale transparent pixels.
|
|
31
|
+
if (alpha === 0)
|
|
32
|
+
continue;
|
|
33
|
+
target[targetOffset] = item.source.pixels[sourceOffset];
|
|
34
|
+
target[targetOffset + 1] = item.source.pixels[sourceOffset + 1];
|
|
35
|
+
target[targetOffset + 2] = item.source.pixels[sourceOffset + 2];
|
|
36
|
+
target[targetOffset + 3] = alpha;
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=render.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"render.js","sourceRoot":"","sources":["../src/render.ts"],"names":[],"mappings":"AAKA,MAAM,UAAU,WAAW,CAAC,OAAkC,EAAE,MAAmB,EAAE,OAAuB;IAC3G,MAAM,MAAM,GAAG,IAAI,UAAU,CAAC,MAAM,CAAC,KAAK,GAAG,MAAM,CAAC,MAAM,GAAG,CAAC,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AAG,CAAC,GAAG,EAAE,CAAC,GAAG,CAAC,CAAC;YAC3D,MAAM,KAAK,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,GAAG,CAAC,CAAE,CAAC;YACpD,6FAA6F;YAC7F,IAAI,KAAK,KAAK,CAAC;gBAAE,SAAS;YAC1B,MAAM,CAAC,YAAY,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,CAAE,CAAC;YACzD,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,GAAG,CAAC,CAAE,CAAC;YACjE,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC,GAAG,IAAI,CAAC,MAAM,CAAC,MAAM,CAAC,YAAY,GAAG,CAAC,CAAE,CAAC;YACjE,MAAM,CAAC,YAAY,GAAG,CAAC,CAAC,GAAG,KAAK,CAAC;QAClC,CAAC;IACF,CAAC;AACF,CAAC"}
|
package/dist/trim.d.ts
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { AtlasSource } from './AtlasTypes.js';
|
|
2
|
+
import type { PackingOptions } from './options.js';
|
|
3
|
+
export interface PreparedSource {
|
|
4
|
+
source: AtlasSource;
|
|
5
|
+
x: number;
|
|
6
|
+
y: number;
|
|
7
|
+
width: number;
|
|
8
|
+
height: number;
|
|
9
|
+
empty: boolean;
|
|
10
|
+
}
|
|
11
|
+
export declare function prepareSource(source: AtlasSource, options: PackingOptions): PreparedSource;
|
|
12
|
+
//# sourceMappingURL=trim.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trim.d.ts","sourceRoot":"","sources":["../src/trim.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,iBAAiB,CAAC;AACnD,OAAO,KAAK,EAAE,cAAc,EAAE,MAAM,cAAc,CAAC;AAEnD,MAAM,WAAW,cAAc;IAC9B,MAAM,EAAE,WAAW,CAAC;IACpB,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,EAAE,MAAM,CAAC;IACV,KAAK,EAAE,MAAM,CAAC;IACd,MAAM,EAAE,MAAM,CAAC;IACf,KAAK,EAAE,OAAO,CAAC;CACf;AAED,wBAAgB,aAAa,CAAC,MAAM,EAAE,WAAW,EAAE,OAAO,EAAE,cAAc,GAAG,cAAc,CA0B1F"}
|
package/dist/trim.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export function prepareSource(source, options) {
|
|
2
|
+
if (!options.trim) {
|
|
3
|
+
return { source, x: 0, y: 0, width: source.width, height: source.height, empty: false };
|
|
4
|
+
}
|
|
5
|
+
let left = source.width;
|
|
6
|
+
let top = source.height;
|
|
7
|
+
let right = -1;
|
|
8
|
+
let bottom = -1;
|
|
9
|
+
for (let y = 0; y < source.height; y++) {
|
|
10
|
+
for (let x = 0; x < source.width; x++) {
|
|
11
|
+
if (source.pixels[(y * source.width + x) * 4 + 3] < options.alphaThreshold)
|
|
12
|
+
continue;
|
|
13
|
+
left = Math.min(left, x);
|
|
14
|
+
top = Math.min(top, y);
|
|
15
|
+
right = Math.max(right, x);
|
|
16
|
+
bottom = Math.max(bottom, y);
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
if (right < 0) {
|
|
20
|
+
// A transparent placeholder retains original logical dimensions without zero-sized textures.
|
|
21
|
+
return { source, x: 0, y: 0, width: 1, height: 1, empty: true };
|
|
22
|
+
}
|
|
23
|
+
left = Math.max(0, left - options.trimMargin);
|
|
24
|
+
top = Math.max(0, top - options.trimMargin);
|
|
25
|
+
right = Math.min(source.width - 1, right + options.trimMargin);
|
|
26
|
+
bottom = Math.min(source.height - 1, bottom + options.trimMargin);
|
|
27
|
+
return { source, x: left, y: top, width: right - left + 1, height: bottom - top + 1, empty: false };
|
|
28
|
+
}
|
|
29
|
+
//# sourceMappingURL=trim.js.map
|
package/dist/trim.js.map
ADDED
|
@@ -0,0 +1 @@
|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K,GAAG,CAAC,EAAE,CAAC;QACf,6FAA6F;QAC7F,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,CAAC,EAAE,KAAK,EAAE,CAAC,EAAE,MAAM,EAAE,CAAC,EAAE,KAAK,EAAE,IAAI,EAAE,CAAC;IACjE,CAAC;IACD,IAAI,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,IAAI,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC9C,GAAG,GAAG,IAAI,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC5C,KAAK,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,EAAE,KAAK,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAC/D,MAAM,GAAG,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,UAAU,CAAC,CAAC;IAClE,OAAO,EAAE,MAAM,EAAE,CAAC,EAAE,IAAI,EAAE,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,KAAK,GAAG,IAAI,GAAG,CAAC,EAAE,MAAM,EAAE,MAAM,GAAG,GAAG,GAAG,CAAC,EAAE,KAAK,EAAE,KAAK,EAAE,CAAC;AACrG,CAAC"}
|
|
@@ -0,0 +1,76 @@
|
|
|
1
|
+
# @kurot/atlas — AI context map
|
|
2
|
+
|
|
3
|
+
Version 0.1.0, initial local implementation, not published. Independent build-time
|
|
4
|
+
atlas library; no Kurot package dependencies and no integration into consumers yet.
|
|
5
|
+
Read README.md for input limits and the pixel/output contract.
|
|
6
|
+
|
|
7
|
+
## Directory map
|
|
8
|
+
|
|
9
|
+
```text
|
|
10
|
+
src/
|
|
11
|
+
index.ts Portable public entry point, no Node or PNG imports
|
|
12
|
+
AtlasTypes.ts Pixels, stable source names, options and flat sheet types
|
|
13
|
+
AtlasError.ts Error categories
|
|
14
|
+
options.ts Bounds and pixel/source validation
|
|
15
|
+
trim.ts Alpha crop, retained margins, transparent placeholder
|
|
16
|
+
max-rects.ts Nonrotated placement, split/prune free rectangles
|
|
17
|
+
layout.ts POT candidate search and deterministic sorting
|
|
18
|
+
render.ts RGBA blit, edge/corner extrusion, frame serialization
|
|
19
|
+
pack-atlas.ts Pure pack pipeline
|
|
20
|
+
png/index.ts Node-only static PNG decode/encode and batch adapter
|
|
21
|
+
test/
|
|
22
|
+
helpers.ts Synthetic RGBA sources and pixel access
|
|
23
|
+
pack-atlas.test.ts Pixel geometry, packing invariants, validation
|
|
24
|
+
png.test.ts Codec roundtrip, PNG contract and input limits
|
|
25
|
+
scripts/
|
|
26
|
+
verify-reference.ts Read-only local sample validation with output/report in a new directory
|
|
27
|
+
reference-checks.ts Independent crop, pixel, extrusion and overlap checks
|
|
28
|
+
```
|
|
29
|
+
|
|
30
|
+
## Public API
|
|
31
|
+
|
|
32
|
+
- `packAtlas(sources, options?)`: in-memory RGBA to AtlasResult (image and data).
|
|
33
|
+
- `AtlasError`, `AtlasErrorCode`: categorized failures.
|
|
34
|
+
- `AtlasImage`, `AtlasSource`, `AtlasOptions`, `AtlasFrame`, `AtlasData`, `AtlasResult`.
|
|
35
|
+
- Subpath `@kurot/atlas/png`: `decodePNG`, `encodePNG`, `packPNGAtlas`,
|
|
36
|
+
`PNGAtlasSource`, `PNGAtlasResult`. All synchronous, Node.js >=20; no I/O.
|
|
37
|
+
|
|
38
|
+
## Non-obvious invariants
|
|
39
|
+
|
|
40
|
+
- Name is the exact authored resource key, not a guessed filename. Duplicate names
|
|
41
|
+
reject, duplicate pixels do not alias. Object member names are safe frame keys.
|
|
42
|
+
- Input buffers remain unchanged. Output zero-alpha RGB is zero; alpha is straight.
|
|
43
|
+
- Extrusion is outside frames; offX/offY locate trimmed pixels in the source canvas.
|
|
44
|
+
SourceW/sourceH are logical dimensions, not packing bounds.
|
|
45
|
+
- Fully transparent cropped images emit one clear pixel plus original logical size.
|
|
46
|
+
- Trimming is per source; no automatic nine-slice detection. Callers may disable
|
|
47
|
+
trimming for a batch. No default-resource metadata is authored here.
|
|
48
|
+
- Candidate sizes are powers of two and can be rectangular. Packing is heuristic;
|
|
49
|
+
output is deterministic but not identical to TexturePacker.
|
|
50
|
+
- Shape padding reserves right/bottom space even for an outermost item; extrusion
|
|
51
|
+
is already included separately. Border padding is outside all placements.
|
|
52
|
+
- Default settings match the observed sample's trim threshold/margin/extrusion and
|
|
53
|
+
POT single-sheet profile. This is not a generic `.tps` compatibility promise.
|
|
54
|
+
- Errors and results never change files. Atomic project replacement, manifest
|
|
55
|
+
preservation, cache invalidation and worker scheduling belong to the caller.
|
|
56
|
+
- PNG metadata/colorspace are not propagated or gamma-corrected; output is RGBA8.
|
|
57
|
+
- Public root exports must stay free of Node/pngjs imports. No runtime library may
|
|
58
|
+
gain an atlas dependency just to load prebuilt sheets.
|
|
59
|
+
|
|
60
|
+
## Task routing
|
|
61
|
+
|
|
62
|
+
| Task | Files |
|
|
63
|
+
| --- | --- |
|
|
64
|
+
| Input/output contracts | AtlasTypes.ts, options.ts, README.md |
|
|
65
|
+
| Crop/empty-source behavior | trim.ts, render.ts, pixel tests |
|
|
66
|
+
| Packing density/determinism | max-rects.ts, layout.ts, packing tests |
|
|
67
|
+
| Extrusion/transparent RGB | render.ts, pixel tests |
|
|
68
|
+
| PNG safety/encoding | png/index.ts, png.test.ts |
|
|
69
|
+
| Reskin/Editor persistence | Consumer service; outside this package |
|
|
70
|
+
| Sample compatibility and repeatable verification | docs/reference-validation.md, scripts/ |
|
|
71
|
+
| CLI asset watching, validation and future packing | docs/cli-integration.md |
|
|
72
|
+
|
|
73
|
+
Build and test from packages/atlas with pnpm; tests typecheck separately.
|
|
74
|
+
Every new exported function has an explicit return type. Keep new source files
|
|
75
|
+
under 300 lines and follow root docs/code-rules.md. Tests must remain self-contained;
|
|
76
|
+
the local ui-tps directories are read-only reference material, not CI dependencies.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# 图集与 CLI 的接入及优化建议
|
|
2
|
+
|
|
3
|
+
状态:2026-10-07 的源码分析与样例验证。本文是后续方案;本次没有改动 CLI、Core、
|
|
4
|
+
Editor 或 Reskin,也没有添加消费者依赖。
|
|
5
|
+
|
|
6
|
+
## 当前链路
|
|
7
|
+
|
|
8
|
+
```text
|
|
9
|
+
原图 → @kurot/atlas → PNG + 图集 JSON
|
|
10
|
+
↓ 调用方保存
|
|
11
|
+
resource/default.res.json → CLI 读取资源默认值 → 编译 KUI
|
|
12
|
+
resource/ 下的 PNG/JSON → CLI copyAssets → 构建输出
|
|
13
|
+
构建输出的图集 JSON/PNG → Core SheetAnalyzer → SpriteSheet / Texture
|
|
14
|
+
```
|
|
15
|
+
|
|
16
|
+
CLI 没有在编译 KUI 时解析图集的帧坐标或 PNG。
|
|
17
|
+
`src/core/kui/skin-module-builder.ts` 读取 `default.res.json`,通过 ui-document 的
|
|
18
|
+
`parseUIResourceConfigEntries` 获得资源和九宫格默认值;`kui-parser.ts` 在副本上依次
|
|
19
|
+
应用资源默认值、Label presets 和颜色。`plugins/copy-assets.ts` 复制 resource,
|
|
20
|
+
排除 authored KUI 和生成的主题文件。
|
|
21
|
+
|
|
22
|
+
Core 的 `packages/core/src/kurot/resource/analyzers/SheetAnalyzer.ts` 在运行时读取
|
|
23
|
+
`file/frames`,将裁剪坐标、偏移和原始尺寸交给 `SpriteSheet.createTexture`。
|
|
24
|
+
因此本次格式兼容已经足够,无需为了接入库改造 CLI 的 KUI parser 或 Core。
|
|
25
|
+
|
|
26
|
+
atlas 保持独立;Reskin/Editor 的资源服务可以调用库。以后若 CLI 提供打包功能,
|
|
27
|
+
CLI 可依赖 atlas,但 atlas 不依赖 CLI/Core。单纯复制、消费已打包图集无需新增依赖。
|
|
28
|
+
|
|
29
|
+
## 第一优先级:资源更新能够到达预览
|
|
30
|
+
|
|
31
|
+
CLI `src/core/dev-server.ts` 的 `watchResources` 目前仅响应 `.kui.xml`、
|
|
32
|
+
`default.res.json` 和 `config/style.json`,并且要求项目配置 `ui`。
|
|
33
|
+
PNG 与普通图集 JSON 的修改不会触发此监听器。完整 build 能复制新产物,
|
|
34
|
+
但运行中的 dev 服务可能继续提供旧资源。这是源码确认的缺口,尚未做运行中 watcher 验收。
|
|
35
|
+
|
|
36
|
+
建议增加资源变化分支:
|
|
37
|
+
|
|
38
|
+
| 变化 | 建议动作 |
|
|
39
|
+
| --- | --- |
|
|
40
|
+
| 图集 PNG / 帧 JSON,资源名称和九宫格不变 | 校验成对产物、复制资源、通知预览重新加载 |
|
|
41
|
+
| default.res.json 中资源默认九宫格变化 | 校验清单、重新编译受影响 KUI、复制资源 |
|
|
42
|
+
| style.json 中颜色 / 字体 / Label presets 变化 | 继续使用既有样式副本解析链和 KUI 重编译 |
|
|
43
|
+
| KUI 变化 | 保持既有编译流程;Reskin 不提供这个编辑入口 |
|
|
44
|
+
|
|
45
|
+
第一版可复用现有 copyAssets,先保证正确性;后续再只复制变化文件。
|
|
46
|
+
资源监听不应绑定 ui 存在,Core-only 项目也需要复制变化的资源。
|
|
47
|
+
无需通过修改或 touch KUI/default.res.json 伪造更新事件。
|
|
48
|
+
|
|
49
|
+
PNG 和 JSON 各自 rename 并不构成双文件原子事务。调用方应先生成并验证临时产物,
|
|
50
|
+
再提交一整组;同进程用提交完成事件串行触发复制。外部文件监听可采用去抖与有界重试,
|
|
51
|
+
但它们不能严格保证同尺寸 PNG/JSON 是同一代。需要跨进程严格一致时,考虑版本目录
|
|
52
|
+
加一次性指针切换,或显式 commit 标记/代次协议;该方案应独立评审。
|
|
53
|
+
只有成组验证和复制完成后才发布成功事件。预览刷新还要处理 Core 资源缓存和浏览器缓存;
|
|
54
|
+
第一阶段整页重载比只清除一个子纹理可靠,不应向产物 JSON 塞入未定义缓存字段。
|
|
55
|
+
|
|
56
|
+
## 第二优先级:构建前资源完整性校验
|
|
57
|
+
|
|
58
|
+
增加独立校验步骤,位于编译/复制前,不改变 authored KUI:
|
|
59
|
+
|
|
60
|
+
- 清单中的 sheet URL 指向存在的 JSON;JSON 的 file 指向合法、存在的相对 PNG。
|
|
61
|
+
- PNG 尺寸、frame 整数与边界、裁剪偏移及逻辑尺寸一致,拒绝不支持的旋转格式。
|
|
62
|
+
- `subkeys` 继续使用对象格式;对照实际 frame 集合报告缺失/多余条目,
|
|
63
|
+
严格模式下对引用不到的帧失败,错误包含 sheet 名、资源路径和 frame 名。
|
|
64
|
+
- 资源默认 `scale9grid` 按未裁剪的 sourceW/sourceH 校验,不能按图集 x/y 或裁剪 w/h 重算。
|
|
65
|
+
- 沿用现有名称/别名优先级,区分重复裸帧名和可使用的 `sheet.frame` 引用。
|
|
66
|
+
|
|
67
|
+
相关契约见 `packages/ui-document/docs/resource-nine-slice.md`。
|
|
68
|
+
现有清单解析器不读取 PNG/帧文件,因此不能把它通过解析视为完整的图集校验。
|
|
69
|
+
新增纯格式检查可放在独立工具模块;不要仅为读取已有图集强迫 Core 或 ui-document 依赖 atlas。
|
|
70
|
+
若将来共享读取校验器,可提供不含 PNG 编码器的独立入口,再评估 CLI 是否需要依赖。
|
|
71
|
+
|
|
72
|
+
Reskin 默认保持资源键集合,按已有键替换素材。重打包不需要重新生成整份清单。
|
|
73
|
+
保留 frame 的既有九宫格和未知元数据、groups、URL 与资源顺序;出现键集合变化时先报告,
|
|
74
|
+
不能静默删除或重写已有引用。不得自动调整皮肤、布局或把局部字段迁移成 presets。
|
|
75
|
+
|
|
76
|
+
## 第三优先级:增量处理和可选打包入口
|
|
77
|
+
|
|
78
|
+
先按源图内容指纹、规范化 options、库版本缓存产物,命中后跳过解码/打包/编码。
|
|
79
|
+
不同原图路径顺序不影响输出,已有确定性校验覆盖这一点。
|
|
80
|
+
CLI 可以在一次构建中只读取一次清单和每个相关 sheet,建立资源索引,避免多皮肤重复 I/O。
|
|
81
|
+
坐标变化不影响 KUI 编译;默认九宫格或样式变化才影响生成代码。
|
|
82
|
+
按资源/样式到皮肤的依赖关系减少编译,需要先完善引用追踪和删除事件测试。
|
|
83
|
+
|
|
84
|
+
若 CLI 增加 opt-in pack 插件,顺序建议为:
|
|
85
|
+
`配置校验 → 打包 → 成组校验/提交 → KUI 编译 → copyAssets`。
|
|
86
|
+
输入配置需要显式 source/output、稳定资源名、打包选项,禁止依靠扫描结果猜测既有资源键。
|
|
87
|
+
通用 CLI 项目不因已有 PNG/JSON 就自动重打包;其余消费者仍直接调用同一 atlas API。
|
|
88
|
+
工具拥有路径、文件提交、队列/worker 与进度;库继续只返回内存结果。
|
|
89
|
+
|
|
90
|
+
## 图集本身的优化空间
|
|
91
|
+
|
|
92
|
+
两组新图集与 TexturePacker 面积相同,优先优化更新链路与诊断。
|
|
93
|
+
可以试验 PNG filter/deflate 设置、替代无损编码器与不同 MaxRects 排序组合,
|
|
94
|
+
同时记录耗时、PNG 字节、图集面积和内存峰值。以多组资源基准选择策略,
|
|
95
|
+
每种策略必须重新通过 RGBA 与裁剪/扩边校验;减少 PNG 字节不等于减少 GPU 内存。
|
|
96
|
+
|
|
97
|
+
重复像素别名在当前两组里无收益;旋转和 multipack 会扩大 Core/清单/工具契约,
|
|
98
|
+
不作为当前格式一致目标的优化。不可用有损量化、丢弃低 alpha 或减少 margin/extrusion
|
|
99
|
+
换取大小优势并继续声称与当前像素契约一致。
|
|
100
|
+
|
|
101
|
+
## 建议验收顺序
|
|
102
|
+
|
|
103
|
+
1. 保留 `verify:reference` 基线,先完成 Reskin/Editor 的成组替换与整页预览刷新。
|
|
104
|
+
2. CLI 验收 dev 下仅替换 PNG/JSON、删除资源、外部提交中途失败和 Core-only 项目资源更新。
|
|
105
|
+
3. 验收九宫格逻辑尺寸、裁剪图标、manifest 不变、KUI 未修改、现有 preset 引用不变。
|
|
106
|
+
4. 再引入输入缓存、增量复制或 opt-in CLI 打包,分别测量改善和测试失败回滚。
|
|
107
|
+
|
|
108
|
+
本次兼容性证据见 [reference-validation.md](./reference-validation.md)。
|
|
@@ -0,0 +1,96 @@
|
|
|
1
|
+
# basis / basis_icon 格式与像素验证
|
|
2
|
+
|
|
3
|
+
验证日期:2026-10-07(Asia/Shanghai)。实现:本地 `@kurot/atlas 0.1.0`。
|
|
4
|
+
参考项目:`CrashMaster/kurot-project-kui`,采用本次更新后的原图与 TexturePacker 产物。
|
|
5
|
+
|
|
6
|
+
## 结论
|
|
7
|
+
|
|
8
|
+
两组共 61 个资源的名称、帧字段、裁剪尺寸、偏移、原始尺寸和像素均通过验证。
|
|
9
|
+
新产物可以采用现有资源格式;不需要修改 KUI、资源清单或 Core 的图集解析。
|
|
10
|
+
排布坐标有所不同,PNG 和 JSON 必须成对替换。格式兼容不代表产物字节相同。
|
|
11
|
+
|
|
12
|
+
| 项目 | basis | basis_icon |
|
|
13
|
+
| --- | --- | --- |
|
|
14
|
+
| 原图 / 帧数 | 29 / 29 | 32 / 32 |
|
|
15
|
+
| 发生裁剪 | 0 | 32 |
|
|
16
|
+
| TexturePacker 图集 | 1024 × 1024 | 256 × 512 |
|
|
17
|
+
| atlas 图集 | 1024 × 1024 | 256 × 512 |
|
|
18
|
+
| 有效帧矩形总面积,不含扩边 | 728,044 px | 97,021 px |
|
|
19
|
+
| 排布坐标不同的帧 | 26 | 32 |
|
|
20
|
+
| TexturePacker PNG 大小 | 875,190 bytes | 158,473 bytes |
|
|
21
|
+
| atlas PNG 大小 | 909,753 bytes | 159,652 bytes |
|
|
22
|
+
|
|
23
|
+
新 PNG 分别大约多 3.95% 和 0.74%。当前没有图集面积或 GPU 纹理内存收益。
|
|
24
|
+
PNG 字节大小由排布与编码共同影响,不能把差异全部归因于压缩器。
|
|
25
|
+
本次没有进行稳定的性能基准,也没有启动浏览器做交互或截图验收。
|
|
26
|
+
|
|
27
|
+
## 参考材料与配置
|
|
28
|
+
|
|
29
|
+
- 原图:`ui-tps/src/basis/*.png`、`ui-tps/src/basis_icon/*.png`。
|
|
30
|
+
- 参考:`ui-tps/export/r_basis.{json,png,tps}`、`r_basis_icon.{json,png,tps}`。
|
|
31
|
+
- 项目使用的资源:`resource/assets/ui/app/r_basis.{json,png}`、`r_basis_icon.{json,png}`。
|
|
32
|
+
两组运行时 PNG、JSON 与 export 文件均逐字节相同。
|
|
33
|
+
- TexturePacker 8.3 的 Egret 导出:单图集、无旋转、POT、允许矩形、最大 2048²、
|
|
34
|
+
缩放 1、RGBA8888 PNG、alpha threshold 1、trim margin 1、extrude 1、
|
|
35
|
+
shape/border padding 0、清除完全透明像素的 RGB。开启 auto-alias,但这两组没有别名帧。
|
|
36
|
+
|
|
37
|
+
本次 atlas 使用默认打包选项,只显式指定对应 PNG 文件名。
|
|
38
|
+
库没有读取 `.tps`,验证范围是上述实际配置与两组资源。
|
|
39
|
+
|
|
40
|
+
## 校验覆盖
|
|
41
|
+
|
|
42
|
+
`scripts/verify-reference.ts` 和 `scripts/reference-checks.ts` 执行:
|
|
43
|
+
|
|
44
|
+
1. 原图名称与参考 JSON 的帧集合完全相同。
|
|
45
|
+
2. JSON 使用 `file` + `frames`;每帧是整数 `x/y/w/h` 与可选
|
|
46
|
+
`offX/offY/sourceW/sourceH`,没有旋转或嵌套格式。
|
|
47
|
+
3. 参考 PNG 与新 PNG 均为 8-bit RGBA;帧与 1 px 扩边在图集边界内,扩边矩形不重叠。
|
|
48
|
+
4. 独立计算原图 alpha ≥ 1 的包围盒,加 1 px margin 后夹到原图边界;
|
|
49
|
+
对参考、新产物分别验证裁剪。裁掉的区域没有非零 alpha。
|
|
50
|
+
5. 按 offX/offY 将每帧放回原始画布,逐通道核对所有保留像素;
|
|
51
|
+
完全透明像素的 RGB 按 0 核对,非零 alpha 像素的 RGBA 必须精确相同。
|
|
52
|
+
6. 逐像素核对四边和四角的 1 px extrusion。
|
|
53
|
+
7. 新旧帧除了 x/y,其他字段的值和字段是否存在都完全相同。
|
|
54
|
+
8. 反转输入顺序后,新 PNG 字节和 JSON 对象保持一致。
|
|
55
|
+
9. 报告保留源图和参考产物的 SHA-256,供后续原图更新时识别验证基线。
|
|
56
|
+
|
|
57
|
+
例如 `r_icon_bars` 裁剪为 56 × 43,offX/offY 为 6/11,逻辑尺寸仍是 64 × 64。
|
|
58
|
+
基础资源 `r_bg_stepper` 为 180 × 60,`r_txt_input` 为 140 × 60。
|
|
59
|
+
|
|
60
|
+
TexturePacker 的顶层 `meta` 不由新库生成;Core 的 SheetAnalyzer 不读取它。
|
|
61
|
+
`meta.smartupdate` 属于 TexturePacker 更新信息,不能保留旧值冒充新产物。
|
|
62
|
+
|
|
63
|
+
## CLI 实际构建验证
|
|
64
|
+
|
|
65
|
+
在项目临时副本中使用样例已安装的 CLI 3.3.0,先构建原始图集,再仅替换两组 PNG/JSON。
|
|
66
|
+
两次 development `build --strict` 和替换后的 `build --release --strict` 均成功,
|
|
67
|
+
每次编译 66 个 KUI skin。没有修改真实示例项目。
|
|
68
|
+
|
|
69
|
+
- 66 个 authored KUI、`default.res.json` 和 `style.json` 与原项目逐字节相同。
|
|
70
|
+
- development 编译的 `default.thm.js` 在归一化 CLI 随机临时目录名后完全相同;
|
|
71
|
+
原始字节比较会因 `kurot-skins-<random>` 的生成来源注释不同而失败。
|
|
72
|
+
- development 和 release 中的四个新图集资源均与打包结果逐字节相同。
|
|
73
|
+
- 构建验证证明现有编译和复制链可接收这些产物;逐帧像素校验与 Core 源码契约
|
|
74
|
+
支持裁剪/偏移兼容判断,未执行浏览器资源加载测试。
|
|
75
|
+
|
|
76
|
+
使用已安装的 CLI 入口直接调用 `node node_modules/@kurot/cli/dist/index.js ...`。
|
|
77
|
+
临时副本中的 `pnpm exec` 尝试检查依赖,并因新发布的 CLI/ui-document 未满足
|
|
78
|
+
minimumReleaseAge 而失败;没有放宽策略、重新安装或更换依赖。
|
|
79
|
+
|
|
80
|
+
## 重复验证
|
|
81
|
+
|
|
82
|
+
从引擎仓库根目录运行,第二个参数必须是尚不存在、且位于示例项目外的目录:
|
|
83
|
+
|
|
84
|
+
```sh
|
|
85
|
+
pnpm --dir packages/atlas verify:reference \
|
|
86
|
+
/path/to/CrashMaster/kurot-project-kui \
|
|
87
|
+
/tmp/kurot-atlas-reference-new
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
脚本只读示例项目,在新目录输出两组 PNG/JSON 和 `report.json`;失败会以非零码退出。
|
|
91
|
+
它是本地参考验证工具,不依赖固定的开发者路径,不纳入默认 CI 测试,也不发布脚本。
|
|
92
|
+
基线文件更新后需要重新执行,旧报告不能证明新文件通过验证。
|
|
93
|
+
|
|
94
|
+
本次本地产物位于 `/private/tmp/kurot-atlas-reference-20261007`,另有
|
|
95
|
+
`cli-report.json` 记录临时项目的构建核对结果。这些临时文件不随包分发。
|
|
96
|
+
后续接入与优化见 [cli-integration.md](./cli-integration.md)。
|
package/package.json
CHANGED
|
@@ -1,6 +1,41 @@
|
|
|
1
1
|
{
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
2
|
+
"name": "@kurot/atlas",
|
|
3
|
+
"version": "0.1.0",
|
|
4
|
+
"description": "Independent RGBA atlas packing and PNG tooling with Kurot-compatible sheet output",
|
|
5
|
+
"author": "Kelvin <kyia.x52@gmail.com>",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"type": "module",
|
|
8
|
+
"main": "./dist/index.js",
|
|
9
|
+
"module": "./dist/index.js",
|
|
10
|
+
"types": "./dist/index.d.ts",
|
|
11
|
+
"exports": {
|
|
12
|
+
".": { "types": "./dist/index.d.ts", "import": "./dist/index.js" },
|
|
13
|
+
"./png": { "types": "./dist/png/index.d.ts", "import": "./dist/png/index.js" }
|
|
14
|
+
},
|
|
15
|
+
"files": ["dist/**/*", "docs/**/*", "CHANGELOG.md", "LICENSE"],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"build": "tsc --build",
|
|
18
|
+
"dev": "tsc --watch",
|
|
19
|
+
"clean": "rm -rf dist",
|
|
20
|
+
"test": "tsc --project tsconfig.test.json && vitest run",
|
|
21
|
+
"test:watch": "vitest",
|
|
22
|
+
"verify:reference": "tsc --project tsconfig.verify.json && node .verify/scripts/verify-reference.js",
|
|
23
|
+
"prepublishOnly": "pnpm run clean && pnpm run build"
|
|
24
|
+
},
|
|
25
|
+
"dependencies": { "pngjs": "^7.0.0" },
|
|
26
|
+
"devDependencies": {
|
|
27
|
+
"@types/node": "^22.0.0",
|
|
28
|
+
"@types/pngjs": "^6.0.5",
|
|
29
|
+
"typescript": "^5.6.0",
|
|
30
|
+
"vitest": "^4.1.4"
|
|
31
|
+
},
|
|
32
|
+
"engines": { "node": ">=20" },
|
|
33
|
+
"keywords": ["kurot", "atlas", "sprite-sheet", "maxrects", "typescript"],
|
|
34
|
+
"repository": {
|
|
35
|
+
"type": "git",
|
|
36
|
+
"url": "git+https://github.com/kurot-engine/kurot.git",
|
|
37
|
+
"directory": "packages/atlas"
|
|
38
|
+
},
|
|
39
|
+
"packageManager": "pnpm@10.33.0",
|
|
40
|
+
"publishConfig": { "access": "public" }
|
|
41
|
+
}
|