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.
Files changed (216) hide show
  1. package/README.md +196 -149
  2. package/dist/descriptor.d.ts +20 -0
  3. package/dist/descriptor.d.ts.map +1 -0
  4. package/dist/descriptor.js +114 -0
  5. package/dist/descriptor.js.map +1 -0
  6. package/dist/engine-data.d.ts +11 -0
  7. package/dist/engine-data.d.ts.map +1 -0
  8. package/dist/engine-data.js +82 -0
  9. package/dist/engine-data.js.map +1 -0
  10. package/dist/image.d.ts +52 -0
  11. package/dist/image.d.ts.map +1 -0
  12. package/dist/image.js +301 -0
  13. package/dist/image.js.map +1 -0
  14. package/dist/index.d.ts +16 -0
  15. package/dist/index.d.ts.map +1 -0
  16. package/dist/index.js +10 -0
  17. package/dist/index.js.map +1 -0
  18. package/dist/layer-info.d.ts +494 -0
  19. package/dist/layer-info.d.ts.map +1 -0
  20. package/dist/layer-info.js +529 -0
  21. package/dist/layer-info.js.map +1 -0
  22. package/dist/layer-styles.d.ts +160 -0
  23. package/dist/layer-styles.d.ts.map +1 -0
  24. package/dist/layer-styles.js +183 -0
  25. package/dist/layer-styles.js.map +1 -0
  26. package/dist/layer.d.ts +143 -0
  27. package/dist/layer.d.ts.map +1 -0
  28. package/dist/layer.js +249 -0
  29. package/dist/layer.js.map +1 -0
  30. package/dist/nodes.d.ts +233 -0
  31. package/dist/nodes.d.ts.map +1 -0
  32. package/dist/nodes.js +326 -0
  33. package/dist/nodes.js.map +1 -0
  34. package/dist/path-record.d.ts +34 -0
  35. package/dist/path-record.d.ts.map +1 -0
  36. package/dist/path-record.js +44 -0
  37. package/dist/path-record.js.map +1 -0
  38. package/dist/png.d.ts +10 -0
  39. package/dist/png.d.ts.map +1 -0
  40. package/dist/png.js +85 -0
  41. package/dist/png.js.map +1 -0
  42. package/dist/psd.d.ts +49 -0
  43. package/dist/psd.d.ts.map +1 -0
  44. package/dist/psd.js +106 -0
  45. package/dist/psd.js.map +1 -0
  46. package/dist/reader.d.ts +41 -0
  47. package/dist/reader.d.ts.map +1 -0
  48. package/dist/reader.js +100 -0
  49. package/dist/reader.js.map +1 -0
  50. package/dist/resources.d.ts +105 -0
  51. package/dist/resources.d.ts.map +1 -0
  52. package/dist/resources.js +173 -0
  53. package/dist/resources.js.map +1 -0
  54. package/dist/type-tool.d.ts +142 -0
  55. package/dist/type-tool.d.ts.map +1 -0
  56. package/dist/type-tool.js +222 -0
  57. package/dist/type-tool.js.map +1 -0
  58. package/package.json +26 -32
  59. package/src/descriptor.ts +141 -0
  60. package/src/engine-data.ts +86 -0
  61. package/src/image.ts +338 -0
  62. package/src/index.ts +38 -0
  63. package/src/layer-info.ts +578 -0
  64. package/src/layer-styles.ts +339 -0
  65. package/src/layer.ts +334 -0
  66. package/src/nodes.ts +411 -0
  67. package/src/path-record.ts +69 -0
  68. package/src/png.ts +92 -0
  69. package/src/psd.ts +133 -0
  70. package/src/reader.ts +125 -0
  71. package/src/resources.ts +273 -0
  72. package/src/type-tool.ts +270 -0
  73. package/.travis.yml +0 -5
  74. package/Cakefile +0 -61
  75. package/DEVELOPMENT.md +0 -27
  76. package/bower.json +0 -26
  77. package/docs/docco.css +0 -284
  78. package/docs/lib/psd/blend_mode.coffee.html +0 -72
  79. package/docs/lib/psd/channel_image.coffee.html +0 -81
  80. package/docs/lib/psd/color.coffee.html +0 -13
  81. package/docs/lib/psd/descriptor.coffee.html +0 -173
  82. package/docs/lib/psd/file.coffee.html +0 -78
  83. package/docs/lib/psd/header.coffee.html +0 -71
  84. package/docs/lib/psd/image.coffee.html +0 -94
  85. package/docs/lib/psd/image_export.coffee.html +0 -5
  86. package/docs/lib/psd/image_exports/png.coffee.html +0 -19
  87. package/docs/lib/psd/image_format.coffee.html +0 -8
  88. package/docs/lib/psd/image_formats/layer_raw.coffee.html +0 -9
  89. package/docs/lib/psd/image_formats/layer_rle.coffee.html +0 -10
  90. package/docs/lib/psd/image_formats/raw.coffee.html +0 -6
  91. package/docs/lib/psd/image_formats/rle.coffee.html +0 -37
  92. package/docs/lib/psd/image_mode.coffee.html +0 -7
  93. package/docs/lib/psd/image_modes/cmyk.coffee.html +0 -36
  94. package/docs/lib/psd/image_modes/greyscale.coffee.html +0 -17
  95. package/docs/lib/psd/image_modes/rgb.coffee.html +0 -32
  96. package/docs/lib/psd/init.coffee.html +0 -22
  97. package/docs/lib/psd/layer/blend_modes.coffee.html +0 -22
  98. package/docs/lib/psd/layer/blending_ranges.coffee.html +0 -29
  99. package/docs/lib/psd/layer/channel_image.coffee.html +0 -13
  100. package/docs/lib/psd/layer/helpers.coffee.html +0 -19
  101. package/docs/lib/psd/layer/info.coffee.html +0 -69
  102. package/docs/lib/psd/layer/mask.coffee.html +0 -11
  103. package/docs/lib/psd/layer/name.coffee.html +0 -15
  104. package/docs/lib/psd/layer/position_channels.coffee.html +0 -24
  105. package/docs/lib/psd/layer.coffee.html +0 -64
  106. package/docs/lib/psd/layer_info/blend_clipping_elements.coffee.html +0 -11
  107. package/docs/lib/psd/layer_info/blend_interior_elements.coffee.html +0 -11
  108. package/docs/lib/psd/layer_info/fill_opacity.coffee.html +0 -10
  109. package/docs/lib/psd/layer_info/gradient_fill.coffee.html +0 -12
  110. package/docs/lib/psd/layer_info/layer_id.coffee.html +0 -10
  111. package/docs/lib/psd/layer_info/layer_name_source.coffee.html +0 -10
  112. package/docs/lib/psd/layer_info/legacy_typetool.coffee.html +0 -82
  113. package/docs/lib/psd/layer_info/locked.coffee.html +0 -24
  114. package/docs/lib/psd/layer_info/metadata.coffee.html +0 -30
  115. package/docs/lib/psd/layer_info/nested_section_divider.coffee.html +0 -23
  116. package/docs/lib/psd/layer_info/object_effects.coffee.html +0 -12
  117. package/docs/lib/psd/layer_info/section_divider.coffee.html +0 -41
  118. package/docs/lib/psd/layer_info/solid_color.coffee.html +0 -24
  119. package/docs/lib/psd/layer_info/typetool.coffee.html +0 -121
  120. package/docs/lib/psd/layer_info/unicode_name.coffee.html +0 -14
  121. package/docs/lib/psd/layer_info/vector_mask.coffee.html +0 -37
  122. package/docs/lib/psd/layer_info/vector_origination.coffee.html +0 -12
  123. package/docs/lib/psd/layer_info/vector_stroke.coffee.html +0 -12
  124. package/docs/lib/psd/layer_info/vector_stroke_content.coffee.html +0 -12
  125. package/docs/lib/psd/layer_info.coffee.html +0 -11
  126. package/docs/lib/psd/layer_mask.coffee.html +0 -73
  127. package/docs/lib/psd/lazy_execute.coffee.html +0 -80
  128. package/docs/lib/psd/mask.coffee.html +0 -47
  129. package/docs/lib/psd/node.coffee.html +0 -100
  130. package/docs/lib/psd/nodes/ancestry.coffee.html +0 -47
  131. package/docs/lib/psd/nodes/build_preview.coffee.html +0 -6
  132. package/docs/lib/psd/nodes/group.coffee.html +0 -19
  133. package/docs/lib/psd/nodes/layer.coffee.html +0 -17
  134. package/docs/lib/psd/nodes/root.coffee.html +0 -61
  135. package/docs/lib/psd/nodes/search.coffee.html +0 -23
  136. package/docs/lib/psd/path_record.coffee.html +0 -73
  137. package/docs/lib/psd/resource.coffee.html +0 -20
  138. package/docs/lib/psd/resource_section.coffee.html +0 -16
  139. package/docs/lib/psd/resources/layer_comps.coffee.html +0 -31
  140. package/docs/lib/psd/resources.coffee.html +0 -44
  141. package/docs/lib/psd/util.coffee.html +0 -24
  142. package/docs/lib/psd.coffee.html +0 -85
  143. package/index.js +0 -2
  144. package/lib/psd/blend_mode.coffee +0 -78
  145. package/lib/psd/channel_image.coffee +0 -87
  146. package/lib/psd/color.coffee +0 -10
  147. package/lib/psd/descriptor.coffee +0 -189
  148. package/lib/psd/file.coffee +0 -88
  149. package/lib/psd/header.coffee +0 -83
  150. package/lib/psd/image.coffee +0 -107
  151. package/lib/psd/image_export.coffee +0 -2
  152. package/lib/psd/image_exports/png.coffee +0 -28
  153. package/lib/psd/image_format.coffee +0 -5
  154. package/lib/psd/image_formats/layer_raw.coffee +0 -6
  155. package/lib/psd/image_formats/layer_rle.coffee +0 -7
  156. package/lib/psd/image_formats/raw.coffee +0 -3
  157. package/lib/psd/image_formats/rle.coffee +0 -38
  158. package/lib/psd/image_mode.coffee +0 -4
  159. package/lib/psd/image_modes/cmyk.coffee +0 -36
  160. package/lib/psd/image_modes/greyscale.coffee +0 -14
  161. package/lib/psd/image_modes/rgb.coffee +0 -39
  162. package/lib/psd/init.coffee +0 -21
  163. package/lib/psd/layer/blend_modes.coffee +0 -20
  164. package/lib/psd/layer/blending_ranges.coffee +0 -28
  165. package/lib/psd/layer/channel_image.coffee +0 -10
  166. package/lib/psd/layer/helpers.coffee +0 -16
  167. package/lib/psd/layer/info.coffee +0 -76
  168. package/lib/psd/layer/mask.coffee +0 -8
  169. package/lib/psd/layer/name.coffee +0 -12
  170. package/lib/psd/layer/position_channels.coffee +0 -22
  171. package/lib/psd/layer.coffee +0 -64
  172. package/lib/psd/layer_info/artboard.coffee +0 -16
  173. package/lib/psd/layer_info/blend_clipping_elements.coffee +0 -8
  174. package/lib/psd/layer_info/blend_interior_elements.coffee +0 -8
  175. package/lib/psd/layer_info/fill_opacity.coffee +0 -7
  176. package/lib/psd/layer_info/gradient_fill.coffee +0 -9
  177. package/lib/psd/layer_info/layer_id.coffee +0 -7
  178. package/lib/psd/layer_info/layer_name_source.coffee +0 -7
  179. package/lib/psd/layer_info/legacy_typetool.coffee +0 -80
  180. package/lib/psd/layer_info/locked.coffee +0 -21
  181. package/lib/psd/layer_info/metadata.coffee +0 -27
  182. package/lib/psd/layer_info/nested_section_divider.coffee +0 -22
  183. package/lib/psd/layer_info/object_effects.coffee +0 -9
  184. package/lib/psd/layer_info/section_divider.coffee +0 -38
  185. package/lib/psd/layer_info/solid_color.coffee +0 -21
  186. package/lib/psd/layer_info/typetool.coffee +0 -172
  187. package/lib/psd/layer_info/unicode_name.coffee +0 -11
  188. package/lib/psd/layer_info/vector_mask.coffee +0 -35
  189. package/lib/psd/layer_info/vector_origination.coffee +0 -9
  190. package/lib/psd/layer_info/vector_stroke.coffee +0 -9
  191. package/lib/psd/layer_info/vector_stroke_content.coffee +0 -9
  192. package/lib/psd/layer_info.coffee +0 -8
  193. package/lib/psd/layer_mask.coffee +0 -73
  194. package/lib/psd/lazy_execute.coffee +0 -86
  195. package/lib/psd/mask.coffee +0 -52
  196. package/lib/psd/node.coffee +0 -150
  197. package/lib/psd/nodes/ancestry.coffee +0 -44
  198. package/lib/psd/nodes/build_preview.coffee +0 -3
  199. package/lib/psd/nodes/group.coffee +0 -16
  200. package/lib/psd/nodes/layer.coffee +0 -14
  201. package/lib/psd/nodes/root.coffee +0 -59
  202. package/lib/psd/nodes/search.coffee +0 -20
  203. package/lib/psd/path_record.coffee +0 -72
  204. package/lib/psd/resource.coffee +0 -17
  205. package/lib/psd/resource_section.coffee +0 -16
  206. package/lib/psd/resources/guides.coffee +0 -25
  207. package/lib/psd/resources/layer_comps.coffee +0 -28
  208. package/lib/psd/resources/layer_links.coffee +0 -17
  209. package/lib/psd/resources/resolution_info.coffee +0 -26
  210. package/lib/psd/resources.coffee +0 -41
  211. package/lib/psd/util.coffee +0 -23
  212. package/lib/psd.coffee +0 -92
  213. package/output/Logo_Glyph.png +0 -0
  214. package/output/Make a change and save..png +0 -0
  215. package/output/Matte.png +0 -0
  216. package/webpack.config.js +0 -23
