Skip to content

Commit 8c69012

Browse files
committed
Merge branch 'master' of https://github.com/jeffsieu/jspdf into master
2 parents 0455859 + 16499ee commit 8c69012

File tree

2 files changed

+79
-11
lines changed

2 files changed

+79
-11
lines changed

src/jspdf.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3340,7 +3340,7 @@ function jsPDF(options) {
33403340
*/
33413341
options = options || {};
33423342
var scope = options.scope || this;
3343-
var payload, da, angle, align, charSpace, maxWidth, flags;
3343+
var payload, da, angle, align, charSpace, maxWidth, renderMaxWidthOverflow, flags;
33443344

33453345
// Pre-August-2012 the order of arguments was function(x, y, text, flags)
33463346
// in effort to make all calls have similar signature like
@@ -3539,6 +3539,7 @@ function jsPDF(options) {
35393539

35403540
//multiline
35413541
maxWidth = options.maxWidth || 0;
3542+
renderMaxWidthOverflow = options.renderMaxWidthOverflow !== undefined ? options.renderMaxWidthOverflow : true;
35423543

35433544
if (maxWidth > 0) {
35443545
if (typeof text === "string") {
@@ -3550,6 +3551,10 @@ function jsPDF(options) {
35503551
}
35513552
}
35523553

3554+
if (!renderMaxWidthOverflow) {
3555+
text = [text[0]];
3556+
}
3557+
35533558
//creating Payload-Object to make text byRef
35543559
payload = {
35553560
text: text,

src/modules/context2d.js

Lines changed: 73 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ import { console } from "../libs/console.js";
4747
this.miterLimit = ctx.miterLimit || 10.0;
4848
this.lastPoint = ctx.lastPoint || new Point();
4949
this.margin = ctx.margin || [0, 0, 0, 0];
50-
50+
this.prevPageLastElemOffset = ctx.prevPageLastElemOffset || 0;
5151
this.ignoreClearRect =
5252
typeof ctx.ignoreClearRect === "boolean" ? ctx.ignoreClearRect : true;
5353
return this;
@@ -1474,29 +1474,45 @@ import { console } from "../libs/console.js";
14741474
for (var i = min; i < max + 1; i++) {
14751475
this.pdf.setPage(i);
14761476

1477+
var topMargin = (i === min ? Math.max(this.posY, this.margin[0]) : this.margin[0]);
1478+
var firstPageHeight = this.pdf.internal.pageSize.height - Math.max(this.posY, this.margin[0]) - this.margin[2];
1479+
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[2];
1480+
var pageWidthMinusMargin = this.pdf.internal.pageSize.width - this.margin[1];
1481+
var previousPageHeightSum = i === 1 ? 0 : firstPageHeight + (i - 2) * pageHeightMinusMargin;
1482+
14771483
if (this.ctx.clip_path.length !== 0) {
14781484
var tmpPaths = this.path;
14791485
clipPath = JSON.parse(JSON.stringify(this.ctx.clip_path));
14801486
this.path = pathPositionRedo(
14811487
clipPath,
1488+
<<<<<<< HEAD
14821489
this.posX + this.margin[3],
14831490
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY + this.margin[0]
1491+
=======
1492+
Math.max(this.posX, this.margin[3]),
1493+
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
1494+
>>>>>>> 16499ee4c73991f345d6adab1d68d2c52ab79d00
14841495
);
14851496
drawPaths.call(this, "fill", true);
14861497
this.path = tmpPaths;
14871498
}
14881499
var tmpRect = JSON.parse(JSON.stringify(xRect));
14891500
tmpRect = pathPositionRedo(
14901501
[tmpRect],
1502+
<<<<<<< HEAD
14911503
this.posX + this.margin[3],
14921504
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY + this.margin[0]
1505+
=======
1506+
Math.max(this.posX, this.margin[3]),
1507+
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
1508+
>>>>>>> 16499ee4c73991f345d6adab1d68d2c52ab79d00
14931509
)[0];
14941510
this.pdf.addImage(
14951511
img,
14961512
"JPEG",
14971513
tmpRect.x,
14981514
tmpRect.y,
1499-
tmpRect.w,
1515+
Math.min(tmpRect.w, this.pdf.internal.pageSize.width - this.margin[1] - tmpRect.x),
15001516
tmpRect.h,
15011517
null,
15021518
null,
@@ -1521,7 +1537,7 @@ import { console } from "../libs/console.js";
15211537
var getPagesByPath = function(path, pageWrapX, pageWrapY) {
15221538
var result = [];
15231539
pageWrapX = pageWrapX || this.pdf.internal.pageSize.width;
1524-
pageWrapY = pageWrapY || this.pdf.internal.pageSize.height;
1540+
pageWrapY = pageWrapY || this.pdf.internal.pageSize.height - this.margin[0] - this.margin[2];
15251541

15261542
switch (path.type) {
15271543
default:
@@ -1670,22 +1686,37 @@ import { console } from "../libs/console.js";
16701686
this.lineWidth = lineWidth;
16711687
this.lineJoin = lineJoin;
16721688

1689+
var topMargin = (k === min ? Math.max(this.posY, this.margin[0]) : this.margin[0]);
1690+
var firstPageHeight = this.pdf.internal.pageSize.height - Math.max(this.posY, this.margin[0]) - this.margin[2];
1691+
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[2];
1692+
var previousPageHeightSum = k === 1 ? 0 : firstPageHeight + (k - 2) * pageHeightMinusMargin;
1693+
16731694
if (this.ctx.clip_path.length !== 0) {
16741695
var tmpPaths = this.path;
16751696
clipPath = JSON.parse(JSON.stringify(this.ctx.clip_path));
16761697
this.path = pathPositionRedo(
16771698
clipPath,
1699+
<<<<<<< HEAD
16781700
this.posX + this.margin[3],
16791701
-1 * this.pdf.internal.pageSize.height * (k - 1) + this.posY + this.margin[0]
1702+
=======
1703+
Math.max(this.posX, this.margin[3]),
1704+
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
1705+
>>>>>>> 16499ee4c73991f345d6adab1d68d2c52ab79d00
16801706
);
16811707
drawPaths.call(this, rule, true);
16821708
this.path = tmpPaths;
16831709
}
16841710
tmpPath = JSON.parse(JSON.stringify(origPath));
16851711
this.path = pathPositionRedo(
16861712
tmpPath,
1713+
<<<<<<< HEAD
16871714
this.posX + this.margin[3],
16881715
-1 * this.pdf.internal.pageSize.height * (k - 1) + this.posY + this.margin[0]
1716+
=======
1717+
Math.max(this.posX, this.margin[3]),
1718+
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
1719+
>>>>>>> 16499ee4c73991f345d6adab1d68d2c52ab79d00
16891720
);
16901721
if (isClip === false || k === 0) {
16911722
drawPaths.call(this, rule, isClip);
@@ -2024,28 +2055,44 @@ import { console } from "../libs/console.js";
20242055
sortPages(pages);
20252056

20262057
var clipPath, oldSize, oldLineWidth;
2027-
if (this.autoPaging === true) {
2058+
if (this.autoPaging) {
20282059
var min = pages[0];
20292060
var max = pages[pages.length - 1];
20302061
for (var i = min; i < max + 1; i++) {
20312062
this.pdf.setPage(i);
20322063

2064+
var topMargin = (i === min ? Math.max(this.posY, this.margin[0]) : this.margin[0]);
2065+
var firstPageHeight = this.pdf.internal.pageSize.height - Math.max(this.posY, this.margin[0]) - this.margin[2];
2066+
var pageHeightMinusMargin = this.pdf.internal.pageSize.height - this.margin[2];
2067+
var pageWidthMinusMargin = this.pdf.internal.pageSize.width - this.margin[1];
2068+
var previousPageHeightSum = i === 1 ? 0 : firstPageHeight + (i - 2) * pageHeightMinusMargin;
2069+
20332070
if (this.ctx.clip_path.length !== 0) {
20342071
var tmpPaths = this.path;
20352072
clipPath = JSON.parse(JSON.stringify(this.ctx.clip_path));
20362073
this.path = pathPositionRedo(
20372074
clipPath,
2075+
<<<<<<< HEAD
20382076
this.posX + this.margin[3],
20392077
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY + this.margin[0]
2078+
=======
2079+
Math.max(this.posX, this.margin[3]),
2080+
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
2081+
>>>>>>> 16499ee4c73991f345d6adab1d68d2c52ab79d00
20402082
);
20412083
drawPaths.call(this, "fill", true);
20422084
this.path = tmpPaths;
20432085
}
20442086
var tmpRect = JSON.parse(JSON.stringify(textRect));
20452087
tmpRect = pathPositionRedo(
20462088
[tmpRect],
2089+
<<<<<<< HEAD
20472090
this.posX + this.margin[3],
20482091
-1 * this.pdf.internal.pageSize.height * (i - 1) + this.posY + this.margin[0]
2092+
=======
2093+
Math.max(this.posX, this.margin[3]),
2094+
-1 * previousPageHeightSum + topMargin + this.ctx.prevPageLastElemOffset
2095+
>>>>>>> 16499ee4c73991f345d6adab1d68d2c52ab79d00
20492096
)[0];
20502097

20512098
if (options.scale >= 0.01) {
@@ -2054,12 +2101,24 @@ import { console } from "../libs/console.js";
20542101
oldLineWidth = this.lineWidth;
20552102
this.lineWidth = oldLineWidth * options.scale;
20562103
}
2057-
this.pdf.text(options.text, tmpRect.x, tmpRect.y, {
2058-
angle: options.angle,
2059-
align: textAlign,
2060-
renderingMode: options.renderingMode,
2061-
maxWidth: options.maxWidth
2062-
});
2104+
2105+
if (tmpRect.y <= pageHeightMinusMargin) {
2106+
if (tmpRect.y - tmpRect.h >= 0 && tmpRect.x <= pageWidthMinusMargin) {
2107+
this.pdf.text(options.text, tmpRect.x, tmpRect.y, {
2108+
angle: options.angle,
2109+
align: textAlign,
2110+
renderingMode: options.renderingMode,
2111+
maxWidth: options.maxWidth || pageWidthMinusMargin - tmpRect.x,
2112+
renderMaxWidthOverflow: false
2113+
});
2114+
}
2115+
} else {
2116+
// This text is the last element of the page, but it got cut off due to the margin
2117+
// so we render it in the next page
2118+
2119+
// As a result, all other elements have their y offset increased
2120+
this.ctx.prevPageLastElemOffset += pageHeightMinusMargin - tmpRect.y + tmpRect.h;
2121+
}
20632122

20642123
if (options.scale >= 0.01) {
20652124
this.pdf.setFontSize(oldSize);
@@ -2073,7 +2132,11 @@ import { console } from "../libs/console.js";
20732132
oldLineWidth = this.lineWidth;
20742133
this.lineWidth = oldLineWidth * options.scale;
20752134
}
2135+
<<<<<<< HEAD
20762136
this.pdf.text(options.text, pt.x + this.posX + this.margin[3], pt.y + this.posY, {
2137+
=======
2138+
this.pdf.text(options.text, pt.x + Math.max(this.posX, this.margin[3]), pt.y + this.posY, {
2139+
>>>>>>> 16499ee4c73991f345d6adab1d68d2c52ab79d00
20772140
angle: options.angle,
20782141
align: textAlign,
20792142
renderingMode: options.renderingMode,

0 commit comments

Comments
 (0)