-
-
Notifications
You must be signed in to change notification settings - Fork 7.9k
Various issues with FontProperties #10249
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
Comments
For the record: TinyCSS2 and CSSSelect2 are maintained, even if there's not much to do (the specs don't change that often 😄).
You can find an implementation of the |
That's really great to hear :) I may revisit this issue, then... |
I should have answered this directly: as the initial value for these properties is "normal", and as the possible values for each property are mutually exclusive, you can actually skip the "normal" values and assign the other values to the matching properties. |
thanks. |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Similar open issue #4822. |
This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help! |
Uh oh!
There was an error while loading. Please reload this page.
FontProperties (henceforth "FP") is used by Matplotlib for font selection. Instances can be passed e.g. to
text()
(as thefontproperties
kwarg) orlegend()
(as theprop
kwarg); they behave essentially as a mapping with fields such asfamily
(DejaVu Sans, etc),weight
(a CSS/OpenType numeric value, per https://developer.mozilla.org/en-US/docs/Web/CSS/font-weight, with a mapping for names such as "bold", etc.), etc., that are matched against the list of system fonts (in the infamous fontList.json), with font_manager.py, using a custom-designed distance function.FPs can be constructed either by passing kwargs to the constructor (
FP(family=..., weight=..., ...)
) or a fontconfig-style pattern string (FP("DejaVu Sans:bold:...")
, https://www.freedesktop.org/software/fontconfig/fontconfig-user.html).A few issues with FPs are listed below. Note that this discussion is not directly related to the font cache (misguessing of font weights, etc.) issues (although the font cache is used to match FPs).
The user API is not uniform:
text(..., fontproperties=...)
accepts a FP instance or a fontconfig pattern string (but not a dict) vslegend(..., prop=...)
which uses a different kwarg and accepts a FP instance or a dict (but not a fontconfig pattern string). While this issue should be relatively easy to fix, I am reluctant to encourage usage of fontconfig pattern strings until the next issue is fixed.The fontconfig-style strings use CSS/OpenType weights, which are very different from fontconfig weights (https://lists.freedesktop.org/archives/fontconfig/2011-September/003646.html). For example,
text(.5, .5, "foo", fontproperties=":weight=200")
will use a thin font (CSS 200 = "Extra/Ultra Light") butfc-list :weight=200
at the terminal will return bold fonts (FC 200 = "Bold"). It would seem more reasonable e.g. to use CSS-style strings (https://developer.mozilla.org/en-US/docs/Web/CSS/font), although these require a font size to be included (but it doesn't seem too bad to allow ignoring it). Note that if we really want to do so, it seems possible to support both fc-style and CSS-style strings (with optional weight) because they can be differentiated (namely, a fc-string (other than a bare family name, which looks the same in both cases up to quoting) contains colons whereas css-strings do not). Not sure it's really worth it though...The FP matching algorithm is custom-made, while we could actually follow a standard algorithm (e.g. https://www.w3.org/TR/css-fonts-4/#font-matching-algorithm, or whatever fontconfig uses (which seems a bit underspecified though, and probably technically depends on the user's fonts.conf...)). Arguably this is less an issue as long as the current algorithm does something "reasonable".
The FP constructor is a bit a mess because it overloads the first argument to be either the family or a fontconfig pattern (the latter needs to be escaped, but not the former), so the syntax for requesting a sans-serif font depends on whether one requests additional properties or not:
(i.e. one needs to write
sans-serif
if the weight is given, butsans\-serif
if not).I think the end-result should be something as follows:
fontproperties
kwarg for Text,prop
forlegend
, etc., in favor of a newfont
kwarg for every one; deprecateFP
as a public class.font
kwarg takes one of{"family": ..., "size": ...}
corresponding to the CSSfont-family: ...; font-size: ...
)Some other potentially relevant CSS options (it's not so much that I deeply love CSS, just that it has the benefit of being a spec that already exists...), just treating this as a list of ideas:
Some more notes:
Unfortunately even tokenizing CSS seems not so trivial; the only Python libs that I found for that task are https://github.com/Kozea/tinycss (no commit for more than a year), https://github.com/Kozea/tinycss2 (essentially abandoned, per Kozea/tinycss2#4 (comment)), and https://bitbucket.org/cthedot/cssutils (LGPL, and no commit for more than a year). (And parsing still needs to go on top of that.)
Moreover, having read the CSS font spec a few times, I just don't understand how the "font" shorthand resolves whether "normal" refers to "font-weight", "font-stetch", or "font-style".
If there's any specialist of CSS syntax I'm all ears.
However the plan of just taking a mapping kwarg remains.
The text was updated successfully, but these errors were encountered: