Skip to content

Commit

Permalink
Apply transfer filters to any graphic commands
Browse files Browse the repository at this point in the history
  • Loading branch information
calixteman committed Mar 7, 2023
1 parent e11371c commit 0a5394e
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 7 deletions.
27 changes: 21 additions & 6 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ import {
TilingPattern,
} from "./pattern_helper.js";
import { convertBlackAndWhiteToRGBA } from "../shared/image_utils.js";
import { isNodeJS } from "../shared/is_node.js";

// <canvas> contexts store most of the state we need natively.
// However, PDF needs a bit more state, which we store here.
Expand Down Expand Up @@ -837,6 +838,7 @@ function copyCtxState(sourceCtx, destCtx) {
"miterLimit",
"globalCompositeOperation",
"font",
"filter",
];
for (const property of properties) {
if (sourceCtx[property] !== undefined) {
Expand All @@ -863,6 +865,7 @@ function resetCtxToDefault(ctx, foregroundColor) {
ctx.setLineDash([]);
ctx.lineDashOffset = 0;
}
ctx.filter = "none";
}

function composeSMaskBackdrop(bytes, r0, g0, b0) {
Expand Down Expand Up @@ -1576,9 +1579,12 @@ class CanvasGraphics {
this.checkSMaskState();
break;
case "TR":
this.current.transferMaps = this.filterFactory
? this.filterFactory.addFilter(value)
: value;
if (this.filterFactory) {
this.ctx.filter = this.current.transferMaps =
this.filterFactory.addFilter(value);
} else {
this.current.transferMaps = value;
}
break;
}
}
Expand Down Expand Up @@ -3025,7 +3031,7 @@ class CanvasGraphics {
}

applyTransferMapsToBitmap(imgData) {
if (!this.current.transferMaps) {
if (!this.current.transferMaps || this.current.transferMaps === "none") {
return imgData.bitmap;
}
const { bitmap, width, height } = imgData;
Expand All @@ -3037,7 +3043,7 @@ class CanvasGraphics {
const tmpCtx = tmpCanvas.context;
tmpCtx.filter = this.current.transferMaps;
tmpCtx.drawImage(bitmap, 0, 0);
tmpCtx.filter = "";
tmpCtx.filter = "none";

return tmpCanvas.canvas;
}
Expand All @@ -3051,6 +3057,15 @@ class CanvasGraphics {
const ctx = this.ctx;

this.save();

if (!isNodeJS) {
// The filter, if any, will be applied in applyTransferMapsToBitmap.
// It must be applied to the image before rescaling else some artifacts
// could appear.
// The final restore will reset it to its value.
ctx.filter = "none";
}

// scale the image to the unit square
ctx.scale(1 / width, -1 / height);

Expand Down Expand Up @@ -3106,7 +3121,7 @@ class CanvasGraphics {
const ctx = this.ctx;
let imgToPaint;
if (imgData.bitmap) {
imgToPaint = this.applyTransferMapsToBitmap(imgData);
imgToPaint = imgData.bitmap;
} else {
const w = imgData.width;
const h = imgData.height;
Expand Down
2 changes: 1 addition & 1 deletion src/display/display_utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class FilterFactory {

addFilter(maps) {
if (!maps) {
return "";
return "none";
}

// When a page is zoomed the page is re-drawn but the maps are likely
Expand Down
2 changes: 2 additions & 0 deletions test/pdfs/issue16114.pdf.link
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
https://github.com/mozilla/pdf.js/files/10896497/spider.pdf

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

0 comments on commit 0a5394e

Please sign in to comment.