diff --git a/src/jspdf.js b/src/jspdf.js index 94f3a5f69..2c4367d0e 100644 --- a/src/jspdf.js +++ b/src/jspdf.js @@ -365,7 +365,10 @@ function jsPDF(options) { * @returns {string} * @private */ - var combineFontStyleAndFontWeight = function(fontStyle, fontWeight) { + var combineFontStyleAndFontWeight = (API.__private__.combineFontStyleAndFontWeight = function( + fontStyle, + fontWeight + ) { if ( (fontStyle == "bold" && fontWeight == "normal") || (fontStyle == "bold" && fontWeight == 400) || @@ -374,19 +377,19 @@ function jsPDF(options) { ) { 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 + if (fontWeight) { fontStyle = - fontWeight == 400 - ? fontStyle == "italic" + fontWeight == 400 || fontWeight === "normal" + ? fontStyle === "italic" ? "italic" : "normal" - : fontWeight == 700 && fontStyle !== "italic" + : (fontWeight == 700 || fontWeight === "bold") && + fontStyle === "normal" ? "bold" - : fontStyle + "" + fontWeight; + : (fontWeight == 700 ? "bold" : fontWeight) + "" + fontStyle; } return fontStyle; - }; + }); /** * @callback ApiSwitchBody diff --git a/test/specs/fontstyle.spec.js b/test/specs/fontstyle.spec.js new file mode 100644 index 000000000..380f9de80 --- /dev/null +++ b/test/specs/fontstyle.spec.js @@ -0,0 +1,31 @@ +describe("Font style and font weight", () => { + beforeAll(loadGlobals); + + it("combine font style and font weight correctly", () => { + const doc = new jsPDF(); + + const combine = doc.__private__.combineFontStyleAndFontWeight; + + expect(combine("normal", "normal")).toEqual("normal"); + expect(combine("normal", "400")).toEqual("normal"); + expect(combine("normal", 400)).toEqual("normal"); + + expect(combine("italic", "normal")).toEqual("italic"); + expect(combine("italic", "400")).toEqual("italic"); + expect(combine("italic", 400)).toEqual("italic"); + + expect(combine("normal", "bold")).toEqual("bold"); + expect(combine("normal", "700")).toEqual("bold"); + expect(combine("normal", 700)).toEqual("bold"); + + expect(combine("italic", "bold")).toEqual("bolditalic"); + expect(combine("italic", "700")).toEqual("bolditalic"); + expect(combine("italic", 700)).toEqual("bolditalic"); + + expect(combine("normal", "300")).toEqual("300normal"); + expect(combine("normal", 300)).toEqual("300normal"); + + expect(combine("italic", "300")).toEqual("300italic"); + expect(combine("italic", 300)).toEqual("300italic"); + }); +});