ag-psd 15.3.1 → 17.0.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 (43) hide show
  1. package/CHANGELOG.md +9 -0
  2. package/README.md +1 -0
  3. package/dist/additionalInfo.js +864 -157
  4. package/dist/additionalInfo.js.map +1 -1
  5. package/dist/bundle.js +1223 -242
  6. package/dist/descriptor.d.ts +129 -0
  7. package/dist/descriptor.js +272 -36
  8. package/dist/descriptor.js.map +1 -1
  9. package/dist/imageResources.js +9 -17
  10. package/dist/imageResources.js.map +1 -1
  11. package/dist/psd.d.ts +450 -74
  12. package/dist/psdReader.d.ts +2 -0
  13. package/dist/psdReader.js +30 -13
  14. package/dist/psdReader.js.map +1 -1
  15. package/dist/psdWriter.d.ts +4 -0
  16. package/dist/psdWriter.js +25 -7
  17. package/dist/psdWriter.js.map +1 -1
  18. package/dist/text.js +23 -12
  19. package/dist/text.js.map +1 -1
  20. package/dist-es/additionalInfo.js +865 -158
  21. package/dist-es/additionalInfo.js.map +1 -1
  22. package/dist-es/descriptor.d.ts +129 -0
  23. package/dist-es/descriptor.js +270 -37
  24. package/dist-es/descriptor.js.map +1 -1
  25. package/dist-es/imageResources.js +10 -18
  26. package/dist-es/imageResources.js.map +1 -1
  27. package/dist-es/psd.d.ts +450 -74
  28. package/dist-es/psdReader.d.ts +2 -0
  29. package/dist-es/psdReader.js +27 -12
  30. package/dist-es/psdReader.js.map +1 -1
  31. package/dist-es/psdWriter.d.ts +4 -0
  32. package/dist-es/psdWriter.js +21 -7
  33. package/dist-es/psdWriter.js.map +1 -1
  34. package/dist-es/text.js +23 -12
  35. package/dist-es/text.js.map +1 -1
  36. package/package.json +1 -1
  37. package/src/additionalInfo.ts +1956 -212
  38. package/src/descriptor.ts +297 -37
  39. package/src/imageResources.ts +14 -19
  40. package/src/psd.ts +472 -49
  41. package/src/psdReader.ts +26 -7
  42. package/src/psdWriter.ts +24 -8
  43. package/src/text.ts +22 -14
package/src/psdWriter.ts CHANGED
@@ -4,7 +4,7 @@ import {
4
4
  offsetForChannel, createImageData, fromBlendMode, ChannelID, Compression, clamp,
5
5
  LayerMaskFlags, MaskParams, ColorSpace, Bounds, largeAdditionalInfoKeys, RAW_IMAGE_DATA, writeDataZipWithoutPrediction
6
6
  } from './helpers';
7
- import { ExtendedWriteOptions, hasMultiEffects, infoHandlers } from './additionalInfo';
7
+ import { ExtendedWriteOptions, infoHandlers } from './additionalInfo';
8
8
  import { resourceHandlers } from './imageResources';
9
9
 
