From c2aa03e19427e3c9d7a18198c5c303e6247b385d Mon Sep 17 00:00:00 2001
From: Calixte Denizet <calixte.denizet@gmail.com>
Date: Mon, 18 Apr 2022 12:14:35 +0200
Subject: [PATCH] Fix clipping issue with pattern (follow-up of #14797)

---
 src/display/canvas.js         | 26 ++++++++++++--------------
 src/display/pattern_helper.js |  6 ++++++
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/src/display/canvas.js b/src/display/canvas.js
index ef1f1cdfd..aa076e63f 100644
--- a/src/display/canvas.js
+++ b/src/display/canvas.js
@@ -604,6 +604,15 @@ class CanvasExtraState {
     this.maxY = Math.max(this.maxY, y);
   }
 
+  updateRectMinMax(transform, rect) {
+    const p1 = Util.applyTransform(rect, transform);
+    const p2 = Util.applyTransform(rect.slice(2), transform);
+    this.minX = Math.min(this.minX, p1[0], p2[0]);
+    this.minY = Math.min(this.minY, p1[1], p2[1]);
+    this.maxX = Math.max(this.maxX, p1[0], p2[0]);
+    this.maxY = Math.max(this.maxY, p1[1], p2[1]);
+  }
+
   updateScalingPathMinMax(transform, minMax) {
     Util.scaleMinMax(transform, minMax);
     this.minX = Math.min(this.minX, minMax[0]);
@@ -621,8 +630,7 @@ class CanvasExtraState {
       minMax[3] = Math.max(minMax[3], box[1], box[3]);
       return;
     }
-    this.updatePathMinMax(transform, box[0], box[1]);
-    this.updatePathMinMax(transform, box[2], box[3]);
+    this.updateRectMinMax(transform, box);
   }
 
   getPathBoundingBox(pathType = PathType.FILL, transform = null) {
@@ -1794,8 +1802,7 @@ class CanvasGraphics {
             ctx.lineTo(x, yh);
           }
           if (!isScalingMatrix) {
-            current.updatePathMinMax(currentTransform, x, y);
-            current.updatePathMinMax(currentTransform, xw, yh);
+            current.updateRectMinMax(currentTransform, [x, y, xw, yh]);
           }
           ctx.closePath();
           break;
@@ -2622,16 +2629,7 @@ class CanvasGraphics {
       const width = bbox[2] - bbox[0];
       const height = bbox[3] - bbox[1];
       this.ctx.rect(bbox[0], bbox[1], width, height);
-      this.current.updatePathMinMax(
-        this.ctx.mozCurrentTransform,
-        bbox[0],
-        bbox[1]
-      );
-      this.current.updatePathMinMax(
-        this.ctx.mozCurrentTransform,
-        bbox[2],
-        bbox[3]
-      );
+      this.current.updateRectMinMax(this.ctx.mozCurrentTransform, bbox);
       this.clip();
       this.endPath();
     }
diff --git a/src/display/pattern_helper.js b/src/display/pattern_helper.js
index 42a62d29c..4bb923aec 100644
--- a/src/display/pattern_helper.js
+++ b/src/display/pattern_helper.js
@@ -619,6 +619,12 @@ class TilingPattern {
     const bboxWidth = x1 - x0;
     const bboxHeight = y1 - y0;
     graphics.ctx.rect(x0, y0, bboxWidth, bboxHeight);
+    graphics.current.updateRectMinMax(graphics.ctx.mozCurrentTransform, [
+      x0,
+      y0,
+      x1,
+      y1,
+    ]);
     graphics.clip();
     graphics.endPath();
   }