diff --git a/src/jspdf.js b/src/jspdf.js index de3a7bf3c..5c73920ed 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -358,6 +358,35 @@ function jsPDF(options) { apiMode = ApiMode.COMPAT; } + /** + * @function combineFontStyleAndFontWeight + * @param {string} fontStyle Fontstyle 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 @@ -4789,13 +4818,17 @@ 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} * @memberof jsPDF# * @name setFont */ - API.setFont = function(fontName, fontStyle) { + API.setFont = function(fontName, fontStyle, fontWeight) { + if (fontWeight) { + fontStyle = combineFontStyleAndFontWeight(fontStyle, fontWeight); + } activeFontKey = getFont(fontName, fontStyle, { disableWarning: false }); @@ -4850,6 +4883,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,7 +4891,25 @@ function jsPDF(options) { * @name addFont * @returns {string} fontId */ - API.addFont = function(postScriptName, fontName, fontStyle, encoding) { + API.addFont = function( + postScriptName, + fontName, + fontStyle, + fontWeight, + encoding + ) { + var 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) { + fontStyle = combineFontStyleAndFontWeight(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..a6db023e5 100644 --- a/test/specs/putTotalPages.spec.js +++ b/test/specs/putTotalPages.spec.js @@ -42,3 +42,90 @@ 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 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 }); + var totalPagesExp = "{totalPages}"; + + doc.addFileToVFS("PTSans.ttf", PTSans); + doc.addFont("PTSans.ttf", "PTSans", "normal",200, "Identity-H"); + + doc.setFont("PTSans",'normal',200); + + 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 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"); +}); diff --git a/types/index.d.ts b/types/index.d.ts index c60a67589..49e436b63 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 | number, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -623,6 +624,7 @@ declare module "jspdf" { url: URL, id: string, fontStyle: string, + fontWeight?: string | number, encoding?: | "StandardEncoding" | "MacRomanEncoding" @@ -763,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): jsPDF; + setFont( + fontName: string, + fontStyle?: string, + fontWeight?: string | number + ): jsPDF; setFontSize(size: number): jsPDF; setGState(gState: any): jsPDF; setLineCap(style: string | number): jsPDF;