Compare commits

...

6 Commits

Author SHA1 Message Date
calixteman
b8de9a372f
Merge pull request #19856 from calixteman/bug1936605
Don't update the visible canvas at 60 fps (bug 1936605)
2025-04-29 22:31:02 +02:00
Jonas Jenwald
0922aa9e9d
Merge pull request #19880 from Snuffleupagus/numberToString-assert-number
Assert that `numberToString` is called with a number (issue 19877)
2025-04-29 20:35:32 +02:00
calixteman
262a1f9895
Merge pull request #19881 from calixteman/bug1963407
Fix the bbox when saving a rotated text field (bug 1963407)
2025-04-29 20:33:53 +02:00
Jonas Jenwald
f5faf86180 Assert that numberToString is called with a number (issue 19877)
*NOTE:* Given that this is an *internal* function, used only in the worker-thread, it's not clear to me that this is an entirely "necessary" change.
2025-04-29 20:31:24 +02:00
Calixte Denizet
7a251b206e Fix the bbox when saving a rotated text field (bug 1963407) 2025-04-29 18:49:07 +02:00
Calixte Denizet
ecc56a61e6 Don't update the visible canvas at 60 fps (bug 1936605)
Instead, we update the visible canvas every 500ms.
With large canvas, updating at 60fps lead to a lot gfx transactions and it can take a lot of time.
For example, with wuppertal_2012.pdf on Windows, displaying it at 150% takes around 14 min !!! without
this patch when it takes only around 14 sec with. Even at 30% it helps to improve the performance
by around 20%.
2025-04-29 18:35:21 +02:00
10 changed files with 99 additions and 6 deletions

View File

