From 9fd6cc7f1ec0c8334c2dc9f9632d1558257c0510 Mon Sep 17 00:00:00 2001
From: Fabian Lange <lange.fabian@gmail.com>
Date: Tue, 24 Jun 2014 12:12:17 +0200
Subject: [PATCH] Fixes CanvasPixelArray set polyfill for chrome < 21 (#4974)

---
 web/compatibility.js | 23 +++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/web/compatibility.js b/web/compatibility.js
index 400a940eb..3bb437780 100644
--- a/web/compatibility.js
+++ b/web/compatibility.js
@@ -481,9 +481,9 @@ if (typeof PDFJS === 'undefined') {
   }
 })();
 
-// TODO CanvasPixelArray is deprecated; use Uint8ClampedArray
-// once it's supported.
+// Support: IE<11, Chrome<21
 (function checkSetPresenceInImageData() {
+  // IE < 11 will use window.CanvasPixelArray which lacks set function.
   if (window.CanvasPixelArray) {
     if (typeof window.CanvasPixelArray.prototype.set !== 'function') {
       window.CanvasPixelArray.prototype.set = function(arr) {
@@ -492,6 +492,25 @@ if (typeof PDFJS === 'undefined') {
         }
       };
     }
+  } else {
+    // Chrome < 21 uses an inaccessible CanvasPixelArray prototype.
+    // Because we cannot feature detect it, we rely on user agent.
+    if (navigator.userAgent.indexOf('Chrom') >= 0) {
+      var versionMatch = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
+      if (versionMatch && parseInt(versionMatch[2]) < 21) {
+        var contextPrototype = window.CanvasRenderingContext2D.prototype;
+        contextPrototype._createImageData = contextPrototype.createImageData;
+        contextPrototype.createImageData = function(w, h) {
+          var imageData = this._createImageData(w, h);
+          imageData.data.set = function(arr) {
+            for (var i = 0, ii = this.length; i < ii; i++) {
+              this[i] = arr[i];
+            }
+          };
+          return imageData;
+        };
+      }
+    }
   }
 })();