@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 0.9.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 (174) hide show
  1. package/README.md +1 -1
  2. package/package.json +33 -18
  3. package/{src/display → pdfjs/dist}/display_utils.js +344 -139
  4. package/{src/display → pdfjs/dist}/fetch_stream.js +115 -97
  5. package/pdfjs/dist/get.js +1857 -0
  6. package/pdfjs/dist/index.js +767 -0
  7. package/pdfjs/dist/l10n_utils.js +140 -0
  8. package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
  9. package/{src/display → pdfjs/dist}/network.js +149 -87
  10. package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
  11. package/{src/display → pdfjs/dist}/node_stream.js +133 -98
  12. package/pdfjs/dist/pdf.js +12778 -0
  13. package/pdfjs/dist/pdf_link_service.js +638 -0
  14. package/pdfjs/dist/pdf_rendering_queue.js +199 -0
  15. package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
  16. package/pdfjs/dist/pdf_viewer.js +3598 -0
  17. package/pdfjs/dist/typeof.js +100 -0
  18. package/pdfjs/dist/ui_utils.js +1033 -0
  19. package/{src/shared → pdfjs/dist}/util.js +301 -287
  20. package/pdfjs/dist/worker.js +62813 -0
  21. package/src/vcf-pdf-viewer.js +77 -27
  22. package/theme/lumo/vcf-pdf-viewer-styles.js +1 -1
  23. package/theme/material/vcf-pdf-viewer.js +2 -2
  24. package/src/core/.eslintrc +0 -13
  25. package/src/core/annotation.js +0 -2948
  26. package/src/core/arithmetic_decoder.js +0 -182
  27. package/src/core/ascii_85_stream.js +0 -98
  28. package/src/core/ascii_hex_stream.js +0 -79
  29. package/src/core/base_stream.js +0 -110
  30. package/src/core/bidi.js +0 -438
  31. package/src/core/calibri_factors.js +0 -308
  32. package/src/core/catalog.js +0 -1459
  33. package/src/core/ccitt.js +0 -1062
  34. package/src/core/ccitt_stream.js +0 -60
  35. package/src/core/cff_font.js +0 -116
  36. package/src/core/cff_parser.js +0 -1949
  37. package/src/core/charsets.js +0 -119
  38. package/src/core/chunked_stream.js +0 -557
  39. package/src/core/cmap.js +0 -1039
  40. package/src/core/colorspace.js +0 -1533
  41. package/src/core/core_utils.js +0 -464
  42. package/src/core/crypto.js +0 -1900
  43. package/src/core/decode_stream.js +0 -170
  44. package/src/core/decrypt_stream.js +0 -59
  45. package/src/core/default_appearance.js +0 -99
  46. package/src/core/document.js +0 -1456
  47. package/src/core/encodings.js +0 -301
  48. package/src/core/evaluator.js +0 -4601
  49. package/src/core/file_spec.js +0 -108
  50. package/src/core/flate_stream.js +0 -402
  51. package/src/core/font_renderer.js +0 -882
  52. package/src/core/fonts.js +0 -3260
  53. package/src/core/fonts_utils.js +0 -221
  54. package/src/core/function.js +0 -1257
  55. package/src/core/glyf.js +0 -706
  56. package/src/core/glyphlist.js +0 -4558
  57. package/src/core/helvetica_factors.js +0 -353
  58. package/src/core/image.js +0 -802
  59. package/src/core/image_utils.js +0 -291
  60. package/src/core/jbig2.js +0 -2572
  61. package/src/core/jbig2_stream.js +0 -73
  62. package/src/core/jpeg_stream.js +0 -105
  63. package/src/core/jpg.js +0 -1416
  64. package/src/core/jpx.js +0 -2343
  65. package/src/core/jpx_stream.js +0 -87
  66. package/src/core/liberationsans_widths.js +0 -221
  67. package/src/core/lzw_stream.js +0 -150
  68. package/src/core/metadata_parser.js +0 -146
  69. package/src/core/metrics.js +0 -2970
  70. package/src/core/murmurhash3.js +0 -139
  71. package/src/core/myriadpro_factors.js +0 -290
  72. package/src/core/name_number_tree.js +0 -153
  73. package/src/core/object_loader.js +0 -149
  74. package/src/core/opentype_file_builder.js +0 -154
  75. package/src/core/operator_list.js +0 -734
  76. package/src/core/parser.js +0 -1416
  77. package/src/core/pattern.js +0 -985
  78. package/src/core/pdf_manager.js +0 -217
  79. package/src/core/predictor_stream.js +0 -238
  80. package/src/core/primitives.js +0 -402
  81. package/src/core/ps_parser.js +0 -272
  82. package/src/core/run_length_stream.js +0 -61
  83. package/src/core/segoeui_factors.js +0 -308
  84. package/src/core/standard_fonts.js +0 -817
  85. package/src/core/stream.js +0 -103
  86. package/src/core/struct_tree.js +0 -335
  87. package/src/core/to_unicode_map.js +0 -103
  88. package/src/core/type1_font.js +0 -421
  89. package/src/core/type1_parser.js +0 -776
  90. package/src/core/unicode.js +0 -1649
  91. package/src/core/worker.js +0 -848
  92. package/src/core/worker_stream.js +0 -135
  93. package/src/core/writer.js +0 -278
  94. package/src/core/xfa/bind.js +0 -652
  95. package/src/core/xfa/builder.js +0 -207
  96. package/src/core/xfa/config.js +0 -1926
  97. package/src/core/xfa/connection_set.js +0 -202
  98. package/src/core/xfa/data.js +0 -82
  99. package/src/core/xfa/datasets.js +0 -76
  100. package/src/core/xfa/factory.js +0 -111
  101. package/src/core/xfa/fonts.js +0 -181
  102. package/src/core/xfa/formcalc_lexer.js +0 -385
  103. package/src/core/xfa/formcalc_parser.js +0 -1340
  104. package/src/core/xfa/html_utils.js +0 -639
  105. package/src/core/xfa/layout.js +0 -383
  106. package/src/core/xfa/locale_set.js +0 -345
  107. package/src/core/xfa/namespaces.js +0 -81
  108. package/src/core/xfa/parser.js +0 -184
  109. package/src/core/xfa/setup.js +0 -38
  110. package/src/core/xfa/signature.js +0 -40
  111. package/src/core/xfa/som.js +0 -338
  112. package/src/core/xfa/stylesheet.js +0 -40
  113. package/src/core/xfa/template.js +0 -6260
  114. package/src/core/xfa/text.js +0 -290
  115. package/src/core/xfa/unknown.js +0 -29
  116. package/src/core/xfa/utils.js +0 -217
  117. package/src/core/xfa/xdp.js +0 -59
  118. package/src/core/xfa/xfa_object.js +0 -1130
  119. package/src/core/xfa/xhtml.js +0 -543
  120. package/src/core/xfa_fonts.js +0 -208
  121. package/src/core/xml_parser.js +0 -507
  122. package/src/core/xref.js +0 -899
  123. package/src/display/annotation_layer.js +0 -2107
  124. package/src/display/annotation_storage.js +0 -113
  125. package/src/display/api.js +0 -3292
  126. package/src/display/base_factory.js +0 -180
  127. package/src/display/canvas.js +0 -2828
  128. package/src/display/font_loader.js +0 -484
  129. package/src/display/metadata.js +0 -41
  130. package/src/display/network_utils.js +0 -100
  131. package/src/display/node_utils.js +0 -83
  132. package/src/display/optional_content_config.js +0 -189
  133. package/src/display/pattern_helper.js +0 -659
  134. package/src/display/svg.js +0 -1709
  135. package/src/display/text_layer.js +0 -847
  136. package/src/display/transport_stream.js +0 -303
  137. package/src/display/worker_options.js +0 -40
  138. package/src/display/xfa_layer.js +0 -204
  139. package/src/doc_helper.js +0 -25
  140. package/src/images/logo.svg +0 -41
  141. package/src/interfaces.js +0 -169
  142. package/src/license_header.js +0 -14
  143. package/src/license_header_libre.js +0 -21
  144. package/src/pdf.image_decoders.js +0 -46
  145. package/src/pdf.js +0 -146
  146. package/src/pdf.sandbox.external.js +0 -181
  147. package/src/pdf.sandbox.js +0 -151
  148. package/src/pdf.scripting.js +0 -25
  149. package/src/pdf.worker.entry.js +0 -19
  150. package/src/pdf.worker.js +0 -23
  151. package/src/scripting_api/aform.js +0 -608
  152. package/src/scripting_api/app.js +0 -621
  153. package/src/scripting_api/color.js +0 -129
  154. package/src/scripting_api/common.js +0 -58
  155. package/src/scripting_api/console.js +0 -38
  156. package/src/scripting_api/constants.js +0 -208
  157. package/src/scripting_api/doc.js +0 -1195
  158. package/src/scripting_api/error.js +0 -23
  159. package/src/scripting_api/event.js +0 -232
  160. package/src/scripting_api/field.js +0 -620
  161. package/src/scripting_api/fullscreen.js +0 -145
  162. package/src/scripting_api/initialization.js +0 -223
  163. package/src/scripting_api/pdf_object.js +0 -24
  164. package/src/scripting_api/print_params.js +0 -146
  165. package/src/scripting_api/proxy.js +0 -139
  166. package/src/scripting_api/thermometer.js +0 -69
  167. package/src/scripting_api/util.js +0 -581
  168. package/src/shared/.eslintrc +0 -13
  169. package/src/shared/cffStandardStrings.js +0 -311
  170. package/src/shared/compatibility.js +0 -114
  171. package/src/shared/fonts_utils.js +0 -429
  172. package/src/shared/is_node.js +0 -27
  173. package/src/shared/scripting_utils.js +0 -85
  174. package/src/worker_loader.js +0 -32
