Skip to content

Commit 0ea7418

Browse files
authored
fix combining font style and font weight for all sorts of combinations (#3217)
1 parent 3dd3375 commit 0ea7418

File tree

2 files changed

+42
-8
lines changed

2 files changed

+42
-8
lines changed

src/jspdf.js

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -365,7 +365,10 @@ function jsPDF(options) {
365365
* @returns {string}
366366
* @private
367367
*/
368-
var combineFontStyleAndFontWeight = function(fontStyle, fontWeight) {
368+
var combineFontStyleAndFontWeight = (API.__private__.combineFontStyleAndFontWeight = function(
369+
fontStyle,
370+
fontWeight
371+
) {
369372
if (
370373
(fontStyle == "bold" && fontWeight == "normal") ||
371374
(fontStyle == "bold" && fontWeight == 400) ||
@@ -374,19 +377,19 @@ function jsPDF(options) {
374377
) {
375378
throw new Error("Invalid Combination of fontweight and fontstyle");
376379
}
377-
if (fontWeight && fontStyle !== fontWeight) {
378-
//if fontstyle is normal and fontweight is normal too no need to append the font-weight
380+
if (fontWeight) {
379381
fontStyle =
380-
fontWeight == 400
381-
? fontStyle == "italic"
382+
fontWeight == 400 || fontWeight === "normal"
383+
? fontStyle === "italic"
382384
? "italic"
383385
: "normal"
384-
: fontWeight == 700 && fontStyle !== "italic"
386+
: (fontWeight == 700 || fontWeight === "bold") &&
387+
fontStyle === "normal"
385388
? "bold"
386-
: fontStyle + "" + fontWeight;
389+
: (fontWeight == 700 ? "bold" : fontWeight) + "" + fontStyle;
387390
}
388391
return fontStyle;
389-
};
392+
});
390393

391394
/**
392395
* @callback ApiSwitchBody

test/specs/fontstyle.spec.js

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
describe("Font style and font weight", () => {
2+
beforeAll(loadGlobals);
3+
4+
it("combine font style and font weight correctly", () => {
5+
const doc = new jsPDF();
6+
7+
const combine = doc.__private__.combineFontStyleAndFontWeight;
8+
9+
expect(combine("normal", "normal")).toEqual("normal");
10+
expect(combine("normal", "400")).toEqual("normal");
11+
expect(combine("normal", 400)).toEqual("normal");
12+
13+
expect(combine("italic", "normal")).toEqual("italic");
14+
expect(combine("italic", "400")).toEqual("italic");
15+
expect(combine("italic", 400)).toEqual("italic");
16+
17+
expect(combine("normal", "bold")).toEqual("bold");
18+
expect(combine("normal", "700")).toEqual("bold");
19+
expect(combine("normal", 700)).toEqual("bold");
20+
21+
expect(combine("italic", "bold")).toEqual("bolditalic");
22+
expect(combine("italic", "700")).toEqual("bolditalic");
23+
expect(combine("italic", 700)).toEqual("bolditalic");
24+
25+
expect(combine("normal", "300")).toEqual("300normal");
26+
expect(combine("normal", 300)).toEqual("300normal");
27+
28+
expect(combine("italic", "300")).toEqual("300italic");
29+
expect(combine("italic", 300)).toEqual("300italic");
30+
});
31+
});

0 commit comments

Comments
 (0)