diff --git a/src/jspdf.js b/src/jspdf.js index ac63154c5..f91dd396f 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -5711,8 +5711,8 @@ function jsPDF(options) { this.x = pageX; this.y = pageY; this.matrix = pageMatrix; - this.width = getPageWidth(currentPage); - this.height = getPageHeight(currentPage); + this.width = getUnscaledPageWidth(currentPage); + this.height = getUnscaledPageHeight(currentPage); this.outputDestination = outputDestination; this.id = ""; // set by endFormObject() @@ -5727,8 +5727,8 @@ function jsPDF(options) { pageX = this.x; pageY = this.y; pageMatrix = this.matrix; - setPageWidth(currentPage, this.width); - setPageHeight(currentPage, this.height); + setPageWidthWithoutScaling(currentPage, this.width); + setPageHeightWithoutScaling(currentPage, this.height); outputDestination = this.outputDestination; }; @@ -5953,32 +5953,46 @@ function jsPDF(options) { } } - var getPageWidth = (API.getPageWidth = function(pageNumber) { - pageNumber = pageNumber || currentPage; + function getUnscaledPageWidth(pageNumber) { + return ( + pagesContext[pageNumber].mediaBox.topRightX - + pagesContext[pageNumber].mediaBox.bottomLeftX + ); + } + + function setPageWidthWithoutScaling(pageNumber, value) { + pagesContext[pageNumber].mediaBox.topRightX = + value + pagesContext[pageNumber].mediaBox.bottomLeftX; + } + + function getUnscaledPageHeight(pageNumber) { return ( - (pagesContext[pageNumber].mediaBox.topRightX - - pagesContext[pageNumber].mediaBox.bottomLeftX) / - scaleFactor + pagesContext[pageNumber].mediaBox.topRightY - + pagesContext[pageNumber].mediaBox.bottomLeftY ); + } + + function setPageHeightWithoutScaling(pageNumber, value) { + pagesContext[pageNumber].mediaBox.topRightY = + value + pagesContext[pageNumber].mediaBox.bottomLeftY; + } + + var getPageWidth = (API.getPageWidth = function(pageNumber) { + pageNumber = pageNumber || currentPage; + return getUnscaledPageWidth(pageNumber) / scaleFactor; }); var setPageWidth = (API.setPageWidth = function(pageNumber, value) { - pagesContext[pageNumber].mediaBox.topRightX = - value * scaleFactor + pagesContext[pageNumber].mediaBox.bottomLeftX; + setPageWidthWithoutScaling(pageNumber, value * scaleFactor); }); var getPageHeight = (API.getPageHeight = function(pageNumber) { pageNumber = pageNumber || currentPage; - return ( - (pagesContext[pageNumber].mediaBox.topRightY - - pagesContext[pageNumber].mediaBox.bottomLeftY) / - scaleFactor - ); + return getUnscaledPageHeight(pageNumber) / scaleFactor; }); var setPageHeight = (API.setPageHeight = function(pageNumber, value) { - pagesContext[pageNumber].mediaBox.topRightY = - value * scaleFactor + pagesContext[pageNumber].mediaBox.bottomLeftY; + setPageHeightWithoutScaling(pageNumber, value * scaleFactor); }); /** diff --git a/test/reference/form-objects-scale-factor.pdf b/test/reference/form-objects-scale-factor.pdf new file mode 100644 index 000000000..497dd9bff Binary files /dev/null and b/test/reference/form-objects-scale-factor.pdf differ diff --git a/test/specs/form-objects.spec.js b/test/specs/form-objects.spec.js new file mode 100644 index 000000000..988bebc2c --- /dev/null +++ b/test/specs/form-objects.spec.js @@ -0,0 +1,25 @@ +/* global jsPDF */ + +describe("Form objects", () => { + beforeAll(loadGlobals); + + it("should use correct bounding box for scale factors other than 1", () => { + const doc = new jsPDF({ unit: "mm", format: [200, 200], orientation: "p" }); + + doc.advancedAPI(); + + doc.beginFormObject(-50, -50, 100, 100, doc.unitMatrix); + doc.rect(-50, -50, 100, 100).fill(); + doc.endFormObject("0"); + + doc.doFormObject("0", new doc.Matrix(1, 0, 0, 1, 100, 100)); + + doc.setDrawColor(255, 0, 0); + doc.rect(50, 50, 100, 100).stroke(); + doc.rect(0, 0, 200, 200).stroke(); + + doc.compatAPI(); + + comparePdf(doc.output(), "form-objects-scale-factor.pdf", "form-objects"); + }); +});