Merge pull request #8398 from Snuffleupagus/es6-dom-utils-factories

Convert the `DOMCMapReaderFactory` and `DOMCanvasFactory` to ES6 classes
This commit is contained in:
Jonas Jenwald 2017-05-11 23:10:44 +02:00 committed by GitHub
commit 028d3421ac
3 changed files with 77 additions and 84 deletions

View File

@ -29,22 +29,22 @@ NodeCanvasFactory.prototype = {
}; };
}, },
reset: function NodeCanvasFactory_reset(canvasAndContextPair, width, height) { reset: function NodeCanvasFactory_reset(canvasAndContext, width, height) {
assert(canvasAndContextPair.canvas, 'Canvas is not specified'); assert(canvasAndContext.canvas, 'Canvas is not specified');
assert(width > 0 && height > 0, 'Invalid canvas size'); assert(width > 0 && height > 0, 'Invalid canvas size');
canvasAndContextPair.canvas.width = width; canvasAndContext.canvas.width = width;
canvasAndContextPair.canvas.height = height; canvasAndContext.canvas.height = height;
}, },
destroy: function NodeCanvasFactory_destroy(canvasAndContextPair) { destroy: function NodeCanvasFactory_destroy(canvasAndContext) {
assert(canvasAndContextPair.canvas, 'Canvas is not specified'); assert(canvasAndContext.canvas, 'Canvas is not specified');
// Zeroing the width and height cause Firefox to release graphics // Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption. // resources immediately, which can greatly reduce memory consumption.
canvasAndContextPair.canvas.width = 0; canvasAndContext.canvas.width = 0;
canvasAndContextPair.canvas.height = 0; canvasAndContext.canvas.height = 0;
canvasAndContextPair.canvas = null; canvasAndContext.canvas = null;
canvasAndContextPair.context = null; canvasAndContext.context = null;
}, },
}; };
@ -65,16 +65,16 @@ pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
// Render the page on a Node canvas with 100% scale. // Render the page on a Node canvas with 100% scale.
var viewport = page.getViewport(1.0); var viewport = page.getViewport(1.0);
var canvasFactory = new NodeCanvasFactory(); var canvasFactory = new NodeCanvasFactory();
var canvasAndContextPair = canvasFactory.create(viewport.width, viewport.height); var canvasAndContext = canvasFactory.create(viewport.width, viewport.height);
var renderContext = { var renderContext = {
canvasContext: canvasAndContextPair.context, canvasContext: canvasAndContext.context,
viewport: viewport, viewport: viewport,
canvasFactory: canvasFactory canvasFactory: canvasFactory
}; };
page.render(renderContext).then(function () { page.render(renderContext).then(function () {
// Convert the canvas to an image buffer. // Convert the canvas to an image buffer.
image = canvasAndContextPair.canvas.toBuffer(); var image = canvasAndContext.canvas.toBuffer();
fs.writeFile('output.png', image, function (error) { fs.writeFile('output.png', image, function (error) {
if (error) { if (error) {
console.error('Error: ' + error); console.error('Error: ' + error);

View File

@ -20,91 +20,85 @@ import {
var DEFAULT_LINK_REL = 'noopener noreferrer nofollow'; var DEFAULT_LINK_REL = 'noopener noreferrer nofollow';
function DOMCanvasFactory() {} class DOMCanvasFactory {
DOMCanvasFactory.prototype = { create(width, height) {
create: function DOMCanvasFactory_create(width, height) {
assert(width > 0 && height > 0, 'invalid canvas size'); assert(width > 0 && height > 0, 'invalid canvas size');
var canvas = document.createElement('canvas'); let canvas = document.createElement('canvas');
var context = canvas.getContext('2d'); let context = canvas.getContext('2d');
canvas.width = width; canvas.width = width;
canvas.height = height; canvas.height = height;
return { return {
canvas, canvas,
context, context,
}; };
}, }
reset: function DOMCanvasFactory_reset(canvasAndContextPair, width, height) { reset(canvasAndContext, width, height) {
assert(canvasAndContextPair.canvas, 'canvas is not specified'); assert(canvasAndContext.canvas, 'canvas is not specified');
assert(width > 0 && height > 0, 'invalid canvas size'); assert(width > 0 && height > 0, 'invalid canvas size');
canvasAndContextPair.canvas.width = width; canvasAndContext.canvas.width = width;
canvasAndContextPair.canvas.height = height; canvasAndContext.canvas.height = height;
}, }
destroy: function DOMCanvasFactory_destroy(canvasAndContextPair) { destroy(canvasAndContext) {
assert(canvasAndContextPair.canvas, 'canvas is not specified'); assert(canvasAndContext.canvas, 'canvas is not specified');
// Zeroing the width and height cause Firefox to release graphics // Zeroing the width and height cause Firefox to release graphics
// resources immediately, which can greatly reduce memory consumption. // resources immediately, which can greatly reduce memory consumption.
canvasAndContextPair.canvas.width = 0; canvasAndContext.canvas.width = 0;
canvasAndContextPair.canvas.height = 0; canvasAndContext.canvas.height = 0;
canvasAndContextPair.canvas = null; canvasAndContext.canvas = null;
canvasAndContextPair.context = null; canvasAndContext.context = null;
} }
}; }
var DOMCMapReaderFactory = (function DOMCMapReaderFactoryClosure() { class DOMCMapReaderFactory {
function DOMCMapReaderFactory(params) { constructor({ baseUrl = null, isCompressed = false, }) {
this.baseUrl = params.baseUrl || null; this.baseUrl = baseUrl;
this.isCompressed = params.isCompressed || false; this.isCompressed = isCompressed;
} }
DOMCMapReaderFactory.prototype = { fetch({ name, }) {
fetch(params) { if (!name) {
var name = params.name; return Promise.reject(new Error('CMap name must be specified.'));
if (!name) { }
return Promise.reject(new Error('CMap name must be specified.')); return new Promise((resolve, reject) => {
let url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
let request = new XMLHttpRequest();
request.open('GET', url, true);
if (this.isCompressed) {
request.responseType = 'arraybuffer';
} }
return new Promise((resolve, reject) => { request.onreadystatechange = () => {
var url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : ''); if (request.readyState !== XMLHttpRequest.DONE) {
return;
var request = new XMLHttpRequest();
request.open('GET', url, true);
if (this.isCompressed) {
request.responseType = 'arraybuffer';
} }
request.onreadystatechange = () => { if (request.status === 200 || request.status === 0) {
if (request.readyState !== XMLHttpRequest.DONE) { let data;
if (this.isCompressed && request.response) {
data = new Uint8Array(request.response);
} else if (!this.isCompressed && request.responseText) {
data = stringToBytes(request.responseText);
}
if (data) {
resolve({
cMapData: data,
compressionType: this.isCompressed ?
CMapCompressionType.BINARY : CMapCompressionType.NONE,
});
return; return;
} }
if (request.status === 200 || request.status === 0) { }
var data; reject(new Error('Unable to load ' +
if (this.isCompressed && request.response) { (this.isCompressed ? 'binary ' : '') +
data = new Uint8Array(request.response); 'CMap at: ' + url));
} else if (!this.isCompressed && request.responseText) { };
data = stringToBytes(request.responseText);
}
if (data) {
resolve({
cMapData: data,
compressionType: this.isCompressed ?
CMapCompressionType.BINARY : CMapCompressionType.NONE,
});
return;
}
}
reject(new Error('Unable to load ' +
(this.isCompressed ? 'binary ' : '') +
'CMap at: ' + url));
};
request.send(null); request.send(null);
}); });
}, }
}; }
return DOMCMapReaderFactory;
})();
/** /**
* Optimised CSS custom property getter/setter. * Optimised CSS custom property getter/setter.

View File

@ -16,20 +16,19 @@
import { CMapCompressionType } from '../../src/shared/util'; import { CMapCompressionType } from '../../src/shared/util';
class NodeCMapReaderFactory { class NodeCMapReaderFactory {
constructor(params) { constructor({ baseUrl = null, isCompressed = false, }) {
this.baseUrl = params.baseUrl || null; this.baseUrl = baseUrl;
this.isCompressed = params.isCompressed || false; this.isCompressed = isCompressed;
} }
fetch(params) { fetch({ name, }) {
var name = params.name;
if (!name) { if (!name) {
return Promise.reject(new Error('CMap name must be specified.')); return Promise.reject(new Error('CMap name must be specified.'));
} }
return new Promise((resolve, reject) => { return new Promise((resolve, reject) => {
var url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : ''); let url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : '');
var fs = require('fs'); let fs = require('fs');
fs.readFile(url, (error, data) => { fs.readFile(url, (error, data) => {
if (error || !data) { if (error || !data) {
reject(new Error('Unable to load ' + reject(new Error('Unable to load ' +