From 1ce6668a7071178bd08e5b817473eea780cd4d07 Mon Sep 17 00:00:00 2001 From: Calixte Denizet Date: Tue, 4 Jul 2023 23:56:24 +0200 Subject: [PATCH] [Editor] Fix dimensions of a rotated FreeText after a dimensions change --- src/display/editor/freetext.js | 11 +++- test/integration/freetext_editor_spec.js | 84 ++++++++++++++++++++++++ 2 files changed, 93 insertions(+), 2 deletions(-) diff --git a/src/display/editor/freetext.js b/src/display/editor/freetext.js index 9de780658..ca3c24b7b 100644 --- a/src/display/editor/freetext.js +++ b/src/display/editor/freetext.js @@ -342,8 +342,15 @@ class FreeTextEditor extends AnnotationEditor { div.style.display = savedDisplay; } - this.width = rect.width / parentWidth; - this.height = rect.height / parentHeight; + // The dimensions are relative to the rotation of the page, hence we need to + // take that into account (see issue #16636). + if (this.rotation % 180 === this.parentRotation % 180) { + this.width = rect.width / parentWidth; + this.height = rect.height / parentHeight; + } else { + this.width = rect.height / parentWidth; + this.height = rect.width / parentHeight; + } } /** diff --git a/test/integration/freetext_editor_spec.js b/test/integration/freetext_editor_spec.js index fd38ac800..023c44f93 100644 --- a/test/integration/freetext_editor_spec.js +++ b/test/integration/freetext_editor_spec.js @@ -1178,4 +1178,88 @@ describe("FreeText Editor", () => { ); }); }); + + describe("FreeText rotation", () => { + let pages; + + beforeAll(async () => { + pages = await loadAndWait("empty.pdf", ".annotationEditorLayer"); + }); + + afterAll(async () => { + await closePages(pages); + }); + + it("must check that the dimensions of a rotated annotations are correct after a font size change", async () => { + await Promise.all( + pages.map(async ([browserName, page]) => { + await page.keyboard.press("r"); + await page.click("#editorFreeText"); + + const rect = await page.$eval(".annotationEditorLayer", el => { + const { x, y } = el.getBoundingClientRect(); + return { x, y }; + }); + + const data = "Hello PDF.js World !!"; + await page.mouse.click(rect.x + 100, rect.y + 100); + await page.type(`${getEditorSelector(0)} .internal`, data); + + const editorRect = await page.$eval(getEditorSelector(0), el => { + const { x, y, width, height } = el.getBoundingClientRect(); + return { + x, + y, + width, + height, + }; + }); + + // Commit. + await page.mouse.click( + editorRect.x, + editorRect.y + 2 * editorRect.height + ); + + let serialized = await getSerialized(page); + let bbox = serialized[0].rect; + let width = bbox[2] - bbox[0]; + let height = bbox[3] - bbox[1]; + expect(width < height) + .withContext(`In ${browserName}`) + .toEqual(true); + + for (let i = 0; i < 3; i++) { + await page.keyboard.press("r"); + await page.waitForTimeout(10); + } + + await page.keyboard.down("Control"); + await page.keyboard.press("a"); + await page.keyboard.up("Control"); + await page.waitForTimeout(10); + + page.evaluate(() => { + window.PDFViewerApplication.eventBus.dispatch( + "switchannotationeditorparams", + { + source: null, + type: /* AnnotationEditorParamsType.FREETEXT_SIZE */ 1, + value: 50, + } + ); + }); + await page.waitForTimeout(10); + + serialized = await getSerialized(page); + bbox = serialized[0].rect; + width = bbox[2] - bbox[0]; + height = bbox[3] - bbox[1]; + expect(width < height) + .withContext(`In ${browserName}`) + .toEqual(true); + }) + ); + }); + }); });