Skip to content

Commit a4b5b7c

Browse files
authored
Merge pull request #19925 from calixteman/bug1962819
[Editor] Keep aspect ratio when rescaling an image before being processed for a signature (bug 1962819)
2 parents 2bb8099 + d660567 commit a4b5b7c

File tree

3 files changed

+90
-8
lines changed

3 files changed

+90
-8
lines changed

src/display/editor/drawers/signaturedraw.js

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -436,14 +436,8 @@ class SignatureExtractor {
436436
const isteps = Math.floor(steps);
437437
steps = steps === isteps ? isteps - 1 : isteps;
438438
for (let i = 0; i < steps; i++) {
439-
newWidth = prevWidth;
440-
newHeight = prevHeight;
441-
if (newWidth > maxDim) {
442-
newWidth = Math.ceil(newWidth / 2);
443-
}
444-
if (newHeight > maxDim) {
445-
newHeight = Math.ceil(newHeight / 2);
446-
}
439+
newWidth = Math.ceil(prevWidth / 2);
440+
newHeight = Math.ceil(prevHeight / 2);
447441

448442
const offscreen = new OffscreenCanvas(newWidth, newHeight);
449443
const ctx = offscreen.getContext("2d");

test/images/samplesignature.png

9.96 KB
Loading

test/integration/signature_editor_spec.mjs

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ import {
2727
} from "./test_utils.mjs";
2828

2929
import { fileURLToPath } from "url";
30+
import fs from "fs";
3031
import path from "path";
32+
import { PNG } from "pngjs";
3133

3234
const __dirname = path.dirname(fileURLToPath(import.meta.url));
3335

@@ -583,4 +585,90 @@ describe("Signature Editor", () => {
583585
);
584586
});
585587
});
588+
589+
describe("Check the aspect ratio (bug 1962819)", () => {
590+
let pages, contentWidth, contentHeight;
591+
592+
function getContentAspectRatio(png) {
593+
const { width, height } = png;
594+
const buffer = new Uint32Array(png.data.buffer);
595+
let x0 = width;
596+
let y0 = height;
597+
let x1 = 0;
598+
let y1 = 0;
599+
for (let i = 0; i < height; i++) {
600+
for (let j = 0; j < width; j++) {
601+
if (buffer[width * i + j] !== 0) {
602+
x0 = Math.min(x0, j);
603+
y0 = Math.min(y0, i);
604+
x1 = Math.max(x1, j);
605+
y1 = Math.max(y1, i);
606+
}
607+
}
608+
}
609+
610+
contentWidth = x1 - x0;
611+
contentHeight = y1 - y0;
612+
}
613+
614+
beforeAll(() => {
615+
const data = fs.readFileSync(
616+
path.join(__dirname, "../images/samplesignature.png")
617+
);
618+
const png = PNG.sync.read(data);
619+
getContentAspectRatio(png);
620+
});
621+
622+
beforeEach(async () => {
623+
pages = await loadAndWait("empty.pdf", ".annotationEditorLayer");
624+
});
625+
626+
afterEach(async () => {
627+
await closePages(pages);
628+
});
629+
630+
it("must check that the signature has the correct aspect ratio", async () => {
631+
await Promise.all(
632+
pages.map(async ([browserName, page]) => {
633+
await switchToSignature(page);
634+
await page.click("#editorSignatureAddSignature");
635+
636+
await page.waitForSelector("#addSignatureDialog", {
637+
visible: true,
638+
});
639+
640+
await page.click("#addSignatureImageButton");
641+
await page.waitForSelector("#addSignatureImagePlaceholder", {
642+
visible: true,
643+
});
644+
await page.waitForSelector(`${addButtonSelector}:disabled`);
645+
646+
const input = await page.$("#addSignatureFilePicker");
647+
await input.uploadFile(
648+
`${path.join(__dirname, "../images/samplesignature.png")}`
649+
);
650+
await page.waitForSelector(`#addSignatureImage > path:not([d=""])`);
651+
652+
// The save button should be enabled now.
653+
await page.waitForSelector(
654+
"#addSignatureSaveContainer > input:not(:disabled)"
655+
);
656+
await page.click("#addSignatureAddButton");
657+
await page.waitForSelector("#addSignatureDialog", {
658+
visible: false,
659+
});
660+
const { width, height } = await getRect(
661+
page,
662+
".canvasWrapper > svg use[href='#path_p1_0']"
663+
);
664+
665+
expect(Math.abs(contentWidth / width - contentHeight / height))
666+
.withContext(
667+
`In ${browserName} (${contentWidth}x${contentHeight} vs ${width}x${height})`
668+
)
669+
.toBeLessThan(0.25);
670+
})
671+
);
672+
});
673+
});
586674
});

0 commit comments

Comments
 (0)