-
Notifications
You must be signed in to change notification settings - Fork 263
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
Font fallback #637
Comments
Hi @CY-Qiu Thank you for opening a discussion about this feature idea. For reference: Regarding your suggestion, I see two main approaches to implement this "font fallback" mechanism:
This is certainly doable, but would require some refactoring in A starting point would be to first implement step 1. and detect when characters are not "present" in the selected font. Finally, another alternative to this "fallback" mechanism can simply be to craft your own font files combining characters from several fonts. |
I think any such check/substitution would have to happen much earlier. The natural place to do something like that is when the text fragments are created. But there are quite a number of questions that we'd need to answer first:
|
I fully agree. I think we could just support fallback Latin-1 characters with a builtin font: https://github.com/PyFPDF/fpdf2/blob/2.6.0/fpdf/fpdf.py#L285 This seems relatively simple, could be useful for Also this "fallback to render unupported chars with a standard font" mechanism would have to be enabled explicitely. What do you think about this @gmischler & @CY-Qiu? |
Hi @CY-Qiu Unless you have more to add to this feature suggestion, I'm probably going to close this discussion. Thank you anyway for opening this exchange 😃 |
Font fallback is also an issue for a project I'm working on, where users can choose their name, and I have plenty of names like 𝕥𝕙𝕚𝕤, 🆃🅷🅸🆂 and emojis that can't be rendered on the report. I explored the option of getting all the possible glyphs on a single font, but fonts can't grow indefinitely - there's a hard limit. An ideal scenario for me would be allowing the user to set an array of fonts on set_font(). FPDF2 will use the first font, and if a glyph is not present, start looking for the glyph on the other fonts in the array. Something like: I believe it could be processed in fragments, similarly to how markdown does. |
Note that you can easily convert a fancy character like 𝕥 to a simple "t": unicodedata.normalize('NFKD', '𝕥') # ref: https://en.wikipedia.org/wiki/Unicode_equivalence#Normalization But I see how one would like to preserve the original format of the names that users provided as input. After experimenting a bit, I found some fonts able to render 𝕥𝕙𝕚𝕤 & 🆃🅷🅸🆂 : from fpdf import FPDF
pdf = FPDF()
pdf.add_page()
pdf.add_font(fname="DejaVuSans.ttf")
pdf.add_font(fname="NotoSansSymbols.ttf")
pdf.add_font(fname="Segoe-UI-Symbol.ttf")
pdf.set_font('DejaVuSans')
pdf.cell(txt="𝕥𝕙𝕚𝕤")
pdf.set_font('NotoSansSymbols')
pdf.cell(txt="🆃🅷🅸🆂")
pdf.set_font('Segoe-UI-Symbol')
pdf.cell(txt="🆃🅷🅸🆂")
pdf.output("fonts.pdf") Produces: fonts.pdf In this scenario, I see how not having to specify what font to use to render every character could be useful. I think that if we want to support something like what you suggested,
A PR implementing this kind of detection mechanism would be welcome as a first step towards a font fallback mechanism. |
Font fallback has been implemented by @andersonhc in #669 It can currently be tested by intalling
A new release of |
Please explain your intent
Both windows MFC and HTML/CSS have font fallback algorithm. This is very important for CJK font. I wonder if fpdf2 could have this feature.
example: We want add a word "𡉃" to output file.
In HTML/CSS, we can add a style: fontFamily = "'Noto Serif CJK SC', 'SimSun-ExtB'".('Noto Serif CJK SC' is developed by Google and published on 'https://github.com/googlefonts/noto-cjk'. 'SimSun-ExtB' is default installed by Windows OS Simplified Chinese version)
this word only exist on 'SimSun-ExtB' font, 'Noto Serif CJK SC' font does not have this word. When the web browser find out 'Noto Serif CJK SC' does not have this word, the web browser would use 'SimSun-ExtB' automatically based on if the font have this word.
However, fpdf2 does not have this feature. if we use 'Noto Serif CJK SC' font, we cannot get "𡉃" displayed. 'SimSun-ExtB' font is very old, many unicode word is not in this font, so we cannot just use 'SimSun-ExtB'.
Describe the solution you'd like
This can be researched.
The text was updated successfully, but these errors were encountered: