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.
Files changed (75) hide show
  1. package/README.md +146 -7
  2. package/dist/descriptor.js +6 -2
  3. package/dist/descriptor.js.map +1 -1
  4. package/dist/image.d.ts +28 -4
  5. package/dist/image.d.ts.map +1 -1
  6. package/dist/image.js +36 -22
  7. package/dist/image.js.map +1 -1
  8. package/dist/index.d.ts +9 -6
  9. package/dist/index.d.ts.map +1 -1
  10. package/dist/index.js +3 -1
  11. package/dist/index.js.map +1 -1
  12. package/dist/layer-info.d.ts +53 -10
  13. package/dist/layer-info.d.ts.map +1 -1
  14. package/dist/layer-info.js +34 -8
  15. package/dist/layer-info.js.map +1 -1
  16. package/dist/layer-styles.d.ts +11 -1
  17. package/dist/layer-styles.d.ts.map +1 -1
  18. package/dist/layer-styles.js +5 -5
  19. package/dist/layer-styles.js.map +1 -1
  20. package/dist/layer.d.ts +32 -0
  21. package/dist/layer.d.ts.map +1 -1
  22. package/dist/layer.js +68 -6
  23. package/dist/layer.js.map +1 -1
  24. package/dist/linked-files.d.ts +32 -0
  25. package/dist/linked-files.d.ts.map +1 -0
  26. package/dist/linked-files.js +45 -0
  27. package/dist/linked-files.js.map +1 -0
  28. package/dist/nodes.d.ts +21 -9
  29. package/dist/nodes.d.ts.map +1 -1
  30. package/dist/nodes.js +16 -6
  31. package/dist/nodes.js.map +1 -1
  32. package/dist/path-record.d.ts +28 -0
  33. package/dist/path-record.d.ts.map +1 -1
  34. package/dist/path-record.js +54 -1
  35. package/dist/path-record.js.map +1 -1
  36. package/dist/patterns.d.ts +24 -0
  37. package/dist/patterns.d.ts.map +1 -0
  38. package/dist/patterns.js +108 -0
  39. package/dist/patterns.js.map +1 -0
  40. package/dist/png.d.ts +12 -4
  41. package/dist/png.d.ts.map +1 -1
  42. package/dist/png.js +68 -31
  43. package/dist/png.js.map +1 -1
  44. package/dist/psd.d.ts +13 -2
  45. package/dist/psd.d.ts.map +1 -1
  46. package/dist/psd.js +23 -1
  47. package/dist/psd.js.map +1 -1
  48. package/dist/resources.d.ts +7 -1
  49. package/dist/resources.d.ts.map +1 -1
  50. package/dist/resources.js +18 -0
  51. package/dist/resources.js.map +1 -1
  52. package/dist/shapes.d.ts +94 -0
  53. package/dist/shapes.d.ts.map +1 -0
  54. package/dist/shapes.js +76 -0
  55. package/dist/shapes.js.map +1 -0
  56. package/dist/type-tool.d.ts +60 -11
  57. package/dist/type-tool.d.ts.map +1 -1
  58. package/dist/type-tool.js +87 -17
  59. package/dist/type-tool.js.map +1 -1
  60. package/package.json +4 -2
  61. package/src/descriptor.ts +6 -2
  62. package/src/image.ts +56 -26
  63. package/src/index.ts +17 -5
  64. package/src/layer-info.ts +76 -8
  65. package/src/layer-styles.ts +8 -6
  66. package/src/layer.ts +79 -6
  67. package/src/linked-files.ts +66 -0
  68. package/src/nodes.ts +20 -6
  69. package/src/path-record.ts +63 -2
  70. package/src/patterns.ts +113 -0
  71. package/src/png.ts +76 -31
  72. package/src/psd.ts +25 -1
  73. package/src/resources.ts +21 -1
  74. package/src/shapes.ts +156 -0
  75. 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
- this.left = r.int32();
71
- this.top = r.int32();
72
- this.right = r.int32();
73
- this.bottom = r.int32();
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 text content. Paragraphs are separated by `\r`. */
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
- /** The style of each run of characters, in order. */
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 fontSet = this.engineData.ResourceDict.FontSet;
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 : fontSet[s.Font]?.Name,
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 points. */
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
- (p: EngineDict) => ALIGNMENTS[Math.min(p.ParagraphSheet.Properties.Justification ?? 0, 3)]!,
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]}pt`,
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
  }