package/src/core/image.js DELETED
@@ -1,802 +0,0 @@
1
- /* Copyright 2012 Mozilla Foundation
2
- *
3
- * Licensed under the Apache License, Version 2.0 (the "License");
4
- * you may not use this file except in compliance with the License.
5
- * You may obtain a copy of the License at
6
- *
7
- * http://www.apache.org/licenses/LICENSE-2.0
8
- *
9
- * Unless required by applicable law or agreed to in writing, software
10
- * distributed under the License is distributed on an "AS IS" BASIS,
11
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
- * See the License for the specific language governing permissions and
13
- * limitations under the License.
14
- */
15
-
16
- import { assert, FormatError, ImageKind, info, warn } from "../shared/util.js";
17
- import { isName, isStream, Name } from "./primitives.js";
18
- import { ColorSpace } from "./colorspace.js";
19
- import { DecodeStream } from "./decode_stream.js";
20
- import { JpegStream } from "./jpeg_stream.js";
21
- import { JpxImage } from "./jpx.js";
22
-
23
- /**
24
- * Decode and clamp a value. The formula is different from the spec because we
25
- * don't decode to float range [0,1], we decode it in the [0,max] range.
26
- */
27
- function decodeAndClamp(value, addend, coefficient, max) {
28
- value = addend + value * coefficient;
29
- // Clamp the value to the range
30
- if (value < 0) {
31
- value = 0;
32
- } else if (value > max) {
33
- value = max;
34
- }
35
- return value;
36
- }
37
-
38
- /**
39
- * Resizes an image mask with 1 component.
40
- * @param {TypedArray} src - The source buffer.
41
- * @param {number} bpc - Number of bits per component.
42
- * @param {number} w1 - Original width.
43
- * @param {number} h1 - Original height.
44
- * @param {number} w2 - New width.
45
- * @param {number} h2 - New height.
46
- * @returns {TypedArray} The resized image mask buffer.
47
- */
48
- function resizeImageMask(src, bpc, w1, h1, w2, h2) {
49
- const length = w2 * h2;
50
- let dest;
51
- if (bpc <= 8) {
52
- dest = new Uint8Array(length);
53
- } else if (bpc <= 16) {
54
- dest = new Uint16Array(length);
55
- } else {
56
- dest = new Uint32Array(length);
57
- }
58
- const xRatio = w1 / w2;
59
- const yRatio = h1 / h2;
60
- let i,
61
- j,
62
- py,
63
- newIndex = 0,
64
- oldIndex;
65
- const xScaled = new Uint16Array(w2);
66
- const w1Scanline = w1;
67
-
68
- for (i = 0; i < w2; i++) {
69
- xScaled[i] = Math.floor(i * xRatio);
70
- }
71
- for (i = 0; i < h2; i++) {
72
- py = Math.floor(i * yRatio) * w1Scanline;
73
- for (j = 0; j < w2; j++) {
74
- oldIndex = py + xScaled[j];
75
- dest[newIndex++] = src[oldIndex];
76
- }
77
- }
78
- return dest;
79
- }
80
-
81
- class PDFImage {
82
- constructor({
83
- xref,
84
- res,
85
- image,
86
- isInline = false,
87
- smask = null,
88
- mask = null,
89
- isMask = false,
90
- pdfFunctionFactory,
91
- localColorSpaceCache,
92
- }) {
93
- this.image = image;
94
- const dict = image.dict;
95
-
96
- const filter = dict.get("Filter");
97
- if (isName(filter)) {
98
- switch (filter.name) {
99
- case "JPXDecode":
100
- const jpxImage = new JpxImage();
101
- jpxImage.parseImageProperties(image.stream);
102
- image.stream.reset();
103
-
104
- image.width = jpxImage.width;
105
- image.height = jpxImage.height;
106
- image.bitsPerComponent = jpxImage.bitsPerComponent;
107
- image.numComps = jpxImage.componentsCount;
108
- break;
109
- case "JBIG2Decode":
110
- image.bitsPerComponent = 1;
111
- image.numComps = 1;
112
- break;
113
- }
114
- }
115
- // TODO cache rendered images?
116
-
117
- let width = dict.get("Width", "W");
118
- let height = dict.get("Height", "H");
119
-
120
- if (
121
- Number.isInteger(image.width) &&
122
- image.width > 0 &&
123
- Number.isInteger(image.height) &&
124
- image.height > 0 &&
125
- (image.width !== width || image.height !== height)
126
- ) {
127
- warn(
128
- "PDFImage - using the Width/Height of the image data, " +
129
- "rather than the image dictionary."
130
- );
131
- width = image.width;
132
- height = image.height;
133
- }
134
- if (width < 1 || height < 1) {
135
- throw new FormatError(
136
- `Invalid image width: ${width} or height: ${height}`
137
- );
138
- }
139
- this.width = width;
140
- this.height = height;
141
-
142
- this.interpolate = dict.get("Interpolate", "I") || false;
143
- this.imageMask = dict.get("ImageMask", "IM") || false;
144
- this.matte = dict.get("Matte") || false;
145
-
146
- let bitsPerComponent = image.bitsPerComponent;
147
- if (!bitsPerComponent) {
148
- bitsPerComponent = dict.get("BitsPerComponent", "BPC");
149
- if (!bitsPerComponent) {
150
- if (this.imageMask) {
151
- bitsPerComponent = 1;
152
- } else {
153
- throw new FormatError(
154
- `Bits per component missing in image: ${this.imageMask}`
155
- );
156
- }
157
- }
158
- }
159
- this.bpc = bitsPerComponent;
160
-
161
- if (!this.imageMask) {
162
- let colorSpace = dict.getRaw("ColorSpace") || dict.getRaw("CS");
163
- if (!colorSpace) {
164
- info("JPX images (which do not require color spaces)");
165
- switch (image.numComps) {
166
- case 1:
167
- colorSpace = Name.get("DeviceGray");
168
- break;
169
- case 3:
170
- colorSpace = Name.get("DeviceRGB");
171
- break;
172
- case 4:
173
- colorSpace = Name.get("DeviceCMYK");
174
- break;
175
- default:
176
- throw new Error(
177
- `JPX images with ${image.numComps} ` +
178
- "color components not supported."
179
- );
180
- }
181
- }
182
- this.colorSpace = ColorSpace.parse({
183
- cs: colorSpace,
184
- xref,
185
- resources: isInline ? res : null,
186
- pdfFunctionFactory,
187
- localColorSpaceCache,
188
- });
189
- this.numComps = this.colorSpace.numComps;
190
- }
191
-
192
- this.decode = dict.getArray("Decode", "D");
193
- this.needsDecode = false;
194
- if (
195
- this.decode &&
196
- ((this.colorSpace &&
197
- !this.colorSpace.isDefaultDecode(this.decode, bitsPerComponent)) ||
198
- (isMask &&
199
- !ColorSpace.isDefaultDecode(this.decode, /* numComps = */ 1)))
200
- ) {
201
- this.needsDecode = true;
202
- // Do some preprocessing to avoid more math.
203
- const max = (1 << bitsPerComponent) - 1;
204
- this.decodeCoefficients = [];
205
- this.decodeAddends = [];
206
- const isIndexed = this.colorSpace && this.colorSpace.name === "Indexed";
207
- for (let i = 0, j = 0; i < this.decode.length; i += 2, ++j) {
208
- const dmin = this.decode[i];
209
- const dmax = this.decode[i + 1];
210
- this.decodeCoefficients[j] = isIndexed
211
- ? (dmax - dmin) / max
212
- : dmax - dmin;
213
- this.decodeAddends[j] = isIndexed ? dmin : max * dmin;
214
- }
215
- }
216
-
217
- if (smask) {
218
- this.smask = new PDFImage({
219
- xref,
220
- res,
221
- image: smask,
222
- isInline,
223
- pdfFunctionFactory,
224
- localColorSpaceCache,
225
- });
226
- } else if (mask) {
227
- if (isStream(mask)) {
228
- const maskDict = mask.dict,
229
- imageMask = maskDict.get("ImageMask", "IM");
230
- if (!imageMask) {
231
- warn("Ignoring /Mask in image without /ImageMask.");
232
- } else {
233
- this.mask = new PDFImage({
234
- xref,
235
- res,
236
- image: mask,
237
- isInline,
238
- isMask: true,
239
- pdfFunctionFactory,
240
- localColorSpaceCache,
241
- });
242
- }
243
- } else {
244
- // Color key mask (just an array).
245
- this.mask = mask;
246
- }
247
- }
248
- }
249
-
250
- /**
251
- * Handles processing of image data and returns the Promise that is resolved
252
- * with a PDFImage when the image is ready to be used.
253
- */
254
- static async buildImage({
255
- xref,
256
- res,
257
- image,
258
- isInline = false,
259
- pdfFunctionFactory,
260
- localColorSpaceCache,
261
- }) {
262
- const imageData = image;
263
- let smaskData = null;
264
- let maskData = null;
265
-
266
- const smask = image.dict.get("SMask");
267
- const mask = image.dict.get("Mask");
268
-
269
- if (smask) {
270
- smaskData = smask;
271
- } else if (mask) {
272
- if (isStream(mask) || Array.isArray(mask)) {
273
- maskData = mask;
274
- } else {
275
- warn("Unsupported mask format.");
276
- }
277
- }
278
-
279
- return new PDFImage({
280
- xref,
281
- res,
282
- image: imageData,
283
- isInline,
284
- smask: smaskData,
285
- mask: maskData,
286
- pdfFunctionFactory,
287
- localColorSpaceCache,
288
- });
289
- }
290
-
291
- static createMask({
292
- imgArray,
293
- width,
294
- height,
295
- imageIsFromDecodeStream,
296
- inverseDecode,
297
- }) {
298
- if (
299
- typeof PDFJSDev === "undefined" ||
300
- PDFJSDev.test("!PRODUCTION || TESTING")
301
- ) {
302
- assert(
303
- imgArray instanceof Uint8ClampedArray,
304
- 'PDFImage.createMask: Unsupported "imgArray" type.'
305
- );
306
- }
307
- // |imgArray| might not contain full data for every pixel of the mask, so
308
- // we need to distinguish between |computedLength| and |actualLength|.
309
- // In particular, if inverseDecode is true, then the array we return must
310
- // have a length of |computedLength|.
311
-
312
- const computedLength = ((width + 7) >> 3) * height;
313
- const actualLength = imgArray.byteLength;
314
- const haveFullData = computedLength === actualLength;
315
- let data, i;
316
-
317
- if (imageIsFromDecodeStream && (!inverseDecode || haveFullData)) {
318
- // imgArray came from a DecodeStream and its data is in an appropriate
319
- // form, so we can just transfer it.
320
- data = imgArray;
321
- } else if (!inverseDecode) {
322
- data = new Uint8ClampedArray(actualLength);
323
- data.set(imgArray);
324
- } else {
325
- data = new Uint8ClampedArray(computedLength);
326
- data.set(imgArray);
327
- for (i = actualLength; i < computedLength; i++) {
328
- data[i] = 0xff;
329
- }
330
- }
331
-
332
- // If necessary, invert the original mask data (but not any extra we might
333
- // have added above). It's safe to modify the array -- whether it's the
334
- // original or a copy, we're about to transfer it anyway, so nothing else
335
- // in this thread can be relying on its contents.
336
- if (inverseDecode) {
337
- for (i = 0; i < actualLength; i++) {
338
- data[i] ^= 0xff;
339
- }
340
- }
341
-
342
- return { data, width, height };
343
- }
344
-
345
- get drawWidth() {
346
- return Math.max(
347
- this.width,
348
- (this.smask && this.smask.width) || 0,
349
- (this.mask && this.mask.width) || 0
350
- );
351
- }
352
-
353
- get drawHeight() {
354
- return Math.max(
355
- this.height,
356
- (this.smask && this.smask.height) || 0,
357
- (this.mask && this.mask.height) || 0
358
- );
359
- }
360
-
361
- decodeBuffer(buffer) {
362
- const bpc = this.bpc;
363
- const numComps = this.numComps;
364
-
365
- const decodeAddends = this.decodeAddends;
366
- const decodeCoefficients = this.decodeCoefficients;
367
- const max = (1 << bpc) - 1;
368
- let i, ii;
369
-
370
- if (bpc === 1) {
371
- // If the buffer needed decode that means it just needs to be inverted.
372
- for (i = 0, ii = buffer.length; i < ii; i++) {
373
- buffer[i] = +!buffer[i];
374
- }
375
- return;
376
- }
377
- let index = 0;
378
- for (i = 0, ii = this.width * this.height; i < ii; i++) {
379
- for (let j = 0; j < numComps; j++) {
380
- buffer[index] = decodeAndClamp(
381
- buffer[index],
382
- decodeAddends[j],
383
- decodeCoefficients[j],
384
- max
385
- );
386
- index++;
387
- }
388
- }
389
- }
390
-
391
- getComponents(buffer) {
392
- const bpc = this.bpc;
393
-
394
- // This image doesn't require any extra work.
395
- if (bpc === 8) {
396
- return buffer;
397
- }
398
-
399
- const width = this.width;
400
- const height = this.height;
401
- const numComps = this.numComps;
402
-
403
- const length = width * height * numComps;
404
- let bufferPos = 0;
405
- let output;
406
- if (bpc <= 8) {
407
- output = new Uint8Array(length);
408
- } else if (bpc <= 16) {
409
- output = new Uint16Array(length);
410
- } else {
411
- output = new Uint32Array(length);
412
- }
413
- const rowComps = width * numComps;
414
-
415
- const max = (1 << bpc) - 1;
416
- let i = 0,
417
- ii,
418
- buf;
419
-
420
- if (bpc === 1) {
421
- // Optimization for reading 1 bpc images.
422
- let mask, loop1End, loop2End;
423
- for (let j = 0; j < height; j++) {
424
- loop1End = i + (rowComps & ~7);
425
- loop2End = i + rowComps;
426
-
427
- // unroll loop for all full bytes
428
- while (i < loop1End) {
429
- buf = buffer[bufferPos++];
430
- output[i] = (buf >> 7) & 1;
431
- output[i + 1] = (buf >> 6) & 1;
432
- output[i + 2] = (buf >> 5) & 1;
433
- output[i + 3] = (buf >> 4) & 1;
434
- output[i + 4] = (buf >> 3) & 1;
435
- output[i + 5] = (buf >> 2) & 1;
436
- output[i + 6] = (buf >> 1) & 1;
437
- output[i + 7] = buf & 1;
438
- i += 8;
439
- }
440
-
441
- // handle remaining bits
442
- if (i < loop2End) {
443
- buf = buffer[bufferPos++];
444
- mask = 128;
445
- while (i < loop2End) {
446
- output[i++] = +!!(buf & mask);
447
- mask >>= 1;
448
- }
449
- }
450
- }
451
- } else {
452
- // The general case that handles all other bpc values.
453
- let bits = 0;
454
- buf = 0;
455
- for (i = 0, ii = length; i < ii; ++i) {
456
- if (i % rowComps === 0) {
457
- buf = 0;
458
- bits = 0;
459
- }
460
-
461
- while (bits < bpc) {
462
- buf = (buf << 8) | buffer[bufferPos++];
463
- bits += 8;
464
- }
465
-
466
- const remainingBits = bits - bpc;
467
- let value = buf >> remainingBits;
468
- if (value < 0) {
469
- value = 0;
470
- } else if (value > max) {
471
- value = max;
472
- }
473
- output[i] = value;
474
- buf &= (1 << remainingBits) - 1;
475
- bits = remainingBits;
476
- }
477
- }
478
- return output;
479
- }
480
-
481
- fillOpacity(rgbaBuf, width, height, actualHeight, image) {
482
- if (
483
- typeof PDFJSDev === "undefined" ||
484
- PDFJSDev.test("!PRODUCTION || TESTING")
485
- ) {
486
- assert(
487
- rgbaBuf instanceof Uint8ClampedArray,
488
- 'PDFImage.fillOpacity: Unsupported "rgbaBuf" type.'
489
- );
490
- }
491
- const smask = this.smask;
492
- const mask = this.mask;
493
- let alphaBuf, sw, sh, i, ii, j;
494
-
495
- if (smask) {
496
- sw = smask.width;
497
- sh = smask.height;
498
- alphaBuf = new Uint8ClampedArray(sw * sh);
499
- smask.fillGrayBuffer(alphaBuf);
500
- if (sw !== width || sh !== height) {
501
- alphaBuf = resizeImageMask(alphaBuf, smask.bpc, sw, sh, width, height);
502
- }
503
- } else if (mask) {
504
- if (mask instanceof PDFImage) {
505
- sw = mask.width;
506
- sh = mask.height;
507
- alphaBuf = new Uint8ClampedArray(sw * sh);
508
- mask.numComps = 1;
509
- mask.fillGrayBuffer(alphaBuf);
510
-
511
- // Need to invert values in rgbaBuf
512
- for (i = 0, ii = sw * sh; i < ii; ++i) {
513
- alphaBuf[i] = 255 - alphaBuf[i];
514
- }
515
-
516
- if (sw !== width || sh !== height) {
517
- alphaBuf = resizeImageMask(alphaBuf, mask.bpc, sw, sh, width, height);
518
- }
519
- } else if (Array.isArray(mask)) {
520
- // Color key mask: if any of the components are outside the range
521
- // then they should be painted.
522
- alphaBuf = new Uint8ClampedArray(width * height);
523
- const numComps = this.numComps;
524
- for (i = 0, ii = width * height; i < ii; ++i) {
525
- let opacity = 0;
526
- const imageOffset = i * numComps;
527
- for (j = 0; j < numComps; ++j) {
528
- const color = image[imageOffset + j];
529
- const maskOffset = j * 2;
530
- if (color < mask[maskOffset] || color > mask[maskOffset + 1]) {
531
- opacity = 255;
532
- break;
533
- }
534
- }
535
- alphaBuf[i] = opacity;
536
- }
537
- } else {
538
- throw new FormatError("Unknown mask format.");
539
- }
540
- }
541
-
542
- if (alphaBuf) {
543
- for (i = 0, j = 3, ii = width * actualHeight; i < ii; ++i, j += 4) {
544
- rgbaBuf[j] = alphaBuf[i];
545
- }
546
- } else {
547
- // No mask.
548
- for (i = 0, j = 3, ii = width * actualHeight; i < ii; ++i, j += 4) {
549
- rgbaBuf[j] = 255;
550
- }
551
- }
552
- }
553
-
554
- undoPreblend(buffer, width, height) {
555
- if (
556
- typeof PDFJSDev === "undefined" ||
557
- PDFJSDev.test("!PRODUCTION || TESTING")
558
- ) {
559
- assert(
560
- buffer instanceof Uint8ClampedArray,
561
- 'PDFImage.undoPreblend: Unsupported "buffer" type.'
562
- );
563
- }
564
- const matte = this.smask && this.smask.matte;
565
- if (!matte) {
566
- return;
567
- }
568
- const matteRgb = this.colorSpace.getRgb(matte, 0);
569
- const matteR = matteRgb[0];
570
- const matteG = matteRgb[1];
571
- const matteB = matteRgb[2];
572
- const length = width * height * 4;
573
- for (let i = 0; i < length; i += 4) {
574
- const alpha = buffer[i + 3];
575
- if (alpha === 0) {
576
- // according formula we have to get Infinity in all components
577
- // making it white (typical paper color) should be okay
578
- buffer[i] = 255;
579
- buffer[i + 1] = 255;
580
- buffer[i + 2] = 255;
581
- continue;
582
- }
583
- const k = 255 / alpha;
584
- buffer[i] = (buffer[i] - matteR) * k + matteR;
585
- buffer[i + 1] = (buffer[i + 1] - matteG) * k + matteG;
586
- buffer[i + 2] = (buffer[i + 2] - matteB) * k + matteB;
587
- }
588
- }
589
-
590
- createImageData(forceRGBA = false) {
591
- const drawWidth = this.drawWidth;
592
- const drawHeight = this.drawHeight;
593
- const imgData = {
594
- width: drawWidth,
595
- height: drawHeight,
596
- kind: 0,
597
- data: null,
598
- // Other fields are filled in below.
599
- };
600
-
601
- const numComps = this.numComps;
602
- const originalWidth = this.width;
603
- const originalHeight = this.height;
604
- const bpc = this.bpc;
605
-
606
- // Rows start at byte boundary.
607
- const rowBytes = (originalWidth * numComps * bpc + 7) >> 3;
608
- let imgArray;
609
-
610
- if (!forceRGBA) {
611
- // If it is a 1-bit-per-pixel grayscale (i.e. black-and-white) image
612
- // without any complications, we pass a same-sized copy to the main
613
- // thread rather than expanding by 32x to RGBA form. This saves *lots*
614
- // of memory for many scanned documents. It's also much faster.
615
- //
616
- // Similarly, if it is a 24-bit-per pixel RGB image without any
617
- // complications, we avoid expanding by 1.333x to RGBA form.
618
- let kind;
619
- if (this.colorSpace.name === "DeviceGray" && bpc === 1) {
620
- kind = ImageKind.GRAYSCALE_1BPP;
621
- } else if (
622
- this.colorSpace.name === "DeviceRGB" &&
623
- bpc === 8 &&
624
- !this.needsDecode
625
- ) {
626
- kind = ImageKind.RGB_24BPP;
627
- }
628
- if (
629
- kind &&
630
- !this.smask &&
631
- !this.mask &&
632
- drawWidth === originalWidth &&
633
- drawHeight === originalHeight
634
- ) {
635
- imgData.kind = kind;
636
-
637
- imgArray = this.getImageBytes(originalHeight * rowBytes);
638
- // If imgArray came from a DecodeStream, we're safe to transfer it
639
- // (and thus detach its underlying buffer) because it will constitute
640
- // the entire DecodeStream's data. But if it came from a Stream, we
641
- // need to copy it because it'll only be a portion of the Stream's
642
- // data, and the rest will be read later on.
643
- if (this.image instanceof DecodeStream) {
644
- imgData.data = imgArray;
645
- } else {
646
- const newArray = new Uint8ClampedArray(imgArray.length);
647
- newArray.set(imgArray);
648
- imgData.data = newArray;
649
- }
650
- if (this.needsDecode) {
651
- // Invert the buffer (which must be grayscale if we reached here).
652
- assert(
653
- kind === ImageKind.GRAYSCALE_1BPP,
654
- "PDFImage.createImageData: The image must be grayscale."
655
- );
656
- const buffer = imgData.data;
657
- for (let i = 0, ii = buffer.length; i < ii; i++) {
658
- buffer[i] ^= 0xff;
659
- }
660
- }
661
- return imgData;
662
- }
663
- if (this.image instanceof JpegStream && !this.smask && !this.mask) {
664
- let imageLength = originalHeight * rowBytes;
665
- switch (this.colorSpace.name) {
666
- case "DeviceGray":
667
- // Avoid truncating the image, since `JpegImage.getData`
668
- // will expand the image data when `forceRGB === true`.
669
- imageLength *= 3;
670
- /* falls through */
671
- case "DeviceRGB":
672
- case "DeviceCMYK":
673
- imgData.kind = ImageKind.RGB_24BPP;
674
- imgData.data = this.getImageBytes(
675
- imageLength,
676
- drawWidth,
677
- drawHeight,
678
- /* forceRGB = */ true
679
- );
680
- return imgData;
681
- }
682
- }
683
- }
684
-
685
- imgArray = this.getImageBytes(originalHeight * rowBytes);
686
- // imgArray can be incomplete (e.g. after CCITT fax encoding).
687
- const actualHeight =
688
- 0 | (((imgArray.length / rowBytes) * drawHeight) / originalHeight);
689
-
690
- const comps = this.getComponents(imgArray);
691
-
692
- // If opacity data is present, use RGBA_32BPP form. Otherwise, use the
693
- // more compact RGB_24BPP form if allowable.
694
- let alpha01, maybeUndoPreblend;
695
- if (!forceRGBA && !this.smask && !this.mask) {
696
- imgData.kind = ImageKind.RGB_24BPP;
697
- imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 3);
698
- alpha01 = 0;
699
- maybeUndoPreblend = false;
700
- } else {
701
- imgData.kind = ImageKind.RGBA_32BPP;
702
- imgData.data = new Uint8ClampedArray(drawWidth * drawHeight * 4);
703
- alpha01 = 1;
704
- maybeUndoPreblend = true;
705
-
706
- // Color key masking (opacity) must be performed before decoding.
707
- this.fillOpacity(
708
- imgData.data,
709
- drawWidth,
710
- drawHeight,
711
- actualHeight,
712
- comps
713
- );
714
- }
715
-
716
- if (this.needsDecode) {
717
- this.decodeBuffer(comps);
718
- }
719
- this.colorSpace.fillRgb(
720
- imgData.data,
721
- originalWidth,
722
- originalHeight,
723
- drawWidth,
724
- drawHeight,
725
- actualHeight,
726
- bpc,
727
- comps,
728
- alpha01
729
- );
730
- if (maybeUndoPreblend) {
731
- this.undoPreblend(imgData.data, drawWidth, actualHeight);
732
- }
733
-
734
- return imgData;
735
- }
736
-
737
- fillGrayBuffer(buffer) {
738
- if (
739
- typeof PDFJSDev === "undefined" ||
740
- PDFJSDev.test("!PRODUCTION || TESTING")
741
- ) {
742
- assert(
743
- buffer instanceof Uint8ClampedArray,
744
- 'PDFImage.fillGrayBuffer: Unsupported "buffer" type.'
745
- );
746
- }
747
- const numComps = this.numComps;
748
- if (numComps !== 1) {
749
- throw new FormatError(
750
- `Reading gray scale from a color image: ${numComps}`
751
- );
752
- }
753
-
754
- const width = this.width;
755
- const height = this.height;
756
- const bpc = this.bpc;
757
-
758
- // rows start at byte boundary
759
- const rowBytes = (width * numComps * bpc + 7) >> 3;
760
- const imgArray = this.getImageBytes(height * rowBytes);
761
-
762
- const comps = this.getComponents(imgArray);
763
- let i, length;
764
-
765
- if (bpc === 1) {
766
- // inline decoding (= inversion) for 1 bpc images
767
- length = width * height;
768
- if (this.needsDecode) {
769
- // invert and scale to {0, 255}
770
- for (i = 0; i < length; ++i) {
771
- buffer[i] = (comps[i] - 1) & 255;
772
- }
773
- } else {
774
- // scale to {0, 255}
775
- for (i = 0; i < length; ++i) {
776
- buffer[i] = -comps[i] & 255;
777
- }
778
- }
779
- return;
780
- }
781
-
782
- if (this.needsDecode) {
783
- this.decodeBuffer(comps);
784
- }
785
- length = width * height;
786
- // we aren't using a colorspace so we need to scale the value
787
- const scale = 255 / ((1 << bpc) - 1);
788
- for (i = 0; i < length; ++i) {
789
- buffer[i] = scale * comps[i];
790
- }
791
- }
792
-
793
- getImageBytes(length, drawWidth, drawHeight, forceRGB = false) {
794
- this.image.reset();
795
- this.image.drawWidth = drawWidth || this.width;
796
- this.image.drawHeight = drawHeight || this.height;
797
- this.image.forceRGB = !!forceRGB;
798
- return this.image.getBytes(length, /* forceClamped = */ true);
799
- }
800
- }
801
-
802
- export { PDFImage };