Skip to content

[Bug]: FontManager.addfont doesn't accept pathlib.Path of TTF font #22582

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

Closed
twm opened this issue Mar 2, 2022 · 2 comments · Fixed by #22591
Closed

[Bug]: FontManager.addfont doesn't accept pathlib.Path of TTF font #22582

twm opened this issue Mar 2, 2022 · 2 comments · Fixed by #22591
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed topic: text/fonts
Milestone

Comments

@twm
Copy link

twm commented Mar 2, 2022

Bug summary

The FontManager.addfont() method is documented as accepting a path-like, but if a path-like for a .ttf font file is passed it raises TypeError

Code for reproduction

from pathlib import Path
from matplotlib import get_data_path, __version__
from matplotlib.font_manager import fontManager

path = Path(get_data_path()) / "fonts" / "ttf" / "cmr10.ttf"

print("matplotlib", __version__)
fontManager.addfont(str(path))  # Works
print("str ok")
fontManager.addfont(path)  # TypeError
print("path ok")

Actual outcome

matplotlib 3.5.1
str ok
Traceback (most recent call last):
  File "/home/twm/dev/roofs/repro.py", line 10, in <module>
    fontManager.addfont(path)  # TypeError
  File "/home/twm/dev/roofs/venv/lib/python3.9/site-packages/matplotlib/font_manager.py", line 1092, in addfont
    font = ft2font.FT2Font(path)
TypeError: First argument must be a path or binary-mode file object

Expected outcome

matplotlib 3.5.1
str ok
path ok

Additional information

It looks like the simplest fix would be to convert path-likes to str since FT2Font is implemented in C++.

Operating system

Ubuntu 20.04

Matplotlib Version

3.5.1

Matplotlib Backend

TkAgg

Python version

3.9.5

Jupyter version

N/A

Installation

pip

@oscargus
Copy link
Member

oscargus commented Mar 2, 2022

The easy fix seems to be replacing path with str(path) in

font = ft2font.FT2Font(path)

but probably the same thing will happen for an afm-font. Which means that maybe the right thing is to simplify convert path to a string before the if, path = str(path) (and then convert it back to a Path when checking the suffix...). Or change the documentation to clarify what path-like means.

@tacaswell tacaswell added this to the v3.6.0 milestone Mar 2, 2022
@timhoffm
Copy link
Member

timhoffm commented Mar 3, 2022

We should support Path as a parameter to addfont().

Both, FT2Font() and afmFontProperty() should receive a str. You can do either path = str(path) at the top, or str(path) in both calls. Either way seems fine.

@oscargus oscargus added Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed labels Mar 3, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Difficulty: Easy https://matplotlib.org/devdocs/devel/contribute.html#good-first-issues Good first issue Open a pull request against these issues if there are no active ones! status: has patch patch suggested, PR still needed topic: text/fonts
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants