Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix cell.js getTextDimensions doesn't respect lineHeightFactor option #3814

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions src/modules/cell.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,17 +171,27 @@ import { jsPDF } from "../jspdf.js";
};

/**
* Gets height and width of text
* @name getTextDimensions
* @function
* @param {string} txt
* @returns {Object} dimensions
* @param {object} [options]
* @param {string} [options.font] Font name or family. Example: "times"
* @param {number} [options.fontSize] Font size in points
* @param {number} [options.lineHeightFactor=1.15] The lineheight of each line
* @param {number} [options.maxWidth=0] Split the text by given width, 0 = no split
* @param {number} [options.scaleFactor] Defaults to value determined by unit declared at inception of PDF document
* @returns {object} `{ w: ${width}, h: ${height} }` (in units declared at inception of PDF document)
*/
jsPDFAPI.getTextDimensions = function(text, options) {
_initialize.call(this);
options = options || {};
var fontSize = options.fontSize || this.getFontSize();
var font = options.font || this.getFont();
var scaleFactor = options.scaleFactor || this.internal.scaleFactor;
var lineHeightFactor =
options.lineHeightFactor || this.internal.getLineHeightFactor();
const maxWidth = options.maxWidth || 0;
var width = 0;
var amountOfLines = 0;
var height = 0;
Expand All @@ -198,7 +208,6 @@ import { jsPDF } from "../jspdf.js";
}
}

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

width = width / scaleFactor;
height = Math.max(
(amountOfLines * fontSize * this.getLineHeightFactor() -
fontSize * (this.getLineHeightFactor() - 1)) /
(amountOfLines * fontSize * lineHeightFactor -
fontSize * (lineHeightFactor - 1)) /
scaleFactor,
0
);
Expand Down
12 changes: 12 additions & 0 deletions test/specs/cell.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,24 @@ describe("Module: Cell", () => {
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150 }).h
).toEqual(16);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150, lineHeightFactor: 2 }).h
).toEqual(16);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100 }).h
).toEqual(34.4);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: undefined }).h
).toEqual(34.4);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: 2 }).h
).toEqual(48);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100 }).w
).toEqual(96.64000000000001);
expect(
doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 100, lineHeightFactor: 2 }).w
).toEqual(96.64000000000001);
expect(
doc.getTextDimensions("Octocat loves jsPDF\njsPDF loves Octocat", { maxWidth: 100 }).h
).toEqual(71.19999999999999);
Expand Down
Loading