micropython-font-to-py/font_test.py

121 wiersze
4.4 KiB
Python
Czysty Zwykły widok Historia

#! /usr/bin/env python3
2016-10-29 10:35:13 +00:00
# The MIT License (MIT)
#
# Copyright (c) 2016 Peter Hinch
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.
# Test programs for the utility font_to_py and for font files created by it.
# The test of most general use is test_font which enables a string to be
# output to the REPL using a font file created by this utility.
import sys
from importlib import import_module
2023-10-25 16:13:23 +00:00
# from font_to_py import Font, write_font
2016-10-29 10:35:13 +00:00
# Utility functions
2023-10-25 16:13:23 +00:00
2016-10-29 10:35:13 +00:00
def validate_hmap(data, height, width):
2023-10-25 16:13:23 +00:00
bpr = (width - 1) // 8 + 1
msg = "Horizontal map, invalid data length got {} expected {}"
2019-09-07 10:55:12 +00:00
assert len(data) == bpr * height, msg.format(len(data), bpr * height)
2016-10-29 10:35:13 +00:00
def validate_vmap(data, height, width):
2023-10-25 16:13:23 +00:00
bpc = (height - 1) // 8 + 1
msg = "Vertical map, invalid data length got {} expected {}"
2019-09-07 10:55:12 +00:00
assert len(data) == bpc * width, msg.format(len(data), bpc * width)
2016-10-29 10:35:13 +00:00
# Routines to render to REPL
def render_row_hmap(data, row, height, width, reverse):
validate_hmap(data, height, width)
2023-10-25 16:13:23 +00:00
bytes_per_row = (width - 1) // 8 + 1
2016-10-29 10:35:13 +00:00
for col in range(width):
byte = data[row * bytes_per_row + col // 8]
if reverse:
bit = (byte & (1 << (col % 8))) > 0
else:
bit = (byte & (1 << (7 - (col % 8)))) > 0
2023-10-25 16:13:23 +00:00
char = "#" if bit else "."
print(char, end="")
2016-10-29 10:35:13 +00:00
def render_row_vmap(data, row, height, width, reverse):
validate_vmap(data, height, width)
2023-10-25 16:13:23 +00:00
bytes_per_col = (height - 1) // 8 + 1
2016-10-29 10:35:13 +00:00
for col in range(width):
2023-10-25 16:13:23 +00:00
byte = data[col * bytes_per_col + row // 8]
2016-10-29 10:35:13 +00:00
if reverse:
bit = (byte & (1 << (7 - (row % 8)))) > 0
else:
bit = (byte & (1 << (row % 8))) > 0
2023-10-25 16:13:23 +00:00
char = "#" if bit else "."
print(char, end="")
2016-10-29 10:35:13 +00:00
# Render a string to REPL using a specified Python font file
# usage font_test.test_font('freeserif', 'abc')
2019-09-07 10:55:12 +00:00
# Default tests outliers with fonts created with -k extended
2023-10-25 16:13:23 +00:00
def test_font(
fontfile,
string="abg" + chr(126) + chr(127) + chr(176) + chr(177) + chr(937) + chr(981),
):
2016-10-29 10:35:13 +00:00
if fontfile in sys.modules:
del sys.modules[fontfile] # force reload
myfont = import_module(fontfile)
2023-10-25 16:13:23 +00:00
print(("Horizontal" if myfont.hmap() else "Vertical") + " map")
print(("Reverse" if myfont.reverse() else "Normal") + " bit order")
print(("Fixed" if myfont.monospaced() else "Proportional") + " spacing")
print(f"Dimensions height*max_width {myfont.height()} * {myfont.max_width()}")
2019-09-07 10:55:12 +00:00
s, e = myfont.min_ch(), myfont.max_ch()
2023-10-25 16:13:23 +00:00
print(f'Start char "{chr(s)}" (ord {s}) end char "{chr(e)}" (ord {e})')
2016-10-29 10:35:13 +00:00
height = myfont.height()
for row in range(height):
for char in string:
2016-11-03 16:57:44 +00:00
data, _, width = myfont.get_ch(char)
2016-10-29 10:35:13 +00:00
if myfont.hmap():
render_row_hmap(data, row, height, width, myfont.reverse())
else:
render_row_vmap(data, row, height, width, myfont.reverse())
print()
2023-10-25 16:13:23 +00:00
usage = """Usage:
2020-02-14 10:57:21 +00:00
./font_test fontfile string
fontfile is a Python font file name with the .py extension omitted.
string is an optional string to render.
If string is omitted a challenging test string will be rendered. This
is for fonts created with -k extended. Other fonts will show "?" for
nonexistent glyphs.
Requires Python 3.2 or newer.
2023-10-25 16:13:23 +00:00
"""
2020-02-14 10:57:21 +00:00
if __name__ == "__main__":
2023-10-25 16:13:23 +00:00
if len(sys.argv) < 2 or sys.argv[1] == "--help":
2020-02-14 10:57:21 +00:00
print(usage)
elif len(sys.argv) == 2:
test_font(sys.argv[1])
else:
test_font(sys.argv[1], sys.argv[2])