From de1985abbbc5be9f1eb9e848fd8fadd48c4e4a47 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Sun, 11 Feb 2024 21:06:29 +0100 Subject: [PATCH] [Editor] Set rotated free highlight at the right position after having changed its thickness (bug 1879108) --- src/display/draw_layer.js | 1 - src/display/editor/highlight.js | 13 +++ test/integration/highlight_editor_spec.mjs | 99 ++++++++++++++++++++++ 3 files changed, 112 insertions(+), 1 deletion(-) diff --git a/src/display/draw_layer.js b/src/display/draw_layer.js index ad96bafc0..9e6aedfd6 100644 --- a/src/display/draw_layer.js +++ b/src/display/draw_layer.js @@ -184,7 +184,6 @@ class DrawLayer { const root = this.#mapping.get(id); const defs = root.firstChild; const path = defs.firstChild; - this.updateBox(id, line.box); path.setAttribute("d", line.toSVGPath()); } diff --git a/src/display/editor/highlight.js b/src/display/editor/highlight.js index 6e894c1c1..349d09a12 100644 --- a/src/display/editor/highlight.js +++ b/src/display/editor/highlight.js @@ -135,8 +135,21 @@ class HighlightEditor extends AnnotationEditor { this.#focusOutlines ); } else if (this.parent) { + const angle = this.parent.viewport.rotation; this.parent.drawLayer.updateLine(this.#id, highlightOutlines); + this.parent.drawLayer.updateBox( + this.#id, + HighlightEditor.#rotateBbox( + this.#highlightOutlines.box, + (angle - this.rotation + 360) % 360 + ) + ); + this.parent.drawLayer.updateLine(this.#outlineId, this.#focusOutlines); + this.parent.drawLayer.updateBox( + this.#outlineId, + HighlightEditor.#rotateBbox(this.#focusOutlines.box, angle) + ); } const { x, y, width, height } = highlightOutlines.box; switch (this.rotation) { diff --git a/test/integration/highlight_editor_spec.mjs b/test/integration/highlight_editor_spec.mjs index b8ad568c7..20730d5cf 100644 --- a/test/integration/highlight_editor_spec.mjs +++ b/test/integration/highlight_editor_spec.mjs @@ -741,4 +741,103 @@ describe("Highlight Editor", () => { ); }); }); + + describe("Free highlight is drawn at the right place after having been rotated (bug 1879108)", () => { + let pages; + + beforeAll(async () => { + pages = await loadAndWait( + "empty.pdf", + ".annotationEditorLayer", + null, + null, + { highlightEditorColors: "yellow=#000000" } + ); + }); + + afterAll(async () => { + await closePages(pages); + }); + + it("must check that highlight is at the correct position", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await page.click("#editorHighlight"); + await page.waitForSelector(".annotationEditorLayer.highlightEditing"); + + const rect = await page.$eval(".annotationEditorLayer", el => { + // With Chrome something is wrong when serializing a DomRect, + // hence we extract the values and just return them. + const { x, y } = el.getBoundingClientRect(); + return { x, y }; + }); + + const clickHandle = await waitForPointerUp(page); + await page.mouse.move(rect.x + 120, rect.y + 120); + await page.mouse.down(); + await page.mouse.move(rect.x + 220, rect.y + 220); + await page.mouse.up(); + await awaitPromise(clickHandle); + + await page.waitForSelector(getEditorSelector(0)); + + await page.evaluate(() => { + window.PDFViewerApplication.rotatePages(90); + }); + await page.waitForSelector( + ".annotationEditorLayer[data-main-rotation='90']" + ); + await selectAll(page); + + const prevWidth = await page.evaluate( + sel => document.querySelector(sel).getBoundingClientRect().width, + getEditorSelector(0) + ); + + page.evaluate(val => { + window.PDFViewerApplication.eventBus.dispatch( + "switchannotationeditorparams", + { + source: null, + type: window.pdfjsLib.AnnotationEditorParamsType + .HIGHLIGHT_THICKNESS, + value: val, + } + ); + }, 24); + + await page.waitForFunction( + (w, sel) => + document.querySelector(sel).getBoundingClientRect().width !== w, + {}, + prevWidth, + getEditorSelector(0) + ); + + const rectDiv = await page.$eval(getEditorSelector(0), el => { + const { x, y, width, height } = el.getBoundingClientRect(); + return { x, y, width, height }; + }); + + const rectSVG = await page.$eval("svg.highlight.free", el => { + const { x, y, width, height } = el.getBoundingClientRect(); + return { x, y, width, height }; + }); + + expect(Math.abs(rectDiv.x - rectSVG.x) <= 2) + .withContext(`In ${browserName}`) + .toBe(true); + expect(Math.abs(rectDiv.y - rectSVG.y) <= 2) + .withContext(`In ${browserName}`) + .toBe(true); + expect(Math.abs(rectDiv.height - rectSVG.height) <= 2) + .withContext(`In ${browserName}`) + .toBe(true); + expect(Math.abs(rectDiv.width - rectSVG.width) <= 2) + .withContext(`In ${browserName}`) + .toBe(true); + }) + ); + }); + }); });