ag-psd 17.0.2 → 17.0.4

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ag-psd",
3
- "version": "17.0.2",
3
+ "version": "17.0.4",
4
4
  "description": "Library for reading and writing PSD files",
5
5
  "main": "dist/index.js",
6
6
  "module": "dist-es/index.js",
@@ -9,7 +9,7 @@ import {
9
9
  CurvesAdjustment, CurvesAdjustmentChannel, HueSaturationAdjustment, HueSaturationAdjustmentChannel,
10
10
  PresetInfo, Color, ColorBalanceValues, WriteOptions, LinkedFile, PlacedLayerType, Warp, KeyDescriptorItem,
11
11
  BooleanOperation, LayerEffectsInfo, Annotation, LayerVectorMask, AnimationFrame, Timeline, PlacedLayerFilter,
12
- UnitsValue, Filter,
12
+ UnitsValue, Filter, PlacedLayer,
13
13
  } from './psd';
14
14
  import {
15
15
  PsdReader, readSignature, readUnicodeString, skipBytes, readUint32, readUint8, readFloat64, readUint16,
@@ -1151,6 +1151,9 @@ addHandler(
1151
1151
  const placed = target.placedLayer!;
1152
1152
  writeSignature(writer, 'plcL');
1153
1153
  writeInt32(writer, 3); // version
1154
+ if (!placed.id || typeof placed.id !== 'string' || !/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(placed.id)) {
1155
+ throw new Error('Placed layer ID must be in a GUID format (example: 20953ddb-9391-11ec-b4f1-c15674f50bc4)');
1156
+ }
1154
1157
  writePascalString(writer, placed.id, 1);
1155
1158
  writeInt32(writer, 1); // pageNumber
1156
1159
  writeInt32(writer, 1); // totalPages
@@ -1159,9 +1162,10 @@ addHandler(
1159
1162
  writeInt32(writer, placedLayerTypes.indexOf(placed.type));
1160
1163
  for (let i = 0; i < 8; i++) writeFloat64(writer, placed.transform[i]);
1161
1164
  writeInt32(writer, 0); // warp version
1162
- const isQuilt = placed.warp && isQuiltWarp(placed.warp);
1165
+ const warp = getWarpFromPlacedLayer(placed);
1166
+ const isQuilt = isQuiltWarp(warp);
1163
1167
  const type = isQuilt ? 'quiltWarp' : 'warp';
1164
- writeVersionAndDescriptor(writer, '', type, encodeWarp(placed.warp || {}), type);
1168
+ writeVersionAndDescriptor(writer, '', type, encodeWarp(warp), type);
1165
1169
  },
1166
1170
  );
1167
1171
 
@@ -3113,6 +3117,41 @@ interface SoLdDescriptor {
3113
3117
 
3114
3118
  // let t: any;
3115
3119
 
3120
+ function getWarpFromPlacedLayer(placed: PlacedLayer): Warp {
3121
+ if (placed.warp) return placed.warp;
3122
+
3123
+ if (!placed.width || !placed.height) throw new Error('You must provide width and height of the linked image in placedLayer');
3124
+
3125
+ const w = placed.width;
3126
+ const h = placed.height;
3127
+ const x0 = 0, x1 = w / 3, x2 = w * 2 / 3, x3 = w;
3128
+ const y0 = 0, y1 = h / 3, y2 = h * 2 / 3, y3 = h;
3129
+
3130
+ return {
3131
+ style: 'custom',
3132
+ value: 0,
3133
+ perspective: 0,
3134
+ perspectiveOther: 0,
3135
+ rotate: 'horizontal',
3136
+ bounds: {
3137
+ top: { value: 0, units: 'Pixels' },
3138
+ left: { value: 0, units: 'Pixels' },
3139
+ bottom: { value: h, units: 'Pixels' },
3140
+ right: { value: w, units: 'Pixels' },
3141
+ },
3142
+ uOrder: 4,
3143
+ vOrder: 4,
3144
+ customEnvelopeWarp: {
3145
+ meshPoints: [
3146
+ { x: x0, y: y0 }, { x: x1, y: y0 }, { x: x2, y: y0 }, { x: x3, y: y0 },
3147
+ { x: x0, y: y1 }, { x: x1, y: y1 }, { x: x2, y: y1 }, { x: x3, y: y1 },
3148
+ { x: x0, y: y2 }, { x: x1, y: y2 }, { x: x2, y: y2 }, { x: x3, y: y2 },
3149
+ { x: x0, y: y3 }, { x: x1, y: y3 }, { x: x2, y: y3 }, { x: x3, y: y3 },
3150
+ ],
3151
+ },
3152
+ };
3153
+ }
3154
+
3116
3155
  addHandler(
3117
3156
  'SoLd',
3118
3157
  hasKey('placedLayer'),
@@ -3168,6 +3207,11 @@ addHandler(
3168
3207
  writeInt32(writer, 4); // version
3169
3208
 
3170
3209
  const placed = target.placedLayer!;
3210
+
3211
+ if (!placed.id || typeof placed.id !== 'string' || !/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(placed.id)) {
3212
+ throw new Error('Placed layer ID must be in a GUID format (example: 20953ddb-9391-11ec-b4f1-c15674f50bc4)');
3213
+ }
3214
+
3171
3215
  const desc: SoLdDescriptor = {
3172
3216
  Idnt: placed.id,
3173
3217
  placed: placed.placed ?? placed.id,
@@ -3182,7 +3226,7 @@ addHandler(
3182
3226
  Trnf: placed.transform,
3183
3227
  nonAffineTransform: placed.nonAffineTransform ?? placed.transform,
3184
3228
  // quiltWarp: {} as any,
3185
- warp: encodeWarp(placed.warp || {}),
3229
+ warp: encodeWarp(getWarpFromPlacedLayer(placed)),
3186
3230
  'Sz ': {
3187
3231
  _name: '',
3188
3232
  _classID: 'Pnt ',
@@ -3480,7 +3524,10 @@ addHandler(
3480
3524
  const sizeOffset = writer.offset;
3481
3525
  writeSignature(writer, file.data ? 'liFD' : 'liFA');
3482
3526
  writeInt32(writer, version);
3483
- writePascalString(writer, file.id || '', 1);
3527
+ if (!file.id || typeof file.id !== 'string' || !/^[0-9a-f]{8}-([0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(file.id)) {
3528
+ throw new Error('Linked file ID must be in a GUID format (example: 20953ddb-9391-11ec-b4f1-c15674f50bc4)');
3529
+ }
3530
+ writePascalString(writer, file.id, 1);
3484
3531
  writeUnicodeStringWithPadding(writer, file.name || '');
3485
3532
  writeSignature(writer, file.type ? `${file.type} `.substring(0, 4) : ' ');
3486
3533
  writeSignature(writer, file.creator ? `${file.creator} `.substring(0, 4) : '\0\0\0\0');
package/src/psd.ts CHANGED
@@ -670,7 +670,7 @@ export interface SelectiveColorAdjustment {
670
670
  }
671
671
 
672
672
  export interface LinkedFile {
673
- id: string;
673
+ id: string; // must be in a GUID format (example: 20953ddb-9391-11ec-b4f1-c15674f50bc4)
674
674
  name: string;
675
675
  type?: string;
676
676
  creator?: string;
@@ -1150,7 +1150,7 @@ export interface PlacedLayerFilter {
1150
1150
  export type PlacedLayerType = 'unknown' | 'vector' | 'raster' | 'image stack';
1151
1151
 
1152
1152
  export interface PlacedLayer {
1153
- id: string; // id of linked image file (psd.linkedFiles)
1153
+ id: string; // id of linked image file (psd.linkedFiles), must be in a GUID format (example: 20953ddb-9391-11ec-b4f1-c15674f50bc4)
1154
1154
  placed?: string; // unique id
1155
1155
  type: PlacedLayerType;
1156
1156
  pageNumber?: number;
@@ -1160,11 +1160,11 @@ export interface PlacedLayer {
1160
1160
  frameCount?: number;
1161
1161
  transform: number[]; // x, y of 4 corners of the transform
1162
1162
  nonAffineTransform?: number[]; // x, y of 4 corners of the transform
1163
- width?: number;
1164
- height?: number;
1163
+ width?: number; // width of the linked image
1164
+ height?: number; // height of the linked image
1165
1165
  resolution?: UnitsValue;
1166
1166
  // antialias ?
1167
- warp?: Warp;
1167
+ warp?: Warp; // warp coordinates are relative to the linked image size
1168
1168
  crop?: number;
1169
1169
  comp?: number;
1170
1170
  compInfo?: { compID: number; originalCompID: number; };