Merge pull request #10246 from Snuffleupagus/api-classes
Convert the remaining code in `src/display/api.js` to use ES6 classes
This commit is contained in:
commit
6ebdbb244f
@ -24,7 +24,8 @@ var DEFAULT_SCALE = 1.0;
|
|||||||
var container = document.getElementById('pageContainer');
|
var container = document.getElementById('pageContainer');
|
||||||
|
|
||||||
// Fetch the PDF document from the URL using promises.
|
// Fetch the PDF document from the URL using promises.
|
||||||
pdfjsLib.getDocument(DEFAULT_URL).then(function (doc) {
|
var loadingTask = pdfjsLib.getDocument(DEFAULT_URL);
|
||||||
|
loadingTask.promise.then(function(doc) {
|
||||||
// Use a promise to fetch and render the next page.
|
// Use a promise to fetch and render the next page.
|
||||||
var promise = Promise.resolve();
|
var promise = Promise.resolve();
|
||||||
|
|
||||||
|
@ -37,11 +37,12 @@ var SCALE = 1.0;
|
|||||||
var container = document.getElementById('pageContainer');
|
var container = document.getElementById('pageContainer');
|
||||||
|
|
||||||
// Loading document.
|
// Loading document.
|
||||||
pdfjsLib.getDocument({
|
var loadingTask = pdfjsLib.getDocument({
|
||||||
url: DEFAULT_URL,
|
url: DEFAULT_URL,
|
||||||
cMapUrl: CMAP_URL,
|
cMapUrl: CMAP_URL,
|
||||||
cMapPacked: CMAP_PACKED,
|
cMapPacked: CMAP_PACKED,
|
||||||
}).then(function(pdfDocument) {
|
});
|
||||||
|
loadingTask.promise.then(function(pdfDocument) {
|
||||||
// Document loaded, retrieving the page.
|
// Document loaded, retrieving the page.
|
||||||
return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
|
return pdfDocument.getPage(PAGE_TO_VIEW).then(function (pdfPage) {
|
||||||
// Creating the page view with default parameters.
|
// Creating the page view with default parameters.
|
||||||
|
@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Loading document.
|
// Loading document.
|
||||||
pdfjsLib.getDocument({
|
var loadingTask = pdfjsLib.getDocument({
|
||||||
url: DEFAULT_URL,
|
url: DEFAULT_URL,
|
||||||
cMapUrl: CMAP_URL,
|
cMapUrl: CMAP_URL,
|
||||||
cMapPacked: CMAP_PACKED,
|
cMapPacked: CMAP_PACKED,
|
||||||
}).then(function(pdfDocument) {
|
});
|
||||||
|
loadingTask.promise.then(function(pdfDocument) {
|
||||||
// Document loaded, specifying document for the viewer and
|
// Document loaded, specifying document for the viewer and
|
||||||
// the (optional) linkService.
|
// the (optional) linkService.
|
||||||
pdfViewer.setDocument(pdfDocument);
|
pdfViewer.setDocument(pdfDocument);
|
||||||
|
@ -60,11 +60,12 @@ document.addEventListener('pagesinit', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Loading document.
|
// Loading document.
|
||||||
pdfjsLib.getDocument({
|
var loadingTask = pdfjsLib.getDocument({
|
||||||
url: DEFAULT_URL,
|
url: DEFAULT_URL,
|
||||||
cMapUrl: CMAP_URL,
|
cMapUrl: CMAP_URL,
|
||||||
cMapPacked: CMAP_PACKED,
|
cMapPacked: CMAP_PACKED,
|
||||||
}).then(function(pdfDocument) {
|
});
|
||||||
|
loadingTask.promise.then(function(pdfDocument) {
|
||||||
// Document loaded, specifying document for the viewer and
|
// Document loaded, specifying document for the viewer and
|
||||||
// the (optional) linkService.
|
// the (optional) linkService.
|
||||||
pdfSinglePageViewer.setDocument(pdfDocument);
|
pdfSinglePageViewer.setDocument(pdfDocument);
|
||||||
|
@ -28,11 +28,12 @@
|
|||||||
//
|
//
|
||||||
// Asynchronous download PDF
|
// Asynchronous download PDF
|
||||||
//
|
//
|
||||||
pdfjsLib.getDocument(url).then(function getPdfHelloWorld(pdf) {
|
var loadingTask = pdfjsLib.getDocument(url);
|
||||||
|
loadingTask.promise.then(function(pdf) {
|
||||||
//
|
//
|
||||||
// Fetch the first page
|
// Fetch the first page
|
||||||
//
|
//
|
||||||
pdf.getPage(1).then(function getPageHelloWorld(page) {
|
pdf.getPage(1).then(function(page) {
|
||||||
var scale = 1.5;
|
var scale = 1.5;
|
||||||
var viewport = page.getViewport(scale);
|
var viewport = page.getViewport(scale);
|
||||||
|
|
||||||
|
@ -39,9 +39,10 @@
|
|||||||
|
|
||||||
// Opening PDF by passing its binary data as a string. It is still preferable
|
// Opening PDF by passing its binary data as a string. It is still preferable
|
||||||
// to use Uint8Array, but string or array-like structure will work too.
|
// to use Uint8Array, but string or array-like structure will work too.
|
||||||
pdfjsLib.getDocument({data: pdfData}).then(function getPdfHelloWorld(pdf) {
|
var loadingTask = pdfjsLib.getDocument({data: pdfData});
|
||||||
|
loadingTask.promise.then(function(pdf) {
|
||||||
// Fetch the first page.
|
// Fetch the first page.
|
||||||
pdf.getPage(1).then(function getPageHelloWorld(page) {
|
pdf.getPage(1).then(function(page) {
|
||||||
var scale = 1.5;
|
var scale = 1.5;
|
||||||
var viewport = page.getViewport(scale);
|
var viewport = page.getViewport(scale);
|
||||||
|
|
||||||
|
@ -117,7 +117,8 @@
|
|||||||
/**
|
/**
|
||||||
* Asynchronously downloads PDF.
|
* Asynchronously downloads PDF.
|
||||||
*/
|
*/
|
||||||
pdfjsLib.getDocument(url).then(function (pdfDoc_) {
|
var loadingTask = pdfjsLib.getDocument(url);
|
||||||
|
loadingTask.promise.then(function(pdfDoc_) {
|
||||||
pdfDoc = pdfDoc_;
|
pdfDoc = pdfDoc_;
|
||||||
document.getElementById('page_count').textContent = pdfDoc.numPages;
|
document.getElementById('page_count').textContent = pdfDoc.numPages;
|
||||||
|
|
||||||
|
@ -17,7 +17,8 @@ var pdfPath = process.argv[2] || '../../web/compressed.tracemonkey-pldi-09.pdf';
|
|||||||
|
|
||||||
// Will be using promises to load document, pages and misc data instead of
|
// Will be using promises to load document, pages and misc data instead of
|
||||||
// callback.
|
// callback.
|
||||||
pdfjsLib.getDocument(pdfPath).then(function (doc) {
|
var loadingTask = pdfjsLib.getDocument(pdfPath);
|
||||||
|
loadingTask.promise.then(function(doc) {
|
||||||
var numPages = doc.numPages;
|
var numPages = doc.numPages;
|
||||||
console.log('# Document Loaded');
|
console.log('# Document Loaded');
|
||||||
console.log('Number of Pages: ' + numPages);
|
console.log('Number of Pages: ' + numPages);
|
||||||
|
@ -57,7 +57,8 @@ var pdfURL = '../../../web/compressed.tracemonkey-pldi-09.pdf';
|
|||||||
var rawData = new Uint8Array(fs.readFileSync(pdfURL));
|
var rawData = new Uint8Array(fs.readFileSync(pdfURL));
|
||||||
|
|
||||||
// Load the PDF file.
|
// Load the PDF file.
|
||||||
pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
|
var loadingTask = pdfjsLib.getDocument(rawData);
|
||||||
|
loadingTask.promise.then(function(pdfDocument) {
|
||||||
console.log('# PDF document loaded.');
|
console.log('# PDF document loaded.');
|
||||||
|
|
||||||
// Get the first page.
|
// Get the first page.
|
||||||
@ -72,7 +73,8 @@ pdfjsLib.getDocument(rawData).then(function (pdfDocument) {
|
|||||||
canvasFactory: canvasFactory
|
canvasFactory: canvasFactory
|
||||||
};
|
};
|
||||||
|
|
||||||
page.render(renderContext).then(function () {
|
var renderTask = page.render(renderContext);
|
||||||
|
renderTask.promise.then(function() {
|
||||||
// Convert the canvas to an image buffer.
|
// Convert the canvas to an image buffer.
|
||||||
var image = canvasAndContext.canvas.toBuffer();
|
var image = canvasAndContext.canvas.toBuffer();
|
||||||
fs.writeFile('output.png', image, function (error) {
|
fs.writeFile('output.png', image, function (error) {
|
||||||
|
@ -84,11 +84,12 @@ function writeSvgToFile(svgElement, filePath) {
|
|||||||
|
|
||||||
// Will be using promises to load document, pages and misc data instead of
|
// Will be using promises to load document, pages and misc data instead of
|
||||||
// callback.
|
// callback.
|
||||||
pdfjsLib.getDocument({
|
var loadingTask = pdfjsLib.getDocument({
|
||||||
data: data,
|
data: data,
|
||||||
// Try to export JPEG images directly if they don't need any further processing.
|
// Try to export JPEG images directly if they don't need any further processing.
|
||||||
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY
|
nativeImageDecoderSupport: pdfjsLib.NativeImageDecoding.DISPLAY
|
||||||
}).then(function (doc) {
|
});
|
||||||
|
loadingTask.promise.then(function(doc) {
|
||||||
var numPages = doc.numPages;
|
var numPages = doc.numPages;
|
||||||
console.log('# Document Loaded');
|
console.log('# Document Loaded');
|
||||||
console.log('Number of Pages: ' + numPages);
|
console.log('Number of Pages: ' + numPages);
|
||||||
|
@ -51,11 +51,12 @@ document.addEventListener('pagesinit', function () {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Loading document.
|
// Loading document.
|
||||||
pdfjsLib.getDocument({
|
var loadingTask = pdfjsLib.getDocument({
|
||||||
url: DEFAULT_URL,
|
url: DEFAULT_URL,
|
||||||
cMapUrl: CMAP_URL,
|
cMapUrl: CMAP_URL,
|
||||||
cMapPacked: CMAP_PACKED,
|
cMapPacked: CMAP_PACKED,
|
||||||
}).then(function(pdfDocument) {
|
});
|
||||||
|
loadingTask.promise.then(function(pdfDocument) {
|
||||||
// Document loaded, specifying document for the viewer and
|
// Document loaded, specifying document for the viewer and
|
||||||
// the (optional) linkService.
|
// the (optional) linkService.
|
||||||
pdfViewer.setDocument(pdfDocument);
|
pdfViewer.setDocument(pdfDocument);
|
||||||
|
@ -49,7 +49,8 @@ function buildSVG(viewport, textContent) {
|
|||||||
|
|
||||||
function pageLoaded() {
|
function pageLoaded() {
|
||||||
// Loading document and page text content
|
// Loading document and page text content
|
||||||
pdfjsLib.getDocument({url: PDF_PATH}).then(function (pdfDocument) {
|
var loadingTask = pdfjsLib.getDocument({url: PDF_PATH});
|
||||||
|
loadingTask.promise.then(function(pdfDocument) {
|
||||||
pdfDocument.getPage(PAGE_NUMBER).then(function (page) {
|
pdfDocument.getPage(PAGE_NUMBER).then(function (page) {
|
||||||
var viewport = page.getViewport(PAGE_SCALE);
|
var viewport = page.getViewport(PAGE_SCALE);
|
||||||
page.getTextContent().then(function (textContent) {
|
page.getTextContent().then(function (textContent) {
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -726,7 +726,7 @@ var CanvasGraphics = (function CanvasGraphicsClosure() {
|
|||||||
|
|
||||||
CanvasGraphics.prototype = {
|
CanvasGraphics.prototype = {
|
||||||
|
|
||||||
beginDrawing({ transform, viewport, transparency,
|
beginDrawing({ transform, viewport, transparency = false,
|
||||||
background = null, }) {
|
background = null, }) {
|
||||||
// For pdfs that use blend modes we have to clear the canvas else certain
|
// For pdfs that use blend modes we have to clear the canvas else certain
|
||||||
// blend modes can look wrong since we'd be blending with a white
|
// blend modes can look wrong since we'd be blending with a white
|
||||||
|
@ -358,7 +358,7 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
|
|||||||
|
|
||||||
let absoluteUrl = new URL(task.file, window.location).href;
|
let absoluteUrl = new URL(task.file, window.location).href;
|
||||||
try {
|
try {
|
||||||
pdfjsLib.getDocument({
|
const loadingTask = pdfjsLib.getDocument({
|
||||||
url: absoluteUrl,
|
url: absoluteUrl,
|
||||||
password: task.password,
|
password: task.password,
|
||||||
nativeImageDecoderSupport: task.nativeImageDecoderSupport,
|
nativeImageDecoderSupport: task.nativeImageDecoderSupport,
|
||||||
@ -367,7 +367,8 @@ var Driver = (function DriverClosure() { // eslint-disable-line no-unused-vars
|
|||||||
disableRange: task.disableRange,
|
disableRange: task.disableRange,
|
||||||
disableAutoFetch: !task.enableAutoFetch,
|
disableAutoFetch: !task.enableAutoFetch,
|
||||||
pdfBug: true,
|
pdfBug: true,
|
||||||
}).then((doc) => {
|
});
|
||||||
|
loadingTask.promise.then((doc) => {
|
||||||
task.pdfDoc = doc;
|
task.pdfDoc = doc;
|
||||||
this._nextPage(task, failure);
|
this._nextPage(task, failure);
|
||||||
}, (err) => {
|
}, (err) => {
|
||||||
|
@ -1332,26 +1332,23 @@ describe('api', function() {
|
|||||||
|
|
||||||
// Render the first page of the given PDF file.
|
// Render the first page of the given PDF file.
|
||||||
// Fulfills the promise with the base64-encoded version of the PDF.
|
// Fulfills the promise with the base64-encoded version of the PDF.
|
||||||
function renderPDF(filename) {
|
async function renderPDF(filename) {
|
||||||
var loadingTask = getDocument(filename);
|
const loadingTask = getDocument(filename);
|
||||||
loadingTasks.push(loadingTask);
|
loadingTasks.push(loadingTask);
|
||||||
return loadingTask.promise
|
const pdf = await loadingTask.promise;
|
||||||
.then(function(pdf) {
|
|
||||||
pdfDocuments.push(pdf);
|
pdfDocuments.push(pdf);
|
||||||
return pdf.getPage(1);
|
const page = await pdf.getPage(1);
|
||||||
}).then(function(page) {
|
const viewport = page.getViewport(1.2);
|
||||||
var viewport = page.getViewport(1.2);
|
const canvasAndCtx = CanvasFactory.create(viewport.width,
|
||||||
var canvasAndCtx = CanvasFactory.create(viewport.width,
|
|
||||||
viewport.height);
|
viewport.height);
|
||||||
return page.render({
|
const renderTask = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
viewport,
|
viewport,
|
||||||
}).then(function() {
|
});
|
||||||
var data = canvasAndCtx.canvas.toDataURL();
|
await renderTask.promise;
|
||||||
|
const data = canvasAndCtx.canvas.toDataURL();
|
||||||
CanvasFactory.destroy(canvasAndCtx);
|
CanvasFactory.destroy(canvasAndCtx);
|
||||||
return data;
|
return data;
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
afterEach(function(done) {
|
afterEach(function(done) {
|
||||||
|
@ -48,9 +48,7 @@ describe('custom canvas rendering', function() {
|
|||||||
}).then(function(data) {
|
}).then(function(data) {
|
||||||
page = data;
|
page = data;
|
||||||
done();
|
done();
|
||||||
}).catch(function (reason) {
|
}).catch(done.fail);
|
||||||
done.fail(reason);
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(function(done) {
|
afterAll(function(done) {
|
||||||
@ -66,20 +64,16 @@ describe('custom canvas rendering', function() {
|
|||||||
var viewport = page.getViewport(1);
|
var viewport = page.getViewport(1);
|
||||||
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
||||||
|
|
||||||
page.render({
|
const renderTask = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
viewport,
|
viewport,
|
||||||
}).then(function() {
|
|
||||||
var { r, g, b, a, } = getTopLeftPixel(canvasAndCtx.context);
|
|
||||||
CanvasFactory.destroy(canvasAndCtx);
|
|
||||||
expect(r).toEqual(255);
|
|
||||||
expect(g).toEqual(255);
|
|
||||||
expect(b).toEqual(255);
|
|
||||||
expect(a).toEqual(255);
|
|
||||||
done();
|
|
||||||
}).catch(function (reason) {
|
|
||||||
done(reason);
|
|
||||||
});
|
});
|
||||||
|
renderTask.promise.then(function() {
|
||||||
|
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual(
|
||||||
|
{ r: 255, g: 255, b: 255, a: 255, });
|
||||||
|
CanvasFactory.destroy(canvasAndCtx);
|
||||||
|
done();
|
||||||
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('renders to canvas with a custom background', function(done) {
|
it('renders to canvas with a custom background', function(done) {
|
||||||
@ -89,20 +83,16 @@ describe('custom canvas rendering', function() {
|
|||||||
var viewport = page.getViewport(1);
|
var viewport = page.getViewport(1);
|
||||||
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
var canvasAndCtx = CanvasFactory.create(viewport.width, viewport.height);
|
||||||
|
|
||||||
page.render({
|
const renderTask = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
viewport,
|
viewport,
|
||||||
background: 'rgba(255,0,0,1.0)',
|
background: 'rgba(255,0,0,1.0)',
|
||||||
}).then(function() {
|
|
||||||
var { r, g, b, a, } = getTopLeftPixel(canvasAndCtx.context);
|
|
||||||
CanvasFactory.destroy(canvasAndCtx);
|
|
||||||
expect(r).toEqual(255);
|
|
||||||
expect(g).toEqual(0);
|
|
||||||
expect(b).toEqual(0);
|
|
||||||
expect(a).toEqual(255);
|
|
||||||
done();
|
|
||||||
}).catch(function (reason) {
|
|
||||||
done(reason);
|
|
||||||
});
|
});
|
||||||
|
renderTask.promise.then(function() {
|
||||||
|
expect(getTopLeftPixel(canvasAndCtx.context)).toEqual(
|
||||||
|
{ r: 255, g: 0, b: 0, a: 255, });
|
||||||
|
CanvasFactory.destroy(canvasAndCtx);
|
||||||
|
done();
|
||||||
|
}).catch(done.fail);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
Loading…
Reference in New Issue
Block a user