ag-psd 15.2.0 → 15.3.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/CHANGELOG.md CHANGED
@@ -1,5 +1,8 @@
1
1
  # Changelog
2
2
 
3
+ ## v15.3.0
4
+ - Added support for smart layer puppet filter (`placedLayer.filter`)
5
+
3
6
  ## v15.2.0
4
7
  - Added handling missing `font` in text layer style by assuming first font on the list
5
8
 
@@ -864,6 +864,173 @@ addHandler('PlLd', hasKey('placedLayer'), function (reader, target, left) {
864
864
  var type = isQuilt ? 'quiltWarp' : 'warp';
865
865
  (0, descriptor_1.writeVersionAndDescriptor)(writer, '', type, encodeWarp(placed.warp || {}), type);
866
866
  });
867
+ function uint8ToFloat32(array) {
868
+ return new Float32Array(array.buffer.slice(array.byteOffset), 0, array.byteLength / 4);
869
+ }
870
+ function uint8ToUint32(array) {
871
+ return new Uint32Array(array.buffer.slice(array.byteOffset), 0, array.byteLength / 4);
872
+ }
873
+ function toUint8(array) {
874
+ return new Uint8Array(array.buffer, array.byteOffset, array.byteLength);
875
+ }
876
+ function arrayToPoints(array) {
877
+ var points = [];
878
+ for (var i = 0; i < array.length; i += 2) {
879
+ points.push({ x: array[i], y: array[i + 1] });
880
+ }
881
+ return points;
882
+ }
883
+ function pointsToArray(points) {
884
+ var array = [];
885
+ for (var i = 0; i < points.length; i++) {
886
+ array.push(points[i].x, points[i].y);
887
+ }
888
+ return array;
889
+ }
890
+ function uin8ToPoints(array) {
891
+ return arrayToPoints(uint8ToFloat32(array));
892
+ }
893
+ function hrznVrtcToPoint(desc) {
894
+ return {
895
+ x: (0, descriptor_1.parseUnits)(desc.Hrzn),
896
+ y: (0, descriptor_1.parseUnits)(desc.Vrtc),
897
+ };
898
+ }
899
+ function pointToHrznVrtc(point) {
900
+ return {
901
+ Hrzn: (0, descriptor_1.unitsValue)(point.x, 'x'),
902
+ Vrtc: (0, descriptor_1.unitsValue)(point.y, 'y'),
903
+ };
904
+ }
905
+ function parseFilterFX(desc) {
906
+ return {
907
+ enabled: desc.enab,
908
+ validAtPosition: desc.validAtPosition,
909
+ maskEnabled: desc.filterMaskEnable,
910
+ maskLinked: desc.filterMaskLinked,
911
+ maskExtendWithWhite: desc.filterMaskExtendWithWhite,
912
+ list: desc.filterFXList.map(function (f) { return ({
913
+ id: f.filterID,
914
+ name: f['Nm '],
915
+ opacity: (0, descriptor_1.parsePercent)(f.blendOptions.Opct),
916
+ blendMode: descriptor_1.BlnM.decode(f.blendOptions['Md ']),
917
+ enabled: f.enab,
918
+ hasOptions: f.hasoptions,
919
+ foregroundColor: (0, descriptor_1.parseColor)(f.FrgC),
920
+ backgroundColor: (0, descriptor_1.parseColor)(f.BckC),
921
+ filter: {
922
+ rigidType: f.Fltr.rigidType,
923
+ bounds: [
924
+ { x: f.Fltr.PuX0, y: f.Fltr.PuY0, },
925
+ { x: f.Fltr.PuX1, y: f.Fltr.PuY1, },
926
+ { x: f.Fltr.PuX2, y: f.Fltr.PuY2, },
927
+ { x: f.Fltr.PuX3, y: f.Fltr.PuY3, },
928
+ ],
929
+ puppetShapeList: f.Fltr.puppetShapeList.map(function (p) { return ({
930
+ rigidType: p.rigidType,
931
+ // TODO: VrsM
932
+ // TODO: VrsN
933
+ originalVertexArray: uin8ToPoints(p.originalVertexArray),
934
+ deformedVertexArray: uin8ToPoints(p.deformedVertexArray),
935
+ indexArray: Array.from(uint8ToUint32(p.indexArray)),
936
+ pinOffsets: arrayToPoints(p.pinOffsets),
937
+ posFinalPins: arrayToPoints(p.posFinalPins),
938
+ pinVertexIndices: p.pinVertexIndices,
939
+ selectedPin: p.selectedPin,
940
+ pinPosition: arrayToPoints(p.PinP),
941
+ pinRotation: p.PnRt,
942
+ pinOverlay: p.PnOv,
943
+ pinDepth: p.PnDp,
944
+ meshQuality: p.meshQuality,
945
+ meshExpansion: p.meshExpansion,
946
+ meshRigidity: p.meshRigidity,
947
+ imageResolution: p.imageResolution,
948
+ meshBoundaryPath: {
949
+ pathComponents: p.meshBoundaryPath.pathComponents.map(function (c) { return ({
950
+ shapeOperation: c.shapeOperation.split('.')[1],
951
+ paths: c.SbpL.map(function (t) { return ({
952
+ closed: t.Clsp,
953
+ points: t['Pts '].map(function (pt) { return ({
954
+ anchor: hrznVrtcToPoint(pt.Anch),
955
+ forward: hrznVrtcToPoint(pt['Fwd ']),
956
+ backward: hrznVrtcToPoint(pt['Bwd ']),
957
+ smooth: pt.Smoo,
958
+ }); }),
959
+ }); }),
960
+ }); }),
961
+ },
962
+ }); }),
963
+ },
964
+ }); }),
965
+ };
966
+ }
967
+ function serializeFilterFX(filter) {
968
+ return {
969
+ enab: filter.enabled,
970
+ validAtPosition: filter.validAtPosition,
971
+ filterMaskEnable: filter.maskEnabled,
972
+ filterMaskLinked: filter.maskLinked,
973
+ filterMaskExtendWithWhite: filter.maskExtendWithWhite,
974
+ filterFXList: filter.list.map(function (f) { return ({
975
+ 'Nm ': f.name,
976
+ blendOptions: {
977
+ Opct: (0, descriptor_1.unitsPercent)(f.opacity),
978
+ 'Md ': descriptor_1.BlnM.encode(f.blendMode),
979
+ },
980
+ enab: f.enabled,
981
+ hasoptions: f.hasOptions,
982
+ FrgC: (0, descriptor_1.serializeColor)(f.foregroundColor),
983
+ BckC: (0, descriptor_1.serializeColor)(f.backgroundColor),
984
+ Fltr: {
985
+ 'null': ['Ordn.Trgt'],
986
+ rigidType: f.filter.rigidType,
987
+ puppetShapeList: f.filter.puppetShapeList.map(function (p) { return ({
988
+ rigidType: p.rigidType,
989
+ VrsM: 1,
990
+ VrsN: 0,
991
+ originalVertexArray: toUint8(new Float32Array(pointsToArray(p.originalVertexArray))),
992
+ deformedVertexArray: toUint8(new Float32Array(pointsToArray(p.deformedVertexArray))),
993
+ indexArray: toUint8(new Uint32Array(p.indexArray)),
994
+ pinOffsets: pointsToArray(p.pinOffsets),
995
+ posFinalPins: pointsToArray(p.posFinalPins),
996
+ selectedPin: p.selectedPin,
997
+ pinVertexIndices: p.pinVertexIndices,
998
+ PinP: pointsToArray(p.pinPosition),
999
+ PnRt: p.pinRotation,
1000
+ PnOv: p.pinOverlay,
1001
+ PnDp: p.pinDepth,
1002
+ meshQuality: p.meshQuality,
1003
+ meshExpansion: p.meshExpansion,
1004
+ meshRigidity: p.meshRigidity,
1005
+ imageResolution: p.imageResolution,
1006
+ meshBoundaryPath: {
1007
+ pathComponents: p.meshBoundaryPath.pathComponents.map(function (c) { return ({
1008
+ shapeOperation: "shapeOperation.".concat(c.shapeOperation),
1009
+ SbpL: c.paths.map(function (path) { return ({
1010
+ Clsp: path.closed,
1011
+ 'Pts ': path.points.map(function (pt) { return ({
1012
+ Anch: pointToHrznVrtc(pt.anchor),
1013
+ 'Fwd ': pointToHrznVrtc(pt.forward),
1014
+ 'Bwd ': pointToHrznVrtc(pt.backward),
1015
+ Smoo: pt.smooth,
1016
+ }); }),
1017
+ }); }),
1018
+ }); }),
1019
+ },
1020
+ }); }),
1021
+ PuX0: f.filter.bounds[0].x,
1022
+ PuX1: f.filter.bounds[1].x,
1023
+ PuX2: f.filter.bounds[2].x,
1024
+ PuX3: f.filter.bounds[3].x,
1025
+ PuY0: f.filter.bounds[0].y,
1026
+ PuY1: f.filter.bounds[1].y,
1027
+ PuY2: f.filter.bounds[2].y,
1028
+ PuY3: f.filter.bounds[3].y,
1029
+ },
1030
+ filterID: f.id,
1031
+ }); }),
1032
+ };
1033
+ }
867
1034
  addHandler('SoLd', hasKey('placedLayer'), function (reader, target, left) {
868
1035
  if ((0, psdReader_1.readSignature)(reader) !== 'soLD')
869
1036
  throw new Error("Invalid SoLd type");
@@ -874,6 +1041,8 @@ addHandler('SoLd', hasKey('placedLayer'), function (reader, target, left) {
874
1041
  // console.log('SoLd', require('util').inspect(desc, false, 99, true));
875
1042
  // console.log('SoLd.warp', require('util').inspect(desc.warp, false, 99, true));
876
1043
  // console.log('SoLd.quiltWarp', require('util').inspect(desc.quiltWarp, false, 99, true));
1044
+ // desc.filterFX!.filterFXList[0].Fltr.puppetShapeList[0].meshBoundaryPath.pathComponents[0].SbpL[0]['Pts '] = [];
1045
+ // console.log('filterFX', require('util').inspect(desc.filterFX, false, 99, true));
877
1046
  target.placedLayer = {
878
1047
  id: desc.Idnt,
879
1048
  placed: desc.placed,
@@ -898,6 +1067,8 @@ addHandler('SoLd', hasKey('placedLayer'), function (reader, target, left) {
898
1067
  target.placedLayer.comp = desc.comp;
899
1068
  if (desc.compInfo)
900
1069
  target.placedLayer.compInfo = desc.compInfo;
1070
+ if (desc.filterFX)
1071
+ target.placedLayer.filter = parseFilterFX(desc.filterFX);
901
1072
  (0, psdReader_1.skipBytes)(reader, left()); // HACK
902
1073
  }, function (writer, target) {
903
1074
  var _a, _b;
@@ -908,6 +1079,8 @@ addHandler('SoLd', hasKey('placedLayer'), function (reader, target, left) {
908
1079
  Wdth: placed.width || 0,
909
1080
  Hght: placed.height || 0, // TODO: find size ?
910
1081
  }, Rslt: placed.resolution ? (0, descriptor_1.unitsValue)(placed.resolution, 'resolution') : { units: 'Density', value: 72 } });
1082
+ if (placed.filter)
1083
+ desc.filterFX = serializeFilterFX(placed.filter);
911
1084
  if (placed.warp && isQuiltWarp(placed.warp)) {
912
1085
  var quiltWarp = encodeWarp(placed.warp);
913
1086
  desc.quiltWarp = quiltWarp;
@@ -2019,4 +2192,85 @@ addHandler('tsly', hasKey('transparencyShapesLayer'), function (reader, target)
2019
2192
  (0, psdWriter_1.writeUint8)(writer, target.transparencyShapesLayer ? 1 : 0);
2020
2193
  (0, psdWriter_1.writeZeros)(writer, 3);
2021
2194
  });
2195
+ /*addHandler(
2196
+ 'FEid',
2197
+ hasKey('filterEffects'),
2198
+ (reader, _target) => {
2199
+ const version = readInt32(reader);
2200
+ if (version < 1 || version > 3) throw new Error(`Invalid filterEffects version ${version}`);
2201
+
2202
+ if (readUint32(reader)) throw new Error('filterEffects: 64 bit length is not supported');
2203
+ const length = readUint32(reader);
2204
+ const end = reader.offset + length;
2205
+
2206
+ while (reader.offset < end) {
2207
+ console.log('bytes to go', end - reader.offset, 'at', reader.offset.toString(16));
2208
+ //
2209
+ const id = readPascalString(reader, 1);
2210
+ const effectVersion = readInt32(reader);
2211
+ if (effectVersion !== 1) throw new Error(`Invalid filterEffect version ${effectVersion}`);
2212
+ if (readUint32(reader)) throw new Error('filterEffect: 64 bit length is not supported');
2213
+ const effectLength = readUint32(reader);
2214
+ const endOfEffect = reader.offset + effectLength;
2215
+ const top = readInt32(reader);
2216
+ const left = readInt32(reader);
2217
+ const bottom = readInt32(reader);
2218
+ const right = readInt32(reader);
2219
+ const depth = readInt32(reader);
2220
+ const maxChannels = readInt32(reader);
2221
+ const channels: any[] = [];
2222
+
2223
+ for (let i = 0; i < (maxChannels + 2); i++) {
2224
+ const exists = readInt32(reader);
2225
+ if (exists) {
2226
+ if (readUint32(reader)) throw new Error('filterEffect: 64 bit length is not supported');
2227
+ const channelLength = readUint32(reader);
2228
+ const compressionMode = readUint16(reader);
2229
+ const data = readBytes(reader, channelLength - 2);
2230
+ channels.push({ channelLength, compressionMode, data: data?.length + ' bytes' });
2231
+ // if (c < 3 || c == 25) e_ = _F.Cn(!0, rL, m, b.rect.F, b.rect.V, X, rp);
2232
+ // if (c == 0) _c.S = e_;
2233
+ // if (c == 1) _c.v = e_;
2234
+ // if (c == 2) _c.e = e_;
2235
+ // if (c == 25) _c.w = e_;
2236
+ } else {
2237
+ channels.push(undefined);
2238
+ }
2239
+ }
2240
+
2241
+ console.log('left at the end', endOfEffect - reader.offset);
2242
+ if (endOfEffect > reader.offset) {
2243
+ if (readUint8(reader)) {
2244
+ const compressionMode = readUint16(reader);
2245
+ const data = endOfEffect > reader.offset ? readBytes(reader, endOfEffect - reader.offset) : undefined;
2246
+ console.log('extra data', { compressionMode, data: data?.length + ' bytes' });
2247
+ } else {
2248
+ console.log('no extra');
2249
+ }
2250
+ }
2251
+
2252
+ console.log('effect', {
2253
+ id,
2254
+ effectVersion,
2255
+ effectLength,
2256
+ top,
2257
+ left,
2258
+ bottom,
2259
+ right,
2260
+ depth,
2261
+ maxChannels,
2262
+ channels,
2263
+ });
2264
+
2265
+ console.log('bytes left after effect', endOfEffect - reader.offset);
2266
+ // if (length % 4) skipBytes(reader, 4 - length % 4);
2267
+ }
2268
+
2269
+ console.log({ version, length });
2270
+ },
2271
+ (_writer, _target) => {
2272
+ },
2273
+ );
2274
+
2275
+ addHandlerAlias('FXid', 'FEid');*/
2022
2276
  //# sourceMappingURL=additionalInfo.js.map