kopia lustrzana https://github.com/peterhinch/micropython-font-to-py
fix test program
rodzic
e1005e56a1
commit
a874f617d2
40
font_test.py
40
font_test.py
|
@ -1,5 +1,4 @@
|
||||||
#! /usr/bin/env python3
|
#! /usr/bin/env python3
|
||||||
# -*- coding: utf-8 -*-
|
|
||||||
|
|
||||||
# The MIT License (MIT)
|
# The MIT License (MIT)
|
||||||
#
|
#
|
||||||
|
@ -28,21 +27,22 @@
|
||||||
# output to the REPL using a font file created by this utility.
|
# output to the REPL using a font file created by this utility.
|
||||||
|
|
||||||
import sys
|
import sys
|
||||||
import os
|
|
||||||
from importlib import import_module
|
from importlib import import_module
|
||||||
from font_to_py import Font, write_font
|
|
||||||
|
# from font_to_py import Font, write_font
|
||||||
|
|
||||||
# Utility functions
|
# Utility functions
|
||||||
|
|
||||||
|
|
||||||
def validate_hmap(data, height, width):
|
def validate_hmap(data, height, width):
|
||||||
bpr = (width - 1) // 8 + 1
|
bpr = (width - 1) // 8 + 1
|
||||||
msg = 'Horizontal map, invalid data length got {} expected {}'
|
msg = "Horizontal map, invalid data length got {} expected {}"
|
||||||
assert len(data) == bpr * height, msg.format(len(data), bpr * height)
|
assert len(data) == bpr * height, msg.format(len(data), bpr * height)
|
||||||
|
|
||||||
|
|
||||||
def validate_vmap(data, height, width):
|
def validate_vmap(data, height, width):
|
||||||
bpc = (height - 1) // 8 + 1
|
bpc = (height - 1) // 8 + 1
|
||||||
msg = 'Vertical map, invalid data length got {} expected {}'
|
msg = "Vertical map, invalid data length got {} expected {}"
|
||||||
assert len(data) == bpc * width, msg.format(len(data), bpc * width)
|
assert len(data) == bpc * width, msg.format(len(data), bpc * width)
|
||||||
|
|
||||||
|
|
||||||
|
@ -56,8 +56,8 @@ def render_row_hmap(data, row, height, width, reverse):
|
||||||
bit = (byte & (1 << (col % 8))) > 0
|
bit = (byte & (1 << (col % 8))) > 0
|
||||||
else:
|
else:
|
||||||
bit = (byte & (1 << (7 - (col % 8)))) > 0
|
bit = (byte & (1 << (7 - (col % 8)))) > 0
|
||||||
char = '#' if bit else '.'
|
char = "#" if bit else "."
|
||||||
print(char, end='')
|
print(char, end="")
|
||||||
|
|
||||||
|
|
||||||
def render_row_vmap(data, row, height, width, reverse):
|
def render_row_vmap(data, row, height, width, reverse):
|
||||||
|
@ -69,23 +69,26 @@ def render_row_vmap(data, row, height, width, reverse):
|
||||||
bit = (byte & (1 << (7 - (row % 8)))) > 0
|
bit = (byte & (1 << (7 - (row % 8)))) > 0
|
||||||
else:
|
else:
|
||||||
bit = (byte & (1 << (row % 8))) > 0
|
bit = (byte & (1 << (row % 8))) > 0
|
||||||
char = '#' if bit else '.'
|
char = "#" if bit else "."
|
||||||
print(char, end='')
|
print(char, end="")
|
||||||
|
|
||||||
|
|
||||||
# Render a string to REPL using a specified Python font file
|
# Render a string to REPL using a specified Python font file
|
||||||
# usage font_test.test_font('freeserif', 'abc')
|
# usage font_test.test_font('freeserif', 'abc')
|
||||||
# Default tests outliers with fonts created with -k extended
|
# Default tests outliers with fonts created with -k extended
|
||||||
def test_font(fontfile, string='abg'+chr(126)+chr(127)+chr(176)+chr(177)+chr(937)+chr(981)):
|
def test_font(
|
||||||
|
fontfile,
|
||||||
|
string="abg" + chr(126) + chr(127) + chr(176) + chr(177) + chr(937) + chr(981),
|
||||||
|
):
|
||||||
if fontfile in sys.modules:
|
if fontfile in sys.modules:
|
||||||
del sys.modules[fontfile] # force reload
|
del sys.modules[fontfile] # force reload
|
||||||
myfont = import_module(fontfile)
|
myfont = import_module(fontfile)
|
||||||
print(('Horizontal' if myfont.hmap() else 'Vertical') + ' map')
|
print(("Horizontal" if myfont.hmap() else "Vertical") + " map")
|
||||||
print(('Reverse' if myfont.reverse() else 'Normal') + ' bit order')
|
print(("Reverse" if myfont.reverse() else "Normal") + " bit order")
|
||||||
print(('Fixed' if myfont.monospaced() else 'Proportional') + ' spacing')
|
print(("Fixed" if myfont.monospaced() else "Proportional") + " spacing")
|
||||||
print('Dimensions height*max_width {} * {}'.format(myfont.height(), myfont.max_width()))
|
print(f"Dimensions height*max_width {myfont.height()} * {myfont.max_width()}")
|
||||||
s, e = myfont.min_ch(), myfont.max_ch()
|
s, e = myfont.min_ch(), myfont.max_ch()
|
||||||
print('Start char "{}" (ord {}) end char "{}" (ord {})'.format(chr(s), s, chr(e), e))
|
print(f'Start char "{chr(s)}" (ord {s}) end char "{chr(e)}" (ord {e})')
|
||||||
|
|
||||||
height = myfont.height()
|
height = myfont.height()
|
||||||
for row in range(height):
|
for row in range(height):
|
||||||
|
@ -97,7 +100,8 @@ def test_font(fontfile, string='abg'+chr(126)+chr(127)+chr(176)+chr(177)+chr(937
|
||||||
render_row_vmap(data, row, height, width, myfont.reverse())
|
render_row_vmap(data, row, height, width, myfont.reverse())
|
||||||
print()
|
print()
|
||||||
|
|
||||||
usage = '''Usage:
|
|
||||||
|
usage = """Usage:
|
||||||
./font_test fontfile string
|
./font_test fontfile string
|
||||||
fontfile is a Python font file name with the .py extension omitted.
|
fontfile is a Python font file name with the .py extension omitted.
|
||||||
string is an optional string to render.
|
string is an optional string to render.
|
||||||
|
@ -105,10 +109,10 @@ If string is omitted a challenging test string will be rendered. This
|
||||||
is for fonts created with -k extended. Other fonts will show "?" for
|
is for fonts created with -k extended. Other fonts will show "?" for
|
||||||
nonexistent glyphs.
|
nonexistent glyphs.
|
||||||
Requires Python 3.2 or newer.
|
Requires Python 3.2 or newer.
|
||||||
'''
|
"""
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
if len(sys.argv) < 2 or sys.argv[1] == '--help':
|
if len(sys.argv) < 2 or sys.argv[1] == "--help":
|
||||||
print(usage)
|
print(usage)
|
||||||
elif len(sys.argv) == 2:
|
elif len(sys.argv) == 2:
|
||||||
test_font(sys.argv[1])
|
test_font(sys.argv[1])
|
||||||
|
|
Ładowanie…
Reference in New Issue