Merge pull request #12726 from brendandahl/standard-fonts

[api-minor] Include and use the 14 standard font files.
This commit is contained in:
Jonas Jenwald 2021-06-08 10:09:40 +02:00 committed by GitHub
commit e7dc822e74
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
36 changed files with 463 additions and 80 deletions

View file

@ -40,6 +40,7 @@ import {
deprecated,
DOMCanvasFactory,
DOMCMapReaderFactory,
DOMStandardFontDataFactory,
isDataScheme,
loadScript,
PageViewport,
@ -47,7 +48,11 @@ import {
StatTimer,
} from "./display_utils.js";
import { FontFaceObject, FontLoader } from "./font_loader.js";
import { NodeCanvasFactory, NodeCMapReaderFactory } from "./node_utils.js";
import {
NodeCanvasFactory,
NodeCMapReaderFactory,
NodeStandardFontDataFactory,
} from "./node_utils.js";
import { AnnotationStorage } from "./annotation_storage.js";
import { apiCompatibilityParams } from "./api_compatibility.js";
import { CanvasGraphics } from "./canvas.js";
@ -69,6 +74,10 @@ const DefaultCMapReaderFactory =
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS
? NodeCMapReaderFactory
: DOMCMapReaderFactory;
const DefaultStandardFontDataFactory =
(typeof PDFJSDev === "undefined" || PDFJSDev.test("GENERIC")) && isNodeJS
? NodeStandardFontDataFactory
: DOMStandardFontDataFactory;
/**
* @typedef {function} IPDFStreamFactory
@ -143,6 +152,19 @@ function setPDFNetworkStreamFactory(pdfNetworkStreamFactory) {
* reading built-in CMap files. Providing a custom factory is useful for
* environments without Fetch API or `XMLHttpRequest` support, such as
* Node.js. The default value is {DOMCMapReaderFactory}.
* @property {boolean} [useSystemFonts] - When true, fonts that aren't embedded
* in the PDF will fallback to a system font. Defaults to true for web
* environments and false for node.
* @property {string} [standardFontDataUrl] - The URL where the standard font
* files are located. Include the trailing slash.
* @property {boolean} [useWorkerFetch] - Enable using fetch in the worker for
* resources. This currently only used for fetching the font data from the
* worker thread. When `true`, StandardFontDataFactory will be ignored. The
* default value is `true` in web environment and `false` for Node.
* @property {Object} [StandardFontDataFactory] - The factory that will be used
* when reading the standard font files. Providing a custom factory is useful
* for environments without Fetch API or `XMLHttpRequest` support, such as
* Node.js. The default value is {DOMStandardFontDataFactory}.
* @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
@ -287,6 +309,8 @@ function getDocument(src) {
params.rangeChunkSize = params.rangeChunkSize || DEFAULT_RANGE_CHUNK_SIZE;
params.CMapReaderFactory =
params.CMapReaderFactory || DefaultCMapReaderFactory;
params.StandardFontDataFactory =
params.StandardFontDataFactory || DefaultStandardFontDataFactory;
params.ignoreErrors = params.stopAtErrors !== true;
params.fontExtraProperties = params.fontExtraProperties === true;
params.pdfBug = params.pdfBug === true;
@ -304,6 +328,13 @@ function getDocument(src) {
if (!Number.isInteger(params.maxImageSize)) {
params.maxImageSize = -1;
}
if (typeof params.useSystemFonts !== "boolean") {
params.useSystemFonts = !isNodeJS;
}
if (typeof params.useWorkerFetch !== "boolean") {
params.useWorkerFetch =
params.StandardFontDataFactory === DOMStandardFontDataFactory;
}
if (typeof params.isEvalSupported !== "boolean") {
params.isEvalSupported = true;
}
@ -455,6 +486,10 @@ function _fetchDocument(worker, source, pdfDataRangeTransport, docId) {
isEvalSupported: source.isEvalSupported,
fontExtraProperties: source.fontExtraProperties,
enableXfa: source.enableXfa,
useSystemFonts: source.useSystemFonts,
standardFontDataUrl: source.useWorkerFetch
? source.standardFontDataUrl
: null,
})
.then(function (workerId) {
if (worker.destroyed) {
@ -2243,6 +2278,9 @@ class WorkerTransport {
baseUrl: params.cMapUrl,
isCompressed: params.cMapPacked,
});
this.StandardFontDataFactory = new params.StandardFontDataFactory({
baseUrl: params.standardFontDataUrl,
});
this.destroyed = false;
this.destroyCapability = null;
@ -2641,6 +2679,13 @@ class WorkerTransport {
this._onUnsupportedFeature.bind(this)
);
messageHandler.on("FetchStandardFontData", data => {
if (this.destroyed) {
return Promise.reject(new Error("Worker was destroyed"));
}
return this.StandardFontDataFactory.fetch(data);
});
messageHandler.on("FetchBuiltInCMap", (data, sink) => {
if (this.destroyed) {
sink.error(new Error("Worker was destroyed"));
@ -3183,6 +3228,7 @@ export {
build,
DefaultCanvasFactory,
DefaultCMapReaderFactory,
DefaultStandardFontDataFactory,
getDocument,
LoopbackPort,
PDFDataRangeTransport,