Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <font file>`.

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
Expand Down
24 changes: 17 additions & 7 deletions pyfiglet/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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.
Expand All @@ -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)]
Expand Down
10 changes: 10 additions & 0 deletions pyfiglet/tests/test_cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

import pytest

import pyfiglet


@pytest.fixture
def test_font_dir():
Expand Down Expand Up @@ -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"
Expand Down
Loading