Skip to content

Commit b928461

Browse files
committed
move common handler code to a shared file
1 parent 4734888 commit b928461

File tree

6 files changed

+144
-233
lines changed

6 files changed

+144
-233
lines changed

src/core/document.js

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -129,11 +129,10 @@ class Page {
129129
};
130130
}
131131

132-
#createPartialEvaluator(handler, rendererHandler) {
132+
#createPartialEvaluator(handler) {
133133
return new PartialEvaluator({
134134
xref: this.xref,
135135
handler,
136-
rendererHandler,
137136
pageIndex: this.pageIndex,
138137
idFactory: this._localIdFactory,
139138
fontCache: this.fontCache,
@@ -450,10 +449,7 @@ class Page {
450449
const contentStreamPromise = this.getContentStream();
451450
const resourcesPromise = this.loadResources(RESOURCES_KEYS_OPERATOR_LIST);
452451

453-
const partialEvaluator = this.#createPartialEvaluator(
454-
handler,
455-
rendererHandler
456-
);
452+
const partialEvaluator = this.#createPartialEvaluator(handler);
457453

458454
const newAnnotsByPage = !this.xfaFactory
459455
? getNewAnnotationsMap(annotationStorage)
@@ -1336,7 +1332,7 @@ class PDFDocument {
13361332
this.xfaFactory.setImages(xfaImages);
13371333
}
13381334

1339-
async #loadXfaFonts(handler, task, rendererHandler) {
1335+
async #loadXfaFonts(handler, task) {
13401336
const acroForm = await this.pdfManager.ensureCatalog("acroForm");
13411337
if (!acroForm) {
13421338
return;
@@ -1362,7 +1358,6 @@ class PDFDocument {
13621358
const partialEvaluator = new PartialEvaluator({
13631359
xref: this.xref,
13641360
handler,
1365-
rendererHandler,
13661361
pageIndex: -1,
13671362
idFactory: this._globalIdFactory,
13681363
fontCache,
@@ -1475,9 +1470,9 @@ class PDFDocument {
14751470
this.xfaFactory.appendFonts(pdfFonts, reallyMissingFonts);
14761471
}
14771472

1478-
loadXfaResources(handler, task, rendererHandler) {
1473+
loadXfaResources(handler, task) {
14791474
return Promise.all([
1480-
this.#loadXfaFonts(handler, task, rendererHandler).catch(() => {
1475+
this.#loadXfaFonts(handler, task).catch(() => {
14811476
// Ignore errors, to allow the document to load.
14821477
}),
14831478
this.#loadXfaImages(),

src/core/evaluator.js

Lines changed: 8 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,6 @@ class PartialEvaluator {
222222
constructor({
223223
xref,
224224
handler,
225-
rendererHandler,
226225
pageIndex,
227226
idFactory,
228227
fontCache,
@@ -235,7 +234,6 @@ class PartialEvaluator {
235234
}) {
236235
this.xref = xref;
237236
this.handler = handler;
238-
this.rendererHandler = rendererHandler;
239237
this.pageIndex = pageIndex;
240238
this.idFactory = idFactory;
241239
this.fontCache = fontCache;
@@ -555,19 +553,13 @@ class PartialEvaluator {
555553
const transfers = imgData ? [imgData.bitmap || imgData.data.buffer] : null;
556554

557555
if (this.parsingType3Font || cacheGlobally) {
558-
this.handler.send("commonobj", [objId, "Image", imgData], transfers);
559-
return this.rendererHandler.send(
556+
return this.handler.send(
560557
"commonobj",
561558
[objId, "Image", imgData],
562559
transfers
563560
);
564561
}
565-
this.handler.send(
566-
"obj",
567-
[objId, this.pageIndex, "Image", imgData],
568-
transfers
569-
);
570-
return this.rendererHandler.send(
562+
return this.handler.send(
571563
"obj",
572564
[objId, this.pageIndex, "Image", imgData],
573565
transfers
@@ -795,10 +787,11 @@ class PartialEvaluator {
795787
// globally, check if the image is still cached locally on the main-thread
796788
// to avoid having to re-parse the image (since that can be slow).
797789
if (w * h > 250000 || hasMask) {
798-
const localLength = await this.rendererHandler.sendWithPromise(
799-
"commonobj",
800-
[objId, "CopyLocalImage", { imageRef }]
801-
);
790+
const localLength = await this.sendWithPromise("commonobj", [
791+
objId,
792+
"CopyLocalImage",
793+
{ imageRef },
794+
]);
802795

803796
if (localLength) {
804797
this.globalImageCache.setData(imageRef, globalCacheData);
@@ -1028,7 +1021,6 @@ class PartialEvaluator {
10281021

10291022
state.font = translated.font;
10301023
translated.send(this.handler);
1031-
translated.send(this.rendererHandler);
10321024
return translated.loadedName;
10331025
}
10341026

@@ -1048,7 +1040,7 @@ class PartialEvaluator {
10481040
PartialEvaluator.buildFontPaths(
10491041
font,
10501042
glyphs,
1051-
this.rendererHandler,
1043+
this.handler,
10521044
this.options
10531045
);
10541046
}
@@ -1526,15 +1518,8 @@ class PartialEvaluator {
15261518

15271519
if (this.parsingType3Font) {
15281520
this.handler.send("commonobj", [id, "Pattern", patternIR]);
1529-
this.rendererHandler.send("commonobj", [id, "Pattern", patternIR]);
15301521
} else {
15311522
this.handler.send("obj", [id, this.pageIndex, "Pattern", patternIR]);
1532-
this.rendererHandler.send("obj", [
1533-
id,
1534-
this.pageIndex,
1535-
"Pattern",
1536-
patternIR,
1537-
]);
15381523
}
15391524
return id;
15401525
}

src/core/worker.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -873,6 +873,10 @@ class WorkerMessageHandler {
873873
.then(page => pdfManager.ensure(page, "getStructTree"));
874874
});
875875

876+
handler.on("FontFallback", function (data) {
877+
return pdfManager.fontFallback(data.id, handler);
878+
});
879+
876880
rendererHandler.on("FontFallback", function (data) {
877881
return pdfManager.fontFallback(data.id, rendererHandler);
878882
});

src/display/api.js

Lines changed: 9 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ import {
4545
RenderingCancelledException,
4646
StatTimer,
4747
} from "./display_utils.js";
48-
import { FontFaceObject, FontLoader } from "./font_loader.js";
4948
import {
5049
getDataProp,
5150
getFactoryUrlProp,
@@ -68,14 +67,15 @@ import { DOMCMapReaderFactory } from "display-cmap_reader_factory";
6867
import { DOMFilterFactory } from "./filter_factory.js";
6968
import { DOMStandardFontDataFactory } from "display-standard_fontdata_factory";
7069
import { DOMWasmFactory } from "display-wasm_factory";
71-
import { FontInfo } from "../shared/obj-bin-transform.js";
70+
import { FontLoader } from "./font_loader.js";
7271
import { GlobalWorkerOptions } from "./worker_options.js";
7372
import { Metadata } from "./metadata.js";
7473
import { OptionalContentConfig } from "./optional_content_config.js";
7574
import { PDFDataTransportStream } from "./transport_stream.js";
7675
import { PDFFetchStream } from "display-fetch_stream";
7776
import { PDFNetworkStream } from "display-network";
7877
import { PDFNodeStream } from "display-node_stream";
78+
import { setupHandler } from "../shared/handle_objs.js";
7979
import { TextLayer } from "./text_layer.js";
8080
import { XfaText } from "./xfa_text.js";
8181

@@ -2828,105 +2828,13 @@ class WorkerTransport {
28282828
page._startRenderPage(data.transparency, data.cacheKey);
28292829
});
28302830

2831-
messageHandler.on("commonobj", ([id, type, exportedData]) => {
2832-
if (this.destroyed) {
2833-
return null; // Ignore any pending requests if the worker was terminated.
2834-
}
2835-
2836-
if (this.commonObjs.has(id)) {
2837-
return null;
2838-
}
2839-
2840-
switch (type) {
2841-
case "Font":
2842-
if ("error" in exportedData) {
2843-
const exportedError = exportedData.error;
2844-
warn(`Error during font loading: ${exportedError}`);
2845-
this.commonObjs.resolve(id, exportedError);
2846-
break;
2847-
}
2848-
2849-
const fontData = new FontInfo(exportedData);
2850-
const inspectFont =
2851-
this._params.pdfBug && globalThis.FontInspector?.enabled
2852-
? (font, url) => globalThis.FontInspector.fontAdded(font, url)
2853-
: null;
2854-
const font = new FontFaceObject(
2855-
fontData,
2856-
inspectFont,
2857-
exportedData.extra,
2858-
exportedData.charProcOperatorList
2859-
);
2860-
2861-
this.fontLoader
2862-
.bind(font)
2863-
.catch(() => messageHandler.sendWithPromise("FontFallback", { id }))
2864-
.finally(() => {
2865-
if (!font.fontExtraProperties && font.data) {
2866-
// Immediately release the `font.data` property once the font
2867-
// has been attached to the DOM, since it's no longer needed,
2868-
// rather than waiting for a `PDFDocumentProxy.cleanup` call.
2869-
// Since `font.data` could be very large, e.g. in some cases
2870-
// multiple megabytes, this will help reduce memory usage.
2871-
font.clearData();
2872-
}
2873-
this.commonObjs.resolve(id, font);
2874-
});
2875-
break;
2876-
case "CopyLocalImage":
2877-
const { imageRef } = exportedData;
2878-
assert(imageRef, "The imageRef must be defined.");
2879-
2880-
for (const pageProxy of this.#pageCache.values()) {
2881-
for (const [, data] of pageProxy.objs) {
2882-
if (data?.ref !== imageRef) {
2883-
continue;
2884-
}
2885-
if (!data.dataLen) {
2886-
return null;
2887-
}
2888-
this.commonObjs.resolve(id, structuredClone(data));
2889-
return data.dataLen;
2890-
}
2891-
}
2892-
break;
2893-
case "FontPath":
2894-
case "Image":
2895-
case "Pattern":
2896-
this.commonObjs.resolve(id, exportedData);
2897-
break;
2898-
default:
2899-
throw new Error(`Got unknown common object type ${type}`);
2900-
}
2901-
2902-
return null;
2903-
});
2904-
2905-
messageHandler.on("obj", ([id, pageIndex, type, imageData]) => {
2906-
if (this.destroyed) {
2907-
// Ignore any pending requests if the worker was terminated.
2908-
return;
2909-
}
2910-
2911-
const pageProxy = this.#pageCache.get(pageIndex);
2912-
if (pageProxy.objs.has(id)) {
2913-
return;
2914-
}
2915-
// Don't store data *after* cleanup has successfully run, see bug 1854145.
2916-
if (pageProxy._intentStates.size === 0) {
2917-
imageData?.bitmap?.close(); // Release any `ImageBitmap` data.
2918-
return;
2919-
}
2920-
2921-
switch (type) {
2922-
case "Image":
2923-
case "Pattern":
2924-
pageProxy.objs.resolve(id, imageData);
2925-
break;
2926-
default:
2927-
throw new Error(`Got unknown object type ${type}`);
2928-
}
2929-
});
2831+
setupHandler(
2832+
messageHandler,
2833+
this.destroyed,
2834+
this.commonObjs,
2835+
this.#pageCache,
2836+
this.fontLoader
2837+
);
29302838

29312839
messageHandler.on("DocProgress", data => {
29322840
if (this.destroyed) {

0 commit comments

Comments
 (0)