From 4a8d742592233ba8fb6d2db86626a0343a089136 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 10 May 2024 12:13:14 +0200 Subject: [PATCH 1/3] Move the reporting of page `Stats` into the API This avoids having to add a couple of event listeners in the viewer, when debugging is enabled, and is consistent with the existing handling of `FontInspector` and `StepperManager` in the API. --- src/display/api.js | 10 ++++++++-- web/app.js | 14 -------------- 2 files changed, 8 insertions(+), 16 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index 9c70a7c22cb2f..5b91922f18907 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -1510,8 +1510,14 @@ class PDFPageProxy { internalRenderTask.capability.resolve(); } - this._stats?.timeEnd("Rendering"); - this._stats?.timeEnd("Overall"); + if (this._stats) { + this._stats.timeEnd("Rendering"); + this._stats.timeEnd("Overall"); + + if (globalThis.Stats?.enabled) { + globalThis.Stats.add(this.pageNumber, this._stats); + } + } }; const internalRenderTask = new InternalRenderTask({ diff --git a/web/app.js b/web/app.js index 491761d69ef10..80ab34877e95f 100644 --- a/web/app.js +++ b/web/app.js @@ -1898,10 +1898,6 @@ const PDFViewerApplication = { signal, }); - if (AppOptions.get("pdfBug")) { - eventBus._on("pagerendered", reportPageStatsPDFBug, { signal }); - eventBus._on("pagechanging", reportPageStatsPDFBug, { signal }); - } if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { eventBus._on("fileinputchange", webViewerFileInputChange, { signal }); eventBus._on("openfile", webViewerOpenFile, { signal }); @@ -2187,16 +2183,6 @@ async function loadPDFBug(self) { self._PDFBug = PDFBug; } -function reportPageStatsPDFBug({ pageNumber }) { - if (!globalThis.Stats?.enabled) { - return; - } - const pageView = PDFViewerApplication.pdfViewer.getPageView( - /* index = */ pageNumber - 1 - ); - globalThis.Stats.add(pageNumber, pageView?.pdfPage?.stats); -} - function webViewerPageRender({ pageNumber }) { // If the page is (the most) visible when it starts rendering, // ensure that the page number input loading indicator is displayed. From ba8c620e4b05d21d6a7714b4e157ed262ebdc58a Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 10 May 2024 12:21:37 +0200 Subject: [PATCH 2/3] Inline the `loadFakeWorker` function at its only call-site in `web/app.js` Given that this is a debug-only, and fairly short, function we can just inline the code. --- web/app.js | 18 +++++++----------- 1 file changed, 7 insertions(+), 11 deletions(-) diff --git a/web/app.js b/web/app.js index 80ab34877e95f..b97ed3d016531 100644 --- a/web/app.js +++ b/web/app.js @@ -267,7 +267,13 @@ const PDFViewerApplication = { if (params.get("disableworker") === "true") { try { - await loadFakeWorker(); + GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc"); + + if (typeof PDFJSDev === "undefined") { + globalThis.pdfjsWorker = await import("pdfjs/pdf.worker.js"); + } else { + await __non_webpack_import__(PDFWorker.workerSrc); + } } catch (ex) { console.error(`_parseHashParams: "${ex.message}".`); } @@ -2164,16 +2170,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { }; } -async function loadFakeWorker() { - GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc"); - - if (typeof PDFJSDev === "undefined") { - globalThis.pdfjsWorker = await import("pdfjs/pdf.worker.js"); - return; - } - await __non_webpack_import__(PDFWorker.workerSrc); -} - async function loadPDFBug(self) { const { PDFBug } = typeof PDFJSDev === "undefined" From 5fbc5ba16a6e595c45b0da1b9a15259558242247 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Fri, 10 May 2024 12:36:35 +0200 Subject: [PATCH 3/3] Move the `loadPDFBug` function into `PDFViewerApplication._parseHashParams` Given that this is a debug-only function we don't need to define it "globally" in the `web/app.js` file. --- web/app.js | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/web/app.js b/web/app.js index b97ed3d016531..a980be77cda4e 100644 --- a/web/app.js +++ b/web/app.js @@ -265,6 +265,18 @@ const PDFViewerApplication = { const { mainContainer, viewerContainer } = this.appConfig, params = parseQueryString(hash); + const loadPDFBug = async () => { + if (this._PDFBug) { + return; + } + const { PDFBug } = + typeof PDFJSDev === "undefined" + ? await import(AppOptions.get("debuggerSrc")) // eslint-disable-line no-unsanitized/method + : await __non_webpack_import__(AppOptions.get("debuggerSrc")); + + this._PDFBug = PDFBug; + }; + if (params.get("disableworker") === "true") { try { GlobalWorkerOptions.workerSrc ||= AppOptions.get("workerSrc"); @@ -312,7 +324,7 @@ const PDFViewerApplication = { case "hover": viewerContainer.classList.add(`textLayer-${params.get("textlayer")}`); try { - await loadPDFBug(this); + await loadPDFBug(); this._PDFBug.loadCSS(); } catch (ex) { console.error(`_parseHashParams: "${ex.message}".`); @@ -321,12 +333,11 @@ const PDFViewerApplication = { } } if (params.has("pdfbug")) { - AppOptions.set("pdfBug", true); - AppOptions.set("fontExtraProperties", true); + AppOptions.setAll({ pdfBug: true, fontExtraProperties: true }); const enabled = params.get("pdfbug").split(","); try { - await loadPDFBug(this); + await loadPDFBug(); this._PDFBug.init(mainContainer, enabled); } catch (ex) { console.error(`_parseHashParams: "${ex.message}".`); @@ -2170,15 +2181,6 @@ if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { }; } -async function loadPDFBug(self) { - const { PDFBug } = - typeof PDFJSDev === "undefined" - ? await import(AppOptions.get("debuggerSrc")) // eslint-disable-line no-unsanitized/method - : await __non_webpack_import__(AppOptions.get("debuggerSrc")); - - self._PDFBug = PDFBug; -} - function webViewerPageRender({ pageNumber }) { // If the page is (the most) visible when it starts rendering, // ensure that the page number input loading indicator is displayed.