@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
@@ -1,847 +0,0 @@
1
- /* Copyright 2015 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
- AbortException,
18
- createPromiseCapability,
19
- Util,
20
- } from "../shared/util.js";
21
-
22
- /**
23
- * Text layer render parameters.
24
- *
25
- * @typedef {Object} TextLayerRenderParameters
26
- * @property {import("./api").TextContent} [textContent] - Text content to
27
- * render (the object is returned by the page's `getTextContent` method).
28
- * @property {ReadableStream} [textContentStream] - Text content stream to
29
- * render (the stream is returned by the page's `streamTextContent` method).
30
- * @property {HTMLElement} container - HTML element that will contain text runs.
31
- * @property {import("./display_utils").PageViewport} viewport - The target
32
- * viewport to properly layout the text runs.
33
- * @property {Array<HTMLElement>} [textDivs] - HTML elements that are correspond
34
- * to the text items of the textContent input. This is output and shall be
35
- * initially be set to empty array.
36
- * @property {Array<string>} [textContentItemsStr] - Strings that correspond to
37
- * the `str` property of the text items of textContent input. This is output
38
- * and shall be initially be set to empty array.
39
- * @property {number} [timeout] - Delay in milliseconds before rendering of the
40
- * text runs occurs.
41
- * @property {boolean} [enhanceTextSelection] - Whether to turn on the text
42
- * selection enhancement.
43
- */
44
-
45
- const MAX_TEXT_DIVS_TO_RENDER = 100000;
46
- const DEFAULT_FONT_SIZE = 30;
47
- const DEFAULT_FONT_ASCENT = 0.8;
48
- const ascentCache = new Map();
49
- const AllWhitespaceRegexp = /^\s+$/g;
50
-
51
- function getAscent(fontFamily, ctx) {
52
- const cachedAscent = ascentCache.get(fontFamily);
53
- if (cachedAscent) {
54
- return cachedAscent;
55
- }
56
-
57
- ctx.save();
58
- ctx.font = `${DEFAULT_FONT_SIZE}px ${fontFamily}`;
59
- const metrics = ctx.measureText("");
60
-
61
- // Both properties aren't available by default in Firefox.
62
- let ascent = metrics.fontBoundingBoxAscent;
63
- let descent = Math.abs(metrics.fontBoundingBoxDescent);
64
- if (ascent) {
65
- ctx.restore();
66
- const ratio = ascent / (ascent + descent);
67
- ascentCache.set(fontFamily, ratio);
68
- return ratio;
69
- }
70
-
71
- // Try basic heuristic to guess ascent/descent.
72
- // Draw a g with baseline at 0,0 and then get the line
73
- // number where a pixel has non-null red component (starting
74
- // from bottom).
75
- ctx.strokeStyle = "red";
76
- ctx.clearRect(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE);
77
- ctx.strokeText("g", 0, 0);
78
- let pixels = ctx.getImageData(
79
- 0,
80
- 0,
81
- DEFAULT_FONT_SIZE,
82
- DEFAULT_FONT_SIZE
83
- ).data;
84
- descent = 0;
85
- for (let i = pixels.length - 1 - 3; i >= 0; i -= 4) {
86
- if (pixels[i] > 0) {
87
- descent = Math.ceil(i / 4 / DEFAULT_FONT_SIZE);
88
- break;
89
- }
90
- }
91
-
92
- // Draw an A with baseline at 0,DEFAULT_FONT_SIZE and then get the line
93
- // number where a pixel has non-null red component (starting
94
- // from top).
95
- ctx.clearRect(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE);
96
- ctx.strokeText("A", 0, DEFAULT_FONT_SIZE);
97
- pixels = ctx.getImageData(0, 0, DEFAULT_FONT_SIZE, DEFAULT_FONT_SIZE).data;
98
- ascent = 0;
99
- for (let i = 0, ii = pixels.length; i < ii; i += 4) {
100
- if (pixels[i] > 0) {
101
- ascent = DEFAULT_FONT_SIZE - Math.floor(i / 4 / DEFAULT_FONT_SIZE);
102
- break;
103
- }
104
- }
105
-
106
- ctx.restore();
107
-
108
- if (ascent) {
109
- const ratio = ascent / (ascent + descent);
110
- ascentCache.set(fontFamily, ratio);
111
- return ratio;
112
- }
113
-
114
- ascentCache.set(fontFamily, DEFAULT_FONT_ASCENT);
115
- return DEFAULT_FONT_ASCENT;
116
- }
117
-
118
- function appendText(task, geom, styles, ctx) {
119
- // Initialize all used properties to keep the caches monomorphic.
120
- const textDiv = document.createElement("span");
121
- const textDivProperties = {
122
- angle: 0,
123
- canvasWidth: 0,
124
- hasText: geom.str !== "",
125
- hasEOL: geom.hasEOL,
126
- originalTransform: null,
127
- paddingBottom: 0,
128
- paddingLeft: 0,
129
- paddingRight: 0,
130
- paddingTop: 0,
131
- scale: 1,
132
- };
133
-
134
- task._textDivs.push(textDiv);
135
-
136
- const tx = Util.transform(task._viewport.transform, geom.transform);
137
- let angle = Math.atan2(tx[1], tx[0]);
138
- const style = styles[geom.fontName];
139
- if (style.vertical) {
140
- angle += Math.PI / 2;
141
- }
142
- const fontHeight = Math.hypot(tx[2], tx[3]);
143
- const fontAscent = fontHeight * getAscent(style.fontFamily, ctx);
144
-
145
- let left, top;
146
- if (angle === 0) {
147
- left = tx[4];
148
- top = tx[5] - fontAscent;
149
- } else {
150
- left = tx[4] + fontAscent * Math.sin(angle);
151
- top = tx[5] - fontAscent * Math.cos(angle);
152
- }
153
- // Setting the style properties individually, rather than all at once,
154
- // should be OK since the `textDiv` isn't appended to the document yet.
155
- textDiv.style.left = `${left}px`;
156
- textDiv.style.top = `${top}px`;
157
- textDiv.style.fontSize = `${fontHeight}px`;
158
- textDiv.style.fontFamily = style.fontFamily;
159
-
160
- // Keeps screen readers from pausing on every new text span.
161
- textDiv.setAttribute("role", "presentation");
162
-
163
- textDiv.textContent = geom.str;
164
- // geom.dir may be 'ttb' for vertical texts.
165
- textDiv.dir = geom.dir;
166
-
167
- // `fontName` is only used by the FontInspector, and we only use `dataset`
168
- // here to make the font name available in the debugger.
169
- if (task._fontInspectorEnabled) {
170
- textDiv.dataset.fontName = geom.fontName;
171
- }
172
- if (angle !== 0) {
173
- textDivProperties.angle = angle * (180 / Math.PI);
174
- }
175
- // We don't bother scaling single-char text divs, because it has very
176
- // little effect on text highlighting. This makes scrolling on docs with
177
- // lots of such divs a lot faster.
178
- let shouldScaleText = false;
179
- if (
180
- geom.str.length > 1 ||
181
- (task._enhanceTextSelection && AllWhitespaceRegexp.test(geom.str))
182
- ) {
183
- shouldScaleText = true;
184
- } else if (geom.transform[0] !== geom.transform[3]) {
185
- const absScaleX = Math.abs(geom.transform[0]),
186
- absScaleY = Math.abs(geom.transform[3]);
187
- // When the horizontal/vertical scaling differs significantly, also scale
188
- // even single-char text to improve highlighting (fixes issue11713.pdf).
189
- if (
190
- absScaleX !== absScaleY &&
191
- Math.max(absScaleX, absScaleY) / Math.min(absScaleX, absScaleY) > 1.5
192
- ) {
193
- shouldScaleText = true;
194
- }
195
- }
196
- if (shouldScaleText) {
197
- if (style.vertical) {
198
- textDivProperties.canvasWidth = geom.height * task._viewport.scale;
199
- } else {
200
- textDivProperties.canvasWidth = geom.width * task._viewport.scale;
201
- }
202
- }
203
- task._textDivProperties.set(textDiv, textDivProperties);
204
- if (task._textContentStream) {
205
- task._layoutText(textDiv);
206
- }
207
-
208
- if (task._enhanceTextSelection && textDivProperties.hasText) {
209
- let angleCos = 1,
210
- angleSin = 0;
211
- if (angle !== 0) {
212
- angleCos = Math.cos(angle);
213
- angleSin = Math.sin(angle);
214
- }
215
- const divWidth =
216
- (style.vertical ? geom.height : geom.width) * task._viewport.scale;
217
- const divHeight = fontHeight;
218
-
219
- let m, b;
220
- if (angle !== 0) {
221
- m = [angleCos, angleSin, -angleSin, angleCos, left, top];
222
- b = Util.getAxialAlignedBoundingBox([0, 0, divWidth, divHeight], m);
223
- } else {
224
- b = [left, top, left + divWidth, top + divHeight];
225
- }
226
-
227
- task._bounds.push({
228
- left: b[0],
229
- top: b[1],
230
- right: b[2],
231
- bottom: b[3],
232
- div: textDiv,
233
- size: [divWidth, divHeight],
234
- m,
235
- });
236
- }
237
- }
238
-
239
- function render(task) {
240
- if (task._canceled) {
241
- return;
242
- }
243
- const textDivs = task._textDivs;
244
- const capability = task._capability;
245
- const textDivsLength = textDivs.length;
246
-
247
- // No point in rendering many divs as it would make the browser
248
- // unusable even after the divs are rendered.
249
- if (textDivsLength > MAX_TEXT_DIVS_TO_RENDER) {
250
- task._renderingDone = true;
251
- capability.resolve();
252
- return;
253
- }
254
-
255
- if (!task._textContentStream) {
256
- for (let i = 0; i < textDivsLength; i++) {
257
- task._layoutText(textDivs[i]);
258
- }
259
- }
260
-
261
- task._renderingDone = true;
262
- capability.resolve();
263
- }
264
-
265
- function findPositiveMin(ts, offset, count) {
266
- let result = 0;
267
- for (let i = 0; i < count; i++) {
268
- const t = ts[offset++];
269
- if (t > 0) {
270
- result = result ? Math.min(t, result) : t;
271
- }
272
- }
273
- return result;
274
- }
275
-
276
- function expand(task) {
277
- const bounds = task._bounds;
278
- const viewport = task._viewport;
279
-
280
- const expanded = expandBounds(viewport.width, viewport.height, bounds);
281
- for (let i = 0; i < expanded.length; i++) {
282
- const div = bounds[i].div;
283
- const divProperties = task._textDivProperties.get(div);
284
- if (divProperties.angle === 0) {
285
- divProperties.paddingLeft = bounds[i].left - expanded[i].left;
286
- divProperties.paddingTop = bounds[i].top - expanded[i].top;
287
- divProperties.paddingRight = expanded[i].right - bounds[i].right;
288
- divProperties.paddingBottom = expanded[i].bottom - bounds[i].bottom;
289
- task._textDivProperties.set(div, divProperties);
290
- continue;
291
- }
292
- // Box is rotated -- trying to find padding so rotated div will not
293
- // exceed its expanded bounds.
294
- const e = expanded[i],
295
- b = bounds[i];
296
- const m = b.m,
297
- c = m[0],
298
- s = m[1];
299
- // Finding intersections with expanded box.
300
- const points = [[0, 0], [0, b.size[1]], [b.size[0], 0], b.size];
301
- const ts = new Float64Array(64);
302
- for (let j = 0, jj = points.length; j < jj; j++) {
303
- const t = Util.applyTransform(points[j], m);
304
- ts[j + 0] = c && (e.left - t[0]) / c;
305
- ts[j + 4] = s && (e.top - t[1]) / s;
306
- ts[j + 8] = c && (e.right - t[0]) / c;
307
- ts[j + 12] = s && (e.bottom - t[1]) / s;
308
-
309
- ts[j + 16] = s && (e.left - t[0]) / -s;
310
- ts[j + 20] = c && (e.top - t[1]) / c;
311
- ts[j + 24] = s && (e.right - t[0]) / -s;
312
- ts[j + 28] = c && (e.bottom - t[1]) / c;
313
-
314
- ts[j + 32] = c && (e.left - t[0]) / -c;
315
- ts[j + 36] = s && (e.top - t[1]) / -s;
316
- ts[j + 40] = c && (e.right - t[0]) / -c;
317
- ts[j + 44] = s && (e.bottom - t[1]) / -s;
318
-
319
- ts[j + 48] = s && (e.left - t[0]) / s;
320
- ts[j + 52] = c && (e.top - t[1]) / -c;
321
- ts[j + 56] = s && (e.right - t[0]) / s;
322
- ts[j + 60] = c && (e.bottom - t[1]) / -c;
323
- }
324
- // Not based on math, but to simplify calculations, using cos and sin
325
- // absolute values to not exceed the box (it can but insignificantly).
326
- const boxScale = 1 + Math.min(Math.abs(c), Math.abs(s));
327
- divProperties.paddingLeft = findPositiveMin(ts, 32, 16) / boxScale;
328
- divProperties.paddingTop = findPositiveMin(ts, 48, 16) / boxScale;
329
- divProperties.paddingRight = findPositiveMin(ts, 0, 16) / boxScale;
330
- divProperties.paddingBottom = findPositiveMin(ts, 16, 16) / boxScale;
331
- task._textDivProperties.set(div, divProperties);
332
- }
333
- }
334
-
335
- function expandBounds(width, height, boxes) {
336
- const bounds = boxes.map(function (box, i) {
337
- return {
338
- x1: box.left,
339
- y1: box.top,
340
- x2: box.right,
341
- y2: box.bottom,
342
- index: i,
343
- x1New: undefined,
344
- x2New: undefined,
345
- };
346
- });
347
- expandBoundsLTR(width, bounds);
348
-
349
- const expanded = new Array(boxes.length);
350
- for (const b of bounds) {
351
- const i = b.index;
352
- expanded[i] = {
353
- left: b.x1New,
354
- top: 0,
355
- right: b.x2New,
356
- bottom: 0,
357
- };
358
- }
359
-
360
- // Rotating on 90 degrees and extending extended boxes. Reusing the bounds
361
- // array and objects.
362
- boxes.map(function (box, i) {
363
- const e = expanded[i],
364
- b = bounds[i];
365
- b.x1 = box.top;
366
- b.y1 = width - e.right;
367
- b.x2 = box.bottom;
368
- b.y2 = width - e.left;
369
- b.index = i;
370
- b.x1New = undefined;
371
- b.x2New = undefined;
372
- });
373
- expandBoundsLTR(height, bounds);
374
-
375
- for (const b of bounds) {
376
- const i = b.index;
377
- expanded[i].top = b.x1New;
378
- expanded[i].bottom = b.x2New;
379
- }
380
- return expanded;
381
- }
382
-
383
- function expandBoundsLTR(width, bounds) {
384
- // Sorting by x1 coordinate and walk by the bounds in the same order.
385
- bounds.sort(function (a, b) {
386
- return a.x1 - b.x1 || a.index - b.index;
387
- });
388
-
389
- // First we see on the horizon is a fake boundary.
390
- const fakeBoundary = {
391
- x1: -Infinity,
392
- y1: -Infinity,
393
- x2: 0,
394
- y2: Infinity,
395
- index: -1,
396
- x1New: 0,
397
- x2New: 0,
398
- };
399
- const horizon = [
400
- {
401
- start: -Infinity,
402
- end: Infinity,
403
- boundary: fakeBoundary,
404
- },
405
- ];
406
-
407
- for (const boundary of bounds) {
408
- // Searching for the affected part of horizon.
409
- // TODO red-black tree or simple binary search
410
- let i = 0;
411
- while (i < horizon.length && horizon[i].end <= boundary.y1) {
412
- i++;
413
- }
414
- let j = horizon.length - 1;
415
- while (j >= 0 && horizon[j].start >= boundary.y2) {
416
- j--;
417
- }
418
-
419
- let horizonPart, affectedBoundary;
420
- let q,
421
- k,
422
- maxXNew = -Infinity;
423
- for (q = i; q <= j; q++) {
424
- horizonPart = horizon[q];
425
- affectedBoundary = horizonPart.boundary;
426
- let xNew;
427
- if (affectedBoundary.x2 > boundary.x1) {
428
- // In the middle of the previous element, new x shall be at the
429
- // boundary start. Extending if further if the affected boundary
430
- // placed on top of the current one.
431
- xNew =
432
- affectedBoundary.index > boundary.index
433
- ? affectedBoundary.x1New
434
- : boundary.x1;
435
- } else if (affectedBoundary.x2New === undefined) {
436
- // We have some space in between, new x in middle will be a fair
437
- // choice.
438
- xNew = (affectedBoundary.x2 + boundary.x1) / 2;
439
- } else {
440
- // Affected boundary has x2new set, using it as new x.
441
- xNew = affectedBoundary.x2New;
442
- }
443
- if (xNew > maxXNew) {
444
- maxXNew = xNew;
445
- }
446
- }
447
-
448
- // Set new x1 for current boundary.
449
- boundary.x1New = maxXNew;
450
-
451
- // Adjusts new x2 for the affected boundaries.
452
- for (q = i; q <= j; q++) {
453
- horizonPart = horizon[q];
454
- affectedBoundary = horizonPart.boundary;
455
- if (affectedBoundary.x2New === undefined) {
456
- // Was not set yet, choosing new x if possible.
457
- if (affectedBoundary.x2 > boundary.x1) {
458
- // Current and affected boundaries intersect. If affected boundary
459
- // is placed on top of the current, shrinking the affected.
460
- if (affectedBoundary.index > boundary.index) {
461
- affectedBoundary.x2New = affectedBoundary.x2;
462
- }
463
- } else {
464
- affectedBoundary.x2New = maxXNew;
465
- }
466
- } else if (affectedBoundary.x2New > maxXNew) {
467
- // Affected boundary is touching new x, pushing it back.
468
- affectedBoundary.x2New = Math.max(maxXNew, affectedBoundary.x2);
469
- }
470
- }
471
-
472
- // Fixing the horizon.
473
- const changedHorizon = [];
474
- let lastBoundary = null;
475
- for (q = i; q <= j; q++) {
476
- horizonPart = horizon[q];
477
- affectedBoundary = horizonPart.boundary;
478
- // Checking which boundary will be visible.
479
- const useBoundary =
480
- affectedBoundary.x2 > boundary.x2 ? affectedBoundary : boundary;
481
- if (lastBoundary === useBoundary) {
482
- // Merging with previous.
483
- changedHorizon[changedHorizon.length - 1].end = horizonPart.end;
484
- } else {
485
- changedHorizon.push({
486
- start: horizonPart.start,
487
- end: horizonPart.end,
488
- boundary: useBoundary,
489
- });
490
- lastBoundary = useBoundary;
491
- }
492
- }
493
- if (horizon[i].start < boundary.y1) {
494
- changedHorizon[0].start = boundary.y1;
495
- changedHorizon.unshift({
496
- start: horizon[i].start,
497
- end: boundary.y1,
498
- boundary: horizon[i].boundary,
499
- });
500
- }
501
- if (boundary.y2 < horizon[j].end) {
502
- changedHorizon[changedHorizon.length - 1].end = boundary.y2;
503
- changedHorizon.push({
504
- start: boundary.y2,
505
- end: horizon[j].end,
506
- boundary: horizon[j].boundary,
507
- });
508
- }
509
-
510
- // Set x2 new of boundary that is no longer visible (see overlapping case
511
- // above).
512
- // TODO more efficient, e.g. via reference counting.
513
- for (q = i; q <= j; q++) {
514
- horizonPart = horizon[q];
515
- affectedBoundary = horizonPart.boundary;
516
- if (affectedBoundary.x2New !== undefined) {
517
- continue;
518
- }
519
- let used = false;
520
- for (
521
- k = i - 1;
522
- !used && k >= 0 && horizon[k].start >= affectedBoundary.y1;
523
- k--
524
- ) {
525
- used = horizon[k].boundary === affectedBoundary;
526
- }
527
- for (
528
- k = j + 1;
529
- !used && k < horizon.length && horizon[k].end <= affectedBoundary.y2;
530
- k++
531
- ) {
532
- used = horizon[k].boundary === affectedBoundary;
533
- }
534
- for (k = 0; !used && k < changedHorizon.length; k++) {
535
- used = changedHorizon[k].boundary === affectedBoundary;
536
- }
537
- if (!used) {
538
- affectedBoundary.x2New = maxXNew;
539
- }
540
- }
541
-
542
- Array.prototype.splice.apply(
543
- horizon,
544
- [i, j - i + 1].concat(changedHorizon)
545
- );
546
- }
547
-
548
- // Set new x2 for all unset boundaries.
549
- for (const horizonPart of horizon) {
550
- const affectedBoundary = horizonPart.boundary;
551
- if (affectedBoundary.x2New === undefined) {
552
- affectedBoundary.x2New = Math.max(width, affectedBoundary.x2);
553
- }
554
- }
555
- }
556
-
557
- class TextLayerRenderTask {
558
- constructor({
559
- textContent,
560
- textContentStream,
561
- container,
562
- viewport,
563
- textDivs,
564
- textContentItemsStr,
565
- enhanceTextSelection,
566
- }) {
567
- this._textContent = textContent;
568
- this._textContentStream = textContentStream;
569
- this._container = container;
570
- this._document = container.ownerDocument;
571
- this._viewport = viewport;
572
- this._textDivs = textDivs || [];
573
- this._textContentItemsStr = textContentItemsStr || [];
574
- this._enhanceTextSelection = !!enhanceTextSelection;
575
- this._fontInspectorEnabled = !!globalThis.FontInspector?.enabled;
576
-
577
- this._reader = null;
578
- this._layoutTextLastFontSize = null;
579
- this._layoutTextLastFontFamily = null;
580
- this._layoutTextCtx = null;
581
- this._textDivProperties = new WeakMap();
582
- this._renderingDone = false;
583
- this._canceled = false;
584
- this._capability = createPromiseCapability();
585
- this._renderTimer = null;
586
- this._bounds = [];
587
-
588
- // Always clean-up the temporary canvas once rendering is no longer pending.
589
- this._capability.promise
590
- .finally(() => {
591
- if (this._layoutTextCtx) {
592
- // Zeroing the width and height cause Firefox to release graphics
593
- // resources immediately, which can greatly reduce memory consumption.
594
- this._layoutTextCtx.canvas.width = 0;
595
- this._layoutTextCtx.canvas.height = 0;
596
- this._layoutTextCtx = null;
597
- }
598
- })
599
- .catch(() => {
600
- /* Avoid "Uncaught promise" messages in the console. */
601
- });
602
- }
603
-
604
- /**
605
- * Promise for textLayer rendering task completion.
606
- * @type {Promise<void>}
607
- */
608
- get promise() {
609
- return this._capability.promise;
610
- }
611
-
612
- /**
613
- * Cancel rendering of the textLayer.
614
- */
615
- cancel() {
616
- this._canceled = true;
617
- if (this._reader) {
618
- this._reader.cancel(new AbortException("TextLayer task cancelled."));
619
- this._reader = null;
620
- }
621
- if (this._renderTimer !== null) {
622
- clearTimeout(this._renderTimer);
623
- this._renderTimer = null;
624
- }
625
- this._capability.reject(new Error("TextLayer task cancelled."));
626
- }
627
-
628
- /**
629
- * @private
630
- */
631
- _processItems(items, styleCache) {
632
- for (let i = 0, len = items.length; i < len; i++) {
633
- if (items[i].str === undefined) {
634
- if (
635
- items[i].type === "beginMarkedContentProps" ||
636
- items[i].type === "beginMarkedContent"
637
- ) {
638
- const parent = this._container;
639
- this._container = document.createElement("span");
640
- this._container.classList.add("markedContent");
641
- if (items[i].id !== null) {
642
- this._container.setAttribute("id", `${items[i].id}`);
643
- }
644
- parent.appendChild(this._container);
645
- } else if (items[i].type === "endMarkedContent") {
646
- this._container = this._container.parentNode;
647
- }
648
- continue;
649
- }
650
- this._textContentItemsStr.push(items[i].str);
651
- appendText(this, items[i], styleCache, this._layoutTextCtx);
652
- }
653
- }
654
-
655
- /**
656
- * @private
657
- */
658
- _layoutText(textDiv) {
659
- const textDivProperties = this._textDivProperties.get(textDiv);
660
-
661
- let transform = "";
662
- if (textDivProperties.canvasWidth !== 0 && textDivProperties.hasText) {
663
- const { fontSize, fontFamily } = textDiv.style;
664
-
665
- // Only build font string and set to context if different from last.
666
- if (
667
- fontSize !== this._layoutTextLastFontSize ||
668
- fontFamily !== this._layoutTextLastFontFamily
669
- ) {
670
- this._layoutTextCtx.font = `${fontSize} ${fontFamily}`;
671
- this._layoutTextLastFontSize = fontSize;
672
- this._layoutTextLastFontFamily = fontFamily;
673
- }
674
- // Only measure the width for multi-char text divs, see `appendText`.
675
- const { width } = this._layoutTextCtx.measureText(textDiv.textContent);
676
-
677
- if (width > 0) {
678
- textDivProperties.scale = textDivProperties.canvasWidth / width;
679
- transform = `scaleX(${textDivProperties.scale})`;
680
- }
681
- }
682
- if (textDivProperties.angle !== 0) {
683
- transform = `rotate(${textDivProperties.angle}deg) ${transform}`;
684
- }
685
- if (transform.length > 0) {
686
- if (this._enhanceTextSelection) {
687
- textDivProperties.originalTransform = transform;
688
- }
689
- textDiv.style.transform = transform;
690
- }
691
-
692
- if (textDivProperties.hasText) {
693
- this._container.appendChild(textDiv);
694
- }
695
- if (textDivProperties.hasEOL) {
696
- const br = document.createElement("br");
697
- br.setAttribute("role", "presentation");
698
- this._container.appendChild(br);
699
- }
700
- }
701
-
702
- /**
703
- * @private
704
- */
705
- _render(timeout = 0) {
706
- const capability = createPromiseCapability();
707
- let styleCache = Object.create(null);
708
-
709
- // The temporary canvas is used to measure text length in the DOM.
710
- const canvas = this._document.createElement("canvas");
711
- canvas.height = canvas.width = DEFAULT_FONT_SIZE;
712
-
713
- if (
714
- typeof PDFJSDev === "undefined" ||
715
- PDFJSDev.test("MOZCENTRAL || GENERIC")
716
- ) {
717
- canvas.mozOpaque = true;
718
- }
719
- this._layoutTextCtx = canvas.getContext("2d", { alpha: false });
720
-
721
- if (this._textContent) {
722
- const textItems = this._textContent.items;
723
- const textStyles = this._textContent.styles;
724
- this._processItems(textItems, textStyles);
725
- capability.resolve();
726
- } else if (this._textContentStream) {
727
- const pump = () => {
728
- this._reader.read().then(({ value, done }) => {
729
- if (done) {
730
- capability.resolve();
731
- return;
732
- }
733
-
734
- Object.assign(styleCache, value.styles);
735
- this._processItems(value.items, styleCache);
736
- pump();
737
- }, capability.reject);
738
- };
739
-
740
- this._reader = this._textContentStream.getReader();
741
- pump();
742
- } else {
743
- throw new Error(
744
- 'Neither "textContent" nor "textContentStream"' +
745
- " parameters specified."
746
- );
747
- }
748
-
749
- capability.promise.then(() => {
750
- styleCache = null;
751
- if (!timeout) {
752
- // Render right away
753
- render(this);
754
- } else {
755
- // Schedule
756
- this._renderTimer = setTimeout(() => {
757
- render(this);
758
- this._renderTimer = null;
759
- }, timeout);
760
- }
761
- }, this._capability.reject);
762
- }
763
-
764
- /**
765
- * @param {boolean} [expandDivs]
766
- */
767
- expandTextDivs(expandDivs = false) {
768
- if (!this._enhanceTextSelection || !this._renderingDone) {
769
- return;
770
- }
771
- if (this._bounds !== null) {
772
- expand(this);
773
- this._bounds = null;
774
- }
775
- const transformBuf = [],
776
- paddingBuf = [];
777
-
778
- for (let i = 0, ii = this._textDivs.length; i < ii; i++) {
779
- const div = this._textDivs[i];
780
- const divProps = this._textDivProperties.get(div);
781
-
782
- if (!divProps.hasText) {
783
- continue;
784
- }
785
- if (expandDivs) {
786
- transformBuf.length = 0;
787
- paddingBuf.length = 0;
788
-
789
- if (divProps.originalTransform) {
790
- transformBuf.push(divProps.originalTransform);
791
- }
792
- if (divProps.paddingTop > 0) {
793
- paddingBuf.push(`${divProps.paddingTop}px`);
794
- transformBuf.push(`translateY(${-divProps.paddingTop}px)`);
795
- } else {
796
- paddingBuf.push(0);
797
- }
798
- if (divProps.paddingRight > 0) {
799
- paddingBuf.push(`${divProps.paddingRight / divProps.scale}px`);
800
- } else {
801
- paddingBuf.push(0);
802
- }
803
- if (divProps.paddingBottom > 0) {
804
- paddingBuf.push(`${divProps.paddingBottom}px`);
805
- } else {
806
- paddingBuf.push(0);
807
- }
808
- if (divProps.paddingLeft > 0) {
809
- paddingBuf.push(`${divProps.paddingLeft / divProps.scale}px`);
810
- transformBuf.push(
811
- `translateX(${-divProps.paddingLeft / divProps.scale}px)`
812
- );
813
- } else {
814
- paddingBuf.push(0);
815
- }
816
-
817
- div.style.padding = paddingBuf.join(" ");
818
- if (transformBuf.length) {
819
- div.style.transform = transformBuf.join(" ");
820
- }
821
- } else {
822
- div.style.padding = null;
823
- div.style.transform = divProps.originalTransform;
824
- }
825
- }
826
- }
827
- }
828
-
829
- /**
830
- * @param {TextLayerRenderParameters} renderParameters
831
- * @returns {TextLayerRenderTask}
832
- */
833
- function renderTextLayer(renderParameters) {
834
- const task = new TextLayerRenderTask({
835
- textContent: renderParameters.textContent,
836
- textContentStream: renderParameters.textContentStream,
837
- container: renderParameters.container,
838
- viewport: renderParameters.viewport,
839
- textDivs: renderParameters.textDivs,
840
- textContentItemsStr: renderParameters.textContentItemsStr,
841
- enhanceTextSelection: renderParameters.enhanceTextSelection,
842
- });
843
- task._render(renderParameters.timeout);
844
- return task;
845
- }
846
-
847
- export { renderTextLayer };