Skip to content

Commit 4900bd8

Browse files
Merge pull request #20939 from Snuffleupagus/BaseCMapReaderFactory-filename
[api-minor] Simplify `BaseCMapReaderFactory` by having the worker-thread create the `filename`
2 parents c0f3627 + 262aeef commit 4900bd8

6 files changed

Lines changed: 53 additions & 37 deletions

File tree

src/core/evaluator.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,7 @@ const DefaultPartialEvaluatorOptions = Object.freeze({
102102
useWasm: true,
103103
useWorkerFetch: true,
104104
cMapUrl: null,
105+
cMapPacked: true,
105106
iccUrl: null,
106107
standardFontDataUrl: null,
107108
wasmUrl: null,
@@ -413,10 +414,13 @@ class PartialEvaluator {
413414
throw new Error("Only worker-thread fetching supported.");
414415
}
415416
// Get the data on the main-thread instead.
416-
data = await this.handler.sendWithPromise("FetchBinaryData", {
417-
type: "cMapReaderFactory",
418-
name,
419-
});
417+
data = {
418+
cMapData: await this.handler.sendWithPromise("FetchBinaryData", {
419+
type: "cMapReaderFactory",
420+
filename: `${name}${this.options.cMapPacked ? ".bcmap" : ""}`,
421+
}),
422+
isCompressed: this.options.cMapPacked,
423+
};
420424
}
421425
// Cache the CMap data, to avoid fetching it repeatedly.
422426
this.builtInCMapCache.set(name, data);

src/display/api.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -366,6 +366,7 @@ function getDocument(src = {}) {
366366
StandardFontDataFactory === DOMStandardFontDataFactory &&
367367
WasmFactory === DOMWasmFactory &&
368368
cMapUrl &&
369+
cMapPacked &&
369370
standardFontDataUrl &&
370371
wasmUrl &&
371372
isValidFetchUrl(cMapUrl, document.baseURI) &&
@@ -391,7 +392,7 @@ function getDocument(src = {}) {
391392
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
392393
useWorkerFetch
393394
? null
394-
: new CMapReaderFactory({ baseUrl: cMapUrl, isCompressed: cMapPacked }),
395+
: new CMapReaderFactory({ baseUrl: cMapUrl }),
395396
standardFontDataFactory:
396397
(typeof PDFJSDev !== "undefined" && PDFJSDev.test("MOZCENTRAL")) ||
397398
useWorkerFetch
@@ -439,6 +440,7 @@ function getDocument(src = {}) {
439440
useWasm,
440441
useWorkerFetch,
441442
cMapUrl,
443+
cMapPacked,
442444
iccUrl,
443445
standardFontDataUrl,
444446
wasmUrl,

src/display/cmap_reader_factory.js

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,30 +17,25 @@ import { stringToBytes, unreachable } from "../shared/util.js";
1717
import { fetchData } from "./display_utils.js";
1818

1919
class BaseCMapReaderFactory {
20-
constructor({ baseUrl = null, isCompressed = true }) {
20+
constructor({ baseUrl = null }) {
2121
if (
2222
(typeof PDFJSDev === "undefined" || PDFJSDev.test("TESTING")) &&
2323
this.constructor === BaseCMapReaderFactory
2424
) {
2525
unreachable("Cannot initialize BaseCMapReaderFactory.");
2626
}
2727
this.baseUrl = baseUrl;
28-
this.isCompressed = isCompressed;
2928
}
3029

31-
async fetch({ name }) {
30+
async fetch({ filename }) {
3231
if (!this.baseUrl) {
33-
throw new Error(
34-
"Ensure that the `cMapUrl` and `cMapPacked` API parameters are provided."
35-
);
32+
throw new Error("Ensure that the `cMapUrl` API parameter is provided.");
3633
}
37-
const url = this.baseUrl + name + (this.isCompressed ? ".bcmap" : "");
34+
const url = `${this.baseUrl}${filename}`;
3835

39-
return this._fetch(url)
40-
.then(cMapData => ({ cMapData, isCompressed: this.isCompressed }))
41-
.catch(reason => {
42-
throw new Error(`Unable to load CMap data at: ${url}`);
43-
});
36+
return this._fetch(url).catch(reason => {
37+
throw new Error(`Unable to load CMap data at: ${url}`);
38+
});
4439
}
4540

4641
/**
@@ -59,7 +54,7 @@ class DOMCMapReaderFactory extends BaseCMapReaderFactory {
5954
async _fetch(url) {
6055
const data = await fetchData(
6156
url,
62-
/* type = */ this.isCompressed ? "bytes" : "text"
57+
/* type = */ url.endsWith(".bcmap") ? "bytes" : "text"
6358
);
6459
return data instanceof Uint8Array ? data : stringToBytes(data);
6560
}

test/unit/annotation_spec.js

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,7 @@ import {
3737
createIdFactory,
3838
DefaultCMapReaderFactory,
3939
DefaultStandardFontDataFactory,
40+
fetchBuiltInCMapHelper,
4041
STANDARD_FONT_DATA_URL,
4142
XRefMock,
4243
} from "./test_utils.js";
@@ -116,16 +117,13 @@ describe("annotation", function () {
116117
const CMapReaderFactory = new DefaultCMapReaderFactory({
117118
baseUrl: CMAP_URL,
118119
});
120+
const fetchBuiltInCMap = name =>
121+
fetchBuiltInCMapHelper(CMapReaderFactory, /* cMapPacked = */ true, name);
119122

120123
const builtInCMapCache = new Map();
121-
builtInCMapCache.set(
122-
"UniJIS-UTF16-H",
123-
await CMapReaderFactory.fetch({ name: "UniJIS-UTF16-H" })
124-
);
125-
builtInCMapCache.set(
126-
"Adobe-Japan1-UCS2",
127-
await CMapReaderFactory.fetch({ name: "Adobe-Japan1-UCS2" })
128-
);
124+
for (const name of ["UniJIS-UTF16-H", "Adobe-Japan1-UCS2"]) {
125+
builtInCMapCache.set(name, await fetchBuiltInCMap(name));
126+
}
129127

130128
idFactoryMock = createIdFactory(/* pageIndex = */ 0);
131129
partialEvaluator = new PartialEvaluator({

test/unit/cmap_spec.js

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,24 @@
1414
*/
1515

1616
import { CMap, CMapFactory, IdentityCMap } from "../../src/core/cmap.js";
17-
import { CMAP_URL, DefaultCMapReaderFactory } from "./test_utils.js";
17+
import {
18+
CMAP_URL,
19+
DefaultCMapReaderFactory,
20+
fetchBuiltInCMapHelper,
21+
} from "./test_utils.js";
1822
import { Name } from "../../src/core/primitives.js";
1923
import { StringStream } from "../../src/core/stream.js";
2024

2125
describe("cmap", function () {
2226
let fetchBuiltInCMap;
2327

2428
beforeAll(function () {
25-
// Allow CMap testing in Node.js, e.g. for Travis.
2629
const CMapReaderFactory = new DefaultCMapReaderFactory({
2730
baseUrl: CMAP_URL,
2831
});
2932

30-
fetchBuiltInCMap = function (name) {
31-
return CMapReaderFactory.fetch({
32-
name,
33-
});
34-
};
33+
fetchBuiltInCMap = name =>
34+
fetchBuiltInCMapHelper(CMapReaderFactory, /* cMapPacked = */ true, name);
3535
});
3636

3737
afterAll(function () {
@@ -206,7 +206,11 @@ describe("cmap", function () {
206206
it("attempts to load a built-in CMap without the necessary API parameters", async function () {
207207
function tmpFetchBuiltInCMap(name) {
208208
const CMapReaderFactory = new DefaultCMapReaderFactory({});
209-
return CMapReaderFactory.fetch({ name });
209+
return fetchBuiltInCMapHelper(
210+
CMapReaderFactory,
211+
/* cMapPacked = */ true,
212+
name
213+
);
210214
}
211215

212216
try {
@@ -221,7 +225,7 @@ describe("cmap", function () {
221225
} catch (reason) {
222226
expect(reason).toBeInstanceOf(Error);
223227
expect(reason.message).toEqual(
224-
"Ensure that the `cMapUrl` and `cMapPacked` API parameters are provided."
228+
"Ensure that the `cMapUrl` API parameter is provided."
225229
);
226230
}
227231
});
@@ -230,9 +234,12 @@ describe("cmap", function () {
230234
function tmpFetchBuiltInCMap(name) {
231235
const CMapReaderFactory = new DefaultCMapReaderFactory({
232236
baseUrl: CMAP_URL,
233-
isCompressed: false,
234237
});
235-
return CMapReaderFactory.fetch({ name });
238+
return fetchBuiltInCMapHelper(
239+
CMapReaderFactory,
240+
/* cMapPacked = */ false,
241+
name
242+
);
236243
}
237244

238245
try {

test/unit/test_utils.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,15 @@ const DefaultStandardFontDataFactory =
5555
? NodeStandardFontDataFactory
5656
: DOMStandardFontDataFactory;
5757

58+
async function fetchBuiltInCMapHelper(cMapReaderFactory, cMapPacked, name) {
59+
return {
60+
cMapData: await cMapReaderFactory.fetch({
61+
filename: `${name}${cMapPacked ? ".bcmap" : ""}`,
62+
}),
63+
isCompressed: cMapPacked,
64+
};
65+
}
66+
5867
function buildGetDocumentParams(filename, options) {
5968
const params = Object.create(null);
6069
params.url = isNodeJS
@@ -252,6 +261,7 @@ export {
252261
DefaultCMapReaderFactory,
253262
DefaultFileReaderFactory,
254263
DefaultStandardFontDataFactory,
264+
fetchBuiltInCMapHelper,
255265
getCrossOriginHostname,
256266
STANDARD_FONT_DATA_URL,
257267
TEST_PDFS_PATH,

0 commit comments

Comments
 (0)