Merge pull request #620 from notmasteryet/imagecache

Cache DOM images; including smask as cachable image...
This commit is contained in:
Brendan Dahl 2011-10-07 16:58:19 -07:00
commit 9733a2b0f7

25
pdf.js
View File

@ -981,7 +981,7 @@ var ImagesLoader = (function imagesLoader() {
}, },
bind: function imagesLoaderBind(jpegStream) { bind: function imagesLoaderBind(jpegStream) {
if (jpegStream.loaded) if (jpegStream.loaded || jpegStream.onLoad)
return; return;
this.imageLoading(); this.imageLoading();
jpegStream.onLoad = this.imageLoaded.bind(this); jpegStream.onLoad = this.imageLoaded.bind(this);
@ -3402,8 +3402,8 @@ var XRef = (function xRefXRef() {
} else { } else {
e = parser.getObj(); e = parser.getObj();
} }
// Don't cache streams since they are mutable. // Don't cache streams since they are mutable (except images).
if (!isStream(e)) if (!isStream(e) || e.getImage)
this.cache[num] = e; this.cache[num] = e;
return e; return e;
} }
@ -4429,8 +4429,13 @@ var PartialEvaluator = (function partialEvaluator() {
xobj.dict.get('Resources'), fonts, xobj.dict.get('Resources'), fonts,
images); images);
} }
if (xobj instanceof JpegStream) if (isStream(xobj) && xobj.getImage) {
images.bind(xobj); // monitoring image load images.bind(xobj); // monitoring image load
var smask = xref.fetchIfRef(xobj.dict.get('SMask'));
if (isStream(smask) && smask.getImage)
images.bind(smask); // monitoring image load
}
} }
} else if (cmd == 'Tf') { // eagerly collect all fonts } else if (cmd == 'Tf') { // eagerly collect all fonts
var fontRes = resources.get('Font'); var fontRes = resources.get('Font');
@ -6533,6 +6538,18 @@ var PDFImage = (function pdfImage() {
var buf = new Uint8Array(width * height); var buf = new Uint8Array(width * height);
if (smask) { if (smask) {
if (smask.image.getImage) {
// smask is a DOM image
var tempCanvas = new ScratchCanvas(width, height);
var tempCtx = tempCanvas.getContext('2d');
var domImage = smask.image.getImage();
tempCtx.drawImage(domImage, 0, 0, domImage.width, domImage.height,
0, 0, width, height);
var data = tempCtx.getImageData(0, 0, width, height).data;
for (var i = 0, j = 0, ii = width * height; i < ii; ++i, j += 4)
buf[i] = data[j]; // getting first component value
return buf;
}
var sw = smask.width; var sw = smask.width;
var sh = smask.height; var sh = smask.height;
if (sw != this.width || sh != this.height) if (sw != this.width || sh != this.height)