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 Document#font_style and Document#font_family #1225

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

c960657
Copy link

@c960657 c960657 commented Nov 14, 2021

Just like you can use font_size to adjust the size without passing the current font name, add font_style to allow changing the style while preserving the font size and family.

@c960657 c960657 changed the title Add Font#font_style and Font#font_family Add#font_style and Font#font_family Nov 15, 2021
@c960657 c960657 changed the title Add#font_style and Font#font_family Add Document#font_style and Document#font_family Nov 15, 2021
Copy link
Member

@gettalong gettalong left a comment

Choose a reason for hiding this comment

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

Thanks for this PR!

I think that this is a feature that can be added without problems once the few questions are addressed.

lib/prawn/font.rb Outdated Show resolved Hide resolved
lib/prawn/font.rb Outdated Show resolved Hide resolved
manual/text/font_style.rb Outdated Show resolved Hide resolved
spec/prawn/font_spec.rb Outdated Show resolved Hide resolved
def font_style(style = nil, &block)
return @font ? @font.style : :normal if style.nil?

font(font_family, style: style, &block)
Copy link
Member

Choose a reason for hiding this comment

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

So, thinking a bit more about this, why not just pass nil here? Then it would automatically use what's defined in #font and we would have only one place where it would be defined.

Copy link
Member

Choose a reason for hiding this comment

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

Another thing that came to mind: What happens if we pass the path to a e.g. TrueType font into #font and then call font_style(:bold)?

Since nothing is defined and we cannot know which file contains the bold version, I guess the only thing to do is to error out here. What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

Maybe we should check here for font_families.key?(font_family) and error out if nothing is found. This would solve both problems, the one when using a TrueType font path and the one where the used key is different from the family name. The error message should probably include the reason for the failure, i.e. that the family name was not registered.

And updating the documentation for #font_families in this regard would also make sense.

Copy link
Author

Choose a reason for hiding this comment

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

I agree that calling font_style with a key not present in font_families for the current font should raise an error. Likewise if style is other than normal for a specific font file.

Currently font does not raise in the latter situation, and changing this would be a BC break, so I suggest we avoid raising in that specific situation (font called with style other than normal for a specific font file). I imagine that it is not uncommon that people do e.g. font 'ArialBlack.ttf', style: :bold in some (mis-guided) attempt to signal that this is a bold font.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed on both accounts.

@pointlessone What do you think?

Copy link
Member

Choose a reason for hiding this comment

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

It's a little confusing because we have two very similar things:

  • font_families which afford for styles
  • font_registry which does not quite do that.

font_families eventually gets mapped to entries in font_registry. Style is only meaningful on the stage of this mapping. Whenever a font file is used directly style has no meaning. It still will be present in the font but there's no way to look up a font for any other style. We never add these fonts to font_families, we never look up fonts by their internal names so we're unable to correlate fonts from the same family. I don't think this should change now. Maybe in the next major release if we ever get there.

I suggest, we make font_style(:random_style) { ... } a noop but issue a warning. Possibly, when $DEBUG is set but it rarely is so it's not very useful.


it 'allows setting font style for a TTF font' do
pdf.font_families.update(
'DejaVu Sans' => {
Copy link
Member

Choose a reason for hiding this comment

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

Thanks for the test case. However, I think to really test this out we need another entry where the key is different from the font family name, e.g. using 'deja' as key.

I don't think that the current implementation would work since the look-up key would be the family name from the TTF.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

3 participants