Browse Source

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

Convert the `DOMCMapReaderFactory` and `DOMCanvasFactory` to ES6 classes
Jonas Jenwald 8 years ago committed by GitHub
parent
commit
028d3421ac
  1. 26
      examples/node/pdf2png/pdf2png.js
  2. 60
      src/display/dom_utils.js
  3. 13
      test/unit/test_utils.js

26
examples/node/pdf2png/pdf2png.js

@ -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);

60
src/display/dom_utils.js

@ -20,54 +20,51 @@ 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) {
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 request = new XMLHttpRequest(); let request = new XMLHttpRequest();
request.open('GET', url, true); request.open('GET', url, true);
if (this.isCompressed) { if (this.isCompressed) {
@ -78,7 +75,7 @@ var DOMCMapReaderFactory = (function DOMCMapReaderFactoryClosure() {
return; return;
} }
if (request.status === 200 || request.status === 0) { if (request.status === 200 || request.status === 0) {
var data; let data;
if (this.isCompressed && request.response) { if (this.isCompressed && request.response) {
data = new Uint8Array(request.response); data = new Uint8Array(request.response);
} else if (!this.isCompressed && request.responseText) { } else if (!this.isCompressed && request.responseText) {
@ -100,11 +97,8 @@ var DOMCMapReaderFactory = (function DOMCMapReaderFactoryClosure() {
request.send(null); request.send(null);
}); });
}, }
}; }
return DOMCMapReaderFactory;
})();
/** /**
* Optimised CSS custom property getter/setter. * Optimised CSS custom property getter/setter.

13
test/unit/test_utils.js

@ -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 ' +

Loading…
Cancel
Save