Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix combining font style and font weight for all sorts of combinations #3217

Merged
merged 2 commits into from
Sep 8, 2021
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");
});
});