Skip to content

Commit a36e884

Browse files
committed
Add a flag to ignore the trailing line break in text height calculations (including tables)
1 parent d837a13 commit a36e884

File tree

7 files changed

+45
-3
lines changed

7 files changed

+45
-3
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
### Unreleased
44

5+
- Add a flag to ignore the trailing line break in text height calculations (including tables)
6+
57
### [v0.17.1] - 2025-05-02
68

79
- Fix null values in table cells rendering as `[object Object]`

lib/line_wrapper.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -236,11 +236,17 @@ class LineWrapper extends EventEmitter {
236236
if (bk.required || !this.canFit(word, w)) {
237237
// if the user specified a max height and an ellipsis, and is about to pass the
238238
// max height and max columns after the next line, append the ellipsis
239-
const lh = this.document.currentLineHeight(true);
239+
let lh = this.document.currentLineHeight(true);
240+
241+
if (options.trailingLineBreak === false) {
242+
if (bk.required) lh = this.document.currentLineHeight();
243+
}
244+
240245
if (
241246
this.height != null &&
242247
this.ellipsis &&
243-
PDFNumber(this.document.y + lh * 2) > this.maxY &&
248+
// If the current line and its following one cant fit, then render the ellipsis
249+
PDFNumber(this.document.y + lh + this.document.currentLineHeight(true)) > this.maxY &&
244250
this.column >= this.columns
245251
) {
246252
if (this.ellipsis === true) {

lib/mixins/text.js

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,10 @@ export default {
174174
}
175175

176176
let contentHeight = this.y - y;
177+
if (options.trailingLineBreak === false) {
178+
const fontGap = this.currentLineHeight(true) - this.currentLineHeight();
179+
contentHeight -= fontGap;
180+
}
177181
// Clamp height to max height
178182
if (options.height) contentHeight = Math.min(contentHeight, options.height);
179183

@@ -245,7 +249,12 @@ export default {
245249
this.y += this.currentLineHeight(true) + lineGap;
246250
});
247251

248-
const height = this.y - y;
252+
let height = this.y - y;
253+
if (options.trailingLineBreak === false) {
254+
const fontGap = this.currentLineHeight(true) - this.currentLineHeight();
255+
height -= fontGap;
256+
}
257+
249258
this.x = x;
250259
this.y = y;
251260

28.2 KB
Loading
14 KB
Loading

tests/visual/table.spec.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,4 +425,18 @@ describe('table', function () {
425425
});
426426
});
427427
});
428+
429+
test('ignore trailing line break - issue #1620', function() {
430+
return runDocTest({systemFonts: true}, function(doc) {
431+
doc.table({
432+
debug: true,
433+
data: [['trailingLineBreak\ndefault (true)']],
434+
});
435+
doc.table({
436+
debug: true,
437+
defaultStyle: { textOptions: { trailingLineBreak: false } },
438+
data: [['trailingLineBreak\nfalse']],
439+
});
440+
});
441+
});
428442
});

tests/visual/text.spec.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,4 +182,15 @@ describe('text', function () {
182182
.fill('blue');
183183
});
184184
});
185+
186+
test('ignore trailing line break - issue #1620', function() {
187+
return runDocTest({systemFonts: true}, function(doc) {
188+
const text = 'test\ntest';
189+
let heightWithout = doc.heightOfString(text, {trailingLineBreak: false});
190+
doc.save().rect(doc.x, doc.y, doc.page.contentWidth, heightWithout).strokeColor("red", 0.3).stroke().restore();
191+
let height = doc.heightOfString(text);
192+
doc.save().rect(doc.x, doc.y, doc.page.contentWidth, height).strokeColor("blue", 0.3).stroke().restore();
193+
doc.text(text, { height });
194+
});
195+
});
185196
});

0 commit comments

Comments
 (0)