Merge pull request #16100 from Snuffleupagus/getDocument-canvasFactory
[api-minor] Move the `canvasFactory` option into `getDocument`
This commit is contained in:
commit
9640add1f7
@ -58,6 +58,8 @@ const CMAP_PACKED = true;
|
|||||||
const STANDARD_FONT_DATA_URL =
|
const STANDARD_FONT_DATA_URL =
|
||||||
"../../../node_modules/pdfjs-dist/standard_fonts/";
|
"../../../node_modules/pdfjs-dist/standard_fonts/";
|
||||||
|
|
||||||
|
const canvasFactory = new NodeCanvasFactory();
|
||||||
|
|
||||||
// Loading file from file system into typed array.
|
// Loading file from file system into typed array.
|
||||||
const pdfPath =
|
const pdfPath =
|
||||||
process.argv[2] || "../../../web/compressed.tracemonkey-pldi-09.pdf";
|
process.argv[2] || "../../../web/compressed.tracemonkey-pldi-09.pdf";
|
||||||
@ -69,6 +71,7 @@ const loadingTask = pdfjsLib.getDocument({
|
|||||||
cMapUrl: CMAP_URL,
|
cMapUrl: CMAP_URL,
|
||||||
cMapPacked: CMAP_PACKED,
|
cMapPacked: CMAP_PACKED,
|
||||||
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
standardFontDataUrl: STANDARD_FONT_DATA_URL,
|
||||||
|
canvasFactory,
|
||||||
});
|
});
|
||||||
|
|
||||||
(async function () {
|
(async function () {
|
||||||
@ -79,7 +82,6 @@ const loadingTask = pdfjsLib.getDocument({
|
|||||||
const page = await pdfDocument.getPage(1);
|
const page = await pdfDocument.getPage(1);
|
||||||
// Render the page on a Node canvas with 100% scale.
|
// Render the page on a Node canvas with 100% scale.
|
||||||
const viewport = page.getViewport({ scale: 1.0 });
|
const viewport = page.getViewport({ scale: 1.0 });
|
||||||
const canvasFactory = new NodeCanvasFactory();
|
|
||||||
const canvasAndContext = canvasFactory.create(
|
const canvasAndContext = canvasFactory.create(
|
||||||
viewport.width,
|
viewport.width,
|
||||||
viewport.height
|
viewport.height
|
||||||
@ -87,7 +89,6 @@ const loadingTask = pdfjsLib.getDocument({
|
|||||||
const renderContext = {
|
const renderContext = {
|
||||||
canvasContext: canvasAndContext.context,
|
canvasContext: canvasAndContext.context,
|
||||||
viewport,
|
viewport,
|
||||||
canvasFactory,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const renderTask = page.render(renderContext);
|
const renderTask = page.render(renderContext);
|
||||||
|
@ -230,6 +230,8 @@ if (typeof PDFJSDev === "undefined" || !PDFJSDev.test("PRODUCTION")) {
|
|||||||
* disabling of pre-fetching to work correctly.
|
* disabling of pre-fetching to work correctly.
|
||||||
* @property {boolean} [pdfBug] - Enables special hooks for debugging PDF.js
|
* @property {boolean} [pdfBug] - Enables special hooks for debugging PDF.js
|
||||||
* (see `web/debugger.js`). The default value is `false`.
|
* (see `web/debugger.js`). The default value is `false`.
|
||||||
|
* @property {Object} [canvasFactory] - The factory instance that will be used
|
||||||
|
* when creating canvases. The default value is {new DOMCanvasFactory()}.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -337,6 +339,8 @@ function getDocument(src) {
|
|||||||
StandardFontDataFactory === DOMStandardFontDataFactory &&
|
StandardFontDataFactory === DOMStandardFontDataFactory &&
|
||||||
isValidFetchUrl(cMapUrl, document.baseURI) &&
|
isValidFetchUrl(cMapUrl, document.baseURI) &&
|
||||||
isValidFetchUrl(standardFontDataUrl, document.baseURI));
|
isValidFetchUrl(standardFontDataUrl, document.baseURI));
|
||||||
|
const canvasFactory =
|
||||||
|
src.canvasFactory || new DefaultCanvasFactory({ ownerDocument });
|
||||||
|
|
||||||
// Parameters only intended for development/testing purposes.
|
// Parameters only intended for development/testing purposes.
|
||||||
const styleElement =
|
const styleElement =
|
||||||
@ -349,17 +353,18 @@ function getDocument(src) {
|
|||||||
|
|
||||||
// Ensure that the various factories can be initialized, when necessary,
|
// Ensure that the various factories can be initialized, when necessary,
|
||||||
// since the user may provide *custom* ones.
|
// since the user may provide *custom* ones.
|
||||||
const transportFactory = useWorkerFetch
|
const transportFactory = {
|
||||||
? null
|
canvasFactory,
|
||||||
: {
|
};
|
||||||
cMapReaderFactory: new CMapReaderFactory({
|
if (!useWorkerFetch) {
|
||||||
baseUrl: cMapUrl,
|
transportFactory.cMapReaderFactory = new CMapReaderFactory({
|
||||||
isCompressed: cMapPacked,
|
baseUrl: cMapUrl,
|
||||||
}),
|
isCompressed: cMapPacked,
|
||||||
standardFontDataFactory: new StandardFontDataFactory({
|
});
|
||||||
baseUrl: standardFontDataUrl,
|
transportFactory.standardFontDataFactory = new StandardFontDataFactory({
|
||||||
}),
|
baseUrl: standardFontDataUrl,
|
||||||
};
|
});
|
||||||
|
}
|
||||||
|
|
||||||
if (!worker) {
|
if (!worker) {
|
||||||
const workerParams = {
|
const workerParams = {
|
||||||
@ -1280,10 +1285,9 @@ class PDFDocumentProxy {
|
|||||||
* Proxy to a `PDFPage` in the worker thread.
|
* Proxy to a `PDFPage` in the worker thread.
|
||||||
*/
|
*/
|
||||||
class PDFPageProxy {
|
class PDFPageProxy {
|
||||||
constructor(pageIndex, pageInfo, transport, ownerDocument, pdfBug = false) {
|
constructor(pageIndex, pageInfo, transport, pdfBug = false) {
|
||||||
this._pageIndex = pageIndex;
|
this._pageIndex = pageIndex;
|
||||||
this._pageInfo = pageInfo;
|
this._pageInfo = pageInfo;
|
||||||
this._ownerDocument = ownerDocument;
|
|
||||||
this._transport = transport;
|
this._transport = transport;
|
||||||
this._stats = pdfBug ? new StatTimer() : null;
|
this._stats = pdfBug ? new StatTimer() : null;
|
||||||
this._pdfBug = pdfBug;
|
this._pdfBug = pdfBug;
|
||||||
@ -1414,6 +1418,16 @@ class PDFPageProxy {
|
|||||||
pageColors = null,
|
pageColors = null,
|
||||||
printAnnotationStorage = null,
|
printAnnotationStorage = null,
|
||||||
}) {
|
}) {
|
||||||
|
if (
|
||||||
|
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) &&
|
||||||
|
canvasFactory
|
||||||
|
) {
|
||||||
|
deprecated(
|
||||||
|
"render no longer accepts the `canvasFactory`-option, " +
|
||||||
|
"please pass it to the `getDocument`-function instead."
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
this._stats?.time("Overall");
|
this._stats?.time("Overall");
|
||||||
|
|
||||||
const intentArgs = this._transport.getRenderingIntent(
|
const intentArgs = this._transport.getRenderingIntent(
|
||||||
@ -1441,9 +1455,6 @@ class PDFPageProxy {
|
|||||||
intentState.streamReaderCancelTimeout = null;
|
intentState.streamReaderCancelTimeout = null;
|
||||||
}
|
}
|
||||||
|
|
||||||
const canvasFactoryInstance =
|
|
||||||
canvasFactory ||
|
|
||||||
new DefaultCanvasFactory({ ownerDocument: this._ownerDocument });
|
|
||||||
const intentPrint = !!(
|
const intentPrint = !!(
|
||||||
intentArgs.renderingIntent & RenderingIntentFlag.PRINT
|
intentArgs.renderingIntent & RenderingIntentFlag.PRINT
|
||||||
);
|
);
|
||||||
@ -1502,7 +1513,7 @@ class PDFPageProxy {
|
|||||||
annotationCanvasMap,
|
annotationCanvasMap,
|
||||||
operatorList: intentState.operatorList,
|
operatorList: intentState.operatorList,
|
||||||
pageIndex: this._pageIndex,
|
pageIndex: this._pageIndex,
|
||||||
canvasFactory: canvasFactoryInstance,
|
canvasFactory: canvasFactory || this._transport.canvasFactory,
|
||||||
useRequestAnimationFrame: !intentPrint,
|
useRequestAnimationFrame: !intentPrint,
|
||||||
pdfBug: this._pdfBug,
|
pdfBug: this._pdfBug,
|
||||||
pageColors,
|
pageColors,
|
||||||
@ -2345,8 +2356,9 @@ class WorkerTransport {
|
|||||||
});
|
});
|
||||||
this._params = params;
|
this._params = params;
|
||||||
|
|
||||||
this.cMapReaderFactory = factory?.cMapReaderFactory;
|
this.canvasFactory = factory.canvasFactory;
|
||||||
this.standardFontDataFactory = factory?.standardFontDataFactory;
|
this.cMapReaderFactory = factory.cMapReaderFactory;
|
||||||
|
this.standardFontDataFactory = factory.standardFontDataFactory;
|
||||||
|
|
||||||
this.destroyed = false;
|
this.destroyed = false;
|
||||||
this.destroyCapability = null;
|
this.destroyCapability = null;
|
||||||
@ -2905,7 +2917,6 @@ class WorkerTransport {
|
|||||||
pageIndex,
|
pageIndex,
|
||||||
pageInfo,
|
pageInfo,
|
||||||
this,
|
this,
|
||||||
this._params.ownerDocument,
|
|
||||||
this._params.pdfBug
|
this._params.pdfBug
|
||||||
);
|
);
|
||||||
this.#pageCache.set(pageIndex, page);
|
this.#pageCache.set(pageIndex, page);
|
||||||
|
@ -2880,7 +2880,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask = pdfPage.render({
|
const renderTask = pdfPage.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
});
|
});
|
||||||
expect(renderTask instanceof RenderTask).toEqual(true);
|
expect(renderTask instanceof RenderTask).toEqual(true);
|
||||||
@ -2916,7 +2915,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask = page.render({
|
const renderTask = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
});
|
});
|
||||||
expect(renderTask instanceof RenderTask).toEqual(true);
|
expect(renderTask instanceof RenderTask).toEqual(true);
|
||||||
@ -2948,7 +2946,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask = page.render({
|
const renderTask = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
});
|
});
|
||||||
expect(renderTask instanceof RenderTask).toEqual(true);
|
expect(renderTask instanceof RenderTask).toEqual(true);
|
||||||
@ -2966,7 +2963,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
|
|
||||||
const reRenderTask = page.render({
|
const reRenderTask = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
});
|
});
|
||||||
expect(reRenderTask instanceof RenderTask).toEqual(true);
|
expect(reRenderTask instanceof RenderTask).toEqual(true);
|
||||||
@ -2990,7 +2986,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask1 = page.render({
|
const renderTask1 = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
optionalContentConfigPromise,
|
optionalContentConfigPromise,
|
||||||
});
|
});
|
||||||
@ -2998,7 +2993,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
|
|
||||||
const renderTask2 = page.render({
|
const renderTask2 = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
optionalContentConfigPromise,
|
optionalContentConfigPromise,
|
||||||
});
|
});
|
||||||
@ -3033,7 +3027,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask = pdfPage.render({
|
const renderTask = pdfPage.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
});
|
});
|
||||||
expect(renderTask instanceof RenderTask).toEqual(true);
|
expect(renderTask instanceof RenderTask).toEqual(true);
|
||||||
@ -3064,7 +3057,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask = pdfPage.render({
|
const renderTask = pdfPage.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
});
|
});
|
||||||
expect(renderTask instanceof RenderTask).toEqual(true);
|
expect(renderTask instanceof RenderTask).toEqual(true);
|
||||||
@ -3166,7 +3158,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask = pdfPage.render({
|
const renderTask = pdfPage.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
intent: "print",
|
intent: "print",
|
||||||
annotationMode: AnnotationMode.ENABLE_STORAGE,
|
annotationMode: AnnotationMode.ENABLE_STORAGE,
|
||||||
@ -3256,7 +3247,6 @@ Caron Broadcasting, Inc., an Ohio corporation (“Lessee”).`)
|
|||||||
);
|
);
|
||||||
const renderTask = page.render({
|
const renderTask = page.render({
|
||||||
canvasContext: canvasAndCtx.context,
|
canvasContext: canvasAndCtx.context,
|
||||||
canvasFactory: CanvasFactory,
|
|
||||||
viewport,
|
viewport,
|
||||||
});
|
});
|
||||||
await renderTask.promise;
|
await renderTask.promise;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user