-
Notifications
You must be signed in to change notification settings - Fork 280
Fix for set_fallback_fonts() when markdown=True #712
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
Conversation
07f4e0b
to
efc5b67
Compare
Codecov ReportPatch coverage:
Additional details and impacted files@@ Coverage Diff @@
## master #712 +/- ##
==========================================
- Coverage 93.75% 93.53% -0.22%
==========================================
Files 26 26
Lines 7009 7051 +42
Branches 1258 1269 +11
==========================================
+ Hits 6571 6595 +24
- Misses 260 271 +11
- Partials 178 185 +7
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. ☔ View full report at Codecov. |
Quoting #669 (comment) :
I introduced |
3ebebea
to
485efd2
Compare
What other possible selection criteria to find the best replacement font can you think of? Let's say someone later wants to add the possibility to match grotesk/serif fonts with each other. Is it better to introduce another |
My idea was preserving the order the user specify the fonts and go with the first match. |
So when you have original text in both eg. regular and bold style, then you don't want to take that distinction into account in the replacement font seleced? |
Currently Also, as mentioned in #637, for complex "fallback" logic it is probably best to craft your own font files combining characters from several fonts. |
This is not about implementing hypothetical features now. It's about considering features that might possibly become interesting over time, and trying to avoid API choices tha would make adding things later unnecessarily complicated. There are many aspect to any choice of font, and most of them are probably orthogonal to each other. So I think that using a dedicated argument for style criteria (regular/bold/italic) is ok. However, I'm not sure if a simple "apply/ignore style" distinction is good enough in the long run. I can think of three possible strategies:
That would give us an argument like: |
Interesting approach. On a complex document, you may have a scenario like this :
The @gmischler: you did not mention my suggestion of letting users override To me it currently seems like the best solution, as it would allow maximum flexibility to end users, |
The ability to override But I don't think that is a good excuse for unnecessarily limiting the built-in functionality. The "best match" strategy I have outlined is trivial to understand and implement. It will cover every situation where the goal is to display as many characters as close to the desired style as possible. Replacing a font with the wrong face type (regular/bold/italic) is a real issue, and those three strategies offer a complete and flexible solution to that. Just because it doesn't also match font families is not a valid argument against solving the problem id does solve. untested concept: def get_fallback_font(self, char, style="", _font_style_pat = re.compile("[BI]*$")):
first_match = None
for fallback_id in self._fallback_font_ids:
if ord(char) in self.fonts[fallback_id]["cmap"]:
if self._fallback_font_style_strategy == StyleStrategy.IGNORE:
return fallback_id # first match, style ignored
if _font_style_pat.search(fallback_id).match == style:
return fallback_id # exact match
if not first_match:
first_match = fallback_id
if self._fallback_font_style_strategy == StyleStrategy.BEST:
return first_match # best match (or None)
return None # no (good enough) match The iterative approach is probably more efficient here than juggling with several levels of iterators, and definitively more readable. |
1660fa4
to
764d7c1
Compare
@gmischler I have added a commit to this PR implementing what we discussed and adding 2 extra unit tests. In the end I went with a boolean |
b5978af
to
c5284a9
Compare
I'm planning on merging this PR next week. |
c5284a9
to
44ad330
Compare
As a follow-up of #669,
fix the rendering issue visible in this test reference file:
https://github.com/PyFPDF/fpdf2/blob/89c528d/test/fonts/font_fallback.pdf