ag-psd 34.2.2 → 34.2.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.
Files changed (64) hide show
  1. package/.v8-cache/v26.13.0-x64-310b2ce3/4511bacf +0 -0
  2. package/.v8-cache/v26.13.0-x64-310b2ce3/71df9cb3 +0 -0
  3. package/.v8-cache/v26.13.0-x64-310b2ce3/7ea06cea +0 -0
  4. package/.v8-cache/v26.13.0-x64-310b2ce3/932f0770 +0 -0
  5. package/.v8-cache/v26.13.0-x64-310b2ce3/a7e80865 +0 -0
  6. package/.v8-cache/v26.13.0-x64-310b2ce3/acc36e66 +0 -0
  7. package/.v8-cache/v26.13.0-x64-310b2ce3/b3c2fab7 +0 -0
  8. package/.v8-cache/v26.13.0-x64-310b2ce3/c314aece +0 -0
  9. package/.v8-cache/v26.13.0-x64-310b2ce3/dc86f000 +0 -0
  10. package/.v8-cache/v26.13.0-x64-310b2ce3/e03e2acd +0 -0
  11. package/.v8-cache/v26.13.0-x64-310b2ce3/e6735171 +0 -0
  12. package/CHANGELOG.md +9 -0
  13. package/README.md +23 -4
  14. package/dist/abr.js +12 -5
  15. package/dist/abr.js.map +1 -1
  16. package/dist/additionalInfo.js +1 -1
  17. package/dist/additionalInfo.js.map +1 -1
  18. package/dist/bundle.js +256 -78
  19. package/dist/descriptor.js +51 -16
  20. package/dist/descriptor.js.map +1 -1
  21. package/dist/engineData.d.ts +1 -0
  22. package/dist/engineData.js +4 -3
  23. package/dist/engineData.js.map +1 -1
  24. package/dist/helpers.d.ts +4 -0
  25. package/dist/helpers.js +11 -0
  26. package/dist/helpers.js.map +1 -1
  27. package/dist/imageResources.js +1 -1
  28. package/dist/imageResources.js.map +1 -1
  29. package/dist/psd.d.ts +1 -1
  30. package/dist/psdReader.d.ts +1 -0
  31. package/dist/psdReader.js +170 -46
  32. package/dist/psdReader.js.map +1 -1
  33. package/dist/text.js +5 -5
  34. package/dist/text.js.map +1 -1
  35. package/dist-es/abr.js +12 -5
  36. package/dist-es/abr.js.map +1 -1
  37. package/dist-es/additionalInfo.js +1 -1
  38. package/dist-es/additionalInfo.js.map +1 -1
  39. package/dist-es/descriptor.js +51 -16
  40. package/dist-es/descriptor.js.map +1 -1
  41. package/dist-es/engineData.d.ts +1 -0
  42. package/dist-es/engineData.js +4 -3
  43. package/dist-es/engineData.js.map +1 -1
  44. package/dist-es/helpers.d.ts +4 -0
  45. package/dist-es/helpers.js +11 -0
  46. package/dist-es/helpers.js.map +1 -1
  47. package/dist-es/imageResources.js +1 -1
  48. package/dist-es/imageResources.js.map +1 -1
  49. package/dist-es/psd.d.ts +1 -1
  50. package/dist-es/psdReader.d.ts +1 -0
  51. package/dist-es/psdReader.js +170 -46
  52. package/dist-es/psdReader.js.map +1 -1
  53. package/dist-es/text.js +5 -5
  54. package/dist-es/text.js.map +1 -1
  55. package/package.json +1 -1
  56. package/src/abr.ts +11 -5
  57. package/src/additionalInfo.ts +2 -2
  58. package/src/descriptor.ts +55 -19
  59. package/src/engineData.ts +1 -1
  60. package/src/helpers.ts +11 -0
  61. package/src/imageResources.ts +2 -2
  62. package/src/psd.ts +1 -1
  63. package/src/psdReader.ts +178 -43
  64. package/src/text.ts +5 -5
package/dist/bundle.js CHANGED
@@ -3,6 +3,7 @@
3
3
  Object.defineProperty(exports, "__esModule", { value: true });
4
4
  exports.readAbr = readAbr;
5
5
  const descriptor_1 = require("./descriptor");
6
+ const helpers_1 = require("./helpers");
6
7
  const psdReader_1 = require("./psdReader");
7
8
  const dynamicsControl = ['off', 'fade', 'pen pressure', 'pen tilt', 'stylus wheel', 'initial direction', 'direction', 'initial rotation', 'rotation'];
8
9
  const dynamicBrushShapeShapes = ['round point', 'round blunt', 'round curve', 'round angle', 'round fan', 'flat point', 'flat blunt', 'flat curve', 'flat angle', 'flat fan'];
@@ -20,7 +21,7 @@ const toBrushType = new Map([
20
21
  ]);
