Skip to content

Commit a906d0e

Browse files
ryzokukenAditi-1400
authored andcommitted
move common handler code to a shared file
1 parent 3909b9a commit a906d0e

6 files changed

Lines changed: 152 additions & 246 deletions

File tree

src/core/document.js

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

131-
#createPartialEvaluator(handler, rendererHandler) {
131+
#createPartialEvaluator(handler) {
132132
return new PartialEvaluator({
133133
xref: this.xref,
134134
handler,
135-
rendererHandler,
136135
pageIndex: this.pageIndex,
137136
idFactory: this._localIdFactory,
138137
fontCache: this.fontCache,
@@ -473,10 +472,7 @@ class Page {
473472
const contentStreamPromise = this.getContentStream();
474473
const resourcesPromise = this.loadResources(RESOURCES_KEYS_OPERATOR_LIST);
475474

476-
const partialEvaluator = this.#createPartialEvaluator(
477-
handler,
478-
rendererHandler
479-
);
475+
const partialEvaluator = this.#createPartialEvaluator(handler);
480476

481477
const newAnnotsByPage = !this.xfaFactory
482478
? getNewAnnotationsMap(annotationStorage)
@@ -1315,7 +1311,7 @@ class PDFDocument {
13151311
this.xfaFactory.setImages(xfaImages);
13161312
}
13171313

1318-
async #loadXfaFonts(handler, task, rendererHandler) {
1314+
async #loadXfaFonts(handler, task) {
13191315
const acroForm = await this.pdfManager.ensureCatalog("acroForm");
13201316
if (!acroForm) {
13211317
return;
@@ -1341,7 +1337,6 @@ class PDFDocument {
13411337
const partialEvaluator = new PartialEvaluator({
13421338
xref: this.xref,
13431339
handler,
1344-
rendererHandler,
13451340
pageIndex: -1,
13461341
idFactory: this._globalIdFactory,
13471342
fontCache,
@@ -1454,9 +1449,9 @@ class PDFDocument {
14541449
this.xfaFactory.appendFonts(pdfFonts, reallyMissingFonts);
14551450
}
14561451

1457-
loadXfaResources(handler, task, rendererHandler) {
1452+
loadXfaResources(handler, task) {
14581453
return Promise.all([
1459-
this.#loadXfaFonts(handler, task, rendererHandler).catch(() => {
1454+
this.#loadXfaFonts(handler, task).catch(() => {
14601455
// Ignore errors, to allow the document to load.
14611456
}),
14621457
this.#loadXfaImages(),

src/core/evaluator.js

Lines changed: 8 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -226,7 +226,6 @@ class PartialEvaluator {
226226
constructor({
227227
xref,
228228
handler,
229-
rendererHandler,
230229
pageIndex,
231230
idFactory,
232231
fontCache,
@@ -239,7 +238,6 @@ class PartialEvaluator {
239238
}) {
240239
this.xref = xref;
241240
this.handler = handler;
242-
this.rendererHandler = rendererHandler;
243241
this.pageIndex = pageIndex;
244242
this.idFactory = idFactory;
245243
this.fontCache = fontCache;
@@ -559,19 +557,13 @@ class PartialEvaluator {
559557
const transfers = imgData ? [imgData.bitmap || imgData.data.buffer] : null;
560558

561559
if (this.parsingType3Font || cacheGlobally) {
562-
this.handler.send("commonobj", [objId, "Image", imgData], transfers);
563-
return this.rendererHandler.send(
560+
return this.handler.send(
564561
"commonobj",
565562
[objId, "Image", imgData],
566563
transfers
567564
);
568565
}
569-
this.handler.send(
570-
"obj",
571-
[objId, this.pageIndex, "Image", imgData],
572-
transfers
573-
);
574-
return this.rendererHandler.send(
566+
return this.handler.send(
575567
"obj",
576568
[objId, this.pageIndex, "Image", imgData],
577569
transfers
@@ -799,10 +791,11 @@ class PartialEvaluator {
799791
// globally, check if the image is still cached locally on the main-thread
800792
// to avoid having to re-parse the image (since that can be slow).
801793
if (w * h > 250000 || hasMask) {
802-
const localLength = await this.rendererHandler.sendWithPromise(
803-
"commonobj",
804-
[objId, "CopyLocalImage", { imageRef }]
805-
);
794+
const localLength = await this.sendWithPromise("commonobj", [
795+
objId,
796+
"CopyLocalImage",
797+
{ imageRef },
798+
]);
806799

807800
if (localLength) {
808801
this.globalImageCache.setData(imageRef, globalCacheData);
@@ -1032,7 +1025,6 @@ class PartialEvaluator {
10321025

10331026
state.font = translated.font;
10341027
translated.send(this.handler);
1035-
translated.send(this.rendererHandler);
10361028
return translated.loadedName;
10371029
}
10381030

@@ -1053,7 +1045,7 @@ class PartialEvaluator {
10531045
PartialEvaluator.buildFontPaths(
10541046
font,
10551047
glyphs,
1056-
this.rendererHandler,
1048+
this.handler,
10571049
this.options
10581050
);
10591051
}
@@ -1534,19 +1526,8 @@ class PartialEvaluator {
15341526
const patternBuffer = PatternInfo.write(patternIR);
15351527
transfers.push(patternBuffer);
15361528
this.handler.send("commonobj", [id, "Pattern", patternBuffer], transfers);
1537-
this.rendererHandler.send(
1538-
"commonobj",
1539-
[id, "Pattern", patternBuffer],
1540-
transfers
1541-
);
15421529
} else {
15431530
this.handler.send("obj", [id, this.pageIndex, "Pattern", patternIR]);
1544-
this.rendererHandler.send("obj", [
1545-
id,
1546-
this.pageIndex,
1547-
"Pattern",
1548-
patternIR,
1549-
]);
15501531
}
15511532
return id;
15521533
}

src/core/worker.js

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

967+
handler.on("FontFallback", function (data) {
968+
return pdfManager.fontFallback(data.id, handler);
969+
});
970+
967971
rendererHandler.on("FontFallback", function (data) {
968972
return pdfManager.fontFallback(data.id, rendererHandler);
969973
});

src/display/api.js

Lines changed: 9 additions & 110 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,6 @@ import {
4747
RenderingCancelledException,
4848
StatTimer,
4949
} from "./display_utils.js";
50-
import { FontFaceObject, FontLoader } from "./font_loader.js";
51-
import {
52-
FontInfo,
53-
FontPathInfo,
54-
PatternInfo,
55-
} from "../shared/obj-bin-transform.js";
5650
import {
5751
getDataProp,
5852
getFactoryUrlProp,
@@ -75,13 +69,15 @@ import { DOMCMapReaderFactory } from "display-cmap_reader_factory";
7569
import { DOMFilterFactory } from "./filter_factory.js";
7670
import { DOMStandardFontDataFactory } from "display-standard_fontdata_factory";
7771
import { DOMWasmFactory } from "display-wasm_factory";
72+
import { FontLoader } from "./font_loader.js";
7873
import { GlobalWorkerOptions } from "./worker_options.js";
7974
import { Metadata } from "./metadata.js";
8075
import { OptionalContentConfig } from "./optional_content_config.js";
8176
import { PDFDataTransportStream } from "./transport_stream.js";
8277
import { PDFFetchStream } from "display-fetch_stream";
8378
import { PDFNetworkStream } from "display-network";
8479
import { PDFNodeStream } from "display-node_stream";
80+
import { setupHandler } from "../shared/handle_objs.js";
8581
import { TextLayer } from "./text_layer.js";
8682
import { XfaText } from "./xfa_text.js";
8783

@@ -2873,110 +2869,13 @@ class WorkerTransport {
28732869
page._startRenderPage(data.transparency, data.cacheKey);
28742870
});
28752871

2876-
messageHandler.on("commonobj", ([id, type, exportedData]) => {
2877-
if (this.destroyed) {
2878-
return null; // Ignore any pending requests if the worker was terminated.
2879-
}
2880-
2881-
if (this.commonObjs.has(id)) {
2882-
return null;
2883-
}
2884-
2885-
switch (type) {
2886-
case "Font":
2887-
if ("error" in exportedData) {
2888-
const exportedError = exportedData.error;
2889-
warn(`Error during font loading: ${exportedError}`);
2890-
this.commonObjs.resolve(id, exportedError);
2891-
break;
2892-
}
2893-
2894-
const fontData = new FontInfo(exportedData);
2895-
const inspectFont =
2896-
this._params.pdfBug && globalThis.FontInspector?.enabled
2897-
? (font, url) => globalThis.FontInspector.fontAdded(font, url)
2898-
: null;
2899-
const font = new FontFaceObject(
2900-
fontData,
2901-
inspectFont,
2902-
exportedData.extra,
2903-
exportedData.charProcOperatorList
2904-
);
2905-
2906-
this.fontLoader
2907-
.bind(font)
2908-
.catch(() => messageHandler.sendWithPromise("FontFallback", { id }))
2909-
.finally(() => {
2910-
if (!font.fontExtraProperties && font.data) {
2911-
// Immediately release the `font.data` property once the font
2912-
// has been attached to the DOM, since it's no longer needed,
2913-
// rather than waiting for a `PDFDocumentProxy.cleanup` call.
2914-
// Since `font.data` could be very large, e.g. in some cases
2915-
// multiple megabytes, this will help reduce memory usage.
2916-
font.clearData();
2917-
}
2918-
this.commonObjs.resolve(id, font);
2919-
});
2920-
break;
2921-
case "CopyLocalImage":
2922-
const { imageRef } = exportedData;
2923-
assert(imageRef, "The imageRef must be defined.");
2924-
2925-
for (const pageProxy of this.#pageCache.values()) {
2926-
for (const [, data] of pageProxy.objs) {
2927-
if (data?.ref !== imageRef) {
2928-
continue;
2929-
}
2930-
if (!data.dataLen) {
2931-
return null;
2932-
}
2933-
this.commonObjs.resolve(id, structuredClone(data));
2934-
return data.dataLen;
2935-
}
2936-
}
2937-
break;
2938-
case "FontPath":
2939-
this.commonObjs.resolve(id, new FontPathInfo(exportedData));
2940-
break;
2941-
case "Image":
2942-
this.commonObjs.resolve(id, exportedData);
2943-
break;
2944-
case "Pattern":
2945-
const pattern = new PatternInfo(exportedData);
2946-
this.commonObjs.resolve(id, pattern.getIR());
2947-
break;
2948-
default:
2949-
throw new Error(`Got unknown common object type ${type}`);
2950-
}
2951-
2952-
return null;
2953-
});
2954-
2955-
messageHandler.on("obj", ([id, pageIndex, type, imageData]) => {
2956-
if (this.destroyed) {
2957-
// Ignore any pending requests if the worker was terminated.
2958-
return;
2959-
}
2960-
2961-
const pageProxy = this.#pageCache.get(pageIndex);
2962-
if (pageProxy.objs.has(id)) {
2963-
return;
2964-
}
2965-
// Don't store data *after* cleanup has successfully run, see bug 1854145.
2966-
if (pageProxy._intentStates.size === 0) {
2967-
imageData?.bitmap?.close(); // Release any `ImageBitmap` data.
2968-
return;
2969-
}
2970-
2971-
switch (type) {
2972-
case "Image":
2973-
case "Pattern":
2974-
pageProxy.objs.resolve(id, imageData);
2975-
break;
2976-
default:
2977-
throw new Error(`Got unknown object type ${type}`);
2978-
}
2979-
});
2872+
setupHandler(
2873+
messageHandler,
2874+
this.destroyed,
2875+
this.commonObjs,
2876+
this.#pageCache,
2877+
this.fontLoader
2878+
);
29802879

29812880
messageHandler.on("DocProgress", data => {
29822881
if (this.destroyed) {

0 commit comments

Comments
 (0)