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

Add fonts with different font-weights #3036

Merged
merged 18 commits into from Dec 22, 2020
Merged

Add fonts with different font-weights #3036

merged 18 commits into from Dec 22, 2020

Conversation

vbinithyanandamv
Copy link
Contributor

Closing: #2920

Now user can pass font-weight to differentiate the style using the following API.

JSPDF.addFont(postScriptName, fontName, fontStyle, fontWeight, encoding)

User can also be able to pass without a fontWeight and encoding.

JSPDF.addFont(postScriptName, fontName, fontStyle)

User can also only pass encoding using like this

JSPDF.addFont(postScriptName, fontName, null, encoding)

Need to discuss on this last case @HackbrettXXX

Need more input on Make sure that passing an encoding without fontWeight will still work.

@vbinithyanandamv vbinithyanandamv changed the title Add fonts with different font-weights #2920 Add fonts with different font-weights Dec 13, 2020
@HackbrettXXX
Copy link
Collaborator

Need more input on Make sure that passing an encoding without fontWeight will still work.

What I mean is that this should still work:

JSPDF.addFont(postScriptName, fontName, fontStyle, encoding)

So we need to check if the 4th parameter is an encoding string (one of the four constants) or a fontWeight string (a number or a string like '500' or 'bold').

Regarding this:

JSPDF.addFont(postScriptName, fontName, null, encoding)

AFAIK, the (old) fontStyle parameter was not optional/nullable, so I think passing null as fontStyle should still be illegal. The fontWeight parameter should be optional for backwards compatibility, though.

src/jspdf.js Outdated
throw new Error("Invalid Combination of fontweight and fontstyle");
}
if (fontWeight && fontStyle !== fontWeight) {
fontStyle = fontWeight == 400 ? 'normal' : fontWeight == 700 ? 'bold' : fontStyle + ' ' + fontWeight;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  • make sure to also compare with 'normal' and 'bold' strings as fontWeight values.
  • Maybe add a comment that the == vs === is intentional

@vbinithyanandamv
Copy link
Contributor Author

vbinithyanandamv commented Dec 14, 2020

Need more input on Make sure that passing an encoding without fontWeight will still work.

What I mean is that this should still work:

JSPDF.addFont(postScriptName, fontName, fontStyle, encoding)

So we need to check if the 4th parameter is an encoding string (one of the four constants) or a fontWeight string (a number or a string like '500' or 'bold').

Regarding this:

JSPDF.addFont(postScriptName, fontName, null, encoding)

AFAIK, the (old) fontStyle parameter was not optional/nullable, so I think passing null as fontStyle should still be illegal. The fontWeight parameter should be optional for backwards compatibility, though.

My bad i meant to make the fontweight parameter null. Anyways had a doubt on that item got it cleared now. Now will make it up based on the encoding check or fontweight argument check. which will keep the old code intact and same without creating many new change.

src/jspdf.js Outdated
fontWeight,
encoding
) {
let encodingOptions = [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't use let. We need to write es5 code.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/jspdf.js Outdated
//IE 11 fix
encoding = arguments[3];
} else if (arguments[3] && encodingOptions.indexOf(arguments[3]) == -1) {
//if weired combination of fontweight and font style throw error
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo ;)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/jspdf.js Outdated Show resolved Hide resolved
src/jspdf.js Outdated
(fontStyle == "normal" && fontWeight == "bold") ||
(fontStyle == "bold" && fontWeight == "normal") ||
(fontStyle == "bold" && fontWeight == 400) ||
(fontStyle == "normal" && fontWeight == 700)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normal+700 as well

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

src/jspdf.js Outdated
Comment on lines 4889 to 4894
fontStyle =
fontWeight == 400
? "normal"
: fontWeight == 700
? "bold"
: fontStyle + "" + fontWeight;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What about "italic" fontStyle? E.g. "italic" + "400" should result in "italic".

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

test/specs/putTotalPages.spec.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@HackbrettXXX HackbrettXXX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also need to change the signatures in the types/index.d.ts file.

src/jspdf.js Outdated
@@ -4795,7 +4795,8 @@ function jsPDF(options) {
* @memberof jsPDF#
* @name setFont
*/
API.setFont = function(fontName, fontStyle) {
API.setFont = function(fontName, fontStyle, fontWeight) {
fontStyle = !fontWeight ? fontStyle : fontStyle + "" + fontWeight;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need the same logic as in addFont here.

src/jspdf.js Outdated
@@ -4850,14 +4851,52 @@ 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.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

number|string

src/jspdf.js Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
types/index.d.ts Outdated Show resolved Hide resolved
Copy link
Collaborator

@HackbrettXXX HackbrettXXX left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Alright, looks good to me now. Please run prettier again, and I'll merge it.

@vbinithyanandamv
Copy link
Contributor Author

Alright, looks good to me now. Please run prettier again, and I'll merge it.

@HackbrettXXX added prettier run already.

@vbinithyanandamv
Copy link
Contributor Author

vbinithyanandamv commented Dec 21, 2020

@HackbrettXXX build is failing on some wired issue in all PR.

This was referenced Mar 12, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants