pdf.js.mirror/src/display/cmap_reader_factory.js
Jonas Jenwald 262aeef3fa [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.)
2026-03-21 15:54:40 +01:00

64 lines
1.8 KiB
JavaScript

/* Copyright 2015 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 { stringToBytes, unreachable } from "../shared/util.js";
import { fetchData } from "./display_utils.js";
class BaseCMapReaderFactory {
constructor({ baseUrl = null }) {
if (
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
this.constructor === BaseCMapReaderFactory
) {
unreachable("Cannot initialize BaseCMapReaderFactory.");
}
this.baseUrl = baseUrl;
}
async fetch({ filename }) {
if (!this.baseUrl) {
throw new Error("Ensure that the `cMapUrl` API parameter is provided.");
}
const url = `${this.baseUrl}${filename}`;
return this._fetch(url).catch(reason => {
throw new Error(`Unable to load CMap data at: ${url}`);
});
}
/**
* @ignore
* @returns {Promise<Uint8Array>}
*/
async _fetch(url) {
unreachable("Abstract method `_fetch` called.");
}
}
class DOMCMapReaderFactory extends BaseCMapReaderFactory {
/**
* @ignore
*/
async _fetch(url) {
const data = await fetchData(
url,
/* type = */ url.endsWith(".bcmap") ? "bytes" : "text"
);
return data instanceof Uint8Array ? data : stringToBytes(data);
}
}
export { BaseCMapReaderFactory, DOMCMapReaderFactory };