Skip to content

Fix that font files never pass the test on Win #24641

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

Merged
merged 1 commit into from
Dec 6, 2022
Merged

Fix that font files never pass the test on Win #24641

merged 1 commit into from
Dec 6, 2022

Conversation

almarklein
Copy link
Contributor

Hi! 👋

I am gratefully making use of MPL's font management code for our text rendering in PyGfx. While I was examining the code, I found what I think is a bug.

The isfile() test is applied on the relative pathname and will thus always fail. This PR should fix that. I would expect that it should also test for the extension, but I'm not 100% sure so I did not add this yet.

Diving a bit deeper, this bug has likely gone unnoticed because the code to detect fonts follows two paths on Windows:

  • It checks the registry for known installed fonts in a small set of predefined directories.
  • It detects font files in these same directories using os.listdir or os.walk.

Why are these two paths followed? I can see how the second route can find fonts that are present but not known in the registry, but why check the registry at all then?

Copy link

@github-actions github-actions bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for opening your first PR into Matplotlib!

If you have not heard from us in a while, please feel free to ping @matplotlib/developers or anyone who has commented on the PR. Most of our reviewers are volunteers and sometimes things fall through the cracks.

You can also join us on gitter for real-time discussion.

For details on testing, writing docs, and our review process, please see the developer guide

We strive to be a welcoming and open project. Please follow our Code of Conduct.

Copy link
Member

@oscargus oscargus left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cannot really tell the background, but realize that this should work. Thanks!

@timhoffm timhoffm merged commit fedb162 into matplotlib:main Dec 6, 2022
@almarklein almarklein deleted the patch-1 branch December 6, 2022 14:14
@almarklein
Copy link
Contributor Author

What about my comment about testing for the extension? The code path in the else branch does test for it, while this one does not. This was not a problem, because the test never passed anyway, but IIUC right now fonts with different extensions could be included. I suspect this was an oversight rather than intentional, but I may be missing something...

@oscargus
Copy link
Member

oscargus commented Dec 6, 2022

I think that @anntzer may know more about it.

@anntzer
Copy link
Contributor

anntzer commented Dec 7, 2022

I suspect that the fix here is indeed incorrect as it will return files with incorrect extensions. The previous (wrong) code came in in #22909 to fix #22859, but given the discussion at #22859 I suspect that the correct approach on Windows is indeed to only return fonts listed by _get_win32_installed_fonts and not bother calling list_fonts at all. (Can anyone open a new issue with that, if they agree with my analysis?)

@almarklein
Copy link
Contributor Author

almarklein commented Dec 7, 2022

Thanks for the historic background!

given the discussion at #22859 I suspect that the correct approach on Windows is indeed to only return fonts listed by _get_win32_installed_fonts and not bother calling list_fonts at all. (Can anyone open a new issue with that, if they agree with my analysis?)

In the top post I stated my confusion for having two approaches that obtain (nearly) the same list of fonts. I think it would be good to pick one approach: either ask the registry, or scrape dirs with list_fonts.

Indeed the discussion in #22859 seemed to be mostly about "what fonts are officially listed" (and show up in e.g. Word). I agree that this points towards asking the registry for a list (no list_fonts). I'll make a PR: #24655

Sidenote: in PyGfx I now took the approach to always scrape directories, because that way we can easily test when fonts are added/removed by checking a dir's mtime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants