From 312326991fc55ab00bb3339879a98823eb535110 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 9 Jun 2021 13:56:31 +0200 Subject: [PATCH 1/2] [api-minor] Set the `disableFontFace` fallback value directly in the API At this point in time, the `apiCompatibilityParams` is essentially unused with the sole exception of the `disableFontFace` handling for Node.js environments. Given that `isNodeJS` is a constant now (originally it was a function), we can simply set the correct fallback value for `disableFontFace` directly in the API and clean-up the code a bit here. --- src/display/api.js | 15 +++++++-------- src/display/api_compatibility.js | 30 ------------------------------ src/pdf.js | 3 --- 3 files changed, 7 insertions(+), 41 deletions(-) delete mode 100644 src/display/api_compatibility.js diff --git a/src/display/api.js b/src/display/api.js index 056e343cc..a1eb9c49d 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -54,7 +54,6 @@ import { NodeStandardFontDataFactory, } from "./node_utils.js"; import { AnnotationStorage } from "./annotation_storage.js"; -import { apiCompatibilityParams } from "./api_compatibility.js"; import { CanvasGraphics } from "./canvas.js"; import { GlobalWorkerOptions } from "./worker_options.js"; import { isNodeJS } from "../shared/is_node.js"; @@ -176,9 +175,10 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) { * as JavaScript. Primarily used to improve performance of font rendering, and * when parsing PDF functions. The default value is `true`. * @property {boolean} [disableFontFace] - By default fonts are converted to - * OpenType fonts and loaded via `@font-face` rules. If disabled, fonts will - * be rendered using a built-in font renderer that constructs the glyphs with - * primitive path commands. The default value is `false`. + * OpenType fonts and loaded via the Font Loading API or `@font-face` rules. + * If disabled, fonts will be rendered using a built-in font renderer that + * constructs the glyphs with primitive path commands. + * The default value is `false` in web environments and `true` in Node.js. * @property {boolean} [fontExtraProperties] - Include additional properties, * which are unused during rendering of PDF documents, when exporting the * parsed font data from the worker-thread. This may be useful for debugging @@ -340,7 +340,8 @@ function getDocument(src) { params.isEvalSupported = true; } if (typeof params.disableFontFace !== "boolean") { - params.disableFontFace = apiCompatibilityParams.disableFontFace || false; + params.disableFontFace = + (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS; } if (typeof params.ownerDocument === "undefined") { params.ownerDocument = globalThis.document; @@ -988,8 +989,7 @@ class PDFDocumentProxy { /** * @type {DocumentInitParameters} A subset of the current - * {DocumentInitParameters}, which are either needed in the viewer and/or - * whose default values may be affected by the `apiCompatibilityParams`. + * {DocumentInitParameters}, which are needed in the viewer. */ get loadingParams() { return this._transport.loadingParams; @@ -2911,7 +2911,6 @@ class WorkerTransport { const params = this._params; return shadow(this, "loadingParams", { disableAutoFetch: params.disableAutoFetch, - disableFontFace: params.disableFontFace, }); } } diff --git a/src/display/api_compatibility.js b/src/display/api_compatibility.js deleted file mode 100644 index d610e3fae..000000000 --- a/src/display/api_compatibility.js +++ /dev/null @@ -1,30 +0,0 @@ -/* Copyright 2018 Mozilla Foundation - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -import { isNodeJS } from "../shared/is_node.js"; - -const compatibilityParams = Object.create(null); -if (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) { - // Support: Node.js - (function checkFontFace() { - // Node.js is missing native support for `@font-face`. - if (isNodeJS) { - compatibilityParams.disableFontFace = true; - } - })(); -} -const apiCompatibilityParams = Object.freeze(compatibilityParams); - -export { apiCompatibilityParams }; diff --git a/src/pdf.js b/src/pdf.js index 59a2ce0d4..e8d0af4c5 100644 --- a/src/pdf.js +++ b/src/pdf.js @@ -53,7 +53,6 @@ import { VerbosityLevel, } from "./shared/util.js"; import { AnnotationLayer } from "./display/annotation_layer.js"; -import { apiCompatibilityParams } from "./display/api_compatibility.js"; import { GlobalWorkerOptions } from "./display/worker_options.js"; import { renderTextLayer } from "./display/text_layer.js"; import { SVGGraphics } from "./display/svg.js"; @@ -162,8 +161,6 @@ export { VerbosityLevel, // From "./display/annotation_layer.js": AnnotationLayer, - // From "./display/api_compatibility.js": - apiCompatibilityParams, // From "./display/worker_options.js": GlobalWorkerOptions, // From "./display/text_layer.js": From 2f8e2548f28e0b1b8ff68449be85ec7d9d8f6120 Mon Sep 17 00:00:00 2001 From: Jonas Jenwald Date: Wed, 9 Jun 2021 14:01:27 +0200 Subject: [PATCH 2/2] Don't initialize `CMapReaderFactory`/`StandardFontDataFactory` when the `useWorkerFetch` API option is set Given that there's no fallback on the worker-thread, it shouldn't be necessary to initialize `CMapReaderFactory`/`StandardFontDataFactory` when `useWorkerFetch = true` is set. Slightly unrelated, but this patch also ensures that the `useSystemFonts` default value only does the `isNodeJS` check in builds where that's actually necessary. --- src/display/api.js | 42 +++++++++++++++++++++++++++++++----------- 1 file changed, 31 insertions(+), 11 deletions(-) diff --git a/src/display/api.js b/src/display/api.js index a1eb9c49d..eb792c309 100644 --- a/src/display/api.js +++ b/src/display/api.js @@ -163,7 +163,7 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) { * @property {boolean} [useWorkerFetch] - Enable using the Fetch API in the * worker-thread when reading CMap and standard font files. When `true`, * the `CMapReaderFactory` and `StandardFontDataFactory` options are ignored. - * The default value is `true` in web wenvironments and `false` in Node.js. + * The default value is `true` in web environments and `false` in Node.js. * @property {boolean} [stopAtErrors] - Reject certain promises, e.g. * `getOperatorList`, `getTextContent`, and `RenderTask`, when the associated * PDF data cannot be successfully parsed, instead of attempting to recover @@ -329,7 +329,10 @@ function getDocument(src) { params.maxImageSize = -1; } if (typeof params.useSystemFonts !== "boolean") { - params.useSystemFonts = !isNodeJS; + params.useSystemFonts = !( + (typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && + isNodeJS + ); } if (typeof params.useWorkerFetch !== "boolean") { params.useWorkerFetch = @@ -2277,13 +2280,16 @@ class WorkerTransport { styleElement: params.styleElement, }); this._params = params; - this.CMapReaderFactory = new params.CMapReaderFactory({ - baseUrl: params.cMapUrl, - isCompressed: params.cMapPacked, - }); - this.StandardFontDataFactory = new params.StandardFontDataFactory({ - baseUrl: params.standardFontDataUrl, - }); + + if (!params.useWorkerFetch) { + this.CMapReaderFactory = new params.CMapReaderFactory({ + baseUrl: params.cMapUrl, + isCompressed: params.cMapPacked, + }); + this.StandardFontDataFactory = new params.StandardFontDataFactory({ + baseUrl: params.standardFontDataUrl, + }); + } this.destroyed = false; this.destroyCapability = null; @@ -2684,14 +2690,28 @@ class WorkerTransport { messageHandler.on("FetchBuiltInCMap", data => { if (this.destroyed) { - return Promise.reject(new Error("Worker was destroyed")); + return Promise.reject(new Error("Worker was destroyed.")); + } + if (!this.CMapReaderFactory) { + return Promise.reject( + new Error( + "CMapReaderFactory not initialized, see the `useWorkerFetch` parameter." + ) + ); } return this.CMapReaderFactory.fetch(data); }); messageHandler.on("FetchStandardFontData", data => { if (this.destroyed) { - return Promise.reject(new Error("Worker was destroyed")); + return Promise.reject(new Error("Worker was destroyed.")); + } + if (!this.StandardFontDataFactory) { + return Promise.reject( + new Error( + "StandardFontDataFactory not initialized, see the `useWorkerFetch` parameter." + ) + ); } return this.StandardFontDataFactory.fetch(data); });