diff --git a/README.md b/README.md index c29e749..65d7f0a 100644 --- a/README.md +++ b/README.md @@ -105,6 +105,10 @@ The font file can be a ZIP file of lots of fonts or just a single font. Depending on how you installed pyfiglet, you may find that you need root access to install the font - e.g. `sudo pyfiglet -L `. +On Unix-like systems, pyfiglet also looks for fonts installed by FIGlet in +`/usr/local/share/figlet` and `/usr/share/figlet`, so system fonts can be used +by name without installing them separately into pyfiglet. + ## **Author** All of the documentation and the majority of the work done was by diff --git a/pyfiglet/__init__.py b/pyfiglet/__init__.py index 545d9d9..51083dd 100755 --- a/pyfiglet/__init__.py +++ b/pyfiglet/__init__.py @@ -60,8 +60,14 @@ if sys.platform == 'win32': SHARED_DIRECTORY = os.path.join(os.environ["APPDATA"], "pyfiglet") + FONT_DIRECTORIES = (SHARED_DIRECTORY,) else: SHARED_DIRECTORY = '/usr/local/share/pyfiglet/' + FONT_DIRECTORIES = ( + SHARED_DIRECTORY, + '/usr/local/share/figlet', + '/usr/share/figlet', + ) def figlet_format(text:str, font:str=DEFAULT_FONT, **kwargs:Any): @@ -144,7 +150,7 @@ def preloadFont(cls, font:str): font_path = path break else: - for location in ("./", SHARED_DIRECTORY): + for location in ("./",) + FONT_DIRECTORIES: full_name = os.path.join(location, fn) if os.path.isfile(full_name): font_path = pathlib.Path(full_name) @@ -173,13 +179,16 @@ def isValidFont(cls, font:str): if not font.endswith(('.flf', '.tlf')): return False f = None - full_file = os.path.join(SHARED_DIRECTORY, font) if os.path.isfile(font): f = open(font, 'rb') - elif os.path.isfile(full_file): - f = open(full_file, 'rb') else: - f = importlib.resources.files('pyfiglet.fonts').joinpath(font).open('rb') + for location in FONT_DIRECTORIES: + full_file = os.path.join(location, font) + if os.path.isfile(full_file): + f = open(full_file, 'rb') + break + else: + f = importlib.resources.files('pyfiglet.fonts').joinpath(font).open('rb') if zipfile.is_zipfile(f): # If we have a match, the ZIP file spec says we should just read the first file in the ZIP. @@ -198,8 +207,9 @@ def isValidFont(cls, font:str): @classmethod def getFonts(cls): all_files = importlib.resources.files('pyfiglet.fonts').iterdir() - if os.path.isdir(SHARED_DIRECTORY): - all_files = itertools.chain(all_files, pathlib.Path(SHARED_DIRECTORY).iterdir()) + for location in FONT_DIRECTORIES: + if os.path.isdir(location): + all_files = itertools.chain(all_files, pathlib.Path(location).iterdir()) return [font.name.split('.', 2)[0] for font in all_files if font.is_file() and cls.isValidFont(font.name)] diff --git a/pyfiglet/tests/test_cli.py b/pyfiglet/tests/test_cli.py index b79e8d0..155d233 100644 --- a/pyfiglet/tests/test_cli.py +++ b/pyfiglet/tests/test_cli.py @@ -3,6 +3,8 @@ import pytest +import pyfiglet + @pytest.fixture def test_font_dir(): @@ -45,6 +47,14 @@ def test_strip_strange_font(test_font_dir): assert result.returncode == 0 +def test_font_directories_are_searched(test_font_dir, monkeypatch): + monkeypatch.setattr(pyfiglet, "FONT_DIRECTORIES", (test_font_dir,)) + + assert "TEST_ONLY" in pyfiglet.FigletFont.getFonts() + rendered = pyfiglet.Figlet(font="TEST_ONLY").renderText("0") + assert "0000000000" in rendered + + # normalize is just strip with padding def test_normalize(): command = "pyfiglet -f slant -n 0"