add arg parser to font_test for testing bitmapped fonts

pull/3/head
Brian Cappello 2017-06-29 14:51:42 -04:00
rodzic e5bdcbe098
commit 3cf50e84c0
1 zmienionych plików z 41 dodań i 16 usunięć

Wyświetl plik

@ -97,15 +97,17 @@ def display(data, hmap, height, width, reverse):
# Basic test of Font class functionality
# Usage font_test.font_test()
def font_test():
fnt = Font('FreeSans.ttf', 20)
for char in 'WM_eg!.,':
charset = 'WM_eg!.,'
fnt = Font('FreeSans.ttf', 20, charset, False)
for char in charset:
fnt[char][0].display()
print(fnt.width)
# Font character streaming
# Usage font_test.test_stream('abc', 20, False, False, False)
def test_stream(string, height, monospaced, hmap, reverse):
fnt = Font("FreeSans.ttf", height, monospaced)
fnt = Font("FreeSans.ttf", height, string, monospaced)
height = fnt.height
for char in string:
width = fnt[char][1]
@ -121,7 +123,7 @@ def chr_addr(index, ordch):
# Font.build_arrays
# Usage font_test.test_arrays('abc', 20, False, False, False)
def test_arrays(string, height, monospaced, hmap, reverse):
fnt = Font("FreeSans.ttf", height, monospaced)
fnt = Font("FreeSans.ttf", height, string, monospaced)
height = fnt.height
data, index = fnt.build_arrays(hmap, reverse)
for char in string:
@ -140,23 +142,18 @@ def test_font(fontfile, string):
del sys.modules[fontfile] # force reload
myfont = import_module(fontfile)
height = myfont.height()
for row in range(height):
for char in string:
data, _, width = myfont.get_char(char)
if myfont.hmap():
render_row_hmap(data, row, height, width, myfont.reverse())
else:
render_row_vmap(data, row, height, width, myfont.reverse())
print()
_display_bitmapped_string(myfont, string)
# Create font file, render a string to REPL using it
# usage font_test.test_file('FreeSans.ttf', 20, 'xyz')
def test_file(fontfile, height, string, *, minchar=32, maxchar=126, defchar=ord('?'),
fixed=False, hmap=False, reverse=False):
fixed=False, hmap=False, lmap=False, reverse=False):
charset = [chr(x) for x in range(minchar, maxchar+1)]
if chr(defchar) not in charset:
charset += chr(defchar)
if not write_font('myfont.py', fontfile, height, fixed,
hmap, reverse, minchar, maxchar, defchar):
hmap, lmap, reverse, charset):
print('Failed to create font file.')
return
@ -164,6 +161,11 @@ def test_file(fontfile, height, string, *, minchar=32, maxchar=126, defchar=ord(
del sys.modules['myfont'] # force reload
import myfont
_display_bitmapped_string(myfont, string)
os.unlink('myfont.py')
def _display_bitmapped_string(myfont, string):
height = myfont.height()
for row in range(height):
for char in string:
@ -173,4 +175,27 @@ def test_file(fontfile, height, string, *, minchar=32, maxchar=126, defchar=ord(
else:
render_row_vmap(data, row, height, width, myfont.reverse())
print()
os.unlink('myfont.py')
if __name__ == '__main__':
import argparse
parser = argparse.ArgumentParser(description='Test font_to_py')
parser.add_argument('-f', '--font', dest='font',
help='Name of a Python font module generated by font_to_py')
parser.add_argument('-F', '--fontfile', dest='fontfile',
help='Path to a TTF font file to test')
parser.add_argument('-s', '--size', dest='size', default=20)
parser.add_argument('test_string', nargs='?', default='ABCD.efghij 123!')
args = parser.parse_args()
if args.fontfile:
if not os.path.exists(args.fontfile):
exit('Sorry, could not find a font file at {}.'.format(args.fontfile))
print('Running test_file with source font {}:'.format(args.fontfile))
test_file(args.fontfile, int(args.size), args.test_string)
elif args.font:
if '.py' in args.font:
args.font = args.font[:-3]
print('Running test_font with py font module {}:'.format(args.font))
test_font(args.font, args.test_string)