Skip to content

Commit 1b427a3

Browse files
authored
Merge pull request #20016 from ryzokuken/move-getcontext
[api-minor] Move getContext call to InternalRenderTask
2 parents 0e2b59e + b1b728d commit 1b427a3

12 files changed

+129
-59
lines changed

src/display/api.js

Lines changed: 29 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -501,7 +501,8 @@ function getDocument(src = {}) {
501501
task,
502502
networkStream,
503503
transportParams,
504-
transportFactory
504+
transportFactory,
505+
enableHWA
505506
);
506507
task._transport = transport;
507508
messageHandler.send("Ready", null);
@@ -1181,8 +1182,12 @@ class PDFDocumentProxy {
11811182
* Page render parameters.
11821183
*
11831184
* @typedef {Object} RenderParameters
1184-
* @property {CanvasRenderingContext2D} canvasContext - A 2D context of a DOM
1185-
* Canvas object.
1185+
* @property {CanvasRenderingContext2D} canvasContext - Deprecated 2D context of
1186+
* a DOM Canvas object for backwards compatibility; it is recommended to use
1187+
* the `canvas` parameter instead.
1188+
* @property {HTMLCanvasElement} canvas - A DOM Canvas object. The default value
1189+
* is the canvas associated with the `canvasContext` parameter if no value is
1190+
* provided explicitly.
11861191
* @property {PageViewport} viewport - Rendering viewport obtained by calling
11871192
* the `PDFPageProxy.getViewport` method.
11881193
* @property {string} [intent] - Rendering intent, can be 'display', 'print',
@@ -1405,6 +1410,7 @@ class PDFPageProxy {
14051410
*/
14061411
render({
14071412
canvasContext,
1413+
canvas = canvasContext.canvas,
14081414
viewport,
14091415
intent = "display",
14101416
annotationMode = AnnotationMode.ENABLE,
@@ -1496,7 +1502,7 @@ class PDFPageProxy {
14961502
callback: complete,
14971503
// Only include the required properties, and *not* the entire object.
14981504
params: {
1499-
canvasContext,
1505+
canvas,
15001506
viewport,
15011507
transform,
15021508
background,
@@ -1511,6 +1517,7 @@ class PDFPageProxy {
15111517
useRequestAnimationFrame: !intentPrint,
15121518
pdfBug: this._pdfBug,
15131519
pageColors,
1520+
enableHWA: this._transport.enableHWA,
15141521
});
15151522

15161523
(intentState.renderTasks ||= new Set()).add(internalRenderTask);
@@ -2305,7 +2312,14 @@ class WorkerTransport {
23052312

23062313
#passwordCapability = null;
23072314

2308-
constructor(messageHandler, loadingTask, networkStream, params, factory) {
2315+
constructor(
2316+
messageHandler,
2317+
loadingTask,
2318+
networkStream,
2319+
params,
2320+
factory,
2321+
enableHWA
2322+
) {
23092323
this.messageHandler = messageHandler;
23102324
this.loadingTask = loadingTask;
23112325
this.commonObjs = new PDFObjects();
@@ -2329,6 +2343,7 @@ class WorkerTransport {
23292343
this._fullReader = null;
23302344
this._lastProgress = null;
23312345
this.downloadInfoCapability = Promise.withResolvers();
2346+
this.enableHWA = enableHWA;
23322347

23332348
this.setupMessageHandler();
23342349

@@ -3104,6 +3119,7 @@ class InternalRenderTask {
31043119
useRequestAnimationFrame = false,
31053120
pdfBug = false,
31063121
pageColors = null,
3122+
enableHWA = false,
31073123
}) {
31083124
this.callback = callback;
31093125
this.params = params;
@@ -3131,7 +3147,8 @@ class InternalRenderTask {
31313147
this._continueBound = this._continue.bind(this);
31323148
this._scheduleNextBound = this._scheduleNext.bind(this);
31333149
this._nextBound = this._next.bind(this);
3134-
this._canvas = params.canvasContext.canvas;
3150+
this._canvas = params.canvas;
3151+
this._enableHWA = enableHWA;
31353152
}
31363153

31373154
get completed() {
@@ -3161,7 +3178,12 @@ class InternalRenderTask {
31613178
this.stepper.init(this.operatorList);
31623179
this.stepper.nextBreakPoint = this.stepper.getNextBreakPoint();
31633180
}
3164-
const { canvasContext, viewport, transform, background } = this.params;
3181+
const { viewport, transform, background } = this.params;
3182+
3183+
const canvasContext = this._canvas.getContext("2d", {
3184+
alpha: false,
3185+
willReadFrequently: !this._enableHWA,
3186+
});
31653187

31663188
this.gfx = new CanvasGraphics(
31673189
canvasContext,

test/driver.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1016,7 +1016,7 @@ class Driver {
10161016
}
10171017
}
10181018
const renderContext = {
1019-
canvasContext: ctx,
1019+
canvas: this.canvas,
10201020
viewport,
10211021
optionalContentConfigPromise: task.optionalContentConfigPromise,
10221022
annotationCanvasMap,

test/integration/jasmine-boot.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ async function runTests(results) {
4141
"stamp_editor_spec.mjs",
4242
"text_field_spec.mjs",
4343
"text_layer_spec.mjs",
44+
"thumbnail_view_spec.mjs",
4445
"viewer_spec.mjs",
4546
],
4647
});
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import { closePages, loadAndWait } from "./test_utils.mjs";
2+
3+
describe("PDF Thumbnail View", () => {
4+
describe("Works without errors", () => {
5+
let pages;
6+
7+
beforeEach(async () => {
8+
pages = await loadAndWait("tracemonkey.pdf", "#sidebarToggleButton");
9+
});
10+
11+
afterEach(async () => {
12+
await closePages(pages);
13+
});
14+
15+
it("should render thumbnails without errors", async () => {
16+
await Promise.all(
17+
pages.map(async ([browserName, page]) => {
18+
await page.click("#sidebarToggleButton");
19+
20+
const thumbSelector = "#thumbnailView .thumbnailImage";
21+
await page.waitForSelector(thumbSelector, { visible: true });
22+
23+
await page.waitForSelector(
24+
'#thumbnailView .thumbnail[data-loaded="true"]'
25+
);
26+
27+
const src = await page.$eval(thumbSelector, el => el.src);
28+
expect(src)
29+
.withContext(`In ${browserName}`)
30+
.toMatch(/^data:image\//);
31+
})
32+
);
33+
});
34+
});
35+
});

test/unit/api_spec.js

Lines changed: 41 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4410,7 +4410,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44104410
viewport.height
44114411
);
44124412
const renderTask = pdfPage.render({
4413-
canvasContext: canvasAndCtx.context,
4413+
canvas: canvasAndCtx.canvas,
44144414
viewport,
44154415
});
44164416
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4446,7 +4446,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44464446
viewport.height
44474447
);
44484448
const renderTask = page.render({
4449-
canvasContext: canvasAndCtx.context,
4449+
canvas: canvasAndCtx.canvas,
44504450
viewport,
44514451
});
44524452
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4477,7 +4477,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44774477
viewport.height
44784478
);
44794479
const renderTask = page.render({
4480-
canvasContext: canvasAndCtx.context,
4480+
canvas: canvasAndCtx.canvas,
44814481
viewport,
44824482
});
44834483
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4494,7 +4494,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
44944494
}
44954495

44964496
const reRenderTask = page.render({
4497-
canvasContext: canvasAndCtx.context,
4497+
canvas: canvasAndCtx.canvas,
44984498
viewport,
44994499
});
45004500
expect(reRenderTask instanceof RenderTask).toEqual(true);
@@ -4518,14 +4518,14 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
45184518
viewport.height
45194519
);
45204520
const renderTask1 = page.render({
4521-
canvasContext: canvasAndCtx.context,
4521+
canvas: canvasAndCtx.canvas,
45224522
viewport,
45234523
optionalContentConfigPromise,
45244524
});
45254525
expect(renderTask1 instanceof RenderTask).toEqual(true);
45264526

45274527
const renderTask2 = page.render({
4528-
canvasContext: canvasAndCtx.context,
4528+
canvas: canvasAndCtx.canvas,
45294529
viewport,
45304530
optionalContentConfigPromise,
45314531
});
@@ -4562,7 +4562,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
45624562
viewport.height
45634563
);
45644564
const renderTask = pdfPage.render({
4565-
canvasContext: canvasAndCtx.context,
4565+
canvas: canvasAndCtx.canvas,
45664566
viewport,
45674567
});
45684568
expect(renderTask instanceof RenderTask).toEqual(true);
@@ -4591,7 +4591,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
45914591
viewport.height
45924592
);
45934593
const renderTask = pdfPage.render({
4594-
canvasContext: canvasAndCtx.context,
4594+
canvas: canvasAndCtx.canvas,
45954595
viewport,
45964596
background: "#FF0000", // See comment below.
45974597
});
@@ -4651,7 +4651,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
46514651
viewport.height
46524652
);
46534653
const renderTask = pdfPage.render({
4654-
canvasContext: canvasAndCtx.context,
4654+
canvas: canvasAndCtx.canvas,
46554655
viewport,
46564656
});
46574657

@@ -4755,7 +4755,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
47554755
viewport.height
47564756
);
47574757
const renderTask = pdfPage.render({
4758-
canvasContext: canvasAndCtx.context,
4758+
canvas: canvasAndCtx.canvas,
47594759
viewport,
47604760
});
47614761

@@ -4802,7 +4802,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
48024802
viewport.height
48034803
);
48044804
const renderTask = pdfPage.render({
4805-
canvasContext: canvasAndCtx.context,
4805+
canvas: canvasAndCtx.canvas,
48064806
viewport,
48074807
});
48084808

