From 5c6f8e4c8c55df2702f9a4aafefc83942d9dc28f Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Sun, 13 Dec 2020 22:24:34 +0530 Subject: [PATCH 01/16] Add fonts with different font-weights #2920 --- src/jspdf.js | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index de3a7bf3c..8ac6c33b4 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4850,6 +4850,7 @@ function jsPDF(options) { * @param {string} postScriptName PDF specification full name for the font. * @param {string} id PDF-document-instance-specific label assinged to the font. * @param {string} fontStyle Style of the Font. + * @param {number || string} fontWeight Weight of the Font. * @param {Object} encoding Encoding_name-to-Font_metrics_object mapping. * @function * @instance @@ -4857,8 +4858,15 @@ function jsPDF(options) { * @name addFont * @returns {string} fontId */ - API.addFont = function(postScriptName, fontName, fontStyle, encoding) { - encoding = encoding || "Identity-H"; + API.addFont = function(postScriptName, fontName, fontStyle, fontWeight, encoding) { + encoding = arguments[4] ? encoding || "Identity-H" : arguments[3]; + fontWeight = arguments[4] ? fontWeight : 'normal'; + if ((fontStyle == 'normal' && fontWeight === 'bold') || (fontStyle == 'bold' && fontWeight == 'normal')) { + throw new Error("Invalid Combination of fontweight and fontstyle"); + } + if (fontStyle !== fontWeight) { + fontStyle = fontWeight == 400 ? 'normal' : fontWeight == 700 ? 'bold' : fontStyle + ' ' + fontWeight; + } return addFont.call(this, postScriptName, fontName, fontStyle, encoding); }; From 9aac0191683c08a1360f807f1b34218f8086e9ae Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Sun, 13 Dec 2020 22:32:10 +0530 Subject: [PATCH 02/16] Handled Optional --- src/jspdf.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 8ac6c33b4..0a734d772 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4859,8 +4859,8 @@ function jsPDF(options) { * @returns {string} fontId */ API.addFont = function(postScriptName, fontName, fontStyle, fontWeight, encoding) { - encoding = arguments[4] ? encoding || "Identity-H" : arguments[3]; - fontWeight = arguments[4] ? fontWeight : 'normal'; + encoding = encoding || "Identity-H" + fontWeight = fontWeight || 'normal'; if ((fontStyle == 'normal' && fontWeight === 'bold') || (fontStyle == 'bold' && fontWeight == 'normal')) { throw new Error("Invalid Combination of fontweight and fontstyle"); } From 847e5afefb1d544126f7e0cfa0bb29887822bf22 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Sun, 13 Dec 2020 22:33:12 +0530 Subject: [PATCH 03/16] Undefined cases handled --- src/jspdf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jspdf.js b/src/jspdf.js index 0a734d772..8f3a284c0 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4860,7 +4860,7 @@ function jsPDF(options) { */ API.addFont = function(postScriptName, fontName, fontStyle, fontWeight, encoding) { encoding = encoding || "Identity-H" - fontWeight = fontWeight || 'normal'; + fontWeight = fontWeight || ''; if ((fontStyle == 'normal' && fontWeight === 'bold') || (fontStyle == 'bold' && fontWeight == 'normal')) { throw new Error("Invalid Combination of fontweight and fontstyle"); } From 1c54cfbe3162bfffe3bed51d77af4f0bd6e42870 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Mon, 14 Dec 2020 10:14:02 +0530 Subject: [PATCH 04/16] Test Fixed --- src/jspdf.js | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 8f3a284c0..56c14e18f 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4860,11 +4860,10 @@ function jsPDF(options) { */ API.addFont = function(postScriptName, fontName, fontStyle, fontWeight, encoding) { encoding = encoding || "Identity-H" - fontWeight = fontWeight || ''; if ((fontStyle == 'normal' && fontWeight === 'bold') || (fontStyle == 'bold' && fontWeight == 'normal')) { throw new Error("Invalid Combination of fontweight and fontstyle"); } - if (fontStyle !== fontWeight) { + if (fontWeight && fontStyle !== fontWeight) { fontStyle = fontWeight == 400 ? 'normal' : fontWeight == 700 ? 'bold' : fontStyle + ' ' + fontWeight; } return addFont.call(this, postScriptName, fontName, fontStyle, encoding); From d4accaf2b26956bd3d2b8c94ebc9d861cf091a11 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Mon, 14 Dec 2020 22:17:08 +0530 Subject: [PATCH 05/16] handled custom fontweight and added test cases --- src/jspdf.js | 18 ++++++++----- test/specs/putTotalPages.spec.js | 46 ++++++++++++++++++++++++++++++++ 2 files changed, 58 insertions(+), 6 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 56c14e18f..fdf8e508f 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4859,13 +4859,19 @@ function jsPDF(options) { * @returns {string} fontId */ API.addFont = function(postScriptName, fontName, fontStyle, fontWeight, encoding) { - encoding = encoding || "Identity-H" - if ((fontStyle == 'normal' && fontWeight === 'bold') || (fontStyle == 'bold' && fontWeight == 'normal')) { - throw new Error("Invalid Combination of fontweight and fontstyle"); - } - if (fontWeight && fontStyle !== fontWeight) { - fontStyle = fontWeight == 400 ? 'normal' : fontWeight == 700 ? 'bold' : fontStyle + ' ' + fontWeight; + let encodingOptions = ["StandardEncoding", "MacRomanEncoding", "Identity-H", "WinAnsiEncoding"] + if (arguments[3] && encodingOptions.includes(arguments[3])) { + encoding = arguments[3]; + }else if(arguments[3] && !encodingOptions.includes(arguments[3])){ + //if weired combination of fontweight and font style throw error + if ((fontStyle == 'normal' && fontWeight == 'bold') || (fontStyle == 'bold' && fontWeight == 'normal') || (fontStyle == 'bold' && fontWeight == 400) || (fontStyle == 'normal' && fontWeight == 700)) { + throw new Error("Invalid Combination of fontweight and fontstyle"); + } + if (fontWeight && fontStyle !== fontWeight) { //if fontstyle is normal and fontweight is normal too no need to append the font-weight + fontStyle = fontWeight == 400 ? 'normal' : fontWeight == 700 ? 'bold' : fontStyle + '' + fontWeight; + } } + encoding = encoding || "Identity-H"; return addFont.call(this, postScriptName, fontName, fontStyle, encoding); }; diff --git a/test/specs/putTotalPages.spec.js b/test/specs/putTotalPages.spec.js index 804dd3559..c2cf5f614 100644 --- a/test/specs/putTotalPages.spec.js +++ b/test/specs/putTotalPages.spec.js @@ -42,3 +42,49 @@ describe("Module: putTotalPages", () => { comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); }); }); + +it("customfont with encoding without passing fontWeight", () => { + var PTSans = loadBinaryResource("reference/PTSans.ttf"); + var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 }); + var totalPagesExp = "{totalPages}"; + + doc.addFileToVFS("PTSans.ttf", PTSans); + doc.addFont("PTSans.ttf", "PTSans", "normal" , "Identity-H"); + + doc.setFont("PTSans"); + + doc.text(10, 10, "Page 1 of {totalPages}"); + doc.addPage(); + + doc.text(10, 10, "Page 2 of {totalPages}"); + + if (typeof doc.putTotalPages === "function") { + doc.putTotalPages(totalPagesExp); + } + + comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); +}); + +it("customfont with fontweight", () => { + var PTSans = loadBinaryResource("reference/PTSans.ttf"); + var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 }); + var totalPagesExp = "{totalPages}"; + + doc.addFileToVFS("PTSans.ttf", PTSans); + doc.addFont("PTSans.ttf", "PTSans", "normal",200, "Identity-H"); + + console.log(doc.getFontList()) + + doc.setFont("PTSans",'normal200'); + + doc.text(10, 10, "Page 1 of {totalPages}"); + doc.addPage(); + + doc.text(10, 10, "Page 2 of {totalPages}"); + + if (typeof doc.putTotalPages === "function") { + doc.putTotalPages(totalPagesExp); + } + + comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); +}); From 9309552335dddf182655037e4b23ed43fde3c5ea Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Tue, 15 Dec 2020 10:05:59 +0530 Subject: [PATCH 06/16] removed console --- test/specs/putTotalPages.spec.js | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/test/specs/putTotalPages.spec.js b/test/specs/putTotalPages.spec.js index c2cf5f614..8c1dc1258 100644 --- a/test/specs/putTotalPages.spec.js +++ b/test/specs/putTotalPages.spec.js @@ -73,8 +73,6 @@ it("customfont with fontweight", () => { doc.addFileToVFS("PTSans.ttf", PTSans); doc.addFont("PTSans.ttf", "PTSans", "normal",200, "Identity-H"); - console.log(doc.getFontList()) - doc.setFont("PTSans",'normal200'); doc.text(10, 10, "Page 1 of {totalPages}"); @@ -85,6 +83,6 @@ it("customfont with fontweight", () => { if (typeof doc.putTotalPages === "function") { doc.putTotalPages(totalPagesExp); } - + comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); }); From 6b6b00bbb5828357d52261316e014b6abe9fefe8 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Tue, 15 Dec 2020 10:57:29 +0530 Subject: [PATCH 07/16] ie fix --- src/jspdf.js | 4 ++-- test/specs/putTotalPages.spec.js | 1 - 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index fdf8e508f..724d2fa36 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4860,9 +4860,9 @@ function jsPDF(options) { */ API.addFont = function(postScriptName, fontName, fontStyle, fontWeight, encoding) { let encodingOptions = ["StandardEncoding", "MacRomanEncoding", "Identity-H", "WinAnsiEncoding"] - if (arguments[3] && encodingOptions.includes(arguments[3])) { + if (arguments[3] && encodingOptions.indexOf(arguments[3]) !== -1) { //IE 11 fix encoding = arguments[3]; - }else if(arguments[3] && !encodingOptions.includes(arguments[3])){ + }else if(arguments[3] && encodingOptions.indexOf(arguments[3]) == -1){ //if weired combination of fontweight and font style throw error if ((fontStyle == 'normal' && fontWeight == 'bold') || (fontStyle == 'bold' && fontWeight == 'normal') || (fontStyle == 'bold' && fontWeight == 400) || (fontStyle == 'normal' && fontWeight == 700)) { throw new Error("Invalid Combination of fontweight and fontstyle"); diff --git a/test/specs/putTotalPages.spec.js b/test/specs/putTotalPages.spec.js index 8c1dc1258..0deba0b72 100644 --- a/test/specs/putTotalPages.spec.js +++ b/test/specs/putTotalPages.spec.js @@ -83,6 +83,5 @@ it("customfont with fontweight", () => { if (typeof doc.putTotalPages === "function") { doc.putTotalPages(totalPagesExp); } - comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); }); From 37171b972c16a142b4417d4a10333d28ad1f1226 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Tue, 15 Dec 2020 14:12:14 +0530 Subject: [PATCH 08/16] Preetier added --- src/jspdf.js | 37 ++++++++++++++++++++++++++++++------- 1 file changed, 30 insertions(+), 7 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 724d2fa36..19297887e 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4858,17 +4858,40 @@ function jsPDF(options) { * @name addFont * @returns {string} fontId */ - API.addFont = function(postScriptName, fontName, fontStyle, fontWeight, encoding) { - let encodingOptions = ["StandardEncoding", "MacRomanEncoding", "Identity-H", "WinAnsiEncoding"] - if (arguments[3] && encodingOptions.indexOf(arguments[3]) !== -1) { //IE 11 fix + API.addFont = function( + postScriptName, + fontName, + fontStyle, + fontWeight, + encoding + ) { + let encodingOptions = [ + "StandardEncoding", + "MacRomanEncoding", + "Identity-H", + "WinAnsiEncoding" + ]; + if (arguments[3] && encodingOptions.indexOf(arguments[3]) !== -1) { + //IE 11 fix encoding = arguments[3]; - }else if(arguments[3] && encodingOptions.indexOf(arguments[3]) == -1){ + } else if (arguments[3] && encodingOptions.indexOf(arguments[3]) == -1) { //if weired combination of fontweight and font style throw error - if ((fontStyle == 'normal' && fontWeight == 'bold') || (fontStyle == 'bold' && fontWeight == 'normal') || (fontStyle == 'bold' && fontWeight == 400) || (fontStyle == 'normal' && fontWeight == 700)) { + if ( + (fontStyle == "normal" && fontWeight == "bold") || + (fontStyle == "bold" && fontWeight == "normal") || + (fontStyle == "bold" && fontWeight == 400) || + (fontStyle == "normal" && fontWeight == 700) + ) { throw new Error("Invalid Combination of fontweight and fontstyle"); } - if (fontWeight && fontStyle !== fontWeight) { //if fontstyle is normal and fontweight is normal too no need to append the font-weight - fontStyle = fontWeight == 400 ? 'normal' : fontWeight == 700 ? 'bold' : fontStyle + '' + fontWeight; + if (fontWeight && fontStyle !== fontWeight) { + //if fontstyle is normal and fontweight is normal too no need to append the font-weight + fontStyle = + fontWeight == 400 + ? "normal" + : fontWeight == 700 + ? "bold" + : fontStyle + "" + fontWeight; } } encoding = encoding || "Identity-H"; From f760a2ed4ea33f8851541661185b0bdbb892c870 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Wed, 16 Dec 2020 17:46:46 +0530 Subject: [PATCH 09/16] Review changes added --- src/jspdf.js | 17 ++++++++++------- test/specs/putTotalPages.spec.js | 26 ++++++++++++++++++++++++-- 2 files changed, 34 insertions(+), 9 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 19297887e..38030448e 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4795,7 +4795,8 @@ function jsPDF(options) { * @memberof jsPDF# * @name setFont */ - API.setFont = function(fontName, fontStyle) { + API.setFont = function(fontName, fontStyle, fontWeight) { + fontStyle = !fontWeight ? fontStyle : fontStyle + "" + fontWeight; activeFontKey = getFont(fontName, fontStyle, { disableWarning: false }); @@ -4865,7 +4866,7 @@ function jsPDF(options) { fontWeight, encoding ) { - let encodingOptions = [ + var encodingOptions = [ "StandardEncoding", "MacRomanEncoding", "Identity-H", @@ -4875,12 +4876,12 @@ function jsPDF(options) { //IE 11 fix encoding = arguments[3]; } else if (arguments[3] && encodingOptions.indexOf(arguments[3]) == -1) { - //if weired combination of fontweight and font style throw error + //if weird combination of fontweight and font style throw error if ( - (fontStyle == "normal" && fontWeight == "bold") || (fontStyle == "bold" && fontWeight == "normal") || (fontStyle == "bold" && fontWeight == 400) || - (fontStyle == "normal" && fontWeight == 700) + (fontStyle == "normal" && fontWeight == "italic") || + (fontStyle == "bold" && fontWeight == "italic") ) { throw new Error("Invalid Combination of fontweight and fontstyle"); } @@ -4888,8 +4889,10 @@ function jsPDF(options) { //if fontstyle is normal and fontweight is normal too no need to append the font-weight fontStyle = fontWeight == 400 - ? "normal" - : fontWeight == 700 + ? fontStyle == "italic" + ? "italic" + : "normal" + : fontWeight == 700 && fontStyle !== "italic" ? "bold" : fontStyle + "" + fontWeight; } diff --git a/test/specs/putTotalPages.spec.js b/test/specs/putTotalPages.spec.js index 0deba0b72..31481f3c1 100644 --- a/test/specs/putTotalPages.spec.js +++ b/test/specs/putTotalPages.spec.js @@ -49,7 +49,7 @@ it("customfont with encoding without passing fontWeight", () => { var totalPagesExp = "{totalPages}"; doc.addFileToVFS("PTSans.ttf", PTSans); - doc.addFont("PTSans.ttf", "PTSans", "normal" , "Identity-H"); + doc.addFont("PTSans.ttf", "PTSans", "normal", "Identity-H"); doc.setFont("PTSans"); @@ -65,6 +65,28 @@ it("customfont with encoding without passing fontWeight", () => { comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); }); +it("customfont check without passing fontweight in setfont", () => { + var PTSans = loadBinaryResource("reference/PTSans.ttf"); + var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 }); + var totalPagesExp = "{totalPages}"; + + doc.addFileToVFS("PTSans.ttf", PTSans); + doc.addFont("PTSans.ttf", "PTSans", "normal"); + + doc.setFont("PTSans",'normal'); + + doc.text(10, 10, "Page 1 of {totalPages}"); + doc.addPage(); + + doc.text(10, 10, "Page 2 of {totalPages}"); + + if (typeof doc.putTotalPages === "function") { + doc.putTotalPages(totalPagesExp); + } + comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); +}); + + it("customfont with fontweight", () => { var PTSans = loadBinaryResource("reference/PTSans.ttf"); var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 }); @@ -73,7 +95,7 @@ it("customfont with fontweight", () => { doc.addFileToVFS("PTSans.ttf", PTSans); doc.addFont("PTSans.ttf", "PTSans", "normal",200, "Identity-H"); - doc.setFont("PTSans",'normal200'); + doc.setFont("PTSans",'normal',200); doc.text(10, 10, "Page 1 of {totalPages}"); doc.addPage(); From d8ecb73c4cd59d1c2a6447e7b5554ef4ae349aab Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Sun, 20 Dec 2020 19:06:46 +0530 Subject: [PATCH 10/16] Fontweight logic added in setfont --- src/jspdf.js | 23 ++++++++++++++++++++++- types/index.d.ts | 4 +++- 2 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 38030448e..4b4eda6f8 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4796,7 +4796,28 @@ function jsPDF(options) { * @name setFont */ API.setFont = function(fontName, fontStyle, fontWeight) { - fontStyle = !fontWeight ? fontStyle : fontStyle + "" + fontWeight; + if (fontWeight) { + //if weird combination of fontweight and font style throw error + if ( + (fontStyle == "bold" && fontWeight == "normal") || + (fontStyle == "bold" && fontWeight == 400) || + (fontStyle == "normal" && fontWeight == "italic") || + (fontStyle == "bold" && fontWeight == "italic") + ) { + throw new Error("Invalid Combination of fontweight and fontstyle"); + } + if (fontWeight && fontStyle !== fontWeight) { + //if fontstyle is normal and fontweight is normal too no need to append the font-weight + fontStyle = + fontWeight == 400 + ? fontStyle == "italic" + ? "italic" + : "normal" + : fontWeight == 700 && fontStyle !== "italic" + ? "bold" + : fontStyle + "" + fontWeight; + } + } activeFontKey = getFont(fontName, fontStyle, { disableWarning: false }); diff --git a/types/index.d.ts b/types/index.d.ts index c60a67589..4cd0499b6 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -612,6 +612,7 @@ declare module "jspdf" { postScriptName: string, id: string, fontStyle: string, + fontWeight?:string, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -623,6 +624,7 @@ declare module "jspdf" { url: URL, id: string, fontStyle: string, + fontWeight?:string, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -763,7 +765,7 @@ declare module "jspdf" { setFileId(value: string): jsPDF; setFillColor(ch1: string): jsPDF; setFillColor(ch1: number, ch2: number, ch3: number, ch4?: number): jsPDF; - setFont(fontName: string, fontStyle?: string): jsPDF; + setFont(fontName: string, fontStyle?: string , fontWeight?:string): jsPDF; setFontSize(size: number): jsPDF; setGState(gState: any): jsPDF; setLineCap(style: string | number): jsPDF; From 54032366ff910871026253952b503dc72c820484 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Sun, 20 Dec 2020 19:09:55 +0530 Subject: [PATCH 11/16] review Feedback updated --- src/jspdf.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/jspdf.js b/src/jspdf.js index 4b4eda6f8..6f53648b7 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4789,6 +4789,7 @@ function jsPDF(options) { * * @param {string} fontName Font name or family. Example: "times". * @param {string} fontStyle Font style or variant. Example: "italic". + * @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400 * @function * @instance * @returns {jsPDF} @@ -4872,7 +4873,7 @@ function jsPDF(options) { * @param {string} postScriptName PDF specification full name for the font. * @param {string} id PDF-document-instance-specific label assinged to the font. * @param {string} fontStyle Style of the Font. - * @param {number || string} fontWeight Weight of the Font. + * @param {number | string} fontWeight Weight of the Font. * @param {Object} encoding Encoding_name-to-Font_metrics_object mapping. * @function * @instance From 60ed83312b75ab92d7267497fae6fedf9f4b5393 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Sun, 20 Dec 2020 19:23:56 +0530 Subject: [PATCH 12/16] prettier done. --- src/jspdf.js | 4 ++-- types/index.d.ts | 6 +++--- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index 6f53648b7..a4207199e 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -4815,8 +4815,8 @@ function jsPDF(options) { ? "italic" : "normal" : fontWeight == 700 && fontStyle !== "italic" - ? "bold" - : fontStyle + "" + fontWeight; + ? "bold" + : fontStyle + "" + fontWeight; } } activeFontKey = getFont(fontName, fontStyle, { diff --git a/types/index.d.ts b/types/index.d.ts index 4cd0499b6..77fe753e8 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -612,7 +612,7 @@ declare module "jspdf" { postScriptName: string, id: string, fontStyle: string, - fontWeight?:string, + fontWeight?: string, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -624,7 +624,7 @@ declare module "jspdf" { url: URL, id: string, fontStyle: string, - fontWeight?:string, + fontWeight?: string, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -765,7 +765,7 @@ declare module "jspdf" { setFileId(value: string): jsPDF; setFillColor(ch1: string): jsPDF; setFillColor(ch1: number, ch2: number, ch3: number, ch4?: number): jsPDF; - setFont(fontName: string, fontStyle?: string , fontWeight?:string): jsPDF; + setFont(fontName: string, fontStyle?: string, fontWeight?: string): jsPDF; setFontSize(size: number): jsPDF; setGState(gState: any): jsPDF; setLineCap(style: string | number): jsPDF; From c2f4cc10108945ec78bacca5ff5eec66f3096ca7 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Sun, 20 Dec 2020 19:54:57 +0530 Subject: [PATCH 13/16] test case added for setfont --- test/specs/putTotalPages.spec.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/test/specs/putTotalPages.spec.js b/test/specs/putTotalPages.spec.js index 31481f3c1..a6db023e5 100644 --- a/test/specs/putTotalPages.spec.js +++ b/test/specs/putTotalPages.spec.js @@ -65,6 +65,7 @@ it("customfont with encoding without passing fontWeight", () => { comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); }); + it("customfont check without passing fontweight in setfont", () => { var PTSans = loadBinaryResource("reference/PTSans.ttf"); var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 }); @@ -107,3 +108,24 @@ it("customfont with fontweight", () => { } comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); }); + +it("customfont with samevalue in fontweight and fontstyle ", () => { + var PTSans = loadBinaryResource("reference/PTSans.ttf"); + var doc = new jsPDF({ filters: ["ASCIIHexEncode"], floatPrecision: 2 }); + var totalPagesExp = "{totalPages}"; + + doc.addFileToVFS("PTSans.ttf", PTSans); + doc.addFont("PTSans.ttf", "PTSans", "normal", "normal", "Identity-H"); + + doc.setFont("PTSans",'normal', "normal"); + + doc.text(10, 10, "Page 1 of {totalPages}"); + doc.addPage(); + + doc.text(10, 10, "Page 2 of {totalPages}"); + + if (typeof doc.putTotalPages === "function") { + doc.putTotalPages(totalPagesExp); + } + comparePdf(doc.output(), "customfont.pdf", "putTotalPages"); +}); From 18dc53e38ddc605c9305f7971667f396a10e522f Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Mon, 21 Dec 2020 16:51:37 +0530 Subject: [PATCH 14/16] review feedback updated --- src/jspdf.js | 71 +++++++++++++++++++++--------------------------- types/index.d.ts | 10 +++++-- 2 files changed, 38 insertions(+), 43 deletions(-) diff --git a/src/jspdf.js b/src/jspdf.js index a4207199e..100b201fb 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -358,6 +358,35 @@ function jsPDF(options) { apiMode = ApiMode.COMPAT; } + /** + * @function addFontWeight + * @param {string} fontStyle Font style or variant. Example: "italic". + * @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400 + * @returns {string} + */ + var combineFontStyleAndFontWeight = function(fontStyle, fontWeight) { + if ( + (fontStyle == "bold" && fontWeight == "normal") || + (fontStyle == "bold" && fontWeight == 400) || + (fontStyle == "normal" && fontWeight == "italic") || + (fontStyle == "bold" && fontWeight == "italic") + ) { + throw new Error("Invalid Combination of fontweight and fontstyle"); + } + if (fontWeight && fontStyle !== fontWeight) { + //if fontstyle is normal and fontweight is normal too no need to append the font-weight + fontStyle = + fontWeight == 400 + ? fontStyle == "italic" + ? "italic" + : "normal" + : fontWeight == 700 && fontStyle !== "italic" + ? "bold" + : fontStyle + "" + fontWeight; + } + return fontStyle; + }; + /** * @callback ApiSwitchBody * @param {jsPDF} pdf @@ -4798,26 +4827,7 @@ function jsPDF(options) { */ API.setFont = function(fontName, fontStyle, fontWeight) { if (fontWeight) { - //if weird combination of fontweight and font style throw error - if ( - (fontStyle == "bold" && fontWeight == "normal") || - (fontStyle == "bold" && fontWeight == 400) || - (fontStyle == "normal" && fontWeight == "italic") || - (fontStyle == "bold" && fontWeight == "italic") - ) { - throw new Error("Invalid Combination of fontweight and fontstyle"); - } - if (fontWeight && fontStyle !== fontWeight) { - //if fontstyle is normal and fontweight is normal too no need to append the font-weight - fontStyle = - fontWeight == 400 - ? fontStyle == "italic" - ? "italic" - : "normal" - : fontWeight == 700 && fontStyle !== "italic" - ? "bold" - : fontStyle + "" + fontWeight; - } + fontStyle = combineFontStyleAndFontWeight(fontStyle, fontWeight); } activeFontKey = getFont(fontName, fontStyle, { disableWarning: false @@ -4898,26 +4908,7 @@ function jsPDF(options) { //IE 11 fix encoding = arguments[3]; } else if (arguments[3] && encodingOptions.indexOf(arguments[3]) == -1) { - //if weird combination of fontweight and font style throw error - if ( - (fontStyle == "bold" && fontWeight == "normal") || - (fontStyle == "bold" && fontWeight == 400) || - (fontStyle == "normal" && fontWeight == "italic") || - (fontStyle == "bold" && fontWeight == "italic") - ) { - throw new Error("Invalid Combination of fontweight and fontstyle"); - } - if (fontWeight && fontStyle !== fontWeight) { - //if fontstyle is normal and fontweight is normal too no need to append the font-weight - fontStyle = - fontWeight == 400 - ? fontStyle == "italic" - ? "italic" - : "normal" - : fontWeight == 700 && fontStyle !== "italic" - ? "bold" - : fontStyle + "" + fontWeight; - } + fontStyle = combineFontStyleAndFontWeight(fontStyle, fontWeight); } encoding = encoding || "Identity-H"; return addFont.call(this, postScriptName, fontName, fontStyle, encoding); diff --git a/types/index.d.ts b/types/index.d.ts index 77fe753e8..49e436b63 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -612,7 +612,7 @@ declare module "jspdf" { postScriptName: string, id: string, fontStyle: string, - fontWeight?: string, + fontWeight?: string | number, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -624,7 +624,7 @@ declare module "jspdf" { url: URL, id: string, fontStyle: string, - fontWeight?: string, + fontWeight?: string | number, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -765,7 +765,11 @@ declare module "jspdf" { setFileId(value: string): jsPDF; setFillColor(ch1: string): jsPDF; setFillColor(ch1: number, ch2: number, ch3: number, ch4?: number): jsPDF; - setFont(fontName: string, fontStyle?: string, fontWeight?: string): jsPDF; + setFont( + fontName: string, + fontStyle?: string, + fontWeight?: string | number + ): jsPDF; setFontSize(size: number): jsPDF; setGState(gState: any): jsPDF; setLineCap(style: string | number): jsPDF; From 6b45c147afa464387bb060089aac37688e58c4c0 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Mon, 21 Dec 2020 17:25:20 +0530 Subject: [PATCH 15/16] Run preetier --- src/jspdf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jspdf.js b/src/jspdf.js index 100b201fb..2a9a3a19c 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -360,7 +360,7 @@ function jsPDF(options) { /** * @function addFontWeight - * @param {string} fontStyle Font style or variant. Example: "italic". + * @param {string} fontStyle Fontstyle or variant. Example: "italic". * @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400 * @returns {string} */ From 9b6806122f80a3cdf90b5db2c3adea6ae1c2d1d9 Mon Sep 17 00:00:00 2001 From: vbinithyanandamv Date: Mon, 21 Dec 2020 18:36:10 +0530 Subject: [PATCH 16/16] Re-run build --- src/jspdf.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/jspdf.js b/src/jspdf.js index 2a9a3a19c..5c73920ed 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -359,7 +359,7 @@ function jsPDF(options) { } /** - * @function addFontWeight + * @function combineFontStyleAndFontWeight * @param {string} fontStyle Fontstyle or variant. Example: "italic". * @param {number | string} fontWeight Weight of the Font. Example: "normal" | 400 * @returns {string}