psd 3.4.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 +196 -149
- package/dist/descriptor.d.ts +20 -0
- package/dist/descriptor.d.ts.map +1 -0
- package/dist/descriptor.js +114 -0
- package/dist/descriptor.js.map +1 -0
- package/dist/engine-data.d.ts +11 -0
- package/dist/engine-data.d.ts.map +1 -0
- package/dist/engine-data.js +82 -0
- package/dist/engine-data.js.map +1 -0
- package/dist/image.d.ts +52 -0
- package/dist/image.d.ts.map +1 -0
- package/dist/image.js +301 -0
- package/dist/image.js.map +1 -0
- package/dist/index.d.ts +16 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/layer-info.d.ts +494 -0
- package/dist/layer-info.d.ts.map +1 -0
- package/dist/layer-info.js +529 -0
- package/dist/layer-info.js.map +1 -0
- package/dist/layer-styles.d.ts +160 -0
- package/dist/layer-styles.d.ts.map +1 -0
- package/dist/layer-styles.js +183 -0
- package/dist/layer-styles.js.map +1 -0
- package/dist/layer.d.ts +143 -0
- package/dist/layer.d.ts.map +1 -0
- package/dist/layer.js +249 -0
- package/dist/layer.js.map +1 -0
- package/dist/nodes.d.ts +233 -0
- package/dist/nodes.d.ts.map +1 -0
- package/dist/nodes.js +326 -0
- package/dist/nodes.js.map +1 -0
- package/dist/path-record.d.ts +34 -0
- package/dist/path-record.d.ts.map +1 -0
- package/dist/path-record.js +44 -0
- package/dist/path-record.js.map +1 -0
- package/dist/png.d.ts +10 -0
- package/dist/png.d.ts.map +1 -0
- package/dist/png.js +85 -0
- package/dist/png.js.map +1 -0
- package/dist/psd.d.ts +49 -0
- package/dist/psd.d.ts.map +1 -0
- package/dist/psd.js +106 -0
- package/dist/psd.js.map +1 -0
- package/dist/reader.d.ts +41 -0
- package/dist/reader.d.ts.map +1 -0
- package/dist/reader.js +100 -0
- package/dist/reader.js.map +1 -0
- package/dist/resources.d.ts +105 -0
- package/dist/resources.d.ts.map +1 -0
- package/dist/resources.js +173 -0
- package/dist/resources.js.map +1 -0
- package/dist/type-tool.d.ts +142 -0
- package/dist/type-tool.d.ts.map +1 -0
- package/dist/type-tool.js +222 -0
- package/dist/type-tool.js.map +1 -0
- package/package.json +26 -32
- package/src/descriptor.ts +141 -0
- package/src/engine-data.ts +86 -0
- package/src/image.ts +338 -0
- package/src/index.ts +38 -0
- package/src/layer-info.ts +578 -0
- package/src/layer-styles.ts +339 -0
- package/src/layer.ts +334 -0
- package/src/nodes.ts +411 -0
- package/src/path-record.ts +69 -0
- package/src/png.ts +92 -0
- package/src/psd.ts +133 -0
- package/src/reader.ts +125 -0
- package/src/resources.ts +273 -0
- package/src/type-tool.ts +270 -0
- package/.travis.yml +0 -5
- package/Cakefile +0 -61
- package/DEVELOPMENT.md +0 -27
- package/bower.json +0 -26
- package/docs/docco.css +0 -284
- package/docs/lib/psd/blend_mode.coffee.html +0 -72
- package/docs/lib/psd/channel_image.coffee.html +0 -81
- package/docs/lib/psd/color.coffee.html +0 -13
- package/docs/lib/psd/descriptor.coffee.html +0 -173
- package/docs/lib/psd/file.coffee.html +0 -78
- package/docs/lib/psd/header.coffee.html +0 -71
- package/docs/lib/psd/image.coffee.html +0 -94
- package/docs/lib/psd/image_export.coffee.html +0 -5
- package/docs/lib/psd/image_exports/png.coffee.html +0 -19
- package/docs/lib/psd/image_format.coffee.html +0 -8
- package/docs/lib/psd/image_formats/layer_raw.coffee.html +0 -9
- package/docs/lib/psd/image_formats/layer_rle.coffee.html +0 -10
- package/docs/lib/psd/image_formats/raw.coffee.html +0 -6
- package/docs/lib/psd/image_formats/rle.coffee.html +0 -37
- package/docs/lib/psd/image_mode.coffee.html +0 -7
- package/docs/lib/psd/image_modes/cmyk.coffee.html +0 -36
- package/docs/lib/psd/image_modes/greyscale.coffee.html +0 -17
- package/docs/lib/psd/image_modes/rgb.coffee.html +0 -32
- package/docs/lib/psd/init.coffee.html +0 -22
- package/docs/lib/psd/layer/blend_modes.coffee.html +0 -22
- package/docs/lib/psd/layer/blending_ranges.coffee.html +0 -29
- package/docs/lib/psd/layer/channel_image.coffee.html +0 -13
- package/docs/lib/psd/layer/helpers.coffee.html +0 -19
- package/docs/lib/psd/layer/info.coffee.html +0 -69
- package/docs/lib/psd/layer/mask.coffee.html +0 -11
- package/docs/lib/psd/layer/name.coffee.html +0 -15
- package/docs/lib/psd/layer/position_channels.coffee.html +0 -24
- package/docs/lib/psd/layer.coffee.html +0 -64
- package/docs/lib/psd/layer_info/blend_clipping_elements.coffee.html +0 -11
- package/docs/lib/psd/layer_info/blend_interior_elements.coffee.html +0 -11
- package/docs/lib/psd/layer_info/fill_opacity.coffee.html +0 -10
- package/docs/lib/psd/layer_info/gradient_fill.coffee.html +0 -12
- package/docs/lib/psd/layer_info/layer_id.coffee.html +0 -10
- package/docs/lib/psd/layer_info/layer_name_source.coffee.html +0 -10
- package/docs/lib/psd/layer_info/legacy_typetool.coffee.html +0 -82
- package/docs/lib/psd/layer_info/locked.coffee.html +0 -24
- package/docs/lib/psd/layer_info/metadata.coffee.html +0 -30
- package/docs/lib/psd/layer_info/nested_section_divider.coffee.html +0 -23
- package/docs/lib/psd/layer_info/object_effects.coffee.html +0 -12
- package/docs/lib/psd/layer_info/section_divider.coffee.html +0 -41
- package/docs/lib/psd/layer_info/solid_color.coffee.html +0 -24
- package/docs/lib/psd/layer_info/typetool.coffee.html +0 -121
- package/docs/lib/psd/layer_info/unicode_name.coffee.html +0 -14
- package/docs/lib/psd/layer_info/vector_mask.coffee.html +0 -37
- package/docs/lib/psd/layer_info/vector_origination.coffee.html +0 -12
- package/docs/lib/psd/layer_info/vector_stroke.coffee.html +0 -12
- package/docs/lib/psd/layer_info/vector_stroke_content.coffee.html +0 -12
- package/docs/lib/psd/layer_info.coffee.html +0 -11
- package/docs/lib/psd/layer_mask.coffee.html +0 -73
- package/docs/lib/psd/lazy_execute.coffee.html +0 -80
- package/docs/lib/psd/mask.coffee.html +0 -47
- package/docs/lib/psd/node.coffee.html +0 -100
- package/docs/lib/psd/nodes/ancestry.coffee.html +0 -47
- package/docs/lib/psd/nodes/build_preview.coffee.html +0 -6
- package/docs/lib/psd/nodes/group.coffee.html +0 -19
- package/docs/lib/psd/nodes/layer.coffee.html +0 -17
- package/docs/lib/psd/nodes/root.coffee.html +0 -61
- package/docs/lib/psd/nodes/search.coffee.html +0 -23
- package/docs/lib/psd/path_record.coffee.html +0 -73
- package/docs/lib/psd/resource.coffee.html +0 -20
- package/docs/lib/psd/resource_section.coffee.html +0 -16
- package/docs/lib/psd/resources/layer_comps.coffee.html +0 -31
- package/docs/lib/psd/resources.coffee.html +0 -44
- package/docs/lib/psd/util.coffee.html +0 -24
- package/docs/lib/psd.coffee.html +0 -85
- package/index.js +0 -2
- package/lib/psd/blend_mode.coffee +0 -78
- package/lib/psd/channel_image.coffee +0 -87
- package/lib/psd/color.coffee +0 -10
- package/lib/psd/descriptor.coffee +0 -189
- package/lib/psd/file.coffee +0 -88
- package/lib/psd/header.coffee +0 -83
- package/lib/psd/image.coffee +0 -107
- package/lib/psd/image_export.coffee +0 -2
- package/lib/psd/image_exports/png.coffee +0 -28
- package/lib/psd/image_format.coffee +0 -5
- package/lib/psd/image_formats/layer_raw.coffee +0 -6
- package/lib/psd/image_formats/layer_rle.coffee +0 -7
- package/lib/psd/image_formats/raw.coffee +0 -3
- package/lib/psd/image_formats/rle.coffee +0 -38
- package/lib/psd/image_mode.coffee +0 -4
- package/lib/psd/image_modes/cmyk.coffee +0 -36
- package/lib/psd/image_modes/greyscale.coffee +0 -14
- package/lib/psd/image_modes/rgb.coffee +0 -39
- package/lib/psd/init.coffee +0 -21
- package/lib/psd/layer/blend_modes.coffee +0 -20
- package/lib/psd/layer/blending_ranges.coffee +0 -28
- package/lib/psd/layer/channel_image.coffee +0 -10
- package/lib/psd/layer/helpers.coffee +0 -16
- package/lib/psd/layer/info.coffee +0 -76
- package/lib/psd/layer/mask.coffee +0 -8
- package/lib/psd/layer/name.coffee +0 -12
- package/lib/psd/layer/position_channels.coffee +0 -22
- package/lib/psd/layer.coffee +0 -64
- package/lib/psd/layer_info/artboard.coffee +0 -16
- package/lib/psd/layer_info/blend_clipping_elements.coffee +0 -8
- package/lib/psd/layer_info/blend_interior_elements.coffee +0 -8
- package/lib/psd/layer_info/fill_opacity.coffee +0 -7
- package/lib/psd/layer_info/gradient_fill.coffee +0 -9
- package/lib/psd/layer_info/layer_id.coffee +0 -7
- package/lib/psd/layer_info/layer_name_source.coffee +0 -7
- package/lib/psd/layer_info/legacy_typetool.coffee +0 -80
- package/lib/psd/layer_info/locked.coffee +0 -21
- package/lib/psd/layer_info/metadata.coffee +0 -27
- package/lib/psd/layer_info/nested_section_divider.coffee +0 -22
- package/lib/psd/layer_info/object_effects.coffee +0 -9
- package/lib/psd/layer_info/section_divider.coffee +0 -38
- package/lib/psd/layer_info/solid_color.coffee +0 -21
- package/lib/psd/layer_info/typetool.coffee +0 -172
- package/lib/psd/layer_info/unicode_name.coffee +0 -11
- package/lib/psd/layer_info/vector_mask.coffee +0 -35
- package/lib/psd/layer_info/vector_origination.coffee +0 -9
- package/lib/psd/layer_info/vector_stroke.coffee +0 -9
- package/lib/psd/layer_info/vector_stroke_content.coffee +0 -9
- package/lib/psd/layer_info.coffee +0 -8
- package/lib/psd/layer_mask.coffee +0 -73
- package/lib/psd/lazy_execute.coffee +0 -86
- package/lib/psd/mask.coffee +0 -52
- package/lib/psd/node.coffee +0 -150
- package/lib/psd/nodes/ancestry.coffee +0 -44
- package/lib/psd/nodes/build_preview.coffee +0 -3
- package/lib/psd/nodes/group.coffee +0 -16
- package/lib/psd/nodes/layer.coffee +0 -14
- package/lib/psd/nodes/root.coffee +0 -59
- package/lib/psd/nodes/search.coffee +0 -20
- package/lib/psd/path_record.coffee +0 -72
- package/lib/psd/resource.coffee +0 -17
- package/lib/psd/resource_section.coffee +0 -16
- package/lib/psd/resources/guides.coffee +0 -25
- package/lib/psd/resources/layer_comps.coffee +0 -28
- package/lib/psd/resources/layer_links.coffee +0 -17
- package/lib/psd/resources/resolution_info.coffee +0 -26
- package/lib/psd/resources.coffee +0 -41
- package/lib/psd/util.coffee +0 -23
- package/lib/psd.coffee +0 -92
- package/output/Logo_Glyph.png +0 -0
- package/output/Make a change and save..png +0 -0
- package/output/Matte.png +0 -0
- package/webpack.config.js +0 -23
package/src/reader.ts
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
1
|
+
const utf16 = new TextDecoder('utf-16be');
|
|
2
|
+
|
|
3
|
+
/** A big-endian cursor over the PSD bytes. Cheap to create, so lazy sections each get their own. */
|
|
4
|
+
export class Reader {
|
|
5
|
+
readonly bytes: Uint8Array;
|
|
6
|
+
readonly view: DataView;
|
|
7
|
+
pos: number;
|
|
8
|
+
|
|
9
|
+
constructor(bytes: Uint8Array, pos = 0) {
|
|
10
|
+
this.bytes = bytes;
|
|
11
|
+
this.view = new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength);
|
|
12
|
+
this.pos = pos;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/** A new reader over the same bytes, positioned at `pos`. */
|
|
16
|
+
at(pos: number): Reader {
|
|
17
|
+
return new Reader(this.bytes, pos);
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
seek(pos: number): void {
|
|
21
|
+
this.pos = pos;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
skip(n: number): void {
|
|
25
|
+
this.pos += n;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
uint8(): number {
|
|
29
|
+
return this.view.getUint8(this.pos++);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
int8(): number {
|
|
33
|
+
return this.view.getInt8(this.pos++);
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
bool(): boolean {
|
|
37
|
+
return this.uint8() !== 0;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
int16(): number {
|
|
41
|
+
const v = this.view.getInt16(this.pos);
|
|
42
|
+
this.pos += 2;
|
|
43
|
+
return v;
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
uint16(): number {
|
|
47
|
+
const v = this.view.getUint16(this.pos);
|
|
48
|
+
this.pos += 2;
|
|
49
|
+
return v;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
int32(): number {
|
|
53
|
+
const v = this.view.getInt32(this.pos);
|
|
54
|
+
this.pos += 4;
|
|
55
|
+
return v;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
uint32(): number {
|
|
59
|
+
const v = this.view.getUint32(this.pos);
|
|
60
|
+
this.pos += 4;
|
|
61
|
+
return v;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
/** 64-bit integer as a Number. Exact up to 2^53, which covers any real file offset. */
|
|
65
|
+
int64(): number {
|
|
66
|
+
const v = Number(this.view.getBigInt64(this.pos));
|
|
67
|
+
this.pos += 8;
|
|
68
|
+
return v;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
float32(): number {
|
|
72
|
+
const v = this.view.getFloat32(this.pos);
|
|
73
|
+
this.pos += 4;
|
|
74
|
+
return v;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
float64(): number {
|
|
78
|
+
const v = this.view.getFloat64(this.pos);
|
|
79
|
+
this.pos += 8;
|
|
80
|
+
return v;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/** A zero-copy view of the next `n` bytes. */
|
|
84
|
+
bytesView(n: number): Uint8Array {
|
|
85
|
+
if (this.pos + n > this.bytes.length) throw new RangeError(`Read of ${n} bytes at ${this.pos} is out of bounds`);
|
|
86
|
+
const v = this.bytes.subarray(this.pos, this.pos + n);
|
|
87
|
+
this.pos += n;
|
|
88
|
+
return v;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/** A single-byte (Latin-1) string with NUL characters removed. */
|
|
92
|
+
ascii(n: number): string {
|
|
93
|
+
return String.fromCharCode(...this.bytesView(n)).replaceAll('\0', '');
|
|
94
|
+
}
|
|
95
|
+
|
|
96
|
+
/** A Pascal string: a length byte followed by that many characters. */
|
|
97
|
+
pascal(): string {
|
|
98
|
+
return this.ascii(this.uint8());
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/** A UTF-16BE string of `length` code units, read from a leading uint32 when omitted. */
|
|
102
|
+
unicode(length = this.uint32()): string {
|
|
103
|
+
return utf16.decode(this.bytesView(length * 2)).replaceAll('\0', '');
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** Signed 8.24 fixed-point number used by path records. */
|
|
107
|
+
pathNumber(): number {
|
|
108
|
+
return this.int32() / (1 << 24);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** A color space ID followed by four 16-bit components (reduced to 8 bits). */
|
|
112
|
+
spaceColor(): SpaceColor {
|
|
113
|
+
const colorSpace = this.int16();
|
|
114
|
+
const components = [0, 0, 0, 0].map(() => this.uint16() >> 8);
|
|
115
|
+
return { colorSpace, components };
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
export interface SpaceColor {
|
|
120
|
+
colorSpace: number;
|
|
121
|
+
components: number[];
|
|
122
|
+
}
|
|
123
|
+
|
|
124
|
+
/** Rounds up to an even number. */
|
|
125
|
+
export const pad2 = (n: number) => (n + 1) & ~1;
|
package/src/resources.ts
ADDED
|
@@ -0,0 +1,273 @@
|
|
|
1
|
+
import { readDescriptor, type Descriptor } from './descriptor.ts';
|
|
2
|
+
import { readPathRecords, type PathRecord } from './path-record.ts';
|
|
3
|
+
import { pad2, Reader } from './reader.ts';
|
|
4
|
+
|
|
5
|
+
/** An image resource block. `offset` points at its data inside the file. */
|
|
6
|
+
export interface ResourceBlock {
|
|
7
|
+
id: number;
|
|
8
|
+
name: string;
|
|
9
|
+
offset: number;
|
|
10
|
+
size: number;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export interface LayerComp {
|
|
14
|
+
id: number;
|
|
15
|
+
name: string;
|
|
16
|
+
/** Bit flags: 1 = visibility, 2 = position, 4 = appearance. */
|
|
17
|
+
capturedInfo: number;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export interface Guide {
|
|
21
|
+
/** Position in pixels. */
|
|
22
|
+
location: number;
|
|
23
|
+
direction: 'horizontal' | 'vertical';
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export interface ResolutionInfo {
|
|
27
|
+
/** Horizontal resolution in pixels per inch. `hResUnitName` is only the unit Photoshop displays it in. */
|
|
28
|
+
hRes: number;
|
|
29
|
+
hResUnit: number;
|
|
30
|
+
hResUnitName: string;
|
|
31
|
+
widthUnit: number;
|
|
32
|
+
widthUnitName: string;
|
|
33
|
+
vRes: number;
|
|
34
|
+
vResUnit: number;
|
|
35
|
+
vResUnitName: string;
|
|
36
|
+
heightUnit: number;
|
|
37
|
+
heightUnitName: string;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export interface Bounds {
|
|
41
|
+
top: number;
|
|
42
|
+
left: number;
|
|
43
|
+
bottom: number;
|
|
44
|
+
right: number;
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* A slice. Version 6 resources store `origin`, `type` and the alignments as numeric codes;
|
|
49
|
+
* versions 7+ store enum names such as `userGenerated` or `Img `.
|
|
50
|
+
*/
|
|
51
|
+
export interface Slice {
|
|
52
|
+
id: number;
|
|
53
|
+
groupId: number;
|
|
54
|
+
origin: number | string;
|
|
55
|
+
/** Only stored by version 6 resources. */
|
|
56
|
+
associatedLayerId: number | null;
|
|
57
|
+
name: string;
|
|
58
|
+
type: number | string;
|
|
59
|
+
bounds: Bounds;
|
|
60
|
+
url: string;
|
|
61
|
+
target: string;
|
|
62
|
+
message: string;
|
|
63
|
+
alt: string;
|
|
64
|
+
cellTextIsHtml: boolean;
|
|
65
|
+
cellText: string;
|
|
66
|
+
horizontalAlignment: number | string;
|
|
67
|
+
verticalAlignment: number | string;
|
|
68
|
+
/** RGBA. Only stored by version 6 resources. */
|
|
69
|
+
color: [number, number, number, number] | null;
|
|
70
|
+
outset?: Bounds;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
export interface Slices {
|
|
74
|
+
version: number;
|
|
75
|
+
name: string;
|
|
76
|
+
bounds: Bounds;
|
|
77
|
+
slices: Slice[];
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
export interface SavedPath {
|
|
81
|
+
id: number;
|
|
82
|
+
name: string;
|
|
83
|
+
records: PathRecord[];
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
const RES_UNITS = ['pixel/inch', 'pixel/cm'];
|
|
87
|
+
const UNITS = ['in', 'cm', 'pt', 'picas', 'columns'];
|
|
88
|
+
const unitName = (names: string[], unit: number) => names[unit - 1] ?? 'unknown';
|
|
89
|
+
|
|
90
|
+
/** The image resources section. Blocks are indexed up front and decoded on first access. */
|
|
91
|
+
export class Resources {
|
|
92
|
+
/** Every resource block in the file, by ID. */
|
|
93
|
+
readonly blocks = new Map<number, ResourceBlock>();
|
|
94
|
+
readonly #reader: Reader;
|
|
95
|
+
readonly #cache = new Map<string, unknown>();
|
|
96
|
+
|
|
97
|
+
constructor(r: Reader, end: number) {
|
|
98
|
+
this.#reader = r;
|
|
99
|
+
while (r.pos < end) {
|
|
100
|
+
r.skip(4); // signature, 8BIM
|
|
101
|
+
const id = r.uint16();
|
|
102
|
+
const nameLength = pad2(r.uint8() + 1) - 1;
|
|
103
|
+
const name = r.ascii(nameLength);
|
|
104
|
+
const size = pad2(r.uint32());
|
|
105
|
+
this.blocks.set(id, { id, name, offset: r.pos, size });
|
|
106
|
+
r.skip(size);
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/** Raw bytes of a resource block. */
|
|
111
|
+
bytes(id: number): Uint8Array | undefined {
|
|
112
|
+
const block = this.blocks.get(id);
|
|
113
|
+
return block && this.#reader.bytes.subarray(block.offset, block.offset + block.size);
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
#parse<T>(id: number, parse: (r: Reader, block: ResourceBlock) => T): T | undefined {
|
|
117
|
+
const key = String(id);
|
|
118
|
+
if (!this.#cache.has(key)) {
|
|
119
|
+
const block = this.blocks.get(id);
|
|
120
|
+
this.#cache.set(key, block && parse(this.#reader.at(block.offset), block));
|
|
121
|
+
}
|
|
122
|
+
return this.#cache.get(key) as T | undefined;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/** Resource 1065. */
|
|
126
|
+
get layerComps(): LayerComp[] {
|
|
127
|
+
return (
|
|
128
|
+
this.#parse(1065, (r) => {
|
|
129
|
+
r.skip(4); // descriptor version
|
|
130
|
+
const list: Descriptor[] = readDescriptor(r).list ?? [];
|
|
131
|
+
return list.map((c) => ({ id: c.compID, name: c['Nm '], capturedInfo: c.capturedInfo }));
|
|
132
|
+
}) ?? []
|
|
133
|
+
);
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
/** Resource 1032. */
|
|
137
|
+
get guides(): Guide[] {
|
|
138
|
+
return (
|
|
139
|
+
this.#parse(1032, (r) => {
|
|
140
|
+
r.skip(12); // version + future grid info
|
|
141
|
+
const count = r.int32();
|
|
142
|
+
return Array.from({ length: count }, (): Guide => ({
|
|
143
|
+
location: r.int32() / 32,
|
|
144
|
+
direction: r.uint8() === 0 ? 'vertical' : 'horizontal',
|
|
145
|
+
}));
|
|
146
|
+
}) ?? []
|
|
147
|
+
);
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/** Resource 1005. */
|
|
151
|
+
get resolutionInfo(): ResolutionInfo | undefined {
|
|
152
|
+
return this.#parse(1005, (r) => {
|
|
153
|
+
const hRes = r.uint32() / 65536;
|
|
154
|
+
const hResUnit = r.uint16();
|
|
155
|
+
const widthUnit = r.uint16();
|
|
156
|
+
const vRes = r.uint32() / 65536;
|
|
157
|
+
const vResUnit = r.uint16();
|
|
158
|
+
const heightUnit = r.uint16();
|
|
159
|
+
return {
|
|
160
|
+
hRes,
|
|
161
|
+
hResUnit,
|
|
162
|
+
hResUnitName: unitName(RES_UNITS, hResUnit),
|
|
163
|
+
widthUnit,
|
|
164
|
+
widthUnitName: unitName(UNITS, widthUnit),
|
|
165
|
+
vRes,
|
|
166
|
+
vResUnit,
|
|
167
|
+
vResUnitName: unitName(RES_UNITS, vResUnit),
|
|
168
|
+
heightUnit,
|
|
169
|
+
heightUnitName: unitName(UNITS, heightUnit),
|
|
170
|
+
};
|
|
171
|
+
});
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** Resource 1050, normalized to the same shape for every version. */
|
|
175
|
+
get slices(): Slices | undefined {
|
|
176
|
+
return this.#parse(1050, readSlices);
|
|
177
|
+
}
|
|
178
|
+
|
|
179
|
+
/** Resource 1060, the raw XMP packet. */
|
|
180
|
+
get xmpMetadata(): string | undefined {
|
|
181
|
+
return this.#parse(1060, (r, block) => new TextDecoder().decode(r.bytesView(block.size)).replace(/\0+$/, ''));
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** Resource 1025. */
|
|
185
|
+
get workPath(): PathRecord[] | undefined {
|
|
186
|
+
return this.#parse(1025, (r, block) => readPathRecords(r, Math.floor(block.size / 26)));
|
|
187
|
+
}
|
|
188
|
+
|
|
189
|
+
/** Resources 2000-2997. */
|
|
190
|
+
get savedPaths(): SavedPath[] {
|
|
191
|
+
return [...this.blocks.values()]
|
|
192
|
+
.filter((b) => b.id >= 2000 && b.id <= 2997)
|
|
193
|
+
.map((b) => ({ id: b.id, name: b.name, records: this.#parse(b.id, (r) => readPathRecords(r, Math.floor(b.size / 26)))! }));
|
|
194
|
+
}
|
|
195
|
+
|
|
196
|
+
/** Resource 1046: the number of colors in an indexed image's color table. */
|
|
197
|
+
get indexedColorCount(): number | undefined {
|
|
198
|
+
return this.#parse(1046, (r) => r.uint16());
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Resource 1047: the color table index that is transparent in an indexed image. */
|
|
202
|
+
get transparencyIndex(): number | undefined {
|
|
203
|
+
return this.#parse(1047, (r) => r.uint16());
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/** Resource 1026: the group ID of each layer, ordered top to bottom like `psd.layers`. */
|
|
207
|
+
get layerGroups(): number[] | undefined {
|
|
208
|
+
return this.#parse(1026, (r, block) => Array.from({ length: block.size / 2 }, () => r.uint16()).reverse());
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
|
|
212
|
+
function readSlices(r: Reader): Slices {
|
|
213
|
+
const version = r.int32();
|
|
214
|
+
if (version === 6) return readLegacySlices(r);
|
|
215
|
+
if (version !== 7 && version !== 8) throw new Error(`Unsupported slices resource version ${version}`);
|
|
216
|
+
|
|
217
|
+
r.skip(4); // descriptor version
|
|
218
|
+
const d = readDescriptor(r);
|
|
219
|
+
const bounds = (b: Descriptor): Bounds => ({ top: b['Top '], left: b['Left'], bottom: b['Btom'], right: b['Rght'] });
|
|
220
|
+
return {
|
|
221
|
+
version,
|
|
222
|
+
name: d.baseName,
|
|
223
|
+
bounds: bounds(d.bounds),
|
|
224
|
+
slices: (d.slices as Descriptor[]).map((s) => ({
|
|
225
|
+
id: s.sliceID,
|
|
226
|
+
groupId: s.groupID,
|
|
227
|
+
origin: s.origin.value,
|
|
228
|
+
associatedLayerId: null,
|
|
229
|
+
name: s['Nm '] ?? '',
|
|
230
|
+
type: s.Type.value,
|
|
231
|
+
bounds: bounds(s.bounds),
|
|
232
|
+
url: s.url ?? '',
|
|
233
|
+
target: s.null ?? '',
|
|
234
|
+
message: s.Msge ?? '',
|
|
235
|
+
alt: s.altTag ?? '',
|
|
236
|
+
cellTextIsHtml: s.cellTextIsHTML ?? false,
|
|
237
|
+
cellText: s.cellText ?? '',
|
|
238
|
+
horizontalAlignment: s.horzAlign.value,
|
|
239
|
+
verticalAlignment: s.vertAlign.value,
|
|
240
|
+
color: null,
|
|
241
|
+
outset: { top: s.topOutset, left: s.leftOutset, bottom: s.bottomOutset, right: s.rightOutset },
|
|
242
|
+
})),
|
|
243
|
+
};
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
function readLegacySlices(r: Reader): Slices {
|
|
247
|
+
const bounds = { top: r.int32(), left: r.int32(), bottom: r.int32(), right: r.int32() };
|
|
248
|
+
const name = r.unicode();
|
|
249
|
+
const count = r.int32();
|
|
250
|
+
const slices = Array.from({ length: count }, (): Slice => {
|
|
251
|
+
const id = r.int32();
|
|
252
|
+
const groupId = r.int32();
|
|
253
|
+
const origin = r.int32();
|
|
254
|
+
const associatedLayerId = origin === 1 ? r.int32() : null;
|
|
255
|
+
const name = r.unicode();
|
|
256
|
+
const type = r.int32();
|
|
257
|
+
const bounds = { left: r.int32(), top: r.int32(), right: r.int32(), bottom: r.int32() };
|
|
258
|
+
const url = r.unicode();
|
|
259
|
+
const target = r.unicode();
|
|
260
|
+
const message = r.unicode();
|
|
261
|
+
const alt = r.unicode();
|
|
262
|
+
const cellTextIsHtml = r.bool();
|
|
263
|
+
const cellText = r.unicode();
|
|
264
|
+
const horizontalAlignment = r.int32();
|
|
265
|
+
const verticalAlignment = r.int32();
|
|
266
|
+
const [a, red, g, b] = [r.uint8(), r.uint8(), r.uint8(), r.uint8()];
|
|
267
|
+
return {
|
|
268
|
+
id, groupId, origin, associatedLayerId, name, type, bounds, url, target, message, alt,
|
|
269
|
+
cellTextIsHtml, cellText, horizontalAlignment, verticalAlignment, color: [red, g, b, a],
|
|
270
|
+
};
|
|
271
|
+
});
|
|
272
|
+
return { version: 6, name, bounds, slices };
|
|
273
|
+
}
|
package/src/type-tool.ts
ADDED
|
@@ -0,0 +1,270 @@
|
|
|
1
|
+
import { readDescriptor, type Descriptor } from './descriptor.ts';
|
|
2
|
+
import { parseEngineData, type EngineDict } from './engine-data.ts';
|
|
3
|
+
import type { Reader } from './reader.ts';
|
|
4
|
+
import type { Bounds } from './resources.ts';
|
|
5
|
+
|
|
6
|
+
export interface Transform {
|
|
7
|
+
xx: number;
|
|
8
|
+
xy: number;
|
|
9
|
+
yx: number;
|
|
10
|
+
yy: number;
|
|
11
|
+
tx: number;
|
|
12
|
+
ty: number;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export type RGBA = [number, number, number, number];
|
|
16
|
+
|
|
17
|
+
/** A run of characters that share one style. */
|
|
18
|
+
export interface TextRun {
|
|
19
|
+
/** Number of characters in the run. */
|
|
20
|
+
length: number;
|
|
21
|
+
font: string | undefined;
|
|
22
|
+
size: number | undefined;
|
|
23
|
+
color: RGBA | undefined;
|
|
24
|
+
leading: number | 'auto';
|
|
25
|
+
fauxBold: boolean;
|
|
26
|
+
fauxItalic: boolean;
|
|
27
|
+
underline: boolean;
|
|
28
|
+
strikethrough: boolean;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
const ALIGNMENTS = ['left', 'right', 'center', 'justify'] as const;
|
|
32
|
+
export type TextAlignment = (typeof ALIGNMENTS)[number];
|
|
33
|
+
|
|
34
|
+
const readTransform = (r: Reader): Transform => ({
|
|
35
|
+
xx: r.float64(),
|
|
36
|
+
xy: r.float64(),
|
|
37
|
+
yx: r.float64(),
|
|
38
|
+
yy: r.float64(),
|
|
39
|
+
tx: r.float64(),
|
|
40
|
+
ty: r.float64(),
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
/** Engine data colors are ARGB floats from 0 to 1. */
|
|
44
|
+
const toRGBA = (values: number[]): RGBA => {
|
|
45
|
+
const [a, r, g, b] = values.map((v) => Math.round(v * 255)) as RGBA;
|
|
46
|
+
return [r, g, b, a];
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
const uniq = <T>(values: T[]): T[] => [...new Map(values.map((v) => [JSON.stringify(v), v])).values()];
|
|
50
|
+
|
|
51
|
+
/** A text layer (the `TySh` block). */
|
|
52
|
+
export class TypeTool {
|
|
53
|
+
readonly transform: Transform;
|
|
54
|
+
/** The raw text descriptor. */
|
|
55
|
+
readonly textData: Descriptor;
|
|
56
|
+
/** The raw warp descriptor. */
|
|
57
|
+
readonly warpData: Descriptor;
|
|
58
|
+
#engineData: EngineDict | undefined;
|
|
59
|
+
|
|
60
|
+
constructor(r: Reader) {
|
|
61
|
+
r.skip(2); // version
|
|
62
|
+
this.transform = readTransform(r);
|
|
63
|
+
r.skip(6); // text version + descriptor version
|
|
64
|
+
this.textData = readDescriptor(r);
|
|
65
|
+
r.skip(6); // warp version + descriptor version
|
|
66
|
+
this.warpData = readDescriptor(r);
|
|
67
|
+
// The block ends with left, top, right and bottom fields that Photoshop leaves as 0.
|
|
68
|
+
}
|
|
69
|
+
|
|
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
|
+
*/
|
|
102
|
+
get text(): string {
|
|
103
|
+
return this.textData['Txt '];
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
/** The parsed EngineData, which holds all styling. Parsed on first access. */
|
|
107
|
+
get engineData(): EngineDict {
|
|
108
|
+
return (this.#engineData ??= parseEngineData(this.textData.EngineData));
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
/** Every font referenced by the text. */
|
|
112
|
+
get fonts(): string[] {
|
|
113
|
+
return this.engineData.ResourceDict.FontSet.map((f: EngineDict) => f.Name);
|
|
114
|
+
}
|
|
115
|
+
|
|
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
|
+
*/
|
|
120
|
+
get runs(): TextRun[] {
|
|
121
|
+
const styleRun = this.engineData.EngineDict.StyleRun;
|
|
122
|
+
const lengths: number[] = [...styleRun.RunLengthArray];
|
|
123
|
+
// EngineData counts a trailing return that the text value omits.
|
|
124
|
+
const total = lengths.reduce((sum, n) => sum + n, 0);
|
|
125
|
+
if (total - this.text.length === 1) lengths[lengths.length - 1]! -= 1;
|
|
126
|
+
|
|
127
|
+
const { FontSet, StyleSheetSet, TheNormalStyleSheet } = this.engineData.ResourceDict;
|
|
128
|
+
const defaults = StyleSheetSet[TheNormalStyleSheet].StyleSheetData;
|
|
129
|
+
return styleRun.RunArray.map((run: EngineDict, i: number): TextRun => {
|
|
130
|
+
const s = { ...defaults, ...run.StyleSheet.StyleSheetData };
|
|
131
|
+
return {
|
|
132
|
+
length: lengths[i] ?? 0,
|
|
133
|
+
font: s.Font === undefined ? undefined : FontSet[s.Font]?.Name,
|
|
134
|
+
size: s.FontSize,
|
|
135
|
+
color: s.FillColor ? toRGBA(s.FillColor.Values) : undefined,
|
|
136
|
+
leading: s.AutoLeading === false && s.Leading !== undefined ? s.Leading : 'auto',
|
|
137
|
+
fauxBold: s.FauxBold ?? false,
|
|
138
|
+
fauxItalic: s.FauxItalic ?? false,
|
|
139
|
+
underline: s.Underline ?? false,
|
|
140
|
+
strikethrough: s.Strikethrough ?? false,
|
|
141
|
+
};
|
|
142
|
+
});
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
/** Unique font sizes as stored, in pixels before `transform` scales the text. See `pointSizes()`. */
|
|
146
|
+
get sizes(): number[] {
|
|
147
|
+
return uniq(this.runs.flatMap((r) => (r.size === undefined ? [] : [r.size])));
|
|
148
|
+
}
|
|
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
|
+
|
|
164
|
+
/** Unique text colors (RGBA). */
|
|
165
|
+
get colors(): RGBA[] {
|
|
166
|
+
return uniq(this.runs.map((r) => r.color ?? [0, 0, 0, 255]));
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
/** Unique explicit leadings (line spacing). */
|
|
170
|
+
get leadings(): number[] {
|
|
171
|
+
return uniq(this.runs.flatMap((r) => (r.leading === 'auto' ? [] : [r.leading])));
|
|
172
|
+
}
|
|
173
|
+
|
|
174
|
+
/** The alignment of each paragraph. */
|
|
175
|
+
get alignment(): TextAlignment[] {
|
|
176
|
+
const { ParagraphSheetSet, TheNormalParagraphSheet } = this.engineData.ResourceDict;
|
|
177
|
+
const defaults = ParagraphSheetSet[TheNormalParagraphSheet].Properties;
|
|
178
|
+
return this.engineData.EngineDict.ParagraphRun.RunArray.map(
|
|
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)]!,
|
|
181
|
+
);
|
|
182
|
+
}
|
|
183
|
+
|
|
184
|
+
/** CSS for the first style run. Fonts may include Photoshop-internal names. */
|
|
185
|
+
toCSS(): string {
|
|
186
|
+
const definition: Record<string, string | undefined> = {
|
|
187
|
+
'font-family': this.fonts.join(', '),
|
|
188
|
+
'font-size': this.sizes[0] === undefined ? undefined : `${this.sizes[0] * this.#scale}px`,
|
|
189
|
+
color: `rgba(${this.colors[0]!.join(', ')})`,
|
|
190
|
+
'text-align': this.alignment[0],
|
|
191
|
+
};
|
|
192
|
+
return Object.entries(definition)
|
|
193
|
+
.filter(([, v]) => v !== undefined)
|
|
194
|
+
.map(([k, v]) => `${k}: ${v};`)
|
|
195
|
+
.join('\n');
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
toJSON() {
|
|
199
|
+
return {
|
|
200
|
+
text: this.text,
|
|
201
|
+
font: {
|
|
202
|
+
name: this.fonts[0],
|
|
203
|
+
names: this.fonts,
|
|
204
|
+
sizes: this.sizes,
|
|
205
|
+
colors: this.colors,
|
|
206
|
+
alignment: this.alignment,
|
|
207
|
+
leadings: this.leadings,
|
|
208
|
+
},
|
|
209
|
+
runs: this.runs,
|
|
210
|
+
left: this.left,
|
|
211
|
+
top: this.top,
|
|
212
|
+
right: this.right,
|
|
213
|
+
bottom: this.bottom,
|
|
214
|
+
boundingBox: this.boundingBox,
|
|
215
|
+
transform: this.transform,
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** The pre-Photoshop 6 text block (`tySh`). Font data only; the text itself isn't stored here. */
|
|
221
|
+
export function readLegacyTypeTool(r: Reader) {
|
|
222
|
+
r.skip(2); // version
|
|
223
|
+
const transform = readTransform(r);
|
|
224
|
+
r.skip(2); // font version
|
|
225
|
+
const faces = Array.from({ length: r.int16() }, () => ({
|
|
226
|
+
mark: r.int16(),
|
|
227
|
+
fontType: r.int32(),
|
|
228
|
+
fontName: r.pascal(),
|
|
229
|
+
fontFamilyName: r.pascal(),
|
|
230
|
+
fontStyleName: r.pascal(),
|
|
231
|
+
script: r.int16(),
|
|
232
|
+
vector: Array.from({ length: r.int32() }, () => r.int32()),
|
|
233
|
+
}));
|
|
234
|
+
const styles = Array.from({ length: r.int16() }, () => {
|
|
235
|
+
const style = {
|
|
236
|
+
mark: r.int16(),
|
|
237
|
+
faceMark: r.int16(),
|
|
238
|
+
size: r.int32(),
|
|
239
|
+
tracking: r.int32(),
|
|
240
|
+
kerning: r.int32(),
|
|
241
|
+
leading: r.int32(),
|
|
242
|
+
baseShift: r.int32(),
|
|
243
|
+
autoKern: r.bool(),
|
|
244
|
+
rotate: false,
|
|
245
|
+
};
|
|
246
|
+
r.skip(1);
|
|
247
|
+
style.rotate = r.bool();
|
|
248
|
+
return style;
|
|
249
|
+
});
|
|
250
|
+
const type = r.int16();
|
|
251
|
+
const scalingFactor = r.int32();
|
|
252
|
+
const characterCount = r.int32();
|
|
253
|
+
const horzPlace = r.int32();
|
|
254
|
+
const vertPlace = r.int32();
|
|
255
|
+
const selectStart = r.int32();
|
|
256
|
+
const selectEnd = r.int32();
|
|
257
|
+
const lines = Array.from({ length: r.int16() }, () => ({
|
|
258
|
+
charCount: r.int32(),
|
|
259
|
+
orientation: r.int16(),
|
|
260
|
+
alignment: r.int16(),
|
|
261
|
+
actualChar: r.int16(),
|
|
262
|
+
style: r.int16(),
|
|
263
|
+
}));
|
|
264
|
+
const color = r.spaceColor();
|
|
265
|
+
const antialias = r.bool();
|
|
266
|
+
return {
|
|
267
|
+
transform, faces, styles, type, scalingFactor, characterCount, horzPlace, vertPlace,
|
|
268
|
+
selectStart, selectEnd, lines, color, antialias,
|
|
269
|
+
};
|
|
270
|
+
}
|