10
10
  export interface PsdWriter {
@@ -43,11 +43,21 @@ export function writeUint16(writer: PsdWriter, value: number) {
43
43
  writer.view.setUint16(offset, value, false);
44
44
  }
45
45
 
46
+ export function writeUint16LE(writer: PsdWriter, value: number) {
47
+ const offset = addSize(writer, 2);
48
+ writer.view.setUint16(offset, value, true);
49
+ }
50
+
46
51
  export function writeInt32(writer: PsdWriter, value: number) {
47
52
  const offset = addSize(writer, 4);
48
53
  writer.view.setInt32(offset, value, false);
49
54
  }
50
55
 
56
+ export function writeInt32LE(writer: PsdWriter, value: number) {
57
+ const offset = addSize(writer, 4);
58
+ writer.view.setInt32(offset, value, true);
59
+ }
60
+
51
61
  export function writeUint32(writer: PsdWriter, value: number) {
52
62
  const offset = addSize(writer, 4);
53
63
  writer.view.setUint32(offset, value, false);
@@ -110,14 +120,23 @@ export function writePascalString(writer: PsdWriter, text: string, padTo: number
110
120
  }
111
121
  }
112
122
 
113
- export function writeUnicodeString(writer: PsdWriter, text: string) {
114
- writeUint32(writer, text.length);
115
-
123
+ export function writeUnicodeStringWithoutLength(writer: PsdWriter, text: string) {
116
124
  for (let i = 0; i < text.length; i++) {
117
125
  writeUint16(writer, text.charCodeAt(i));
118
126
  }
119
127
  }
120
128
 
129
+ export function writeUnicodeStringWithoutLengthLE(writer: PsdWriter, text: string) {
130
+ for (let i = 0; i < text.length; i++) {
131
+ writeUint16LE(writer, text.charCodeAt(i));
132
+ }
133
+ }
134
+
135
+ export function writeUnicodeString(writer: PsdWriter, text: string) {
136
+ writeUint32(writer, text.length);
137
+ writeUnicodeStringWithoutLength(writer, text);
138
+ }
139
+
121
140
  export function writeUnicodeStringWithPadding(writer: PsdWriter, text: string) {
122
141
  writeUint32(writer, text.length + 1);
123
142
 
@@ -304,10 +323,7 @@ function writeLayerInfo(tempBuffer: Uint8Array, writer: PsdWriter, psd: Psd, glo
304
323
  if (layer.vectorMask || (layer.sectionDivider && layer.sectionDivider.type !== SectionDividerType.Other)) {
305
324
  flags |= 0x10; // pixel data irrelevant to appearance of document
306
325
  }
307
- if (layer.effects && hasMultiEffects(layer.effects)) { // TODO: this is not correct
308
- flags |= 0x20; // just guessing this one, might be completely incorrect
309
- }
310
- // if ('_2' in layer) flags |= 0x20; // TEMP!!!
326
+ if (layer.effectsOpen) flags |= 0x20;
311
327
 
312
328
  writeUint8(writer, flags);
313
329
  writeUint8(writer, 0); // filler
package/src/text.ts CHANGED
@@ -263,24 +263,32 @@ const antialias: AntiAlias[] = ['none', 'crisp', 'strong', 'smooth', 'sharp'];
263
263
  const justification: Justification[] = ['left', 'right', 'center'];
264
264
 
265
265
  function upperFirst(value: string) {
266
- return value.substr(0, 1).toUpperCase() + value.substr(1);
266
+ return value.substring(0, 1).toUpperCase() + value.substring(1);
267
267
  }
268
268
 
269
- function decodeColor(color: { Type: number; Values: number[]; }): Color {
269
+ function decodeColor(color: TypeValues): Color {
270
270
  const c = color.Values;
271
-
272
- if (color.Type === 0) { // grayscale
273
- return { r: c[1] * 255, g: c[1] * 255, b: c[1] * 255 }; // , c[0] * 255];
274
- } else { // rgb
275
- return { r: c[1] * 255, g: c[2] * 255, b: c[3] * 255, a: c[0] }; // , c[0] * 255];
271
+ switch (color.Type) {
272
+ case 0: return { k: c[1] * 255 }; // grayscale (alpha?)
273
+ case 1: return c[0] === 1 ?
274
+ { r: c[1] * 255, g: c[2] * 255, b: c[3] * 255 } : // rgb
275
+ { r: c[1] * 255, g: c[2] * 255, b: c[3] * 255, a: c[0] * 255 }; // rgba
276
+ case 2: return { c: c[1] * 255, m: c[2] * 255, y: c[3] * 255, k: c[4] * 255 }; // cmyk (alpha?)
277
+ default: throw new Error('Unknown color type in text layer');
276
278
  }
277
279
  }
278
280
 
279
- function encodeColor(color: Color | undefined) {
280
- if (color && 'r' in color) {
281
- return ['a' in color ? color.a : 1, color.r / 255, color.g / 255, color.b / 255];
281
+ function encodeColor(color: Color | undefined): TypeValues {
282
+ if (!color) {
283
+ return { Type: 1, Values: [0, 0, 0, 0] };
284
+ } else if ('r' in color) {
285
+ return { Type: 1, Values: ['a' in color ? color.a / 255 : 1, color.r / 255, color.g / 255, color.b / 255] };
286
+ } else if ('c' in color) {
287
+ return { Type: 2, Values: [1, color.c / 255, color.m / 255, color.y / 255, color.k / 255] };
288
+ } else if ('k' in color) {
289
+ return { Type: 0, Values: [1, color.k / 255] };
282
290
  } else {
283
- return [0, 0, 0, 0];
291
+ throw new Error('Invalid color type in text layer');
284
292
  }
285
293
  }
286
294
 
@@ -342,7 +350,7 @@ function encodeObject(obj: any, keys: string[], fonts: Font[]) {
342
350
  } else if (key === 'font') {
343
351
  result[Key] = findOrAddFont(fonts, obj[key]);
344
352
  } else if (key === 'fillColor' || key === 'strokeColor') {
345
- result[Key] = { Type: 1, Values: encodeColor(obj[key]) } as TypeValues;
353
+ result[Key] = encodeColor(obj[key]);
346
354
  } else {
347
355
  result[Key] = obj[key];
348
356
  }
@@ -722,8 +730,8 @@ export function encodeEngineData(data: LayerTextData) {
722
730
  ShowGrid: !!gridInfo.show,
723
731
  GridSize: gridInfo.size ?? 18,
724
732
  GridLeading: gridInfo.leading ?? 22,
725
- GridColor: { Type: 1, Values: encodeColor(gridInfo.color) },
726
- GridLeadingFillColor: { Type: 1, Values: encodeColor(gridInfo.color) },
733
+ GridColor: encodeColor(gridInfo.color),
734
+ GridLeadingFillColor: encodeColor(gridInfo.color),
727
735
  AlignLineHeightToGridFlags: !!gridInfo.alignLineHeightToGridFlags,
728
736
  },
729
737
  AntiAlias: antialias.indexOf(data.antiAlias ?? 'sharp'),