@vaadin-component-factory/vcf-pdf-viewer 0.9.0 → 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (172) hide show
  1. package/README.md +1 -1
  2. package/package.json +42 -26
  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/l10n_utils.js +140 -0
  6. package/{src/shared → pdfjs/dist}/message_handler.js +243 -259
  7. package/{src/display → pdfjs/dist}/network.js +149 -87
  8. package/{src/display/content_disposition.js → pdfjs/dist/network_utils.js} +167 -55
  9. package/{src/display → pdfjs/dist}/node_stream.js +133 -98
  10. package/pdfjs/dist/pdf.js +12778 -0
  11. package/pdfjs/dist/pdf_link_service.js +638 -0
  12. package/pdfjs/dist/pdf_rendering_queue.js +199 -0
  13. package/pdfjs/dist/pdf_thumbnail_viewer.js +819 -0
  14. package/pdfjs/dist/pdf_viewer.js +3598 -0
  15. package/pdfjs/dist/ui_utils.js +1033 -0
  16. package/{src/shared → pdfjs/dist}/util.js +301 -287
  17. package/pdfjs/dist/worker.js +62813 -0
  18. package/src/vcf-pdf-viewer.js +98 -46
  19. package/theme/lumo/vcf-pdf-viewer-styles.js +4 -4
  20. package/theme/material/vcf-pdf-viewer-styles.js +4 -4
  21. package/theme/material/vcf-pdf-viewer.js +2 -2
  22. package/src/core/.eslintrc +0 -13
  23. package/src/core/annotation.js +0 -2948
  24. package/src/core/arithmetic_decoder.js +0 -182
  25. package/src/core/ascii_85_stream.js +0 -98
  26. package/src/core/ascii_hex_stream.js +0 -79
  27. package/src/core/base_stream.js +0 -110
  28. package/src/core/bidi.js +0 -438
  29. package/src/core/calibri_factors.js +0 -308
  30. package/src/core/catalog.js +0 -1459
  31. package/src/core/ccitt.js +0 -1062
  32. package/src/core/ccitt_stream.js +0 -60
  33. package/src/core/cff_font.js +0 -116
  34. package/src/core/cff_parser.js +0 -1949
  35. package/src/core/charsets.js +0 -119
  36. package/src/core/chunked_stream.js +0 -557
  37. package/src/core/cmap.js +0 -1039
  38. package/src/core/colorspace.js +0 -1533
  39. package/src/core/core_utils.js +0 -464
  40. package/src/core/crypto.js +0 -1900
  41. package/src/core/decode_stream.js +0 -170
  42. package/src/core/decrypt_stream.js +0 -59
  43. package/src/core/default_appearance.js +0 -99
  44. package/src/core/document.js +0 -1456
  45. package/src/core/encodings.js +0 -301
  46. package/src/core/evaluator.js +0 -4601
  47. package/src/core/file_spec.js +0 -108
  48. package/src/core/flate_stream.js +0 -402
  49. package/src/core/font_renderer.js +0 -882
  50. package/src/core/fonts.js +0 -3260
  51. package/src/core/fonts_utils.js +0 -221
  52. package/src/core/function.js +0 -1257
  53. package/src/core/glyf.js +0 -706
  54. package/src/core/glyphlist.js +0 -4558
  55. package/src/core/helvetica_factors.js +0 -353
  56. package/src/core/image.js +0 -802
  57. package/src/core/image_utils.js +0 -291
  58. package/src/core/jbig2.js +0 -2572
  59. package/src/core/jbig2_stream.js +0 -73
  60. package/src/core/jpeg_stream.js +0 -105
  61. package/src/core/jpg.js +0 -1416
  62. package/src/core/jpx.js +0 -2343
  63. package/src/core/jpx_stream.js +0 -87
  64. package/src/core/liberationsans_widths.js +0 -221
  65. package/src/core/lzw_stream.js +0 -150
  66. package/src/core/metadata_parser.js +0 -146
  67. package/src/core/metrics.js +0 -2970
  68. package/src/core/murmurhash3.js +0 -139
  69. package/src/core/myriadpro_factors.js +0 -290
  70. package/src/core/name_number_tree.js +0 -153
  71. package/src/core/object_loader.js +0 -149
  72. package/src/core/opentype_file_builder.js +0 -154
  73. package/src/core/operator_list.js +0 -734
  74. package/src/core/parser.js +0 -1416
  75. package/src/core/pattern.js +0 -985
  76. package/src/core/pdf_manager.js +0 -217
  77. package/src/core/predictor_stream.js +0 -238
  78. package/src/core/primitives.js +0 -402
  79. package/src/core/ps_parser.js +0 -272
  80. package/src/core/run_length_stream.js +0 -61
  81. package/src/core/segoeui_factors.js +0 -308
  82. package/src/core/standard_fonts.js +0 -817
  83. package/src/core/stream.js +0 -103
  84. package/src/core/struct_tree.js +0 -335
  85. package/src/core/to_unicode_map.js +0 -103
  86. package/src/core/type1_font.js +0 -421
  87. package/src/core/type1_parser.js +0 -776
  88. package/src/core/unicode.js +0 -1649
  89. package/src/core/worker.js +0 -848
  90. package/src/core/worker_stream.js +0 -135
  91. package/src/core/writer.js +0 -278
  92. package/src/core/xfa/bind.js +0 -652
  93. package/src/core/xfa/builder.js +0 -207
  94. package/src/core/xfa/config.js +0 -1926
  95. package/src/core/xfa/connection_set.js +0 -202
  96. package/src/core/xfa/data.js +0 -82
  97. package/src/core/xfa/datasets.js +0 -76
  98. package/src/core/xfa/factory.js +0 -111
  99. package/src/core/xfa/fonts.js +0 -181
  100. package/src/core/xfa/formcalc_lexer.js +0 -385
  101. package/src/core/xfa/formcalc_parser.js +0 -1340
  102. package/src/core/xfa/html_utils.js +0 -639
  103. package/src/core/xfa/layout.js +0 -383
  104. package/src/core/xfa/locale_set.js +0 -345
  105. package/src/core/xfa/namespaces.js +0 -81
  106. package/src/core/xfa/parser.js +0 -184
  107. package/src/core/xfa/setup.js +0 -38
  108. package/src/core/xfa/signature.js +0 -40
  109. package/src/core/xfa/som.js +0 -338
  110. package/src/core/xfa/stylesheet.js +0 -40
  111. package/src/core/xfa/template.js +0 -6260
  112. package/src/core/xfa/text.js +0 -290
  113. package/src/core/xfa/unknown.js +0 -29
  114. package/src/core/xfa/utils.js +0 -217
  115. package/src/core/xfa/xdp.js +0 -59
  116. package/src/core/xfa/xfa_object.js +0 -1130
  117. package/src/core/xfa/xhtml.js +0 -543
  118. package/src/core/xfa_fonts.js +0 -208
  119. package/src/core/xml_parser.js +0 -507
  120. package/src/core/xref.js +0 -899
  121. package/src/display/annotation_layer.js +0 -2107
  122. package/src/display/annotation_storage.js +0 -113
  123. package/src/display/api.js +0 -3292
  124. package/src/display/base_factory.js +0 -180
  125. package/src/display/canvas.js +0 -2828
  126. package/src/display/font_loader.js +0 -484
  127. package/src/display/metadata.js +0 -41
  128. package/src/display/network_utils.js +0 -100
  129. package/src/display/node_utils.js +0 -83
  130. package/src/display/optional_content_config.js +0 -189
  131. package/src/display/pattern_helper.js +0 -659
  132. package/src/display/svg.js +0 -1709
  133. package/src/display/text_layer.js +0 -847
  134. package/src/display/transport_stream.js +0 -303
  135. package/src/display/worker_options.js +0 -40
  136. package/src/display/xfa_layer.js +0 -204
  137. package/src/doc_helper.js +0 -25
  138. package/src/images/logo.svg +0 -41
  139. package/src/interfaces.js +0 -169
  140. package/src/license_header.js +0 -14
  141. package/src/license_header_libre.js +0 -21
  142. package/src/pdf.image_decoders.js +0 -46
  143. package/src/pdf.js +0 -146
  144. package/src/pdf.sandbox.external.js +0 -181
  145. package/src/pdf.sandbox.js +0 -151
  146. package/src/pdf.scripting.js +0 -25
  147. package/src/pdf.worker.entry.js +0 -19
  148. package/src/pdf.worker.js +0 -23
  149. package/src/scripting_api/aform.js +0 -608
  150. package/src/scripting_api/app.js +0 -621
  151. package/src/scripting_api/color.js +0 -129
  152. package/src/scripting_api/common.js +0 -58
  153. package/src/scripting_api/console.js +0 -38
  154. package/src/scripting_api/constants.js +0 -208
  155. package/src/scripting_api/doc.js +0 -1195
  156. package/src/scripting_api/error.js +0 -23
  157. package/src/scripting_api/event.js +0 -232
  158. package/src/scripting_api/field.js +0 -620
  159. package/src/scripting_api/fullscreen.js +0 -145
  160. package/src/scripting_api/initialization.js +0 -223
  161. package/src/scripting_api/pdf_object.js +0 -24
  162. package/src/scripting_api/print_params.js +0 -146
  163. package/src/scripting_api/proxy.js +0 -139
  164. package/src/scripting_api/thermometer.js +0 -69
  165. package/src/scripting_api/util.js +0 -581
  166. package/src/shared/.eslintrc +0 -13
  167. package/src/shared/cffStandardStrings.js +0 -311
  168. package/src/shared/compatibility.js +0 -114
  169. package/src/shared/fonts_utils.js +0 -429
  170. package/src/shared/is_node.js +0 -27
  171. package/src/shared/scripting_utils.js +0 -85
  172. package/src/worker_loader.js +0 -32
