Load FIGlet font format

This commit is contained in:
Isaiah Odhner 2023-05-13 00:56:55 -04:00
parent 8b6a1586ef
commit e1245aa9e9
3 changed files with 23 additions and 28 deletions

View File

@ -29,6 +29,7 @@
"Bresenham's", "Bresenham's",
"cmdpxl", "cmdpxl",
"DIALOGEX", "DIALOGEX",
"Figlet",
"Fira", "Fira",
"fudgedness", "fudgedness",
"getpid", "getpid",

View File

@ -1,6 +1,7 @@
appscript==1.2.2; sys_platform == 'darwin' appscript==1.2.2; sys_platform == 'darwin'
Pillow==9.5.0 Pillow==9.5.0
psutil==5.9.0 psutil==5.9.0
pyfiglet==0.8.post1
# PyGObject==3.42.1 # PyGObject==3.42.1
pyperclip==1.8.2 pyperclip==1.8.2
pyxdg==0.28 pyxdg==0.28

View File

@ -34,6 +34,7 @@ from textual.widgets import Button, Static, Input, Header, RadioSet, RadioButton
from textual.binding import Binding from textual.binding import Binding
from textual.color import Color from textual.color import Color
from PIL import Image, UnidentifiedImageError from PIL import Image, UnidentifiedImageError
from pyfiglet import Figlet, FigletFont
from menus import MenuBar, Menu, MenuItem, Separator from menus import MenuBar, Menu, MenuItem, Separator
from windows import Window, DialogWindow, CharacterSelectorDialogWindow, MessageBox, get_warning_icon, get_question_icon from windows import Window, DialogWindow, CharacterSelectorDialogWindow, MessageBox, get_warning_icon, get_question_icon
@ -241,37 +242,29 @@ class MetaGlyphFont:
self.load() self.load()
def load(self): def load(self):
"""Load the font from the file. """Load the font from the .flf FIGlet font file."""
# fig = Figlet(font=self.file_path) # gives FontNotFound error!
The format is simply the rows of each glyph. There is no more separation between glyphs than between rows of a glyph. # Figlet constructor only supports looking for installed fonts.
TODO: support loading FIGlet fonts # I could install the font, with FigletFont.installFonts,
""" # maybe with some prefixed name, but I don't want to do that.
with open(self.file_path, "r") as f:
i = 0 with open(self.file_path) as f:
glyph: list[str] = [] flf = f.read()
for line in f: fig_font = FigletFont()
if line.startswith("#"): fig_font.data = flf
continue fig_font.loadFont()
# Note that whitespace should be preserved. fig = Figlet()
line = line.rstrip("\n") # fig.setFont(font=fig_font) # nope, that's also looking for a font name
if i % self.height == 0: fig.font = self.file_path # may not be used
glyph = [] fig.Font = fig_font # this feels so wrong
ch_code = i // self.height for char in self.covered_characters:
# this makes more characters work meta_glyph = fig.renderText(char) # type: ignore
# TODO: figure out what's wrong self.glyphs[char] = meta_glyph.split("\n")
# I might've been confused by different character sets when making the font
ch_code = (ch_code - 2) % 256
ch_byte = bytes([ch_code])
ch = ch_byte.decode('cp437')
if ch in self.covered_characters:
self.glyphs[ch] = glyph
glyph.append(line)
i += 1
covered_characters = R""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~""" covered_characters = R""" !"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcdefghijklmnopqrstuvwxyz{|}~"""
meta_glyph_fonts: dict[int, MetaGlyphFont] = { meta_glyph_fonts: dict[int, MetaGlyphFont] = {
2: MetaGlyphFont(os.path.join(os.path.dirname(__file__), "../../NanoTiny_v14_2x2.txt"), 2, 2, covered_characters), 2: MetaGlyphFont(os.path.join(os.path.dirname(__file__), "../../NanoTiny_v14_2x2.flf"), 2, 2, covered_characters),
# 4: MetaGlyphFont(os.path.join(os.path.dirname(__file__), "../../NanoTiny_v14_4x4.txt"), 4, 4, covered_characters), # 4: MetaGlyphFont(os.path.join(os.path.dirname(__file__), "../../NanoTiny_v14_4x4.flf"), 4, 4, covered_characters),
# TODO: less specialized (more practical) fonts for larger sizes # TODO: less specialized (more practical) fonts for larger sizes
} }