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

Misc. follow-up fixes to PR 6299 (Convert canvas thumbnails to PNG) #6441

Merged
merged 3 commits into from
Sep 11, 2015
Merged
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
52 changes: 37 additions & 15 deletions web/pdf_thumbnail_view.js
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,10 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
this.canvas.height = 0;
delete this.canvas;
}
if (this.image) {
this.image.removeAttribute('src');
delete this.image;
}
},

update: function PDFThumbnailView_update(rotation) {
Expand All @@ -186,24 +190,48 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {

var ctx = canvas.getContext('2d');
var outputScale = getOutputScale(ctx);

canvas.width = (this.canvasWidth * outputScale.sx) | 0;
canvas.height = (this.canvasHeight * outputScale.sy) | 0;
canvas.style.width = this.canvasWidth + 'px';
canvas.style.height = this.canvasHeight + 'px';

if (!noCtxScale && outputScale.scaled) {
ctx.scale(outputScale.sx, outputScale.sy);
}
this.image = document.createElement('img');
this.image.id = this.renderingId;
this.image.style.height = canvas.style.height;
this.image.style.width = canvas.style.width;
this.image.className = 'thumbnailImage';
this.image.setAttribute('aria-label', mozL10n.get('thumb_page_canvas',

var image = document.createElement('img');
image.id = this.renderingId;

image.className = 'thumbnailImage';
image.setAttribute('aria-label', mozL10n.get('thumb_page_canvas',
{ page: this.id }, 'Thumbnail of Page {{page}}'));

image.style.width = canvas.style.width;
image.style.height = canvas.style.height;

this.image = image;
this.ring.appendChild(this.image);

return ctx;
},

/**
* @private
*/
_convertCanvasToImage: function PDFThumbnailView_convertCanvasToImage() {
if (!this.canvas) {
return;
}
this.image.src = this.canvas.toDataURL();

// Zeroing the width and height causes Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption.
this.canvas.width = 0;
this.canvas.height = 0;
delete this.canvas;
},

draw: function PDFThumbnailView_draw() {
if (this.renderingState !== RenderingStates.INITIAL) {
console.error('Must be in new state before drawing');
Expand Down Expand Up @@ -232,12 +260,8 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
rejectRenderPromise(error);
return;
}

self.image.src = self.canvas.toDataURL();
self.canvas.width = 0;
self.canvas.height = 0;
delete self.canvas;
self.renderingState = RenderingStates.FINISHED;
self._convertCanvasToImage();

if (!error) {
resolveRenderPromise(undefined);
Expand Down Expand Up @@ -295,6 +319,7 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
if (img.width <= 2 * canvas.width) {
ctx.drawImage(img, 0, 0, img.width, img.height,
0, 0, canvas.width, canvas.height);
this._convertCanvasToImage();
return;
}
// drawImage does an awful job of rescaling the image, doing it gradually.
Expand All @@ -319,10 +344,7 @@ var PDFThumbnailView = (function PDFThumbnailViewClosure() {
}
ctx.drawImage(reducedImage, 0, 0, reducedWidth, reducedHeight,
0, 0, canvas.width, canvas.height);
this.image.src = canvas.toDataURL();
this.canvas.width = 0;
this.canvas.height = 0;
delete this.canvas;
this._convertCanvasToImage();
}
};

Expand Down