Skip to content

Build your personal knowledge base with Trilium Notes

fix(pdf): show layers nested under a group label

Elian Doran94f628941a31
diff --git a/packages/pdfjs-viewer/src/layers.spec.ts b/packages/pdfjs-viewer/src/layers.spec.ts
index 34e36861f7..df823b2492 100644
--- a/packages/pdfjs-viewer/src/layers.spec.ts
+++ b/packages/pdfjs-viewer/src/layers.spec.ts
@@ -2,13 +2,13 @@
/**
* Drives the optional-content (layer) plumbing against a real `OptionalContentConfig` built by
* pdf.js from the fixtures in {@link ./test/fixture_pdf}, rather than a stand-in with
- * hand-chosen fields. Two of the branches in `layers.ts` turn out to be unreachable against
- * real pdf.js output, and one real shape is mishandled — see the tests below.
+ * hand-chosen fields. Driving the real thing is what surfaced the nested-group handling
+ * covered below: a fake built from our own assumptions would have agreed with them.
*/
import { afterEach, describe, expect, it, vi } from "vitest";
import { setupPdfLayers } from "./layers";
-import { allFeaturesPdf, barePdf, layeredPdf } from "./test/fixture_pdf";
+import { allFeaturesPdf, barePdf, buildPdf, layeredPdf } from "./test/fixture_pdf";
import { InstalledViewer, installViewerApp, uninstallViewerApp } from "./test/viewer_app";
let viewer: InstalledViewer;
@@ -40,7 +40,23 @@ describe("layer extraction", () => {
// The nameless OCG in the fixture must not reach the sidebar as an unlabelled row.
const { layers } = viewer.lastMessageOfType("pdfjs-viewer-layers");
expect(layers.map((layer: any) => layer.name)).not.toContain(null);
- expect(layers.map((layer: any) => layer.name)).toEqual([ "Visible layer", "Hidden layer" ]);
+ });
+
+ it("includes layers nested under a group label and layers missing from /Order", async () => {
+ viewer = await installViewerApp(layeredPdf());
+
+ await setupPdfLayers();
+
+ // pdf.js reports both of these as `{ name, order }` wrappers rather than plain ids:
+ // "Nested layer" sits under a "Grouped" label, and "Unordered layer" is absent from
+ // /Order so pdf.js gathers it into a synthetic null-named entry. Grouped layers are
+ // routine in CAD and Illustrator exports, so dropping them hid real content.
+ expect(viewer.lastMessageOfType("pdfjs-viewer-layers").layers).toEqual([
+ { id: expect.any(String), name: "Visible layer", visible: true },
+ { id: expect.any(String), name: "Hidden layer", visible: false },
+ { id: expect.any(String), name: "Nested layer", visible: true },
+ { id: expect.any(String), name: "Unordered layer", visible: true }
+ ]);
});
it("reports an empty list for a document with no optional content", async () => {
@@ -110,23 +126,21 @@ describe("layer toggling", () => {
});
});
-describe("getOrder() shapes we do not handle", () => {
- it("DEFECT: silently drops layers nested under a group label", async () => {
- viewer = await installViewerApp(layeredPdf());
+describe("nesting depth", () => {
+ it("reaches layers nested more than one level deep", async () => {
+ // pdf.js allows nesting up to MAX_NESTED_LEVELS, so the flattening has to recurse
+ // rather than special-case a single level.
+ viewer = await installViewerApp(buildPdf([
+ "<< /Type /Catalog /Pages 2 0 R /OCProperties << /OCGs [4 0 R] "
+ + "/D << /Order [[(Outer) [(Inner) 4 0 R]]] /ON [4 0 R] >> >> >>",
+ "<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>",
+ "<< /Type /OCG /Name (Deeply nested layer) >>"
+ ]));
await setupPdfLayers();
- // pdf.js returns a nested group as `{ name, order: [...] }`. `flattenOrder` only
- // recurses into bare arrays and only reads `item.id` off objects, so neither branch
- // matches and "Nested layer" never reaches the sidebar. Grouped layers are common in
- // CAD/Illustrator exports, so this is a real gap, not a fixture artefact — the fix is
- // to recurse into `item.order`. Pinned here so it is visible rather than silent.
- const { layers } = viewer.lastMessageOfType("pdfjs-viewer-layers");
- expect(layers.map((layer: any) => layer.name)).not.toContain("Nested layer");
-
- // Meanwhile pdf.js does know about it, which is what makes this our bug.
- const config = await viewer.pdfDocument.getOptionalContentConfig();
- const nested = config?.getOrder().find((item: any) => typeof item === "object");
- expect(config?.getGroup(nested.order[0]).name).toBe("Nested layer");
+ expect(viewer.lastMessageOfType("pdfjs-viewer-layers").layers)
+ .toEqual([ { id: expect.any(String), name: "Deeply nested layer", visible: true } ]);
});
});
diff --git a/packages/pdfjs-viewer/src/layers.ts b/packages/pdfjs-viewer/src/layers.ts
index 3c80c089c8..95395525bb 100644
--- a/packages/pdfjs-viewer/src/layers.ts
+++ b/packages/pdfjs-viewer/src/layers.ts
@@ -43,16 +43,18 @@ async function extractAndSendLayers() {
return;
}
- // Flatten the order array (it can be nested) and extract group IDs
+ // Flatten the order and extract group IDs. pdf.js produces exactly two kinds of entry
+ // (see parseOrder/parseNestedOrder in its catalog): a group id string, or a
+ // `{ name, order }` wrapper. The wrapper covers both layers the document nests under a
+ // label and a synthetic, null-named entry collecting groups left out of /Order — so
+ // failing to recurse into it hides those layers entirely.
const groupIds: string[] = [];
const flattenOrder = (items: any[]) => {
for (const item of items) {
- if (typeof item === 'string') {
+ if (typeof item === "string") {
groupIds.push(item);
- } else if (Array.isArray(item)) {
- flattenOrder(item);
- } else if (item && typeof item === 'object' && item.id) {
- groupIds.push(item.id);
+ } else if (item?.order) {
+ flattenOrder(item.order);
}
}
};
diff --git a/packages/pdfjs-viewer/src/test/fixture_pdf.ts b/packages/pdfjs-viewer/src/test/fixture_pdf.ts
index e2ab523e14..1bbba2d806 100644
--- a/packages/pdfjs-viewer/src/test/fixture_pdf.ts
+++ b/packages/pdfjs-viewer/src/test/fixture_pdf.ts
@@ -88,21 +88,30 @@ export function allFeaturesPdf(): Uint8Array {
}
/**
- * A document whose optional-content groups cover the awkward cases `layers.ts` filters on:
- * a normal visible layer, a hidden one, one with no `/Name`, and one nested under a label.
+ * A document whose optional-content groups cover every awkward case `layers.ts` has to cope
+ * with: a normal visible layer, a hidden one, one with no `/Name`, one nested under a group
+ * label, and one left out of `/Order` entirely.
+ *
+ * The last two both reach us as `{ name, order }` entries — a nested group keeps its label,
+ * while groups missing from `/Order` are gathered into a synthetic entry with a null name (see
+ * `parseOrder`/`parseNestedOrder` in pdf.js' catalog). Note the nesting syntax: the label
+ * belongs *inside* the sub-array, as `[(Grouped) 7 0 R]`.
*/
export function layeredPdf(): Uint8Array {
return buildPdf([
"<< /Type /Catalog /Pages 2 0 R /OCProperties "
- + "<< /OCGs [4 0 R 5 0 R 6 0 R 7 0 R] "
- + "/D << /Order [4 0 R 5 0 R 6 0 R (Grouped) [7 0 R]] /ON [4 0 R 6 0 R 7 0 R] /OFF [5 0 R] >> >> >>",
+ + "<< /OCGs [4 0 R 5 0 R 6 0 R 7 0 R 8 0 R] "
+ + "/D << /Order [4 0 R 5 0 R 6 0 R [(Grouped) 7 0 R]] "
+ + "/ON [4 0 R 6 0 R 7 0 R 8 0 R] /OFF [5 0 R] >> >> >>",
"<< /Type /Pages /Kids [3 0 R] /Count 1 >>",
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] >>",
"<< /Type /OCG /Name (Visible layer) >>",
"<< /Type /OCG /Name (Hidden layer) >>",
// No /Name: pdf.js reports `name: null`, which we must not surface as a layer.
"<< /Type /OCG >>",
- "<< /Type /OCG /Name (Nested layer) >>"
+ "<< /Type /OCG /Name (Nested layer) >>",
+ // Absent from /Order, so pdf.js hands it back under the synthetic null-named entry.
+ "<< /Type /OCG /Name (Unordered layer) >>"
]);
}
diff --git a/packages/pdfjs-viewer/src/upstream_contract.spec.ts b/packages/pdfjs-viewer/src/upstream_contract.spec.ts
index af332d08bc..77bcb4cf77 100644
--- a/packages/pdfjs-viewer/src/upstream_contract.spec.ts
+++ b/packages/pdfjs-viewer/src/upstream_contract.spec.ts
@@ -18,11 +18,18 @@ import { afterEach, beforeEach, describe, expect, it } from "vitest";
import { AnnotationType } from "./annotations";
import { ANNOTATION_EDITOR_MODE_NONE } from "./editing";
-import { allFeaturesPdf } from "./test/fixture_pdf";
+import { allFeaturesPdf, layeredPdf } from "./test/fixture_pdf";
import { InstalledViewer, installViewerApp, uninstallViewerApp } from "./test/viewer_app";
let viewer: InstalledViewer;
+/** Swaps in the layer-heavy fixture and returns its optional-content config. */
+async function installLayeredDocument() {
+ uninstallViewerApp();
+ viewer = await installViewerApp(layeredPdf());
+ return await viewer.optionalContentConfig();
+}
+
beforeEach(async () => {
viewer = await installViewerApp(allFeaturesPdf());
});
@@ -141,6 +148,26 @@ describe("PDFDocumentProxy shape", () => {
expect(config?.getGroup(order[1]).visible).toBe(true);
});
+ it("orders optional-content groups as ids and { name, order } wrappers only", async () => {
+ // `flattenOrder` in layers.ts walks exactly these two shapes. pdf.js builds them in
+ // parseOrder/parseNestedOrder: a group id string, or a wrapper holding nested groups —
+ // the latter used both for labelled groups and for a synthetic, null-named entry
+ // collecting groups left out of /Order. If a third shape ever appears, or nesting stops
+ // being expressed this way, layers silently vanish from the sidebar; fail here instead.
+ const config = await installLayeredDocument();
+ const order = config.getOrder();
+
+ expect(order).toEqual([
+ expect.any(String),
+ expect.any(String),
+ expect.any(String),
+ { name: "Grouped", order: [ expect.any(String) ] },
+ { name: null, order: [ expect.any(String) ] }
+ ]);
+ // Specifically: never a bare nested array, which the old flattening looked for.
+ expect(order.some((item: any) => Array.isArray(item))).toBe(false);
+ });
+
it("keeps annotationStorage's modification hooks reachable", () => {
// `onSetModified` is a private monkey-patch point (custom.ts, annotations.ts) with no
// public type. We cannot bind its contract, but we can catch it disappearing.