Skip to content

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

@twm

Description

@twm

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Difficulty: Easyhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesGood first issueOpen a pull request against these issues if there are no active ones!status: has patchpatch suggested, PR still neededtopic: text/fonts

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions

      pFad - Phonifier reborn

      Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

      Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


      Alternative Proxies:

      Alternative Proxy

      pFad Proxy

      pFad v3 Proxy

      pFad v4 Proxy