Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Search also for user fonts on Windows (#12954) #12957

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 7 commits into from
Jan 13, 2019

Conversation

JanSellner
Copy link

PR Summary

This PR fixes #12954, i.e. the fonts installed to the user directory are now also found by matplotlib.

@tacaswell tacaswell added this to the v3.1 milestone Dec 9, 2018
@@ -180,24 +184,46 @@ def win32InstalledFonts(directory=None, fontext='ttf'):

items = set()
for fontdir in MSFontDirectories:
# System fonts
Copy link
Member

Choose a reason for hiding this comment

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

The logic in this is complicated enough, can you put it in a helper function that we call twice?

Copy link
Member

Choose a reason for hiding this comment

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

although, I now see there is some subtlety with absolute vs relative paths.

@tacaswell
Copy link
Member

Thanks for your work on this!

My comments are mostly directed at the long-term maintainability of this code path, we recently went through a few rounds of edits on those try/except blocks interacting with the registry so I am a bit concerned about having 2 copies of that code path (so if we do still have bugs in it we risk only fixing it in one place).

How hard would it be to add a test to appveyor (or to add a test and move the wintests to azure)?

try:
path = Path(directory, direc).resolve()
path = Path(value).resolve()
Copy link
Contributor

Choose a reason for hiding this comment

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

I think this should still prepend a sensible base direc (which gets dropped anyways if value is absolute) or perhaps better just skip values that are not absolute to start with; otherwise non-absolute values get resolved relative to cwd which is certainly not correct.

directory = win32FontDirectory()
Parameters
----------
reg_domain : `int`
Copy link
Contributor

Choose a reason for hiding this comment

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

builtin types should not be backquoted in this position


fontext = ['.' + ext for ext in get_fontext_synonyms(fontext)]
base_dir : `string`
Copy link
Contributor

Choose a reason for hiding this comment

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

str

@JanSellner
Copy link
Author

Thanks @tacaswell and @anntzer for your reviews.

I also considered the duplication of the try blocks not optimal but since pathlib.Path does not modify absolute paths, this gets easier. I updated the code which now makes use of a helper function.

In case of the user fonts, I used the user directories in MSUserFontDirectories. When Microsoft decides to store only the font filename in the registry at one day, we are already prepared :P.

Regarding the tests, I cannot say too much about it. If a test can run in a Windows 1809 image with some user fonts installed, it should be easy to compare the result of findSystemFonts() with some ground truth. But you know more about your test infrastructure. However, since there is currently no test for this in the font manager, this is probably best done in a separate issue.

One final question: I guess it is a good idea to increase the version number since the behaviour changed and new fonts may now be found. Should I do this or do you match this number with the version of matplotlib somehow?

@anntzer
Copy link
Contributor

anntzer commented Dec 10, 2018

You can set the version to e.g. '310', it doesn't have to match exactly the matplotlib version but something approaching it makes sense.
When does the registry get updated? e.g. if one just dumps a font in the user folder, does that immediately update the registry? If yes you could possibly write a test only-to-be-run-on-appveyor/azure, that copies matplotlib's shipped dejavusans to the user fonts directory, and check that it does indeed get picked up.

@JanSellner
Copy link
Author

Just tested, the registry is not updated when a file is moved to the user folder. It has to be installed.

However, the test idea may still work since the font should also be detected if it is only stored in the folder and not in the registry (via fontpaths update iteration). I will look into it. Is there by any chance an existing test which does something similar you can point me to as a reference (e.g. to set it to run only on appveyor)?

@anntzer
Copy link
Contributor

anntzer commented Dec 10, 2018

You can lookup os.environ.get("APPVEYOR", False).

def test_user_fonts():
if not os.environ.get('APPVEYOR', False):
pytest.xfail('This test does only work on appveyor since user fonts '
'are Windows specific')
Copy link
Contributor

Choose a reason for hiding this comment

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

Not only that, but you don't even want to run it on a dev's Windows computer to not overwrite their fonts directory.

Copy link
Author

Choose a reason for hiding this comment

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

Yes, that it true. I agree that respecting the user directories of other developers is a thing to keep in mind. Do you want me to change the message or was this only a mere side note?^^

Copy link
Contributor

Choose a reason for hiding this comment

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

Please do change the note too.

@tacaswell
Copy link
Member

Thanks @Milania1 !

'afm'.
"""
import winreg
def win32RegistryFonts(reg_domain, base_dir):
Copy link
Contributor

Choose a reason for hiding this comment

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

Please make this function private (underscore-prefixed).

@anntzer
Copy link
Contributor

anntzer commented Jan 13, 2019

Also needs a rebase.

@JanSellner
Copy link
Author

Ok, I applied the changes and rebased. Hopefully, everything is correct :-)

@anntzer
Copy link
Contributor

anntzer commented Jan 13, 2019

It's win32RegistryFonts which should be private, not win32InstalledFonts (which should stay public to avoid API breakage...) :)

and make _win32RegistryFonts private
@tacaswell
Copy link
Member

@Milania1 I took the liberty of pushing a commit that flips which function is private, hope you do not mind!

Copy link
Contributor

@anntzer anntzer left a comment

Choose a reason for hiding this comment

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

anyone can merge after ci

@anntzer anntzer merged commit a598811 into matplotlib:master Jan 13, 2019
@tacaswell
Copy link
Member

Thanks @Milania1 ! Congratulations on your first contribution to matplotlib 🎉 Hopefully we will hear from you again!

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

Successfully merging this pull request may close these issues.

Fonts installed in the user directory are not detected (Windows 1809)
3 participants