pdfjs-dist 2.5.207 → 2.6.347

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.

Potentially problematic release.


This version of pdfjs-dist might be problematic. Click here for more details.

Files changed (133) hide show
  1. package/bower.json +1 -1
  2. package/build/pdf.js +867 -216
  3. package/build/pdf.js.map +1 -1
  4. package/build/pdf.min.js +1 -1
  5. package/build/pdf.worker.js +10214 -8547
  6. package/build/pdf.worker.js.map +1 -1
  7. package/build/pdf.worker.min.js +1 -1
  8. package/es5/build/pdf.js +2023 -1046
  9. package/es5/build/pdf.js.map +1 -1
  10. package/es5/build/pdf.min.js +22 -0
  11. package/es5/build/pdf.worker.js +11136 -8144
  12. package/es5/build/pdf.worker.js.map +1 -1
  13. package/es5/build/pdf.worker.min.js +22 -0
  14. package/es5/image_decoders/pdf.image_decoders.js +12529 -0
  15. package/es5/image_decoders/pdf.image_decoders.js.map +1 -0
  16. package/es5/image_decoders/pdf.image_decoders.min.js +22 -0
  17. package/es5/web/images/shadow.png +0 -0
  18. package/es5/web/pdf_viewer.css +8 -5
  19. package/es5/web/pdf_viewer.js +195 -51
  20. package/es5/web/pdf_viewer.js.map +1 -1
  21. package/image_decoders/pdf.image_decoders.js +24 -16
  22. package/image_decoders/pdf.image_decoders.js.map +1 -1
  23. package/image_decoders/pdf.image_decoders.min.js +1 -1
  24. package/lib/core/annotation.js +479 -43
  25. package/lib/core/cff_parser.js +643 -615
  26. package/lib/core/chunked_stream.js +64 -50
  27. package/lib/core/colorspace.js +126 -104
  28. package/lib/core/crypto.js +37 -0
  29. package/lib/core/document.js +128 -75
  30. package/lib/core/evaluator.js +3015 -2754
  31. package/lib/core/fonts.js +47 -46
  32. package/lib/core/function.js +79 -6
  33. package/lib/core/image.js +364 -357
  34. package/lib/core/image_utils.js +109 -8
  35. package/lib/core/jpg.js +8 -3
  36. package/lib/core/obj.js +255 -34
  37. package/lib/core/operator_list.js +17 -15
  38. package/lib/core/parser.js +40 -3
  39. package/lib/core/pattern.js +24 -11
  40. package/lib/core/primitives.js +112 -53
  41. package/lib/core/type1_parser.js +2 -0
  42. package/lib/core/worker.js +102 -37
  43. package/lib/core/writer.js +242 -0
  44. package/lib/display/annotation_layer.js +51 -10
  45. package/lib/display/annotation_storage.js +89 -0
  46. package/lib/display/api.js +140 -54
  47. package/lib/display/canvas.js +215 -46
  48. package/lib/display/display_utils.js +55 -18
  49. package/lib/display/fetch_stream.js +6 -0
  50. package/lib/display/font_loader.js +26 -15
  51. package/lib/display/metadata.js +1 -7
  52. package/lib/display/network.js +0 -14
  53. package/lib/display/node_utils.js +87 -0
  54. package/lib/display/optional_content_config.js +184 -0
  55. package/lib/display/svg.js +1 -1
  56. package/lib/display/text_layer.js +4 -1
  57. package/lib/pdf.js +2 -2
  58. package/lib/pdf.worker.js +2 -2
  59. package/lib/shared/is_node.js +1 -1
  60. package/lib/shared/util.js +13 -10
  61. package/lib/test/unit/annotation_spec.js +520 -50
  62. package/lib/test/unit/annotation_storage_spec.js +83 -0
  63. package/lib/test/unit/api_spec.js +16 -6
  64. package/lib/test/unit/cmap_spec.js +4 -4
  65. package/lib/test/unit/colorspace_spec.js +216 -24
  66. package/lib/test/unit/crypto_spec.js +51 -4
  67. package/lib/test/unit/custom_spec.js +129 -1
  68. package/lib/test/unit/document_spec.js +112 -1
  69. package/lib/test/unit/evaluator_spec.js +29 -10
  70. package/lib/test/unit/jasmine-boot.js +1 -1
  71. package/lib/test/unit/metadata_spec.js +3 -3
  72. package/lib/test/unit/primitives_spec.js +242 -63
  73. package/lib/test/unit/test_utils.js +42 -89
  74. package/lib/test/unit/testreporter.js +17 -6
  75. package/lib/test/unit/util_spec.js +13 -12
  76. package/lib/test/unit/writer_spec.js +84 -0
  77. package/lib/web/annotation_layer_builder.js +13 -9
  78. package/lib/web/app.js +231 -26
  79. package/lib/web/app_options.js +1 -6
  80. package/lib/web/base_tree_viewer.js +100 -0
  81. package/lib/web/base_viewer.js +53 -4
  82. package/lib/web/download_manager.js +3 -10
  83. package/lib/web/firefox_print_service.js +32 -12
  84. package/lib/web/firefoxcom.js +20 -6
  85. package/lib/web/interfaces.js +1 -1
  86. package/lib/web/pdf_attachment_viewer.js +80 -38
  87. package/lib/web/pdf_find_controller.js +2 -1
  88. package/lib/web/pdf_layer_viewer.js +208 -0
  89. package/lib/web/pdf_outline_viewer.js +24 -57
  90. package/lib/web/pdf_page_view.js +29 -6
  91. package/lib/web/pdf_print_service.js +13 -11
  92. package/lib/web/pdf_sidebar.js +46 -25
  93. package/lib/web/pdf_thumbnail_view.js +16 -3
  94. package/lib/web/pdf_thumbnail_viewer.js +17 -1
  95. package/lib/web/pdf_viewer.component.js +2 -2
  96. package/lib/web/preferences.js +1 -1
  97. package/lib/web/toolbar.js +1 -1
  98. package/lib/web/ui_utils.js +7 -1
  99. package/package.json +3 -1
  100. package/types/display/annotation_layer.d.ts +61 -0
  101. package/types/display/annotation_storage.d.ts +36 -0
  102. package/types/display/api.d.ts +1107 -0
  103. package/types/display/api_compatibility.d.ts +1 -0
  104. package/types/display/canvas.d.ts +4 -0
  105. package/types/display/content_disposition.d.ts +7 -0
  106. package/types/display/display_utils.d.ts +243 -0
  107. package/types/display/fetch_stream.d.ts +67 -0
  108. package/types/display/font_loader.d.ts +19 -0
  109. package/types/display/metadata.d.ts +9 -0
  110. package/types/display/network.d.ts +80 -0
  111. package/types/display/network_utils.d.ts +14 -0
  112. package/types/display/node_stream.d.ts +72 -0
  113. package/types/display/node_utils.d.ts +6 -0
  114. package/types/display/optional_content_config.d.ts +12 -0
  115. package/types/display/pattern_helper.d.ts +5 -0
  116. package/types/display/svg.d.ts +2 -0
  117. package/types/display/text_layer.d.ts +83 -0
  118. package/types/display/transport_stream.d.ts +57 -0
  119. package/types/display/webgl.d.ts +20 -0
  120. package/types/display/worker_options.d.ts +27 -0
  121. package/types/display/xml_parser.d.ts +35 -0
  122. package/types/pdf.d.ts +33 -0
  123. package/types/shared/compatibility.d.ts +1 -0
  124. package/types/shared/is_node.d.ts +1 -0
  125. package/types/shared/message_handler.d.ts +62 -0
  126. package/types/shared/util.d.ts +421 -0
  127. package/web/images/shadow.png +0 -0
  128. package/web/pdf_viewer.css +8 -5
  129. package/web/pdf_viewer.js +109 -33
  130. package/web/pdf_viewer.js.map +1 -1
  131. package/webpack.js +1 -9
  132. package/es5/web/images/texture.png +0 -0
  133. package/web/images/texture.png +0 -0
