Title: Various issues with FontProperties · Issue #10249 · matplotlib/matplotlib · GitHub
Open Graph Title: Various issues with FontProperties · Issue #10249 · matplotlib/matplotlib
X Title: Various issues with FontProperties · Issue #10249 · matplotlib/matplotlib
Description: FontProperties (henceforth "FP") is used by Matplotlib for font selection. Instances can be passed e.g. to text() (as the fontproperties kwarg) or legend() (as the prop kwarg); they behave essentially as a mapping with fields such as fam...
Open Graph Description: FontProperties (henceforth "FP") is used by Matplotlib for font selection. Instances can be passed e.g. to text() (as the fontproperties kwarg) or legend() (as the prop kwarg); they behave essentia...
X Description: FontProperties (henceforth "FP") is used by Matplotlib for font selection. Instances can be passed e.g. to text() (as the fontproperties kwarg) or legend() (as the prop kwarg); they behav...
Opengraph URL: https://github.com/matplotlib/matplotlib/issues/10249
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Various issues with FontProperties","articleBody":"FontProperties (henceforth \"FP\") is used by Matplotlib for font selection. Instances can be passed e.g. to `text()` (as the `fontproperties` kwarg) or `legend()` (as the `prop` kwarg); they behave essentially as a mapping with fields such as `family` (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.\r\n\r\nFPs 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).\r\n\r\nA 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).\r\n\r\n- [ ] The user API is not uniform: `text(..., fontproperties=...)` accepts a FP instance or a fontconfig pattern string (but not a dict) vs `legend(..., 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.\r\n\r\n- [ ] 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\") but `fc-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...\r\n\r\n- [ ] 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\".\r\n\r\n- [ ] 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:\r\n```\r\n[ins] In [1]: matplotlib.font_manager.FontProperties(\"sans-serif\").get_family()\r\n\u003cdeep pyparsing traceback, then ValueError: Could not parse font string: 'sans-serif'\u003e\r\n\r\n[ins] In [2]: matplotlib.font_manager.FontProperties(\"sans-serif\", weight=\"bold\").get_family() \r\nOut[2]: ['sans-serif']\r\n\r\n[ins] In [3]: matplotlib.font_manager.FontProperties(\"sans\\-serif\").get_family() \r\nOut[3]: ['sans-serif']\r\n\r\n[ins] In [4]: matplotlib.font_manager.FontProperties(\"sans\\-serif\", weight=\"bold\").get_family() \r\nOut[4]: ['sans\\\\-serif']\r\n```\r\n(i.e. one needs to write `sans-serif` if the weight is given, but `sans\\-serif` if not).\r\n\r\n- [ ] Edit: it may be necessary to distinguish an unset property in a FP and one which has already resolved its fallback-to-default, due to the issue demonstrated in https://github.com/matplotlib/matplotlib/issues/16389#issuecomment-585110776.\r\n\r\n---\r\n\r\nI think the end-result should be something as follows:\r\n\r\n- Deprecate the `fontproperties` kwarg for Text, `prop` for `legend`, etc., in favor of a new `font` kwarg for every one; deprecate `FP` as a public class.\r\n- The `font` kwarg takes one of\r\n * a Path object (explicit font path; raises if the files embeds multiple font faces)\r\n * a (Path, index) pair (to handle files with multiple font faces embedded)\r\n * a CSS font string, with the following allowances:\r\n * \"bare\" font names do not need to be quoted\r\n * the font size can be ignored (defaulting to the rcparam)\r\n * the font family can be ignored (defaulting to the rcparam)\r\n * a CSS font mapping (`{\"family\": ..., \"size\": ...}` corresponding to the CSS `font-family: ...; font-size: ...`)\r\n * possibly (though unclear if needed), a fontconfig font string (using *fontconfig* weights) or fontconfig mapping (as above), although the latter definitely needs to be marked in some way to distinguish it from CSS font mappings.\r\n\r\n---\r\n\r\nSome 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:\r\n- line-height (i.e. linespacing for us)\r\n\r\n---\r\n\r\nSome more notes:\r\nUnfortunately 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 https://github.com/Kozea/tinycss2/issues/4#issuecomment-301393942), 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.)\r\nMoreover, 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\".\r\nIf there's any specialist of CSS syntax I'm all ears.\r\nHowever the plan of just taking a mapping kwarg remains.","author":{"url":"https://github.com/anntzer","@type":"Person","name":"anntzer"},"datePublished":"2018-01-14T09:15:12.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/10249/matplotlib/issues/10249"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:56080153-0d6c-ea52-8af9-b9c3f96bb61d |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | CCEE:2D3512:155AD50:1C5884F:6A52B597 |
| html-safe-nonce | 1916a88b7d6dabb64faed90f99a8f06497ddce138b21279386997b9f547303e5 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJDQ0VFOjJEMzUxMjoxNTVBRDUwOjFDNTg4NEY6NkE1MkI1OTciLCJ2aXNpdG9yX2lkIjoiMjE2Nzc5NDc1NjI4NTAxOTU0MyIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | ad733845991b184fbae8a1e7d4791123a7964abcd4ce7d1274e0de90f785db94 |
| hovercard-subject-tag | issue:288394098 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/matplotlib/matplotlib/10249/issue_layout |
| twitter:image | https://opengraph.githubassets.com/6e0a98f38d64405a163974570c5360cd45d470dfb7a1fa2c98a161fafea80eb3/matplotlib/matplotlib/issues/10249 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/6e0a98f38d64405a163974570c5360cd45d470dfb7a1fa2c98a161fafea80eb3/matplotlib/matplotlib/issues/10249 |
| og:image:alt | FontProperties (henceforth "FP") is used by Matplotlib for font selection. Instances can be passed e.g. to text() (as the fontproperties kwarg) or legend() (as the prop kwarg); they behave essentia... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | anntzer |
| hostname | github.com |
| expected-hostname | github.com |
| None | b9a586c06a05a7a86fc7e3f4dbd03e42f6869085879aa184aa6369456dbd50fb |
| turbo-cache-control | no-preview |
| go-import | github.com/matplotlib/matplotlib git https://github.com/matplotlib/matplotlib.git |
| octolytics-dimension-user_id | 215947 |
| octolytics-dimension-user_login | matplotlib |
| octolytics-dimension-repository_id | 1385122 |
| octolytics-dimension-repository_nwo | matplotlib/matplotlib |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 1385122 |
| octolytics-dimension-repository_network_root_nwo | matplotlib/matplotlib |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 07a982c1d40157c619b364352b704c3ce66bb332 |
| ui-target | canary-2 |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width