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

Support font-variant: small-caps approximation #123

Closed
blagasz opened this issue Sep 21, 2013 · 14 comments · Fixed by #2345
Closed

Support font-variant: small-caps approximation #123

blagasz opened this issue Sep 21, 2013 · 14 comments · Fixed by #2345
Labels
bug Existing features not working as expected
Milestone

Comments

@blagasz
Copy link

blagasz commented Sep 21, 2013

Hi,

It is written in the documentation that I should report as a bug if some CSS 2.1 feature is not working. I found that the font-variant: small-caps CSS is not working (it works in the browser, but not in the rendered PDF).

Hope this helps, regards, Sz'

@SimonSapin
Copy link
Member

I could reproduce this. I just tested that we’re correctly setting VARIANT_SMALL_CAPS when calling Pango, but I don’t know what’s going on after that… @liZe ?

@SimonSapin
Copy link
Member

Possibly this only works for fonts that have a small-caps variant, but I don’t know how to find one.

@blagasz
Copy link
Author

blagasz commented Sep 21, 2013

I was thinking about that as well, but is it really necessary? I mean every font could be easily small-capitalized by capitalization and adjustment of font size. I am not sure though if that is how it works!

@SimonSapin
Copy link
Member

Yes, that would be synthesizing the small-caps variant. I suspect that’s what browsers are doing when the font does not provide it directly. I suppose we could do it with a combination of text-transform and font-size.

@liZe
Copy link
Member

liZe commented Sep 21, 2013

Yes, you're right, many softwares (including browsers) use caps with a smaller size instead of small caps when they are not available. That a crime against typography (real small caps characters keep the same weight as regular variants for a size, false small caps don't), but that's definitely not the worst.

Small caps are available in a separate font file (just as bold and italic, Fontin is a good example) or via an OpenType feature (like ligatures and kerning tables, Libertine is a good example). Unfortunately, both solutions seem to be broken in WeasyPrint, even if the code looks OK. We have to figure out what's going on.

@liZe
Copy link
Member

liZe commented Sep 21, 2013

By the way, creating a "false" small-caps variant fallback can be difficult, because I'm not sure that there's a way to know in Pango if a real small-caps variant exists.

@liZe liZe changed the title font-variant: small-caps Support font-variant: small-caps approximation Sep 12, 2016
@liZe
Copy link
Member

liZe commented Dec 7, 2016

After playing with Pango and fontconfig, I now know that fontconfig is able to generate false small-caps, italic and bold fonts. I don't know exactly how it works, but I suppose that it could be included in WP. I'm ready to help anybody interested in this feature 😄.

@manuthema
Copy link

Hi liZe. I´m interesting in this feature

@liZe
Copy link
Member

liZe commented May 29, 2019

Hi liZe. I´m interesting in this feature

Good to know!

After playing with Pango and fontconfig, I now know that fontconfig is able to generate false small-caps, italic and bold fonts. I don't know exactly how it works, but I suppose that it could be included in WP. I'm ready to help anybody interested in this feature .

Well… It's definitely true for "faux" bold and italic, and it's already included in WeasyPrint. But unfortunately, I can't find anything about small caps 😢. I don't know if it was just a dream, if I've seen small caps where there was just bold and italic, or if I had found something else… If anyone knows anything about that, I'd be glad to learn!

@liZe liZe added bug Existing features not working as expected and removed conformance labels Dec 17, 2019
maxocub pushed a commit to erudit/eruditorg that referenced this issue Feb 6, 2020
If a font does not have a small cap variant, Weasyprint will not try to
approximate one.

This commit displays small caps characters in regular uppercase.

ref: Kozea/WeasyPrint#123
ref: #2055
@liZe liZe added this to the 55.0 milestone Dec 11, 2021
@grewn0uille grewn0uille removed this from the 55.0 milestone Mar 31, 2022
@bpj
Copy link

bpj commented May 8, 2022

/*** fake small-caps to overcome bug ***/

.sc, .smallcaps, .small-caps {
  /* font-variant: small-caps; */
  text-transform: uppercase; font-size: 75%; 
}

/* force uppercase inside faked small-caps:
  <span class="sc"><span class="scu">C</span>aesar</span>
*/
.scu {
  /* font-variant: small-caps; */
  text-transform: uppercase; font-size: 133%; 
}

/* .usc = uppercase to small-caps:
   <span class="usc">SMALL CAPS</span>
   <span class="usc"><span class="scu">C</span>AESAR</span>
*/
.usc {
  /* text-transform: lowercase; font-variant: small-caps; */
  text-transform: uppercase; font-size: 75%;
}

Fake small caps may be a crime against typography, but what can you do when the style guide demands them?

@t4k
Copy link

t4k commented Sep 22, 2022

Thanks for that fake small-caps example.

I'd really like to see this implemented natively, if possible. The markup required for surrounding all the true uppercase letters in a string with spans seems incredibly daunting for a publishing process we are trying to automate.

Some other folks were looking at similar issues at some point: Automattic/node-canvas#894

@xavidotron
Copy link
Contributor

For what it's worth, Pango does seem to support synthetic small caps these days. E.g., the following with pango-view does the right thing:

<span font='Times New Roman 64'> 
<span variant='small-caps'>Some Words</span>
</span>

However, for reasons that I don't understand the naive approach of adding this to get_font_description() in text/fonts.py does not seem to work, giving me full-sized caps that overlap each other (as if the spacing for small caps was being used but not the actual small caps).

    if style['font_variant_caps'] != 'normal':                                  
        pango.pango_font_description_set_variant(                               
            font_description, PANGO_VARIANT[style['font_variant_caps']])

(with PANGO_VARIANT mapping 'small-caps' to PANGO_VARIANT_SMALL_CAPS).

Example output:

Image

As compared to pango-view:

Image

I'm at a loss for debugging this but perhaps someone else has more insight.

@xavidotron
Copy link
Contributor

(To be more precise, Pango's synthetic small caps won't work on Mac until https://gitlab.gnome.org/GNOME/pango/-/issues/841 is resolved, but it seems to work on Linux at HEAD.)

liZe added a commit that referenced this issue Jan 11, 2025
liZe added a commit that referenced this issue Jan 11, 2025
@liZe liZe mentioned this issue Jan 11, 2025
5 tasks
@liZe
Copy link
Member

liZe commented Jan 11, 2025

For what it's worth, Pango does seem to support synthetic small caps these days.

@xavidotron Thanks for your code, that was really useful. 😍

I opened #2345 with some code I only tested manually. Don’t hesitate to give some feedback and share your thought about it!

(Bug 123 solved by PR 2345 would be fun.)

@liZe liZe added this to the 64.0 milestone Jan 19, 2025
@liZe liZe closed this as completed in fc147ba Jan 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Existing features not working as expected
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants