Skip to content

Commit 8c53bf8

Browse files
authored
Merge pull request #13437 from calixteman/xfa_mv_root
XFA - Move the fake HTML representation of XFA from the worker to the main thread
2 parents dd0014e + 45c3f00 commit 8c53bf8

File tree

5 files changed

+50
-32
lines changed

5 files changed

+50
-32
lines changed

src/core/document.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,7 @@ class Page {
146146

147147
_getBoundingBox(name) {
148148
if (this.xfaData) {
149-
const { width, height } = this.xfaData.attributes.style;
150-
return [0, 0, parseInt(width), parseInt(height)];
149+
return this.xfaData.bbox;
151150
}
152151

153152
const box = this._getInheritableProperty(name, /* getArray = */ true);
@@ -241,7 +240,9 @@ class Page {
241240

242241
get xfaData() {
243242
if (this.xfaFactory) {
244-
return shadow(this, "xfaData", this.xfaFactory.getPage(this.pageIndex));
243+
return shadow(this, "xfaData", {
244+
bbox: this.xfaFactory.getBoundingBox(this.pageIndex),
245+
});
245246
}
246247
return shadow(this, "xfaData", null);
247248
}
@@ -851,8 +852,11 @@ class PDFDocument {
851852
return shadow(this, "xfaFaxtory", null);
852853
}
853854

854-
get isPureXfa() {
855-
return this.xfaFactory !== null;
855+
get htmlForXfa() {
856+
if (this.xfaFactory) {
857+
return this.xfaFactory.getPages();
858+
}
859+
return null;
856860
}
857861

858862
async loadXfaFonts(handler, task) {

src/core/worker.js

+4-10
Original file line numberDiff line numberDiff line change
@@ -187,13 +187,13 @@ class WorkerMessageHandler {
187187
await pdfManager.ensureDoc("checkFirstPage");
188188
}
189189

190-
const [numPages, fingerprint, isPureXfa] = await Promise.all([
190+
const [numPages, fingerprint, htmlForXfa] = await Promise.all([
191191
pdfManager.ensureDoc("numPages"),
192192
pdfManager.ensureDoc("fingerprint"),
193-
pdfManager.ensureDoc("isPureXfa"),
193+
pdfManager.ensureDoc("htmlForXfa"),
194194
]);
195195

196-
if (isPureXfa) {
196+
if (htmlForXfa) {
197197
const task = new WorkerTask("loadXfaFonts");
198198
startWorkerTask(task);
199199
await pdfManager
@@ -203,7 +203,7 @@ class WorkerMessageHandler {
203203
})
204204
.then(() => finishWorkerTask(task));
205205
}
206-
return { numPages, fingerprint, isPureXfa };
206+
return { numPages, fingerprint, htmlForXfa };
207207
}
208208

209209
function getPdfManager(data, evaluatorOptions, enableXfa) {
@@ -501,12 +501,6 @@ class WorkerMessageHandler {
501501
});
502502
});
503503

504-
handler.on("GetPageXfa", function wphSetupGetXfa({ pageIndex }) {
505-
return pdfManager.getPage(pageIndex).then(function (page) {
506-
return pdfManager.ensure(page, "xfaData");
507-
});
508-
});
509-
510504
handler.on("GetOutline", function wphSetupGetOutline(data) {
511505
return pdfManager.ensureCatalog("documentOutline");
512506
});

src/core/xfa/factory.js

+21-4
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,35 @@ class XFAFactory {
2222
try {
2323
this.root = new XFAParser().parse(XFAFactory._createDocument(data));
2424
this.form = new Binder(this.root).bind();
25-
this.pages = this.form[$toHTML]();
25+
this._createPages();
2626
} catch (e) {
2727
console.log(e);
2828
}
2929
}
3030

31-
getPage(pageIndex) {
32-
return this.pages.children[pageIndex];
31+
_createPages() {
32+
this.pages = this.form[$toHTML]();
33+
this.dims = this.pages.children.map(c => {
34+
const { width, height } = c.attributes.style;
35+
return [0, 0, parseInt(width), parseInt(height)];
36+
});
37+
}
38+
39+
getBoundingBox(pageIndex) {
40+
return this.dims[pageIndex];
3341
}
3442

3543
get numberPages() {
36-
return this.pages.children.length;
44+
return this.dims.length;
45+
}
46+
47+
getPages() {
48+
if (!this.pages) {
49+
this._createPages();
50+
}
51+
const pages = this.pages;
52+
this.pages = null;
53+
return pages;
3754
}
3855

3956
static _createDocument(data) {

src/display/api.js

+13-11
Original file line numberDiff line numberDiff line change
@@ -695,7 +695,15 @@ class PDFDocumentProxy {
695695
* @type {boolean} True if only XFA form.
696696
*/
697697
get isPureXfa() {
698-
return this._pdfInfo.isPureXfa;
698+
return !!this._transport._htmlForXfa;
699+
}
700+
701+
/**
702+
* @type {Object | null} An object representing a HTML tree structure
703+
* to render the XFA, or `null` when no XFA form exists.
704+
*/
705+
get allXfaHtml() {
706+
return this._transport._htmlForXfa;
699707
}
700708

701709
/**
@@ -1253,8 +1261,8 @@ class PDFPageProxy {
12531261
* are {Object} with a name, attributes (class, style, ...), value and
12541262
* children, very similar to a HTML DOM tree), or `null` if no XFA exists.
12551263
*/
1256-
getXfa() {
1257-
return (this._xfaPromise ||= this._transport.getPageXfa(this._pageIndex));
1264+
async getXfa() {
1265+
return this._transport._htmlForXfa?.children[this._pageIndex] || null;
12581266
}
12591267

12601268
/**
@@ -1540,7 +1548,6 @@ class PDFPageProxy {
15401548
this.objs.clear();
15411549
this._annotationsPromise = null;
15421550
this._jsActionsPromise = null;
1543-
this._xfaPromise = null;
15441551
this._structTreePromise = null;
15451552
this.pendingCleanup = false;
15461553
return Promise.all(waitOn);
@@ -1576,7 +1583,6 @@ class PDFPageProxy {
15761583
this.objs.clear();
15771584
this._annotationsPromise = null;
15781585
this._jsActionsPromise = null;
1579-
this._xfaPromise = null;
15801586
this._structTreePromise = null;
15811587
if (resetStats && this._stats) {
15821588
this._stats = new StatTimer();
@@ -2444,6 +2450,8 @@ class WorkerTransport {
24442450

24452451
messageHandler.on("GetDoc", ({ pdfInfo }) => {
24462452
this._numPages = pdfInfo.numPages;
2453+
this._htmlForXfa = pdfInfo.htmlForXfa;
2454+
delete pdfInfo.htmlForXfa;
24472455
loadingTask._capability.resolve(new PDFDocumentProxy(pdfInfo, this));
24482456
});
24492457

@@ -2800,12 +2808,6 @@ class WorkerTransport {
28002808
});
28012809
}
28022810

2803-
getPageXfa(pageIndex) {
2804-
return this.messageHandler.sendWithPromise("GetPageXfa", {
2805-
pageIndex,
2806-
});
2807-
}
2808-
28092811
getStructTree(pageIndex) {
28102812
return this.messageHandler.sendWithPromise("GetStructTree", {
28112813
pageIndex,

test/unit/xfa_tohtml_spec.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,8 @@ describe("XFAFactory", function () {
5757

5858
expect(factory.numberPages).toEqual(2);
5959

60-
const page1 = factory.getPage(0);
60+
const pages = factory.getPages();
61+
const page1 = pages.children[0];
6162
expect(page1.attributes.style).toEqual({
6263
height: "789px",
6364
width: "456px",
@@ -99,7 +100,7 @@ describe("XFAFactory", function () {
99100

100101
// draw element must be on each page.
101102
expect(draw.attributes.style).toEqual(
102-
factory.getPage(1).children[1].children[0].attributes.style
103+
pages.children[1].children[1].children[0].attributes.style
103104
);
104105
});
105106
});

0 commit comments

Comments
 (0)