-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Adding font weight support #402
Comments
Hey. Font.register(src1, { fontFamily: 'Roboto-Regular' });
Font.register(src2, { fontFamily: 'Roboto-Bold' });
const styles = StyleSheet.create({
title: { fontFamily: 'Roboto-Bold' },
paragraph: { fontFamily: 'Roboto-Regular' },
}) Or you can also choose one of the default fonts shipped with the lib:
The API could change, but you still have to provide different files and change the font-family for each chunk you want styled differently. I know it's not ideal, but it's the only way |
Closing this, since there's nothing else to do about it |
Hi there, this wasn't a request for support, but instead a request for conversation about how one might implement font weights in I noticed that Google Fonts has moved over to bundled |
Just as a note, I am interested in contributing on this feature but if the issue remains closed I will just assume that you are not looking for this particular contribution. It looks like you're using a modified version of |
My apologizes! I thought you were just asking how to implement font-weights 😄 I wasn't thinking that you were requesting a change haha. Even if you did, I truly don't see any other alternative rather than loading different font files. You are very welcomed to contribute! Would be awesome if you can came up with a nicer solution to this feature Yes. I'm using a
About the first point, I do remember having trouble with some fontkit dependencies so I had to disable some font extensions. I never had time to look back at that part of the project, and that was some time ago, so I don't remember the details exactly right now. I would very pleased to assist you on this fix though. So those are basically the differences from the original |
As far as the interface is concerned, I’d suggest implementing the |
If it can help someone, here is a way to work with font-weight. It works great if using inline style. Essentially, it's a Text primitive that wraps the React-Pdf Text component and parses the style prop to add the right font-family. You still have to register the different fonts, but using font-weights as you build your pdf is easier. import React from 'react';
import { Text as TextPDF } from '@react-pdf/renderer';
class Text extends React.Component {
_getFontFamily = ({ fontWeight, fontFamily }) => {
if (fontFamily) return fontFamily;
if (!fontWeight) return 'Roboto';
const weight = parseInt(fontWeight, 10);
if (fontWeight <= 300) {
return 'Roboto-Light';
} else if (fontWeight <= 400) {
return 'Roboto';
} else if (fontWeight <= 500) {
return 'Roboto-Medium';
} else if (fontWeight <= 700) {
return 'Roboto-Bold';
} else {
return 'Roboto-Black';
}
};
render() {
const { children, className, style = {}, force, debug } = this.props;
return (
<TextPDF
debug={debug}
className={className}
style={{
fontFamily: this._getFontFamily(style),
...style,
}}
>
{children}
</TextPDF>
);
}
}
export default Text; A possible Library side solution might be to register fonts with a font-weight or preferred weight. This way, the style font-weight could be implemented on the library side by following the technique outlined above. @diegomura , what do you think ? ex. Font.register(`${public_url}/fonts/Roboto-Light.ttf`, {
family: 'Roboto-Light',
weight: '100,200,300',
});
Font.register(`${public_url}/fonts/Roboto-Medium.ttf`, {
family: 'Roboto-Medium',
weight: '400,500',
}); |
Thanks for pushing this feature forward. Here's what I think: I always tried to be as close to the web APIs as possible. It's great to take advantage of these, since most of people already know them. So implementing the I think @agarant solution really makes a change though. Because would enable to handle the styling of a text as you would do it in the web (using the In conclusion, I think we can use some kind of mixture of both solutions: Font.register(`${public_url}/fonts/Roboto-Light.ttf`, {
family: 'Roboto',
weight: '100,200,300', // or [100, 200, 300]
});
Font.register(`${public_url}/fonts/Roboto-Medium.ttf`, {
family: 'Roboto',
weight: '400,500',
}); Internally we can also map values such as What do you guys think? |
I think this is a fine solution. My only additional wish would be to support font formats that have multiple weights inside them, as it seems like |
I’ve updated the link to I like updating
We could call |
I've been giving this more thought and Flutter uses a similar method where you register the fonts:
- family: Trajan Pro
fonts:
- asset: fonts/TrajanPro-ExtraLight.ttf
weight: 200
- asset: fonts/TrajanPro-Light.ttf
weight: 300
- asset: fonts/TrajanPro-Regular.ttf
weight: 400
- asset: fonts/TrajanPro-Medium.ttf
weight: 500
- asset: fonts/TrajanPro-Bold.ttf
weight: 600 Note: I don't think we should use a yaml file, this is just an example from outside React which supports the current idea |
@lukepighetti for reference, MDN explains the algorithm used on the web to resolve font weight keywords against numeric weights. |
@lukepighetti were you able to make any headway with this? I am doing some work related to this over at #508. |
Hi @DuncanMacWeb , I am sorry to report that I haven't been using |
No worries @lukepighetti! |
I am rendering pdf by passing blob to the file prop. Can we change the font size or fontweight of that? |
Hi there,
I'd like to add font weight support to
react-pdf
and I was hoping a contributor could recommend an efficient strategy or inform me of any existing work before I dive in.Cheers
Luke
The text was updated successfully, but these errors were encountered: