@adnsistemas/pdf-lib 2.9.0 → 2.9.1

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.
@@ -1737,24 +1737,23 @@ export default class PDFDocument {
1737
1737
  const upind = this.context.xrefs.length - lastUpdateMinusX - 1;
1738
1738
  const entries = this.context.listXrefEntries(upind);
1739
1739
  if (!entries.length) return [];
1740
- const changed = new Map<PDFRef, PDFObjectVersions>();
1740
+ const changed = new Map<string, PDFObjectVersions>();
1741
1741
  for (const entry of entries) {
1742
1742
  const ref = entry.ref;
1743
- changed.set(ref, {
1743
+ changed.set(ref.toString(), {
1744
1744
  ref,
1745
1745
  actual: entry.deleted ? undefined : this.context.lookup(ref),
1746
1746
  previous: this.context.getObjectVersions(ref),
1747
1747
  });
1748
1748
  }
1749
1749
  // if not the las update, then check objects later modified and adjust PDFObjectVersions accordingly
1750
- if (!lastUpdateMinusX)
1751
- return Array.from(changed.entries()).map((value) => value[1]);
1750
+ if (!lastUpdateMinusX) return Array.from(changed.values());
1752
1751
  while (lastUpdateMinusX) {
1753
1752
  lastUpdateMinusX -= 1;
1754
1753
  const upind = this.context.xrefs.length - lastUpdateMinusX - 1;
1755
1754
  const nentries = this.context.listXrefEntries(upind);
1756
1755
  for (const nentry of nentries) {
1757
- const oce = changed.get(nentry.ref);
1756
+ const oce = changed.get(nentry.ref.toString());
1758
1757
  if (oce && oce.actual) {
1759
1758
  oce.actual = oce.previous[0];
1760
1759
  oce.previous = oce.previous.slice(1);
@@ -1762,9 +1761,9 @@ export default class PDFDocument {
1762
1761
  }
1763
1762
  }
1764
1763
  // if PDF has errors, it may happen to end with objects that has no current, nor previous versions
1765
- return Array.from(changed.entries())
1766
- .map((value) => value[1])
1767
- .filter((ov) => ov.actual || ov.previous.length);
1764
+ return Array.from(changed.values()).filter(
1765
+ (ov) => ov.actual || ov.previous.length,
1766
+ );
1768
1767
  }
1769
1768
 
1770
1769
  /**
@@ -86,8 +86,8 @@ class PDFContext {
86
86
 
87
87
  security?: PDFSecurity;
88
88
 
89
- private readonly indirectObjects: Map<PDFRef, PDFObject>;
90
- private readonly objectsPreviousVersions: Map<PDFRef, PDFObject[]>;
89
+ private readonly indirectObjects: Map<string, [PDFRef, PDFObject]>;
90
+ private readonly objectsPreviousVersions: Map<string, PDFObject[]>;
91
91
 
92
92
  private pushGraphicsStateContentStreamRef?: PDFRef;
93
93
  private popGraphicsStateContentStreamRef?: PDFRef;
@@ -112,17 +112,17 @@ class PDFContext {
112
112
 
113
113
  assign(ref: PDFRef, object: PDFObject): void {
114
114
  if (this.preserveObjectsVersions) {
115
- const prevOV = this.indirectObjects.get(ref);
115
+ const prevOV = this.indirectObjects.get(ref.toString());
116
116
  if (prevOV) {
117
- const prevList = this.objectsPreviousVersions.get(ref);
117
+ const prevList = this.objectsPreviousVersions.get(ref.toString());
118
118
  if (!prevList) {
119
- this.objectsPreviousVersions.set(ref, [prevOV]);
119
+ this.objectsPreviousVersions.set(ref.toString(), [prevOV[1]]);
120
120
  } else {
121
- prevList.unshift(prevOV);
121
+ prevList.unshift(prevOV[1]);
122
122
  }
123
123
  }
124
124
  }
125
- this.indirectObjects.set(ref, object);
125
+ this.indirectObjects.set(ref.toString(), [ref, object]);
126
126
  if (ref.objectNumber > this.largestObjectNumber) {
127
127
  this.largestObjectNumber = ref.objectNumber;
128
128
  }
@@ -144,18 +144,18 @@ class PDFContext {
144
144
  delete(ref: PDFRef): boolean {
145
145
  if (this.snapshot) this.snapshot.markDeletedRef(ref);
146
146
  if (this.preserveObjectsVersions) {
147
- const object = this.indirectObjects.get(ref);
147
+ const object = this.indirectObjects.get(ref.toString());
148
148
  if (object) {
149
149
  // check is not already deleted
150
- const verlist = this.objectsPreviousVersions.get(ref);
150
+ const verlist = this.objectsPreviousVersions.get(ref.toString());
151
151
  if (verlist) {
152
- verlist.unshift(object);
152
+ verlist.unshift(object[1]);
153
153
  } else {
154
- this.objectsPreviousVersions.set(ref, [object]);
154
+ this.objectsPreviousVersions.set(ref.toString(), [object[1]]);
155
155
  }
156
156
  }
157
157
  }
158
- return this.indirectObjects.delete(ref);
158
+ return this.indirectObjects.delete(ref.toString());
159
159
  }
160
160
 
161
161
  lookupMaybe(ref: LookupKey, type: typeof PDFArray): PDFArray | undefined;
@@ -182,10 +182,13 @@ class PDFContext {
182
182
  // removed in next breaking API change.
183
183
  const preservePDFNull = types.includes(PDFNull);
184
184
 
185
- const result = isPDFInstance(ref, PDFClasses.PDFRef)
186
- ? this.indirectObjects.get(ref as PDFRef)
187
- : ref;
188
-
185
+ let result;
186
+ if (isPDFInstance(ref, PDFClasses.PDFRef)) {
187
+ const iobj = this.indirectObjects.get((ref as PDFRef).toString());
188
+ result = iobj ? iobj[1] : undefined;
189
+ } else {
190
+ result = ref;
191
+ }
189
192
  if (
190
193
  !result ||
191
194
  (isPDFInstance(result, PDFClasses.PDFNull) && !preservePDFNull)
@@ -222,9 +225,13 @@ class PDFContext {
222
225
  ): PDFString | PDFHexString;
223
226
 
224
227
  lookup(ref: LookupKey, ...types: any[]) {
225
- const result = isPDFInstance(ref, PDFClasses.PDFRef)
226
- ? this.indirectObjects.get(ref as PDFRef)
227
- : ref;
228
+ let result;
229
+ if (isPDFInstance(ref, PDFClasses.PDFRef)) {
230
+ const iobj = this.indirectObjects.get((ref as PDFRef).toString());
231
+ result = iobj ? iobj[1] : undefined;
232
+ } else {
233
+ result = ref;
234
+ }
228
235
 
229
236
  if (types.length === 0) return result;
230
237
 
@@ -247,7 +254,7 @@ class PDFContext {
247
254
  }
248
255
 
249
256
  getObjectRef(pdfObject: PDFObject): PDFRef | undefined {
250
- const entries = Array.from(this.indirectObjects.entries());
257
+ const entries = Array.from(this.indirectObjects.values());
251
258
  for (let idx = 0, len = entries.length; idx < len; idx++) {
252
259
  const [ref, object] = entries[idx];
253
260
  if (object === pdfObject) {
@@ -259,7 +266,7 @@ class PDFContext {
259
266
  }
260
267
 
261
268
  enumerateIndirectObjects(): [PDFRef, PDFObject][] {
262
- return Array.from(this.indirectObjects.entries()).sort(
269
+ return Array.from(this.indirectObjects.values()).sort(
263
270
  byAscendingObjectNumber,
264
271
  );
265
272
  }
@@ -456,7 +463,7 @@ class PDFContext {
456
463
  }
457
464
 
458
465
  private findContainingIndirectObject(target: PDFObject): PDFRef | undefined {
459
- const entries = Array.from(this.indirectObjects.entries());
466
+ const entries = Array.from(this.indirectObjects.values());
460
467
  for (let idx = 0, len = entries.length; idx < len; idx++) {
461
468
  const [ref, object] = entries[idx];
462
469
  if (this.objectContains(object, target)) {
@@ -489,7 +496,7 @@ class PDFContext {
489
496
 
490
497
  getObjectVersions(ref: PDFRef): PDFObject[] {
491
498
  if (!this.preserveObjectsVersions) return [];
492
- const list = this.objectsPreviousVersions.get(ref);
499
+ const list = this.objectsPreviousVersions.get(ref.toString());
493
500
  if (list) return list;
494
501
  return [];
495
502
  }
@@ -10,9 +10,11 @@ import PDFStream from './PDFStream';
10
10
  import PDFString from './PDFString';
11
11
  import PDFContext from '../PDFContext';
12
12
  import CharCodes from '../syntax/CharCodes';
13
- import { PDFClasses } from '../../api/objects';
13
+ import { isPDFInstance, PDFClasses } from '../../api/objects';
14
14
 
15
15
  export type DictMap = Map<PDFName, PDFObject>;
16
+ // dictionary keys must be unique, using PDFName does not guarantee that
17
+ type InternalDictMap = Map<string, [PDFName, PDFObject]>;
16
18
 
17
19
  class PDFDict extends PDFObject {
18
20
  static className = () => PDFClasses.PDFDict;
@@ -26,31 +28,33 @@ class PDFDict extends PDFObject {
26
28
 
27
29
  readonly context: PDFContext;
28
30
 
29
- private readonly dict: DictMap;
31
+ private readonly dict: InternalDictMap;
30
32
 
31
33
  suppressEncryption: boolean = false;
32
34
 
33
35
  protected constructor(map: DictMap, context: PDFContext) {
34
36
  super();
35
- this.dict = map;
37
+ this.dict = new Map(
38
+ Array.from(map.entries()).map((entry) => [entry[0].toString(), entry]),
39
+ );
36
40
  this.context = context;
37
41
  }
38
42
 
39
43
  keys(): PDFName[] {
40
- return Array.from(this.dict.keys());
44
+ return Array.from(this.dict.values()).map((value) => value[0]);
41
45
  }
42
46
 
43
47
  values(): PDFObject[] {
44
- return Array.from(this.dict.values());
48
+ return Array.from(this.dict.values()).map((value) => value[1]);
45
49
  }
46
50
 
47
51
  entries(): [PDFName, PDFObject][] {
48
- return Array.from(this.dict.entries());
52
+ return Array.from(this.dict.values());
49
53
  }
50
54
 
51
55
  set(key: PDFName, value: PDFObject): void {
52
56
  this.registerChange();
53
- this.dict.set(key, value);
57
+ this.dict.set(key.asString(), [key, value]);
54
58
  }
55
59
 
56
60
  get(
@@ -59,14 +63,20 @@ class PDFDict extends PDFObject {
59
63
  // removed in next breaking API change.
60
64
  preservePDFNull = false,
61
65
  ): PDFObject | undefined {
62
- const value = this.dict.get(key);
63
- if (value === PDFNull && !preservePDFNull) return undefined;
64
- return value;
66
+ if (!key.asString) return undefined;
67
+ const value = this.dict.get(key.asString());
68
+ if (
69
+ !value ||
70
+ (isPDFInstance(value[1], PDFClasses.PDFNull) && !preservePDFNull)
71
+ )
72
+ return undefined;
73
+ return value[1];
65
74
  }
66
75
 
67
76
  has(key: PDFName): boolean {
68
- const value = this.dict.get(key);
69
- return value !== undefined && value !== PDFNull;
77
+ if (!key.asString) return false;
78
+ const value = this.dict.get(key.asString());
79
+ return value !== undefined && !isPDFInstance(value[1], PDFClasses.PDFNull);
70
80
  }
71
81
 
72
82
  lookupMaybe(key: PDFName, type: typeof PDFArray): PDFArray | undefined;
@@ -110,7 +120,8 @@ class PDFDict extends PDFObject {
110
120
  ...types,
111
121
  ) as any;
112
122
 
113
- if (value === PDFNull && !preservePDFNull) return undefined;
123
+ if (isPDFInstance(value, PDFClasses.PDFNull) && !preservePDFNull)
124
+ return undefined;
114
125
 
115
126
  return value;
116
127
  }
@@ -154,18 +165,19 @@ class PDFDict extends PDFObject {
154
165
  ...types,
155
166
  ) as any;
156
167
 
157
- if (value === PDFNull && !preservePDFNull) return undefined;
168
+ if (isPDFInstance(value, PDFClasses.PDFNull) && !preservePDFNull)
169
+ return undefined;
158
170
 
159
171
  return value;
160
172
  }
161
173
 
162
174
  delete(key: PDFName): boolean {
163
175
  this.registerChange();
164
- return this.dict.delete(key);
176
+ return this.dict.delete(key.asString());
165
177
  }
166
178
 
167
179
  asMap(): Map<PDFName, PDFObject> {
168
- return new Map(this.dict);
180
+ return new Map(this.dict.values());
169
181
  }
170
182
 
171
183
  /** Generate a random key that doesn't exist in current key set */