Skip to content

Build your personal knowledge base with Trilium Notes

fix(pdf): label a free-hand highlight as a highlight, not a drawing

Elian Dorane799c2185e1b
diff --git a/packages/pdfjs-viewer/src/annotations.spec.ts b/packages/pdfjs-viewer/src/annotations.spec.ts
index 472a4dc73e..a461cc6653 100644
--- a/packages/pdfjs-viewer/src/annotations.spec.ts
+++ b/packages/pdfjs-viewer/src/annotations.spec.ts
@@ -81,6 +81,19 @@ describe("processAnnotation", () => {
expect(result.highlightedText).toBe("");
});
+ it("reports a free-hand highlight as a highlight, not as a drawing", () => {
+ // pdf.js stores it as Ink and marks it with /IT; only the pen's own strokes are drawings.
+ const inkHighlight = { ...SAMPLE_HIGHLIGHT, annotationType: AnnotationType.INK, it: "InkHighlight" };
+ expect(processAnnotation(inkHighlight, 1)!.type).toBe("highlight");
+
+ const penStroke = { ...SAMPLE_HIGHLIGHT, annotationType: AnnotationType.INK, it: undefined };
+ expect(processAnnotation(penStroke, 1)!.type).toBe("ink");
+
+ // /IT is shared with other types, where it says nothing about the tool used.
+ const stampedHighlight = { ...SAMPLE_HIGHLIGHT, it: "InkHighlight" };
+ expect(processAnnotation(stampedHighlight, 1)!.type).toBe("highlight");
+ });
+
it("keeps an annotation carrying no text at all", () => {
// pdf.js writes a free-hand highlight as Ink, which never has contents or overlaidText;
// dropping those hid every highlight not drawn over selected text (#11059).
@@ -171,6 +184,14 @@ describe("extraction from a real document", () => {
type: "freetext",
contents: "Typed in the box",
pageNumber: 2
+ }),
+ expect.objectContaining({
+ id: "20R",
+ type: "highlight",
+ contents: "",
+ highlightedText: "",
+ pageNumber: 2,
+ color: "#ffff00"
})
];
@@ -218,7 +239,7 @@ describe("extraction from a real document", () => {
await setupPdfAnnotations();
const { annotations } = viewer.lastMessageOfType("pdfjs-viewer-annotations");
- expect(annotations.map((annotation: any) => annotation.id)).toEqual([ "14R", "17R", "18R", "19R" ]);
+ expect(annotations.map((annotation: any) => annotation.id)).toEqual([ "14R", "17R", "18R", "19R", "20R" ]);
});
it("reports an empty list when extraction fails", async () => {
@@ -271,7 +292,8 @@ describe("re-extraction from saved bytes", () => {
expect.objectContaining({ contents: "A sticky note" }),
expect.objectContaining({ id: "17R", type: "highlight" }),
expect.objectContaining({ id: "18R", type: "ink" }),
- expect.objectContaining({ id: "19R", type: "freetext", contents: "Typed in the box" })
+ expect.objectContaining({ id: "19R", type: "freetext", contents: "Typed in the box" }),
+ expect.objectContaining({ id: "20R", type: "highlight" })
]);
// The temporary document owns a worker, so failing to destroy it leaks one per save.
expect(destroy).toHaveBeenCalledTimes(1);
diff --git a/packages/pdfjs-viewer/src/annotations.ts b/packages/pdfjs-viewer/src/annotations.ts
index f5e8758b1b..8a4a1cccaa 100644
--- a/packages/pdfjs-viewer/src/annotations.ts
+++ b/packages/pdfjs-viewer/src/annotations.ts
@@ -43,7 +43,7 @@ export function processAnnotation(ann: Record<string, any>, pageNumber: number):
return {
id: ann.id,
- type: TYPE_NAMES[ann.annotationType] ?? "unknown",
+ type: resolveTypeName(ann),
contents,
highlightedText,
author: ann.titleObj?.str || "",
@@ -54,6 +54,19 @@ export function processAnnotation(ann: Record<string, any>, pageNumber: number):
};
}
+/**
+ * The kind the sidebar names an annotation by. A highlight drawn free-hand is stored as an Ink
+ * annotation tagged `/IT /InkHighlight` — a storage detail of pdf.js, not something the reader
+ * chose, so it is reported as the highlight it is rather than as a drawing.
+ */
+function resolveTypeName(ann: Record<string, any>): string {
+ if (ann.annotationType === AnnotationType.INK && ann.it === "InkHighlight") {
+ return TYPE_NAMES[AnnotationType.HIGHLIGHT];
+ }
+
+ return TYPE_NAMES[ann.annotationType] ?? "unknown";
+}
+
export async function setupPdfAnnotations() {
await extractAndSendAnnotations();
diff --git a/packages/pdfjs-viewer/src/test/fixture_pdf.ts b/packages/pdfjs-viewer/src/test/fixture_pdf.ts
index 3ef817c2c8..a3618f9de1 100644
--- a/packages/pdfjs-viewer/src/test/fixture_pdf.ts
+++ b/packages/pdfjs-viewer/src/test/fixture_pdf.ts
@@ -58,7 +58,7 @@ export function allFeaturesPdf(): Uint8Array {
"<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [5 0 R 14 0 R] >>",
// 4 — page 2, target of the second outline entry, carrying a link (the one type the
// sidebar filters out) alongside the annotations it lists without a comment.
- "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [16 0 R 17 0 R 18 0 R 19 0 R] >>",
+ "<< /Type /Page /Parent 2 0 R /MediaBox [0 0 612 792] /Annots [16 0 R 17 0 R 18 0 R 19 0 R 20 0 R] >>",
// 5 — highlight annotation with a comment, author and colour.
"<< /Type /Annot /Subtype /Highlight /Rect [100 700 200 720] "
+ "/Contents (A remark) /T (Alice) /C [1 0 0] "
@@ -98,7 +98,11 @@ export function allFeaturesPdf(): Uint8Array {
// 19 — a free-text box, what the toolbar's text tool writes. Its own words are its
// contents, so unlike ink it reaches the sidebar with something to show.
"<< /Type /Annot /Subtype /FreeText /Rect [100 300 300 340] "
- + "/Contents (Typed in the box) /DA (/Helv 12 Tf 0 g) >>"
+ + "/Contents (Typed in the box) /DA (/Helv 12 Tf 0 g) >>",
+ // 20 — the other ink: a highlight drawn free-hand, which pdf.js marks with /IT so it
+ // can be told apart from object 18's pen stroke.
+ "<< /Type /Annot /Subtype /Ink /IT /InkHighlight /Rect [400 100 500 150] /C [1 1 0] "
+ + "/InkList [[400 100 450 150 500 100]] >>"
]);
}
diff --git a/packages/pdfjs-viewer/src/upstream_contract.spec.ts b/packages/pdfjs-viewer/src/upstream_contract.spec.ts
index 6e263d5035..295d1f5024 100644
--- a/packages/pdfjs-viewer/src/upstream_contract.spec.ts
+++ b/packages/pdfjs-viewer/src/upstream_contract.spec.ts
@@ -125,6 +125,15 @@ describe("PDFDocumentProxy shape", () => {
expect(sticky?.contentsObj?.str).toBe("A sticky note");
});
+ it("still reports /IT, the only thing separating a free-hand highlight from a pen stroke", async () => {
+ const page = await viewer.pdfDocument.getPage(2);
+ const inks = (await page.getAnnotations({ intent: "display" }))
+ .filter((annotation: any) => annotation.annotationType === UpstreamAnnotationType.INK);
+
+ // Both are Ink; dropping `it` would relabel every highlight drawn free-hand as a drawing.
+ expect(inks.map((ink: any) => ink.it)).toEqual([ undefined, "InkHighlight" ]);
+ });
+
it("exposes optional-content groups with the name/usage/visible fields we filter on", async () => {
const config = await viewer.pdfDocument.getOptionalContentConfig();
expect(config).not.toBeNull();