kopia lustrzana https://github.com/peterhinch/micropython-font-to-py
add arg parser to font_test for testing bitmapped fonts
rodzic
e5bdcbe098
commit
3cf50e84c0
57
font_test.py
57
font_test.py
|
@ -97,15 +97,17 @@ def display(data, hmap, height, width, reverse):
|
||||||
# Basic test of Font class functionality
|
# Basic test of Font class functionality
|
||||||
# Usage font_test.font_test()
|
# Usage font_test.font_test()
|
||||||
def font_test():
|
def font_test():
|
||||||
fnt = Font('FreeSans.ttf', 20)
|
charset = 'WM_eg!.,'
|
||||||
for char in 'WM_eg!.,':
|
fnt = Font('FreeSans.ttf', 20, charset, False)
|
||||||
|
for char in charset:
|
||||||
fnt[char][0].display()
|
fnt[char][0].display()
|
||||||
print(fnt.width)
|
print(fnt.width)
|
||||||
|
|
||||||
|
|
||||||
# Font character streaming
|
# Font character streaming
|
||||||
# Usage font_test.test_stream('abc', 20, False, False, False)
|
# Usage font_test.test_stream('abc', 20, False, False, False)
|
||||||
def test_stream(string, height, monospaced, hmap, reverse):
|
def test_stream(string, height, monospaced, hmap, reverse):
|
||||||
fnt = Font("FreeSans.ttf", height, monospaced)
|
fnt = Font("FreeSans.ttf", height, string, monospaced)
|
||||||
height = fnt.height
|
height = fnt.height
|
||||||
for char in string:
|
for char in string:
|
||||||
width = fnt[char][1]
|
width = fnt[char][1]
|
||||||
|
@ -121,7 +123,7 @@ def chr_addr(index, ordch):
|
||||||
# Font.build_arrays
|
# Font.build_arrays
|
||||||
# Usage font_test.test_arrays('abc', 20, False, False, False)
|
# Usage font_test.test_arrays('abc', 20, False, False, False)
|
||||||
def test_arrays(string, height, monospaced, hmap, reverse):
|
def test_arrays(string, height, monospaced, hmap, reverse):
|
||||||
fnt = Font("FreeSans.ttf", height, monospaced)
|
fnt = Font("FreeSans.ttf", height, string, monospaced)
|
||||||
height = fnt.height
|
height = fnt.height
|
||||||
data, index = fnt.build_arrays(hmap, reverse)
|
data, index = fnt.build_arrays(hmap, reverse)
|
||||||
for char in string:
|
for char in string:
|
||||||
|
@ -140,23 +142,18 @@ def test_font(fontfile, string):
|
||||||
del sys.modules[fontfile] # force reload
|
del sys.modules[fontfile] # force reload
|
||||||
myfont = import_module(fontfile)
|
myfont = import_module(fontfile)
|
||||||
|
|
||||||
height = myfont.height()
|
_display_bitmapped_string(myfont, string)
|
||||||
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()
|
|
||||||
|
|
||||||
|
|
||||||
# Create font file, render a string to REPL using it
|
# Create font file, render a string to REPL using it
|
||||||
# usage font_test.test_file('FreeSans.ttf', 20, 'xyz')
|
# usage font_test.test_file('FreeSans.ttf', 20, 'xyz')
|
||||||
def test_file(fontfile, height, string, *, minchar=32, maxchar=126, defchar=ord('?'),
|
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,
|
if not write_font('myfont.py', fontfile, height, fixed,
|
||||||
hmap, reverse, minchar, maxchar, defchar):
|
hmap, lmap, reverse, charset):
|
||||||
print('Failed to create font file.')
|
print('Failed to create font file.')
|
||||||
return
|
return
|
||||||
|
|
||||||
|
@ -164,6 +161,11 @@ def test_file(fontfile, height, string, *, minchar=32, maxchar=126, defchar=ord(
|
||||||
del sys.modules['myfont'] # force reload
|
del sys.modules['myfont'] # force reload
|
||||||
import myfont
|
import myfont
|
||||||
|
|
||||||
|
_display_bitmapped_string(myfont, string)
|
||||||
|
os.unlink('myfont.py')
|
||||||
|
|
||||||
|
|
||||||
|
def _display_bitmapped_string(myfont, string):
|
||||||
height = myfont.height()
|
height = myfont.height()
|
||||||
for row in range(height):
|
for row in range(height):
|
||||||
for char in string:
|
for char in string:
|
||||||
|
@ -173,4 +175,27 @@ def test_file(fontfile, height, string, *, minchar=32, maxchar=126, defchar=ord(
|
||||||
else:
|
else:
|
||||||
render_row_vmap(data, row, height, width, myfont.reverse())
|
render_row_vmap(data, row, height, width, myfont.reverse())
|
||||||
print()
|
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)
|
||||||
|
|
Ładowanie…
Reference in New Issue