Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't use an intermediate canvas when rendering a tiling pattern bigger than the rectangle to fill #18815

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 33 additions & 4 deletions src/display/canvas.js
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ class CanvasExtraState {
// Default fore and background colors
this.fillColor = "#000000";
this.strokeColor = "#000000";
this.tilingPatternRectangle = null;
this.patternFill = false;
// Note: fill alpha applies to all non-stroking operations
this.fillAlpha = 1;
Expand All @@ -497,6 +498,7 @@ class CanvasExtraState {
clone() {
const clone = Object.create(this);
clone.clipBox = this.clipBox.slice();
clone.tilingPatternRectangle = this.tilingPatternRectangle?.slice();
return clone;
}

Expand Down Expand Up @@ -1610,8 +1612,7 @@ class CanvasGraphics {

// Path
constructPath(ops, args, minMax) {
const ctx = this.ctx;
const current = this.current;
const { ctx, current } = this;
let x = current.x,
y = current.y;
let startX, startY;
Expand Down Expand Up @@ -1649,6 +1650,13 @@ class CanvasGraphics {
if (!isScalingMatrix) {
current.updateRectMinMax(currentTransform, [x, y, xw, yh]);
}
const dims = current.tilingPatternDims;
if (dims && isNaN(dims[0]) && x === 0 && y === 0) {
dims[0] = width;
dims[1] = height;
} else {
current.tilingPatternDims = null;
}
ctx.closePath();
break;
case OPS.moveTo:
Expand All @@ -1658,6 +1666,7 @@ class CanvasGraphics {
if (!isScalingMatrix) {
current.updatePathMinMax(currentTransform, x, y);
}
current.tilingPatternDims = null;
break;
case OPS.lineTo:
x = args[j++];
Expand All @@ -1666,6 +1675,7 @@ class CanvasGraphics {
if (!isScalingMatrix) {
current.updatePathMinMax(currentTransform, x, y);
}
current.tilingPatternDims = null;
break;
case OPS.curveTo:
startX = x;
Expand Down Expand Up @@ -1693,6 +1703,7 @@ class CanvasGraphics {
minMaxForBezier
);
j += 6;
current.tilingPatternDims = null;
break;
case OPS.curveTo2:
startX = x;
Expand Down Expand Up @@ -1720,6 +1731,7 @@ class CanvasGraphics {
x = args[j + 2];
y = args[j + 3];
j += 4;
current.tilingPatternDims = null;
break;
case OPS.curveTo3:
startX = x;
Expand All @@ -1740,6 +1752,7 @@ class CanvasGraphics {
minMaxForBezier
);
j += 4;
current.tilingPatternDims = null;
break;
case OPS.closePath:
ctx.closePath();
Expand Down Expand Up @@ -1796,8 +1809,22 @@ class CanvasGraphics {
const fillColor = this.current.fillColor;
const isPatternFill = this.current.patternFill;
let needRestore = false;
const intersect = this.current.getClippedPathBoundingBox();

if (isPatternFill) {
const dims = this.current.tilingPatternDims;
if (dims && fillColor.canSkipPatternCanvas(dims)) {
// A rectangle with its origin at (0, 0) is filled with a tiling
// pattern. If the dimensions of the rectangle are smaller than the
// tile's ones we can directly draw the tile without having to use
// a pattern.
fillColor.drawPattern(this);
if (consumePath) {
this.consumePath(intersect);
}
this.current.tilingPatternDims = null;
return;
}
ctx.save();
ctx.fillStyle = fillColor.getPattern(
ctx,
Expand All @@ -1808,7 +1835,6 @@ class CanvasGraphics {
needRestore = true;
}

const intersect = this.current.getClippedPathBoundingBox();
if (this.contentVisible && intersect !== null) {
if (this.pendingEOFill) {
ctx.fill("evenodd");
Expand Down Expand Up @@ -2381,8 +2407,11 @@ class CanvasGraphics {
}

setFillColorN() {
this.current.fillColor = this.getColorN_Pattern(arguments);
const pattern = (this.current.fillColor =
this.getColorN_Pattern(arguments));
this.current.patternFill = true;
this.current.tilingPatternDims =
pattern instanceof TilingPattern ? [NaN, NaN] : null;
}

setStrokeRGBColor(r, g, b) {
Expand Down
15 changes: 15 additions & 0 deletions src/display/pattern_helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -468,6 +468,21 @@ class TilingPattern {
this.baseTransform = baseTransform;
}

canSkipPatternCanvas([width, height]) {
return (
width <= this.xstep + 1e-6 &&
height <= this.ystep + 1e-6 &&
Util.isIdentityMatrix(this.matrix)
);
}

drawPattern(owner) {
owner.save();
owner.ctx.clip();
owner.executeOperatorList(this.operatorList);
owner.restore();
}

createPatternCanvas(owner) {
const {
bbox,
Expand Down
11 changes: 11 additions & 0 deletions src/shared/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -659,6 +659,17 @@ class Util {
return `#${hexNumbers[r]}${hexNumbers[g]}${hexNumbers[b]}`;
}

static isIdentityMatrix(m) {
return (
Math.abs(m[0] - 1) <= 1e-6 &&
Math.abs(m[1]) <= 1e-6 &&
Math.abs(m[2]) <= 1e-6 &&
Math.abs(m[3] - 1) <= 1e-6 &&
Math.abs(m[4]) <= 1e-6 &&
Math.abs(m[5]) <= 1e-6
);
}

// Apply a scaling matrix to some min/max values.
// If a scaling factor is negative then min and max must be
// swapped.
Expand Down