@@ -4852,7 +4852,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
48524852
viewport.height
48534853
);
48544854
const renderTask = pdfPage.render({
4855-
canvasContext: canvasAndCtx.context,
4855+
canvas: canvasAndCtx.canvas,
48564856
viewport,
48574857
intent: "print",
48584858
annotationMode: AnnotationMode.ENABLE_STORAGE,
@@ -4911,6 +4911,34 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
49114911

49124912
await loadingTask.destroy();
49134913
});
4914+
4915+
it("should work with the legacy canvasContext parameter", async function () {
4916+
const loadingTask = getDocument(tracemonkeyGetDocumentParams);
4917+
const pdfDoc = await loadingTask.promise;
4918+
const pdfPage = await pdfDoc.getPage(1);
4919+
const viewport = pdfPage.getViewport({ scale: 1 });
4920+
4921+
const { canvasFactory } = pdfDoc;
4922+
const canvasAndCtx = canvasFactory.create(
4923+
viewport.width,
4924+
viewport.height
4925+
);
4926+
const renderTask = pdfPage.render({
4927+
canvasContext: canvasAndCtx.context,
4928+
viewport,
4929+
});
4930+
expect(renderTask instanceof RenderTask).toEqual(true);
4931+
4932+
await renderTask.promise;
4933+
expect(
4934+
canvasAndCtx.context
4935+
.getImageData(0, 0, viewport.width, viewport.height)
4936+
.data.some(channel => channel !== 0)
4937+
).toEqual(true);
4938+
4939+
canvasFactory.destroy(canvasAndCtx);
4940+
await loadingTask.destroy();
4941+
});
49144942
});
49154943

