Skip to content

Commit d10ccc5

Browse files
authored
Merge pull request #3 from imjosh/update-celljs-getTextDimensions
Fix cell.js getTextDimensions doesn't respect lineHeightFactor option
2 parents 20dd3cc + 6565948 commit d10ccc5

File tree

2 files changed

+25
-4
lines changed

2 files changed

+25
-4
lines changed

src/modules/cell.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,17 +171,27 @@ import { jsPDF } from "../jspdf.js";
171171
};
172172

173173
/**
174+
* Gets height and width of text
174175
* @name getTextDimensions
175176
* @function
176177
* @param {string} txt
177-
* @returns {Object} dimensions
178+
* @param {object} [options]
179+
* @param {string} [options.font] Font name or family. Example: "times"
180+
* @param {number} [options.fontSize] Font size in points
181+
* @param {number} [options.lineHeightFactor=1.15] The lineheight of each line
182+
* @param {number} [options.maxWidth=0] Split the text by given width, 0 = no split
183+
* @param {number} [options.scaleFactor] Defaults to value determined by unit declared at inception of PDF document
184+
* @returns {object} `{ w: ${width}, h: ${height} }` (in units declared at inception of PDF document)
178185
*/
179186
jsPDFAPI.getTextDimensions = function(text, options) {
180187
_initialize.call(this);
181188
options = options || {};
182189
var fontSize = options.fontSize || this.getFontSize();
183190
var font = options.font || this.getFont();
184191
var scaleFactor = options.scaleFactor || this.internal.scaleFactor;
192+
var lineHeightFactor =
193+
options.lineHeightFactor || this.internal.getLineHeightFactor();
194+
const maxWidth = options.maxWidth || 0;
185195
var width = 0;
186196
var amountOfLines = 0;
187197
var height = 0;
@@ -198,7 +208,6 @@ import { jsPDF } from "../jspdf.js";
198208
}
199209
}
200210

201-
const maxWidth = options.maxWidth;
202211
if (maxWidth > 0) {
203212
if (typeof text === "string") {
204213
text = this.splitTextToSize(text, maxWidth);
@@ -225,8 +234,8 @@ import { jsPDF } from "../jspdf.js";
225234

226235
width = width / scaleFactor;
227236
height = Math.max(
228-
(amountOfLines * fontSize * this.getLineHeightFactor() -
229-
fontSize * (this.getLineHeightFactor() - 1)) /
237+
(amountOfLines * fontSize * lineHeightFactor -
238+
fontSize * (lineHeightFactor - 1)) /
230239
scaleFactor,
231240
0
232241
);

test/specs/cell.spec.js

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,24 @@ describe("Module: Cell", () => {
2020
expect(
2121
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150 }).h
2222
).toEqual(16);
23+
expect(
24+
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150, lineHeightFactor: 2 }).h
25+
).toEqual(16);
2326
expect(
2427
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100 }).h
2528
).toEqual(34.4);
29+
expect(
30+
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: undefined }).h
31+
).toEqual(34.4);
32+
expect(
33+
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: 2 }).h
34+
).toEqual(48);
2635
expect(
2736
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100 }).w
2837
).toEqual(96.64000000000001);
38+
expect(
39+
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: 2 }).w
40+
).toEqual(96.64000000000001);
2941
expect(
3042
doc.getTextDimensions("Octocat loves jsPDF\njsPDF loves Octocat", { maxWidth: 100 }).h
3143
).toEqual(71.19999999999999);

0 commit comments

Comments
 (0)