forked from mozilla/pdf.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdriver.js
920 lines (838 loc) · 29.4 KB
/
driver.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
/* Copyright 2012 Mozilla Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* eslint-disable no-var */
/* globals pdfjsLib, pdfjsViewer */
"use strict";
const WAITING_TIME = 100; // ms
const PDF_TO_CSS_UNITS = 96.0 / 72.0;
const CMAP_URL = "/build/generic/web/cmaps/";
const CMAP_PACKED = true;
const STANDARD_FONT_DATA_URL = "/build/generic/web/standard_fonts/";
const IMAGE_RESOURCES_PATH = "/web/images/";
const WORKER_SRC = "../build/generic/build/pdf.worker.js";
const RENDER_TASK_ON_CONTINUE_DELAY = 5; // ms
const SVG_NS = "http://www.w3.org/2000/svg";
function loadStyles(styles) {
styles = Object.values(styles);
if (styles.every(style => style.promise)) {
return Promise.all(styles.map(style => style.promise));
}
for (const style of styles) {
style.promise = new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.open("GET", style.file);
xhr.onload = function () {
resolve(xhr.responseText);
};
xhr.onerror = function (e) {
reject(new Error(`Error fetching style (${style.file}): ${e}`));
};
xhr.send(null);
});
}
return Promise.all(styles.map(style => style.promise));
}
function writeSVG(svgElement, ctx, resolve, reject) {
// We need to have UTF-8 encoded XML.
const svg_xml = unescape(
encodeURIComponent(new XMLSerializer().serializeToString(svgElement))
);
const img = new Image();
img.src = "data:image/svg+xml;base64," + btoa(svg_xml);
img.onload = function () {
ctx.drawImage(img, 0, 0);
resolve();
};
img.onerror = function (e) {
reject(new Error("Error rasterizing text layer " + e));
};
}
function inlineImages(images) {
const imagePromises = [];
for (let i = 0, ii = images.length; i < ii; i++) {
imagePromises.push(
new Promise(function (resolve, reject) {
const xhr = new XMLHttpRequest();
xhr.responseType = "blob";
xhr.onload = function () {
const reader = new FileReader();
reader.onloadend = function () {
resolve(reader.result);
};
reader.readAsDataURL(xhr.response);
};
xhr.onerror = function (e) {
reject(new Error("Error fetching inline image " + e));
};
xhr.open("GET", images[i].src);
xhr.send();
})
);
}
return Promise.all(imagePromises);
}
async function resolveImages(node, silentErrors = false) {
const images = node.getElementsByTagName("img");
const data = await inlineImages(images);
const loadedPromises = [];
for (let i = 0, ii = data.length; i < ii; i++) {
loadedPromises.push(
new Promise(function (resolveImage, rejectImage) {
images[i].onload = resolveImage;
images[i].onerror = function (e) {
if (silentErrors) {
resolveImage();
} else {
rejectImage(new Error("Error loading image " + e));
}
};
images[i].src = data[i];
})
);
}
await Promise.all(loadedPromises);
}
/**
* @class
*/
var rasterizeTextLayer = (function rasterizeTextLayerClosure() {
const styles = {
common: {
file: "./text_layer_test.css",
promise: null,
},
};
function getTextLayerStyle() {
return loadStyles(styles);
}
// eslint-disable-next-line no-shadow
function rasterizeTextLayer(
ctx,
viewport,
textContent,
enhanceTextSelection
) {
return new Promise(function (resolve, reject) {
// Building SVG with size of the viewport.
var svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("width", viewport.width + "px");
svg.setAttribute("height", viewport.height + "px");
// items are transformed to have 1px font size
svg.setAttribute("font-size", 1);
// Adding element to host our HTML (style + text layer div).
var foreignObject = document.createElementNS(SVG_NS, "svg:foreignObject");
foreignObject.setAttribute("x", "0");
foreignObject.setAttribute("y", "0");
foreignObject.setAttribute("width", viewport.width + "px");
foreignObject.setAttribute("height", viewport.height + "px");
var style = document.createElement("style");
var stylePromise = getTextLayerStyle();
foreignObject.appendChild(style);
var div = document.createElement("div");
div.className = "textLayer";
foreignObject.appendChild(div);
stylePromise
.then(async cssRules => {
style.textContent = cssRules;
// Rendering text layer as HTML.
var task = pdfjsLib.renderTextLayer({
textContent,
container: div,
viewport,
enhanceTextSelection,
});
await task.promise;
task.expandTextDivs(true);
svg.appendChild(foreignObject);
writeSVG(svg, ctx, resolve, reject);
})
.catch(reason => {
reject(new Error(`rasterizeTextLayer: "${reason?.message}".`));
});
});
}
return rasterizeTextLayer;
})();
/**
* @class
*/
var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
/**
* For the reference tests, the entire annotation layer must be visible. To
* achieve this, we load the common styles as used by the viewer and extend
* them with a set of overrides to make all elements visible.
*
* Note that we cannot simply use `@import` to import the common styles in
* the overrides file because the browser does not resolve that when the
* styles are inserted via XHR. Therefore, we load and combine them here.
*/
const styles = {
common: {
file: "../web/annotation_layer_builder.css",
promise: null,
},
overrides: {
file: "./annotation_layer_builder_overrides.css",
promise: null,
},
};
function getAnnotationLayerStyle() {
return loadStyles(styles);
}
// eslint-disable-next-line no-shadow
function rasterizeAnnotationLayer(
ctx,
viewport,
annotations,
page,
imageResourcesPath,
renderInteractiveForms
) {
return new Promise(function (resolve, reject) {
// Building SVG with size of the viewport.
var svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("width", viewport.width + "px");
svg.setAttribute("height", viewport.height + "px");
// Adding element to host our HTML (style + annotation layer div).
var foreignObject = document.createElementNS(SVG_NS, "svg:foreignObject");
foreignObject.setAttribute("x", "0");
foreignObject.setAttribute("y", "0");
foreignObject.setAttribute("width", viewport.width + "px");
foreignObject.setAttribute("height", viewport.height + "px");
var style = document.createElement("style");
var stylePromise = getAnnotationLayerStyle();
foreignObject.appendChild(style);
var div = document.createElement("div");
div.className = "annotationLayer";
// Rendering annotation layer as HTML.
stylePromise
.then(async (common, overrides) => {
style.textContent = common + overrides;
var annotation_viewport = viewport.clone({ dontFlip: true });
var parameters = {
viewport: annotation_viewport,
div,
annotations,
page,
linkService: new pdfjsViewer.SimpleLinkService(),
imageResourcesPath,
renderInteractiveForms,
};
pdfjsLib.AnnotationLayer.render(parameters);
// Inline SVG images from text annotations.
await resolveImages(div);
foreignObject.appendChild(div);
svg.appendChild(foreignObject);
writeSVG(svg, ctx, resolve, reject);
})
.catch(reason => {
reject(new Error(`rasterizeAnnotationLayer: "${reason?.message}".`));
});
});
}
return rasterizeAnnotationLayer;
})();
/**
* @class
*/
var rasterizeXfaLayer = (function rasterizeXfaLayerClosure() {
const styles = {
common: {
file: "../web/xfa_layer_builder.css",
promise: null,
},
};
function getXfaLayerStyle() {
return loadStyles(styles);
}
// eslint-disable-next-line no-shadow
function rasterizeXfaLayer(
ctx,
viewport,
xfa,
fontRules,
annotationStorage,
isPrint
) {
return new Promise(function (resolve, reject) {
// Building SVG with size of the viewport.
const svg = document.createElementNS(SVG_NS, "svg:svg");
svg.setAttribute("width", viewport.width + "px");
svg.setAttribute("height", viewport.height + "px");
const foreignObject = document.createElementNS(
SVG_NS,
"svg:foreignObject"
);
foreignObject.setAttribute("x", "0");
foreignObject.setAttribute("y", "0");
foreignObject.setAttribute("width", viewport.width + "px");
foreignObject.setAttribute("height", viewport.height + "px");
const style = document.createElement("style");
const stylePromise = getXfaLayerStyle();
foreignObject.appendChild(style);
const div = document.createElement("div");
foreignObject.appendChild(div);
stylePromise
.then(async cssRules => {
style.textContent = fontRules + "\n" + cssRules;
pdfjsLib.XfaLayer.render({
xfa,
div,
viewport: viewport.clone({ dontFlip: true }),
annotationStorage,
intent: isPrint ? "print" : "display",
});
// Some unsupported type of images (e.g. tiff)
// lead to errors.
await resolveImages(div, /* silentErrors = */ true);
svg.appendChild(foreignObject);
writeSVG(svg, ctx, resolve, reject);
})
.catch(reason => {
reject(new Error(`rasterizeXfaLayer: "${reason?.message}".`));
});
});
}
return rasterizeXfaLayer;
})();
/**
* @typedef {Object} DriverOptions
* @property {HTMLSpanElement} inflight - Field displaying the number of
* inflight requests.
* @property {HTMLInputElement} disableScrolling - Checkbox to disable
* automatic scrolling of the output container.
* @property {HTMLPreElement} output - Container for all output messages.
* @property {HTMLDivElement} end - Container for a completion message.
*/
/**
* @class
*/
// eslint-disable-next-line no-unused-vars
var Driver = (function DriverClosure() {
/**
* @constructs Driver
* @param {DriverOptions} options
*/
// eslint-disable-next-line no-shadow
function Driver(options) {
// Configure the global worker options.
pdfjsLib.GlobalWorkerOptions.workerSrc = WORKER_SRC;
// Set the passed options
this.inflight = options.inflight;
this.disableScrolling = options.disableScrolling;
this.output = options.output;
this.end = options.end;
// Set parameters from the query string
var parameters = this._getQueryStringParameters();
this.browser = parameters.browser;
this.manifestFile = parameters.manifestFile;
this.delay = parameters.delay | 0 || 0;
this.inFlightRequests = 0;
this.testFilter = parameters.testFilter
? JSON.parse(parameters.testFilter)
: [];
// Create a working canvas
this.canvas = document.createElement("canvas");
}
Driver.prototype = {
_getQueryStringParameters: function Driver_getQueryStringParameters() {
var queryString = window.location.search.substring(1);
var values = queryString.split("&");
var parameters = {};
for (var i = 0, ii = values.length; i < ii; i++) {
var value = values[i].split("=");
parameters[unescape(value[0])] = unescape(value[1]);
}
return parameters;
},
run: function Driver_run() {
var self = this;
window.onerror = function (message, source, line, column, error) {
self._info(
"Error: " +
message +
" Script: " +
source +
" Line: " +
line +
" Column: " +
column +
" StackTrace: " +
error
);
};
this._info("User agent: " + navigator.userAgent);
this._log(`Harness thinks this browser is ${this.browser}\n`);
this._log('Fetching manifest "' + this.manifestFile + '"... ');
var r = new XMLHttpRequest();
r.open("GET", this.manifestFile, false);
r.onreadystatechange = function () {
if (r.readyState === 4) {
self._log("done\n");
self.manifest = JSON.parse(r.responseText);
if (self.testFilter && self.testFilter.length) {
self.manifest = self.manifest.filter(function (item) {
return self.testFilter.includes(item.id);
});
}
self.currentTask = 0;
self._nextTask();
}
};
if (this.delay > 0) {
this._log("\nDelaying for " + this.delay + " ms...\n");
}
// When gathering the stats the numbers seem to be more reliable
// if the browser is given more time to start.
setTimeout(function () {
r.send(null);
}, this.delay);
},
_nextTask() {
let failure = "";
this._cleanup().then(() => {
if (this.currentTask === this.manifest.length) {
this._done();
return;
}
const task = this.manifest[this.currentTask];
task.round = 0;
task.pageNum = task.firstPage || 1;
task.stats = { times: [] };
task.enableXfa = task.enableXfa === true;
// Support *linked* test-cases for the other suites, e.g. unit- and
// integration-tests, without needing to run them as reference-tests.
if (task.type === "other") {
this._log(`Skipping file "${task.file}"\n`);
if (!task.link) {
this._nextPage(task, 'Expected "other" test-case to be linked.');
return;
}
this.currentTask++;
this._nextTask();
return;
}
this._log('Loading file "' + task.file + '"\n');
const absoluteUrl = new URL(task.file, window.location).href;
try {
let xfaStyleElement = null;
if (task.enableXfa) {
// Need to get the font definitions to inject them in the SVG.
// So we create this element and those definitions will be
// appended in font_loader.js.
xfaStyleElement = document.createElement("style");
document.documentElement
.getElementsByTagName("head")[0]
.appendChild(xfaStyleElement);
}
const loadingTask = pdfjsLib.getDocument({
url: absoluteUrl,
password: task.password,
cMapUrl: CMAP_URL,
cMapPacked: CMAP_PACKED,
standardFontDataUrl: STANDARD_FONT_DATA_URL,
disableRange: task.disableRange,
disableAutoFetch: !task.enableAutoFetch,
pdfBug: true,
useSystemFonts: task.useSystemFonts,
useWorkerFetch: task.useWorkerFetch,
enableXfa: task.enableXfa,
styleElement: xfaStyleElement,
});
loadingTask.promise.then(
doc => {
if (task.enableXfa) {
task.fontRules = "";
for (const rule of xfaStyleElement.sheet.cssRules) {
task.fontRules += rule.cssText + "\n";
}
}
task.pdfDoc = doc;
task.optionalContentConfigPromise =
doc.getOptionalContentConfig();
this._nextPage(task, failure);
},
err => {
failure = "Loading PDF document: " + err;
this._nextPage(task, failure);
}
);
return;
} catch (e) {
failure = "Loading PDF document: " + this._exceptionToString(e);
}
this._nextPage(task, failure);
});
},
_cleanup() {
// Clear out all the stylesheets since a new one is created for each font.
while (document.styleSheets.length > 0) {
const styleSheet = document.styleSheets[0];
while (styleSheet.cssRules.length > 0) {
styleSheet.deleteRule(0);
}
styleSheet.ownerNode.remove();
}
const body = document.body;
while (body.lastChild !== this.end) {
body.removeChild(body.lastChild);
}
const destroyedPromises = [];
// Wipe out the link to the pdfdoc so it can be GC'ed.
for (let i = 0; i < this.manifest.length; i++) {
if (this.manifest[i].pdfDoc) {
destroyedPromises.push(this.manifest[i].pdfDoc.destroy());
delete this.manifest[i].pdfDoc;
}
}
return Promise.all(destroyedPromises);
},
_exceptionToString: function Driver_exceptionToString(e) {
if (typeof e !== "object") {
return String(e);
}
if (!("message" in e)) {
return JSON.stringify(e);
}
return e.message + ("stack" in e ? " at " + e.stack.split("\n")[0] : "");
},
_getLastPageNumber: function Driver_getLastPageNumber(task) {
if (!task.pdfDoc) {
return task.firstPage || 1;
}
var lastPageNumber = task.lastPage || 0;
if (!lastPageNumber || lastPageNumber > task.pdfDoc.numPages) {
lastPageNumber = task.pdfDoc.numPages;
}
return lastPageNumber;
},
_nextPage: function Driver_nextPage(task, loadError) {
var self = this;
var failure = loadError || "";
var ctx;
if (!task.pdfDoc) {
var dataUrl = this.canvas.toDataURL("image/png");
this._sendResult(dataUrl, task, failure, function () {
self._log(
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
);
self.currentTask++;
self._nextTask();
});
return;
}
if (task.pageNum > this._getLastPageNumber(task)) {
if (++task.round < task.rounds) {
this._log(" Round " + (1 + task.round) + "\n");
task.pageNum = task.firstPage || 1;
} else {
this.currentTask++;
this._nextTask();
return;
}
}
if (task.skipPages && task.skipPages.includes(task.pageNum)) {
this._log(
" Skipping page " +
task.pageNum +
"/" +
task.pdfDoc.numPages +
"...\n"
);
task.pageNum++;
this._nextPage(task);
return;
}
if (!failure) {
try {
this._log(
" Loading page " +
task.pageNum +
"/" +
task.pdfDoc.numPages +
"... "
);
this.canvas.mozOpaque = true;
ctx = this.canvas.getContext("2d", { alpha: false });
task.pdfDoc.getPage(task.pageNum).then(
function (page) {
var viewport = page.getViewport({ scale: PDF_TO_CSS_UNITS });
self.canvas.width = viewport.width;
self.canvas.height = viewport.height;
self._clearCanvas();
// Initialize various `eq` test subtypes, see comment below.
var renderAnnotations = false,
renderForms = false,
renderPrint = false,
renderXfa = false;
if (task.annotationStorage) {
const entries = Object.entries(task.annotationStorage),
docAnnotationStorage = task.pdfDoc.annotationStorage;
for (const [key, value] of entries) {
docAnnotationStorage.setValue(key, value);
}
}
var textLayerCanvas, annotationLayerCanvas;
var initPromise;
if (task.type === "text") {
// Using a dummy canvas for PDF context drawing operations
textLayerCanvas = self.textLayerCanvas;
if (!textLayerCanvas) {
textLayerCanvas = document.createElement("canvas");
self.textLayerCanvas = textLayerCanvas;
}
textLayerCanvas.width = viewport.width;
textLayerCanvas.height = viewport.height;
var textLayerContext = textLayerCanvas.getContext("2d");
textLayerContext.clearRect(
0,
0,
textLayerCanvas.width,
textLayerCanvas.height
);
var enhanceText = !!task.enhance;
// The text builder will draw its content on the test canvas
initPromise = page
.getTextContent({
normalizeWhitespace: true,
includeMarkedContent: true,
})
.then(function (textContent) {
return rasterizeTextLayer(
textLayerContext,
viewport,
textContent,
enhanceText
);
});
} else {
textLayerCanvas = null;
// We fetch the `eq` specific test subtypes here, to avoid
// accidentally changing the behaviour for other types of tests.
renderAnnotations = !!task.annotations;
renderForms = !!task.forms;
renderPrint = !!task.print;
renderXfa = !!task.enableXfa;
// Render the annotation layer if necessary.
if (renderAnnotations || renderForms || renderXfa) {
// Create a dummy canvas for the drawing operations.
annotationLayerCanvas = self.annotationLayerCanvas;
if (!annotationLayerCanvas) {
annotationLayerCanvas = document.createElement("canvas");
self.annotationLayerCanvas = annotationLayerCanvas;
}
annotationLayerCanvas.width = viewport.width;
annotationLayerCanvas.height = viewport.height;
var annotationLayerContext =
annotationLayerCanvas.getContext("2d");
annotationLayerContext.clearRect(
0,
0,
annotationLayerCanvas.width,
annotationLayerCanvas.height
);
if (!renderXfa) {
// The annotation builder will draw its content
// on the canvas.
initPromise = page
.getAnnotations({ intent: "display" })
.then(function (annotations) {
return rasterizeAnnotationLayer(
annotationLayerContext,
viewport,
annotations,
page,
IMAGE_RESOURCES_PATH,
renderForms
);
});
} else {
initPromise = page.getXfa().then(function (xfa) {
return rasterizeXfaLayer(
annotationLayerContext,
viewport,
xfa,
task.fontRules,
task.pdfDoc.annotationStorage,
task.renderPrint
);
});
}
} else {
annotationLayerCanvas = null;
initPromise = Promise.resolve();
}
}
var renderContext = {
canvasContext: ctx,
viewport,
renderInteractiveForms: renderForms,
optionalContentConfigPromise: task.optionalContentConfigPromise,
};
if (renderPrint) {
if (task.annotationStorage) {
renderContext.includeAnnotationStorage = true;
}
renderContext.intent = "print";
}
var completeRender = function (error) {
// if text layer is present, compose it on top of the page
if (textLayerCanvas) {
ctx.save();
ctx.globalCompositeOperation = "screen";
ctx.fillStyle = "rgb(128, 255, 128)"; // making it green
ctx.fillRect(0, 0, viewport.width, viewport.height);
ctx.restore();
ctx.drawImage(textLayerCanvas, 0, 0);
}
// If we have annotation layer, compose it on top of the page.
if (annotationLayerCanvas) {
ctx.drawImage(annotationLayerCanvas, 0, 0);
}
if (page.stats) {
// Get the page stats *before* running cleanup.
task.stats = page.stats;
}
page.cleanup(/* resetStats = */ true);
self._snapshot(task, error);
};
initPromise
.then(function () {
const renderTask = page.render(renderContext);
if (task.renderTaskOnContinue) {
renderTask.onContinue = function (cont) {
// Slightly delay the continued rendering.
setTimeout(cont, RENDER_TASK_ON_CONTINUE_DELAY);
};
}
return renderTask.promise.then(function () {
completeRender(false);
});
})
.catch(function (error) {
completeRender("render : " + error);
});
},
function (error) {
self._snapshot(task, "render : " + error);
}
);
} catch (e) {
failure = "page setup : " + this._exceptionToString(e);
this._snapshot(task, failure);
}
}
},
_clearCanvas: function Driver_clearCanvas() {
var ctx = this.canvas.getContext("2d", { alpha: false });
ctx.beginPath();
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
},
_snapshot: function Driver_snapshot(task, failure) {
var self = this;
this._log("Snapshotting... ");
var dataUrl = this.canvas.toDataURL("image/png");
this._sendResult(dataUrl, task, failure, function () {
self._log(
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
);
task.pageNum++;
self._nextPage(task);
});
},
_quit: function Driver_quit() {
this._log("Done !");
this.end.textContent = "Tests finished. Close this window!";
// Send the quit request
var r = new XMLHttpRequest();
r.open("POST", `/tellMeToQuit?browser=${escape(this.browser)}`, false);
r.onreadystatechange = function (e) {
if (r.readyState === 4) {
window.close();
}
};
r.send(null);
},
_info: function Driver_info(message) {
this._send(
"/info",
JSON.stringify({
browser: this.browser,
message,
})
);
},
_log: function Driver_log(message) {
// Using insertAdjacentHTML yields a large performance gain and
// reduces runtime significantly.
if (this.output.insertAdjacentHTML) {
// eslint-disable-next-line no-unsanitized/method
this.output.insertAdjacentHTML("BeforeEnd", message);
} else {
this.output.textContent += message;
}
if (message.lastIndexOf("\n") >= 0 && !this.disableScrolling.checked) {
// Scroll to the bottom of the page
this.output.scrollTop = this.output.scrollHeight;
}
},
_done: function Driver_done() {
if (this.inFlightRequests > 0) {
this.inflight.textContent = this.inFlightRequests;
setTimeout(this._done.bind(this), WAITING_TIME);
} else {
setTimeout(this._quit.bind(this), WAITING_TIME);
}
},
_sendResult: function Driver_sendResult(snapshot, task, failure, callback) {
var result = JSON.stringify({
browser: this.browser,
id: task.id,
numPages: task.pdfDoc ? task.lastPage || task.pdfDoc.numPages : 0,
lastPageNum: this._getLastPageNumber(task),
failure,
file: task.file,
round: task.round,
page: task.pageNum,
snapshot,
stats: task.stats.times,
});
this._send("/submit_task_results", result, callback);
},
_send: function Driver_send(url, message, callback) {
var self = this;
var r = new XMLHttpRequest();
r.open("POST", url, true);
r.setRequestHeader("Content-Type", "application/json");
r.onreadystatechange = function (e) {
if (r.readyState === 4) {
self.inFlightRequests--;
// Retry until successful
if (r.status !== 200) {
setTimeout(function () {
self._send(url, message);
});
}
if (callback) {
callback();
}
}
};
this.inflight.textContent = this.inFlightRequests++;
r.send(message);
},
};
return Driver;
})();