Skip to content

Commit 394d1e7

Browse files
authored
fix scaling of form object bounding boxes (#3888)
1 parent 064194f commit 394d1e7

File tree

3 files changed

+57
-18
lines changed

3 files changed

+57
-18
lines changed

src/jspdf.js

Lines changed: 32 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -5711,8 +5711,8 @@ function jsPDF(options) {
57115711
this.x = pageX;
57125712
this.y = pageY;
57135713
this.matrix = pageMatrix;
5714-
this.width = getPageWidth(currentPage);
5715-
this.height = getPageHeight(currentPage);
5714+
this.width = getUnscaledPageWidth(currentPage);
5715+
this.height = getUnscaledPageHeight(currentPage);
57165716
this.outputDestination = outputDestination;
57175717

57185718
this.id = ""; // set by endFormObject()
@@ -5727,8 +5727,8 @@ function jsPDF(options) {
57275727
pageX = this.x;
57285728
pageY = this.y;
57295729
pageMatrix = this.matrix;
5730-
setPageWidth(currentPage, this.width);
5731-
setPageHeight(currentPage, this.height);
5730+
setPageWidthWithoutScaling(currentPage, this.width);
5731+
setPageHeightWithoutScaling(currentPage, this.height);
57325732
outputDestination = this.outputDestination;
57335733
};
57345734

@@ -5953,32 +5953,46 @@ function jsPDF(options) {
59535953
}
59545954
}
59555955

5956-
var getPageWidth = (API.getPageWidth = function(pageNumber) {
5957-
pageNumber = pageNumber || currentPage;
5956+
function getUnscaledPageWidth(pageNumber) {
5957+
return (
5958+
pagesContext[pageNumber].mediaBox.topRightX -
5959+
pagesContext[pageNumber].mediaBox.bottomLeftX
5960+
);
5961+
}
5962+
5963+
function setPageWidthWithoutScaling(pageNumber, value) {
5964+
pagesContext[pageNumber].mediaBox.topRightX =
5965+
value + pagesContext[pageNumber].mediaBox.bottomLeftX;
5966+
}
5967+
5968+
function getUnscaledPageHeight(pageNumber) {
59585969
return (
5959-
(pagesContext[pageNumber].mediaBox.topRightX -
5960-
pagesContext[pageNumber].mediaBox.bottomLeftX) /
5961-
scaleFactor
5970+
pagesContext[pageNumber].mediaBox.topRightY -
5971+
pagesContext[pageNumber].mediaBox.bottomLeftY
59625972
);
5973+
}
5974+
5975+
function setPageHeightWithoutScaling(pageNumber, value) {
5976+
pagesContext[pageNumber].mediaBox.topRightY =
5977+
value + pagesContext[pageNumber].mediaBox.bottomLeftY;
5978+
}
5979+
5980+
var getPageWidth = (API.getPageWidth = function(pageNumber) {
5981+
pageNumber = pageNumber || currentPage;
5982+
return getUnscaledPageWidth(pageNumber) / scaleFactor;
59635983
});
59645984

59655985
var setPageWidth = (API.setPageWidth = function(pageNumber, value) {
5966-
pagesContext[pageNumber].mediaBox.topRightX =
5967-
value * scaleFactor + pagesContext[pageNumber].mediaBox.bottomLeftX;
5986+
setPageWidthWithoutScaling(pageNumber, value * scaleFactor);
59685987
});
59695988

59705989
var getPageHeight = (API.getPageHeight = function(pageNumber) {
59715990
pageNumber = pageNumber || currentPage;
5972-
return (
5973-
(pagesContext[pageNumber].mediaBox.topRightY -
5974-
pagesContext[pageNumber].mediaBox.bottomLeftY) /
5975-
scaleFactor
5976-
);
5991+
return getUnscaledPageHeight(pageNumber) / scaleFactor;
59775992
});
59785993

59795994
var setPageHeight = (API.setPageHeight = function(pageNumber, value) {
5980-
pagesContext[pageNumber].mediaBox.topRightY =
5981-
value * scaleFactor + pagesContext[pageNumber].mediaBox.bottomLeftY;
5995+
setPageHeightWithoutScaling(pageNumber, value * scaleFactor);
59825996
});
59835997

59845998
/**
3.34 KB
Binary file not shown.

test/specs/form-objects.spec.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/* global jsPDF */
2+
3+
describe("Form objects", () => {
4+
beforeAll(loadGlobals);
5+
6+
it("should use correct bounding box for scale factors other than 1", () => {
7+
const doc = new jsPDF({ unit: "mm", format: [200, 200], orientation: "p" });
8+
9+
doc.advancedAPI();
10+
11+
doc.beginFormObject(-50, -50, 100, 100, doc.unitMatrix);
12+
doc.rect(-50, -50, 100, 100).fill();
13+
doc.endFormObject("0");
14+
15+
doc.doFormObject("0", new doc.Matrix(1, 0, 0, 1, 100, 100));
16+
17+
doc.setDrawColor(255, 0, 0);
18+
doc.rect(50, 50, 100, 100).stroke();
19+
doc.rect(0, 0, 200, 200).stroke();
20+
21+
doc.compatAPI();
22+
23+
comparePdf(doc.output(), "form-objects-scale-factor.pdf", "form-objects");
24+
});
25+
});

0 commit comments

Comments
 (0)