@ -2248,7 +2248,11 @@ class WidgetAnnotation extends Annotation {
const appearanceDict = (appearanceStream.dict = new Dict(xref));
appearanceDict.set("Subtype", Name.get("Form"));
appearanceDict.set("Resources", resources);
appearanceDict.set("BBox", [0, 0, this.width, this.height]);
const bbox =
rotation % 180 === 0
? [0, 0, this.width, this.height]
: [0, 0, this.height, this.width];
appearanceDict.set("BBox", bbox);
const rotationMatrix = this.getRotationMatrix(annotationStorage);
if (rotationMatrix !== IDENTITY_MATRIX) {

View File

@ -632,6 +632,13 @@ function recoverJsURL(str) {
}
function numberToString(value) {
if (typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) {
assert(
typeof value === "number",
`numberToString - the value (${value}) should be a number.`
);
}
if (Number.isInteger(value)) {
return value.toString();
}

View File

@ -723,3 +723,4 @@
!issue18529.pdf
!issue16742.pdf
!chrome-text-selection-markedContent.pdf
!bug1963407.pdf

BIN
test/pdfs/bug1963407.pdf Executable file

Binary file not shown.

View File

@ -12093,5 +12093,19 @@
"md5": "98b19e944339c01c10c503685eee3ad2",
"rounds": 1,
"type": "eq"
},
{
"id": "bug1963407",
"file": "pdfs/bug1963407.pdf",
"md5": "49d3d2dd42014d978af6e6ad814e5e64",
"rounds": 1,
"type": "eq",
"save": true,
"print": true,
"annotationStorage": {
"24R": {
"value": "Hello World"
}
}
}
]

View File

@ -2257,7 +2257,7 @@ describe("annotation", function () {
);
expect(newData.data).toEqual(
"2 0 obj\n<< /Subtype /Form /Resources " +
"<< /Font << /Helv 314 0 R>>>> /BBox [0 0 32 10] /Matrix [0 1 -1 0 32 0] /Length 74>> stream\n" +
"<< /Font << /Helv 314 0 R>>>> /BBox [0 0 10 32] /Matrix [0 1 -1 0 32 0] /Length 74>> stream\n" +
"/Tx BMC q BT /Helv 5 Tf 1 0 0 1 0 0 Tm 2 2.94 Td (hello world) Tj " +
"ET Q EMC\nendstream\nendobj\n"
);
@ -3808,7 +3808,7 @@ describe("annotation", function () {
[
"2 0 obj",
"<< /Subtype /Form /Resources << /Font << /Helv 314 0 R>>>> " +
"/BBox [0 0 32 10] /Matrix [0 -1 1 0 0 10] /Length 170>> stream",
"/BBox [0 0 10 32] /Matrix [0 -1 1 0 0 10] /Length 170>> stream",
"/Tx BMC q",
"1 1 10 32 re W n",
"0.600006 0.756866 0.854904 rg",

View File

@ -326,8 +326,6 @@ const PDFViewerApplication = {
}
}
if (params.has("pdfbug")) {
AppOptions.setAll({ pdfBug: true, fontExtraProperties: true });
const enabled = params.get("pdfbug").split(",");
try {
await loadPDFBug();
@ -335,6 +333,12 @@ const PDFViewerApplication = {
} catch (ex) {
console.error("_parseHashParams:", ex);
}
const debugOpts = { pdfBug: true, fontExtraProperties: true };
if (globalThis.StepperManager?.enabled) {
debugOpts.minDurationToUpdateCanvas = 0;
}
AppOptions.setAll(debugOpts);
}
// It is not possible to change locale for the (various) extension builds.
if (
@ -519,6 +523,7 @@ const PDFViewerApplication = {
enableHWA,
supportsPinchToZoom: this.supportsPinchToZoom,
enableAutoLinking: AppOptions.get("enableAutoLinking"),
minDurationToUpdateCanvas: AppOptions.get("minDurationToUpdateCanvas"),
}));
renderingQueue.setViewer(pdfViewer);

View File

@ -302,6 +302,11 @@ const defaultOptions = {
value: 2 ** 25,
kind: OptionKind.VIEWER,
},
minDurationToUpdateCanvas: {
/** @type {number} */
value: 500, // ms
kind: OptionKind.VIEWER,
},
forcePageColors: {
/** @type {boolean} */
value: false,

View File

@ -21,12 +21,18 @@ class BasePDFPageView {
#loadingId = null;
#minDurationToUpdateCanvas = 0;
#renderError = null;
#renderingState = RenderingStates.INITIAL;
#showCanvas = null;
#startTime = 0;
#tempCanvas = null;
canvas = null;
/** @type {null | HTMLDivElement} */
@ -51,6 +57,7 @@ class BasePDFPageView {
this.id = options.id;
this.pageColors = options.pageColors || null;
this.renderingQueue = options.renderingQueue;
this.#minDurationToUpdateCanvas = options.minDurationToUpdateCanvas ?? 500;
}
get renderingState() {
@ -71,6 +78,9 @@ class BasePDFPageView {
switch (state) {
case RenderingStates.PAUSED:
this.div.classList.remove("loading");
// Display the canvas as it has been drawn.
this.#startTime = 0;
this.#showCanvas?.(false);
break;
case RenderingStates.RUNNING:
this.div.classList.add("loadingIcon");
@ -82,10 +92,12 @@ class BasePDFPageView {
this.div.classList.add("loading");
this.#loadingId = null;
}, 0);
this.#startTime = Date.now();
break;
case RenderingStates.INITIAL:
case RenderingStates.FINISHED:
this.div.classList.remove("loadingIcon", "loading");
this.#startTime = 0;
break;
}
}
@ -100,10 +112,41 @@ class BasePDFPageView {
// have a final flash we just display it once all the drawing is done.
const updateOnFirstShow = !prevCanvas && !hasHCM && !hideUntilComplete;
const canvas = (this.canvas = document.createElement("canvas"));
let canvas = (this.canvas = document.createElement("canvas"));
this.#showCanvas = isLastShow => {
if (updateOnFirstShow) {
let tempCanvas = this.#tempCanvas;
if (!isLastShow && this.#minDurationToUpdateCanvas > 0) {
// We draw on the canvas at 60fps (in using `requestAnimationFrame`),
// so if the canvas is large, updating it at 60fps can be a way too
// much and can cause some serious performance issues.
// To avoid that we only update the canvas every
// `this.#minDurationToUpdateCanvas` ms.
if (Date.now() - this.#startTime < this.#minDurationToUpdateCanvas) {
return;
}
if (!tempCanvas) {
tempCanvas = this.#tempCanvas = canvas;
canvas = this.canvas = canvas.cloneNode(false);
onShow(canvas);
}
}
if (tempCanvas) {
const ctx = canvas.getContext("2d", {
alpha: false,
});
ctx.drawImage(tempCanvas, 0, 0);
if (isLastShow) {
this.#resetTempCanvas();
} else {
this.#startTime = Date.now();
}
return;
}
// Don't add the canvas until the first draw callback, or until
// drawing is complete when `!this.renderingQueue`, to prevent black
// flickering.
@ -152,6 +195,14 @@ class BasePDFPageView {
canvas.remove();
canvas.width = canvas.height = 0;
this.canvas = null;
this.#resetTempCanvas();
}
#resetTempCanvas() {
if (this.#tempCanvas) {
this.#tempCanvas.width = this.#tempCanvas.height = 0;
this.#tempCanvas = null;
}
}
async _drawCanvas(options, onCancel, onFinish) {

View File

@ -139,6 +139,8 @@ function isValidAnnotationEditorMode(mode) {
* The default value is `true`.
* @property {boolean} [enableAutoLinking] - Enable creation of hyperlinks from
* text that look like URLs. The default value is `true`.
* @property {number} [minDurationToUpdateCanvas] - Minimum duration to wait
* before updating the canvas. The default value is `500`.
*/
class PDFPageViewBuffer {
@ -243,6 +245,8 @@ class PDFViewer {
#eventAbortController = null;
#minDurationToUpdateCanvas = 0;
#mlManager = null;
#scrollTimeoutId = null;
@ -342,6 +346,7 @@ class PDFViewer {
this.#enableHWA = options.enableHWA || false;
this.#supportsPinchToZoom = options.supportsPinchToZoom !== false;
this.#enableAutoLinking = options.enableAutoLinking !== false;
this.#minDurationToUpdateCanvas = options.minDurationToUpdateCanvas ?? 500;
this.defaultRenderingQueue = !options.renderingQueue;
if (
@ -1003,6 +1008,7 @@ class PDFViewer {
layerProperties: this._layerProperties,
enableHWA: this.#enableHWA,
enableAutoLinking: this.#enableAutoLinking,
minDurationToUpdateCanvas: this.#minDurationToUpdateCanvas,
});
this._pages.push(pageView);
}