From fd14c743c1a3ea7a978ee46a09398798c0d0f246 Mon Sep 17 00:00:00 2001 From: "Peter A. Bigot" Date: Mon, 15 Jul 2019 16:55:13 -0500 Subject: [PATCH] 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 --- scripts/gen_cfb_font_header.py | 75 ++++++++++++++++++++++++++-------- 1 file changed, 59 insertions(+), 16 deletions(-) diff --git a/scripts/gen_cfb_font_header.py b/scripts/gen_cfb_font_header.py index 4e743ec48ef..bbe00188be3 100755 --- a/scripts/gen_cfb_font_header.py +++ b/scripts/gen_cfb_font_header.py @@ -30,18 +30,42 @@ def generate_element(image, charcode): 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") - for octet in range(0, int(height / 8)): - value = "" - for bit in range(0, 8): - row = octet * 8 + bit - if pixels[col, row]: - value = "0" + value - else: - value = "1" + value + bits = [] + for value in packed: + bits.append(value) + if not args.msb_first: + value = value[::-1] 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") def extract_font_glyphs(): @@ -58,9 +82,13 @@ def extract_font_glyphs(): if fh > fh_max: fh_max = fh - # Round the vtiled length up to pack into bytes. - width = fw_max - height = 8 * int((fh_max + args.y_offset + 7) / 8) + # Round the packed length up to pack into bytes. + if args.hpack: + 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 if width != args.width: @@ -95,6 +123,15 @@ def extract_image_glyphs(): def generate_header(): """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 = [] for arg in sys.argv: 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}, {width}, {height}, - CFB_FONT_MONO_VPACKED, + {caps}, cfb_font_{name}_{width}{height}, {first}, {last} ); """ .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(): """Parse arguments""" @@ -191,7 +228,7 @@ def parse_args(): "-x", "--width", required=True, type=int, help="width of the CFB font elements in pixels") 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") group.add_argument( "-n", "--name", default="custom", @@ -208,6 +245,12 @@ def parse_args(): group.add_argument( "--y-offset", type=int, default=0, 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()