Skip to content

Commit

Permalink
Merge pull request #18169 from Snuffleupagus/worker-shorter-code
Browse files Browse the repository at this point in the history
Slightly shorten some worker-thread code
  • Loading branch information
timvandermeij authored May 28, 2024
2 parents 401a1d4 + 27436d5 commit aac738c
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 65 deletions.
119 changes: 60 additions & 59 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -436,77 +436,78 @@ class Page {
options: this.evaluatorOptions,
});

const newAnnotationsByPage = !this.xfaFactory
const newAnnotsByPage = !this.xfaFactory
? getNewAnnotationsMap(annotationStorage)
: null;
const newAnnots = newAnnotsByPage?.get(this.pageIndex);
let newAnnotationsPromise = Promise.resolve(null);
let deletedAnnotations = null;

let newAnnotationsPromise = Promise.resolve(null);
if (newAnnotationsByPage) {
const newAnnotations = newAnnotationsByPage.get(this.pageIndex);
if (newAnnotations) {
const annotationGlobalsPromise =
this.pdfManager.ensureDoc("annotationGlobals");
let imagePromises;

// An annotation can contain a reference to a bitmap, but this bitmap
// is defined in another annotation. So we need to find this annotation
// and generate the bitmap.
const missingBitmaps = new Set();
for (const { bitmapId, bitmap } of newAnnotations) {
if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) {
missingBitmaps.add(bitmapId);
}
if (newAnnots) {
const annotationGlobalsPromise =
this.pdfManager.ensureDoc("annotationGlobals");
let imagePromises;

// An annotation can contain a reference to a bitmap, but this bitmap
// is defined in another annotation. So we need to find this annotation
// and generate the bitmap.
const missingBitmaps = new Set();
for (const { bitmapId, bitmap } of newAnnots) {
if (bitmapId && !bitmap && !missingBitmaps.has(bitmapId)) {
missingBitmaps.add(bitmapId);
}
}

const { isOffscreenCanvasSupported } = this.evaluatorOptions;
if (missingBitmaps.size > 0) {
const annotationWithBitmaps = newAnnotations.slice();
for (const [key, annotation] of annotationStorage) {
if (!key.startsWith(AnnotationEditorPrefix)) {
continue;
}
if (annotation.bitmap && missingBitmaps.has(annotation.bitmapId)) {
annotationWithBitmaps.push(annotation);
}
const { isOffscreenCanvasSupported } = this.evaluatorOptions;
if (missingBitmaps.size > 0) {
const annotationWithBitmaps = newAnnots.slice();
for (const [key, annotation] of annotationStorage) {
if (!key.startsWith(AnnotationEditorPrefix)) {
continue;
}
if (annotation.bitmap && missingBitmaps.has(annotation.bitmapId)) {
annotationWithBitmaps.push(annotation);
}
// The array annotationWithBitmaps cannot be empty: the check above
// makes sure to have at least one annotation containing the bitmap.
imagePromises = AnnotationFactory.generateImages(
annotationWithBitmaps,
this.xref,
isOffscreenCanvasSupported
);
} else {
imagePromises = AnnotationFactory.generateImages(
newAnnotations,
this.xref,
isOffscreenCanvasSupported
);
}
// The array annotationWithBitmaps cannot be empty: the check above
// makes sure to have at least one annotation containing the bitmap.
imagePromises = AnnotationFactory.generateImages(
annotationWithBitmaps,
this.xref,
isOffscreenCanvasSupported
);
} else {
imagePromises = AnnotationFactory.generateImages(
newAnnots,
this.xref,
isOffscreenCanvasSupported
);
}

deletedAnnotations = new RefSet();
this.#replaceIdByRef(newAnnotations, deletedAnnotations, null);
deletedAnnotations = new RefSet();
this.#replaceIdByRef(newAnnots, deletedAnnotations, null);

newAnnotationsPromise = annotationGlobalsPromise.then(
annotationGlobals => {
if (!annotationGlobals) {
return null;
}

return AnnotationFactory.printNewAnnotations(
annotationGlobals,
partialEvaluator,
task,
newAnnotations,
imagePromises
);
newAnnotationsPromise = annotationGlobalsPromise.then(
annotationGlobals => {
if (!annotationGlobals) {
return null;
}
);
}

return AnnotationFactory.printNewAnnotations(
annotationGlobals,
partialEvaluator,
task,
newAnnots,
imagePromises
);
}
);
}
const dataPromises = Promise.all([contentStreamPromise, resourcesPromise]);
const pageListPromise = dataPromises.then(([contentStream]) => {

const pageListPromise = Promise.all([
contentStreamPromise,
resourcesPromise,
]).then(([contentStream]) => {
const opList = new OperatorList(intent, sink);

handler.send("StartRenderPage", {
Expand Down
9 changes: 6 additions & 3 deletions src/core/evaluator.js
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ class PartialEvaluator {
this.globalImageCache = globalImageCache;
this.systemFontCache = systemFontCache;
this.options = options || DefaultPartialEvaluatorOptions;
this.parsingType3Font = false;
this.type3FontRefs = null;

this._regionalImageCache = new RegionalImageCache();
this._fetchBuiltInCMapBound = this.fetchBuiltInCMap.bind(this);
Expand All @@ -243,6 +243,10 @@ class PartialEvaluator {
return shadow(this, "_pdfFunctionFactory", pdfFunctionFactory);
}

get parsingType3Font() {
return !!this.type3FontRefs;
}

clone(newOptions = null) {
const newEvaluator = Object.create(this);
newEvaluator.options = Object.assign(
Expand Down Expand Up @@ -1253,7 +1257,7 @@ class PartialEvaluator {
}
}
if (fontRef) {
if (this.parsingType3Font && this.type3FontRefs.has(fontRef)) {
if (this.type3FontRefs?.has(fontRef)) {
return errorFont();
}

Expand Down Expand Up @@ -4633,7 +4637,6 @@ class TranslatedFont {
// Compared to the parsing of e.g. an entire page, it doesn't really
// make sense to only be able to render a Type3 glyph partially.
const type3Evaluator = evaluator.clone({ ignoreErrors: false });
type3Evaluator.parsingType3Font = true;
// Prevent circular references in Type3 fonts.
const type3FontRefs = new RefSet(evaluator.type3FontRefs);
if (this.dict.objId && !type3FontRefs.has(this.dict.objId)) {
Expand Down
5 changes: 2 additions & 3 deletions src/core/parser.js
Original file line number Diff line number Diff line change
Expand Up @@ -710,11 +710,10 @@ class Parser {
this.shift(); // 'stream'
} else {
// Bad stream length, scanning for endstream command.
const actualLength = this.#findStreamLength(startPos);
if (actualLength < 0) {
length = this.#findStreamLength(startPos);
if (length < 0) {
throw new FormatError("Missing endstream command.");
}
length = actualLength;

lexer.nextChar();
this.shift();
Expand Down

0 comments on commit aac738c

Please sign in to comment.