1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Merge pull request #14247 from calixteman/button

[api-minor] Render pushbuttons on their own canvas (bug 1737260)
This commit is contained in:
Brendan Dahl 2021-11-16 08:10:40 -08:00 committed by GitHub
commit 3209c013c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 333 additions and 86 deletions

View file

@ -101,6 +101,25 @@ function inlineImages(images) {
return Promise.all(imagePromises);
}
async function convertCanvasesToImages(annotationCanvasMap) {
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");
image.onload = resolve;
results.set(key, image);
image.src = URL.createObjectURL(blob);
});
})
);
}
await Promise.all(promises);
return results;
}
async function resolveImages(node, silentErrors = false) {
const images = node.getElementsByTagName("img");
const data = await inlineImages(images);
@ -227,6 +246,7 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
ctx,
viewport,
annotations,
annotationCanvasMap,
page,
imageResourcesPath,
renderForms = false
@ -255,6 +275,10 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
style.textContent = common + "\n" + overrides;
var annotation_viewport = viewport.clone({ dontFlip: true });
const annotationImageMap = await convertCanvasesToImages(
annotationCanvasMap
);
var parameters = {
viewport: annotation_viewport,
div,
@ -263,6 +287,7 @@ var rasterizeAnnotationLayer = (function rasterizeAnnotationLayerClosure() {
linkService: new SimpleLinkService(),
imageResourcesPath,
renderForms,
annotationCanvasMap: annotationImageMap,
};
AnnotationLayer.render(parameters);
@ -665,7 +690,8 @@ var Driver = (function DriverClosure() {
var renderAnnotations = false,
renderForms = false,
renderPrint = false,
renderXfa = false;
renderXfa = false,
annotationCanvasMap = null;
if (task.annotationStorage) {
const entries = Object.entries(task.annotationStorage),
@ -739,18 +765,8 @@ var Driver = (function DriverClosure() {
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
);
});
initPromise = page.getAnnotations({ intent: "display" });
annotationCanvasMap = new Map();
} else {
initPromise = page.getXfa().then(function (xfa) {
return rasterizeXfaLayer(
@ -768,11 +784,11 @@ var Driver = (function DriverClosure() {
initPromise = Promise.resolve();
}
}
var renderContext = {
canvasContext: ctx,
viewport,
optionalContentConfigPromise: task.optionalContentConfigPromise,
annotationCanvasMap,
};
if (renderForms) {
renderContext.annotationMode = AnnotationMode.ENABLE_FORMS;
@ -805,7 +821,7 @@ var Driver = (function DriverClosure() {
self._snapshot(task, error);
};
initPromise
.then(function () {
.then(function (data) {
const renderTask = page.render(renderContext);
if (task.renderTaskOnContinue) {
@ -815,7 +831,21 @@ var Driver = (function DriverClosure() {
};
}
return renderTask.promise.then(function () {
completeRender(false);
if (annotationCanvasMap) {
rasterizeAnnotationLayer(
annotationLayerContext,
viewport,
data,
annotationCanvasMap,
page,
IMAGE_RESOURCES_PATH,
renderForms
).then(() => {
completeRender(false);
});
} else {
completeRender(false);
}
});
})
.catch(function (error) {

View file

@ -26,6 +26,7 @@ import {
AnnotationFlag,
AnnotationType,
OPS,
RenderingIntentFlag,
stringToBytes,
stringToUTF8String,
} from "../../src/shared/util.js";
@ -1680,6 +1681,7 @@ describe("annotation", function () {
const operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -1694,6 +1696,7 @@ describe("annotation", function () {
[0, 0, 32, 10],
[32, 0, 0, 10, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([26, 51, 76])
@ -2319,6 +2322,7 @@ describe("annotation", function () {
const operatorList = await annotation.getOperatorList(
checkboxEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2335,6 +2339,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[3][0][0].unicode).toEqual("4");
});
@ -2378,6 +2383,7 @@ describe("annotation", function () {
let operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2392,6 +2398,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([26, 51, 76])
@ -2402,6 +2409,7 @@ describe("annotation", function () {
operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2416,6 +2424,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([76, 51, 26])
@ -2464,6 +2473,7 @@ describe("annotation", function () {
const operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2478,6 +2488,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([26, 51, 76])
@ -2524,6 +2535,7 @@ describe("annotation", function () {
const operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2538,6 +2550,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([26, 51, 76])
@ -2727,6 +2740,7 @@ describe("annotation", function () {
let operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2741,6 +2755,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([26, 51, 76])
@ -2751,6 +2766,7 @@ describe("annotation", function () {
operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2765,6 +2781,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([76, 51, 26])
@ -2811,6 +2828,7 @@ describe("annotation", function () {
const operatorList = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.PRINT,
false,
annotationStorage
);
@ -2825,6 +2843,7 @@ describe("annotation", function () {
[0, 0, 0, 0],
[0, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 0, 0],
false,
]);
expect(operatorList.argsArray[1]).toEqual(
new Uint8ClampedArray([76, 51, 26])