From 6a6c4ce44e5608a41d069d54395b4be9c239bb2e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20N=C3=B8rregaard?= Date: Sun, 26 Jul 2020 12:23:04 +0200 Subject: [PATCH 1/6] Now maxTextWidth can be specified and the number of lines can automatically be inferred based on that and the text width --- spec/cell.spec.js | 1 + src/modules/cell.js | 4 +++- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/spec/cell.spec.js b/spec/cell.spec.js index 811ead67e..ed1c101e4 100644 --- a/spec/cell.spec.js +++ b/spec/cell.spec.js @@ -19,6 +19,7 @@ describe("Module: Cell", () => { 144.48000000000002 ); expect(doc.getTextDimensions("Octocat loves jsPDF").h).toEqual(16); + expect(doc.getTextDimensions("Octocat loves jsPDF", {maxTextLength: 100 }).h,).toEqual(32); expect(doc.getTextDimensions("").w).toEqual(0); expect(doc.getTextDimensions("").h).toEqual(0); expect(doc.getTextDimensions([""]).w).toEqual(0); diff --git a/src/modules/cell.js b/src/modules/cell.js index e2b2a02eb..7c7aec34f 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -198,7 +198,9 @@ if (width < tempWidth) { width = tempWidth; } - if (width !== 0) { + if (options.maxTextLength && width !== 0) { + amountOfLines = Math.cell(width / options.maxTextLength) + } else if (width !== 0) { amountOfLines = text.length; } } From 0ef7aa2626d09ed141e9fa0db4e67c361650f25a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20N=C3=B8rregaard?= Date: Mon, 27 Jul 2020 16:47:51 +0200 Subject: [PATCH 2/6] Change the name of maxTextWidth to maxWidth so that it is consistent with the naming for text --- spec/cell.spec.js | 2 +- src/modules/cell.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/spec/cell.spec.js b/spec/cell.spec.js index ed1c101e4..cf6fa6695 100644 --- a/spec/cell.spec.js +++ b/spec/cell.spec.js @@ -19,7 +19,7 @@ describe("Module: Cell", () => { 144.48000000000002 ); expect(doc.getTextDimensions("Octocat loves jsPDF").h).toEqual(16); - expect(doc.getTextDimensions("Octocat loves jsPDF", {maxTextLength: 100 }).h,).toEqual(32); + expect(doc.getTextDimensions("Octocat loves jsPDF", {maxWidth: 100 }).h,).toEqual(32); expect(doc.getTextDimensions("").w).toEqual(0); expect(doc.getTextDimensions("").h).toEqual(0); expect(doc.getTextDimensions([""]).w).toEqual(0); diff --git a/src/modules/cell.js b/src/modules/cell.js index 7c7aec34f..6e73fc7f3 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -198,8 +198,8 @@ if (width < tempWidth) { width = tempWidth; } - if (options.maxTextLength && width !== 0) { - amountOfLines = Math.cell(width / options.maxTextLength) + if (options.maxWidth && width !== 0) { + amountOfLines = Math.cell(width / options.maxWidth) } else if (width !== 0) { amountOfLines = text.length; } From cb495b95cbb09cfaa07673a1b5641f419ccccbeb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20N=C3=B8rregaard?= Date: Thu, 30 Jul 2020 12:34:34 +0200 Subject: [PATCH 3/6] Changed to using splitTextToSize and updated the test to fit the actual result --- spec/cell.spec.js | 2 +- src/modules/cell.js | 11 +++++------ 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/spec/cell.spec.js b/spec/cell.spec.js index cf6fa6695..fc1a805fa 100644 --- a/spec/cell.spec.js +++ b/spec/cell.spec.js @@ -19,7 +19,7 @@ describe("Module: Cell", () => { 144.48000000000002 ); expect(doc.getTextDimensions("Octocat loves jsPDF").h).toEqual(16); - expect(doc.getTextDimensions("Octocat loves jsPDF", {maxWidth: 100 }).h,).toEqual(32); + expect(doc.getTextDimensions("Octocat loves jsPDF", {maxWidth: 100 }).h).toEqual(34.4); expect(doc.getTextDimensions("").w).toEqual(0); expect(doc.getTextDimensions("").h).toEqual(0); expect(doc.getTextDimensions([""]).w).toEqual(0); diff --git a/src/modules/cell.js b/src/modules/cell.js index 6e73fc7f3..909be54a9 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -192,17 +192,16 @@ ); } - text = Array.isArray(text) ? text : [text]; + text = Array.isArray(text) ? text : options.maxWidth ? this.splitTextToSize(text, options.maxWidth) : [text]; for (var i = 0; i < text.length; i++) { tempWidth = this.getStringUnitWidth(text[i], { font: font }) * fontSize; if (width < tempWidth) { width = tempWidth; } - if (options.maxWidth && width !== 0) { - amountOfLines = Math.cell(width / options.maxWidth) - } else if (width !== 0) { - amountOfLines = text.length; - } + } + + if (width !== 0) { + amountOfLines = text.length; } width = width / scaleFactor; From facb6ef67c42189b2a5499866e0ad47f5831d25e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Niklas=20N=C3=B8rregaard?= Date: Thu, 30 Jul 2020 12:45:06 +0200 Subject: [PATCH 4/6] Changed to using splitTextToSize for arrays as well to stay consistent --- src/modules/cell.js | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/modules/cell.js b/src/modules/cell.js index 909be54a9..3957ecc09 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -192,7 +192,7 @@ ); } - text = Array.isArray(text) ? text : options.maxWidth ? this.splitTextToSize(text, options.maxWidth) : [text]; + text = Array.isArray(text) ? text : [text]; for (var i = 0; i < text.length; i++) { tempWidth = this.getStringUnitWidth(text[i], { font: font }) * fontSize; if (width < tempWidth) { @@ -200,7 +200,10 @@ } } - if (width !== 0) { + if (options.maxWidth){ + amountOfLines = this.splitTextToSize(text, options.maxWidth) + } + else if (width !== 0) { amountOfLines = text.length; } From dbe4354542831066797db47a151a8e0cc9c3f247 Mon Sep 17 00:00:00 2001 From: Niklas Moss Date: Fri, 31 Jul 2020 11:46:26 +0200 Subject: [PATCH 5/6] Update tests and change cell.js according to PR feedback --- spec/cell.spec.js | 11 +++++------ src/modules/cell.js | 9 ++++----- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/spec/cell.spec.js b/spec/cell.spec.js index fc1a805fa..a6ab9d9b7 100644 --- a/spec/cell.spec.js +++ b/spec/cell.spec.js @@ -9,17 +9,16 @@ describe("Module: Cell", () => { expect( doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 50)).h ).toEqual(71.19999999999999); + expect(doc.getTextDimensions("Octocat loves jsPDF").w).toEqual(144.48000000000002); + expect(doc.getTextDimensions("Octocat loves jsPDF").h).toEqual(16); expect( - doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 150)).w + doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150}).w ).toEqual(144.48000000000002); expect( - doc.getTextDimensions(doc.splitTextToSize("Octocat loves jsPDF", 150)).h + doc.getTextDimensions("Octocat loves jsPDF", { maxWidth: 150 }).h ).toEqual(16); - expect(doc.getTextDimensions("Octocat loves jsPDF").w).toEqual( - 144.48000000000002 - ); - expect(doc.getTextDimensions("Octocat loves jsPDF").h).toEqual(16); expect(doc.getTextDimensions("Octocat loves jsPDF", {maxWidth: 100 }).h).toEqual(34.4); + expect(doc.getTextDimensions("Octocat loves jsPDF", {maxWidth: 100 }).w).toEqual(96.64000000000001); expect(doc.getTextDimensions("").w).toEqual(0); expect(doc.getTextDimensions("").h).toEqual(0); expect(doc.getTextDimensions([""]).w).toEqual(0); diff --git a/src/modules/cell.js b/src/modules/cell.js index 3957ecc09..cf6c953bb 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -192,7 +192,9 @@ ); } - text = Array.isArray(text) ? text : [text]; + const maxWidthIsDefind = options.maxWidth !== undefined && options.maxWidth !== null + text = Array.isArray(text) ? text : maxWidthIsDefind ? this.splitTextToSize(text, options.maxWidth) : [text]; + for (var i = 0; i < text.length; i++) { tempWidth = this.getStringUnitWidth(text[i], { font: font }) * fontSize; if (width < tempWidth) { @@ -200,10 +202,7 @@ } } - if (options.maxWidth){ - amountOfLines = this.splitTextToSize(text, options.maxWidth) - } - else if (width !== 0) { + if (width !== 0) { amountOfLines = text.length; } From 37e474a6dc33e14d8ea48944b1a840a76ff39904 Mon Sep 17 00:00:00 2001 From: Niklas Moss Date: Sun, 2 Aug 2020 10:37:53 +0200 Subject: [PATCH 6/6] Now it should work exactly the same as the text method --- src/modules/cell.js | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/modules/cell.js b/src/modules/cell.js index cf6c953bb..b8e3d607c 100644 --- a/src/modules/cell.js +++ b/src/modules/cell.js @@ -192,8 +192,18 @@ ); } - const maxWidthIsDefind = options.maxWidth !== undefined && options.maxWidth !== null - text = Array.isArray(text) ? text : maxWidthIsDefind ? this.splitTextToSize(text, options.maxWidth) : [text]; + + const maxWidth = options.maxWidth + if (maxWidth > 0){ + if (typeof text === 'string'){ + text = this.splitTextToSize(text, maxWidth) + } else if (Object.prototype.toString.call(text) === "[object Array]") { + text = this.splitTextToSize(text.join(" "), maxWidth) + } + } else { + // Without the else clause, it will not work if you do not pass along maxWidth + text = Array.isArray(text) ? text : [text]; + } for (var i = 0; i < text.length; i++) { tempWidth = this.getStringUnitWidth(text[i], { font: font }) * fontSize;