@@ -1,1533 +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 {
17
- assert,
18
- FormatError,
19
- info,
20
- shadow,
21
- unreachable,
22
- warn,
23
- } from "../shared/util.js";
24
- import { isDict, isName, isStream, Name, Ref } from "./primitives.js";
25
- import { MissingDataException } from "./core_utils.js";
26
-
27
- /**
28
- * Resizes an RGB image with 3 components.
29
- * @param {TypedArray} src - The source buffer.
30
- * @param {TypedArray} dest - The destination buffer.
31
- * @param {number} w1 - Original width.
32
- * @param {number} h1 - Original height.
33
- * @param {number} w2 - New width.
34
- * @param {number} h2 - New height.
35
- * @param {number} alpha01 - Size reserved for the alpha channel.
36
- */
37
- function resizeRgbImage(src, dest, w1, h1, w2, h2, alpha01) {
38
- const COMPONENTS = 3;
39
- alpha01 = alpha01 !== 1 ? 0 : alpha01;
40
- const xRatio = w1 / w2;
41
- const yRatio = h1 / h2;
42
- let newIndex = 0,
43
- oldIndex;
44
- const xScaled = new Uint16Array(w2);
45
- const w1Scanline = w1 * COMPONENTS;
46
-
47
- for (let i = 0; i < w2; i++) {
48
- xScaled[i] = Math.floor(i * xRatio) * COMPONENTS;
49
- }
50
- for (let i = 0; i < h2; i++) {
51
- const py = Math.floor(i * yRatio) * w1Scanline;
52
- for (let j = 0; j < w2; j++) {
53
- oldIndex = py + xScaled[j];
54
- dest[newIndex++] = src[oldIndex++];
55
- dest[newIndex++] = src[oldIndex++];
56
- dest[newIndex++] = src[oldIndex++];
57
- newIndex += alpha01;
58
- }
59
- }
60
- }
61
-
62
- class ColorSpace {
63
- constructor(name, numComps) {
64
- if (this.constructor === ColorSpace) {
65
- unreachable("Cannot initialize ColorSpace.");
66
- }
67
- this.name = name;
68
- this.numComps = numComps;
69
- }
70
-
71
- /**
72
- * Converts the color value to the RGB color. The color components are
73
- * located in the src array starting from the srcOffset. Returns the array
74
- * of the rgb components, each value ranging from [0,255].
75
- */
76
- getRgb(src, srcOffset) {
77
- const rgb = new Uint8ClampedArray(3);
78
- this.getRgbItem(src, srcOffset, rgb, 0);
79
- return rgb;
80
- }
81
-
82
- /**
83
- * Converts the color value to the RGB color, similar to the getRgb method.
84
- * The result placed into the dest array starting from the destOffset.
85
- */
86
- getRgbItem(src, srcOffset, dest, destOffset) {
87
- unreachable("Should not call ColorSpace.getRgbItem");
88
- }
89
-
90
- /**
91
- * Converts the specified number of the color values to the RGB colors.
92
- * The colors are located in the src array starting from the srcOffset.
93
- * The result is placed into the dest array starting from the destOffset.
94
- * The src array items shall be in [0,2^bits) range, the dest array items
95
- * will be in [0,255] range. alpha01 indicates how many alpha components
96
- * there are in the dest array; it will be either 0 (RGB array) or 1 (RGBA
97
- * array).
98
- */
99
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
100
- unreachable("Should not call ColorSpace.getRgbBuffer");
101
- }
102
-
103
- /**
104
- * Determines the number of bytes required to store the result of the
105
- * conversion done by the getRgbBuffer method. As in getRgbBuffer,
106
- * |alpha01| is either 0 (RGB output) or 1 (RGBA output).
107
- */
108
- getOutputLength(inputLength, alpha01) {
109
- unreachable("Should not call ColorSpace.getOutputLength");
110
- }
111
-
112
- /**
113
- * Returns true if source data will be equal the result/output data.
114
- */
115
- isPassthrough(bits) {
116
- return false;
117
- }
118
-
119
- /**
120
- * Refer to the static `ColorSpace.isDefaultDecode` method below.
121
- */
122
- isDefaultDecode(decodeMap, bpc) {
123
- return ColorSpace.isDefaultDecode(decodeMap, this.numComps);
124
- }
125
-
126
- /**
127
- * Fills in the RGB colors in the destination buffer. alpha01 indicates
128
- * how many alpha components there are in the dest array; it will be either
129
- * 0 (RGB array) or 1 (RGBA array).
130
- */
131
- fillRgb(
132
- dest,
133
- originalWidth,
134
- originalHeight,
135
- width,
136
- height,
137
- actualHeight,
138
- bpc,
139
- comps,
140
- alpha01
141
- ) {
142
- if (
143
- typeof PDFJSDev === "undefined" ||
144
- PDFJSDev.test("!PRODUCTION || TESTING")
145
- ) {
146
- assert(
147
- dest instanceof Uint8ClampedArray,
148
- 'ColorSpace.fillRgb: Unsupported "dest" type.'
149
- );
150
- }
151
- const count = originalWidth * originalHeight;
152
- let rgbBuf = null;
153
- const numComponentColors = 1 << bpc;
154
- const needsResizing = originalHeight !== height || originalWidth !== width;
155
-
156
- if (this.isPassthrough(bpc)) {
157
- rgbBuf = comps;
158
- } else if (
159
- this.numComps === 1 &&
160
- count > numComponentColors &&
161
- this.name !== "DeviceGray" &&
162
- this.name !== "DeviceRGB"
163
- ) {
164
- // Optimization: create a color map when there is just one component and
165
- // we are converting more colors than the size of the color map. We
166
- // don't build the map if the colorspace is gray or rgb since those
167
- // methods are faster than building a map. This mainly offers big speed
168
- // ups for indexed and alternate colorspaces.
169
- //
170
- // TODO it may be worth while to cache the color map. While running
171
- // testing I never hit a cache so I will leave that out for now (perhaps
172
- // we are reparsing colorspaces too much?).
173
- const allColors =
174
- bpc <= 8
175
- ? new Uint8Array(numComponentColors)
176
- : new Uint16Array(numComponentColors);
177
- for (let i = 0; i < numComponentColors; i++) {
178
- allColors[i] = i;
179
- }
180
- const colorMap = new Uint8ClampedArray(numComponentColors * 3);
181
- this.getRgbBuffer(
182
- allColors,
183
- 0,
184
- numComponentColors,
185
- colorMap,
186
- 0,
187
- bpc,
188
- /* alpha01 = */ 0
189
- );
190
-
191
- if (!needsResizing) {
192
- // Fill in the RGB values directly into |dest|.
193
- let destPos = 0;
194
- for (let i = 0; i < count; ++i) {
195
- const key = comps[i] * 3;
196
- dest[destPos++] = colorMap[key];
197
- dest[destPos++] = colorMap[key + 1];
198
- dest[destPos++] = colorMap[key + 2];
199
- destPos += alpha01;
200
- }
201
- } else {
202
- rgbBuf = new Uint8Array(count * 3);
203
- let rgbPos = 0;
204
- for (let i = 0; i < count; ++i) {
205
- const key = comps[i] * 3;
206
- rgbBuf[rgbPos++] = colorMap[key];
207
- rgbBuf[rgbPos++] = colorMap[key + 1];
208
- rgbBuf[rgbPos++] = colorMap[key + 2];
209
- }
210
- }
211
- } else {
212
- if (!needsResizing) {
213
- // Fill in the RGB values directly into |dest|.
214
- this.getRgbBuffer(
215
- comps,
216
- 0,
217
- width * actualHeight,
218
- dest,
219
- 0,
220
- bpc,
221
- alpha01
222
- );
223
- } else {
224
- rgbBuf = new Uint8ClampedArray(count * 3);
225
- this.getRgbBuffer(comps, 0, count, rgbBuf, 0, bpc, /* alpha01 = */ 0);
226
- }
227
- }
228
-
229
- if (rgbBuf) {
230
- if (needsResizing) {
231
- resizeRgbImage(
232
- rgbBuf,
233
- dest,
234
- originalWidth,
235
- originalHeight,
236
- width,
237
- height,
238
- alpha01
239
- );
240
- } else {
241
- let destPos = 0,
242
- rgbPos = 0;
243
- for (let i = 0, ii = width * actualHeight; i < ii; i++) {
244
- dest[destPos++] = rgbBuf[rgbPos++];
245
- dest[destPos++] = rgbBuf[rgbPos++];
246
- dest[destPos++] = rgbBuf[rgbPos++];
247
- destPos += alpha01;
248
- }
249
- }
250
- }
251
- }
252
-
253
- /**
254
- * True if the colorspace has components in the default range of [0, 1].
255
- * This should be true for all colorspaces except for lab color spaces
256
- * which are [0,100], [-128, 127], [-128, 127].
257
- */
258
- get usesZeroToOneRange() {
259
- return shadow(this, "usesZeroToOneRange", true);
260
- }
261
-
262
- /**
263
- * @private
264
- */
265
- static _cache(cacheKey, xref, localColorSpaceCache, parsedColorSpace) {
266
- if (!localColorSpaceCache) {
267
- throw new Error(
268
- 'ColorSpace._cache - expected "localColorSpaceCache" argument.'
269
- );
270
- }
271
- if (!parsedColorSpace) {
272
- throw new Error(
273
- 'ColorSpace._cache - expected "parsedColorSpace" argument.'
274
- );
275
- }
276
- let csName, csRef;
277
- if (cacheKey instanceof Ref) {
278
- csRef = cacheKey;
279
-
280
- // If parsing succeeded, we know that this call cannot throw.
281
- cacheKey = xref.fetch(cacheKey);
282
- }
283
- if (cacheKey instanceof Name) {
284
- csName = cacheKey.name;
285
- }
286
- if (csName || csRef) {
287
- localColorSpaceCache.set(csName, csRef, parsedColorSpace);
288
- }
289
- }
290
-
291
- static getCached(cacheKey, xref, localColorSpaceCache) {
292
- if (!localColorSpaceCache) {
293
- throw new Error(
294
- 'ColorSpace.getCached - expected "localColorSpaceCache" argument.'
295
- );
296
- }
297
- if (cacheKey instanceof Ref) {
298
- const localColorSpace = localColorSpaceCache.getByRef(cacheKey);
299
- if (localColorSpace) {
300
- return localColorSpace;
301
- }
302
-
303
- try {
304
- cacheKey = xref.fetch(cacheKey);
305
- } catch (ex) {
306
- if (ex instanceof MissingDataException) {
307
- throw ex;
308
- }
309
- // Any errors should be handled during parsing, rather than here.
310
- }
311
- }
312
- if (cacheKey instanceof Name) {
313
- const localColorSpace = localColorSpaceCache.getByName(cacheKey.name);
314
- if (localColorSpace) {
315
- return localColorSpace;
316
- }
317
- }
318
- return null;
319
- }
320
-
321
- static async parseAsync({
322
- cs,
323
- xref,
324
- resources = null,
325
- pdfFunctionFactory,
326
- localColorSpaceCache,
327
- }) {
328
- if (
329
- typeof PDFJSDev === "undefined" ||
330
- PDFJSDev.test("!PRODUCTION || TESTING")
331
- ) {
332
- assert(
333
- !this.getCached(cs, xref, localColorSpaceCache),
334
- "Expected `ColorSpace.getCached` to have been manually checked " +
335
- "before calling `ColorSpace.parseAsync`."
336
- );
337
- }
338
- const parsedColorSpace = this._parse(
339
- cs,
340
- xref,
341
- resources,
342
- pdfFunctionFactory
343
- );
344
-
345
- // Attempt to cache the parsed ColorSpace, by name and/or reference.
346
- this._cache(cs, xref, localColorSpaceCache, parsedColorSpace);
347
-
348
- return parsedColorSpace;
349
- }
350
-
351
- static parse({
352
- cs,
353
- xref,
354
- resources = null,
355
- pdfFunctionFactory,
356
- localColorSpaceCache,
357
- }) {
358
- const cachedColorSpace = this.getCached(cs, xref, localColorSpaceCache);
359
- if (cachedColorSpace) {
360
- return cachedColorSpace;
361
- }
362
- const parsedColorSpace = this._parse(
363
- cs,
364
- xref,
365
- resources,
366
- pdfFunctionFactory
367
- );
368
-
369
- // Attempt to cache the parsed ColorSpace, by name and/or reference.
370
- this._cache(cs, xref, localColorSpaceCache, parsedColorSpace);
371
-
372
- return parsedColorSpace;
373
- }
374
-
375
- /**
376
- * @private
377
- */
378
- static _parse(cs, xref, resources = null, pdfFunctionFactory) {
379
- cs = xref.fetchIfRef(cs);
380
- if (isName(cs)) {
381
- switch (cs.name) {
382
- case "DeviceGray":
383
- case "G":
384
- return this.singletons.gray;
385
- case "DeviceRGB":
386
- case "RGB":
387
- return this.singletons.rgb;
388
- case "DeviceCMYK":
389
- case "CMYK":
390
- return this.singletons.cmyk;
391
- case "Pattern":
392
- return new PatternCS(/* baseCS = */ null);
393
- default:
394
- if (isDict(resources)) {
395
- const colorSpaces = resources.get("ColorSpace");
396
- if (isDict(colorSpaces)) {
397
- const resourcesCS = colorSpaces.get(cs.name);
398
- if (resourcesCS) {
399
- if (isName(resourcesCS)) {
400
- return this._parse(
401
- resourcesCS,
402
- xref,
403
- resources,
404
- pdfFunctionFactory
405
- );
406
- }
407
- cs = resourcesCS;
408
- break;
409
- }
410
- }
411
- }
412
- throw new FormatError(`Unrecognized ColorSpace: ${cs.name}`);
413
- }
414
- }
415
- if (Array.isArray(cs)) {
416
- const mode = xref.fetchIfRef(cs[0]).name;
417
- let params, numComps, baseCS, whitePoint, blackPoint, gamma;
418
-
419
- switch (mode) {
420
- case "DeviceGray":
421
- case "G":
422
- return this.singletons.gray;
423
- case "DeviceRGB":
424
- case "RGB":
425
- return this.singletons.rgb;
426
- case "DeviceCMYK":
427
- case "CMYK":
428
- return this.singletons.cmyk;
429
- case "CalGray":
430
- params = xref.fetchIfRef(cs[1]);
431
- whitePoint = params.getArray("WhitePoint");
432
- blackPoint = params.getArray("BlackPoint");
433
- gamma = params.get("Gamma");
434
- return new CalGrayCS(whitePoint, blackPoint, gamma);
435
- case "CalRGB":
436
- params = xref.fetchIfRef(cs[1]);
437
- whitePoint = params.getArray("WhitePoint");
438
- blackPoint = params.getArray("BlackPoint");
439
- gamma = params.getArray("Gamma");
440
- const matrix = params.getArray("Matrix");
441
- return new CalRGBCS(whitePoint, blackPoint, gamma, matrix);
442
- case "ICCBased":
443
- const stream = xref.fetchIfRef(cs[1]);
444
- const dict = stream.dict;
445
- numComps = dict.get("N");
446
- const alt = dict.get("Alternate");
447
- if (alt) {
448
- const altCS = this._parse(alt, xref, resources, pdfFunctionFactory);
449
- // Ensure that the number of components are correct,
450
- // and also (indirectly) that it is not a PatternCS.
451
- if (altCS.numComps === numComps) {
452
- return altCS;
453
- }
454
- warn("ICCBased color space: Ignoring incorrect /Alternate entry.");
455
- }
456
- if (numComps === 1) {
457
- return this.singletons.gray;
458
- } else if (numComps === 3) {
459
- return this.singletons.rgb;
460
- } else if (numComps === 4) {
461
- return this.singletons.cmyk;
462
- }
463
- break;
464
- case "Pattern":
465
- baseCS = cs[1] || null;
466
- if (baseCS) {
467
- baseCS = this._parse(baseCS, xref, resources, pdfFunctionFactory);
468
- }
469
- return new PatternCS(baseCS);
470
- case "Indexed":
471
- case "I":
472
- baseCS = this._parse(cs[1], xref, resources, pdfFunctionFactory);
473
- const hiVal = xref.fetchIfRef(cs[2]) + 1;
474
- const lookup = xref.fetchIfRef(cs[3]);
475
- return new IndexedCS(baseCS, hiVal, lookup);
476
- case "Separation":
477
- case "DeviceN":
478
- const name = xref.fetchIfRef(cs[1]);
479
- numComps = Array.isArray(name) ? name.length : 1;
480
- baseCS = this._parse(cs[2], xref, resources, pdfFunctionFactory);
481
- const tintFn = pdfFunctionFactory.create(cs[3]);
482
- return new AlternateCS(numComps, baseCS, tintFn);
483
- case "Lab":
484
- params = xref.fetchIfRef(cs[1]);
485
- whitePoint = params.getArray("WhitePoint");
486
- blackPoint = params.getArray("BlackPoint");
487
- const range = params.getArray("Range");
488
- return new LabCS(whitePoint, blackPoint, range);
489
- default:
490
- throw new FormatError(`Unimplemented ColorSpace object: ${mode}`);
491
- }
492
- }
493
- throw new FormatError(`Unrecognized ColorSpace object: ${cs}`);
494
- }
495
-
496
- /**
497
- * Checks if a decode map matches the default decode map for a color space.
498
- * This handles the general decode maps where there are two values per
499
- * component, e.g. [0, 1, 0, 1, 0, 1] for a RGB color.
500
- * This does not handle Lab, Indexed, or Pattern decode maps since they are
501
- * slightly different.
502
- * @param {Array} decode - Decode map (usually from an image).
503
- * @param {number} numComps - Number of components the color space has.
504
- */
505
- static isDefaultDecode(decode, numComps) {
506
- if (!Array.isArray(decode)) {
507
- return true;
508
- }
509
- if (numComps * 2 !== decode.length) {
510
- warn("The decode map is not the correct length");
511
- return true;
512
- }
513
- for (let i = 0, ii = decode.length; i < ii; i += 2) {
514
- if (decode[i] !== 0 || decode[i + 1] !== 1) {
515
- return false;
516
- }
517
- }
518
- return true;
519
- }
520
-
521
- static get singletons() {
522
- return shadow(this, "singletons", {
523
- get gray() {
524
- return shadow(this, "gray", new DeviceGrayCS());
525
- },
526
- get rgb() {
527
- return shadow(this, "rgb", new DeviceRgbCS());
528
- },
529
- get cmyk() {
530
- return shadow(this, "cmyk", new DeviceCmykCS());
531
- },
532
- });
533
- }
534
- }
535
-
536
- /**
537
- * Alternate color space handles both Separation and DeviceN color spaces. A
538
- * Separation color space is actually just a DeviceN with one color component.
539
- * Both color spaces use a tinting function to convert colors to a base color
540
- * space.
541
- *
542
- * The default color is `new Float32Array(new Array(numComps).fill(1))`.
543
- */
544
- class AlternateCS extends ColorSpace {
545
- constructor(numComps, base, tintFn) {
546
- super("Alternate", numComps);
547
- this.base = base;
548
- this.tintFn = tintFn;
549
- this.tmpBuf = new Float32Array(base.numComps);
550
- }
551
-
552
- getRgbItem(src, srcOffset, dest, destOffset) {
553
- if (
554
- typeof PDFJSDev === "undefined" ||
555
- PDFJSDev.test("!PRODUCTION || TESTING")
556
- ) {
557
- assert(
558
- dest instanceof Uint8ClampedArray,
559
- 'AlternateCS.getRgbItem: Unsupported "dest" type.'
560
- );
561
- }
562
- const tmpBuf = this.tmpBuf;
563
- this.tintFn(src, srcOffset, tmpBuf, 0);
564
- this.base.getRgbItem(tmpBuf, 0, dest, destOffset);
565
- }
566
-
567
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
568
- if (
569
- typeof PDFJSDev === "undefined" ||
570
- PDFJSDev.test("!PRODUCTION || TESTING")
571
- ) {
572
- assert(
573
- dest instanceof Uint8ClampedArray,
574
- 'AlternateCS.getRgbBuffer: Unsupported "dest" type.'
575
- );
576
- }
577
- const tintFn = this.tintFn;
578
- const base = this.base;
579
- const scale = 1 / ((1 << bits) - 1);
580
- const baseNumComps = base.numComps;
581
- const usesZeroToOneRange = base.usesZeroToOneRange;
582
- const isPassthrough =
583
- (base.isPassthrough(8) || !usesZeroToOneRange) && alpha01 === 0;
584
- let pos = isPassthrough ? destOffset : 0;
585
- const baseBuf = isPassthrough
586
- ? dest
587
- : new Uint8ClampedArray(baseNumComps * count);
588
- const numComps = this.numComps;
589
-
590
- const scaled = new Float32Array(numComps);
591
- const tinted = new Float32Array(baseNumComps);
592
- let i, j;
593
-
594
- for (i = 0; i < count; i++) {
595
- for (j = 0; j < numComps; j++) {
596
- scaled[j] = src[srcOffset++] * scale;
597
- }
598
- tintFn(scaled, 0, tinted, 0);
599
- if (usesZeroToOneRange) {
600
- for (j = 0; j < baseNumComps; j++) {
601
- baseBuf[pos++] = tinted[j] * 255;
602
- }
603
- } else {
604
- base.getRgbItem(tinted, 0, baseBuf, pos);
605
- pos += baseNumComps;
606
- }
607
- }
608
-
609
- if (!isPassthrough) {
610
- base.getRgbBuffer(baseBuf, 0, count, dest, destOffset, 8, alpha01);
611
- }
612
- }
613
-
614
- getOutputLength(inputLength, alpha01) {
615
- return this.base.getOutputLength(
616
- (inputLength * this.base.numComps) / this.numComps,
617
- alpha01
618
- );
619
- }
620
- }
621
-
622
- class PatternCS extends ColorSpace {
623
- constructor(baseCS) {
624
- super("Pattern", null);
625
- this.base = baseCS;
626
- }
627
-
628
- isDefaultDecode(decodeMap, bpc) {
629
- unreachable("Should not call PatternCS.isDefaultDecode");
630
- }
631
- }
632
-
633
- /**
634
- * The default color is `new Uint8Array([0])`.
635
- */
636
- class IndexedCS extends ColorSpace {
637
- constructor(base, highVal, lookup) {
638
- super("Indexed", 1);
639
- this.base = base;
640
- this.highVal = highVal;
641
-
642
- const length = base.numComps * highVal;
643
- this.lookup = new Uint8Array(length);
644
-
645
- if (isStream(lookup)) {
646
- const bytes = lookup.getBytes(length);
647
- this.lookup.set(bytes);
648
- } else if (typeof lookup === "string") {
649
- for (let i = 0; i < length; ++i) {
650
- this.lookup[i] = lookup.charCodeAt(i) & 0xff;
651
- }
652
- } else {
653
- throw new FormatError(`IndexedCS - unrecognized lookup table: ${lookup}`);
654
- }
655
- }
656
-
657
- getRgbItem(src, srcOffset, dest, destOffset) {
658
- if (
659
- typeof PDFJSDev === "undefined" ||
660
- PDFJSDev.test("!PRODUCTION || TESTING")
661
- ) {
662
- assert(
663
- dest instanceof Uint8ClampedArray,
664
- 'IndexedCS.getRgbItem: Unsupported "dest" type.'
665
- );
666
- }
667
- const numComps = this.base.numComps;
668
- const start = src[srcOffset] * numComps;
669
- this.base.getRgbBuffer(this.lookup, start, 1, dest, destOffset, 8, 0);
670
- }
671
-
672
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
673
- if (
674
- typeof PDFJSDev === "undefined" ||
675
- PDFJSDev.test("!PRODUCTION || TESTING")
676
- ) {
677
- assert(
678
- dest instanceof Uint8ClampedArray,
679
- 'IndexedCS.getRgbBuffer: Unsupported "dest" type.'
680
- );
681
- }
682
- const base = this.base;
683
- const numComps = base.numComps;
684
- const outputDelta = base.getOutputLength(numComps, alpha01);
685
- const lookup = this.lookup;
686
-
687
- for (let i = 0; i < count; ++i) {
688
- const lookupPos = src[srcOffset++] * numComps;
689
- base.getRgbBuffer(lookup, lookupPos, 1, dest, destOffset, 8, alpha01);
690
- destOffset += outputDelta;
691
- }
692
- }
693
-
694
- getOutputLength(inputLength, alpha01) {
695
- return this.base.getOutputLength(inputLength * this.base.numComps, alpha01);
696
- }
697
-
698
- isDefaultDecode(decodeMap, bpc) {
699
- if (!Array.isArray(decodeMap)) {
700
- return true;
701
- }
702
- if (decodeMap.length !== 2) {
703
- warn("Decode map length is not correct");
704
- return true;
705
- }
706
- if (!Number.isInteger(bpc) || bpc < 1) {
707
- warn("Bits per component is not correct");
708
- return true;
709
- }
710
- return decodeMap[0] === 0 && decodeMap[1] === (1 << bpc) - 1;
711
- }
712
- }
713
-
714
- /**
715
- * The default color is `new Float32Array([0])`.
716
- */
717
- class DeviceGrayCS extends ColorSpace {
718
- constructor() {
719
- super("DeviceGray", 1);
720
- }
721
-
722
- getRgbItem(src, srcOffset, dest, destOffset) {
723
- if (
724
- typeof PDFJSDev === "undefined" ||
725
- PDFJSDev.test("!PRODUCTION || TESTING")
726
- ) {
727
- assert(
728
- dest instanceof Uint8ClampedArray,
729
- 'DeviceGrayCS.getRgbItem: Unsupported "dest" type.'
730
- );
731
- }
732
- const c = src[srcOffset] * 255;
733
- dest[destOffset] = dest[destOffset + 1] = dest[destOffset + 2] = c;
734
- }
735
-
736
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
737
- if (
738
- typeof PDFJSDev === "undefined" ||
739
- PDFJSDev.test("!PRODUCTION || TESTING")
740
- ) {
741
- assert(
742
- dest instanceof Uint8ClampedArray,
743
- 'DeviceGrayCS.getRgbBuffer: Unsupported "dest" type.'
744
- );
745
- }
746
- const scale = 255 / ((1 << bits) - 1);
747
- let j = srcOffset,
748
- q = destOffset;
749
- for (let i = 0; i < count; ++i) {
750
- const c = scale * src[j++];
751
- dest[q++] = c;
752
- dest[q++] = c;
753
- dest[q++] = c;
754
- q += alpha01;
755
- }
756
- }
757
-
758
- getOutputLength(inputLength, alpha01) {
759
- return inputLength * (3 + alpha01);
760
- }
761
- }
762
-
763
- /**
764
- * The default color is `new Float32Array([0, 0, 0])`.
765
- */
766
- class DeviceRgbCS extends ColorSpace {
767
- constructor() {
768
- super("DeviceRGB", 3);
769
- }
770
-
771
- getRgbItem(src, srcOffset, dest, destOffset) {
772
- if (
773
- typeof PDFJSDev === "undefined" ||
774
- PDFJSDev.test("!PRODUCTION || TESTING")
775
- ) {
776
- assert(
777
- dest instanceof Uint8ClampedArray,
778
- 'DeviceRgbCS.getRgbItem: Unsupported "dest" type.'
779
- );
780
- }
781
- dest[destOffset] = src[srcOffset] * 255;
782
- dest[destOffset + 1] = src[srcOffset + 1] * 255;
783
- dest[destOffset + 2] = src[srcOffset + 2] * 255;
784
- }
785
-
786
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
787
- if (
788
- typeof PDFJSDev === "undefined" ||
789
- PDFJSDev.test("!PRODUCTION || TESTING")
790
- ) {
791
- assert(
792
- dest instanceof Uint8ClampedArray,
793
- 'DeviceRgbCS.getRgbBuffer: Unsupported "dest" type.'
794
- );
795
- }
796
- if (bits === 8 && alpha01 === 0) {
797
- dest.set(src.subarray(srcOffset, srcOffset + count * 3), destOffset);
798
- return;
799
- }
800
- const scale = 255 / ((1 << bits) - 1);
801
- let j = srcOffset,
802
- q = destOffset;
803
- for (let i = 0; i < count; ++i) {
804
- dest[q++] = scale * src[j++];
805
- dest[q++] = scale * src[j++];
806
- dest[q++] = scale * src[j++];
807
- q += alpha01;
808
- }
809
- }
810
-
811
- getOutputLength(inputLength, alpha01) {
812
- return ((inputLength * (3 + alpha01)) / 3) | 0;
813
- }
814
-
815
- isPassthrough(bits) {
816
- return bits === 8;
817
- }
818
- }
819
-
820
- /**
821
- * The default color is `new Float32Array([0, 0, 0, 1])`.
822
- */
823
- const DeviceCmykCS = (function DeviceCmykCSClosure() {
824
- // The coefficients below was found using numerical analysis: the method of
825
- // steepest descent for the sum((f_i - color_value_i)^2) for r/g/b colors,
826
- // where color_value is the tabular value from the table of sampled RGB colors
827
- // from CMYK US Web Coated (SWOP) colorspace, and f_i is the corresponding
828
- // CMYK color conversion using the estimation below:
829
- // f(A, B,.. N) = Acc+Bcm+Ccy+Dck+c+Fmm+Gmy+Hmk+Im+Jyy+Kyk+Ly+Mkk+Nk+255
830
- function convertToRgb(src, srcOffset, srcScale, dest, destOffset) {
831
- const c = src[srcOffset] * srcScale;
832
- const m = src[srcOffset + 1] * srcScale;
833
- const y = src[srcOffset + 2] * srcScale;
834
- const k = src[srcOffset + 3] * srcScale;
835
-
836
- dest[destOffset] =
837
- 255 +
838
- c *
839
- (-4.387332384609988 * c +
840
- 54.48615194189176 * m +
841
- 18.82290502165302 * y +
842
- 212.25662451639585 * k +
843
- -285.2331026137004) +
844
- m *
845
- (1.7149763477362134 * m -
846
- 5.6096736904047315 * y +
847
- -17.873870861415444 * k -
848
- 5.497006427196366) +
849
- y *
850
- (-2.5217340131683033 * y - 21.248923337353073 * k + 17.5119270841813) +
851
- k * (-21.86122147463605 * k - 189.48180835922747);
852
-
853
- dest[destOffset + 1] =
854
- 255 +
855
- c *
856
- (8.841041422036149 * c +
857
- 60.118027045597366 * m +
858
- 6.871425592049007 * y +
859
- 31.159100130055922 * k +
860
- -79.2970844816548) +
861
- m *
862
- (-15.310361306967817 * m +
863
- 17.575251261109482 * y +
864
- 131.35250912493976 * k -
865
- 190.9453302588951) +
866
- y * (4.444339102852739 * y + 9.8632861493405 * k - 24.86741582555878) +
867
- k * (-20.737325471181034 * k - 187.80453709719578);
868
-
869
- dest[destOffset + 2] =
870
- 255 +
871
- c *
872
- (0.8842522430003296 * c +
873
- 8.078677503112928 * m +
874
- 30.89978309703729 * y -
875
- 0.23883238689178934 * k +
876
- -14.183576799673286) +
877
- m *
878
- (10.49593273432072 * m +
879
- 63.02378494754052 * y +
880
- 50.606957656360734 * k -
881
- 112.23884253719248) +
882
- y *
883
- (0.03296041114873217 * y +
884
- 115.60384449646641 * k +
885
- -193.58209356861505) +
886
- k * (-22.33816807309886 * k - 180.12613974708367);
887
- }
888
-
889
- // eslint-disable-next-line no-shadow
890
- class DeviceCmykCS extends ColorSpace {
891
- constructor() {
892
- super("DeviceCMYK", 4);
893
- }
894
-
895
- getRgbItem(src, srcOffset, dest, destOffset) {
896
- if (
897
- typeof PDFJSDev === "undefined" ||
898
- PDFJSDev.test("!PRODUCTION || TESTING")
899
- ) {
900
- assert(
901
- dest instanceof Uint8ClampedArray,
902
- 'DeviceCmykCS.getRgbItem: Unsupported "dest" type.'
903
- );
904
- }
905
- convertToRgb(src, srcOffset, 1, dest, destOffset);
906
- }
907
-
908
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
909
- if (
910
- typeof PDFJSDev === "undefined" ||
911
- PDFJSDev.test("!PRODUCTION || TESTING")
912
- ) {
913
- assert(
914
- dest instanceof Uint8ClampedArray,
915
- 'DeviceCmykCS.getRgbBuffer: Unsupported "dest" type.'
916
- );
917
- }
918
- const scale = 1 / ((1 << bits) - 1);
919
- for (let i = 0; i < count; i++) {
920
- convertToRgb(src, srcOffset, scale, dest, destOffset);
921
- srcOffset += 4;
922
- destOffset += 3 + alpha01;
923
- }
924
- }
925
-
926
- getOutputLength(inputLength, alpha01) {
927
- return ((inputLength / 4) * (3 + alpha01)) | 0;
928
- }
929
- }
930
- return DeviceCmykCS;
931
- })();
932
-
933
- /**
934
- * CalGrayCS: Based on "PDF Reference, Sixth Ed", p.245
935
- *
936
- * The default color is `new Float32Array([0])`.
937
- */
938
- const CalGrayCS = (function CalGrayCSClosure() {
939
- function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
940
- // A represents a gray component of a calibrated gray space.
941
- // A <---> AG in the spec
942
- const A = src[srcOffset] * scale;
943
- const AG = A ** cs.G;
944
-
945
- // Computes L as per spec. ( = cs.YW * AG )
946
- // Except if other than default BlackPoint values are used.
947
- const L = cs.YW * AG;
948
- // http://www.poynton.com/notes/colour_and_gamma/ColorFAQ.html, Ch 4.
949
- // Convert values to rgb range [0, 255].
950
- const val = Math.max(295.8 * L ** 0.333333333333333333 - 40.8, 0);
951
- dest[destOffset] = val;
952
- dest[destOffset + 1] = val;
953
- dest[destOffset + 2] = val;
954
- }
955
-
956
- // eslint-disable-next-line no-shadow
957
- class CalGrayCS extends ColorSpace {
958
- constructor(whitePoint, blackPoint, gamma) {
959
- super("CalGray", 1);
960
-
961
- if (!whitePoint) {
962
- throw new FormatError(
963
- "WhitePoint missing - required for color space CalGray"
964
- );
965
- }
966
- blackPoint = blackPoint || [0, 0, 0];
967
- gamma = gamma || 1;
968
-
969
- // Translate arguments to spec variables.
970
- this.XW = whitePoint[0];
971
- this.YW = whitePoint[1];
972
- this.ZW = whitePoint[2];
973
-
974
- this.XB = blackPoint[0];
975
- this.YB = blackPoint[1];
976
- this.ZB = blackPoint[2];
977
-
978
- this.G = gamma;
979
-
980
- // Validate variables as per spec.
981
- if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
982
- throw new FormatError(
983
- `Invalid WhitePoint components for ${this.name}` +
984
- ", no fallback available"
985
- );
986
- }
987
-
988
- if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
989
- info(`Invalid BlackPoint for ${this.name}, falling back to default.`);
990
- this.XB = this.YB = this.ZB = 0;
991
- }
992
-
993
- if (this.XB !== 0 || this.YB !== 0 || this.ZB !== 0) {
994
- warn(
995
- `${this.name}, BlackPoint: XB: ${this.XB}, YB: ${this.YB}, ` +
996
- `ZB: ${this.ZB}, only default values are supported.`
997
- );
998
- }
999
-
1000
- if (this.G < 1) {
1001
- info(
1002
- `Invalid Gamma: ${this.G} for ${this.name}, ` +
1003
- "falling back to default."
1004
- );
1005
- this.G = 1;
1006
- }
1007
- }
1008
-
1009
- getRgbItem(src, srcOffset, dest, destOffset) {
1010
- if (
1011
- typeof PDFJSDev === "undefined" ||
1012
- PDFJSDev.test("!PRODUCTION || TESTING")
1013
- ) {
1014
- assert(
1015
- dest instanceof Uint8ClampedArray,
1016
- 'CalGrayCS.getRgbItem: Unsupported "dest" type.'
1017
- );
1018
- }
1019
- convertToRgb(this, src, srcOffset, dest, destOffset, 1);
1020
- }
1021
-
1022
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
1023
- if (
1024
- typeof PDFJSDev === "undefined" ||
1025
- PDFJSDev.test("!PRODUCTION || TESTING")
1026
- ) {
1027
- assert(
1028
- dest instanceof Uint8ClampedArray,
1029
- 'CalGrayCS.getRgbBuffer: Unsupported "dest" type.'
1030
- );
1031
- }
1032
- const scale = 1 / ((1 << bits) - 1);
1033
-
1034
- for (let i = 0; i < count; ++i) {
1035
- convertToRgb(this, src, srcOffset, dest, destOffset, scale);
1036
- srcOffset += 1;
1037
- destOffset += 3 + alpha01;
1038
- }
1039
- }
1040
-
1041
- getOutputLength(inputLength, alpha01) {
1042
- return inputLength * (3 + alpha01);
1043
- }
1044
- }
1045
- return CalGrayCS;
1046
- })();
1047
-
1048
- /**
1049
- * CalRGBCS: Based on "PDF Reference, Sixth Ed", p.247
1050
- *
1051
- * The default color is `new Float32Array([0, 0, 0])`.
1052
- */
1053
- const CalRGBCS = (function CalRGBCSClosure() {
1054
- // See http://www.brucelindbloom.com/index.html?Eqn_ChromAdapt.html for these
1055
- // matrices.
1056
- // prettier-ignore
1057
- const BRADFORD_SCALE_MATRIX = new Float32Array([
1058
- 0.8951, 0.2664, -0.1614,
1059
- -0.7502, 1.7135, 0.0367,
1060
- 0.0389, -0.0685, 1.0296]);
1061
-
1062
- // prettier-ignore
1063
- const BRADFORD_SCALE_INVERSE_MATRIX = new Float32Array([
1064
- 0.9869929, -0.1470543, 0.1599627,
1065
- 0.4323053, 0.5183603, 0.0492912,
1066
- -0.0085287, 0.0400428, 0.9684867]);
1067
-
1068
- // See http://www.brucelindbloom.com/index.html?Eqn_RGB_XYZ_Matrix.html.
1069
- // prettier-ignore
1070
- const SRGB_D65_XYZ_TO_RGB_MATRIX = new Float32Array([
1071
- 3.2404542, -1.5371385, -0.4985314,
1072
- -0.9692660, 1.8760108, 0.0415560,
1073
- 0.0556434, -0.2040259, 1.0572252]);
1074
-
1075
- const FLAT_WHITEPOINT_MATRIX = new Float32Array([1, 1, 1]);
1076
-
1077
- const tempNormalizeMatrix = new Float32Array(3);
1078
- const tempConvertMatrix1 = new Float32Array(3);
1079
- const tempConvertMatrix2 = new Float32Array(3);
1080
-
1081
- const DECODE_L_CONSTANT = ((8 + 16) / 116) ** 3 / 8.0;
1082
-
1083
- function matrixProduct(a, b, result) {
1084
- result[0] = a[0] * b[0] + a[1] * b[1] + a[2] * b[2];
1085
- result[1] = a[3] * b[0] + a[4] * b[1] + a[5] * b[2];
1086
- result[2] = a[6] * b[0] + a[7] * b[1] + a[8] * b[2];
1087
- }
1088
-
1089
- function convertToFlat(sourceWhitePoint, LMS, result) {
1090
- result[0] = (LMS[0] * 1) / sourceWhitePoint[0];
1091
- result[1] = (LMS[1] * 1) / sourceWhitePoint[1];
1092
- result[2] = (LMS[2] * 1) / sourceWhitePoint[2];
1093
- }
1094
-
1095
- function convertToD65(sourceWhitePoint, LMS, result) {
1096
- const D65X = 0.95047;
1097
- const D65Y = 1;
1098
- const D65Z = 1.08883;
1099
-
1100
- result[0] = (LMS[0] * D65X) / sourceWhitePoint[0];
1101
- result[1] = (LMS[1] * D65Y) / sourceWhitePoint[1];
1102
- result[2] = (LMS[2] * D65Z) / sourceWhitePoint[2];
1103
- }
1104
-
1105
- function sRGBTransferFunction(color) {
1106
- // See http://en.wikipedia.org/wiki/SRGB.
1107
- if (color <= 0.0031308) {
1108
- return adjustToRange(0, 1, 12.92 * color);
1109
- }
1110
- // Optimization:
1111
- // If color is close enough to 1, skip calling the following transform
1112
- // since calling Math.pow is expensive. If color is larger than
1113
- // the threshold, the final result is larger than 254.5 since
1114
- // ((1 + 0.055) * 0.99554525 ** (1 / 2.4) - 0.055) * 255 ===
1115
- // 254.50000003134699
1116
- if (color >= 0.99554525) {
1117
- return 1;
1118
- }
1119
- return adjustToRange(0, 1, (1 + 0.055) * color ** (1 / 2.4) - 0.055);
1120
- }
1121
-
1122
- function adjustToRange(min, max, value) {
1123
- return Math.max(min, Math.min(max, value));
1124
- }
1125
-
1126
- function decodeL(L) {
1127
- if (L < 0) {
1128
- return -decodeL(-L);
1129
- }
1130
- if (L > 8.0) {
1131
- return ((L + 16) / 116) ** 3;
1132
- }
1133
- return L * DECODE_L_CONSTANT;
1134
- }
1135
-
1136
- function compensateBlackPoint(sourceBlackPoint, XYZ_Flat, result) {
1137
- // In case the blackPoint is already the default blackPoint then there is
1138
- // no need to do compensation.
1139
- if (
1140
- sourceBlackPoint[0] === 0 &&
1141
- sourceBlackPoint[1] === 0 &&
1142
- sourceBlackPoint[2] === 0
1143
- ) {
1144
- result[0] = XYZ_Flat[0];
1145
- result[1] = XYZ_Flat[1];
1146
- result[2] = XYZ_Flat[2];
1147
- return;
1148
- }
1149
-
1150
- // For the blackPoint calculation details, please see
1151
- // http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/
1152
- // AdobeBPC.pdf.
1153
- // The destination blackPoint is the default blackPoint [0, 0, 0].
1154
- const zeroDecodeL = decodeL(0);
1155
-
1156
- const X_DST = zeroDecodeL;
1157
- const X_SRC = decodeL(sourceBlackPoint[0]);
1158
-
1159
- const Y_DST = zeroDecodeL;
1160
- const Y_SRC = decodeL(sourceBlackPoint[1]);
1161
-
1162
- const Z_DST = zeroDecodeL;
1163
- const Z_SRC = decodeL(sourceBlackPoint[2]);
1164
-
1165
- const X_Scale = (1 - X_DST) / (1 - X_SRC);
1166
- const X_Offset = 1 - X_Scale;
1167
-
1168
- const Y_Scale = (1 - Y_DST) / (1 - Y_SRC);
1169
- const Y_Offset = 1 - Y_Scale;
1170
-
1171
- const Z_Scale = (1 - Z_DST) / (1 - Z_SRC);
1172
- const Z_Offset = 1 - Z_Scale;
1173
-
1174
- result[0] = XYZ_Flat[0] * X_Scale + X_Offset;
1175
- result[1] = XYZ_Flat[1] * Y_Scale + Y_Offset;
1176
- result[2] = XYZ_Flat[2] * Z_Scale + Z_Offset;
1177
- }
1178
-
1179
- function normalizeWhitePointToFlat(sourceWhitePoint, XYZ_In, result) {
1180
- // In case the whitePoint is already flat then there is no need to do
1181
- // normalization.
1182
- if (sourceWhitePoint[0] === 1 && sourceWhitePoint[2] === 1) {
1183
- result[0] = XYZ_In[0];
1184
- result[1] = XYZ_In[1];
1185
- result[2] = XYZ_In[2];
1186
- return;
1187
- }
1188
-
1189
- const LMS = result;
1190
- matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
1191
-
1192
- const LMS_Flat = tempNormalizeMatrix;
1193
- convertToFlat(sourceWhitePoint, LMS, LMS_Flat);
1194
-
1195
- matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_Flat, result);
1196
- }
1197
-
1198
- function normalizeWhitePointToD65(sourceWhitePoint, XYZ_In, result) {
1199
- const LMS = result;
1200
- matrixProduct(BRADFORD_SCALE_MATRIX, XYZ_In, LMS);
1201
-
1202
- const LMS_D65 = tempNormalizeMatrix;
1203
- convertToD65(sourceWhitePoint, LMS, LMS_D65);
1204
-
1205
- matrixProduct(BRADFORD_SCALE_INVERSE_MATRIX, LMS_D65, result);
1206
- }
1207
-
1208
- function convertToRgb(cs, src, srcOffset, dest, destOffset, scale) {
1209
- // A, B and C represent a red, green and blue components of a calibrated
1210
- // rgb space.
1211
- const A = adjustToRange(0, 1, src[srcOffset] * scale);
1212
- const B = adjustToRange(0, 1, src[srcOffset + 1] * scale);
1213
- const C = adjustToRange(0, 1, src[srcOffset + 2] * scale);
1214
-
1215
- // A <---> AGR in the spec
1216
- // B <---> BGG in the spec
1217
- // C <---> CGB in the spec
1218
- const AGR = A === 1 ? 1 : A ** cs.GR;
1219
- const BGG = B === 1 ? 1 : B ** cs.GG;
1220
- const CGB = C === 1 ? 1 : C ** cs.GB;
1221
-
1222
- // Computes intermediate variables L, M, N as per spec.
1223
- // To decode X, Y, Z values map L, M, N directly to them.
1224
- const X = cs.MXA * AGR + cs.MXB * BGG + cs.MXC * CGB;
1225
- const Y = cs.MYA * AGR + cs.MYB * BGG + cs.MYC * CGB;
1226
- const Z = cs.MZA * AGR + cs.MZB * BGG + cs.MZC * CGB;
1227
-
1228
- // The following calculations are based on this document:
1229
- // http://www.adobe.com/content/dam/Adobe/en/devnet/photoshop/sdk/
1230
- // AdobeBPC.pdf.
1231
- const XYZ = tempConvertMatrix1;
1232
- XYZ[0] = X;
1233
- XYZ[1] = Y;
1234
- XYZ[2] = Z;
1235
- const XYZ_Flat = tempConvertMatrix2;
1236
-
1237
- normalizeWhitePointToFlat(cs.whitePoint, XYZ, XYZ_Flat);
1238
-
1239
- const XYZ_Black = tempConvertMatrix1;
1240
- compensateBlackPoint(cs.blackPoint, XYZ_Flat, XYZ_Black);
1241
-
1242
- const XYZ_D65 = tempConvertMatrix2;
1243
- normalizeWhitePointToD65(FLAT_WHITEPOINT_MATRIX, XYZ_Black, XYZ_D65);
1244
-
1245
- const SRGB = tempConvertMatrix1;
1246
- matrixProduct(SRGB_D65_XYZ_TO_RGB_MATRIX, XYZ_D65, SRGB);
1247
-
1248
- // Convert the values to rgb range [0, 255].
1249
- dest[destOffset] = sRGBTransferFunction(SRGB[0]) * 255;
1250
- dest[destOffset + 1] = sRGBTransferFunction(SRGB[1]) * 255;
1251
- dest[destOffset + 2] = sRGBTransferFunction(SRGB[2]) * 255;
1252
- }
1253
-
1254
- // eslint-disable-next-line no-shadow
1255
- class CalRGBCS extends ColorSpace {
1256
- constructor(whitePoint, blackPoint, gamma, matrix) {
1257
- super("CalRGB", 3);
1258
-
1259
- if (!whitePoint) {
1260
- throw new FormatError(
1261
- "WhitePoint missing - required for color space CalRGB"
1262
- );
1263
- }
1264
- blackPoint = blackPoint || new Float32Array(3);
1265
- gamma = gamma || new Float32Array([1, 1, 1]);
1266
- matrix = matrix || new Float32Array([1, 0, 0, 0, 1, 0, 0, 0, 1]);
1267
-
1268
- // Translate arguments to spec variables.
1269
- const XW = whitePoint[0];
1270
- const YW = whitePoint[1];
1271
- const ZW = whitePoint[2];
1272
- this.whitePoint = whitePoint;
1273
-
1274
- const XB = blackPoint[0];
1275
- const YB = blackPoint[1];
1276
- const ZB = blackPoint[2];
1277
- this.blackPoint = blackPoint;
1278
-
1279
- this.GR = gamma[0];
1280
- this.GG = gamma[1];
1281
- this.GB = gamma[2];
1282
-
1283
- this.MXA = matrix[0];
1284
- this.MYA = matrix[1];
1285
- this.MZA = matrix[2];
1286
- this.MXB = matrix[3];
1287
- this.MYB = matrix[4];
1288
- this.MZB = matrix[5];
1289
- this.MXC = matrix[6];
1290
- this.MYC = matrix[7];
1291
- this.MZC = matrix[8];
1292
-
1293
- // Validate variables as per spec.
1294
- if (XW < 0 || ZW < 0 || YW !== 1) {
1295
- throw new FormatError(
1296
- `Invalid WhitePoint components for ${this.name}` +
1297
- ", no fallback available"
1298
- );
1299
- }
1300
-
1301
- if (XB < 0 || YB < 0 || ZB < 0) {
1302
- info(
1303
- `Invalid BlackPoint for ${this.name} [${XB}, ${YB}, ${ZB}], ` +
1304
- "falling back to default."
1305
- );
1306
- this.blackPoint = new Float32Array(3);
1307
- }
1308
-
1309
- if (this.GR < 0 || this.GG < 0 || this.GB < 0) {
1310
- info(
1311
- `Invalid Gamma [${this.GR}, ${this.GG}, ${this.GB}] for ` +
1312
- `${this.name}, falling back to default.`
1313
- );
1314
- this.GR = this.GG = this.GB = 1;
1315
- }
1316
- }
1317
-
1318
- getRgbItem(src, srcOffset, dest, destOffset) {
1319
- if (
1320
- typeof PDFJSDev === "undefined" ||
1321
- PDFJSDev.test("!PRODUCTION || TESTING")
1322
- ) {
1323
- assert(
1324
- dest instanceof Uint8ClampedArray,
1325
- 'CalRGBCS.getRgbItem: Unsupported "dest" type.'
1326
- );
1327
- }
1328
- convertToRgb(this, src, srcOffset, dest, destOffset, 1);
1329
- }
1330
-
1331
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
1332
- if (
1333
- typeof PDFJSDev === "undefined" ||
1334
- PDFJSDev.test("!PRODUCTION || TESTING")
1335
- ) {
1336
- assert(
1337
- dest instanceof Uint8ClampedArray,
1338
- 'CalRGBCS.getRgbBuffer: Unsupported "dest" type.'
1339
- );
1340
- }
1341
- const scale = 1 / ((1 << bits) - 1);
1342
-
1343
- for (let i = 0; i < count; ++i) {
1344
- convertToRgb(this, src, srcOffset, dest, destOffset, scale);
1345
- srcOffset += 3;
1346
- destOffset += 3 + alpha01;
1347
- }
1348
- }
1349
-
1350
- getOutputLength(inputLength, alpha01) {
1351
- return ((inputLength * (3 + alpha01)) / 3) | 0;
1352
- }
1353
- }
1354
- return CalRGBCS;
1355
- })();
1356
-
1357
- /**
1358
- * LabCS: Based on "PDF Reference, Sixth Ed", p.250
1359
- *
1360
- * The default color is `new Float32Array([0, 0, 0])`.
1361
- */
1362
- const LabCS = (function LabCSClosure() {
1363
- // Function g(x) from spec
1364
- function fn_g(x) {
1365
- let result;
1366
- if (x >= 6 / 29) {
1367
- result = x ** 3;
1368
- } else {
1369
- result = (108 / 841) * (x - 4 / 29);
1370
- }
1371
- return result;
1372
- }
1373
-
1374
- function decode(value, high1, low2, high2) {
1375
- return low2 + (value * (high2 - low2)) / high1;
1376
- }
1377
-
1378
- // If decoding is needed maxVal should be 2^bits per component - 1.
1379
- function convertToRgb(cs, src, srcOffset, maxVal, dest, destOffset) {
1380
- // XXX: Lab input is in the range of [0, 100], [amin, amax], [bmin, bmax]
1381
- // not the usual [0, 1]. If a command like setFillColor is used the src
1382
- // values will already be within the correct range. However, if we are
1383
- // converting an image we have to map the values to the correct range given
1384
- // above.
1385
- // Ls,as,bs <---> L*,a*,b* in the spec
1386
- let Ls = src[srcOffset];
1387
- let as = src[srcOffset + 1];
1388
- let bs = src[srcOffset + 2];
1389
- if (maxVal !== false) {
1390
- Ls = decode(Ls, maxVal, 0, 100);
1391
- as = decode(as, maxVal, cs.amin, cs.amax);
1392
- bs = decode(bs, maxVal, cs.bmin, cs.bmax);
1393
- }
1394
-
1395
- // Adjust limits of 'as' and 'bs'
1396
- if (as > cs.amax) {
1397
- as = cs.amax;
1398
- } else if (as < cs.amin) {
1399
- as = cs.amin;
1400
- }
1401
- if (bs > cs.bmax) {
1402
- bs = cs.bmax;
1403
- } else if (bs < cs.bmin) {
1404
- bs = cs.bmin;
1405
- }
1406
-
1407
- // Computes intermediate variables X,Y,Z as per spec
1408
- const M = (Ls + 16) / 116;
1409
- const L = M + as / 500;
1410
- const N = M - bs / 200;
1411
-
1412
- const X = cs.XW * fn_g(L);
1413
- const Y = cs.YW * fn_g(M);
1414
- const Z = cs.ZW * fn_g(N);
1415
-
1416
- let r, g, b;
1417
- // Using different conversions for D50 and D65 white points,
1418
- // per http://www.color.org/srgb.pdf
1419
- if (cs.ZW < 1) {
1420
- // Assuming D50 (X=0.9642, Y=1.00, Z=0.8249)
1421
- r = X * 3.1339 + Y * -1.617 + Z * -0.4906;
1422
- g = X * -0.9785 + Y * 1.916 + Z * 0.0333;
1423
- b = X * 0.072 + Y * -0.229 + Z * 1.4057;
1424
- } else {
1425
- // Assuming D65 (X=0.9505, Y=1.00, Z=1.0888)
1426
- r = X * 3.2406 + Y * -1.5372 + Z * -0.4986;
1427
- g = X * -0.9689 + Y * 1.8758 + Z * 0.0415;
1428
- b = X * 0.0557 + Y * -0.204 + Z * 1.057;
1429
- }
1430
- // Convert the color values to the [0,255] range (clamping is automatic).
1431
- dest[destOffset] = Math.sqrt(r) * 255;
1432
- dest[destOffset + 1] = Math.sqrt(g) * 255;
1433
- dest[destOffset + 2] = Math.sqrt(b) * 255;
1434
- }
1435
-
1436
- // eslint-disable-next-line no-shadow
1437
- class LabCS extends ColorSpace {
1438
- constructor(whitePoint, blackPoint, range) {
1439
- super("Lab", 3);
1440
-
1441
- if (!whitePoint) {
1442
- throw new FormatError(
1443
- "WhitePoint missing - required for color space Lab"
1444
- );
1445
- }
1446
- blackPoint = blackPoint || [0, 0, 0];
1447
- range = range || [-100, 100, -100, 100];
1448
-
1449
- // Translate args to spec variables
1450
- this.XW = whitePoint[0];
1451
- this.YW = whitePoint[1];
1452
- this.ZW = whitePoint[2];
1453
- this.amin = range[0];
1454
- this.amax = range[1];
1455
- this.bmin = range[2];
1456
- this.bmax = range[3];
1457
-
1458
- // These are here just for completeness - the spec doesn't offer any
1459
- // formulas that use BlackPoint in Lab
1460
- this.XB = blackPoint[0];
1461
- this.YB = blackPoint[1];
1462
- this.ZB = blackPoint[2];
1463
-
1464
- // Validate vars as per spec
1465
- if (this.XW < 0 || this.ZW < 0 || this.YW !== 1) {
1466
- throw new FormatError(
1467
- "Invalid WhitePoint components, no fallback available"
1468
- );
1469
- }
1470
-
1471
- if (this.XB < 0 || this.YB < 0 || this.ZB < 0) {
1472
- info("Invalid BlackPoint, falling back to default");
1473
- this.XB = this.YB = this.ZB = 0;
1474
- }
1475
-
1476
- if (this.amin > this.amax || this.bmin > this.bmax) {
1477
- info("Invalid Range, falling back to defaults");
1478
- this.amin = -100;
1479
- this.amax = 100;
1480
- this.bmin = -100;
1481
- this.bmax = 100;
1482
- }
1483
- }
1484
-
1485
- getRgbItem(src, srcOffset, dest, destOffset) {
1486
- if (
1487
- typeof PDFJSDev === "undefined" ||
1488
- PDFJSDev.test("!PRODUCTION || TESTING")
1489
- ) {
1490
- assert(
1491
- dest instanceof Uint8ClampedArray,
1492
- 'LabCS.getRgbItem: Unsupported "dest" type.'
1493
- );
1494
- }
1495
- convertToRgb(this, src, srcOffset, false, dest, destOffset);
1496
- }
1497
-
1498
- getRgbBuffer(src, srcOffset, count, dest, destOffset, bits, alpha01) {
1499
- if (
1500
- typeof PDFJSDev === "undefined" ||
1501
- PDFJSDev.test("!PRODUCTION || TESTING")
1502
- ) {
1503
- assert(
1504
- dest instanceof Uint8ClampedArray,
1505
- 'LabCS.getRgbBuffer: Unsupported "dest" type.'
1506
- );
1507
- }
1508
- const maxVal = (1 << bits) - 1;
1509
- for (let i = 0; i < count; i++) {
1510
- convertToRgb(this, src, srcOffset, maxVal, dest, destOffset);
1511
- srcOffset += 3;
1512
- destOffset += 3 + alpha01;
1513
- }
1514
- }
1515
-
1516
- getOutputLength(inputLength, alpha01) {
1517
- return ((inputLength * (3 + alpha01)) / 3) | 0;
1518
- }
1519
-
1520
- isDefaultDecode(decodeMap, bpc) {
1521
- // XXX: Decoding is handled with the lab conversion because of the strange
1522
- // ranges that are used.
1523
- return true;
1524
- }
1525
-
1526
- get usesZeroToOneRange() {
1527
- return shadow(this, "usesZeroToOneRange", false);
1528
- }
1529
- }
1530
- return LabCS;
1531
- })();
1532
-
1533
- export { ColorSpace };