Skip to content

Commit

Permalink
Reset all the canvas states after rendering each annotations (#14105)
Browse files Browse the repository at this point in the history
- each annotation must be rendered independently of the others. So
  after having rendered each annotation, the canvas states are reset
  in order to have something clean to render the next one.
  • Loading branch information
calixteman committed Jun 7, 2022
1 parent 135b9fb commit 96d0d22
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 12 deletions.
32 changes: 20 additions & 12 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -1453,7 +1453,7 @@ class CanvasGraphics {
}
}

endDrawing() {
#restoreInitialState() {
// Finishing all opened operations such as SMask group painting.
while (this.stateStack.length || this.inSMaskMode) {
this.restore();
Expand All @@ -1469,6 +1469,10 @@ class CanvasGraphics {
this.ctx.restore();
this.transparentCanvas = null;
}
}

endDrawing() {
this.#restoreInitialState();

this.cachedCanvases.clear();
this.cachedPatterns.clear();
Expand Down Expand Up @@ -2972,20 +2976,25 @@ class CanvasGraphics {
}
}

beginAnnotations() {
this.save();
if (this.baseTransform) {
this.ctx.setTransform.apply(this.ctx, this.baseTransform);
}
}
beginAnnotations() {}

endAnnotations() {
this.restore();
}
endAnnotations() {}

beginAnnotation(id, rect, transform, matrix, hasOwnCanvas) {
// The annotations are drawn just after the page content.
// The page content drawing can potentially have set a transform,
// a clipping path, whatever...
// So in order to have something clean, we restore the initial state.
this.#restoreInitialState();
resetCtxToDefault(this.ctx, this.foregroundColor);

this.ctx.save();
this.save();

if (this.baseTransform) {
this.ctx.setTransform.apply(this.ctx, this.baseTransform);
}

if (Array.isArray(rect) && rect.length === 4) {
const width = rect[2] - rect[0];
const height = rect[3] - rect[1];
Expand Down Expand Up @@ -3022,8 +3031,8 @@ class CanvasGraphics {
this.annotationCanvasMap.set(id, canvas);
this.annotationCanvas.savedCtx = this.ctx;
this.ctx = context;
this.ctx.setTransform(scaleX, 0, 0, -scaleY, 0, height * scaleY);
addContextCurrentTransform(this.ctx);
this.ctx.setTransform(scaleX, 0, 0, -scaleY, 0, height * scaleY);

resetCtxToDefault(this.ctx, this.foregroundColor);
} else {
Expand All @@ -3050,7 +3059,6 @@ class CanvasGraphics {
delete this.annotationCanvas.savedCtx;
delete this.annotationCanvas;
}
this.restore();
}

paintImageMaskXObject(img) {
Expand Down
2 changes: 2 additions & 0 deletions test/pdfs/issue14105.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/mozilla/pdf.js/files/7291567/Stamp_Problem.pdf

8 changes: 8 additions & 0 deletions test/test_manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -6545,5 +6545,13 @@
"link": true,
"type": "eq",
"annotations": true
},
{ "id": "issue14105",
"file": "pdfs/issue14105.pdf",
"md5": "554174cd461180cbe46c137e846dd527",
"rounds": 1,
"link": true,
"type": "eq",
"annotations": true
}
]

0 comments on commit 96d0d22

Please sign in to comment.