21
22
  function parseDynamics(desc) {
22
23
  return {
23
- control: dynamicsControl[desc.bVTy],
24
+ control: (0, helpers_1.arrayItem)(dynamicsControl, desc.bVTy),
24
25
  steps: desc.fStp,
25
26
  jitter: (0, descriptor_1.parsePercent)(desc.jitter),
26
27
  minimum: (0, descriptor_1.parsePercent)(desc['Mnm ']),
@@ -58,7 +59,7 @@ function parseBrushShape(desc) {
58
59
  case 'dBrush':
59
60
  return {
60
61
  type: 'dynamic',
61
- shape: dynamicBrushShapeShapes[desc['Shp ']],
62
+ shape: (0, helpers_1.arrayItem)(dynamicBrushShapeShapes, desc['Shp ']),
62
63
  angle: (0, descriptor_1.parseAngle)(desc.Angl),
63
64
  size: (0, descriptor_1.parseUnitsToNumber)(desc.Dmtr, 'Pixels'),
64
65
  density: (0, descriptor_1.parsePercent)(desc.Dnst),
@@ -73,9 +74,9 @@ function parseBrushShape(desc) {
73
74
  flipY: desc.flipY,
74
75
  };
75
76
  case 'dTips': {
76
- return Object.assign(Object.assign({ type: 'tips', angle: (0, descriptor_1.parseAngle)(desc.Angl), size: (0, descriptor_1.parseUnitsToNumber)(desc.Dmtr, 'Pixels'), shape: dynamicBrushShapeShapes[desc['Shp ']], physics: desc.physics, spacing: (0, descriptor_1.parsePercent)(desc.Spcn), spacingOn: desc.Intr, flipX: desc.flipX, flipY: desc.flipY,
77
+ return Object.assign(Object.assign({ type: 'tips', angle: (0, descriptor_1.parseAngle)(desc.Angl), size: (0, descriptor_1.parseUnitsToNumber)(desc.Dmtr, 'Pixels'), shape: (0, helpers_1.arrayItem)(dynamicBrushShapeShapes, desc['Shp ']), physics: desc.physics, spacing: (0, descriptor_1.parsePercent)(desc.Spcn), spacingOn: desc.Intr, flipX: desc.flipX, flipY: desc.flipY,
77
78
  // tips:
78
- tipsType: tipsBrushShapeShapes[desc.dtipsType], tipsLengthRatio: (0, descriptor_1.parsePercent)(desc.dtipsLengthRatio), tipsHardness: (0, descriptor_1.parsePercent)(desc.dtipsHardness) }, ((desc.dtipsGridSize && desc.dtipsErodibleTipHeightMap) ? {
79
+ tipsType: (0, helpers_1.arrayItem)(tipsBrushShapeShapes, desc.dtipsType), tipsLengthRatio: (0, descriptor_1.parsePercent)(desc.dtipsLengthRatio), tipsHardness: (0, descriptor_1.parsePercent)(desc.dtipsHardness) }, ((desc.dtipsGridSize && desc.dtipsErodibleTipHeightMap) ? {
79
80
  tipsGridSize: desc.dtipsGridSize,
80
81
  tipsErodibleTipHeightMap: parseHeightmap(desc.dtipsErodibleTipHeightMap),
81
82
  } : {})), {
@@ -104,7 +105,13 @@ function parseHeightmap(array) {
104
105
  // descriptor can't make the parser read (and allocate) data out into the rest of the file
105
106
  function readSectionDescriptor(reader, end) {
106
107
  const sectionReader = (0, psdReader_1.createReader)(reader.view.buffer, reader.view.byteOffset + reader.offset, end - reader.offset);
107
- return (0, descriptor_1.readVersionAndDescriptor)(sectionReader, true);
108
+ sectionReader.totalMemoryLimit = reader.totalMemoryLimit; // share the budget with the section reader
109
+ try {
110
+ return (0, descriptor_1.readVersionAndDescriptor)(sectionReader, true);
111
+ }
112
+ finally {
113
+ reader.totalMemoryLimit = sectionReader.totalMemoryLimit; // carry section consumption back to the shared budget
114
+ }
108
115
  }
109
116
  function readAbr(buffer, options = {}) {
110
117
  const reader = (0, psdReader_1.createReader)(buffer.buffer, buffer.byteOffset, buffer.byteLength);
@@ -507,7 +514,7 @@ function parseBrush(brush) {
507
514
  return b;
508
515
  }
509
516
 
510
- },{"./descriptor":5,"./psdReader":14}],2:[function(require,module,exports){
517
+ },{"./descriptor":5,"./helpers":9,"./psdReader":14}],2:[function(require,module,exports){
511
518
  "use strict";
512
519
  var __rest = (this && this.__rest) || function (s, e) {
513
520
  var t = {};
@@ -3058,7 +3065,7 @@ addHandler('SoLd', target => !!target.placedLayer && !target.placedLayer.externa
3058
3065
  target.placedLayer = {
3059
3066
  id: desc.Idnt,
3060
3067
  placed: desc.placed,
3061
- type: placedLayerTypes[desc.Type],
3068
+ type: (0, helpers_1.arrayItem)(placedLayerTypes, desc.Type),
3062
3069
  pageNumber: desc.PgNm,
3063
3070
  totalPages: desc.totalPages,
3064
3071
  frameStep: (0, descriptor_1.frac)(desc.frameStep),
@@ -5332,7 +5339,7 @@ function getTypeByKey(key, value, root, parent) {
5332
5339
  return 'long';
5333
5340
  }
5334
5341
  else {
5335
- return fieldToType[key];
5342
+ return (0, helpers_1.ownValue)(fieldToType, key);
5336
5343
  }
5337
5344
  }
5338
5345
  function readAsciiStringOrClassId(reader) {
@@ -5355,11 +5362,13 @@ function writeAsciiStringOrClassId(writer, value) {
5355
5362
  }
5356
5363
  function readDescriptorStructure(reader, includeClass, depth = 0) {
5357
5364
  const struct = readClassStructure(reader);
5365
+ (0, psdReader_1.consumeMemory)(reader, engineData_1.NODE_COST);
5358
5366
  const object = includeClass ? { _name: struct.name, _classID: struct.classID } : {};
5359
5367
  // console.log('>> ', struct);
5360
5368
  const itemsCount = (0, psdReader_1.readUint32)(reader);
5361
5369
  for (let i = 0; i < itemsCount; i++) {
5362
5370
  const key = readAsciiStringOrClassId(reader);
5371
+ (0, psdReader_1.consumeMemory)(reader, key.length);
5363
5372
  const type = (0, psdReader_1.readSignature)(reader);
5364
5373
  // console.log(`> '${key}' '${type}'`);
5365
5374
  const data = readOSType(reader, type, includeClass, depth + 1);
@@ -5389,9 +5398,9 @@ function writeDescriptorStructure(writer, name, classId, value, root) {
5389
5398
  for (const key of keys) {
5390
5399
  let type = getTypeByKey(key, value[key], root, value);
5391
5400
  let childRoot = root;
5392
- let extType = fieldToExtType[key];
5401
+ let extType = (0, helpers_1.ownValue)(fieldToExtType, key);
5393
5402
  if (classId === exports.cameraRawFilterClassID) {
5394
- type = (_a = exports.cameraRawFilterKeyTypes[key]) !== null && _a !== void 0 ? _a : 'long'; // other settings are integer levels
5403
+ type = (_a = (0, helpers_1.ownValue)(exports.cameraRawFilterKeyTypes, key)) !== null && _a !== void 0 ? _a : 'long'; // other settings are integer levels
5395
5404
  childRoot = exports.cameraRawFilterClassID; // lists (curves) contain integer levels
5396
5405
  }
5397
5406
  if (key === 'bounds' && root === 'text') {
@@ -5523,9 +5532,13 @@ function writeDescriptorStructure(writer, name, classId, value, root) {
5523
5532
  // descriptors nest via lists (VlLs) and nested descriptors (Objc); a crafted file can nest them
5524
5533
  // arbitrarily deep, which would overflow the call stack (RangeError), so the depth is capped
5525
5534
  const maxDescriptorDepth = 1000;
5535
+ // Parsed values (and lengths of strings) are charged against `totalMemoryLimit`, the charge is not returned when
5536
+ // parsed descriptor is discarded. Crafted files can declare lists and descriptors with millions of few byte items,
5537
+ // building an object graph many times larger than the file.
5526
5538
  function readOSType(reader, type, includeClass, depth) {
5527
5539
  if (depth > maxDescriptorDepth)
5528
5540
  throw new Error('Descriptor is nested too deeply');
5541
+ (0, psdReader_1.consumeMemory)(reader, engineData_1.NODE_COST);
5529
5542
  switch (type) {
5530
5543
  case 'obj ': // Reference
5531
5544
  return readReferenceStructure(reader);
@@ -5560,11 +5573,15 @@ function readOSType(reader, type, includeClass, depth) {
5560
5573
  throw new Error(`Invalid units: ${units}`);
5561
5574
  return { units: unit, value };
5562
5575
  }
5563
- case 'TEXT': // String
5564
- return (0, psdReader_1.readUnicodeString)(reader);
5576
+ case 'TEXT': { // String
5577
+ const value = (0, psdReader_1.readUnicodeString)(reader);
5578
+ (0, psdReader_1.consumeMemory)(reader, value.length * 2);
5579
+ return value;
5580
+ }
5565
5581
  case 'enum': { // Enumerated
5566
5582
  const enumType = readAsciiStringOrClassId(reader);
5567
5583
  const value = readAsciiStringOrClassId(reader);
5584
+ (0, psdReader_1.consumeMemory)(reader, enumType.length + value.length);
5568
5585
  return `${enumType}.${value}`;
5569
5586
  }
5570
5587
  case 'long': // Integer
@@ -5581,7 +5598,9 @@ function readOSType(reader, type, includeClass, depth) {
5581
5598
  return readClassStructure(reader);
5582
5599
  case 'alis': { // Alias
5583
5600
  const length = (0, psdReader_1.readInt32)(reader);
5584
- return (0, psdReader_1.readAsciiString)(reader, length);
5601
+ const value = (0, psdReader_1.readAsciiString)(reader, length);
5602
+ (0, psdReader_1.consumeMemory)(reader, value.length);
5603
+ return value;
5585
5604
  }
5586
5605
  case 'tdta': { // Raw Data
5587
5606
  const length = (0, psdReader_1.readInt32)(reader);
@@ -5602,6 +5621,7 @@ function readOSType(reader, type, includeClass, depth) {
5602
5621
  (0, psdReader_1.readSignature)(reader); // UnFl
5603
5622
  (0, psdReader_1.readSignature)(reader); // units ? '#Pxl'
5604
5623
  const valuesCount = (0, psdReader_1.readInt32)(reader);
5624
+ (0, psdReader_1.consumeMemory)(reader, engineData_1.NODE_COST + type1.length + Math.max(0, valuesCount) * 8);
5605
5625
  const values = [];
5606
5626
  for (let j = 0; j < valuesCount; j++) {
5607
5627
  values.push((0, psdReader_1.readFloat64)(reader));
@@ -5616,6 +5636,7 @@ function readOSType(reader, type, includeClass, depth) {
5616
5636
  /*const pathSize =*/ (0, psdReader_1.readInt32LE)(reader); // the same as length
5617
5637
  const charsCount = (0, psdReader_1.readInt32LE)(reader);
5618
5638
  const path = (0, psdReader_1.readUnicodeStringWithLengthLE)(reader, charsCount);
5639
+ (0, psdReader_1.consumeMemory)(reader, path.length * 2);
5619
5640
  return { sig, path };
5620
5641
  }
5621
5642
  default:
@@ -5649,9 +5670,9 @@ function writeOSType(writer, type, value, key, extType, root) {
5649
5670
  throw new Error(`Invalid list value: ${JSON.stringify(value)}, key: ${key}`);
5650
5671
  (0, psdWriter_1.writeInt32)(writer, value.length);
5651
5672
  for (let i = 0; i < value.length; i++) {
5652
- const type = root === exports.cameraRawFilterClassID ? 'long' : ((_a = fieldToArrayType[key]) !== null && _a !== void 0 ? _a : guessOSType(value[i]));
5673
+ const type = root === exports.cameraRawFilterClassID ? 'long' : ((_a = (0, helpers_1.ownValue)(fieldToArrayType, key)) !== null && _a !== void 0 ? _a : guessOSType(value[i]));
5653
5674
  (0, psdWriter_1.writeSignature)(writer, type || 'long');
5654
- writeOSType(writer, type || 'long', value[i], `${key}[]`, fieldToArrayExtType[key], root);
5675
+ writeOSType(writer, type || 'long', value[i], `${key}[]`, (0, helpers_1.ownValue)(fieldToArrayExtType, key), root);
5655
5676
  if (writer.logMissingFeatures && !type)
5656
5677
  writer.log(`Missing descriptor array type for: '${key}' in`, value);
5657
5678
  }
@@ -5727,7 +5748,7 @@ function writeOSType(writer, type, value, key, extType, root) {
5727
5748
  case 'ObAr': { // Object array
5728
5749
  (0, psdWriter_1.writeInt32)(writer, 16); // version
5729
5750
  (0, psdWriter_1.writeUnicodeStringWithPadding)(writer, ''); // name
5730
- const type = ObArTypes[key];
5751
+ const type = (0, helpers_1.ownValue)(ObArTypes, key);
5731
5752
  if (!type)
5732
5753
  throw new Error(`Not implemented ObArType for: ${key}`);
5733
5754
  writeAsciiStringOrClassId(writer, type);
@@ -5766,11 +5787,15 @@ function readReferenceStructure(reader) {
5766
5787
  const items = [];
5767
5788
  for (let i = 0; i < itemsCount; i++) {
5768
5789
  const type = (0, psdReader_1.readSignature)(reader);
5790
+ (0, psdReader_1.consumeMemory)(reader, engineData_1.NODE_COST); // see readOSType
5769
5791
  switch (type) {
5770
- case 'prop': // Property
5792
+ case 'prop': { // Property
5771
5793
  readClassStructure(reader);
5772
- items.push(readAsciiStringOrClassId(reader));
5794
+ const value = readAsciiStringOrClassId(reader);
5795
+ (0, psdReader_1.consumeMemory)(reader, value.length);
5796
+ items.push(value);
5773
5797
  break;
5798
+ }
5774
5799
  case 'Clss': // Class
5775
5800
  items.push(readClassStructure(reader));
5776
5801
  break;
@@ -5778,6 +5803,7 @@ function readReferenceStructure(reader) {
5778
5803
  readClassStructure(reader);
5779
5804
  const typeID = readAsciiStringOrClassId(reader);
5780
5805
  const enumValue = readAsciiStringOrClassId(reader);
5806
+ (0, psdReader_1.consumeMemory)(reader, typeID.length + enumValue.length);
5781
5807
  items.push(`${typeID}.${enumValue}`);
5782
5808
  break;
5783
5809
  }
@@ -5789,10 +5815,13 @@ function readReferenceStructure(reader) {
5789
5815
  case 'indx': // Index
5790
5816
  items.push((0, psdReader_1.readInt32)(reader));
5791
5817
  break;
5792
- case 'name': // Name
5818
+ case 'name': { // Name
5793
5819
  readClassStructure(reader);
5794
- items.push((0, psdReader_1.readUnicodeString)(reader));
5820
+ const value = (0, psdReader_1.readUnicodeString)(reader);
5821
+ (0, psdReader_1.consumeMemory)(reader, value.length * 2);
5822
+ items.push(value);
5795
5823
  break;
5824
+ }
5796
5825
  default:
5797
5826
  throw new Error(`Invalid descriptor reference type: ${type}`);
5798
5827
  }
@@ -5872,6 +5901,7 @@ function writeReferenceStructure(writer, _key, items) {
5872
5901
  function readClassStructure(reader) {
5873
5902
  const name = (0, psdReader_1.readUnicodeString)(reader);
5874
5903
  const classID = readAsciiStringOrClassId(reader);
5904
+ (0, psdReader_1.consumeMemory)(reader, name.length * 2 + classID.length);
5875
5905
  return { name, classID };
5876
5906
  }
5877
5907
  function writeClassStructure(writer, name, classID) {
@@ -5882,9 +5912,21 @@ function readVersionAndDescriptor(reader, includeClass = false) {
5882
5912
  const version = (0, psdReader_1.readUint32)(reader);
5883
5913
  if (version !== 16)
5884
5914
  throw new Error(`Invalid descriptor version: ${version}`);
5885
- const desc = readDescriptorStructure(reader, includeClass);
5886
- // console.log(require('util').inspect(desc, false, 99, true));
5887
- return desc;
5915
+ // descriptor can't be read past the end of its section, otherwise many small sections could all parse the same
5916
+ // large block of data from the rest of the file (the reader returns to the section end after reading a section),
5917
+ // the view is shortened with the same start, so the offsets stay the same
5918
+ const view = reader.view;
5919
+ const end = reader.sectionEnd;
5920
+ if (end !== undefined && end < view.byteLength)
5921
+ reader.view = new DataView(view.buffer, view.byteOffset, end);
5922
+ try {
5923
+ const desc = readDescriptorStructure(reader, includeClass);
5924
+ // console.log(require('util').inspect(desc, false, 99, true));
5925
+ return desc;
5926
+ }
5927
+ finally {
5928
+ reader.view = view;
5929
+ }
5888
5930
  }
5889
5931
  function writeVersionAndDescriptor(writer, name, classID, descriptor, root = '') {
5890
5932
  (0, psdWriter_1.writeUint32)(writer, 16); // version
@@ -7512,6 +7554,7 @@ function writeEffects(writer, effects) {
7512
7554
  },{"./helpers":9,"./psdReader":14,"./psdWriter":15}],7:[function(require,module,exports){
7513
7555
  "use strict";
7514
7556
  Object.defineProperty(exports, "__esModule", { value: true });
7557
+ exports.NODE_COST = void 0;
7515
7558
  exports.setProperty = setProperty;
7516
7559
  exports.parseEngineData = parseEngineData;
7517
7560
  exports.serializeEngineData = serializeEngineData;
@@ -7535,7 +7578,7 @@ function setProperty(target, key, value) {
7535
7578
  // estimated cost of one parsed node (array/object/value) with V8 overhead, charged against
7536
7579
  // `totalMemoryLimit` so malformed data of single-byte tokens (e.g. '[[[[...') can't build an
7537
7580
  // unbounded object graph that bypasses the limit
7538
- const NODE_COST = 64;
7581
+ exports.NODE_COST = 64;
7539
7582
  function parseEngineData(data, reader) {
7540
7583
  let index = 0;
7541
7584
  function skipWhitespace() {
@@ -7595,7 +7638,7 @@ function parseEngineData(data, reader) {
7595
7638
  function pushValue(value) {
7596
7639
  if (!stack.length)
7597
7640
  throw new Error('Invalid data');
7598
- (0, psdReader_1.consumeMemory)(reader, NODE_COST);
7641
+ (0, psdReader_1.consumeMemory)(reader, exports.NODE_COST);
7599
7642
  const top = stack[stack.length - 1];
7600
7643
  if (typeof top === 'string') {
7601
7644
  const target = stack[stack.length - 2];
@@ -7623,7 +7666,7 @@ function parseEngineData(data, reader) {
7623
7666
  }
7624
7667
  }
7625
7668
  else if (top && typeof top === 'object') {
7626
- (0, psdReader_1.consumeMemory)(reader, NODE_COST + name.length * 2);
7669
+ (0, psdReader_1.consumeMemory)(reader, exports.NODE_COST + name.length * 2);
7627
7670
  stack.push(name);
7628
7671
  }
7629
7672
  else {
@@ -8328,6 +8371,8 @@ exports.offsetForChannel = offsetForChannel;
8328
8371
  exports.normalizeLabAB = normalizeLabAB;
8329
8372
  exports.denormalizeLabAB = denormalizeLabAB;
8330
8373
  exports.clamp = clamp;
8374
+ exports.arrayItem = arrayItem;
8375
+ exports.ownValue = ownValue;
8331
8376
  exports.hasAlpha = hasAlpha;
8332
8377
  exports.cmykToRgbValues = cmykToRgbValues;
8333
8378
  exports.rgbToCmykValues = rgbToCmykValues;
@@ -8463,6 +8508,15 @@ function denormalizeLabAB(value, scale = 1) {
8463
8508
  function clamp(value, min, max) {
8464
8509
  return value < min ? min : (value > max ? max : value);
8465
8510
  }
8511
+ // index read from a file can be any type (e.g. string '__proto__' or a list coerced to 'length'),
8512
+ // only integer indexes within the array return an item, so inherited properties are never returned
8513
+ function arrayItem(array, index) {
8514
+ return (typeof index === 'number' && Number.isInteger(index) && index >= 0 && index < array.length) ? array[index] : undefined;
8515
+ }
8516
+ // key read from a file can be a name of an inherited property (e.g. 'constructor' or 'toString')
8517
+ function ownValue(dict, key) {
8518
+ return Object.prototype.hasOwnProperty.call(dict, key) ? dict[key] : undefined;
8519
+ }
8466
8520
  function hasAlpha(data) {
8467
8521
  const size = data.width * data.height * 4;
8468
8522
  const max = data.data instanceof Float32Array ? 1 : (data.data instanceof Uint16Array ? 0xffff : 0xff);
@@ -9744,7 +9798,7 @@ target => target.onionSkins !== undefined, (reader, target) => {
9744
9798
  frameSpacing: desc.Spcn,
9745
9799
  minOpacity: desc.minOpacity / 100,
9746
9800
  maxOpacity: desc.maxOpacity / 100,
9747
- blendMode: onionSkinsBlendModes[desc.BlnM] || 'normal',
9801
+ blendMode: (0, helpers_1.arrayItem)(onionSkinsBlendModes, desc.BlnM) || 'normal',
9748
9802
  };
9749
9803
  }, (writer, target) => {
9750
9804
  const onionSkins = target.onionSkins;
@@ -12747,7 +12801,8 @@ function readImageData(reader, psd) {
12747
12801
  const { width, height } = psd;
12748
12802
  if (exports.supportedColorModes.indexOf(psd.colorMode) === -1)
12749
12803
  throw new Error(`Color mode not supported: ${psd.colorMode}`);
12750
- if (compression !== 0 /* Compression.RawData */ && compression !== 1 /* Compression.RleCompressed */)
12804
+ if (compression !== 0 /* Compression.RawData */ && compression !== 1 /* Compression.RleCompressed */ &&
12805
+ compression !== 2 /* Compression.ZipWithoutPrediction */ && compression !== 3 /* Compression.ZipWithPrediction */)
12751
12806
  throw new Error(`Compression type not supported: ${compression}`);
12752
12807
  const imageData = createImageDataBitDepth(width, height, bitsPerChannel, 4, reader.totalMemoryLimit);
12753
12808
  if (reader.totalMemoryLimit !== undefined)
@@ -12764,7 +12819,7 @@ function readImageData(reader, psd) {
12764
12819
  }
12765
12820
  else {
12766
12821
  bytes = new Uint8Array(rowBytes * height);
12767
- readDataRLE(reader, { data: bytes, width: rowBytes, height }, rowBytes, height, 8, 1, [0], reader.large);
12822
+ readCompositeChannels(reader, compression, { data: bytes, width: rowBytes, height }, 8, 1, [0]);
12768
12823
  }
12769
12824
  (0, helpers_1.decodeBitmap)(bytes, imageData.data, width, height);
12770
12825
  break;
@@ -12853,10 +12908,74 @@ function readCompositeChannels(reader, compression, pixelData, bitsPerChannel, s
12853
12908
  else if (compression === 1 /* Compression.RleCompressed */) {
12854
12909
  readDataRLE(reader, pixelData, pixelData.width, pixelData.height, bitsPerChannel, step, offsets, reader.large);
12855
12910
  }
12911
+ else if (compression === 2 /* Compression.ZipWithoutPrediction */ || compression === 3 /* Compression.ZipWithPrediction */) {
12912
+ readCompositeChannelsZip(reader, pixelData, bitsPerChannel, step, offsets, compression === 3 /* Compression.ZipWithPrediction */);
12913
+ }
12856
12914
  else {
12857
12915
  throw new Error(`Compression not supported: ${compression}`);
12858
12916
  }
12859
12917
  }
12918
+ // Zip compressed composite image data has no lengths of channels. Applications that write it (Photoshop writes RLE)
12919
+ // store each channel as a separate zlib stream, like channels of layers, or all the channels in one stream. Reads all
12920
+ // the remaining data, the composite image is the last section of the file.
12921
+ function readCompositeChannelsZip(reader, pixelData, bitsPerChannel, step, offsets, prediction) {
12922
+ const { width, height } = pixelData;
12923
+ const bpv = bytesPerValue(bitsPerChannel);
12924
+ const channelSize = width * height * bpv;
12925
+ verifyArrayType(pixelData.data, bpv);
12926
+ const input = readBytes(reader, Math.max(0, reader.view.byteLength - reader.offset));
12927
+ consumeMemory(reader, channelSize);
12928
+ try {
12929
+ const channel = new Uint8Array(channelSize);
12930
+ for (let i = 0, position = 0; i < offsets.length && position < input.length; i++) {
12931
+ if (i)
12932
+ channel.fill(0);
12933
+ const stream = inflateZlibStream(input.subarray(position), channel);
12934
+ if (i === 0 && stream.overflow) {
12935
+ readCompositeChannelsZipSingleStream(reader, input, pixelData, bpv, step, offsets, prediction);
12936
+ return;
12937
+ }
12938
+ if (stream.overflow) {
12939
+ warnOrThrow(reader, zipOverflow.message);
12940
+ }
12941
+ else if (!stream.complete) {
12942
+ warnOrThrow(reader, 'Invalid zip data: unexpected end of data');
12943
+ }
12944
+ if (offsets[i] >= 0)
12945
+ writeZipChannel(channel, pixelData, width, height, bpv, step, offsets[i], prediction);
12946
+ position += stream.length;
12947
+ }
12948
+ }
12949
+ finally {
12950
+ recoverMemory(reader, channelSize);
12951
+ }
12952
+ }
12953
+ // all the channels in one zlib stream, one after another
12954
+ function readCompositeChannelsZipSingleStream(reader, input, pixelData, bpv, step, offsets, prediction) {
12955
+ const { width, height } = pixelData;
12956
+ const channelSize = width * height * bpv;
12957
+ const size = channelSize * offsets.length;
12958
+ consumeMemory(reader, size);
12959
+ try {
12960
+ const channels = new Uint8Array(size);
12961
+ const stream = inflateZlibStream(input, channels);
12962
+ if (stream.overflow) {
12963
+ warnOrThrow(reader, zipOverflow.message);
12964
+ }
12965
+ else if (!stream.complete) {
12966
+ warnOrThrow(reader, 'Invalid zip data: unexpected end of data');
12967
+ }
12968
+ for (let i = 0; i < offsets.length; i++) {
12969
+ if (offsets[i] < 0)
12970
+ continue;
12971
+ const channel = channels.subarray(i * channelSize, (i + 1) * channelSize);
12972
+ writeZipChannel(channel, pixelData, width, height, bpv, step, offsets[i], prediction);
12973
+ }
12974
+ }
12975
+ finally {
12976
+ recoverMemory(reader, size);
12977
+ }
12978
+ }
12860
12979
  // un-premultiplies colors from white matte, used for composite image of documents with global alpha
12861
12980
  function removeWhiteMatte({ data, width, height }) {
12862
12981
  const size = width * height * 4;
@@ -13045,66 +13164,117 @@ function inflateLimited(compressed, expectedSize, reader, raw = false) {
13045
13164
  }
13046
13165
  return output;
13047
13166
  }
13167
+ // Inflates the zlib stream at the start of `input` into `output`. Returns the length of the stream in `input`
13168
+ // (header and checksum included) and whether the stream ended within `input` without exceeding `output`.
13169
+ // The data is inflated as raw deflate data, because in zlib mode pako continues into the stream that follows as if
13170
+ // it was part of the same data. The checksum is not verified.
13171
+ function inflateZlibStream(input, output) {
13172
+ const cmf = input[0], flg = input[1];
13173
+ // deflate compression method, valid header check bits, no preset dictionary
13174
+ if (input.length < 2 || (cmf & 0x0f) !== 8 || ((cmf << 8) | flg) % 31 !== 0 || (flg & 0x20) !== 0) {
13175
+ throw new Error('Invalid zip data: incorrect header check');
13176
+ }
13177
+ let written = 0;
13178
+ let overflow = false;
13179
+ let ended = false;
13180
+ let deflateLength = 0;
13181
+ const inflator = new pako_1.Inflate({ chunkSize: Math.max(1024, Math.min(output.length, 0x10000)), raw: true });
13182
+ inflator.onData = (chunk) => {
13183
+ const data = chunk;
13184
+ const count = Math.min(data.length, output.length - written);
13185
+ if (count > 0)
13186
+ output.set(count === data.length ? data : data.subarray(0, count), written);
13187
+ written += count;
13188
+ if (count < data.length) {
13189
+ overflow = true;
13190
+ throw zipOverflow;
13191
+ }
13192
+ };
13193
+ inflator.onEnd = (status) => {
13194
+ var _a, _b, _c;
13195
+ inflator.err = status;
13196
+ inflator.msg = ((_a = inflator.strm) === null || _a === void 0 ? void 0 : _a.msg) || '';
13197
+ ended = true;
13198
+ deflateLength = (_c = (_b = inflator.strm) === null || _b === void 0 ? void 0 : _b.next_in) !== null && _c !== void 0 ? _c : 0;
13199
+ };
13200
+ try {
13201
+ inflator.push(input.subarray(2), true);
13202
+ }
13203
+ catch (e) {
13204
+ if (e !== zipOverflow)
13205
+ throw e;
13206
+ }
13207
+ if (overflow)
13208
+ return { length: input.length, complete: false, overflow };
13209
+ if (inflator.err)
13210
+ throw new Error(`Invalid zip data: ${inflator.msg}`);
13211
+ if (!ended)
13212
+ return { length: input.length, complete: false, overflow }; // data ends before the stream does
13213
+ return { length: Math.min(input.length, 2 + deflateLength + 4), complete: true, overflow };
13214
+ }
13048
13215
  function readDataZip(compressed, pixelData, width, height, bitDepth, step, offset, prediction, reader) {
13049
13216
  const bpv = bytesPerValue(bitDepth);
13050
13217
  if (!pixelData || offset < 0 || offset >= step)
13051
13218
  return;
13052
- const data = pixelData.data;
13053
- verifyArrayType(data, bpv);
13219
+ verifyArrayType(pixelData.data, bpv);
13054
13220
  const expectedSize = width * height * bpv;
13055
13221
  if (reader)
13056
13222
  consumeMemory(reader, expectedSize);
13057
13223
  try {
13058
- const decompressed = inflateLimited(compressed, expectedSize, reader);
13059
- if (bpv === 1) {
13060
- for (let y = 0, i = 0; y < height; y++) {
13061
- let prev = 0;
13062
- for (let x = 0, p = offset + y * width * step; x < width; x++, i++, p += step) {
13063
- let v = decompressed[i];
13064
- if (prediction)
13065
- v = prev = (prev + v) & 0xff;
13066
- data[p] = v;
13067
- }
13224
+ writeZipChannel(inflateLimited(compressed, expectedSize, reader), pixelData, width, height, bpv, step, offset, prediction);
13225
+ }
13226
+ finally {
13227
+ if (reader)
13228
+ recoverMemory(reader, expectedSize);
13229
+ }
13230
+ }
13231
+ // writes inflated values of one channel to `pixelData` (modifies `decompressed` when decoding 32bit prediction)
13232
+ function writeZipChannel(decompressed, pixelData, width, height, bpv, step, offset, prediction) {
13233
+ const data = pixelData.data;
13234
+ if (bpv === 1) {
13235
+ for (let y = 0, i = 0; y < height; y++) {
13236
+ let prev = 0;
13237
+ for (let x = 0, p = offset + y * width * step; x < width; x++, i++, p += step) {
13238
+ let v = decompressed[i];
13239
+ if (prediction)
13240
+ v = prev = (prev + v) & 0xff;
13241
+ data[p] = v;
13068
13242
  }
13069
13243
  }
13070
- else if (bpv === 2) {
13071
- for (let y = 0, i = 0; y < height; y++) {
13072
- let prev = 0;
13073
- for (let x = 0, p = offset + y * width * step; x < width; x++, i += 2, p += step) {
13074
- let v = (decompressed[i] << 8) | decompressed[i + 1];
13075
- if (prediction)
13076
- v = prev = (prev + v) & 0xffff;
13077
- data[p] = v;
13078
- }
13244
+ }
13245
+ else if (bpv === 2) {
13246
+ for (let y = 0, i = 0; y < height; y++) {
13247
+ let prev = 0;
13248
+ for (let x = 0, p = offset + y * width * step; x < width; x++, i += 2, p += step) {
13249
+ let v = (decompressed[i] << 8) | decompressed[i + 1];
13250
+ if (prediction)
13251
+ v = prev = (prev + v) & 0xffff;
13252
+ data[p] = v;
13079
13253
  }
13080
13254
  }
13081
- else {
13082
- const dst = new Uint32Array(data.buffer, data.byteOffset, data.length);
13083
- if (prediction) {
13084
- // bytes are delta encoded per row and stored in planar order (all 1st bytes, all 2nd bytes, ...)
13085
- decodePredicted(decompressed, width * 4, height);
13086
- for (let y = 0, di = offset; y < height; y++) {
13087
- let a = width * 4 * y;
13088
- for (let x = 0; x < width; x++, a++, di += step) {
13089
- const b = a + width;
13090
- const c = b + width;
13091
- const d = c + width;
13092
- dst[di] = ((decompressed[a] << 24) | (decompressed[b] << 16) | (decompressed[c] << 8) | decompressed[d]) >>> 0;
13093
- }
13255
+ }
13256
+ else {
13257
+ const dst = new Uint32Array(data.buffer, data.byteOffset, data.length);
13258
+ if (prediction) {
13259
+ // bytes are delta encoded per row and stored in planar order (all 1st bytes, all 2nd bytes, ...)
13260
+ decodePredicted(decompressed, width * 4, height);
13261
+ for (let y = 0, di = offset; y < height; y++) {
13262
+ let a = width * 4 * y;
13263
+ for (let x = 0; x < width; x++, a++, di += step) {
13264
+ const b = a + width;
13265
+ const c = b + width;
13266
+ const d = c + width;
13267
+ dst[di] = ((decompressed[a] << 24) | (decompressed[b] << 16) | (decompressed[c] << 8) | decompressed[d]) >>> 0;
13094
13268
  }
13095
13269
  }
13096
- else {
13097
- const view = new DataView(decompressed.buffer, decompressed.byteOffset, decompressed.byteLength);
13098
- for (let i = 0, j = 0, di = offset, size = width * height; i < size; i++, j += 4, di += step) {
13099
- dst[di] = view.getUint32(j, false);
13100
- }
13270
+ }
13271
+ else {
13272
+ const view = new DataView(decompressed.buffer, decompressed.byteOffset, decompressed.byteLength);
13273
+ for (let i = 0, j = 0, di = offset, size = width * height; i < size; i++, j += 4, di += step) {
13274
+ dst[di] = view.getUint32(j, false);
13101
13275
  }
13102
13276
  }
13103
13277
  }
13104
- finally {
13105
- if (reader)
13106
- recoverMemory(reader, expectedSize);
13107
- }
13108
13278
  }
13109
13279
  // decodes single PackBits row into byte buffer, returns number of decoded bytes
13110
13280
  function decodeRLERow(src, i, end, dst, count) {
@@ -13273,7 +13443,15 @@ function readSection(reader, round, func, skipEmpty = true, eightBytes = false)
13273
13443
  let end = reader.offset + length;
13274
13444
  if (end > reader.view.byteLength)
13275
13445
  throw new Error('Section exceeds file size');
13276
- const result = func(() => end - reader.offset);
13446
+ const parentSectionEnd = reader.sectionEnd;
13447
+ reader.sectionEnd = end;
13448
+ let result;
13449
+ try {
13450
+ result = func(() => end - reader.offset);
13451
+ }
13452
+ finally {
13453
+ reader.sectionEnd = parentSectionEnd;
13454
+ }
13277
13455
  if (reader.offset !== end) {
13278
13456
  if (reader.offset > end) {
13279
13457
  warnOrThrow(reader, 'Exceeded section limits');
@@ -15017,7 +15195,7 @@ function decodeGridInfo(grid) {
15017
15195
  return isDefault ? undefined : result;
15018
15196
  }
15019
15197
  function decodeEngineData(engineData) {
15020
- var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k;
15198
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j;
15021
15199
  // console.log('engineData', require('util').inspect(engineData, false, 99, true));
15022
15200
  const engineDict = engineData.EngineDict;
15023
15201
  const resourceDict = engineData.ResourceDict;
@@ -15037,7 +15215,7 @@ function decodeEngineData(engineData) {
15037
15215
  }
15038
15216
  const result = {
15039
15217
  text,
15040
- antiAlias: (_a = antialias[engineDict.AntiAlias]) !== null && _a !== void 0 ? _a : 'smooth',
15218
+ antiAlias: (_a = (0, helpers_1.arrayItem)(antialias, engineDict.AntiAlias)) !== null && _a !== void 0 ? _a : 'smooth',
15041
15219
  useFractionalGlyphWidths: !!engineDict.UseFractionalGlyphWidths,
15042
15220
  superscriptSize: resourceDict.SuperscriptSize,
15043
15221
  superscriptPosition: resourceDict.SuperscriptPosition,
@@ -15090,9 +15268,9 @@ function decodeEngineData(engineData) {
15090
15268
  // const styleSheetData = styleSheetSet[theNormalStyleSheet].StyleSheetData;
15091
15269
  const styleRun = engineDict.StyleRun;
15092
15270
  // style runs without a font use the font of the normal style sheet
15093
- const normalStyleSheet = (_j = (_g = resourceDict.StyleSheetSet) === null || _g === void 0 ? void 0 : _g[(_h = resourceDict.TheNormalStyleSheet) !== null && _h !== void 0 ? _h : 0]) === null || _j === void 0 ? void 0 : _j.StyleSheetData;
15094
- const normalFontIndex = normalStyleSheet === null || normalStyleSheet === void 0 ? void 0 : normalStyleSheet.Font;
15095
- const normalFont = (_k = (normalFontIndex !== undefined ? fonts[normalFontIndex] : undefined)) !== null && _k !== void 0 ? _k : fonts[0];
15271
+ const styleSheetSet = Array.isArray(resourceDict.StyleSheetSet) ? resourceDict.StyleSheetSet : [];
15272
+ const normalStyleSheet = (_h = (0, helpers_1.arrayItem)(styleSheetSet, (_g = resourceDict.TheNormalStyleSheet) !== null && _g !== void 0 ? _g : 0)) === null || _h === void 0 ? void 0 : _h.StyleSheetData;
15273
+ const normalFont = (_j = (0, helpers_1.arrayItem)(fonts, normalStyleSheet === null || normalStyleSheet === void 0 ? void 0 : normalStyleSheet.Font)) !== null && _j !== void 0 ? _j : fonts[0];
15096
15274
  result.style = {}; // decodeStyle(styleSheetData, fonts);
15097
15275
  result.styleRuns = [];
15098
15276
  for (let i = 0; i < styleRun.RunArray.length; i++) {