Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 11 additions & 8 deletions src/jspdf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand All @@ -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
Expand Down
31 changes: 31 additions & 0 deletions test/specs/fontstyle.spec.js
Original file line number Diff line number Diff line change
@@ -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");
});
});