Skip to content

trouble loading local fonts #34

@paniterka

Description

@paniterka

Thanks for developing this package! It really simplifies things.
While installing and managing fonts from the external URL works great, I am having some trouble loading local fonts. I get an error unless the path to the font file is preceded with "file:\\". I am on Windows 11, using Jupyter 7 with Nbclassic, Matplotlib 3.10.0, Python 3.12.9, venv created using anaconda and managed using pip, pyfonts 1.1.3. I guess it is related to how urllib works. If this is intended behavior, it would be great to either include this detail in the documentation (here and on https://python-graph-gallery.com/pyfonts/) or to handle it within load_font.

from pyfonts import load_font
font1 = load_font('C:\\Windows\\Fonts\\arial.ttf') # this doesn't work 
font2 = load_font('file:\\C:\\Windows\\Fonts\\arial.ttf') # this works
---------------------------------------------------------------------------
URLError                                  Traceback (most recent call last)
File C:\APPS\Anaconda3\envs\bib2025\Lib\site-packages\pyfonts\main.py:98, in load_font(font_url, use_cache, danger_not_verify_ssl, font_path)
     97 try:
---> 98     response = urlopen(font_url)
     99 except HTTPError as e:

File C:\APPS\Anaconda3\envs\bib2025\Lib\urllib\request.py:215, in urlopen(url, data, timeout, cafile, capath, cadefault, context)
    214     opener = _opener
--> 215 return opener.open(url, data, timeout)

File C:\APPS\Anaconda3\envs\bib2025\Lib\urllib\request.py:515, in OpenerDirector.open(self, fullurl, data, timeout)
    514 sys.audit('urllib.Request', req.full_url, req.data, req.headers, req.get_method())
--> 515 response = self._open(req, data)
    517 # post-process response

File C:\APPS\Anaconda3\envs\bib2025\Lib\urllib\request.py:537, in OpenerDirector._open(self, req, data)
    535     return result
--> 537 return self._call_chain(self.handle_open, 'unknown',
    538                         'unknown_open', req)

File C:\APPS\Anaconda3\envs\bib2025\Lib\urllib\request.py:492, in OpenerDirector._call_chain(self, chain, kind, meth_name, *args)
    491 func = getattr(handler, meth_name)
--> 492 result = func(*args)
    493 if result is not None:

File C:\APPS\Anaconda3\envs\bib2025\Lib\urllib\request.py:1420, in UnknownHandler.unknown_open(self, req)
   1419 type = req.type
-> 1420 raise URLError('unknown url type: %s' % type)

URLError: <urlopen error unknown url type: c>

During handling of the above exception, another exception occurred:

Exception                                 Traceback (most recent call last)
Cell In[10], line 1
----> 1 font1 = load_font('C:\\Windows\\Fonts\\arial.ttf')

File C:\APPS\Anaconda3\envs\bib2025\Lib\site-packages\pyfonts\main.py:125, in load_font(font_url, use_cache, danger_not_verify_ssl, font_path)
    118             raise Exception(
    119                 "SSL certificate verification failed. "
    120                 "If you are behind a firewall or using a proxy, "
    121                 "try setting `danger_not_verify_ssl=True` to bypass verification."
    122                 "verification."
    123             )
    124     else:
--> 125         raise Exception(
    126             "Failed to load font. This may be due to a lack of internet connection "
    127             "or an environment where local files are not accessible (Pyodide, etc)."
    128         )
    130 content = response.read()
    131 with open(cached_fontfile, "wb") as f:

Exception: Failed to load font. This may be due to a lack of internet connection or an environment where local files are not accessible (Pyodide, etc).

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions