psd 4.0.0 → 4.2.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 +146 -7
- package/dist/descriptor.js +6 -2
- package/dist/descriptor.js.map +1 -1
- package/dist/image.d.ts +28 -4
- package/dist/image.d.ts.map +1 -1
- package/dist/image.js +36 -22
- package/dist/image.js.map +1 -1
- package/dist/index.d.ts +9 -6
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +3 -1
- package/dist/index.js.map +1 -1
- package/dist/layer-info.d.ts +53 -10
- package/dist/layer-info.d.ts.map +1 -1
- package/dist/layer-info.js +34 -8
- package/dist/layer-info.js.map +1 -1
- package/dist/layer-styles.d.ts +11 -1
- package/dist/layer-styles.d.ts.map +1 -1
- package/dist/layer-styles.js +5 -5
- package/dist/layer-styles.js.map +1 -1
- package/dist/layer.d.ts +32 -0
- package/dist/layer.d.ts.map +1 -1
- package/dist/layer.js +68 -6
- package/dist/layer.js.map +1 -1
- package/dist/linked-files.d.ts +32 -0
- package/dist/linked-files.d.ts.map +1 -0
- package/dist/linked-files.js +45 -0
- package/dist/linked-files.js.map +1 -0
- package/dist/nodes.d.ts +21 -9
- package/dist/nodes.d.ts.map +1 -1
- package/dist/nodes.js +16 -6
- package/dist/nodes.js.map +1 -1
- package/dist/path-record.d.ts +28 -0
- package/dist/path-record.d.ts.map +1 -1
- package/dist/path-record.js +54 -1
- package/dist/path-record.js.map +1 -1
- package/dist/patterns.d.ts +24 -0
- package/dist/patterns.d.ts.map +1 -0
- package/dist/patterns.js +108 -0
- package/dist/patterns.js.map +1 -0
- package/dist/png.d.ts +12 -4
- package/dist/png.d.ts.map +1 -1
- package/dist/png.js +68 -31
- package/dist/png.js.map +1 -1
- package/dist/psd.d.ts +13 -2
- package/dist/psd.d.ts.map +1 -1
- package/dist/psd.js +23 -1
- package/dist/psd.js.map +1 -1
- package/dist/resources.d.ts +7 -1
- package/dist/resources.d.ts.map +1 -1
- package/dist/resources.js +18 -0
- package/dist/resources.js.map +1 -1
- package/dist/shapes.d.ts +94 -0
- package/dist/shapes.d.ts.map +1 -0
- package/dist/shapes.js +76 -0
- package/dist/shapes.js.map +1 -0
- package/dist/type-tool.d.ts +60 -11
- package/dist/type-tool.d.ts.map +1 -1
- package/dist/type-tool.js +87 -17
- package/dist/type-tool.js.map +1 -1
- package/package.json +4 -2
- package/src/descriptor.ts +6 -2
- package/src/image.ts +56 -26
- package/src/index.ts +17 -5
- package/src/layer-info.ts +76 -8
- package/src/layer-styles.ts +8 -6
- package/src/layer.ts +79 -6
- package/src/linked-files.ts +66 -0
- package/src/nodes.ts +20 -6
- package/src/path-record.ts +63 -2
- package/src/patterns.ts +113 -0
- package/src/png.ts +76 -31
- package/src/psd.ts +25 -1
- package/src/resources.ts +21 -1
- package/src/shapes.ts +156 -0
- package/src/type-tool.ts +106 -17
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;
|
|
@@ -25,8 +26,32 @@ export interface TextRun {
|
|
|
25
26
|
fauxItalic: boolean;
|
|
26
27
|
underline: boolean;
|
|
27
28
|
strikethrough: boolean;
|
|
29
|
+
/** Tracking (letter spacing) in 1/1000 em, as in the Character panel. Multiply by `size / 1000` for pixels. */
|
|
30
|
+
tracking: number;
|
|
31
|
+
/**
|
|
32
|
+
* `'auto'` when the font's kerning is used (Metrics in the Character panel), otherwise manual kerning
|
|
33
|
+
* in 1/1000 em. Photoshop often stores the first character as its own run with a kerning of 0.
|
|
34
|
+
*/
|
|
35
|
+
kerning: number | 'auto';
|
|
36
|
+
/** Baseline shift in pixels before `transform` scales the text, like `size`. Positive values raise the text. */
|
|
37
|
+
baselineShift: number;
|
|
38
|
+
/** Small Caps or All Caps from the Character panel. `text` keeps the case it was typed in. */
|
|
39
|
+
caps: TextCaps;
|
|
40
|
+
/** Superscript or subscript from the Character panel. */
|
|
41
|
+
baseline: TextBaseline;
|
|
28
42
|
}
|
|
29
43
|
|
|
44
|
+
const CAPS = ['normal', 'small-caps', 'all-caps'] as const;
|
|
45
|
+
export type TextCaps = (typeof CAPS)[number];
|
|
46
|
+
const BASELINES = ['normal', 'superscript', 'subscript'] as const;
|
|
47
|
+
export type TextBaseline = (typeof BASELINES)[number];
|
|
48
|
+
|
|
49
|
+
const lookup = <T>(values: readonly T[], key: string, index: number): T => {
|
|
50
|
+
const value = values[index];
|
|
51
|
+
if (value === undefined) throw new Error(`Unknown ${key} value: ${index}`);
|
|
52
|
+
return value;
|
|
53
|
+
};
|
|
54
|
+
|
|
30
55
|
const ALIGNMENTS = ['left', 'right', 'center', 'justify'] as const;
|
|
31
56
|
export type TextAlignment = (typeof ALIGNMENTS)[number];
|
|
32
57
|
|
|
@@ -54,10 +79,6 @@ export class TypeTool {
|
|
|
54
79
|
readonly textData: Descriptor;
|
|
55
80
|
/** The raw warp descriptor. */
|
|
56
81
|
readonly warpData: Descriptor;
|
|
57
|
-
readonly left: number;
|
|
58
|
-
readonly top: number;
|
|
59
|
-
readonly right: number;
|
|
60
|
-
readonly bottom: number;
|
|
61
82
|
#engineData: EngineDict | undefined;
|
|
62
83
|
|
|
63
84
|
constructor(r: Reader) {
|
|
@@ -67,13 +88,54 @@ export class TypeTool {
|
|
|
67
88
|
this.textData = readDescriptor(r);
|
|
68
89
|
r.skip(6); // warp version + descriptor version
|
|
69
90
|
this.warpData = readDescriptor(r);
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
91
|
+
// The block ends with left, top, right and bottom fields that Photoshop leaves as 0.
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The left edge of the text's frame, in pixels relative to the transform origin (`tx`, `ty`) and
|
|
96
|
+
* before the transform. The frame spans the lines around the anchor point for point text, or the box
|
|
97
|
+
* for paragraph text (it can reach beyond the box to fit the first line; see `boxBounds`).
|
|
98
|
+
* Undefined in files from older Photoshop versions.
|
|
99
|
+
*/
|
|
100
|
+
get left(): number | undefined {
|
|
101
|
+
return this.textData.bounds?.Left.value;
|
|
102
|
+
}
|
|
103
|
+
/** See `left`. */
|
|
104
|
+
get top(): number | undefined {
|
|
105
|
+
return this.textData.bounds?.['Top '].value;
|
|
106
|
+
}
|
|
107
|
+
/** See `left`. */
|
|
108
|
+
get right(): number | undefined {
|
|
109
|
+
return this.textData.bounds?.Rght.value;
|
|
110
|
+
}
|
|
111
|
+
/** See `left`. */
|
|
112
|
+
get bottom(): number | undefined {
|
|
113
|
+
return this.textData.bounds?.Btom.value;
|
|
74
114
|
}
|
|
75
115
|
|
|
76
|
-
/** The
|
|
116
|
+
/** The tight bounds of the glyphs, relative to the transform origin like `left`/`top`/`right`/`bottom`. */
|
|
117
|
+
get boundingBox(): Bounds | undefined {
|
|
118
|
+
const box = this.textData.boundingBox;
|
|
119
|
+
return box && { left: box.Left.value, top: box['Top '].value, right: box.Rght.value, bottom: box.Btom.value };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
/**
|
|
123
|
+
* The box of paragraph (box) text, exactly as Photoshop draws it, relative to the transform origin and
|
|
124
|
+
* before the transform like `left`/`top`/`right`/`bottom`. Undefined for text without a box, such as
|
|
125
|
+
* point text. Unlike the frame, it doesn't grow to fit the first line, and older Photoshop versions
|
|
126
|
+
* store it too.
|
|
127
|
+
*/
|
|
128
|
+
get boxBounds(): Bounds | undefined {
|
|
129
|
+
const shape = this.engineData.EngineDict.Rendered.Shapes.Children[0].Cookie.Photoshop;
|
|
130
|
+
if (shape.ShapeType !== 1) return undefined;
|
|
131
|
+
const [left, top, right, bottom] = shape.BoxBounds;
|
|
132
|
+
return { left, top, right, bottom };
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/**
|
|
136
|
+
* The text content. Paragraphs are separated by `\r`, and line breaks within a paragraph
|
|
137
|
+
* (Shift+Enter) are `\u0003`; neither displays as a new line in HTML or a terminal.
|
|
138
|
+
*/
|
|
77
139
|
get text(): string {
|
|
78
140
|
return this.textData['Txt '];
|
|
79
141
|
}
|
|
@@ -88,7 +150,10 @@ export class TypeTool {
|
|
|
88
150
|
return this.engineData.ResourceDict.FontSet.map((f: EngineDict) => f.Name);
|
|
89
151
|
}
|
|
90
152
|
|
|
91
|
-
/**
|
|
153
|
+
/**
|
|
154
|
+
* The style of each run of characters, in order. Runs only store what they change from the
|
|
155
|
+
* document's normal style sheet, so the rest comes from that.
|
|
156
|
+
*/
|
|
92
157
|
get runs(): TextRun[] {
|
|
93
158
|
const styleRun = this.engineData.EngineDict.StyleRun;
|
|
94
159
|
const lengths: number[] = [...styleRun.RunLengthArray];
|
|
@@ -96,29 +161,48 @@ export class TypeTool {
|
|
|
96
161
|
const total = lengths.reduce((sum, n) => sum + n, 0);
|
|
97
162
|
if (total - this.text.length === 1) lengths[lengths.length - 1]! -= 1;
|
|
98
163
|
|
|
99
|
-
const
|
|
164
|
+
const { FontSet, StyleSheetSet, TheNormalStyleSheet } = this.engineData.ResourceDict;
|
|
165
|
+
const defaults = StyleSheetSet[TheNormalStyleSheet].StyleSheetData;
|
|
100
166
|
return styleRun.RunArray.map((run: EngineDict, i: number): TextRun => {
|
|
101
|
-
const s = run.StyleSheet.StyleSheetData;
|
|
167
|
+
const s = { ...defaults, ...run.StyleSheet.StyleSheetData };
|
|
102
168
|
return {
|
|
103
169
|
length: lengths[i] ?? 0,
|
|
104
|
-
font: s.Font === undefined ? undefined :
|
|
170
|
+
font: s.Font === undefined ? undefined : FontSet[s.Font]?.Name,
|
|
105
171
|
size: s.FontSize,
|
|
106
|
-
// Opaque black is sometimes omitted.
|
|
107
172
|
color: s.FillColor ? toRGBA(s.FillColor.Values) : undefined,
|
|
108
173
|
leading: s.AutoLeading === false && s.Leading !== undefined ? s.Leading : 'auto',
|
|
109
174
|
fauxBold: s.FauxBold ?? false,
|
|
110
175
|
fauxItalic: s.FauxItalic ?? false,
|
|
111
176
|
underline: s.Underline ?? false,
|
|
112
177
|
strikethrough: s.Strikethrough ?? false,
|
|
178
|
+
tracking: s.Tracking ?? 0,
|
|
179
|
+
kerning: s.AutoKerning === false ? (s.Kerning ?? 0) : 'auto',
|
|
180
|
+
baselineShift: s.BaselineShift ?? 0,
|
|
181
|
+
caps: lookup(CAPS, 'FontCaps', s.FontCaps ?? 0),
|
|
182
|
+
baseline: lookup(BASELINES, 'FontBaseline', s.FontBaseline ?? 0),
|
|
113
183
|
};
|
|
114
184
|
});
|
|
115
185
|
}
|
|
116
186
|
|
|
117
|
-
/** Unique font sizes, in
|
|
187
|
+
/** Unique font sizes as stored, in pixels before `transform` scales the text. See `pointSizes()`. */
|
|
118
188
|
get sizes(): number[] {
|
|
119
189
|
return uniq(this.runs.flatMap((r) => (r.size === undefined ? [] : [r.size])));
|
|
120
190
|
}
|
|
121
191
|
|
|
192
|
+
/** How much `transform` scales the text vertically, which is what Photoshop scales font sizes by. */
|
|
193
|
+
get #scale(): number {
|
|
194
|
+
return Math.hypot(this.transform.yx, this.transform.yy);
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
/**
|
|
198
|
+
* Unique font sizes in points, as Photoshop's Character panel shows them: scaled by `transform`
|
|
199
|
+
* and converted at `ppi` pixels per inch (`psd.resources.resolutionInfo.vRes`). Pass 72 for pixels.
|
|
200
|
+
*/
|
|
201
|
+
pointSizes(ppi: number): number[] {
|
|
202
|
+
if (!(ppi > 0)) throw new Error(`Invalid resolution: ${ppi}`);
|
|
203
|
+
return this.sizes.map((size) => (size * this.#scale * 72) / ppi);
|
|
204
|
+
}
|
|
205
|
+
|
|
122
206
|
/** Unique text colors (RGBA). */
|
|
123
207
|
get colors(): RGBA[] {
|
|
124
208
|
return uniq(this.runs.map((r) => r.color ?? [0, 0, 0, 255]));
|
|
@@ -131,8 +215,11 @@ export class TypeTool {
|
|
|
131
215
|
|
|
132
216
|
/** The alignment of each paragraph. */
|
|
133
217
|
get alignment(): TextAlignment[] {
|
|
218
|
+
const { ParagraphSheetSet, TheNormalParagraphSheet } = this.engineData.ResourceDict;
|
|
219
|
+
const defaults = ParagraphSheetSet[TheNormalParagraphSheet].Properties;
|
|
134
220
|
return this.engineData.EngineDict.ParagraphRun.RunArray.map(
|
|
135
|
-
|
|
221
|
+
// Photoshop leaves Justification out of the normal sheet when it's 0 (left).
|
|
222
|
+
(p: EngineDict) => ALIGNMENTS[Math.min(p.ParagraphSheet.Properties.Justification ?? defaults.Justification ?? 0, 3)]!,
|
|
136
223
|
);
|
|
137
224
|
}
|
|
138
225
|
|
|
@@ -140,7 +227,7 @@ export class TypeTool {
|
|
|
140
227
|
toCSS(): string {
|
|
141
228
|
const definition: Record<string, string | undefined> = {
|
|
142
229
|
'font-family': this.fonts.join(', '),
|
|
143
|
-
'font-size': this.sizes[0] === undefined ? undefined : `${this.sizes[0]}
|
|
230
|
+
'font-size': this.sizes[0] === undefined ? undefined : `${this.sizes[0] * this.#scale}px`,
|
|
144
231
|
color: `rgba(${this.colors[0]!.join(', ')})`,
|
|
145
232
|
'text-align': this.alignment[0],
|
|
146
233
|
};
|
|
@@ -166,6 +253,8 @@ export class TypeTool {
|
|
|
166
253
|
top: this.top,
|
|
167
254
|
right: this.right,
|
|
168
255
|
bottom: this.bottom,
|
|
256
|
+
boundingBox: this.boundingBox,
|
|
257
|
+
boxBounds: this.boxBounds,
|
|
169
258
|
transform: this.transform,
|
|
170
259
|
};
|
|
171
260
|
}
|