Compare commits

..

No commits in common. "00e3a4d87a22d92938687f7535bc35961a2a40e3" and "6243afa85e4639da6435c075913512f6bfbdd291" have entirely different histories.

View File

@ -251,20 +251,20 @@ class Page {
/** /**
* @returns {Promise<BaseStream>} * @returns {Promise<BaseStream>}
*/ */
async getContentStream() { getContentStream() {
const content = await this.pdfManager.ensure(this, "content"); return this.pdfManager.ensure(this, "content").then(content => {
if (content instanceof BaseStream) {
if (content instanceof BaseStream) { return content;
return content; }
} if (Array.isArray(content)) {
if (Array.isArray(content)) { return new StreamsSequenceStream(
return new StreamsSequenceStream( content,
content, this._onSubStreamError.bind(this)
this._onSubStreamError.bind(this) );
); }
} // Replace non-existent page content with empty content.
// Replace non-existent page content with empty content. return new NullStream();
return new NullStream(); });
} }
get xfaData() { get xfaData() {
@ -375,7 +375,7 @@ class Page {
} }
} }
async save(handler, task, annotationStorage, changes) { save(handler, task, annotationStorage, changes) {
const partialEvaluator = new PartialEvaluator({ const partialEvaluator = new PartialEvaluator({
xref: this.xref, xref: this.xref,
handler, handler,
@ -392,34 +392,37 @@ class Page {
// Fetch the page's annotations and save the content // Fetch the page's annotations and save the content
// in case of interactive form fields. // in case of interactive form fields.
const annotations = await this._parsedAnnotations; return this._parsedAnnotations.then(function (annotations) {
const promises = [];
for (const annotation of annotations) {
promises.push(
annotation
.save(partialEvaluator, task, annotationStorage, changes)
.catch(function (reason) {
warn(
"save - ignoring annotation data during " +
`"${task.name}" task: "${reason}".`
);
return null;
})
);
}
const promises = []; return Promise.all(promises);
for (const annotation of annotations) { });
promises.push(
annotation
.save(partialEvaluator, task, annotationStorage, changes)
.catch(function (reason) {
warn(
"save - ignoring annotation data during " +
`"${task.name}" task: "${reason}".`
);
return null;
})
);
}
return Promise.all(promises);
} }
async loadResources(keys) { loadResources(keys) {
// TODO: add async `_getInheritableProperty` and remove this. // TODO: add async `_getInheritableProperty` and remove this.
await (this.resourcesPromise ??= this.pdfManager.ensure(this, "resources")); this.resourcesPromise ||= this.pdfManager.ensure(this, "resources");
const objectLoader = new ObjectLoader(this.resources, keys, this.xref); return this.resourcesPromise.then(() => {
await objectLoader.load(); const objectLoader = new ObjectLoader(this.resources, keys, this.xref);
return objectLoader.load();
});
} }
async getOperatorList({ getOperatorList({
handler, handler,
sink, sink,
task, task,
@ -524,7 +527,7 @@ class Page {
const pageListPromise = Promise.all([ const pageListPromise = Promise.all([
contentStreamPromise, contentStreamPromise,
resourcesPromise, resourcesPromise,
]).then(async ([contentStream]) => { ]).then(([contentStream]) => {
const opList = new OperatorList(intent, sink); const opList = new OperatorList(intent, sink);
handler.send("StartRenderPage", { handler.send("StartRenderPage", {
@ -536,102 +539,109 @@ class Page {
cacheKey, cacheKey,
}); });
await partialEvaluator.getOperatorList({ return partialEvaluator
stream: contentStream, .getOperatorList({
task, stream: contentStream,
resources: this.resources, task,
operatorList: opList, resources: this.resources,
}); operatorList: opList,
return opList; })
.then(() => opList);
}); });
// Fetch the page's annotations and add their operator lists to the // Fetch the page's annotations and add their operator lists to the
// page's operator list to render them. // page's operator list to render them.
// eslint-disable-next-line prefer-const return Promise.all([
let [pageOpList, annotations, newAnnotations] = await Promise.all([
pageListPromise, pageListPromise,
this._parsedAnnotations, this._parsedAnnotations,
newAnnotationsPromise, newAnnotationsPromise,
]); ]).then(function ([pageOpList, annotations, newAnnotations]) {
if (newAnnotations) {
if (newAnnotations) { // Some annotations can already exist (if it has the refToReplace
// Some annotations can already exist (if it has the refToReplace // property). In this case, we replace the old annotation by the new
// property). In this case, we replace the old annotation by the new one. // one.
annotations = annotations.filter( annotations = annotations.filter(
a => !(a.ref && deletedAnnotations.has(a.ref)) a => !(a.ref && deletedAnnotations.has(a.ref))
); );
for (let i = 0, ii = newAnnotations.length; i < ii; i++) { for (let i = 0, ii = newAnnotations.length; i < ii; i++) {
const newAnnotation = newAnnotations[i]; const newAnnotation = newAnnotations[i];
if (newAnnotation.refToReplace) { if (newAnnotation.refToReplace) {
const j = annotations.findIndex( const j = annotations.findIndex(
a => a.ref && isRefsEqual(a.ref, newAnnotation.refToReplace) a => a.ref && isRefsEqual(a.ref, newAnnotation.refToReplace)
); );
if (j >= 0) { if (j >= 0) {
annotations.splice(j, 1, newAnnotation); annotations.splice(j, 1, newAnnotation);
newAnnotations.splice(i--, 1); newAnnotations.splice(i--, 1);
ii--; ii--;
}
} }
} }
annotations = annotations.concat(newAnnotations);
} }
annotations = annotations.concat(newAnnotations);
}
if (
annotations.length === 0 ||
intent & RenderingIntentFlag.ANNOTATIONS_DISABLE
) {
pageOpList.flush(/* lastChunk = */ true);
return { length: pageOpList.totalLength };
}
const renderForms = !!(intent & RenderingIntentFlag.ANNOTATIONS_FORMS),
isEditing = !!(intent & RenderingIntentFlag.IS_EDITING),
intentAny = !!(intent & RenderingIntentFlag.ANY),
intentDisplay = !!(intent & RenderingIntentFlag.DISPLAY),
intentPrint = !!(intent & RenderingIntentFlag.PRINT);
// Collect the operator list promises for the annotations. Each promise
// is resolved with the complete operator list for a single annotation.
const opListPromises = [];
for (const annotation of annotations) {
if ( if (
intentAny || annotations.length === 0 ||
(intentDisplay && intent & RenderingIntentFlag.ANNOTATIONS_DISABLE
annotation.mustBeViewed(annotationStorage, renderForms) &&
annotation.mustBeViewedWhenEditing(isEditing, modifiedIds)) ||
(intentPrint && annotation.mustBePrinted(annotationStorage))
) { ) {
opListPromises.push( pageOpList.flush(/* lastChunk = */ true);
annotation return { length: pageOpList.totalLength };
.getOperatorList(partialEvaluator, task, intent, annotationStorage)
.catch(function (reason) {
warn(
"getOperatorList - ignoring annotation data during " +
`"${task.name}" task: "${reason}".`
);
return {
opList: null,
separateForm: false,
separateCanvas: false,
};
})
);
} }
} const renderForms = !!(intent & RenderingIntentFlag.ANNOTATIONS_FORMS),
isEditing = !!(intent & RenderingIntentFlag.IS_EDITING),
intentAny = !!(intent & RenderingIntentFlag.ANY),
intentDisplay = !!(intent & RenderingIntentFlag.DISPLAY),
intentPrint = !!(intent & RenderingIntentFlag.PRINT);
const opLists = await Promise.all(opListPromises); // Collect the operator list promises for the annotations. Each promise
let form = false, // is resolved with the complete operator list for a single annotation.
canvas = false; const opListPromises = [];
for (const annotation of annotations) {
if (
intentAny ||
(intentDisplay &&
annotation.mustBeViewed(annotationStorage, renderForms) &&
annotation.mustBeViewedWhenEditing(isEditing, modifiedIds)) ||
(intentPrint && annotation.mustBePrinted(annotationStorage))
) {
opListPromises.push(
annotation
.getOperatorList(
partialEvaluator,
task,
intent,
annotationStorage
)
.catch(function (reason) {
warn(
"getOperatorList - ignoring annotation data during " +
`"${task.name}" task: "${reason}".`
);
return {
opList: null,
separateForm: false,
separateCanvas: false,
};
})
);
}
}
for (const { opList, separateForm, separateCanvas } of opLists) { return Promise.all(opListPromises).then(function (opLists) {
pageOpList.addOpList(opList); let form = false,
canvas = false;
form ||= separateForm; for (const { opList, separateForm, separateCanvas } of opLists) {
canvas ||= separateCanvas; pageOpList.addOpList(opList);
}
pageOpList.flush( form ||= separateForm;
/* lastChunk = */ true, canvas ||= separateCanvas;
/* separateAnnots = */ { form, canvas } }
); pageOpList.flush(
return { length: pageOpList.totalLength }; /* lastChunk = */ true,
/* separateAnnots = */ { form, canvas }
);
return { length: pageOpList.totalLength };
});
});
} }
async extractTextContent({ async extractTextContent({