1
0
Fork 0
mirror of https://github.com/mozilla/pdf.js.git synced 2025-04-20 15:18:08 +02:00

Merge pull request #17986 from calixteman/fix_struct_tree

Allow to insert several annotations under the same parent in the structure tree
This commit is contained in:
calixteman 2024-04-24 18:32:00 +02:00 committed by GitHub
commit d1f494d68c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 243 additions and 112 deletions

View file

@ -645,3 +645,4 @@
!issue12213.pdf
!tracemonkey_freetext.pdf
!issue17998.pdf
!pdfjs_wikipedia.pdf

BIN
test/pdfs/pdfjs_wikipedia.pdf Executable file

Binary file not shown.

View file

@ -1030,6 +1030,20 @@ describe("api", function () {
await pdfLoadingTask.destroy();
});
function findNode(parent, node, index, check) {
if (check(node)) {
return [parent.children[index - 1], node];
}
for (let i = 0; i < node.children?.length ?? 0; i++) {
const child = node.children[i];
const elements = findNode(node, child, i, check);
if (elements) {
return elements;
}
}
return null;
}
it("gets number of pages", function () {
expect(pdfDocument.numPages).toEqual(3);
});
@ -2396,7 +2410,22 @@ describe("api", function () {
pdfDoc = await loadingTask.promise;
const page = await pdfDoc.getPage(1);
const tree = await page.getStructTree();
const leaf = tree.children[0].children[6].children[1];
const [predecessor, leaf] = findNode(
null,
tree,
0,
node => node.role === "Figure"
);
expect(predecessor).toEqual({
role: "Span",
children: [
{
type: "content",
id: "p3R_mc12",
},
],
});
expect(leaf).toEqual({
role: "Figure",
@ -2412,6 +2441,104 @@ describe("api", function () {
await loadingTask.destroy();
});
it("write a new stamp annotation in a tagged pdf (with some MCIDs), save and check the structure tree", async function () {
if (isNodeJS) {
pending("Cannot create a bitmap from Node.js.");
}
const TEST_IMAGES_PATH = "../images/";
const filename = "firefox_logo.png";
const path = new URL(TEST_IMAGES_PATH + filename, window.location).href;
const response = await fetch(path);
const blob = await response.blob();
const bitmap = await createImageBitmap(blob);
let loadingTask = getDocument(
buildGetDocumentParams("pdfjs_wikipedia.pdf")
);
let pdfDoc = await loadingTask.promise;
for (let i = 0; i < 2; i++) {
pdfDoc.annotationStorage.setValue(`pdfjs_internal_editor_${i}`, {
annotationType: AnnotationEditorType.STAMP,
bitmapId: `im${i}`,
pageIndex: 0,
rect: [257 + i, 572 + i, 286 + i, 603 + i],
rotation: 0,
isSvg: false,
structTreeParentId: "p2R_mc155",
accessibilityData: {
type: "Figure",
alt: `Firefox logo ${i}`,
},
bitmap: structuredClone(bitmap),
});
}
const data = await pdfDoc.saveDocument();
await loadingTask.destroy();
loadingTask = getDocument(data);
pdfDoc = await loadingTask.promise;
const page = await pdfDoc.getPage(1);
const tree = await page.getStructTree();
let [predecessor, figure] = findNode(
null,
tree,
0,
node => node.role === "Figure" && node.alt === "Firefox logo 1"
);
expect(predecessor).toEqual({
role: "NonStruct",
children: [
{
type: "content",
id: "p2R_mc155",
},
],
});
expect(figure).toEqual({
role: "Figure",
children: [
{
type: "annotation",
id: "pdfjs_internal_id_420R",
},
],
alt: "Firefox logo 1",
});
[predecessor, figure] = findNode(
null,
tree,
0,
node => node.role === "Figure" && node.alt === "Firefox logo 0"
);
expect(predecessor).toEqual({
role: "Figure",
children: [
{
type: "annotation",
id: "pdfjs_internal_id_420R",
},
],
alt: "Firefox logo 1",
});
expect(figure).toEqual({
role: "Figure",
children: [
{
type: "annotation",
id: "pdfjs_internal_id_416R",
},
],
alt: "Firefox logo 0",
});
await loadingTask.destroy();
});
it("write a new stamp annotation in a tagged pdf, save, repeat and check the structure tree", async function () {
if (isNodeJS) {
pending("Cannot create a bitmap from Node.js.");

View file

@ -498,6 +498,15 @@ describe("primitives", function () {
cache.put(ref2, obj2);
expect([...cache]).toEqual([obj1, obj2]);
});
it("should support iteration over key-value pairs", function () {
cache.put(ref1, obj1);
cache.put(ref2, obj2);
expect([...cache.items()]).toEqual([
[ref1, obj1],
[ref2, obj2],
]);
});
});
describe("isName", function () {