Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -1266,8 +1266,8 @@ class Annotation {
(!this._ownCanvasRequiresForms ||
intent & RenderingIntentFlag.ANNOTATIONS_FORMS)
);
if (isUsingOwnCanvas && (this.width === 0 || this.height === 0)) {
// Empty annotation, don't draw anything.
if (isUsingOwnCanvas && !this._hasCanvasSize) {
// Skip empty or invalid annotations.
this.data.hasOwnCanvas = false;
return this._getOperatorListNoAppearance();
}
Expand Down Expand Up @@ -1551,6 +1551,12 @@ class Annotation {
get height() {
return this.data.rect[3] - this.data.rect[1];
}

// Rect coordinates can overflow to Infinity.
get _hasCanvasSize() {
const { width, height } = this;
return width > 0 && height > 0 && Number.isFinite(width + height);
}
}

/**
Expand Down Expand Up @@ -2155,8 +2161,8 @@ class WidgetAnnotation extends Annotation {
const isUsingOwnCanvas = !!(
this.data.hasOwnCanvas && intent & RenderingIntentFlag.DISPLAY
);
if (isUsingOwnCanvas && (this.width === 0 || this.height === 0)) {
// Don't request a separate zero-sized canvas (see bug 2069428).
if (isUsingOwnCanvas && !this._hasCanvasSize) {
// Skip invalid canvas sizes (see bug 2069428).
this.data.hasOwnCanvas = false;
return this._getOperatorListNoAppearance();
}
Expand Down
20 changes: 15 additions & 5 deletions src/display/renderer_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
*/

import { CanvasGraphics, getAnnotationCanvasName } from "./canvas.js";
import { isNodeJS, makeMap, setVerbosityLevel } from "../shared/util.js";
import { isNodeJS, makeMap, setVerbosityLevel, warn } from "../shared/util.js";
import { createCanvasTrackers } from "./canvas_dependency_tracker.js";
import { FontLoader } from "./font_loader.js";
import { initGPU } from "./webgpu.js";
Expand Down Expand Up @@ -104,8 +104,13 @@ class RendererMessageHandler {

static async #sendFrame(handler, renderTaskState, isFinal) {
const { canvas, renderTaskId } = renderTaskState;
let bitmap;
if (isFinal) {
let bitmap = null;
if (canvas.width === 0 || canvas.height === 0) {
// transferToImageBitmap() fails for zero-sized canvases.
if (!isFinal) {
return;
}
} else if (isFinal) {
bitmap = canvas.transferToImageBitmap();
} else {
// We need to use createImageBitmap for interim frames because
Expand All @@ -122,7 +127,7 @@ class RendererMessageHandler {
bitmap = await createImageBitmap(canvas);
}
}
const transfers = [bitmap];
const transfers = bitmap ? [bitmap] : [];
Comment thread
calixteman marked this conversation as resolved.
Resolved
const annotationBitmaps = isFinal
? this.#collectAnnotationBitmaps(renderTaskState, transfers)
: null;
Expand All @@ -144,7 +149,12 @@ class RendererMessageHandler {
) {
return;
}
await this.#sendFrame(handler, renderTaskState, /* isFinal = */ false);
try {
await this.#sendFrame(handler, renderTaskState, /* isFinal = */ false);
} catch (ex) {
// An interim frame failure must not abort rendering.
warn(`Failed to send an interim frame: ${ex.message}`);
}
renderTaskState.lastFrameTime = Date.now();
renderTaskState.lastFrameIdx = renderTaskState.operatorListIdx;
}
Expand Down
8 changes: 5 additions & 3 deletions src/display/renderer_worker_proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import { OutputScale } from "./display_utils.js";
import { setAnnotationCanvasName } from "./canvas.js";

function closeFrame({ bitmap, annotationBitmaps }) {
bitmap.close();
bitmap?.close();
if (annotationBitmaps) {
for (const [, , annotationBitmap] of annotationBitmaps) {
annotationBitmap.close();
Expand Down Expand Up @@ -184,8 +184,10 @@ class WorkerRenderTask {

drawFrame(frame) {
try {
const ctx = this.#canvasFactory.getNoAlphaContext(this.#canvas);
ctx.drawImage(frame.bitmap, 0, 0);
if (frame.bitmap) {
const ctx = this.#canvasFactory.getNoAlphaContext(this.#canvas);
ctx.drawImage(frame.bitmap, 0, 0);
}

if (frame.annotationBitmaps) {
this.#drawAnnotationCanvases(frame.annotationBitmaps);
Expand Down
67 changes: 67 additions & 0 deletions test/unit/annotation_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -491,6 +491,36 @@ describe("annotation", function () {

expect(annotation.color).toEqual(new Uint8ClampedArray([0, 0, 0]));
});

it("should not use its own canvas when it has a non-finite dimension", async function () {
const annotationRef = Ref.get(819, 0);
const annotationDict = new Dict();
annotationDict.set("Type", Name.get("Annot"));
annotationDict.set("Subtype", Name.get("Square"));
annotationDict.set("F", AnnotationFlag.NOROTATE);
annotationDict.set("Rect", [0, 0, Infinity, 20]);

const xref = new XRefMock([{ ref: annotationRef, data: annotationDict }]);
partialEvaluator.xref = xref;

const annotation = await AnnotationFactory.create(
xref,
annotationRef,
annotationGlobalsMock,
idFactoryMock
);
expect(annotation.data.hasOwnCanvas).toBeTrue();

const { opList, separateCanvas } = await annotation.getOperatorList(
partialEvaluator,
new WorkerTask("test display"),
RenderingIntentFlag.DISPLAY,
new Map()
);
expect(opList.length).toEqual(0);
expect(separateCanvas).toBeFalse();
expect(annotation.data.hasOwnCanvas).toBeFalse();
});
});

describe("AnnotationBorderStyle", function () {
Expand Down Expand Up @@ -2353,6 +2383,43 @@ describe("annotation", function () {
expect(annotation.data.hasOwnCanvas).toBeFalse();
});

it("should not use its own canvas when the field has a non-finite dimension", async function () {
textWidgetDict.set("Ff", AnnotationFieldFlag.READONLY);

for (const width of [Infinity, NaN]) {
textWidgetDict.set("Rect", [0, 0, width, 75]);

const textWidgetRef = Ref.get(271, 0);
const xref = new XRefMock([
{ ref: textWidgetRef, data: textWidgetDict },
helvRefObj,
]);
const task = new WorkerTask("test display");
partialEvaluator.xref = xref;

const annotation = await AnnotationFactory.create(
xref,
textWidgetRef,
annotationGlobalsMock,
idFactoryMock
);
expect(annotation.data.hasOwnCanvas).toBeTrue();

const annotationStorage = new Map();
annotationStorage.set(annotation.data.id, { value: "A" });

const { opList, separateCanvas } = await annotation.getOperatorList(
partialEvaluator,
task,
RenderingIntentFlag.DISPLAY,
annotationStorage
);
expect(opList.length).toEqual(0);
expect(separateCanvas).toBeFalse();
expect(annotation.data.hasOwnCanvas).toBeFalse();
}
});

it("should render comb for printing", async function () {
textWidgetDict.set("Ff", AnnotationFieldFlag.COMB);
textWidgetDict.set("MaxLen", 4);
Expand Down
28 changes: 28 additions & 0 deletions test/unit/api_spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -5899,6 +5899,34 @@ have written that much by now. So, here’s to squashing bugs.`);
await loadingTask.destroy();
});

it("renders into a zero-width canvas", async function () {
if (isNodeJS) {
pending("Worker rendering is not supported in Node.js.");
}
const loadingTask = getDocument(buildGetDocumentParams("bug1844576.pdf"));
const pdfDoc = await loadingTask.promise;
const pdfPage = await pdfDoc.getPage(1);

const viewport = pdfPage.getViewport({ scale: 1 });
const canvas = document.createElement("canvas");
canvas.width = 0;
canvas.height = viewport.height;
const annotationCanvasMap = new Map();

const renderTask = pdfPage.render({
canvas,
viewport,
annotationMode: AnnotationMode.ENABLE_FORMS,
annotationCanvasMap,
});
expect(renderTask.isWorkerRendering).toBeTrue();
await renderTask.promise;

expect([...annotationCanvasMap.keys()].sort()).toEqual(["12R", "9R"]);

await loadingTask.destroy();
});

it("cleans up document resources after rendering of page", async function () {
const loadingTask = getDocument(buildGetDocumentParams(basicApiFileName));
const pdfDoc = await loadingTask.promise;
Expand Down
Loading