Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[api-minor] Sort PopupAnnotations already on the worker-thread (PR 11535 follow-up) #15283

Merged
merged 1 commit into from
Aug 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/core/annotation.js
Original file line number Diff line number Diff line change
Expand Up @@ -4092,4 +4092,5 @@ export {
AnnotationFactory,
getQuadPoints,
MarkupAnnotation,
PopupAnnotation,
};
29 changes: 27 additions & 2 deletions src/core/document.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* limitations under the License.
*/

import { AnnotationFactory, PopupAnnotation } from "./annotation.js";
import {
assert,
FormatError,
Expand Down Expand Up @@ -42,7 +43,6 @@ import {
} from "./core_utils.js";
import { Dict, isName, Name, Ref } from "./primitives.js";
import { getXfaFontDict, getXfaFontName } from "./xfa_fonts.js";
import { AnnotationFactory } from "./annotation.js";
import { BaseStream } from "./base_stream.js";
import { calculateMD5 } from "./crypto.js";
import { Catalog } from "./catalog.js";
Expand Down Expand Up @@ -656,7 +656,32 @@ class Page {
}

return Promise.all(annotationPromises).then(function (annotations) {
return annotations.filter(annotation => !!annotation);
if (annotations.length === 0) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it can be simplified in using sort function.
I just tried:

["a",1, 2, "d", "e", 3, "aa"].sort((a, b) => typeof a === typeof b ? 0 : (typeof a === "string" ? -1 : +1))

and it works.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To be honest, I'm not finding such a code-snippet particularly easy to read/understand at a glance.
Also, wouldn't you still need to filter out any empty entries first? If so, that'd effectively require two loops through the Array rather than just the one.

return annotations;
}

const sortedAnnotations = [];
let popupAnnotations;
// Ensure that PopupAnnotations are handled last, since they depend on
// their parent Annotation in the display layer; fixes issue 11362.
for (const annotation of annotations) {
if (!annotation) {
continue;
}
if (annotation instanceof PopupAnnotation) {
if (!popupAnnotations) {
popupAnnotations = [];
}
popupAnnotations.push(annotation);
continue;
}
sortedAnnotations.push(annotation);
}
if (popupAnnotations) {
sortedAnnotations.push(...popupAnnotations);
}

return sortedAnnotations;
});
});

Expand Down
27 changes: 5 additions & 22 deletions src/display/annotation_layer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2477,30 +2477,13 @@ class AnnotationLayer {

this.#setDimensions(div, viewport);

const sortedAnnotations = [],
popupAnnotations = [];
// Ensure that Popup annotations are handled last, since they're dependant
// upon the parent annotation having already been rendered (please refer to
// the `PopupAnnotationElement.render` method); fixes issue 11362.
for (const data of annotations) {
if (!data) {
continue;
}
if (data.annotationType === AnnotationType.POPUP) {
popupAnnotations.push(data);
continue;
}
const { width, height } = getRectDims(data.rect);
if (width <= 0 || height <= 0) {
continue; // Ignore empty annotations.
if (data.annotationType !== AnnotationType.POPUP) {
const { width, height } = getRectDims(data.rect);
if (width <= 0 || height <= 0) {
continue; // Ignore empty annotations.
}
}
sortedAnnotations.push(data);
}
if (popupAnnotations.length) {
sortedAnnotations.push(...popupAnnotations);
}

for (const data of sortedAnnotations) {
const element = AnnotationElementFactory.create({
data,
layer: div,
Expand Down