@@ -0,0 +1,142 @@
1
+ import { type Descriptor } from './descriptor.ts';
2
+ import { type EngineDict } from './engine-data.ts';
3
+ import type { Reader } from './reader.ts';
4
+ import type { Bounds } from './resources.ts';
5
+ export interface Transform {
6
+ xx: number;
7
+ xy: number;
8
+ yx: number;
9
+ yy: number;
10
+ tx: number;
11
+ ty: number;
12
+ }
13
+ export type RGBA = [number, number, number, number];
14
+ /** A run of characters that share one style. */
15
+ export interface TextRun {
16
+ /** Number of characters in the run. */
17
+ length: number;
18
+ font: string | undefined;
19
+ size: number | undefined;
20
+ color: RGBA | undefined;
21
+ leading: number | 'auto';
22
+ fauxBold: boolean;
23
+ fauxItalic: boolean;
24
+ underline: boolean;
25
+ strikethrough: boolean;
26
+ }
27
+ declare const ALIGNMENTS: readonly ['left', 'right', 'center', 'justify'];
28
+ export type TextAlignment = (typeof ALIGNMENTS)[number];
29
+ /** A text layer (the `TySh` block). */
30
+ export declare class TypeTool {
31
+ #private;
32
+ readonly transform: Transform;
33
+ /** The raw text descriptor. */
34
+ readonly textData: Descriptor;
35
+ /** The raw warp descriptor. */
36
+ readonly warpData: Descriptor;
37
+ constructor(r: Reader);
38
+ /**
39
+ * The left edge of the text's frame, in pixels relative to the transform origin (`tx`, `ty`) and
40
+ * before the transform. The frame spans the lines around the anchor point for point text, or the box
41
+ * for paragraph text (its top can reach above the box to fit the first line). Undefined in files
42
+ * from older Photoshop versions.
43
+ */
44
+ get left(): number | undefined;
45
+ /** See `left`. */
46
+ get top(): number | undefined;
47
+ /** See `left`. */
48
+ get right(): number | undefined;
49
+ /** See `left`. */
50
+ get bottom(): number | undefined;
51
+ /** The tight bounds of the glyphs, relative to the transform origin like `left`/`top`/`right`/`bottom`. */
52
+ get boundingBox(): Bounds | undefined;
53
+ /**
54
+ * The text content. Paragraphs are separated by `\r`, and line breaks within a paragraph
55
+ * (Shift+Enter) are `\u0003`; neither displays as a new line in HTML or a terminal.
56
+ */
57
+ get text(): string;
58
+ /** The parsed EngineData, which holds all styling. Parsed on first access. */
59
+ get engineData(): EngineDict;
60
+ /** Every font referenced by the text. */
61
+ get fonts(): string[];
62
+ /**
63
+ * The style of each run of characters, in order. Runs only store what they change from the
64
+ * document's normal style sheet, so the rest comes from that.
65
+ */
66
+ get runs(): TextRun[];
67
+ /** Unique font sizes as stored, in pixels before `transform` scales the text. See `pointSizes()`. */
68
+ get sizes(): number[];
69
+ /**
70
+ * Unique font sizes in points, as Photoshop's Character panel shows them: scaled by `transform`
71
+ * and converted at `ppi` pixels per inch (`psd.resources.resolutionInfo.vRes`). Pass 72 for pixels.
72
+ */
73
+ pointSizes(ppi: number): number[];
74
+ /** Unique text colors (RGBA). */
75
+ get colors(): RGBA[];
76
+ /** Unique explicit leadings (line spacing). */
77
+ get leadings(): number[];
78
+ /** The alignment of each paragraph. */
79
+ get alignment(): TextAlignment[];
80
+ /** CSS for the first style run. Fonts may include Photoshop-internal names. */
81
+ toCSS(): string;
82
+ toJSON(): {
83
+ text: string;
84
+ font: {
85
+ name: string | undefined;
86
+ names: string[];
87
+ sizes: number[];
88
+ colors: RGBA[];
89
+ alignment: ("center" | "justify" | "left" | "right")[];
90
+ leadings: number[];
91
+ };
92
+ runs: TextRun[];
93
+ left: number | undefined;
94
+ top: number | undefined;
95
+ right: number | undefined;
96
+ bottom: number | undefined;
97
+ boundingBox: Bounds | undefined;
98
+ transform: Transform;
99
+ };
100
+ }
101
+ /** The pre-Photoshop 6 text block (`tySh`). Font data only; the text itself isn't stored here. */
102
+ export declare function readLegacyTypeTool(r: Reader): {
103
+ transform: Transform;
104
+ faces: {
105
+ mark: number;
106
+ fontType: number;
107
+ fontName: string;
108
+ fontFamilyName: string;
109
+ fontStyleName: string;
110
+ script: number;
111
+ vector: number[];
112
+ }[];
113
+ styles: {
114
+ mark: number;
115
+ faceMark: number;
116
+ size: number;
117
+ tracking: number;
118
+ kerning: number;
119
+ leading: number;
120
+ baseShift: number;
121
+ autoKern: boolean;
122
+ rotate: boolean;
123
+ }[];
124
+ type: number;
125
+ scalingFactor: number;
126
+ characterCount: number;
127
+ horzPlace: number;
128
+ vertPlace: number;
129
+ selectStart: number;
130
+ selectEnd: number;
131
+ lines: {
132
+ charCount: number;
133
+ orientation: number;
134
+ alignment: number;
135
+ actualChar: number;
136
+ style: number;
137
+ }[];
138
+ color: import("./reader.ts").SpaceColor;
139
+ antialias: boolean;
140
+ };
141
+ export {};
142
+ //# sourceMappingURL=type-tool.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"type-tool.d.ts","sourceRoot":"","sources":["../src/type-tool.ts"],"names":[],"mappings":"AAAA,OAAO,EAAkB,KAAK,UAAU,EAAE,MAAM,iBAAiB,CAAC;AAClE,OAAO,EAAmB,KAAK,UAAU,EAAE,MAAM,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js';
3
+ const ALIGNMENTS = ['left', 'right', 'center', 'justify'];
4
+ const readTransform = (r) => ({
5
+ xx: r.float64(),
6
+ xy: r.float64(),
7
+ yx: r.float64(),
8
+ yy: r.float64(),
9
+ tx: r.float64(),
10
+ ty: r.float64(),
11
+ });
12
+ /** Engine data colors are ARGB floats from 0 to 1. */
13
+ const toRGBA = (values) => {
14
+ const [a, r, g, b] = values.map((v) => Math.round(v * 255));
15
+ return [r, g, b, a];
16
+ };
17
+ const uniq = (values) => [...new Map(values.map((v) => [JSON.stringify(v), v])).values()];
18
+ /** A text layer (the `TySh` block). */
19
+ export class TypeTool {
20
+ transform;
21
+ /** The raw text descriptor. */
22
+ textData;
23
+ /** The raw warp descriptor. */
24
+ warpData;
25
+ #engineData;
26
+ constructor(r) {
27
+ r.skip(2); // version
28
+ this.transform = readTransform(r);
29
+ r.skip(6); // text version + descriptor version
30
+ this.textData = readDescriptor(r);
31
+ r.skip(6); // warp version + descriptor version
32
+ this.warpData = readDescriptor(r);
33
+ // The block ends with left, top, right and bottom fields that Photoshop leaves as 0.
34
+ }
35
+ /**
36
+ * The left edge of the text's frame, in pixels relative to the transform origin (`tx`, `ty`) and
37
+ * before the transform. The frame spans the lines around the anchor point for point text, or the box
38
+ * for paragraph text (its top can reach above the box to fit the first line). Undefined in files
39
+ * from older Photoshop versions.
40
+ */
41
+ get left() {
42
+ return this.textData.bounds?.Left.value;
43
+ }
44
+ /** See `left`. */
45
+ get top() {
46
+ return this.textData.bounds?.['Top '].value;
47
+ }
48
+ /** See `left`. */
49
+ get right() {
50
+ return this.textData.bounds?.Rght.value;
51
+ }
52
+ /** See `left`. */
53
+ get bottom() {
54
+ return this.textData.bounds?.Btom.value;
55
+ }
56
+ /** The tight bounds of the glyphs, relative to the transform origin like `left`/`top`/`right`/`bottom`. */
57
+ get boundingBox() {
58
+ const box = this.textData.boundingBox;
59
+ return box && { left: box.Left.value, top: box['Top '].value, right: box.Rght.value, bottom: box.Btom.value };
60
+ }
61
+ /**
62
+ * The text content. Paragraphs are separated by `\r`, and line breaks within a paragraph
63
+ * (Shift+Enter) are `\u0003`; neither displays as a new line in HTML or a terminal.
64
+ */
65
+ get text() {
66
+ return this.textData['Txt '];
67
+ }
68
+ /** The parsed EngineData, which holds all styling. Parsed on first access. */
69
+ get engineData() {
70
+ return (this.#engineData ??= parseEngineData(this.textData.EngineData));
71
+ }
72
+ /** Every font referenced by the text. */
73
+ get fonts() {
74
+ return this.engineData.ResourceDict.FontSet.map((f) => f.Name);
75
+ }
76
+ /**
77
+ * The style of each run of characters, in order. Runs only store what they change from the
78
+ * document's normal style sheet, so the rest comes from that.
79
+ */
80
+ get runs() {
81
+ const styleRun = this.engineData.EngineDict.StyleRun;
82
+ const lengths = [...styleRun.RunLengthArray];
83
+ // EngineData counts a trailing return that the text value omits.
84
+ const total = lengths.reduce((sum, n) => sum + n, 0);
85
+ if (total - this.text.length === 1)
86
+ lengths[lengths.length - 1] -= 1;
87
+ const { FontSet, StyleSheetSet, TheNormalStyleSheet } = this.engineData.ResourceDict;
88
+ const defaults = StyleSheetSet[TheNormalStyleSheet].StyleSheetData;
89
+ return styleRun.RunArray.map((run, i) => {
90
+ const s = { ...defaults, ...run.StyleSheet.StyleSheetData };
91
+ return {
92
+ length: lengths[i] ?? 0,
93
+ font: s.Font === undefined ? undefined : FontSet[s.Font]?.Name,
94
+ size: s.FontSize,
95
+ color: s.FillColor ? toRGBA(s.FillColor.Values) : undefined,
96
+ leading: s.AutoLeading === false && s.Leading !== undefined ? s.Leading : 'auto',
97
+ fauxBold: s.FauxBold ?? false,
98
+ fauxItalic: s.FauxItalic ?? false,
99
+ underline: s.Underline ?? false,
100
+ strikethrough: s.Strikethrough ?? false,
101
+ };
102
+ });
103
+ }
104
+ /** Unique font sizes as stored, in pixels before `transform` scales the text. See `pointSizes()`. */
105
+ get sizes() {
106
+ return uniq(this.runs.flatMap((r) => (r.size === undefined ? [] : [r.size])));
107
+ }
108
+ /** How much `transform` scales the text vertically, which is what Photoshop scales font sizes by. */
109
+ get #scale() {
110
+ return Math.hypot(this.transform.yx, this.transform.yy);
111
+ }
112
+ /**
113
+ * Unique font sizes in points, as Photoshop's Character panel shows them: scaled by `transform`
114
+ * and converted at `ppi` pixels per inch (`psd.resources.resolutionInfo.vRes`). Pass 72 for pixels.
115
+ */
116
+ pointSizes(ppi) {
117
+ if (!(ppi > 0))
118
+ throw new Error(`Invalid resolution: ${ppi}`);
119
+ return this.sizes.map((size) => (size * this.#scale * 72) / ppi);
120
+ }
121
+ /** Unique text colors (RGBA). */
122
+ get colors() {
123
+ return uniq(this.runs.map((r) => r.color ?? [0, 0, 0, 255]));
124
+ }
125
+ /** Unique explicit leadings (line spacing). */
126
+ get leadings() {
127
+ return uniq(this.runs.flatMap((r) => (r.leading === 'auto' ? [] : [r.leading])));
128
+ }
129
+ /** The alignment of each paragraph. */
130
+ get alignment() {
131
+ const { ParagraphSheetSet, TheNormalParagraphSheet } = this.engineData.ResourceDict;
132
+ const defaults = ParagraphSheetSet[TheNormalParagraphSheet].Properties;
133
+ return this.engineData.EngineDict.ParagraphRun.RunArray.map(
134
+ // Photoshop leaves Justification out of the normal sheet when it's 0 (left).
135
+ (p) => ALIGNMENTS[Math.min(p.ParagraphSheet.Properties.Justification ?? defaults.Justification ?? 0, 3)]);
136
+ }
137
+ /** CSS for the first style run. Fonts may include Photoshop-internal names. */
138
+ toCSS() {
139
+ const definition = {
140
+ 'font-family': this.fonts.join(', '),
141
+ 'font-size': this.sizes[0] === undefined ? undefined : `${this.sizes[0] * this.#scale}px`,
142
+ color: `rgba(${this.colors[0].join(', ')})`,
143
+ 'text-align': this.alignment[0],
144
+ };
145
+ return Object.entries(definition)
146
+ .filter(([, v]) => v !== undefined)
147
+ .map(([k, v]) => `${k}: ${v};`)
148
+ .join('\n');
149
+ }
150
+ toJSON() {
151
+ return {
152
+ text: this.text,
153
+ font: {
154
+ name: this.fonts[0],
155
+ names: this.fonts,
156
+ sizes: this.sizes,
157
+ colors: this.colors,
158
+ alignment: this.alignment,
159
+ leadings: this.leadings,
160
+ },
161
+ runs: this.runs,
162
+ left: this.left,
163
+ top: this.top,
164
+ right: this.right,
165
+ bottom: this.bottom,
166
+ boundingBox: this.boundingBox,
167
+ transform: this.transform,
168
+ };
169
+ }
170
+ }
171
+ /** The pre-Photoshop 6 text block (`tySh`). Font data only; the text itself isn't stored here. */
172
+ export function readLegacyTypeTool(r) {
173
+ r.skip(2); // version
174
+ const transform = readTransform(r);
175
+ r.skip(2); // font version
176
+ const faces = Array.from({ length: r.int16() }, () => ({
177
+ mark: r.int16(),
178
+ fontType: r.int32(),
179
+ fontName: r.pascal(),
180
+ fontFamilyName: r.pascal(),
181
+ fontStyleName: r.pascal(),
182
+ script: r.int16(),
183
+ vector: Array.from({ length: r.int32() }, () => r.int32()),
184
+ }));
185
+ const styles = Array.from({ length: r.int16() }, () => {
186
+ const style = {
187
+ mark: r.int16(),
188
+ faceMark: r.int16(),
189
+ size: r.int32(),
190
+ tracking: r.int32(),
191
+ kerning: r.int32(),
192
+ leading: r.int32(),
193
+ baseShift: r.int32(),
194
+ autoKern: r.bool(),
195
+ rotate: false,
196
+ };
197
+ r.skip(1);
198
+ style.rotate = r.bool();
199
+ return style;
200
+ });
201
+ const type = r.int16();
202
+ const scalingFactor = r.int32();
203
+ const characterCount = r.int32();
204
+ const horzPlace = r.int32();
205
+ const vertPlace = r.int32();
206
+ const selectStart = r.int32();
207
+ const selectEnd = r.int32();
208
+ const lines = Array.from({ length: r.int16() }, () => ({
209
+ charCount: r.int32(),
210
+ orientation: r.int16(),
211
+ alignment: r.int16(),
212
+ actualChar: r.int16(),
213
+ style: r.int16(),
214
+ }));
215
+ const color = r.spaceColor();
216
+ const antialias = r.bool();
217
+ return {
218
+ transform, faces, styles, type, scalingFactor, characterCount, horzPlace, vertPlace,
219
+ selectStart, selectEnd, lines, color, antialias,
220
+ };
221
+ }
222
+ //# sourceMappingURL=type-tool.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file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AE,CAAC;IAC7B,MAAM,SAAS,GAAG,CAAC,CAAC,IAAI,EAAE,CAAC;IAC3B,OAAO;QACL,SAAS,EAAE,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,aAAa,EAAE,cAAc,EAAE,SAAS,EAAE,SAAS;QACnF,WAAW,EAAE,SAAS,EAAE,KAAK,EAAE,KAAK,EAAE,SAAS;KAChD,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,10 +1,13 @@
1
1
  {
2
2
  "name": "psd",
3
- "description": "A general purpose Photoshop file parser.",
4
- "version": "3.4.0",
5
- "main": "./index.js",
3
+ "description": "A general purpose Photoshop file parser for Node and the browser.",
4
+ "version": "4.1.0",
5
+ "license": "MIT",
6
+ "author": "Ryan LeFevre (https://github.com/meltingice)",
7
+ "type": "module",
6
8
  "keywords": [
7
9
  "psd",
10
+ "psb",
8
11
  "parser",
9
12
  "photoshop",
10
13
  "adobe",
@@ -12,39 +15,30 @@
12
15
  ],
13
16
  "repository": {
14
17
  "type": "git",
15
- "url": "git@github.com:meltingice/psd.js.git"
18
+ "url": "git+https://github.com/meltingice/psd.js.git"
16
19
  },
17
- "dependencies": {
18
- "coffee-script": "~ 1.7.1",
19
- "coffeescript-module": "~ 0.2.1",
20
- "iconv-lite": "~ 0.4.4",
21
- "jspack": "~ 0.0.3",
22
- "lodash": "~4.17.21",
23
- "parse-engine-data": "~ 0.1",
24
- "pngjs": "3.2.0",
25
- "rsvp": "~ 3.0.6"
20
+ "exports": {
21
+ ".": {
22
+ "types": "./dist/index.d.ts",
23
+ "default": "./dist/index.js"
24
+ }
25
+ },
26
+ "files": [
27
+ "dist",
28
+ "src"
29
+ ],
30
+ "sideEffects": false,
31
+ "engines": {
32
+ "node": ">=20"
26
33
  },
27
34
  "scripts": {
28
- "test": "mocha test",
29
- "docs": "node_modules/docco-husky/bin/generate lib shims",
30
- "build": "cake compile-browser",
31
- "generate-docs": "cake docs:generate",
32
- "local-dev": "npm run build && rm -rf ./examples/browser/dist && cp -R ./dist ./examples/browser/dist && webpack serve"
35
+ "build": "rm -rf dist && tsc -p tsconfig.build.json",
36
+ "typecheck": "tsc --noEmit",
37
+ "test": "node --test",
38
+ "prepublishOnly": "npm run typecheck && npm test && npm run build"
33
39
  },
34
40
  "devDependencies": {
35
- "acorn": "^5.1.1",
36
- "bluebird": "^2.9.25",
37
- "browserify": "^17.0.0",
38
- "coffeeify": "~ 1.1.0",
39
- "copy-webpack-plugin": "^9.0.1",
40
- "docco-husky": "*",
41
- "html-loader": "^2.1.2",
42
- "mocha": "^9.1.1",
43
- "rimraf": "~ 2.2.8",
44
- "should": "~ 3.3.1",
45
- "uglify-js": "^2.4.21",
46
- "webpack": "^5.53.0",
47
- "webpack-cli": "^4.8.0",
48
- "webpack-dev-server": "^4.2.1"
41
+ "@types/node": "^24.0.0",
42
+ "typescript": "^7.0.2"
49
43
  }
50
44
  }
@@ -0,0 +1,141 @@
1
+ import type { Reader } from './reader.ts';
2
+
3
+ /**
4
+ * Descriptors are Photoshop's self-describing key/value structures (the "Action Descriptor"
5
+ * format). Their contents vary by block and Photoshop version, so values are typed loosely.
6
+ */
7
+ export interface Descriptor {
8
+ class: DescriptorClass;
9
+ [key: string]: any;
10
+ }
11
+
12
+ export interface DescriptorClass {
13
+ name: string;
14
+ id: string;
15
+ }
16
+
17
+ export interface UnitValue {
18
+ id: string;
19
+ unit: string | undefined;
20
+ value: number;
21
+ }
22
+
23
+ const UNITS: Record<string, string> = {
24
+ '#Ang': 'Angle',
25
+ '#Rsl': 'Density',
26
+ '#Rlt': 'Distance',
27
+ '#Nne': 'None',
28
+ '#Prc': 'Percent',
29
+ '#Pxl': 'Pixels',
30
+ '#Mlm': 'Millimeters',
31
+ '#Pnt': 'Points',
32
+ };
33
+
34
+ export function readDescriptor(r: Reader): Descriptor {
35
+ const data: Descriptor = { class: readClass(r) };
36
+ const count = r.int32();
37
+ for (let i = 0; i < count; i++) {
38
+ const key = readId(r);
39
+ data[key] = readItem(r, r.ascii(4));
40
+ }
41
+ return data;
42
+ }
43
+
44
+ function readClass(r: Reader): DescriptorClass {
45
+ return { name: r.unicode(), id: readId(r) };
46
+ }
47
+
48
+ function readId(r: Reader): string {
49
+ const len = r.int32();
50
+ return r.ascii(len === 0 ? 4 : len);
51
+ }
52
+
53
+ function readItem(r: Reader, type: string): unknown {
54
+ switch (type) {
55
+ case 'bool':
56
+ return r.bool();
57
+ case 'type':
58
+ case 'GlbC':
59
+ return readClass(r);
60
+ case 'Objc':
61
+ case 'GlbO':
62
+ return readDescriptor(r);
63
+ case 'doub':
64
+ return r.float64();
65
+ case 'enum':
66
+ return { type: readId(r), value: readId(r) };
67
+ case 'alis':
68
+ return r.ascii(r.int32());
69
+ case 'Pth ': {
70
+ r.skip(4); // length
71
+ const sig = r.ascii(4);
72
+ r.skip(4); // path size, little endian
73
+ const chars = r.view.getInt32(r.pos, true);
74
+ r.skip(4);
75
+ return { sig, path: r.unicode(chars) };
76
+ }
77
+ case 'long':
78
+ return r.int32();
79
+ case 'comp':
80
+ return r.int64();
81
+ case 'VlLs': {
82
+ const count = r.int32();
83
+ return Array.from({ length: count }, () => readItem(r, r.ascii(4)));
84
+ }
85
+ case 'tdta':
86
+ return r.bytesView(r.int32());
87
+ case 'obj ':
88
+ return readReference(r);
89
+ case 'TEXT':
90
+ return r.unicode();
91
+ case 'UntF':
92
+ return readUnit(r, () => r.float64());
93
+ case 'UnFl': {
94
+ // A unit followed by a list of doubles. Only seen inside object arrays.
95
+ const id = r.ascii(4);
96
+ const count = r.int32();
97
+ return { id, unit: UNITS[id], values: Array.from({ length: count }, () => r.float64()) };
98
+ }
99
+ default:
100
+ // Includes 'ObAr' (object array), which is undocumented. Unknown types would desync the
101
+ // reader, so fail loudly instead of returning garbage.
102
+ throw new Error(`Unsupported descriptor item type '${type}' at offset ${r.pos - 4}`);
103
+ }
104
+ }
105
+
106
+ function readUnit(r: Reader, read: () => number): UnitValue {
107
+ const id = r.ascii(4);
108
+ return { id, unit: UNITS[id], value: read() };
109
+ }
110
+
111
+ function readReference(r: Reader): Array<{ type: string; value: unknown }> {
112
+ const count = r.int32();
113
+ const items = [];
114
+ for (let i = 0; i < count; i++) {
115
+ const type = r.ascii(4);
116
+ let value: unknown;
117
+ switch (type) {
118
+ case 'prop':
119
+ value = { class: readClass(r), id: readId(r) };
120
+ break;
121
+ case 'Clss':
122
+ value = readClass(r);
123
+ break;
124
+ case 'Enmr':
125
+ value = { class: readClass(r), type: readId(r), value: readId(r) };
126
+ break;
127
+ case 'Idnt':
128
+ case 'indx':
129
+ case 'rele':
130
+ value = r.int32();
131
+ break;
132
+ case 'name':
133
+ value = r.unicode();
134
+ break;
135
+ default:
136
+ throw new Error(`Unsupported descriptor reference type '${type}' at offset ${r.pos - 4}`);
137
+ }
138
+ items.push({ type, value });
139
+ }
140
+ return items;
141
+ }