2012-09-01 07:48:21 +09:00
|
|
|
/* 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.
|
|
|
|
*/
|
2018-03-16 05:49:28 +09:00
|
|
|
/* globals pdfjsLib, pdfjsViewer */
|
2012-09-01 07:48:21 +09:00
|
|
|
|
[api-minor] Introduce a new `annotationMode`-option, in `PDFPageProxy.{render, getOperatorList}`
*This is a follow-up to PRs 13867 and 13899.*
This patch is tagged `api-minor` for the following reasons:
- It replaces the `renderInteractiveForms`/`includeAnnotationStorage`-options, in the `PDFPageProxy.render`-method, with the single `annotationMode`-option that controls which annotations are being rendered and how. Note that the old options were mutually exclusive, and setting both to `true` would result in undefined behaviour.
- For improved consistency in the API, the `annotationMode`-option will also work together with the `PDFPageProxy.getOperatorList`-method.
- It's now also possible to disable *all* annotation rendering in both the API and the Viewer, since the other changes meant that this could now be supported with a single added line on the worker-thread[1]; fixes 7282.
---
[1] Please note that in order to simplify the overall implementation, we'll purposely only support disabling of *all* annotations and that the option is being shared between the API and the Viewer. For any more "specialized" use-cases, where e.g. only some annotation-types are being rendered and/or the API and Viewer render different sets of annotations, that'll have to be handled in third-party implementations/forks of the PDF.js code-base.
2021-08-08 21:36:28 +09:00
|
|
|
const {
|
|
|
|
AnnotationLayer,
|
|
|
|
AnnotationMode,
|
|
|
|
getDocument,
|
|
|
|
GlobalWorkerOptions,
|
2021-09-11 18:11:32 +09:00
|
|
|
PixelsPerInch,
|
2022-03-08 01:41:41 +09:00
|
|
|
PromiseCapability,
|
[api-minor] Introduce a new `annotationMode`-option, in `PDFPageProxy.{render, getOperatorList}`
*This is a follow-up to PRs 13867 and 13899.*
This patch is tagged `api-minor` for the following reasons:
- It replaces the `renderInteractiveForms`/`includeAnnotationStorage`-options, in the `PDFPageProxy.render`-method, with the single `annotationMode`-option that controls which annotations are being rendered and how. Note that the old options were mutually exclusive, and setting both to `true` would result in undefined behaviour.
- For improved consistency in the API, the `annotationMode`-option will also work together with the `PDFPageProxy.getOperatorList`-method.
- It's now also possible to disable *all* annotation rendering in both the API and the Viewer, since the other changes meant that this could now be supported with a single added line on the worker-thread[1]; fixes 7282.
---
[1] Please note that in order to simplify the overall implementation, we'll purposely only support disabling of *all* annotations and that the option is being shared between the API and the Viewer. For any more "specialized" use-cases, where e.g. only some annotation-types are being rendered and/or the API and Viewer render different sets of annotations, that'll have to be handled in third-party implementations/forks of the PDF.js code-base.
2021-08-08 21:36:28 +09:00
|
|
|
renderTextLayer,
|
2021-12-05 22:57:01 +09:00
|
|
|
shadow,
|
[api-minor] Introduce a new `annotationMode`-option, in `PDFPageProxy.{render, getOperatorList}`
*This is a follow-up to PRs 13867 and 13899.*
This patch is tagged `api-minor` for the following reasons:
- It replaces the `renderInteractiveForms`/`includeAnnotationStorage`-options, in the `PDFPageProxy.render`-method, with the single `annotationMode`-option that controls which annotations are being rendered and how. Note that the old options were mutually exclusive, and setting both to `true` would result in undefined behaviour.
- For improved consistency in the API, the `annotationMode`-option will also work together with the `PDFPageProxy.getOperatorList`-method.
- It's now also possible to disable *all* annotation rendering in both the API and the Viewer, since the other changes meant that this could now be supported with a single added line on the worker-thread[1]; fixes 7282.
---
[1] Please note that in order to simplify the overall implementation, we'll purposely only support disabling of *all* annotations and that the option is being shared between the API and the Viewer. For any more "specialized" use-cases, where e.g. only some annotation-types are being rendered and/or the API and Viewer render different sets of annotations, that'll have to be handled in third-party implementations/forks of the PDF.js code-base.
2021-08-08 21:36:28 +09:00
|
|
|
XfaLayer,
|
|
|
|
} = pdfjsLib;
|
2022-11-19 07:08:11 +09:00
|
|
|
const { GenericL10n, NullL10n, parseQueryString, SimpleLinkService } =
|
|
|
|
pdfjsViewer;
|
[api-minor] Introduce a new `annotationMode`-option, in `PDFPageProxy.{render, getOperatorList}`
*This is a follow-up to PRs 13867 and 13899.*
This patch is tagged `api-minor` for the following reasons:
- It replaces the `renderInteractiveForms`/`includeAnnotationStorage`-options, in the `PDFPageProxy.render`-method, with the single `annotationMode`-option that controls which annotations are being rendered and how. Note that the old options were mutually exclusive, and setting both to `true` would result in undefined behaviour.
- For improved consistency in the API, the `annotationMode`-option will also work together with the `PDFPageProxy.getOperatorList`-method.
- It's now also possible to disable *all* annotation rendering in both the API and the Viewer, since the other changes meant that this could now be supported with a single added line on the worker-thread[1]; fixes 7282.
---
[1] Please note that in order to simplify the overall implementation, we'll purposely only support disabling of *all* annotations and that the option is being shared between the API and the Viewer. For any more "specialized" use-cases, where e.g. only some annotation-types are being rendered and/or the API and Viewer render different sets of annotations, that'll have to be handled in third-party implementations/forks of the PDF.js code-base.
2021-08-08 21:36:28 +09:00
|
|
|
|
2018-02-14 22:49:24 +09:00
|
|
|
const WAITING_TIME = 100; // ms
|
2021-06-08 19:02:26 +09:00
|
|
|
const CMAP_URL = "/build/generic/web/cmaps/";
|
2020-12-11 10:32:18 +09:00
|
|
|
const STANDARD_FONT_DATA_URL = "/build/generic/web/standard_fonts/";
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
const IMAGE_RESOURCES_PATH = "/web/images/";
|
2022-06-25 16:52:32 +09:00
|
|
|
const VIEWER_CSS = "../build/components/pdf_viewer.css";
|
2022-11-19 07:08:11 +09:00
|
|
|
const VIEWER_LOCALE = "en-US";
|
[api-major] Output JavaScript modules in the builds (issue 10317)
At this point in time all browsers, and also Node.js, support standard `import`/`export` statements and we can now finally consider outputting modern JavaScript modules in the builds.[1]
In order for this to work we can *only* use proper `import`/`export` statements throughout the main code-base, and (as expected) our Node.js support made this much more complicated since both the official builds and the GitHub Actions-based tests must keep working.[2]
One remaining issue is that the `pdf.scripting.js` file cannot be built as a JavaScript module, since doing so breaks PDF scripting.
Note that my initial goal was to try and split these changes into a couple of commits, however that unfortunately didn't really work since it turned out to be difficult for smaller patches to work correctly and pass (all) tests that way.[3]
This is a classic case of every change requiring a couple of other changes, with each of those changes requiring further changes in turn and the size/scope quickly increasing as a result.
One possible "issue" with these changes is that we'll now only output JavaScript modules in the builds, which could perhaps be a problem with older tools. However it unfortunately seems far too complicated/time-consuming for us to attempt to support both the old and modern module formats, hence the alternative would be to do "nothing" here and just keep our "old" builds.[4]
---
[1] The final blocker was module support in workers in Firefox, which was implemented in Firefox 114; please see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/import#browser_compatibility
[2] It's probably possible to further improve/simplify especially the Node.js-specific code, but it does appear to work as-is.
[3] Having partially "broken" patches, that fail tests, as part of the commit history is *really not* a good idea in general.
[4] Outputting JavaScript modules was first requested almost five years ago, see issue 10317, and nowadays there *should* be much better support for JavaScript modules in various tools.
2023-09-28 20:00:10 +09:00
|
|
|
const WORKER_SRC = "../build/generic/build/pdf.worker.mjs";
|
2020-06-29 03:14:03 +09:00
|
|
|
const RENDER_TASK_ON_CONTINUE_DELAY = 5; // ms
|
2021-06-07 22:20:29 +09:00
|
|
|
const SVG_NS = "http://www.w3.org/2000/svg";
|
2015-05-15 22:54:48 +09:00
|
|
|
|
2022-02-09 00:59:18 +09:00
|
|
|
const md5FileMap = new Map();
|
|
|
|
|
2021-06-07 22:20:29 +09:00
|
|
|
function loadStyles(styles) {
|
2021-12-05 22:57:01 +09:00
|
|
|
const promises = [];
|
2011-11-16 07:43:05 +09:00
|
|
|
|
2021-12-05 22:57:01 +09:00
|
|
|
for (const file of styles) {
|
|
|
|
promises.push(
|
2022-03-06 21:57:42 +09:00
|
|
|
fetch(file)
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.text();
|
|
|
|
})
|
|
|
|
.catch(reason => {
|
|
|
|
throw new Error(`Error fetching style (${file}): ${reason}`);
|
|
|
|
})
|
2021-12-05 22:57:01 +09:00
|
|
|
);
|
2021-06-07 22:20:29 +09:00
|
|
|
}
|
|
|
|
|
2021-12-05 22:57:01 +09:00
|
|
|
return Promise.all(promises);
|
2021-06-07 22:20:29 +09:00
|
|
|
}
|
|
|
|
|
2023-07-20 03:14:18 +09:00
|
|
|
function loadImage(svg_xml, ctx) {
|
2021-12-05 22:04:23 +09:00
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const img = new Image();
|
|
|
|
img.src = "data:image/svg+xml;base64," + btoa(svg_xml);
|
|
|
|
img.onload = function () {
|
2023-07-20 03:14:18 +09:00
|
|
|
ctx?.drawImage(img, 0, 0);
|
2021-12-05 22:04:23 +09:00
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
img.onerror = function (e) {
|
|
|
|
reject(new Error(`Error rasterizing SVG: ${e}`));
|
|
|
|
};
|
|
|
|
});
|
2021-06-07 22:20:29 +09:00
|
|
|
}
|
|
|
|
|
2023-07-20 03:14:18 +09:00
|
|
|
async function writeSVG(svgElement, ctx) {
|
|
|
|
// We need to have UTF-8 encoded XML.
|
|
|
|
const svg_xml = unescape(
|
|
|
|
encodeURIComponent(new XMLSerializer().serializeToString(svgElement))
|
|
|
|
);
|
|
|
|
if (svg_xml.includes("background-image: url("data:image")) {
|
|
|
|
// Workaround for https://bugzilla.mozilla.org/show_bug.cgi?id=1844414
|
|
|
|
// we load the image two times.
|
|
|
|
await loadImage(svg_xml, null);
|
2023-05-30 19:36:35 +09:00
|
|
|
await new Promise(resolve => {
|
|
|
|
setTimeout(resolve, 10);
|
|
|
|
});
|
2023-07-20 03:14:18 +09:00
|
|
|
}
|
|
|
|
return loadImage(svg_xml, ctx);
|
|
|
|
}
|
|
|
|
|
2022-03-09 18:55:27 +09:00
|
|
|
async function inlineImages(node, silentErrors = false) {
|
|
|
|
const promises = [];
|
|
|
|
|
|
|
|
for (const image of node.getElementsByTagName("img")) {
|
|
|
|
const url = image.src;
|
|
|
|
|
|
|
|
promises.push(
|
|
|
|
fetch(url)
|
|
|
|
.then(response => {
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.statusText);
|
|
|
|
}
|
|
|
|
return response.blob();
|
|
|
|
})
|
|
|
|
.then(blob => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
const reader = new FileReader();
|
|
|
|
reader.onload = () => {
|
|
|
|
resolve(reader.result);
|
|
|
|
};
|
|
|
|
reader.onerror = reject;
|
|
|
|
|
|
|
|
reader.readAsDataURL(blob);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.then(dataUrl => {
|
|
|
|
return new Promise((resolve, reject) => {
|
|
|
|
image.onload = resolve;
|
|
|
|
image.onerror = evt => {
|
|
|
|
if (silentErrors) {
|
|
|
|
resolve();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
reject(evt);
|
|
|
|
};
|
|
|
|
|
|
|
|
image.src = dataUrl;
|
|
|
|
});
|
|
|
|
})
|
|
|
|
.catch(reason => {
|
|
|
|
throw new Error(`Error inlining image (${url}): ${reason}`);
|
|
|
|
})
|
2021-06-28 00:17:44 +09:00
|
|
|
);
|
|
|
|
}
|
2022-03-09 18:55:27 +09:00
|
|
|
|
|
|
|
await Promise.all(promises);
|
2021-06-28 00:17:44 +09:00
|
|
|
}
|
|
|
|
|
2022-02-08 07:40:21 +09:00
|
|
|
async function convertCanvasesToImages(annotationCanvasMap, outputScale) {
|
2021-11-07 02:36:49 +09:00
|
|
|
const results = new Map();
|
|
|
|
const promises = [];
|
|
|
|
for (const [key, canvas] of annotationCanvasMap) {
|
|
|
|
promises.push(
|
|
|
|
new Promise(resolve => {
|
|
|
|
canvas.toBlob(blob => {
|
|
|
|
const image = document.createElement("img");
|
2023-03-05 21:57:27 +09:00
|
|
|
image.classList.add("wasCanvas");
|
2022-02-08 07:40:21 +09:00
|
|
|
image.onload = function () {
|
|
|
|
image.style.width = Math.floor(image.width / outputScale) + "px";
|
|
|
|
resolve();
|
|
|
|
};
|
2021-11-07 02:36:49 +09:00
|
|
|
results.set(key, image);
|
|
|
|
image.src = URL.createObjectURL(blob);
|
|
|
|
});
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
|
|
|
await Promise.all(promises);
|
|
|
|
return results;
|
|
|
|
}
|
|
|
|
|
2021-12-05 22:57:01 +09:00
|
|
|
class Rasterize {
|
2017-09-11 01:10:43 +09:00
|
|
|
/**
|
2021-12-05 22:57:01 +09:00
|
|
|
* For the reference tests, the full content of the various layers 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.
|
2017-09-11 01:10:43 +09:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
2021-12-05 22:57:01 +09:00
|
|
|
static get annotationStylePromise() {
|
2022-06-25 16:52:32 +09:00
|
|
|
const styles = [VIEWER_CSS, "./annotation_layer_builder_overrides.css"];
|
2021-12-05 22:57:01 +09:00
|
|
|
return shadow(this, "annotationStylePromise", loadStyles(styles));
|
|
|
|
}
|
|
|
|
|
|
|
|
static get textStylePromise() {
|
2022-11-22 01:15:39 +09:00
|
|
|
const styles = [VIEWER_CSS, "./text_layer_test.css"];
|
2021-12-05 22:57:01 +09:00
|
|
|
return shadow(this, "textStylePromise", loadStyles(styles));
|
|
|
|
}
|
|
|
|
|
|
|
|
static get xfaStylePromise() {
|
2022-06-25 16:52:32 +09:00
|
|
|
const styles = [VIEWER_CSS, "./xfa_layer_builder_overrides.css"];
|
2021-12-05 22:57:01 +09:00
|
|
|
return shadow(this, "xfaStylePromise", loadStyles(styles));
|
|
|
|
}
|
|
|
|
|
2021-12-05 23:15:31 +09:00
|
|
|
static createContainer(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");
|
2022-06-12 19:20:25 +09:00
|
|
|
foreignObject.append(style);
|
2021-12-05 23:15:31 +09:00
|
|
|
|
|
|
|
const div = document.createElement("div");
|
2022-06-12 19:20:25 +09:00
|
|
|
foreignObject.append(div);
|
2021-12-05 23:15:31 +09:00
|
|
|
|
|
|
|
return { svg, foreignObject, style, div };
|
|
|
|
}
|
|
|
|
|
2021-12-05 22:57:01 +09:00
|
|
|
static async annotationLayer(
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
ctx,
|
|
|
|
viewport,
|
2022-02-08 07:40:21 +09:00
|
|
|
outputScale,
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
annotations,
|
2021-11-07 02:36:49 +09:00
|
|
|
annotationCanvasMap,
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
page,
|
|
|
|
imageResourcesPath,
|
2022-11-19 07:08:11 +09:00
|
|
|
renderForms = false,
|
|
|
|
l10n = NullL10n
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
) {
|
2021-12-05 22:04:23 +09:00
|
|
|
try {
|
2021-12-05 23:15:31 +09:00
|
|
|
const { svg, foreignObject, style, div } = this.createContainer(viewport);
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
div.className = "annotationLayer";
|
2015-12-19 06:29:22 +09:00
|
|
|
|
2021-12-05 22:57:01 +09:00
|
|
|
const [common, overrides] = await this.annotationStylePromise;
|
2022-06-25 16:52:32 +09:00
|
|
|
style.textContent =
|
|
|
|
`${common}\n${overrides}\n` +
|
|
|
|
`:root { --scale-factor: ${viewport.scale} }`;
|
2015-12-19 06:29:22 +09:00
|
|
|
|
2021-12-05 23:15:31 +09:00
|
|
|
const annotationViewport = viewport.clone({ dontFlip: true });
|
2021-12-05 22:04:23 +09:00
|
|
|
const annotationImageMap = await convertCanvasesToImages(
|
2022-02-08 07:40:21 +09:00
|
|
|
annotationCanvasMap,
|
|
|
|
outputScale
|
2021-12-05 22:04:23 +09:00
|
|
|
);
|
2021-03-12 22:00:12 +09:00
|
|
|
|
2021-12-05 23:15:31 +09:00
|
|
|
// Rendering annotation layer as HTML.
|
2021-12-05 22:18:06 +09:00
|
|
|
const parameters = {
|
2021-12-05 22:04:23 +09:00
|
|
|
annotations,
|
|
|
|
linkService: new SimpleLinkService(),
|
|
|
|
imageResourcesPath,
|
|
|
|
renderForms,
|
|
|
|
};
|
2023-10-13 23:23:17 +09:00
|
|
|
|
|
|
|
// Ensure that the annotationLayer gets translated.
|
|
|
|
document.l10n.connectRoot(div);
|
|
|
|
|
2023-06-02 03:31:05 +09:00
|
|
|
const annotationLayer = new AnnotationLayer({
|
|
|
|
div,
|
|
|
|
annotationCanvasMap: annotationImageMap,
|
2023-06-15 18:59:59 +09:00
|
|
|
page,
|
|
|
|
l10n,
|
|
|
|
viewport: annotationViewport,
|
2023-06-02 03:31:05 +09:00
|
|
|
});
|
2023-06-20 23:46:51 +09:00
|
|
|
await annotationLayer.render(parameters);
|
2023-06-15 18:59:59 +09:00
|
|
|
await annotationLayer.showPopups();
|
2021-12-05 22:04:23 +09:00
|
|
|
|
2023-10-13 23:23:17 +09:00
|
|
|
// With Fluent, the translations are triggered by the MutationObserver
|
|
|
|
// hence the translations could be not finished when we rasterize the div.
|
|
|
|
// So in order to be sure that all translations are done, we wait for
|
|
|
|
// them here.
|
|
|
|
await document.l10n.translateRoots();
|
|
|
|
|
|
|
|
// All translation should be complete here.
|
|
|
|
document.l10n.disconnectRoot(div);
|
|
|
|
|
2021-12-05 22:04:23 +09:00
|
|
|
// Inline SVG images from text annotations.
|
2022-03-09 18:55:27 +09:00
|
|
|
await inlineImages(div);
|
2022-06-12 19:20:25 +09:00
|
|
|
foreignObject.append(div);
|
|
|
|
svg.append(foreignObject);
|
2021-12-05 22:04:23 +09:00
|
|
|
|
|
|
|
await writeSVG(svg, ctx);
|
|
|
|
} catch (reason) {
|
2021-12-05 22:57:01 +09:00
|
|
|
throw new Error(`Rasterize.annotationLayer: "${reason?.message}".`);
|
2021-12-05 22:04:23 +09:00
|
|
|
}
|
2015-12-19 06:29:22 +09:00
|
|
|
}
|
|
|
|
|
2022-01-19 02:09:12 +09:00
|
|
|
static async textLayer(ctx, viewport, textContent) {
|
2021-12-05 22:57:01 +09:00
|
|
|
try {
|
2021-12-05 23:15:31 +09:00
|
|
|
const { svg, foreignObject, style, div } = this.createContainer(viewport);
|
2021-12-05 22:57:01 +09:00
|
|
|
div.className = "textLayer";
|
2021-12-05 23:15:31 +09:00
|
|
|
|
|
|
|
// Items are transformed to have 1px font size.
|
|
|
|
svg.setAttribute("font-size", 1);
|
2021-12-05 22:57:01 +09:00
|
|
|
|
2022-11-22 01:15:39 +09:00
|
|
|
const [common, overrides] = await this.textStylePromise;
|
|
|
|
style.textContent =
|
|
|
|
`${common}\n${overrides}\n` +
|
|
|
|
`:root { --scale-factor: ${viewport.scale} }`;
|
2021-12-05 22:57:01 +09:00
|
|
|
|
|
|
|
// Rendering text layer as HTML.
|
|
|
|
const task = renderTextLayer({
|
2022-12-05 01:42:24 +09:00
|
|
|
textContentSource: textContent,
|
2021-12-05 22:57:01 +09:00
|
|
|
container: div,
|
|
|
|
viewport,
|
|
|
|
});
|
2022-11-22 01:15:39 +09:00
|
|
|
|
2021-12-05 22:57:01 +09:00
|
|
|
await task.promise;
|
2022-06-12 19:20:25 +09:00
|
|
|
svg.append(foreignObject);
|
2021-12-05 22:57:01 +09:00
|
|
|
|
|
|
|
await writeSVG(svg, ctx);
|
|
|
|
} catch (reason) {
|
|
|
|
throw new Error(`Rasterize.textLayer: "${reason?.message}".`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static async xfaLayer(
|
2021-07-16 16:18:53 +09:00
|
|
|
ctx,
|
|
|
|
viewport,
|
2021-12-15 07:59:17 +09:00
|
|
|
xfaHtml,
|
2021-07-16 16:18:53 +09:00
|
|
|
fontRules,
|
|
|
|
annotationStorage,
|
|
|
|
isPrint
|
|
|
|
) {
|
2021-12-05 22:04:23 +09:00
|
|
|
try {
|
2021-12-05 23:15:31 +09:00
|
|
|
const { svg, foreignObject, style, div } = this.createContainer(viewport);
|
2021-06-07 22:20:29 +09:00
|
|
|
|
2021-12-05 22:57:01 +09:00
|
|
|
const [common, overrides] = await this.xfaStylePromise;
|
2022-06-25 16:52:32 +09:00
|
|
|
style.textContent = `${common}\n${overrides}\n${fontRules}`;
|
2021-12-05 22:04:23 +09:00
|
|
|
|
2021-12-05 23:15:31 +09:00
|
|
|
// Rendering XFA layer as HTML.
|
2021-12-05 22:04:23 +09:00
|
|
|
XfaLayer.render({
|
|
|
|
viewport: viewport.clone({ dontFlip: true }),
|
2021-12-15 07:59:17 +09:00
|
|
|
div,
|
|
|
|
xfaHtml,
|
2021-12-05 22:04:23 +09:00
|
|
|
annotationStorage,
|
|
|
|
linkService: new SimpleLinkService(),
|
|
|
|
intent: isPrint ? "print" : "display",
|
|
|
|
});
|
|
|
|
|
2021-12-05 23:15:31 +09:00
|
|
|
// Some unsupported type of images (e.g. tiff) lead to errors.
|
2022-03-09 18:55:27 +09:00
|
|
|
await inlineImages(div, /* silentErrors = */ true);
|
2022-06-12 19:20:25 +09:00
|
|
|
svg.append(foreignObject);
|
2021-12-05 22:04:23 +09:00
|
|
|
|
|
|
|
await writeSVG(svg, ctx);
|
|
|
|
} catch (reason) {
|
2021-12-05 22:57:01 +09:00
|
|
|
throw new Error(`Rasterize.xfaLayer: "${reason?.message}".`);
|
2021-12-05 22:04:23 +09:00
|
|
|
}
|
2021-06-07 22:20:29 +09:00
|
|
|
}
|
2021-12-05 22:57:01 +09:00
|
|
|
}
|
2021-06-07 22:20:29 +09:00
|
|
|
|
2015-05-15 22:29:43 +09:00
|
|
|
/**
|
|
|
|
* @typedef {Object} DriverOptions
|
|
|
|
* @property {HTMLSpanElement} inflight - Field displaying the number of
|
|
|
|
* inflight requests.
|
2015-05-16 02:58:34 +09:00
|
|
|
* @property {HTMLInputElement} disableScrolling - Checkbox to disable
|
|
|
|
* automatic scrolling of the output container.
|
|
|
|
* @property {HTMLPreElement} output - Container for all output messages.
|
2015-05-15 22:29:43 +09:00
|
|
|
* @property {HTMLDivElement} end - Container for a completion message.
|
|
|
|
*/
|
2011-07-05 06:17:23 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
class Driver {
|
2015-05-15 22:29:43 +09:00
|
|
|
/**
|
|
|
|
* @param {DriverOptions} options
|
|
|
|
*/
|
2021-12-04 23:21:04 +09:00
|
|
|
constructor(options) {
|
2018-02-14 22:49:24 +09:00
|
|
|
// Configure the global worker options.
|
[api-minor] Introduce a new `annotationMode`-option, in `PDFPageProxy.{render, getOperatorList}`
*This is a follow-up to PRs 13867 and 13899.*
This patch is tagged `api-minor` for the following reasons:
- It replaces the `renderInteractiveForms`/`includeAnnotationStorage`-options, in the `PDFPageProxy.render`-method, with the single `annotationMode`-option that controls which annotations are being rendered and how. Note that the old options were mutually exclusive, and setting both to `true` would result in undefined behaviour.
- For improved consistency in the API, the `annotationMode`-option will also work together with the `PDFPageProxy.getOperatorList`-method.
- It's now also possible to disable *all* annotation rendering in both the API and the Viewer, since the other changes meant that this could now be supported with a single added line on the worker-thread[1]; fixes 7282.
---
[1] Please note that in order to simplify the overall implementation, we'll purposely only support disabling of *all* annotations and that the option is being shared between the API and the Viewer. For any more "specialized" use-cases, where e.g. only some annotation-types are being rendered and/or the API and Viewer render different sets of annotations, that'll have to be handled in third-party implementations/forks of the PDF.js code-base.
2021-08-08 21:36:28 +09:00
|
|
|
GlobalWorkerOptions.workerSrc = WORKER_SRC;
|
2018-02-14 22:49:24 +09:00
|
|
|
|
2022-11-19 07:08:11 +09:00
|
|
|
this._l10n = new GenericL10n(VIEWER_LOCALE);
|
|
|
|
|
2015-05-15 22:29:43 +09:00
|
|
|
// Set the passed options
|
|
|
|
this.inflight = options.inflight;
|
2015-05-16 02:58:34 +09:00
|
|
|
this.disableScrolling = options.disableScrolling;
|
|
|
|
this.output = options.output;
|
2015-05-15 22:29:43 +09:00
|
|
|
this.end = options.end;
|
|
|
|
|
|
|
|
// Set parameters from the query string
|
2022-02-19 17:41:04 +09:00
|
|
|
const params = parseQueryString(window.location.search.substring(1));
|
|
|
|
this.browser = params.get("browser");
|
|
|
|
this.manifestFile = params.get("manifestfile");
|
|
|
|
this.delay = params.get("delay") | 0;
|
2015-05-15 22:29:43 +09:00
|
|
|
this.inFlightRequests = 0;
|
2022-02-19 17:41:04 +09:00
|
|
|
this.testFilter = JSON.parse(params.get("testfilter") || "[]");
|
|
|
|
this.xfaOnly = params.get("xfaonly") === "true";
|
2015-05-15 22:29:43 +09:00
|
|
|
|
|
|
|
// Create a working canvas
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
this.canvas = document.createElement("canvas");
|
2013-01-31 04:31:08 +09:00
|
|
|
}
|
2014-04-10 08:44:07 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
run() {
|
2021-12-04 23:45:47 +09:00
|
|
|
window.onerror = (message, source, line, column, error) => {
|
|
|
|
this._info(
|
2021-12-04 23:21:04 +09:00
|
|
|
"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 + '"... ');
|
|
|
|
|
|
|
|
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.
|
2022-03-06 20:47:12 +09:00
|
|
|
setTimeout(async () => {
|
|
|
|
const response = await fetch(this.manifestFile);
|
|
|
|
if (!response.ok) {
|
|
|
|
throw new Error(response.statusText);
|
|
|
|
}
|
|
|
|
this._log("done\n");
|
|
|
|
this.manifest = await response.json();
|
|
|
|
|
|
|
|
if (this.testFilter?.length || this.xfaOnly) {
|
|
|
|
this.manifest = this.manifest.filter(item => {
|
|
|
|
if (this.testFilter.includes(item.id)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
if (this.xfaOnly && item.enableXfa) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
this.currentTask = 0;
|
|
|
|
this._nextTask();
|
2021-12-04 23:21:04 +09:00
|
|
|
}, this.delay);
|
|
|
|
}
|
2021-11-04 03:57:48 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
/**
|
|
|
|
* A debugging tool to log to the terminal while tests are running.
|
|
|
|
* XXX: This isn't currently referenced, but it's useful for debugging so
|
|
|
|
* do not remove it.
|
|
|
|
*
|
|
|
|
* @param {string} msg - The message to log, it will be prepended with the
|
|
|
|
* current PDF ID if there is one.
|
|
|
|
*/
|
|
|
|
log(msg) {
|
|
|
|
let id = this.browser;
|
|
|
|
const task = this.manifest[this.currentTask];
|
|
|
|
if (task) {
|
|
|
|
id += `-${task.id}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
this._info(`${id}: ${msg}`);
|
|
|
|
}
|
2021-11-04 03:57:48 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_nextTask() {
|
|
|
|
let failure = "";
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
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;
|
|
|
|
|
2022-02-09 00:59:18 +09:00
|
|
|
const prevFile = md5FileMap.get(task.md5);
|
|
|
|
if (prevFile) {
|
|
|
|
if (task.file !== prevFile) {
|
|
|
|
this._nextPage(
|
|
|
|
task,
|
|
|
|
`The "${task.file}" file is identical to the previously used "${prevFile}" file.`
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
md5FileMap.set(task.md5, task.file);
|
|
|
|
}
|
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
// 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.');
|
2017-05-11 19:54:48 +09:00
|
|
|
return;
|
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
this.currentTask++;
|
|
|
|
this._nextTask();
|
|
|
|
return;
|
|
|
|
}
|
2021-03-30 20:24:04 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
this._log('Loading file "' + task.file + '"\n');
|
|
|
|
|
|
|
|
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]
|
2022-06-12 19:20:25 +09:00
|
|
|
.append(xfaStyleElement);
|
2021-03-30 20:24:04 +09:00
|
|
|
}
|
[api-minor] Extend general transfer function support to browsers without `OffscreenCanvas`
This patch extends PR 16115 to work in all browsers, regardless of their `OffscreenCanvas` support, such that transfer functions will be applied to general rendering (and not just image data).
In order to do this we introduce the `BaseFilterFactory` that is then extended in browsers/Node.js environments, similar to all the other factories used in the API, such that we always have the necessary factory available in `src/display/canvas.js`.
These changes help simplify the existing `putBinaryImageData` function, and the new method can easily be stubbed-out in the Firefox PDF Viewer.
*Please note:* This patch removes the old *partial* transfer function support, which only applied to image data, from Node.js environments since the `node-canvas` package currently doesn't support filters. However, this should hopefully be fine given that:
- Transfer functions are not very commonly used in PDF documents.
- Browsers in general, and Firefox in particular, are the *primary* development target for the PDF.js library.
- The FAQ only lists Node.js as *mostly* supported, see https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
2023-03-12 23:47:01 +09:00
|
|
|
const isOffscreenCanvasSupported =
|
|
|
|
task.isOffscreenCanvasSupported === false ? false : undefined;
|
2021-03-30 20:24:04 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
const loadingTask = getDocument({
|
2023-02-13 22:38:22 +09:00
|
|
|
url: new URL(task.file, window.location),
|
2021-12-04 23:21:04 +09:00
|
|
|
password: task.password,
|
|
|
|
cMapUrl: CMAP_URL,
|
|
|
|
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
|
|
|
disableAutoFetch: !task.enableAutoFetch,
|
|
|
|
pdfBug: true,
|
|
|
|
useSystemFonts: task.useSystemFonts,
|
|
|
|
useWorkerFetch: task.useWorkerFetch,
|
|
|
|
enableXfa: task.enableXfa,
|
[api-minor] Extend general transfer function support to browsers without `OffscreenCanvas`
This patch extends PR 16115 to work in all browsers, regardless of their `OffscreenCanvas` support, such that transfer functions will be applied to general rendering (and not just image data).
In order to do this we introduce the `BaseFilterFactory` that is then extended in browsers/Node.js environments, similar to all the other factories used in the API, such that we always have the necessary factory available in `src/display/canvas.js`.
These changes help simplify the existing `putBinaryImageData` function, and the new method can easily be stubbed-out in the Firefox PDF Viewer.
*Please note:* This patch removes the old *partial* transfer function support, which only applied to image data, from Node.js environments since the `node-canvas` package currently doesn't support filters. However, this should hopefully be fine given that:
- Transfer functions are not very commonly used in PDF documents.
- Browsers in general, and Firefox in particular, are the *primary* development target for the PDF.js library.
- The FAQ only lists Node.js as *mostly* supported, see https://github.com/mozilla/pdf.js/wiki/Frequently-Asked-Questions#faq-support
2023-03-12 23:47:01 +09:00
|
|
|
isOffscreenCanvasSupported,
|
2021-12-04 23:21:04 +09:00
|
|
|
styleElement: xfaStyleElement,
|
|
|
|
});
|
2022-10-19 00:07:47 +09:00
|
|
|
let promise = loadingTask.promise;
|
|
|
|
|
2023-06-23 02:48:40 +09:00
|
|
|
if (task.annotationStorage) {
|
|
|
|
for (const annotation of Object.values(task.annotationStorage)) {
|
2023-07-18 22:39:18 +09:00
|
|
|
const { bitmapName } = annotation;
|
|
|
|
if (bitmapName) {
|
2023-06-23 02:48:40 +09:00
|
|
|
promise = promise.then(async doc => {
|
|
|
|
const response = await fetch(
|
2023-07-18 22:39:18 +09:00
|
|
|
new URL(`./images/${bitmapName}`, window.location)
|
2023-06-23 02:48:40 +09:00
|
|
|
);
|
|
|
|
const blob = await response.blob();
|
2023-07-18 22:39:18 +09:00
|
|
|
if (bitmapName.endsWith(".svg")) {
|
|
|
|
const image = new Image();
|
|
|
|
const url = URL.createObjectURL(blob);
|
|
|
|
const imagePromise = new Promise((resolve, reject) => {
|
|
|
|
image.onload = () => {
|
|
|
|
const canvas = new OffscreenCanvas(
|
|
|
|
image.width,
|
|
|
|
image.height
|
|
|
|
);
|
|
|
|
const ctx = canvas.getContext("2d");
|
|
|
|
ctx.drawImage(image, 0, 0);
|
|
|
|
annotation.bitmap = canvas.transferToImageBitmap();
|
|
|
|
URL.revokeObjectURL(url);
|
|
|
|
resolve();
|
|
|
|
};
|
|
|
|
image.onerror = reject;
|
|
|
|
});
|
|
|
|
image.src = url;
|
|
|
|
await imagePromise;
|
|
|
|
} else {
|
|
|
|
annotation.bitmap = await createImageBitmap(blob);
|
|
|
|
}
|
2023-06-23 02:48:40 +09:00
|
|
|
|
|
|
|
return doc;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-19 00:07:47 +09:00
|
|
|
if (task.save) {
|
2023-06-23 02:48:40 +09:00
|
|
|
promise = promise.then(async doc => {
|
2023-02-13 17:56:08 +09:00
|
|
|
if (!task.annotationStorage) {
|
|
|
|
throw new Error("Missing `annotationStorage` entry.");
|
|
|
|
}
|
|
|
|
doc.annotationStorage.setAll(task.annotationStorage);
|
2022-10-19 00:07:47 +09:00
|
|
|
|
2023-02-13 17:56:08 +09:00
|
|
|
const data = await doc.saveDocument();
|
|
|
|
await loadingTask.destroy();
|
|
|
|
delete task.annotationStorage;
|
|
|
|
|
|
|
|
return getDocument(data).promise;
|
|
|
|
});
|
2022-10-19 00:07:47 +09:00
|
|
|
}
|
|
|
|
|
|
|
|
promise.then(
|
2021-12-04 23:21:04 +09:00
|
|
|
async doc => {
|
|
|
|
if (task.enableXfa) {
|
|
|
|
task.fontRules = "";
|
|
|
|
for (const rule of xfaStyleElement.sheet.cssRules) {
|
|
|
|
task.fontRules += rule.cssText + "\n";
|
2021-06-07 22:20:29 +09:00
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2021-06-07 22:20:29 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
task.pdfDoc = doc;
|
|
|
|
task.optionalContentConfigPromise = doc.getOptionalContentConfig();
|
2020-08-07 03:51:57 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
if (task.optionalContent) {
|
|
|
|
const entries = Object.entries(task.optionalContent),
|
|
|
|
optionalContentConfig = await task.optionalContentConfigPromise;
|
|
|
|
for (const [id, visible] of entries) {
|
|
|
|
optionalContentConfig.setVisibility(id, visible);
|
2021-08-24 18:48:54 +09:00
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
}
|
2011-12-08 13:07:34 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
this._nextPage(task, failure);
|
|
|
|
},
|
|
|
|
err => {
|
|
|
|
failure = "Loading PDF document: " + err;
|
|
|
|
this._nextPage(task, failure);
|
|
|
|
}
|
|
|
|
);
|
|
|
|
return;
|
|
|
|
} catch (e) {
|
|
|
|
failure = "Loading PDF document: " + this._exceptionToString(e);
|
2015-05-15 22:29:43 +09:00
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
this._nextPage(task, failure);
|
|
|
|
});
|
|
|
|
}
|
2011-09-24 23:44:50 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_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);
|
2015-05-15 22:29:43 +09:00
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
styleSheet.ownerNode.remove();
|
|
|
|
}
|
|
|
|
const body = document.body;
|
|
|
|
while (body.lastChild !== this.end) {
|
|
|
|
body.lastChild.remove();
|
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
2015-05-15 22:29:43 +09:00
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
|
|
|
return Promise.all(destroyedPromises);
|
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_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] : "");
|
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_getLastPageNumber(task) {
|
|
|
|
if (!task.pdfDoc) {
|
|
|
|
return task.firstPage || 1;
|
|
|
|
}
|
2022-10-20 21:15:58 +09:00
|
|
|
return task.lastPage || task.pdfDoc.numPages;
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2012-09-16 23:46:29 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_nextPage(task, loadError) {
|
2021-12-05 22:18:06 +09:00
|
|
|
let failure = loadError || "";
|
|
|
|
let ctx;
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
if (!task.pdfDoc) {
|
2021-12-05 22:18:06 +09:00
|
|
|
const dataUrl = this.canvas.toDataURL("image/png");
|
2022-03-07 22:54:59 +09:00
|
|
|
this._sendResult(dataUrl, task, failure).then(() => {
|
2021-12-04 23:45:47 +09:00
|
|
|
this._log(
|
2021-12-04 23:21:04 +09:00
|
|
|
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
);
|
2021-12-04 23:45:47 +09:00
|
|
|
this.currentTask++;
|
|
|
|
this._nextTask();
|
2021-12-04 23:21:04 +09:00
|
|
|
});
|
|
|
|
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();
|
2015-05-15 22:29:43 +09:00
|
|
|
return;
|
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
if (task.skipPages && task.skipPages.includes(task.pageNum)) {
|
|
|
|
this._log(
|
|
|
|
" Skipping page " + task.pageNum + "/" + task.pdfDoc.numPages + "...\n"
|
|
|
|
);
|
|
|
|
task.pageNum++;
|
|
|
|
this._nextPage(task);
|
|
|
|
return;
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
if (!failure) {
|
|
|
|
try {
|
|
|
|
this._log(
|
|
|
|
" Loading page " + task.pageNum + "/" + task.pdfDoc.numPages + "... "
|
|
|
|
);
|
|
|
|
ctx = this.canvas.getContext("2d", { alpha: false });
|
|
|
|
task.pdfDoc.getPage(task.pageNum).then(
|
2021-12-04 23:45:47 +09:00
|
|
|
page => {
|
2022-02-08 07:40:21 +09:00
|
|
|
// Default to creating the test images at the devices pixel ratio,
|
|
|
|
// unless the test explicitly specifies an output scale.
|
|
|
|
const outputScale = task.outputScale || window.devicePixelRatio;
|
|
|
|
let viewport = page.getViewport({
|
2021-12-04 23:21:04 +09:00
|
|
|
scale: PixelsPerInch.PDF_TO_CSS_UNITS,
|
|
|
|
});
|
2023-03-05 21:57:27 +09:00
|
|
|
if (task.rotation) {
|
|
|
|
viewport = viewport.clone({ rotation: task.rotation });
|
|
|
|
}
|
2022-02-08 07:40:21 +09:00
|
|
|
// Restrict the test from creating a canvas that is too big.
|
|
|
|
const MAX_CANVAS_PIXEL_DIMENSION = 4096;
|
|
|
|
const largestDimension = Math.max(viewport.width, viewport.height);
|
|
|
|
if (
|
|
|
|
Math.floor(largestDimension * outputScale) >
|
|
|
|
MAX_CANVAS_PIXEL_DIMENSION
|
|
|
|
) {
|
|
|
|
const rescale = MAX_CANVAS_PIXEL_DIMENSION / largestDimension;
|
|
|
|
viewport = viewport.clone({
|
|
|
|
scale: PixelsPerInch.PDF_TO_CSS_UNITS * rescale,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
const pixelWidth = Math.floor(viewport.width * outputScale);
|
|
|
|
const pixelHeight = Math.floor(viewport.height * outputScale);
|
|
|
|
task.viewportWidth = Math.floor(viewport.width);
|
|
|
|
task.viewportHeight = Math.floor(viewport.height);
|
|
|
|
task.outputScale = outputScale;
|
|
|
|
this.canvas.width = pixelWidth;
|
|
|
|
this.canvas.height = pixelHeight;
|
|
|
|
this.canvas.style.width = Math.floor(viewport.width) + "px";
|
|
|
|
this.canvas.style.height = Math.floor(viewport.height) + "px";
|
2021-12-04 23:45:47 +09:00
|
|
|
this._clearCanvas();
|
2021-12-04 23:21:04 +09:00
|
|
|
|
2022-02-08 07:40:21 +09:00
|
|
|
const transform =
|
|
|
|
outputScale !== 1 ? [outputScale, 0, 0, outputScale, 0, 0] : null;
|
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
// Initialize various `eq` test subtypes, see comment below.
|
2021-12-05 22:18:06 +09:00
|
|
|
let renderAnnotations = false,
|
2021-12-04 23:21:04 +09:00
|
|
|
renderForms = false,
|
|
|
|
renderPrint = false,
|
|
|
|
renderXfa = false,
|
2022-05-04 22:37:13 +09:00
|
|
|
annotationCanvasMap = null,
|
|
|
|
pageColors = null;
|
2021-12-04 23:21:04 +09:00
|
|
|
|
|
|
|
if (task.annotationStorage) {
|
2023-02-13 17:56:08 +09:00
|
|
|
task.pdfDoc.annotationStorage.setAll(task.annotationStorage);
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2021-07-16 16:18:53 +09:00
|
|
|
|
2021-12-05 22:18:06 +09:00
|
|
|
let textLayerCanvas, annotationLayerCanvas, annotationLayerContext;
|
|
|
|
let initPromise;
|
2021-12-04 23:21:04 +09:00
|
|
|
if (task.type === "text") {
|
|
|
|
// Using a dummy canvas for PDF context drawing operations
|
2021-12-04 23:45:47 +09:00
|
|
|
textLayerCanvas = this.textLayerCanvas;
|
2021-12-04 23:21:04 +09:00
|
|
|
if (!textLayerCanvas) {
|
|
|
|
textLayerCanvas = document.createElement("canvas");
|
2021-12-04 23:45:47 +09:00
|
|
|
this.textLayerCanvas = textLayerCanvas;
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2022-02-08 07:40:21 +09:00
|
|
|
textLayerCanvas.width = pixelWidth;
|
|
|
|
textLayerCanvas.height = pixelHeight;
|
2021-12-05 22:18:06 +09:00
|
|
|
const textLayerContext = textLayerCanvas.getContext("2d");
|
2021-12-04 23:21:04 +09:00
|
|
|
textLayerContext.clearRect(
|
|
|
|
0,
|
|
|
|
0,
|
|
|
|
textLayerCanvas.width,
|
|
|
|
textLayerCanvas.height
|
|
|
|
);
|
2022-02-08 07:40:21 +09:00
|
|
|
textLayerContext.scale(outputScale, outputScale);
|
2021-12-04 23:21:04 +09:00
|
|
|
// The text builder will draw its content on the test canvas
|
|
|
|
initPromise = page
|
|
|
|
.getTextContent({
|
|
|
|
includeMarkedContent: true,
|
2023-03-23 18:15:14 +09:00
|
|
|
disableNormalization: true,
|
2021-12-04 23:21:04 +09:00
|
|
|
})
|
|
|
|
.then(function (textContent) {
|
2021-12-05 22:57:01 +09:00
|
|
|
return Rasterize.textLayer(
|
2021-12-04 23:21:04 +09:00
|
|
|
textLayerContext,
|
|
|
|
viewport,
|
2022-01-19 02:09:12 +09:00
|
|
|
textContent
|
2021-12-04 23:21:04 +09:00
|
|
|
);
|
|
|
|
});
|
|
|
|
} 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;
|
2022-05-04 22:37:13 +09:00
|
|
|
pageColors = task.pageColors || null;
|
2021-12-04 23:21:04 +09:00
|
|
|
|
|
|
|
// Render the annotation layer if necessary.
|
|
|
|
if (renderAnnotations || renderForms || renderXfa) {
|
|
|
|
// Create a dummy canvas for the drawing operations.
|
2021-12-04 23:45:47 +09:00
|
|
|
annotationLayerCanvas = this.annotationLayerCanvas;
|
2021-12-04 23:21:04 +09:00
|
|
|
if (!annotationLayerCanvas) {
|
|
|
|
annotationLayerCanvas = document.createElement("canvas");
|
2021-12-04 23:45:47 +09:00
|
|
|
this.annotationLayerCanvas = annotationLayerCanvas;
|
2015-12-19 06:29:22 +09:00
|
|
|
}
|
2022-02-08 07:40:21 +09:00
|
|
|
annotationLayerCanvas.width = pixelWidth;
|
|
|
|
annotationLayerCanvas.height = pixelHeight;
|
2021-12-05 22:18:06 +09:00
|
|
|
annotationLayerContext = annotationLayerCanvas.getContext("2d");
|
2021-12-04 23:21:04 +09:00
|
|
|
annotationLayerContext.clearRect(
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
0,
|
|
|
|
0,
|
2021-12-04 23:21:04 +09:00
|
|
|
annotationLayerCanvas.width,
|
|
|
|
annotationLayerCanvas.height
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
);
|
2022-02-08 07:40:21 +09:00
|
|
|
annotationLayerContext.scale(outputScale, outputScale);
|
2021-12-04 23:21:04 +09:00
|
|
|
|
|
|
|
if (!renderXfa) {
|
|
|
|
// The annotation builder will draw its content
|
|
|
|
// on the canvas.
|
|
|
|
initPromise = page.getAnnotations({ intent: "display" });
|
|
|
|
annotationCanvasMap = new Map();
|
|
|
|
} else {
|
2021-12-15 07:59:17 +09:00
|
|
|
initPromise = page.getXfa().then(function (xfaHtml) {
|
2021-12-05 22:57:01 +09:00
|
|
|
return Rasterize.xfaLayer(
|
2021-12-04 23:21:04 +09:00
|
|
|
annotationLayerContext,
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
viewport,
|
2021-12-15 07:59:17 +09:00
|
|
|
xfaHtml,
|
2021-12-04 23:21:04 +09:00
|
|
|
task.fontRules,
|
|
|
|
task.pdfDoc.annotationStorage,
|
|
|
|
task.renderPrint
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
);
|
2015-12-19 06:29:22 +09:00
|
|
|
});
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
} else {
|
|
|
|
annotationLayerCanvas = null;
|
|
|
|
initPromise = Promise.resolve();
|
2015-12-19 06:29:22 +09:00
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2021-12-05 22:18:06 +09:00
|
|
|
const renderContext = {
|
2021-12-04 23:21:04 +09:00
|
|
|
canvasContext: ctx,
|
|
|
|
viewport,
|
|
|
|
optionalContentConfigPromise: task.optionalContentConfigPromise,
|
|
|
|
annotationCanvasMap,
|
2022-05-04 22:37:13 +09:00
|
|
|
pageColors,
|
2022-02-08 07:40:21 +09:00
|
|
|
transform,
|
2021-12-04 23:21:04 +09:00
|
|
|
};
|
|
|
|
if (renderForms) {
|
2022-05-19 22:12:28 +09:00
|
|
|
renderContext.annotationMode = task.annotationStorage
|
|
|
|
? AnnotationMode.ENABLE_STORAGE
|
|
|
|
: AnnotationMode.ENABLE_FORMS;
|
2021-12-04 23:21:04 +09:00
|
|
|
} else if (renderPrint) {
|
|
|
|
if (task.annotationStorage) {
|
|
|
|
renderContext.annotationMode = AnnotationMode.ENABLE_STORAGE;
|
2020-08-18 05:03:01 +09:00
|
|
|
}
|
2021-12-04 23:21:04 +09:00
|
|
|
renderContext.intent = "print";
|
|
|
|
}
|
2020-08-18 05:03:01 +09:00
|
|
|
|
2021-12-05 22:18:06 +09:00
|
|
|
const completeRender = error => {
|
2021-12-04 23:21:04 +09:00
|
|
|
// 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
|
2022-02-08 07:40:21 +09:00
|
|
|
ctx.fillRect(0, 0, pixelWidth, pixelHeight);
|
2021-12-04 23:21:04 +09:00
|
|
|
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);
|
2021-12-04 23:45:47 +09:00
|
|
|
this._snapshot(task, error);
|
2021-12-04 23:21:04 +09:00
|
|
|
};
|
|
|
|
initPromise
|
2022-11-19 07:08:11 +09:00
|
|
|
.then(data => {
|
2021-12-04 23:21:04 +09:00
|
|
|
const renderTask = page.render(renderContext);
|
|
|
|
|
|
|
|
if (task.renderTaskOnContinue) {
|
|
|
|
renderTask.onContinue = function (cont) {
|
|
|
|
// Slightly delay the continued rendering.
|
|
|
|
setTimeout(cont, RENDER_TASK_ON_CONTINUE_DELAY);
|
|
|
|
};
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
}
|
2022-11-19 07:08:11 +09:00
|
|
|
return renderTask.promise.then(() => {
|
2021-12-04 23:21:04 +09:00
|
|
|
if (annotationCanvasMap) {
|
2021-12-05 22:57:01 +09:00
|
|
|
Rasterize.annotationLayer(
|
2021-12-04 23:21:04 +09:00
|
|
|
annotationLayerContext,
|
|
|
|
viewport,
|
2022-02-08 07:40:21 +09:00
|
|
|
outputScale,
|
2021-12-04 23:21:04 +09:00
|
|
|
data,
|
|
|
|
annotationCanvasMap,
|
|
|
|
page,
|
|
|
|
IMAGE_RESOURCES_PATH,
|
2022-11-19 07:08:11 +09:00
|
|
|
renderForms,
|
|
|
|
this._l10n
|
2021-12-04 23:21:04 +09:00
|
|
|
).then(() => {
|
2021-11-07 02:36:49 +09:00
|
|
|
completeRender(false);
|
2021-12-04 23:21:04 +09:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
completeRender(false);
|
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
});
|
2021-12-04 23:21:04 +09:00
|
|
|
})
|
|
|
|
.catch(function (error) {
|
|
|
|
completeRender("render : " + error);
|
|
|
|
});
|
|
|
|
},
|
2021-12-04 23:45:47 +09:00
|
|
|
error => {
|
|
|
|
this._snapshot(task, "render : " + error);
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
);
|
2021-12-04 23:21:04 +09:00
|
|
|
} catch (e) {
|
|
|
|
failure = "page setup : " + this._exceptionToString(e);
|
|
|
|
this._snapshot(task, failure);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_clearCanvas() {
|
2021-12-05 22:18:06 +09:00
|
|
|
const ctx = this.canvas.getContext("2d", { alpha: false });
|
2021-12-04 23:21:04 +09:00
|
|
|
ctx.beginPath();
|
|
|
|
ctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_snapshot(task, failure) {
|
|
|
|
this._log("Snapshotting... ");
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-05 22:18:06 +09:00
|
|
|
const dataUrl = this.canvas.toDataURL("image/png");
|
2022-03-07 22:54:59 +09:00
|
|
|
this._sendResult(dataUrl, task, failure).then(() => {
|
2021-12-04 23:45:47 +09:00
|
|
|
this._log(
|
2021-12-04 23:21:04 +09:00
|
|
|
"done" + (failure ? " (failed !: " + failure + ")" : "") + "\n"
|
Enable auto-formatting of the entire code-base using Prettier (issue 11444)
Note that Prettier, purposely, has only limited [configuration options](https://prettier.io/docs/en/options.html). The configuration file is based on [the one in `mozilla central`](https://searchfox.org/mozilla-central/source/.prettierrc) with just a few additions (to avoid future breakage if the defaults ever changes).
Prettier is being used for a couple of reasons:
- To be consistent with `mozilla-central`, where Prettier is already in use across the tree.
- To ensure a *consistent* coding style everywhere, which is automatically enforced during linting (since Prettier is used as an ESLint plugin). This thus ends "all" formatting disussions once and for all, removing the need for review comments on most stylistic matters.
Many ESLint options are now redundant, and I've tried my best to remove all the now unnecessary options (but I may have missed some).
Note also that since Prettier considers the `printWidth` option as a guide, rather than a hard rule, this patch resorts to a small hack in the ESLint config to ensure that *comments* won't become too long.
*Please note:* This patch is generated automatically, by appending the `--fix` argument to the ESLint call used in the `gulp lint` task. It will thus require some additional clean-up, which will be done in a *separate* commit.
(On a more personal note, I'll readily admit that some of the changes Prettier makes are *extremely* ugly. However, in the name of consistency we'll probably have to live with that.)
2019-12-25 23:59:37 +09:00
|
|
|
);
|
2021-12-04 23:21:04 +09:00
|
|
|
task.pageNum++;
|
2021-12-04 23:45:47 +09:00
|
|
|
this._nextPage(task);
|
2021-12-04 23:21:04 +09:00
|
|
|
});
|
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_quit() {
|
|
|
|
this._log("Done !");
|
|
|
|
this.end.textContent = "Tests finished. Close this window!";
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
// Send the quit request
|
2022-03-06 23:36:48 +09:00
|
|
|
fetch(`/tellMeToQuit?browser=${escape(this.browser)}`, {
|
|
|
|
method: "POST",
|
|
|
|
});
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_info(message) {
|
|
|
|
this._send(
|
|
|
|
"/info",
|
|
|
|
JSON.stringify({
|
2015-05-15 22:29:43 +09:00
|
|
|
browser: this.browser,
|
2021-12-04 23:21:04 +09:00
|
|
|
message,
|
|
|
|
})
|
|
|
|
);
|
|
|
|
}
|
2015-05-15 22:29:43 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_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;
|
|
|
|
}
|
|
|
|
}
|
2013-05-31 08:33:05 +09:00
|
|
|
|
2021-12-04 23:21:04 +09:00
|
|
|
_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);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-07 22:54:59 +09:00
|
|
|
_sendResult(snapshot, task, failure) {
|
2021-12-05 22:18:06 +09:00
|
|
|
const result = JSON.stringify({
|
2021-12-04 23:21:04 +09:00
|
|
|
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,
|
2022-02-08 07:40:21 +09:00
|
|
|
viewportWidth: task.viewportWidth,
|
|
|
|
viewportHeight: task.viewportHeight,
|
|
|
|
outputScale: task.outputScale,
|
2021-12-04 23:21:04 +09:00
|
|
|
});
|
2022-03-07 22:54:59 +09:00
|
|
|
return this._send("/submit_task_results", result);
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
|
|
|
|
2022-03-07 22:54:59 +09:00
|
|
|
_send(url, message) {
|
2022-03-08 01:41:41 +09:00
|
|
|
const capability = new PromiseCapability();
|
2022-03-07 22:54:59 +09:00
|
|
|
this.inflight.textContent = this.inFlightRequests++;
|
2021-12-04 23:21:04 +09:00
|
|
|
|
2022-03-07 22:54:59 +09:00
|
|
|
fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
headers: {
|
|
|
|
"Content-Type": "application/json",
|
|
|
|
},
|
|
|
|
body: message,
|
|
|
|
})
|
|
|
|
.then(response => {
|
|
|
|
// Retry until successful.
|
|
|
|
if (!response.ok || response.status !== 200) {
|
|
|
|
throw new Error(response.statusText);
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
2022-03-07 22:54:59 +09:00
|
|
|
|
|
|
|
this.inFlightRequests--;
|
|
|
|
capability.resolve();
|
|
|
|
})
|
|
|
|
.catch(reason => {
|
|
|
|
console.warn(`Driver._send failed (${url}): ${reason}`);
|
|
|
|
|
|
|
|
this.inFlightRequests--;
|
|
|
|
capability.resolve();
|
|
|
|
|
|
|
|
this._send(url, message);
|
|
|
|
});
|
|
|
|
|
|
|
|
return capability.promise;
|
2021-12-04 23:21:04 +09:00
|
|
|
}
|
|
|
|
}
|
2022-12-03 19:54:56 +09:00
|
|
|
|
|
|
|
export { Driver };
|