mirror of
https://github.com/mozilla/pdf.js.git
synced 2026-07-02 21:25:50 +02:00
[api-minor] Simplify BaseCMapReaderFactory by having the worker-thread create the filename
The `BaseCMapReaderFactory`, `BaseStandardFontDataFactory`, and `BaseWasmFactory` classes are all very similar, and the only difference is really in their respective `fetch` methods. By have the worker-thread "compute" the complete `filename` it's possible to simplify the `BaseCMapReaderFactory.prototype.fetch` method, which will allow future improvements to all of these classes. A couple of things to note: - This code is unused, and it's not even bundled, in the Firefox PDF Viewer. - In browsers it's unused by default, and worker-thread fetching will always be used when possible since that's more efficient. *Please note:* For users that provide a custom `CMapReaderFactory` instance when calling `getDocument` this could be a breaking change, however it's unlikely that any such users exist. (The *internal* format of this data was changed previously in PR 18951, and there hasn't been a single question/complaint about it in well over a year.)
This commit is contained in:
parent
918a319de6
commit
262aeef3fa
@ -102,6 +102,7 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
|
|||||||
useWasm: true,
|
useWasm: true,
|
||||||
useWorkerFetch: true,
|
useWorkerFetch: true,
|
||||||
cMapUrl: null,
|
cMapUrl: null,
|
||||||
|
cMapPacked: true,
|
||||||
iccUrl: null,
|
iccUrl: null,
|
||||||
standardFontDataUrl: null,
|
standardFontDataUrl: null,
|
||||||
wasmUrl: null,
|
wasmUrl: null,
|
||||||
@ -413,10 +414,13 @@ class PartialEvaluator {
|
|||||||
throw new Error("Only worker-thread fetching supported.");
|
throw new Error("Only worker-thread fetching supported.");
|
||||||
}
|
}
|
||||||
// Get the data on the main-thread instead.
|
// Get the data on the main-thread instead.
|
||||||
data = await this.handler.sendWithPromise("FetchBinaryData", {
|
data = {
|
||||||
type: "cMapReaderFactory",
|
cMapData: await this.handler.sendWithPromise("FetchBinaryData", {
|
||||||
name,
|
type: "cMapReaderFactory",
|
||||||
});
|
filename: `${name}${this.options.cMapPacked ? ".bcmap" : ""}`,
|
||||||
|
}),
|
||||||
|
isCompressed: this.options.cMapPacked,
|
||||||
|
};
|
||||||
}
|
}
|
||||||
// Cache the CMap data, to avoid fetching it repeatedly.
|
// Cache the CMap data, to avoid fetching it repeatedly.
|
||||||
this.builtInCMapCache.set(name, data);
|
this.builtInCMapCache.set(name, data);
|
||||||
|
|||||||
@ -366,6 +366,7 @@ function getDocument(src = {}) {
|
|||||||
StandardFontDataFactory === DOMStandardFontDataFactory &&
|
StandardFontDataFactory === DOMStandardFontDataFactory &&
|
||||||
WasmFactory === DOMWasmFactory &&
|
WasmFactory === DOMWasmFactory &&
|
||||||
cMapUrl &&
|
cMapUrl &&
|
||||||
|
cMapPacked &&
|
||||||
standardFontDataUrl &&
|
standardFontDataUrl &&
|
||||||
wasmUrl &&
|
wasmUrl &&
|
||||||
isValidFetchUrl(cMapUrl, document.baseURI) &&
|
isValidFetchUrl(cMapUrl, document.baseURI) &&
|
||||||
@ -391,7 +392,7 @@ function getDocument(src = {}) {
|
|||||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||||
useWorkerFetch
|
useWorkerFetch
|
||||||
? null
|
? null
|
||||||
: new CMapReaderFactory({ baseUrl: cMapUrl, isCompressed: cMapPacked }),
|
: new CMapReaderFactory({ baseUrl: cMapUrl }),
|
||||||
standardFontDataFactory:
|
standardFontDataFactory:
|
||||||
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
|
||||||
useWorkerFetch
|
useWorkerFetch
|
||||||
@ -439,6 +440,7 @@ function getDocument(src = {}) {
|
|||||||
useWasm,
|
useWasm,
|
||||||
useWorkerFetch,
|
useWorkerFetch,
|
||||||
cMapUrl,
|
cMapUrl,
|
||||||
|
cMapPacked,
|
||||||
iccUrl,
|
iccUrl,
|
||||||
standardFontDataUrl,
|
standardFontDataUrl,
|
||||||
wasmUrl,
|
wasmUrl,
|
||||||
|
|||||||
@ -17,7 +17,7 @@ import { stringToBytes, unreachable } from "../shared/util.js";
|
|||||||
import { fetchData } from "./display_utils.js";
|
import { fetchData } from "./display_utils.js";
|
||||||
|
|
||||||
class BaseCMapReaderFactory {
|
class BaseCMapReaderFactory {
|
||||||
constructor({ baseUrl = null, isCompressed = true }) {
|
constructor({ baseUrl = null }) {
|
||||||
if (
|
if (
|
||||||
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
|
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
|
||||||
this.constructor === BaseCMapReaderFactory
|
this.constructor === BaseCMapReaderFactory
|
||||||
@ -25,22 +25,17 @@ class BaseCMapReaderFactory {
|
|||||||
unreachable("Cannot initialize BaseCMapReaderFactory.");
|
unreachable("Cannot initialize BaseCMapReaderFactory.");
|
||||||
}
|
}
|
||||||
this.baseUrl = baseUrl;
|
this.baseUrl = baseUrl;
|
||||||
this.isCompressed = isCompressed;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
async fetch({ name }) {
|
async fetch({ filename }) {
|
||||||
if (!this.baseUrl) {
|
if (!this.baseUrl) {
|
||||||
throw new Error(
|
throw new Error("Ensure that the `cMapUrl` API parameter is provided.");
|
||||||
"Ensure that the `cMapUrl` and `cMapPacked` API parameters are provided."
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
|
const url = `${this.baseUrl}${filename}`;
|
||||||
|
|
||||||
return this._fetch(url)
|
return this._fetch(url).catch(reason => {
|
||||||
.then(cMapData => ({ cMapData, isCompressed: this.isCompressed }))
|
throw new Error(`Unable to load CMap data at: ${url}`);
|
||||||
.catch(reason => {
|
});
|
||||||
throw new Error(`Unable to load CMap data at: ${url}`);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -59,7 +54,7 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
|
|||||||
async _fetch(url) {
|
async _fetch(url) {
|
||||||
const data = await fetchData(
|
const data = await fetchData(
|
||||||
url,
|
url,
|
||||||
/* type = */ this.isCompressed ? "bytes" : "text"
|
/* type = */ url.endsWith(".bcmap") ? "bytes" : "text"
|
||||||
);
|
);
|
||||||
return data instanceof Uint8Array ? data : stringToBytes(data);
|
return data instanceof Uint8Array ? data : stringToBytes(data);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -37,6 +37,7 @@ import {
|
|||||||
createIdFactory,
|
createIdFactory,
|
||||||
DefaultCMapReaderFactory,
|
DefaultCMapReaderFactory,
|
||||||
DefaultStandardFontDataFactory,
|
DefaultStandardFontDataFactory,
|
||||||
|
fetchBuiltInCMapHelper,
|
||||||
STANDARD_FONT_DATA_URL,
|
STANDARD_FONT_DATA_URL,
|
||||||
XRefMock,
|
XRefMock,
|
||||||
} from "./test_utils.js";
|
} from "./test_utils.js";
|
||||||
@ -116,16 +117,13 @@ describe("annotation", function () {
|
|||||||
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
||||||
baseUrl: CMAP_URL,
|
baseUrl: CMAP_URL,
|
||||||
});
|
});
|
||||||
|
const fetchBuiltInCMap = name =>
|
||||||
|
fetchBuiltInCMapHelper(CMapReaderFactory, /* cMapPacked = */ true, name);
|
||||||
|
|
||||||
const builtInCMapCache = new Map();
|
const builtInCMapCache = new Map();
|
||||||
builtInCMapCache.set(
|
for (const name of ["UniJIS-UTF16-H", "Adobe-Japan1-UCS2"]) {
|
||||||
"UniJIS-UTF16-H",
|
builtInCMapCache.set(name, await fetchBuiltInCMap(name));
|
||||||
await CMapReaderFactory.fetch({ name: "UniJIS-UTF16-H" })
|
}
|
||||||
);
|
|
||||||
builtInCMapCache.set(
|
|
||||||
"Adobe-Japan1-UCS2",
|
|
||||||
await CMapReaderFactory.fetch({ name: "Adobe-Japan1-UCS2" })
|
|
||||||
);
|
|
||||||
|
|
||||||
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
|
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
|
||||||
partialEvaluator = new PartialEvaluator({
|
partialEvaluator = new PartialEvaluator({
|
||||||
|
|||||||
@ -14,7 +14,11 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
import { CMap, CMapFactory, IdentityCMap } from "../../src/core/cmap.js";
|
import { CMap, CMapFactory, IdentityCMap } from "../../src/core/cmap.js";
|
||||||
import { CMAP_URL, DefaultCMapReaderFactory } from "./test_utils.js";
|
import {
|
||||||
|
CMAP_URL,
|
||||||
|
DefaultCMapReaderFactory,
|
||||||
|
fetchBuiltInCMapHelper,
|
||||||
|
} from "./test_utils.js";
|
||||||
import { Name } from "../../src/core/primitives.js";
|
import { Name } from "../../src/core/primitives.js";
|
||||||
import { StringStream } from "../../src/core/stream.js";
|
import { StringStream } from "../../src/core/stream.js";
|
||||||
|
|
||||||
@ -22,16 +26,12 @@ describe("cmap", function () {
|
|||||||
let fetchBuiltInCMap;
|
let fetchBuiltInCMap;
|
||||||
|
|
||||||
beforeAll(function () {
|
beforeAll(function () {
|
||||||
// Allow CMap testing in Node.js, e.g. for Travis.
|
|
||||||
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
||||||
baseUrl: CMAP_URL,
|
baseUrl: CMAP_URL,
|
||||||
});
|
});
|
||||||
|
|
||||||
fetchBuiltInCMap = function (name) {
|
fetchBuiltInCMap = name =>
|
||||||
return CMapReaderFactory.fetch({
|
fetchBuiltInCMapHelper(CMapReaderFactory, /* cMapPacked = */ true, name);
|
||||||
name,
|
|
||||||
});
|
|
||||||
};
|
|
||||||
});
|
});
|
||||||
|
|
||||||
afterAll(function () {
|
afterAll(function () {
|
||||||
@ -206,7 +206,11 @@ describe("cmap", function () {
|
|||||||
it("attempts to load a built-in CMap without the necessary API parameters", async function () {
|
it("attempts to load a built-in CMap without the necessary API parameters", async function () {
|
||||||
function tmpFetchBuiltInCMap(name) {
|
function tmpFetchBuiltInCMap(name) {
|
||||||
const CMapReaderFactory = new DefaultCMapReaderFactory({});
|
const CMapReaderFactory = new DefaultCMapReaderFactory({});
|
||||||
return CMapReaderFactory.fetch({ name });
|
return fetchBuiltInCMapHelper(
|
||||||
|
CMapReaderFactory,
|
||||||
|
/* cMapPacked = */ true,
|
||||||
|
name
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
@ -221,7 +225,7 @@ describe("cmap", function () {
|
|||||||
} catch (reason) {
|
} catch (reason) {
|
||||||
expect(reason).toBeInstanceOf(Error);
|
expect(reason).toBeInstanceOf(Error);
|
||||||
expect(reason.message).toEqual(
|
expect(reason.message).toEqual(
|
||||||
"Ensure that the `cMapUrl` and `cMapPacked` API parameters are provided."
|
"Ensure that the `cMapUrl` API parameter is provided."
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
@ -230,9 +234,12 @@ describe("cmap", function () {
|
|||||||
function tmpFetchBuiltInCMap(name) {
|
function tmpFetchBuiltInCMap(name) {
|
||||||
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
const CMapReaderFactory = new DefaultCMapReaderFactory({
|
||||||
baseUrl: CMAP_URL,
|
baseUrl: CMAP_URL,
|
||||||
isCompressed: false,
|
|
||||||
});
|
});
|
||||||
return CMapReaderFactory.fetch({ name });
|
return fetchBuiltInCMapHelper(
|
||||||
|
CMapReaderFactory,
|
||||||
|
/* cMapPacked = */ false,
|
||||||
|
name
|
||||||
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
|||||||
@ -55,6 +55,15 @@ const DefaultStandardFontDataFactory =
|
|||||||
? NodeStandardFontDataFactory
|
? NodeStandardFontDataFactory
|
||||||
: DOMStandardFontDataFactory;
|
: DOMStandardFontDataFactory;
|
||||||
|
|
||||||
|
async function fetchBuiltInCMapHelper(cMapReaderFactory, cMapPacked, name) {
|
||||||
|
return {
|
||||||
|
cMapData: await cMapReaderFactory.fetch({
|
||||||
|
filename: `${name}${cMapPacked ? ".bcmap" : ""}`,
|
||||||
|
}),
|
||||||
|
isCompressed: cMapPacked,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
function buildGetDocumentParams(filename, options) {
|
function buildGetDocumentParams(filename, options) {
|
||||||
const params = Object.create(null);
|
const params = Object.create(null);
|
||||||
params.url = isNodeJS
|
params.url = isNodeJS
|
||||||
@ -252,6 +261,7 @@ export {
|
|||||||
DefaultCMapReaderFactory,
|
DefaultCMapReaderFactory,
|
||||||
DefaultFileReaderFactory,
|
DefaultFileReaderFactory,
|
||||||
DefaultStandardFontDataFactory,
|
DefaultStandardFontDataFactory,
|
||||||
|
fetchBuiltInCMapHelper,
|
||||||
getCrossOriginHostname,
|
getCrossOriginHostname,
|
||||||
STANDARD_FONT_DATA_URL,
|
STANDARD_FONT_DATA_URL,
|
||||||
TEST_PDFS_PATH,
|
TEST_PDFS_PATH,
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user