diff --git a/examples/node/pdf2png/pdf2png.js b/examples/node/pdf2png/pdf2png.js index 468d1bab8..b595861d0 100644 --- a/examples/node/pdf2png/pdf2png.js +++ b/examples/node/pdf2png/pdf2png.js @@ -29,22 +29,22 @@ NodeCanvasFactory.prototype = { }; }, - reset: function NodeCanvasFactory_reset(canvasAndContextPair, width, height) { - assert(canvasAndContextPair.canvas, 'Canvas is not specified'); + reset: function NodeCanvasFactory_reset(canvasAndContext, width, height) { + assert(canvasAndContext.canvas, 'Canvas is not specified'); assert(width > 0 && height > 0, 'Invalid canvas size'); - canvasAndContextPair.canvas.width = width; - canvasAndContextPair.canvas.height = height; + canvasAndContext.canvas.width = width; + canvasAndContext.canvas.height = height; }, - destroy: function NodeCanvasFactory_destroy(canvasAndContextPair) { - assert(canvasAndContextPair.canvas, 'Canvas is not specified'); + destroy: function NodeCanvasFactory_destroy(canvasAndContext) { + assert(canvasAndContext.canvas, 'Canvas is not specified'); // Zeroing the width and height cause Firefox to release graphics // resources immediately, which can greatly reduce memory consumption. - canvasAndContextPair.canvas.width = 0; - canvasAndContextPair.canvas.height = 0; - canvasAndContextPair.canvas = null; - canvasAndContextPair.context = null; + canvasAndContext.canvas.width = 0; + canvasAndContext.canvas.height = 0; + canvasAndContext.canvas = null; + canvasAndContext.context = null; }, }; @@ -65,16 +65,16 @@ pdfjsLib.getDocument(rawData).then(function (pdfDocument) { // Render the page on a Node canvas with 100% scale. var viewport = page.getViewport(1.0); var canvasFactory = new NodeCanvasFactory(); - var canvasAndContextPair = canvasFactory.create(viewport.width, viewport.height); + var canvasAndContext = canvasFactory.create(viewport.width, viewport.height); var renderContext = { - canvasContext: canvasAndContextPair.context, + canvasContext: canvasAndContext.context, viewport: viewport, canvasFactory: canvasFactory }; page.render(renderContext).then(function () { // Convert the canvas to an image buffer. - image = canvasAndContextPair.canvas.toBuffer(); + var image = canvasAndContext.canvas.toBuffer(); fs.writeFile('output.png', image, function (error) { if (error) { console.error('Error: ' + error); diff --git a/src/display/dom_utils.js b/src/display/dom_utils.js index e89123dba..06dfafd76 100644 --- a/src/display/dom_utils.js +++ b/src/display/dom_utils.js @@ -20,91 +20,85 @@ import { var DEFAULT_LINK_REL = 'noopener noreferrer nofollow'; -function DOMCanvasFactory() {} -DOMCanvasFactory.prototype = { - create: function DOMCanvasFactory_create(width, height) { +class DOMCanvasFactory { + create(width, height) { assert(width > 0 && height > 0, 'invalid canvas size'); - var canvas = document.createElement('canvas'); - var context = canvas.getContext('2d'); + let canvas = document.createElement('canvas'); + let context = canvas.getContext('2d'); canvas.width = width; canvas.height = height; return { canvas, context, }; - }, + } - reset: function DOMCanvasFactory_reset(canvasAndContextPair, width, height) { - assert(canvasAndContextPair.canvas, 'canvas is not specified'); + reset(canvasAndContext, width, height) { + assert(canvasAndContext.canvas, 'canvas is not specified'); assert(width > 0 && height > 0, 'invalid canvas size'); - canvasAndContextPair.canvas.width = width; - canvasAndContextPair.canvas.height = height; - }, + canvasAndContext.canvas.width = width; + canvasAndContext.canvas.height = height; + } - destroy: function DOMCanvasFactory_destroy(canvasAndContextPair) { - assert(canvasAndContextPair.canvas, 'canvas is not specified'); + destroy(canvasAndContext) { + assert(canvasAndContext.canvas, 'canvas is not specified'); // Zeroing the width and height cause Firefox to release graphics // resources immediately, which can greatly reduce memory consumption. - canvasAndContextPair.canvas.width = 0; - canvasAndContextPair.canvas.height = 0; - canvasAndContextPair.canvas = null; - canvasAndContextPair.context = null; + canvasAndContext.canvas.width = 0; + canvasAndContext.canvas.height = 0; + canvasAndContext.canvas = null; + canvasAndContext.context = null; } -}; +} -var DOMCMapReaderFactory = (function DOMCMapReaderFactoryClosure() { - function DOMCMapReaderFactory(params) { - this.baseUrl = params.baseUrl || null; - this.isCompressed = params.isCompressed || false; +class DOMCMapReaderFactory { + constructor({ baseUrl = null, isCompressed = false, }) { + this.baseUrl = baseUrl; + this.isCompressed = isCompressed; } - DOMCMapReaderFactory.prototype = { - fetch(params) { - var name = params.name; - if (!name) { - return Promise.reject(new Error('CMap name must be specified.')); + fetch({ name, }) { + 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) => { - var url = this.baseUrl + name + (this.isCompressed ? '.bcmap' : ''); - - var request = new XMLHttpRequest(); - request.open('GET', url, true); - - if (this.isCompressed) { - request.responseType = 'arraybuffer'; + request.onreadystatechange = () => { + if (request.readyState !== XMLHttpRequest.DONE) { + return; } - request.onreadystatechange = () => { - if (request.readyState !== XMLHttpRequest.DONE) { + if (request.status === 200 || request.status === 0) { + 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; } - if (request.status === 200 || request.status === 0) { - var 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; - } - } - reject(new Error('Unable to load ' + - (this.isCompressed ? 'binary ' : '') + - 'CMap at: ' + url)); - }; + } + reject(new Error('Unable to load ' + + (this.isCompressed ? 'binary ' : '') + + 'CMap at: ' + url)); + }; - request.send(null); - }); - }, - }; - - return DOMCMapReaderFactory; -})(); + request.send(null); + }); + } +} /** * Optimised CSS custom property getter/setter. diff --git a/test/unit/test_utils.js b/test/unit/test_utils.js index 64280cac1..7dab9f1e4 100644 --- a/test/unit/test_utils.js +++ b/test/unit/test_utils.js @@ -16,20 +16,19 @@ import { CMapCompressionType } from '../../src/shared/util'; class NodeCMapReaderFactory { - constructor(params) { - this.baseUrl = params.baseUrl || null; - this.isCompressed = params.isCompressed || false; + constructor({ baseUrl = null, isCompressed = false, }) { + this.baseUrl = baseUrl; + this.isCompressed = isCompressed; } - fetch(params) { - var name = params.name; + fetch({ name, }) { if (!name) { return Promise.reject(new Error('CMap name must be specified.')); } 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) => { if (error || !data) { reject(new Error('Unable to load ' +