psd 4.0.0 → 4.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/README.md +7 -2
- package/dist/image.d.ts +6 -2
- package/dist/image.d.ts.map +1 -1
- package/dist/image.js +8 -12
- package/dist/image.js.map +1 -1
- package/dist/layer-info.d.ts +1 -5
- package/dist/layer-info.d.ts.map +1 -1
- package/dist/layer-info.js +2 -3
- package/dist/layer-info.js.map +1 -1
- package/dist/layer-styles.d.ts +1 -1
- package/dist/layer-styles.d.ts.map +1 -1
- package/dist/layer.d.ts.map +1 -1
- package/dist/layer.js +13 -2
- package/dist/layer.js.map +1 -1
- package/dist/nodes.d.ts +10 -8
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +6 -6
- package/dist/nodes.js.map +1 -1
- package/dist/png.d.ts +1 -2
- package/dist/png.d.ts.map +1 -1
- package/dist/png.js +57 -29
- package/dist/png.js.map +1 -1
- package/dist/resources.d.ts +1 -1
- package/dist/resources.d.ts.map +1 -1
- package/dist/type-tool.d.ts +35 -11
- package/dist/type-tool.d.ts.map +1 -1
- package/dist/type-tool.js +60 -17
- package/dist/type-tool.js.map +1 -1
- package/package.json +2 -1
- package/src/image.ts +8 -14
- package/src/layer-info.ts +2 -3
- package/src/layer-styles.ts +1 -1
- package/src/layer.ts +13 -2
- package/src/nodes.ts +6 -5
- package/src/png.ts +55 -29
- package/src/resources.ts +1 -1
- package/src/type-tool.ts +63 -17
package/src/nodes.ts
CHANGED
|
@@ -347,13 +347,14 @@ export class RootNode extends BaseNode {
|
|
|
347
347
|
const stack: Array<RootNode | GroupNode> = [];
|
|
348
348
|
for (const layer of psd.layers) {
|
|
349
349
|
if (layer.isFolder) {
|
|
350
|
+
const group = new GroupNode(layer, current);
|
|
351
|
+
current.children.push(group);
|
|
350
352
|
stack.push(current);
|
|
351
|
-
current =
|
|
353
|
+
current = group;
|
|
352
354
|
} else if (layer.isFolderEnd) {
|
|
353
|
-
|
|
354
|
-
|
|
355
|
-
|
|
356
|
-
current = parent;
|
|
355
|
+
// Files from some editors have unbalanced markers. Rather than fail, a stray end marker is
|
|
356
|
+
// ignored, and groups still open at the bottom of the list end there.
|
|
357
|
+
current = stack.pop() ?? current;
|
|
357
358
|
} else {
|
|
358
359
|
current.children.push(new LayerNode(layer, current));
|
|
359
360
|
}
|
package/src/png.ts
CHANGED
|
@@ -5,31 +5,53 @@ export interface Pixels {
|
|
|
5
5
|
data: Uint8Array | Uint8ClampedArray;
|
|
6
6
|
}
|
|
7
7
|
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
/** Writes the chunks to a (de)compression stream as they're produced and returns its output chunks. */
|
|
9
|
+
async function transform(input: Iterable<Uint8Array>, stream: CompressionStream | DecompressionStream): Promise<Uint8Array[]> {
|
|
10
|
+
const writer = stream.writable.getWriter();
|
|
11
|
+
const reader = stream.readable.getReader();
|
|
12
|
+
const output: Uint8Array[] = [];
|
|
13
|
+
const write = async () => {
|
|
14
|
+
for (const bytes of input) await writer.write(bytes as Uint8Array<ArrayBuffer>);
|
|
15
|
+
await writer.close();
|
|
16
|
+
};
|
|
17
|
+
const read = async () => {
|
|
18
|
+
for (let r = await reader.read(); !r.done; r = await reader.read()) output.push(r.value);
|
|
19
|
+
};
|
|
20
|
+
await Promise.all([write(), read()]);
|
|
21
|
+
return output;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function concat(parts: Uint8Array[]): Uint8Array {
|
|
25
|
+
const out = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
26
|
+
let offset = 0;
|
|
27
|
+
for (const p of parts) {
|
|
28
|
+
out.set(p, offset);
|
|
29
|
+
offset += p.length;
|
|
30
|
+
}
|
|
31
|
+
return out;
|
|
32
|
+
}
|
|
10
33
|
|
|
11
|
-
export const inflate = (bytes: Uint8Array) => transform(bytes, new DecompressionStream('deflate'));
|
|
12
|
-
export const deflate = (bytes: Uint8Array) => transform(bytes, new CompressionStream('deflate'));
|
|
34
|
+
export const inflate = async (bytes: Uint8Array) => concat(await transform([bytes], new DecompressionStream('deflate')));
|
|
13
35
|
|
|
14
36
|
const CRC_TABLE = Array.from({ length: 256 }, (_, n) => {
|
|
15
37
|
for (let k = 0; k < 8; k++) n = n & 1 ? 0xedb88320 ^ (n >>> 1) : n >>> 1;
|
|
16
38
|
return n >>> 0;
|
|
17
39
|
});
|
|
18
40
|
|
|
19
|
-
function crc32(
|
|
41
|
+
function crc32(parts: Uint8Array[]): number {
|
|
20
42
|
let crc = 0xffffffff;
|
|
21
|
-
for (const b of bytes) crc = CRC_TABLE[(crc ^ b) & 0xff]! ^ (crc >>> 8);
|
|
43
|
+
for (const bytes of parts) for (const b of bytes) crc = CRC_TABLE[(crc ^ b) & 0xff]! ^ (crc >>> 8);
|
|
22
44
|
return (crc ^ 0xffffffff) >>> 0;
|
|
23
45
|
}
|
|
24
46
|
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
const
|
|
28
|
-
|
|
29
|
-
for (let i = 0; i < 4; i++)
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
return
|
|
47
|
+
/** A PNG chunk as a list of parts, so large data isn't copied until the file is assembled. */
|
|
48
|
+
function chunk(type: string, data: Uint8Array[]): Uint8Array[] {
|
|
49
|
+
const head = new Uint8Array(8);
|
|
50
|
+
new DataView(head.buffer).setUint32(0, data.reduce((n, d) => n + d.length, 0));
|
|
51
|
+
for (let i = 0; i < 4; i++) head[4 + i] = type.charCodeAt(i);
|
|
52
|
+
const crc = new Uint8Array(4);
|
|
53
|
+
new DataView(crc.buffer).setUint32(0, crc32([head.subarray(4), ...data]));
|
|
54
|
+
return [head, ...data, crc];
|
|
33
55
|
}
|
|
34
56
|
|
|
35
57
|
/** Encodes 8-bit grayscale or RGBA pixels as a PNG file. */
|
|
@@ -45,22 +67,26 @@ export async function encodePng({ width, height, data }: Pixels): Promise<Uint8A
|
|
|
45
67
|
header[8] = 8; // bit depth
|
|
46
68
|
header[9] = channels === 4 ? 6 : 0; // RGBA or grayscale
|
|
47
69
|
|
|
48
|
-
// Each row is prefixed with filter type 0 (none).
|
|
70
|
+
// Each row is prefixed with filter type 0 (none). Rows are compressed about 1MB at a time
|
|
71
|
+
// rather than copied into one buffer, which would double the image's memory.
|
|
49
72
|
const rowLength = width * channels;
|
|
50
|
-
const
|
|
51
|
-
|
|
73
|
+
const blockRows = Math.max(1, Math.floor(2 ** 20 / (rowLength + 1)));
|
|
74
|
+
function* rows() {
|
|
75
|
+
for (let start = 0; start < height; start += blockRows) {
|
|
76
|
+
const count = Math.min(blockRows, height - start);
|
|
77
|
+
const block = new Uint8Array((rowLength + 1) * count);
|
|
78
|
+
for (let y = 0; y < count; y++) {
|
|
79
|
+
const row = (start + y) * rowLength;
|
|
80
|
+
block.set(data.subarray(row, row + rowLength), y * (rowLength + 1) + 1);
|
|
81
|
+
}
|
|
82
|
+
yield block;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
52
85
|
|
|
53
|
-
|
|
86
|
+
return concat([
|
|
54
87
|
new Uint8Array([0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a]),
|
|
55
|
-
chunk('IHDR', header),
|
|
56
|
-
chunk('IDAT', await
|
|
57
|
-
chunk('IEND',
|
|
58
|
-
];
|
|
59
|
-
const png = new Uint8Array(parts.reduce((n, p) => n + p.length, 0));
|
|
60
|
-
let offset = 0;
|
|
61
|
-
for (const p of parts) {
|
|
62
|
-
png.set(p, offset);
|
|
63
|
-
offset += p.length;
|
|
64
|
-
}
|
|
65
|
-
return png;
|
|
88
|
+
...chunk('IHDR', [header]),
|
|
89
|
+
...chunk('IDAT', await transform(rows(), new CompressionStream('deflate'))),
|
|
90
|
+
...chunk('IEND', []),
|
|
91
|
+
]);
|
|
66
92
|
}
|
package/src/resources.ts
CHANGED
|
@@ -24,7 +24,7 @@ export interface Guide {
|
|
|
24
24
|
}
|
|
25
25
|
|
|
26
26
|
export interface ResolutionInfo {
|
|
27
|
-
/** Horizontal resolution in pixels per `hResUnitName
|
|
27
|
+
/** Horizontal resolution in pixels per inch. `hResUnitName` is only the unit Photoshop displays it in. */
|
|
28
28
|
hRes: number;
|
|
29
29
|
hResUnit: number;
|
|
30
30
|
hResUnitName: string;
|
package/src/type-tool.ts
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
import { readDescriptor, type Descriptor } from './descriptor.ts';
|
|
2
2
|
import { parseEngineData, type EngineDict } from './engine-data.ts';
|
|
3
3
|
import type { Reader } from './reader.ts';
|
|
4
|
+
import type { Bounds } from './resources.ts';
|
|
4
5
|
|
|
5
6
|
export interface Transform {
|
|
6
7
|
xx: number;
|
|
@@ -54,10 +55,6 @@ export class TypeTool {
|
|
|
54
55
|
readonly textData: Descriptor;
|
|
55
56
|
/** The raw warp descriptor. */
|
|
56
57
|
readonly warpData: Descriptor;
|
|
57
|
-
readonly left: number;
|
|
58
|
-
readonly top: number;
|
|
59
|
-
readonly right: number;
|
|
60
|
-
readonly bottom: number;
|
|
61
58
|
#engineData: EngineDict | undefined;
|
|
62
59
|
|
|
63
60
|
constructor(r: Reader) {
|
|
@@ -67,13 +64,41 @@ export class TypeTool {
|
|
|
67
64
|
this.textData = readDescriptor(r);
|
|
68
65
|
r.skip(6); // warp version + descriptor version
|
|
69
66
|
this.warpData = readDescriptor(r);
|
|
70
|
-
|
|
71
|
-
this.top = r.int32();
|
|
72
|
-
this.right = r.int32();
|
|
73
|
-
this.bottom = r.int32();
|
|
67
|
+
// The block ends with left, top, right and bottom fields that Photoshop leaves as 0.
|
|
74
68
|
}
|
|
75
69
|
|
|
76
|
-
/**
|
|
70
|
+
/**
|
|
71
|
+
* The left edge of the text's frame, in pixels relative to the transform origin (`tx`, `ty`) and
|
|
72
|
+
* before the transform. The frame spans the lines around the anchor point for point text, or the box
|
|
73
|
+
* for paragraph text (its top can reach above the box to fit the first line). Undefined in files
|
|
74
|
+
* from older Photoshop versions.
|
|
75
|
+
*/
|
|
76
|
+
get left(): number | undefined {
|
|
77
|
+
return this.textData.bounds?.Left.value;
|
|
78
|
+
}
|
|
79
|
+
/** See `left`. */
|
|
80
|
+
get top(): number | undefined {
|
|
81
|
+
return this.textData.bounds?.['Top '].value;
|
|
82
|
+
}
|
|
83
|
+
/** See `left`. */
|
|
84
|
+
get right(): number | undefined {
|
|
85
|
+
return this.textData.bounds?.Rght.value;
|
|
86
|
+
}
|
|
87
|
+
/** See `left`. */
|
|
88
|
+
get bottom(): number | undefined {
|
|
89
|
+
return this.textData.bounds?.Btom.value;
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
/** The tight bounds of the glyphs, relative to the transform origin like `left`/`top`/`right`/`bottom`. */
|
|
93
|
+
get boundingBox(): Bounds | undefined {
|
|
94
|
+
const box = this.textData.boundingBox;
|
|
95
|
+
return box && { left: box.Left.value, top: box['Top '].value, right: box.Rght.value, bottom: box.Btom.value };
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* The text content. Paragraphs are separated by `\r`, and line breaks within a paragraph
|
|
100
|
+
* (Shift+Enter) are `\u0003`; neither displays as a new line in HTML or a terminal.
|
|
101
|
+
*/
|
|
77
102
|
get text(): string {
|
|
78
103
|
return this.textData['Txt '];
|
|
79
104
|
}
|
|
@@ -88,7 +113,10 @@ export class TypeTool {
|
|
|
88
113
|
return this.engineData.ResourceDict.FontSet.map((f: EngineDict) => f.Name);
|
|
89
114
|
}
|
|
90
115
|
|
|
91
|
-
/**
|
|
116
|
+
/**
|
|
117
|
+
* The style of each run of characters, in order. Runs only store what they change from the
|
|
118
|
+
* document's normal style sheet, so the rest comes from that.
|
|
119
|
+
*/
|
|
92
120
|
get runs(): TextRun[] {
|
|
93
121
|
const styleRun = this.engineData.EngineDict.StyleRun;
|
|
94
122
|
const lengths: number[] = [...styleRun.RunLengthArray];
|
|
@@ -96,14 +124,14 @@ export class TypeTool {
|
|
|
96
124
|
const total = lengths.reduce((sum, n) => sum + n, 0);
|
|
97
125
|
if (total - this.text.length === 1) lengths[lengths.length - 1]! -= 1;
|
|
98
126
|
|
|
99
|
-
const
|
|
127
|
+
const { FontSet, StyleSheetSet, TheNormalStyleSheet } = this.engineData.ResourceDict;
|
|
128
|
+
const defaults = StyleSheetSet[TheNormalStyleSheet].StyleSheetData;
|
|
100
129
|
return styleRun.RunArray.map((run: EngineDict, i: number): TextRun => {
|
|
101
|
-
const s = run.StyleSheet.StyleSheetData;
|
|
130
|
+
const s = { ...defaults, ...run.StyleSheet.StyleSheetData };
|
|
102
131
|
return {
|
|
103
132
|
length: lengths[i] ?? 0,
|
|
104
|
-
font: s.Font === undefined ? undefined :
|
|
133
|
+
font: s.Font === undefined ? undefined : FontSet[s.Font]?.Name,
|
|
105
134
|
size: s.FontSize,
|
|
106
|
-
// Opaque black is sometimes omitted.
|
|
107
135
|
color: s.FillColor ? toRGBA(s.FillColor.Values) : undefined,
|
|
108
136
|
leading: s.AutoLeading === false && s.Leading !== undefined ? s.Leading : 'auto',
|
|
109
137
|
fauxBold: s.FauxBold ?? false,
|
|
@@ -114,11 +142,25 @@ export class TypeTool {
|
|
|
114
142
|
});
|
|
115
143
|
}
|
|
116
144
|
|
|
117
|
-
/** Unique font sizes, in
|
|
145
|
+
/** Unique font sizes as stored, in pixels before `transform` scales the text. See `pointSizes()`. */
|
|
118
146
|
get sizes(): number[] {
|
|
119
147
|
return uniq(this.runs.flatMap((r) => (r.size === undefined ? [] : [r.size])));
|
|
120
148
|
}
|
|
121
149
|
|
|
150
|
+
/** How much `transform` scales the text vertically, which is what Photoshop scales font sizes by. */
|
|
151
|
+
get #scale(): number {
|
|
152
|
+
return Math.hypot(this.transform.yx, this.transform.yy);
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Unique font sizes in points, as Photoshop's Character panel shows them: scaled by `transform`
|
|
157
|
+
* and converted at `ppi` pixels per inch (`psd.resources.resolutionInfo.vRes`). Pass 72 for pixels.
|
|
158
|
+
*/
|
|
159
|
+
pointSizes(ppi: number): number[] {
|
|
160
|
+
if (!(ppi > 0)) throw new Error(`Invalid resolution: ${ppi}`);
|
|
161
|
+
return this.sizes.map((size) => (size * this.#scale * 72) / ppi);
|
|
162
|
+
}
|
|
163
|
+
|
|
122
164
|
/** Unique text colors (RGBA). */
|
|
123
165
|
get colors(): RGBA[] {
|
|
124
166
|
return uniq(this.runs.map((r) => r.color ?? [0, 0, 0, 255]));
|
|
@@ -131,8 +173,11 @@ export class TypeTool {
|
|
|
131
173
|
|
|
132
174
|
/** The alignment of each paragraph. */
|
|
133
175
|
get alignment(): TextAlignment[] {
|
|
176
|
+
const { ParagraphSheetSet, TheNormalParagraphSheet } = this.engineData.ResourceDict;
|
|
177
|
+
const defaults = ParagraphSheetSet[TheNormalParagraphSheet].Properties;
|
|
134
178
|
return this.engineData.EngineDict.ParagraphRun.RunArray.map(
|
|
135
|
-
|
|
179
|
+
// Photoshop leaves Justification out of the normal sheet when it's 0 (left).
|
|
180
|
+
(p: EngineDict) => ALIGNMENTS[Math.min(p.ParagraphSheet.Properties.Justification ?? defaults.Justification ?? 0, 3)]!,
|
|
136
181
|
);
|
|
137
182
|
}
|
|
138
183
|
|
|
@@ -140,7 +185,7 @@ export class TypeTool {
|
|
|
140
185
|
toCSS(): string {
|
|
141
186
|
const definition: Record<string, string | undefined> = {
|
|
142
187
|
'font-family': this.fonts.join(', '),
|
|
143
|
-
'font-size': this.sizes[0] === undefined ? undefined : `${this.sizes[0]}
|
|
188
|
+
'font-size': this.sizes[0] === undefined ? undefined : `${this.sizes[0] * this.#scale}px`,
|
|
144
189
|
color: `rgba(${this.colors[0]!.join(', ')})`,
|
|
145
190
|
'text-align': this.alignment[0],
|
|
146
191
|
};
|
|
@@ -166,6 +211,7 @@ export class TypeTool {
|
|
|
166
211
|
top: this.top,
|
|
167
212
|
right: this.right,
|
|
168
213
|
bottom: this.bottom,
|
|
214
|
+
boundingBox: this.boundingBox,
|
|
169
215
|
transform: this.transform,
|
|
170
216
|
};
|
|
171
217
|
}
|