diff --git a/pdf.js b/pdf.js index e1fce54ce..9739128f9 100644 --- a/pdf.js +++ b/pdf.js @@ -5112,15 +5112,18 @@ var CanvasGraphics = (function() { var tmpCanvas = new this.ScratchCanvas(w, h); var tmpCtx = tmpCanvas.getContext('2d'); + if (imageObj.imageMask) { + var fillColor = this.current.fillColor; + tmpCtx.fillStyle = (fillColor && fillColor.type === 'Pattern') ? + fillColor.getPattern(tmpCtx) : fillColor; + tmpCtx.fillRect(0, 0, w, h); + } var imgData = tmpCtx.getImageData(0, 0, w, h); var pixels = imgData.data; if (imageObj.imageMask) { - var inverseDecode = imageObj.decode && imageObj.decode[0] > 0; - // TODO fillColor pattern support - var fillColor = this.current.fillColor; - imageObj.fillUsingStencilMask(pixels, fillColor, - inverseDecode); + var inverseDecode = !!imageObj.decode && imageObj.decode[0] > 0; + imageObj.applyStencilMask(pixels, inverseDecode); } else imageObj.fillRgbaBuffer(pixels); @@ -6000,26 +6003,27 @@ var PDFImage = (function() { } return buf; }, - fillUsingStencilMask: function fillUsingStencilMask(buffer, - cssRgb, inverseDecode) { - var m = /rgb\((\d+),(\d+),(\d+)\)/.exec(cssRgb); // parse CSS color - var r = m[1] | 0, g = m[2] | 0, b = m[3] | 0; - var bufferLength = this.width * this.height; - var imgArray = this.image.getBytes((bufferLength + 7) >> 3); - var i, mask; - var bufferPos = 0, imgArrayPos = 0; - for (i = 0; i < bufferLength; i++) { - var buf = imgArray[imgArrayPos++]; - for (mask = 128; mask > 0; mask >>= 1) { - if (!(buf & mask) != inverseDecode) { - buffer[bufferPos++] = r; - buffer[bufferPos++] = g; - buffer[bufferPos++] = b; - buffer[bufferPos++] = 255; - } else { - buffer[bufferPos + 3] = 0; - bufferPos += 4; + applyStencilMask: function fillUsingStencilMask(buffer, + inverseDecode) { + var width = this.width, height = this.height; + var bitStrideLength = (width + 7) >> 3; + var imgArray = this.image.getBytes(bitStrideLength * height); + var imgArrayPos = 0; + var i, j, mask, buf; + // removing making non-masked pixels transparent + var bufferPos = 3; // alpha component offset + for (i = 0; i < height; i++) { + mask = 0; + for (j = 0; j < width; j++) { + if (!mask) { + buf = imgArray[imgArrayPos++]; + mask = 128; } + if (!(buf & mask) == inverseDecode) { + buffer[bufferPos] = 0; + } + bufferPos += 4; + mask >>= 1; } } },