49164944
describe("Multiple `getDocument` instances", function () {
@@ -4939,7 +4967,7 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
49394967
viewport.height
49404968
);
49414969
const renderTask = page.render({
4942-
canvasContext: canvasAndCtx.context,
4970+
canvas: canvasAndCtx.canvas,
49434971
viewport,
49444972
});
49454973
await renderTask.promise;

test/unit/custom_spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ describe("custom canvas rendering", function () {
5050
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
5151

5252
const renderTask = page.render({
53-
canvasContext: canvasAndCtx.context,
53+
canvas: canvasAndCtx.canvas,
5454
viewport,
5555
});
5656
await renderTask.promise;
@@ -70,7 +70,7 @@ describe("custom canvas rendering", function () {
7070
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
7171

7272
const renderTask = page.render({
73-
canvasContext: canvasAndCtx.context,
73+
canvas: canvasAndCtx.canvas,
7474
viewport,
7575
background: "rgba(255,0,0,1.0)",
7676
});
@@ -160,7 +160,7 @@ describe("custom ownerDocument", function () {
160160
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
161161

162162
await page.render({
163-
canvasContext: canvasAndCtx.context,
163+
canvas: canvasAndCtx.canvas,
164164
viewport,
165165
}).promise;
166166

@@ -194,7 +194,7 @@ describe("custom ownerDocument", function () {
194194
const canvasAndCtx = canvasFactory.create(viewport.width, viewport.height);
195195

196196
await page.render({
197-
canvasContext: canvasAndCtx.context,
197+
canvas: canvasAndCtx.canvas,
198198
viewport,
199199
}).promise;
200200

web/base_pdf_page_view.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,6 @@ import { RenderingCancelledException } from "pdfjs-lib";
1717
import { RenderingStates } from "./ui_utils.js";
1818

1919
class BasePDFPageView {
20-
#enableHWA = false;
21-
2220
#loadingId = null;
2321

2422
#minDurationToUpdateCanvas = 0;
@@ -51,8 +49,6 @@ class BasePDFPageView {
5149
resume = null;
5250

5351
constructor(options) {
54-
this.#enableHWA =
55-
#enableHWA in options ? options.#enableHWA : options.enableHWA || false;
5652
this.eventBus = options.eventBus;
5753
this.id = options.id;
5854
this.pageColors = options.pageColors || null;
@@ -166,12 +162,7 @@ class BasePDFPageView {
166162
}
167163
};
168164

169-
const ctx = canvas.getContext("2d", {
170-
alpha: false,
171-
willReadFrequently: !this.#enableHWA,
172-
});
173-
174-
return { canvas, prevCanvas, ctx };
165+
return { canvas, prevCanvas };
175166
}
176167

177168
#renderContinueCallback = cont => {

web/firefox_print_service.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ function composePage(
7272
currentRenderTask = null;
7373
}
7474
const renderContext = {
75-
canvasContext: ctx,
75+
canvas: ctx.canvas,
7676
transform: [PRINT_UNITS, 0, 0, PRINT_UNITS, 0, 0],
7777
viewport: pdfPage.getViewport({ scale: 1, rotation: size.rotation }),
7878
intent: "print",

0 commit comments

Comments
 (0)