scripts: gen_cfb_font_header: extend to additional representations

The default vertical tiling is designed for displays that are rotated 90
or 270 degrees from normal orientation.  Devices where pixel data
advances first horizontally then vertically requires horizontally-tiled
data.

Similarly when generating upright text on a row-major-order monochrome
display the most significant bit may encode the pixel at the lowest
horizontal position.

Add options to control horizontal vs vertical tiling, and msb vs lsb
pixel order.

The commit also generates the representation of the glyph in comments at
each row.

Signed-off-by: Peter A. Bigot <pab@pabigot.com>
This commit is contained in:
Peter A. Bigot 2019-07-15 16:55:13 -05:00 committed by Ioannis Glaropoulos
commit fd14c743c1

View file

@ -30,18 +30,42 @@ def generate_element(image, charcode):
args.output.write("""\t/* {:d}{} */\n\t{{\n""".format(charcode, char)) args.output.write("""\t/* {:d}{} */\n\t{{\n""".format(charcode, char))
for col in range(0, width): glyph = []
if args.hpack:
for row in range(0, height):
packed = []
for octet in range(0, int(width / 8)):
value = ""
for bit in range(0, 8):
col = octet * 8 + bit
if pixels[col, row]:
value = value + "0"
else:
value = value + "1"
packed.append(value)
glyph.append(packed)
else:
for col in range(0, width):
packed = []
for octet in range(0, int(height / 8)):
value = ""
for bit in range(0, 8):
row = octet * 8 + bit
if pixels[col, row]:
value = value + "0"
else:
value = value + "1"
packed.append(value)
glyph.append(packed)
for packed in glyph:
args.output.write("\t\t") args.output.write("\t\t")
for octet in range(0, int(height / 8)): bits = []
value = "" for value in packed:
for bit in range(0, 8): bits.append(value)
row = octet * 8 + bit if not args.msb_first:
if pixels[col, row]: value = value[::-1]
value = "0" + value
else:
value = "1" + value
args.output.write("0x{:02x},".format(int(value, 2))) args.output.write("0x{:02x},".format(int(value, 2)))
args.output.write("\n") args.output.write(" /* {} */\n".format(''.join(bits).replace('0', ' ').replace('1', '#')))
args.output.write("\t},\n") args.output.write("\t},\n")
def extract_font_glyphs(): def extract_font_glyphs():
@ -58,9 +82,13 @@ def extract_font_glyphs():
if fh > fh_max: if fh > fh_max:
fh_max = fh fh_max = fh
# Round the vtiled length up to pack into bytes. # Round the packed length up to pack into bytes.
width = fw_max if args.hpack:
height = 8 * int((fh_max + args.y_offset + 7) / 8) width = 8 * int((fw_max + 7) / 8)
height = fh_max + args.y_offset
else:
width = fw_max
height = 8 * int((fh_max + args.y_offset + 7) / 8)
# Diagnose inconsistencies with arguments # Diagnose inconsistencies with arguments
if width != args.width: if width != args.width:
@ -95,6 +123,15 @@ def extract_image_glyphs():
def generate_header(): def generate_header():
"""Generate CFB font header file""" """Generate CFB font header file"""
caps = []
if args.hpack:
caps.append('MONO_HPACKED')
else:
caps.append('MONO_VPACKED')
if args.msb_first:
caps.append('MSB_FIRST')
caps = ' | '.join(['CFB_FONT_' + f for f in caps])
clean_cmd = [] clean_cmd = []
for arg in sys.argv: for arg in sys.argv:
if arg.startswith("--bindir"): if arg.startswith("--bindir"):
@ -144,13 +181,13 @@ static const u8_t cfb_font_{name:s}_{width:d}{height:d}[{elem:d}][{b:.0f}] = {{\
FONT_ENTRY_DEFINE({name}_{width}{height}, FONT_ENTRY_DEFINE({name}_{width}{height},
{width}, {width},
{height}, {height},
CFB_FONT_MONO_VPACKED, {caps},
cfb_font_{name}_{width}{height}, cfb_font_{name}_{width}{height},
{first}, {first},
{last} {last}
); );
""" .format(name=args.name, width=args.width, height=args.height, """ .format(name=args.name, width=args.width, height=args.height,
first=args.first, last=args.last)) caps=caps, first=args.first, last=args.last))
def parse_args(): def parse_args():
"""Parse arguments""" """Parse arguments"""
@ -191,7 +228,7 @@ def parse_args():
"-x", "--width", required=True, type=int, "-x", "--width", required=True, type=int,
help="width of the CFB font elements in pixels") help="width of the CFB font elements in pixels")
group.add_argument( group.add_argument(
"-y", "--height", required=True, type=int, choices=range(8, 128, 8), "-y", "--height", required=True, type=int,
help="height of the CFB font elements in pixels") help="height of the CFB font elements in pixels")
group.add_argument( group.add_argument(
"-n", "--name", default="custom", "-n", "--name", default="custom",
@ -208,6 +245,12 @@ def parse_args():
group.add_argument( group.add_argument(
"--y-offset", type=int, default=0, "--y-offset", type=int, default=0,
help="vertical offset for character glyphs (default: %(default)s)") help="vertical offset for character glyphs (default: %(default)s)")
group.add_argument(
"--hpack", dest='hpack', default=False, action='store_true',
help="generate bytes encoding row data rather than column data (default: %(default)s)")
group.add_argument(
"--msb-first", action='store_true',
help="packed content starts at high bit of each byte (default: lsb-first)")
args = parser.parse_args() args = parser.parse_args()