diff options
author | Pablo MartÃnez <[email protected]> | 2024-03-10 01:29:09 +0100 |
---|---|---|
committer | GitHub <[email protected]> | 2024-03-10 00:29:09 +0000 |
commit | c5225ab5009476c60a9cb27837615d4f29c9b19a (patch) | |
tree | 0dc68687abe26995d3a655e83c34f703885ffebf /lib/python/qmk/cli | |
parent | 729520f302e905e2b3e6711b1fc6d0061843e355 (diff) | |
download | qmk_firmware-c5225ab5009476c60a9cb27837615d4f29c9b19a.tar.gz qmk_firmware-c5225ab5009476c60a9cb27837615d4f29c9b19a.zip |
[Feature] Some metadata on QGF/QFF files (#20101)
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r-- | lib/python/qmk/cli/painter/convert_graphics.py | 34 | ||||
-rw-r--r-- | lib/python/qmk/cli/painter/make_font.py | 36 |
2 files changed, 21 insertions, 49 deletions
diff --git a/lib/python/qmk/cli/painter/convert_graphics.py b/lib/python/qmk/cli/painter/convert_graphics.py index 2519c49b25..553c26aa5d 100644 --- a/lib/python/qmk/cli/painter/convert_graphics.py +++ b/lib/python/qmk/cli/painter/convert_graphics.py @@ -1,10 +1,8 @@ """This script tests QGF functionality. """ -import re -import datetime from io import BytesIO from qmk.path import normpath -from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats +from qmk.painter import generate_subs, render_header, render_source, valid_formats from milc import cli from PIL import Image @@ -12,7 +10,7 @@ from PIL import Image @cli.argument('-v', '--verbose', arg_only=True, action='store_true', help='Turns on verbose output.') @cli.argument('-i', '--input', required=True, help='Specify input graphic file.') @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.') [email protected]('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys()))) [email protected]('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}') @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disables the use of RLE when encoding images.') @cli.argument('-d', '--no-deltas', arg_only=True, action='store_true', help='Disables the use of delta frames when encoding animations.') @cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QGF file as raw data instead of c/h combo.') @@ -51,43 +49,31 @@ def painter_convert_graphics(cli): # Convert the image to QGF using PIL out_data = BytesIO() - input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose) + metadata = [] + input_img.save(out_data, "QGF", use_deltas=(not cli.args.no_deltas), use_rle=(not cli.args.no_rle), qmk_format=format, verbose=cli.args.verbose, metadata=metadata) out_bytes = out_data.getvalue() if cli.args.raw: - raw_file = cli.args.output / (cli.args.input.stem + ".qgf") + raw_file = cli.args.output / f"{cli.args.input.stem}.qgf" with open(raw_file, 'wb') as raw: raw.write(out_bytes) return # Work out the text substitutions for rendering the output data - subs = { - 'generated_type': 'image', - 'var_prefix': 'gfx', - 'generator_command': f'qmk painter-convert-graphics -i {cli.args.input.name} -f {cli.args.format}', - 'year': datetime.date.today().strftime("%Y"), - 'input_file': cli.args.input.name, - 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem), - 'byte_count': len(out_bytes), - 'bytes_lines': render_bytes(out_bytes), - 'format': cli.args.format, - } - - # Render the license - subs.update({'license': render_license(subs)}) + args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "format", "no-rle", "no-deltas"])) + command = f"qmk painter-convert-graphics {args_str}" + subs = generate_subs(cli, out_bytes, image_metadata=metadata, command=command) # Render and write the header file header_text = render_header(subs) - header_file = cli.args.output / (cli.args.input.stem + ".qgf.h") + header_file = cli.args.output / f"{cli.args.input.stem}.qgf.h" with open(header_file, 'w') as header: print(f"Writing {header_file}...") header.write(header_text) - header.close() # Render and write the source file source_text = render_source(subs) - source_file = cli.args.output / (cli.args.input.stem + ".qgf.c") + source_file = cli.args.output / f"{cli.args.input.stem}.qgf.c" with open(source_file, 'w') as source: print(f"Writing {source_file}...") source.write(source_text) - source.close() diff --git a/lib/python/qmk/cli/painter/make_font.py b/lib/python/qmk/cli/painter/make_font.py index c0189920d2..19db844931 100644 --- a/lib/python/qmk/cli/painter/make_font.py +++ b/lib/python/qmk/cli/painter/make_font.py @@ -1,12 +1,10 @@ """This script automates the conversion of font files into a format QMK firmware understands. """ -import re -import datetime from io import BytesIO from qmk.path import normpath -from qmk.painter_qff import QFFFont -from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats +from qmk.painter_qff import _generate_font_glyphs_list, QFFFont +from qmk.painter import generate_subs, render_header, render_source, valid_formats from milc import cli @@ -31,7 +29,7 @@ def painter_make_font_image(cli): @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.') @cli.argument('-n', '--no-ascii', arg_only=True, action='store_true', help='Disables output of the full ASCII character set (0x20..0x7E), exporting only the glyphs specified.') @cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.') [email protected]('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys()))) [email protected]('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}') @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disable the use of RLE to minimise converted image size.') @cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QFF file as raw data instead of c/h combo.') @cli.subcommand('Converts an input font image to something QMK firmware understands') @@ -53,43 +51,31 @@ def painter_convert_font_image(cli): # Render out the data out_data = BytesIO() - font.save_to_qff(format, (False if cli.args.no_rle else True), out_data) + font.save_to_qff(format, not cli.args.no_rle, out_data) out_bytes = out_data.getvalue() if cli.args.raw: - raw_file = cli.args.output / (cli.args.input.stem + ".qff") + raw_file = cli.args.output / f"{cli.args.input.stem}.qff" with open(raw_file, 'wb') as raw: raw.write(out_bytes) return # Work out the text substitutions for rendering the output data - subs = { - 'generated_type': 'font', - 'var_prefix': 'font', - 'generator_command': f'qmk painter-convert-font-image -i {cli.args.input.name} -f {cli.args.format}', - 'year': datetime.date.today().strftime("%Y"), - 'input_file': cli.args.input.name, - 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem), - 'byte_count': len(out_bytes), - 'bytes_lines': render_bytes(out_bytes), - 'format': cli.args.format, - } - - # Render the license - subs.update({'license': render_license(subs)}) + args_str = " ".join((f"--{arg} {getattr(cli.args, arg.replace('-', '_'))}" for arg in ["input", "output", "no-ascii", "unicode-glyphs", "format", "no-rle"])) + command = f"qmk painter-convert-font-image {args_str}" + metadata = {"glyphs": _generate_font_glyphs_list(not cli.args.no_ascii, cli.args.unicode_glyphs)} + subs = generate_subs(cli, out_bytes, font_metadata=metadata, command=command) # Render and write the header file header_text = render_header(subs) - header_file = cli.args.output / (cli.args.input.stem + ".qff.h") + header_file = cli.args.output / f"{cli.args.input.stem}.qff.h" with open(header_file, 'w') as header: print(f"Writing {header_file}...") header.write(header_text) - header.close() # Render and write the source file source_text = render_source(subs) - source_file = cli.args.output / (cli.args.input.stem + ".qff.c") + source_file = cli.args.output / f"{cli.args.input.stem}.qff.c" with open(source_file, 'w') as source: print(f"Writing {source_file}...") source.write(source_text) - source.close() |