@@ -37,8 +37,7 @@ class ChunkedStream {
37
37
  this.pos = 0;
38
38
  this.end = length;
39
39
  this.chunkSize = chunkSize;
40
- this.loadedChunks = [];
41
- this.numChunksLoaded = 0;
40
+ this._loadedChunks = new Set();
42
41
  this.numChunks = Math.ceil(length / chunkSize);
43
42
  this.manager = manager;
44
43
  this.progressiveDataLength = 0;
@@ -49,7 +48,7 @@ class ChunkedStream {
49
48
  const chunks = [];
50
49
 
51
50
  for (let chunk = 0, n = this.numChunks; chunk < n; ++chunk) {
52
- if (!this.loadedChunks[chunk]) {
51
+ if (!this._loadedChunks.has(chunk)) {
53
52
  chunks.push(chunk);
54
53
  }
55
54
  }
@@ -61,6 +60,10 @@ class ChunkedStream {
61
60
  return [this];
62
61
  }
63
62
 
63
+ get numChunksLoaded() {
64
+ return this._loadedChunks.size;
65
+ }
66
+
64
67
  allChunksLoaded() {
65
68
  return this.numChunksLoaded === this.numChunks;
66
69
  }
@@ -83,10 +86,7 @@ class ChunkedStream {
83
86
  const endChunk = Math.floor((end - 1) / chunkSize) + 1;
84
87
 
85
88
  for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
86
- if (!this.loadedChunks[curChunk]) {
87
- this.loadedChunks[curChunk] = true;
88
- ++this.numChunksLoaded;
89
- }
89
+ this._loadedChunks.add(curChunk);
90
90
  }
91
91
  }
92
92
 
@@ -99,10 +99,7 @@ class ChunkedStream {
99
99
  const endChunk = position >= this.end ? this.numChunks : Math.floor(position / this.chunkSize);
100
100
 
101
101
  for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
102
- if (!this.loadedChunks[curChunk]) {
103
- this.loadedChunks[curChunk] = true;
104
- ++this.numChunksLoaded;
105
- }
102
+ this._loadedChunks.add(curChunk);
106
103
  }
107
104
  }
108
105
 
@@ -117,7 +114,7 @@ class ChunkedStream {
117
114
  return;
118
115
  }
119
116
 
120
- if (!this.loadedChunks[chunk]) {
117
+ if (!this._loadedChunks.has(chunk)) {
121
118
  throw new _core_utils.MissingDataException(pos, pos + 1);
122
119
  }
123
120
 
@@ -138,7 +135,7 @@ class ChunkedStream {
138
135
  const endChunk = Math.floor((end - 1) / chunkSize) + 1;
139
136
 
140
137
  for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
141
- if (!this.loadedChunks[chunk]) {
138
+ if (!this._loadedChunks.has(chunk)) {
142
139
  throw new _core_utils.MissingDataException(begin, end);
143
140
  }
144
141
  }
@@ -150,7 +147,7 @@ class ChunkedStream {
150
147
  for (let i = 0; i < numChunks; ++i) {
151
148
  const chunk = (beginChunk + i) % numChunks;
152
149
 
153
- if (!this.loadedChunks[chunk]) {
150
+ if (!this._loadedChunks.has(chunk)) {
154
151
  return chunk;
155
152
  }
156
153
  }
@@ -159,7 +156,7 @@ class ChunkedStream {
159
156
  }
160
157
 
161
158
  hasChunk(chunk) {
162
- return !!this.loadedChunks[chunk];
159
+ return this._loadedChunks.has(chunk);
163
160
  }
164
161
 
165
162
  get length() {
@@ -302,7 +299,7 @@ class ChunkedStream {
302
299
  const missingChunks = [];
303
300
 
304
301
  for (let chunk = beginChunk; chunk < endChunk; ++chunk) {
305
- if (!this.loadedChunks[chunk]) {
302
+ if (!this._loadedChunks.has(chunk)) {
306
303
  missingChunks.push(chunk);
307
304
  }
308
305
  }
@@ -338,9 +335,9 @@ class ChunkedStreamManager {
338
335
  this.disableAutoFetch = args.disableAutoFetch;
339
336
  this.msgHandler = args.msgHandler;
340
337
  this.currRequestId = 0;
341
- this.chunksNeededByRequest = Object.create(null);
342
- this.requestsByChunk = Object.create(null);
343
- this.promisesByRequest = Object.create(null);
338
+ this._chunksNeededByRequest = new Map();
339
+ this._requestsByChunk = new Map();
340
+ this._promisesByRequest = new Map();
344
341
  this.progressiveDataLength = 0;
345
342
  this.aborted = false;
346
343
  this._loadedStreamCapability = (0, _util.createPromiseCapability)();
@@ -409,47 +406,57 @@ class ChunkedStreamManager {
409
406
 
410
407
  _requestChunks(chunks) {
411
408
  const requestId = this.currRequestId++;
412
- const chunksNeeded = Object.create(null);
413
- this.chunksNeededByRequest[requestId] = chunksNeeded;
409
+ const chunksNeeded = new Set();
410
+
411
+ this._chunksNeededByRequest.set(requestId, chunksNeeded);
414
412
 
415
413
  for (const chunk of chunks) {
416
414
  if (!this.stream.hasChunk(chunk)) {
417
- chunksNeeded[chunk] = true;
415
+ chunksNeeded.add(chunk);
418
416
  }
419
417
  }
420
418
 
421
- if ((0, _util.isEmptyObj)(chunksNeeded)) {
419
+ if (chunksNeeded.size === 0) {
422
420
  return Promise.resolve();
423
421
  }
424
422
 
425
423
  const capability = (0, _util.createPromiseCapability)();
426
- this.promisesByRequest[requestId] = capability;
424
+
425
+ this._promisesByRequest.set(requestId, capability);
426
+
427
427
  const chunksToRequest = [];
428
428
 
429
- for (let chunk in chunksNeeded) {
430
- chunk = chunk | 0;
429
+ for (const chunk of chunksNeeded) {
430
+ let requestIds = this._requestsByChunk.get(chunk);
431
+
432
+ if (!requestIds) {
433
+ requestIds = [];
434
+
435
+ this._requestsByChunk.set(chunk, requestIds);
431
436
 
432
- if (!(chunk in this.requestsByChunk)) {
433
- this.requestsByChunk[chunk] = [];
434
437
  chunksToRequest.push(chunk);
435
438
  }
436
439
 
437
- this.requestsByChunk[chunk].push(requestId);
438
- }
439
-
440
- if (!chunksToRequest.length) {
441
- return capability.promise;
440
+ requestIds.push(requestId);
442
441
  }
443
442
 
444
- const groupedChunksToRequest = this.groupChunks(chunksToRequest);
443
+ if (chunksToRequest.length > 0) {
444
+ const groupedChunksToRequest = this.groupChunks(chunksToRequest);
445
445
 
446
- for (const groupedChunk of groupedChunksToRequest) {
447
- const begin = groupedChunk.beginChunk * this.chunkSize;
448
- const end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
449
- this.sendRequest(begin, end);
446
+ for (const groupedChunk of groupedChunksToRequest) {
447
+ const begin = groupedChunk.beginChunk * this.chunkSize;
448
+ const end = Math.min(groupedChunk.endChunk * this.chunkSize, this.length);
449
+ this.sendRequest(begin, end);
450
+ }
450
451
  }
451
452
 
452
- return capability.promise;
453
+ return capability.promise.catch(reason => {
454
+ if (this.aborted) {
455
+ return;
456
+ }
457
+
458
+ throw reason;
459
+ });
453
460
  }
454
461
 
455
462
  getStream() {
@@ -551,17 +558,22 @@ class ChunkedStreamManager {
551
558
  const loadedRequests = [];
552
559
 
553
560
  for (let curChunk = beginChunk; curChunk < endChunk; ++curChunk) {
554
- const requestIds = this.requestsByChunk[curChunk] || [];
555
- delete this.requestsByChunk[curChunk];
561
+ const requestIds = this._requestsByChunk.get(curChunk);
562
+
563
+ if (!requestIds) {
564
+ continue;
565
+ }
566
+
567
+ this._requestsByChunk.delete(curChunk);
556
568
 
557
569
  for (const requestId of requestIds) {
558
- const chunksNeeded = this.chunksNeededByRequest[requestId];
570
+ const chunksNeeded = this._chunksNeededByRequest.get(requestId);
559
571
 
560
- if (curChunk in chunksNeeded) {
561
- delete chunksNeeded[curChunk];
572
+ if (chunksNeeded.has(curChunk)) {
573
+ chunksNeeded.delete(curChunk);
562
574
  }
563
575
 
564
- if (!(0, _util.isEmptyObj)(chunksNeeded)) {
576
+ if (chunksNeeded.size > 0) {
565
577
  continue;
566
578
  }
567
579
 
@@ -569,7 +581,7 @@ class ChunkedStreamManager {
569
581
  }
570
582
  }
571
583
 
572
- if (!this.disableAutoFetch && (0, _util.isEmptyObj)(this.requestsByChunk)) {
584
+ if (!this.disableAutoFetch && this._requestsByChunk.size === 0) {
573
585
  let nextEmptyChunk;
574
586
 
575
587
  if (this.stream.numChunksLoaded === 1) {
@@ -588,8 +600,10 @@ class ChunkedStreamManager {
588
600
  }
589
601
 
590
602
  for (const requestId of loadedRequests) {
591
- const capability = this.promisesByRequest[requestId];
592
- delete this.promisesByRequest[requestId];
603
+ const capability = this._promisesByRequest.get(requestId);
604
+
605
+ this._promisesByRequest.delete(requestId);
606
+
593
607
  capability.resolve();
594
608
  }
595
609
 
@@ -618,8 +632,8 @@ class ChunkedStreamManager {
618
632
  this.pdfNetworkStream.cancelAllRequests(reason);
619
633
  }
620
634
 
621
- for (const requestId in this.promisesByRequest) {
622
- this.promisesByRequest[requestId].reject(reason);
635
+ for (const capability of this._promisesByRequest.values()) {
636
+ capability.reject(reason);
623
637
  }
624
638
  }
625
639
 
@@ -30,6 +30,8 @@ var _util = require("../shared/util.js");
30
30
 
31
31
  var _primitives = require("./primitives.js");
32
32
 
33
+ var _core_utils = require("./core_utils.js");
34
+
33
35
  function resizeRgbImage(src, dest, w1, h1, w2, h2, alpha01) {
34
36
  const COMPONENTS = 3;
35
37
  alpha01 = alpha01 !== 1 ? 0 : alpha01;
@@ -162,135 +164,162 @@ class ColorSpace {
162
164
  return (0, _util.shadow)(this, "usesZeroToOneRange", true);
163
165
  }
164
166
 
165
- static parse(cs, xref, res, pdfFunctionFactory) {
166
- const IR = this.parseToIR(cs, xref, res, pdfFunctionFactory);
167
- return this.fromIR(IR);
168
- }
167
+ static _cache(cacheKey, xref, localColorSpaceCache, parsedColorSpace) {
168
+ if (!localColorSpaceCache) {
169
+ throw new Error('ColorSpace._cache - expected "localColorSpaceCache" argument.');
170
+ }
169
171
 
170
- static fromIR(IR) {
171
- const name = Array.isArray(IR) ? IR[0] : IR;
172
- let whitePoint, blackPoint, gamma;
172
+ if (!parsedColorSpace) {
173
+ throw new Error('ColorSpace._cache - expected "parsedColorSpace" argument.');
174
+ }
173
175
 
174
- switch (name) {
175
- case "DeviceGrayCS":
176
- return this.singletons.gray;
176
+ let csName, csRef;
177
177
 
178
- case "DeviceRgbCS":
179
- return this.singletons.rgb;
178
+ if (cacheKey instanceof _primitives.Ref) {
179
+ csRef = cacheKey;
180
+ cacheKey = xref.fetch(cacheKey);
181
+ }
180
182
 
181
- case "DeviceCmykCS":
182
- return this.singletons.cmyk;
183
+ if (cacheKey instanceof _primitives.Name) {
184
+ csName = cacheKey.name;
185
+ }
183
186
 
184
- case "CalGrayCS":
185
- whitePoint = IR[1];
186
- blackPoint = IR[2];
187
- gamma = IR[3];
188
- return new CalGrayCS(whitePoint, blackPoint, gamma);
187
+ if (csName || csRef) {
188
+ localColorSpaceCache.set(csName, csRef, parsedColorSpace);
189
+ }
190
+ }
189
191
 
190
- case "CalRGBCS":
191
- whitePoint = IR[1];
192
- blackPoint = IR[2];
193
- gamma = IR[3];
194
- const matrix = IR[4];
195
- return new CalRGBCS(whitePoint, blackPoint, gamma, matrix);
192
+ static getCached(cacheKey, xref, localColorSpaceCache) {
193
+ if (!localColorSpaceCache) {
194
+ throw new Error('ColorSpace.getCached - expected "localColorSpaceCache" argument.');
195
+ }
196
+
197
+ if (cacheKey instanceof _primitives.Ref) {
198
+ const localColorSpace = localColorSpaceCache.getByRef(cacheKey);
196
199
 
197
- case "PatternCS":
198
- let basePatternCS = IR[1];
200
+ if (localColorSpace) {
201
+ return localColorSpace;
202
+ }
199
203
 
200
- if (basePatternCS) {
201
- basePatternCS = this.fromIR(basePatternCS);
204
+ try {
205
+ cacheKey = xref.fetch(cacheKey);
206
+ } catch (ex) {
207
+ if (ex instanceof _core_utils.MissingDataException) {
208
+ throw ex;
202
209
  }
210
+ }
211
+ }
203
212
 
204
- return new PatternCS(basePatternCS);
213
+ if (cacheKey instanceof _primitives.Name) {
214
+ const localColorSpace = localColorSpaceCache.getByName(cacheKey.name);
205
215
 
206
- case "IndexedCS":
207
- const baseIndexedCS = IR[1];
208
- const hiVal = IR[2];
209
- const lookup = IR[3];
210
- return new IndexedCS(this.fromIR(baseIndexedCS), hiVal, lookup);
216
+ if (localColorSpace) {
217
+ return localColorSpace;
218
+ }
219
+ }
211
220
 
212
- case "AlternateCS":
213
- const numComps = IR[1];
214
- const alt = IR[2];
215
- const tintFn = IR[3];
216
- return new AlternateCS(numComps, this.fromIR(alt), tintFn);
221
+ return null;
222
+ }
223
+
224
+ static async parseAsync({
225
+ cs,
226
+ xref,
227
+ resources = null,
228
+ pdfFunctionFactory,
229
+ localColorSpaceCache
230
+ }) {
231
+ const parsedColorSpace = this._parse(cs, xref, resources, pdfFunctionFactory);
217
232
 
218
- case "LabCS":
219
- whitePoint = IR[1];
220
- blackPoint = IR[2];
221
- const range = IR[3];
222
- return new LabCS(whitePoint, blackPoint, range);
233
+ this._cache(cs, xref, localColorSpaceCache, parsedColorSpace);
234
+
235
+ return parsedColorSpace;
236
+ }
223
237
 
224
- default:
225
- throw new _util.FormatError(`Unknown colorspace name: ${name}`);
238
+ static parse({
239
+ cs,
240
+ xref,
241
+ resources = null,
242
+ pdfFunctionFactory,
243
+ localColorSpaceCache
244
+ }) {
245
+ const cachedColorSpace = this.getCached(cs, xref, localColorSpaceCache);
246
+
247
+ if (cachedColorSpace) {
248
+ return cachedColorSpace;
226
249
  }
250
+
251
+ const parsedColorSpace = this._parse(cs, xref, resources, pdfFunctionFactory);
252
+
253
+ this._cache(cs, xref, localColorSpaceCache, parsedColorSpace);
254
+
255
+ return parsedColorSpace;
227
256
  }
228
257
 
229
- static parseToIR(cs, xref, res = null, pdfFunctionFactory) {
258
+ static _parse(cs, xref, resources = null, pdfFunctionFactory) {
230
259
  cs = xref.fetchIfRef(cs);
231
260
 
232
261
  if ((0, _primitives.isName)(cs)) {
233
262
  switch (cs.name) {
234
263
  case "DeviceGray":
235
264
  case "G":
236
- return "DeviceGrayCS";
265
+ return this.singletons.gray;
237
266
 
238
267
  case "DeviceRGB":
239
268
  case "RGB":
240
- return "DeviceRgbCS";
269
+ return this.singletons.rgb;
241
270
 
242
271
  case "DeviceCMYK":
243
272
  case "CMYK":
244
- return "DeviceCmykCS";
273
+ return this.singletons.cmyk;
245
274
 
246
275
  case "Pattern":
247
- return ["PatternCS", null];
276
+ return new PatternCS(null);
248
277
 
249
278
  default:
250
- if ((0, _primitives.isDict)(res)) {
251
- const colorSpaces = res.get("ColorSpace");
279
+ if ((0, _primitives.isDict)(resources)) {
280
+ const colorSpaces = resources.get("ColorSpace");
252
281
 
253
282
  if ((0, _primitives.isDict)(colorSpaces)) {
254
- const resCS = colorSpaces.get(cs.name);
283
+ const resourcesCS = colorSpaces.get(cs.name);
255
284
 
256
- if (resCS) {
257
- if ((0, _primitives.isName)(resCS)) {
258
- return this.parseToIR(resCS, xref, res, pdfFunctionFactory);
285
+ if (resourcesCS) {
286
+ if ((0, _primitives.isName)(resourcesCS)) {
287
+ return this._parse(resourcesCS, xref, resources, pdfFunctionFactory);
259
288
  }
260
289
 
261
- cs = resCS;
290
+ cs = resourcesCS;
262
291
  break;
263
292
  }
264
293
  }
265
294
  }
266
295
 
267
- throw new _util.FormatError(`unrecognized colorspace ${cs.name}`);
296
+ throw new _util.FormatError(`Unrecognized ColorSpace: ${cs.name}`);
268
297
  }
269
298
  }
270
299
 
271
300
  if (Array.isArray(cs)) {
272
301
  const mode = xref.fetchIfRef(cs[0]).name;
273
- let numComps, params, alt, whitePoint, blackPoint, gamma;
302
+ let params, numComps, baseCS, whitePoint, blackPoint, gamma;
274
303
 
275
304
  switch (mode) {
276
305
  case "DeviceGray":
277
306
  case "G":
278
- return "DeviceGrayCS";
307
+ return this.singletons.gray;
279
308
 
280
309
  case "DeviceRGB":
281
310
  case "RGB":
282
- return "DeviceRgbCS";
311
+ return this.singletons.rgb;
283
312
 
284
313
  case "DeviceCMYK":
285
314
  case "CMYK":
286
- return "DeviceCmykCS";
315
+ return this.singletons.cmyk;
287
316
 
288
317
  case "CalGray":
289
318
  params = xref.fetchIfRef(cs[1]);
290
319
  whitePoint = params.getArray("WhitePoint");
291
320
  blackPoint = params.getArray("BlackPoint");
292
321
  gamma = params.get("Gamma");
293
- return ["CalGrayCS", whitePoint, blackPoint, gamma];
322
+ return new CalGrayCS(whitePoint, blackPoint, gamma);
294
323
 
295
324
  case "CalRGB":
296
325
  params = xref.fetchIfRef(cs[1]);
@@ -298,77 +327,71 @@ class ColorSpace {
298
327
  blackPoint = params.getArray("BlackPoint");
299
328
  gamma = params.getArray("Gamma");
300
329
  const matrix = params.getArray("Matrix");
301
- return ["CalRGBCS", whitePoint, blackPoint, gamma, matrix];
330
+ return new CalRGBCS(whitePoint, blackPoint, gamma, matrix);
302
331
 
303
332
  case "ICCBased":
304
333
  const stream = xref.fetchIfRef(cs[1]);
305
334
  const dict = stream.dict;
306
335
  numComps = dict.get("N");
307
- alt = dict.get("Alternate");
336
+ const alt = dict.get("Alternate");
308
337
 
309
338
  if (alt) {
310
- const altIR = this.parseToIR(alt, xref, res, pdfFunctionFactory);
311
- const altCS = this.fromIR(altIR, pdfFunctionFactory);
339
+ const altCS = this._parse(alt, xref, resources, pdfFunctionFactory);
312
340
 
313
341
  if (altCS.numComps === numComps) {
314
- return altIR;
342
+ return altCS;
315
343
  }
316
344
 
317
345
  (0, _util.warn)("ICCBased color space: Ignoring incorrect /Alternate entry.");
318
346
  }
319
347
 
320
348
  if (numComps === 1) {
321
- return "DeviceGrayCS";
349
+ return this.singletons.gray;
322
350
  } else if (numComps === 3) {
323
- return "DeviceRgbCS";
351
+ return this.singletons.rgb;
324
352
  } else if (numComps === 4) {
325
- return "DeviceCmykCS";
353
+ return this.singletons.cmyk;
326
354
  }
327
355
 
328
356
  break;
329
357
 
330
358
  case "Pattern":
331
- let basePatternCS = cs[1] || null;
359
+ baseCS = cs[1] || null;
332
360
 
333
- if (basePatternCS) {
334
- basePatternCS = this.parseToIR(basePatternCS, xref, res, pdfFunctionFactory);
361
+ if (baseCS) {
362
+ baseCS = this._parse(baseCS, xref, resources, pdfFunctionFactory);
335
363
  }
336
364
 
337
- return ["PatternCS", basePatternCS];
365
+ return new PatternCS(baseCS);
338
366
 
339
367
  case "Indexed":
340
368
  case "I":
341
- const baseIndexedCS = this.parseToIR(cs[1], xref, res, pdfFunctionFactory);
369
+ baseCS = this._parse(cs[1], xref, resources, pdfFunctionFactory);
342
370
  const hiVal = xref.fetchIfRef(cs[2]) + 1;
343
- let lookup = xref.fetchIfRef(cs[3]);
344
-
345
- if ((0, _primitives.isStream)(lookup)) {
346
- lookup = lookup.getBytes();
347
- }
348
-
349
- return ["IndexedCS", baseIndexedCS, hiVal, lookup];
371
+ const lookup = xref.fetchIfRef(cs[3]);
372
+ return new IndexedCS(baseCS, hiVal, lookup);
350
373
 
351
374
  case "Separation":
352
375
  case "DeviceN":
353
376
  const name = xref.fetchIfRef(cs[1]);
354
377
  numComps = Array.isArray(name) ? name.length : 1;
355
- alt = this.parseToIR(cs[2], xref, res, pdfFunctionFactory);
356
- const tintFn = pdfFunctionFactory.create(xref.fetchIfRef(cs[3]));
357
- return ["AlternateCS", numComps, alt, tintFn];
378
+ baseCS = this._parse(cs[2], xref, resources, pdfFunctionFactory);
379
+ const tintFn = pdfFunctionFactory.create(cs[3]);
380
+ return new AlternateCS(numComps, baseCS, tintFn);
358
381
 
359
382
  case "Lab":
360
383
  params = xref.fetchIfRef(cs[1]);
361
384
  whitePoint = params.getArray("WhitePoint");
362
385
  blackPoint = params.getArray("BlackPoint");
363
386
  const range = params.getArray("Range");
364
- return ["LabCS", whitePoint, blackPoint, range];
387
+ return new LabCS(whitePoint, blackPoint, range);
365
388
 
366
389
  default:
367
- throw new _util.FormatError(`unimplemented color space object "${mode}"`);
390
+ throw new _util.FormatError(`Unimplemented ColorSpace object: ${mode}`);
368
391
  }
369
392
  }
370
393
 
371
- throw new _util.FormatError(`unrecognized color space object: "${cs}"`);
394
+ throw new _util.FormatError(`Unrecognized ColorSpace object: ${cs}`);
372
395
  }
373
396
 
374
397
  static isDefaultDecode(decode, numComps) {
@@ -484,23 +507,18 @@ class IndexedCS extends ColorSpace {
484
507
  super("Indexed", 1);
485
508
  this.base = base;
486
509
  this.highVal = highVal;
487
- const baseNumComps = base.numComps;
488
- const length = baseNumComps * highVal;
510
+ const length = base.numComps * highVal;
511
+ this.lookup = new Uint8Array(length);
489
512
 
490
513
  if ((0, _primitives.isStream)(lookup)) {
491
- this.lookup = new Uint8Array(length);
492
514
  const bytes = lookup.getBytes(length);
493
515
  this.lookup.set(bytes);
494
- } else if ((0, _util.isString)(lookup)) {
495
- this.lookup = new Uint8Array(length);
496
-
516
+ } else if (typeof lookup === "string") {
497
517
  for (let i = 0; i < length; ++i) {
498
- this.lookup[i] = lookup.charCodeAt(i);
518
+ this.lookup[i] = lookup.charCodeAt(i) & 0xff;
499
519
  }
500
- } else if (lookup instanceof Uint8Array) {
501
- this.lookup = lookup;
502
520
  } else {
503
- throw new _util.FormatError(`Unrecognized lookup table: ${lookup}`);
521
+ throw new _util.FormatError(`IndexedCS - unrecognized lookup table: ${lookup}`);
504
522
  }
505
523
  }
506
524
 
@@ -762,6 +780,10 @@ const CalRGBCS = function CalRGBCSClosure() {
762
780
  return adjustToRange(0, 1, 12.92 * color);
763
781
  }
764
782
 
783
+ if (color >= 0.99554525) {
784
+ return 1;
785
+ }
786
+
765
787
  return adjustToRange(0, 1, (1 + 0.055) * color ** (1 / 2.4) - 0.055);
766
788
  }
767
789
 
@@ -834,9 +856,9 @@ const CalRGBCS = function CalRGBCSClosure() {
834
856
  const A = adjustToRange(0, 1, src[srcOffset] * scale);
835
857
  const B = adjustToRange(0, 1, src[srcOffset + 1] * scale);
836
858
  const C = adjustToRange(0, 1, src[srcOffset + 2] * scale);
837
- const AGR = A ** cs.GR;
838
- const BGG = B ** cs.GG;
839
- const CGB = C ** cs.GB;
859
+ const AGR = A === 1 ? 1 : A ** cs.GR;
860
+ const BGG = B === 1 ? 1 : B ** cs.GG;
861
+ const CGB = C === 1 ? 1 : C ** cs.GB;
840
862
  const X = cs.MXA * AGR + cs.MXB * BGG + cs.MXC * CGB;
841
863
  const Y = cs.MYA * AGR + cs.MYB * BGG + cs.MYC * CGB;
842
864
  const Z = cs.MZA * AGR + cs.MZB * BGG + cs.MZC * CGB;