From c44df884ece1e2e3a2054c0a97695fb0880a51d7 Mon Sep 17 00:00:00 2001 From: Megharth Date: Sat, 2 Oct 2021 03:02:52 -0400 Subject: [PATCH 1/2] fix lineHeightFactor bug refs: #3234 --- src/jspdf.js | 9 ++++++++- test/reference/lineHeight.pdf | Bin 0 -> 3145 bytes test/specs/lineHeight.spec.js | 20 ++++++++++++++++++++ 3 files changed, 28 insertions(+), 1 deletion(-) create mode 100644 test/reference/lineHeight.pdf create mode 100644 test/specs/lineHeight.spec.js diff --git a/src/jspdf.js b/src/jspdf.js index 4a973423e..88d46b98b 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -3612,7 +3612,14 @@ function jsPDF(options) { //baseline var height = activeFontSize / scope.internal.scaleFactor; - var descent = height * (lineHeightFactor - 1); + var descent; + if ( + options.lineHeightFactor && + typeof options.lineHeightFactor === "number" + ) + descent = height * (options.lineHeightFactor - 1); + else descent = height * (lineHeightFactor - 1); + switch (options.baseline) { case "bottom": y -= descent; diff --git a/test/reference/lineHeight.pdf b/test/reference/lineHeight.pdf new file mode 100644 index 0000000000000000000000000000000000000000..8bbd7df6624f1b5850f96e09834520fa265309f0 GIT binary patch literal 3145 zcmcIm-EQhe5We51n49P%7s31E^-qN&fq|Z=sffT*t48O-z`}84?9^UPlRi_QpfA%t zN@t8I1ow!XDj0-!huNLkZ|CQ`xF3wJMMtrVKmYpUZ>E4lF?(d)t`omJ8F22Cm0|c> zn%siGe<#eDn5x*6b5nub3Vt+;H0c-5@W4@^L*0>nxPN@7L{nq^)&ga`4G zNw)*>9m-cs1|8*kuJ*i8hC-m)xzFdWv~{|<(&{qqzBak?mO&EMK2OXp`e z|LMZGKmXaBI9(fmNuHJitpBsbVy`(ai*ITH;&kzwzN}|O_CNPO%&7aam`{y`2M1{e zI59Rsp?bhv^0s^VVNCMpqjT8_(G;)ECKqC!QKxyPbUC(If zBU5Ok0n`aj(hx-5lyQifV#_yhj7(u?3w4n-Z1+QDv9pKDHE0uRq|VRd*`t|TKu2-9 z@9W~QmgB(}o4G0B^0A_$^A3KlknbROv1ySH5^Lac@SWrmX${Ah*2=r)?g!34TpjEx z+f_PBFbRqkv9c9%u;#4eCzJQ)wD*pEAS19JA>U*fVoSPr$xE@p3mE4nDzeQwKd|#$ znkCXCDcGJ;frn0MSi(-G2ts-w-^rv<=;3P_=LG4IOlS;@BboBsYu&KD){||SuQ3RY z_6yo;1*fZfttW(wS9>Hw#@dp(?Q;m^iY+}PqAeK`;Fc^@Ees@A7>cibk;-rRERnkI z^=z4>8Il4@mTCICp?h;CMPZ?i7mWAYd|5ypk~A5>19W?XXy}db9#n6n`A83a(evFP fiiC>1LBDtN7L`vFGnGwBTStu4UcfFchU3vc$aZUc literal 0 HcmV?d00001 diff --git a/test/specs/lineHeight.spec.js b/test/specs/lineHeight.spec.js new file mode 100644 index 000000000..1aa1d084e --- /dev/null +++ b/test/specs/lineHeight.spec.js @@ -0,0 +1,20 @@ +/* eslint-disable no-self-assign */ +/* global describe, it, expect, jsPDF, comparePdf, Button, ComboBox, ChoiceField, EditBox, ListBox, PushButton, CheckBox, TextField, PasswordField, RadioButton, AcroForm */ + +/** + * line height testing + */ + + describe("Module: Line Height Unit Test", function() { + beforeAll(loadGlobals); + it('test line height', function() { + const doc1 = new jsPDF(); + doc1.setLineHeightFactor(1.5); + doc1.text('Some text', 10, 10, { baseline: 'middle' }); + comparePdf(doc1.output(), 'lineHeight.pdf', 'lineHeight'); + + const doc2 = new jsPDF(); + doc2.text('Some text', 10, 10, { lineHeightFactor: 1.5, baseline: 'middle' }); + comparePdf(doc2.output(), 'lineHeight.pdf', 'lineHeight'); + }); + }); \ No newline at end of file From 03c05bd21f42942655c7ca7a924e29a35b63a34c Mon Sep 17 00:00:00 2001 From: Megharth Date: Sat, 2 Oct 2021 12:52:26 -0400 Subject: [PATCH 2/2] move test to jspdf.unit.spec.js refs: #3234 --- src/jspdf.js | 8 +------- test/reference/lineHeight.pdf | Bin 3145 -> 0 bytes test/specs/jspdf.unit.spec.js | 15 +++++++++++++++ test/specs/lineHeight.spec.js | 20 -------------------- 4 files changed, 16 insertions(+), 27 deletions(-) delete mode 100644 test/reference/lineHeight.pdf delete mode 100644 test/specs/lineHeight.spec.js diff --git a/src/jspdf.js b/src/jspdf.js index 88d46b98b..d66adf5cb 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -3612,13 +3612,7 @@ function jsPDF(options) { //baseline var height = activeFontSize / scope.internal.scaleFactor; - var descent; - if ( - options.lineHeightFactor && - typeof options.lineHeightFactor === "number" - ) - descent = height * (options.lineHeightFactor - 1); - else descent = height * (lineHeightFactor - 1); + var descent = height * (lineHeight - 1); switch (options.baseline) { case "bottom": diff --git a/test/reference/lineHeight.pdf b/test/reference/lineHeight.pdf deleted file mode 100644 index 8bbd7df6624f1b5850f96e09834520fa265309f0..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3145 zcmcIm-EQhe5We51n49P%7s31E^-qN&fq|Z=sffT*t48O-z`}84?9^UPlRi_QpfA%t zN@t8I1ow!XDj0-!huNLkZ|CQ`xF3wJMMtrVKmYpUZ>E4lF?(d)t`omJ8F22Cm0|c> zn%siGe<#eDn5x*6b5nub3Vt+;H0c-5@W4@^L*0>nxPN@7L{nq^)&ga`4G zNw)*>9m-cs1|8*kuJ*i8hC-m)xzFdWv~{|<(&{qqzBak?mO&EMK2OXp`e z|LMZGKmXaBI9(fmNuHJitpBsbVy`(ai*ITH;&kzwzN}|O_CNPO%&7aam`{y`2M1{e zI59Rsp?bhv^0s^VVNCMpqjT8_(G;)ECKqC!QKxyPbUC(If zBU5Ok0n`aj(hx-5lyQifV#_yhj7(u?3w4n-Z1+QDv9pKDHE0uRq|VRd*`t|TKu2-9 z@9W~QmgB(}o4G0B^0A_$^A3KlknbROv1ySH5^Lac@SWrmX${Ah*2=r)?g!34TpjEx z+f_PBFbRqkv9c9%u;#4eCzJQ)wD*pEAS19JA>U*fVoSPr$xE@p3mE4nDzeQwKd|#$ znkCXCDcGJ;frn0MSi(-G2ts-w-^rv<=;3P_=LG4IOlS;@BboBsYu&KD){||SuQ3RY z_6yo;1*fZfttW(wS9>Hw#@dp(?Q;m^iY+}PqAeK`;Fc^@Ees@A7>cibk;-rRERnkI z^=z4>8Il4@mTCICp?h;CMPZ?i7mWAYd|5ypk~A5>19W?XXy}db9#n6n`A83a(evFP fiiC>1LBDtN7L`vFGnGwBTStu4UcfFchU3vc$aZUc diff --git a/test/specs/jspdf.unit.spec.js b/test/specs/jspdf.unit.spec.js index 23c7818c5..b24dc2064 100644 --- a/test/specs/jspdf.unit.spec.js +++ b/test/specs/jspdf.unit.spec.js @@ -2523,6 +2523,21 @@ This is a test too.`, ]); }); + it('jsPDF test text with line height', function() { + const doc1 = new jsPDF(); + let writeArray1 = []; + doc1.__private__.setCustomOutputDestination(writeArray1); + doc1.setLineHeightFactor(1.5); + doc1.text('Some text', 10, 10, { baseline: 'middle' }); + + let writeArray2 = []; + const doc2 = new jsPDF(); + doc2.__private__.setCustomOutputDestination(writeArray2); + doc2.text('Some text', 10, 10, { lineHeightFactor: 1.5, baseline: 'middle' }); + + expect(writeArray1).toEqual(writeArray2); +}); + it("jsPDF private function setLineCap", () => { var doc = jsPDF({ floatPrecision: 2 }); diff --git a/test/specs/lineHeight.spec.js b/test/specs/lineHeight.spec.js deleted file mode 100644 index 1aa1d084e..000000000 --- a/test/specs/lineHeight.spec.js +++ /dev/null @@ -1,20 +0,0 @@ -/* eslint-disable no-self-assign */ -/* global describe, it, expect, jsPDF, comparePdf, Button, ComboBox, ChoiceField, EditBox, ListBox, PushButton, CheckBox, TextField, PasswordField, RadioButton, AcroForm */ - -/** - * line height testing - */ - - describe("Module: Line Height Unit Test", function() { - beforeAll(loadGlobals); - it('test line height', function() { - const doc1 = new jsPDF(); - doc1.setLineHeightFactor(1.5); - doc1.text('Some text', 10, 10, { baseline: 'middle' }); - comparePdf(doc1.output(), 'lineHeight.pdf', 'lineHeight'); - - const doc2 = new jsPDF(); - doc2.text('Some text', 10, 10, { lineHeightFactor: 1.5, baseline: 'middle' }); - comparePdf(doc2.output(), 'lineHeight.pdf', 'lineHeight'); - }); - }); \ No newline at end of file