diff options
author | Nick Brassel <nick@tzarc.org> | 2022-04-13 18:00:18 +1000 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-04-13 18:00:18 +1000 |
commit | 1f2b1dedccdf21b629c45ece80b4ca32f6653296 (patch) | |
tree | a4283b928fe11c6662be10067314531f12774152 /lib/python/qmk/cli | |
parent | 1dbbd2b6b068b9f921ebc0341c890df16a491007 (diff) |
Quantum Painter (#10174)
* Install dependencies before executing unit tests.
* Split out UTF-8 decoder.
* Fixup python formatting rules.
* Add documentation for QGF/QFF and the RLE format used.
* Add CLI commands for converting images and fonts.
* Add stub rules.mk for QP.
* Add stream type.
* Add base driver and comms interfaces.
* Add support for SPI, SPI+D/C comms drivers.
* Include <qp.h> when enabled.
* Add base support for SPI+D/C+RST panels, as well as concrete implementation of ST7789.
* Add support for GC9A01.
* Add support for ILI9341.
* Add support for ILI9163.
* Add support for SSD1351.
* Implement qp_setpixel, including pixdata buffer management.
* Implement qp_line.
* Implement qp_rect.
* Implement qp_circle.
* Implement qp_ellipse.
* Implement palette interpolation.
* Allow for streams to work with either flash or RAM.
* Image loading.
* Font loading.
* QGF palette loading.
* Progressive decoder of pixel data supporting Raw+RLE, 1-,2-,4-,8-bpp monochrome and palette-based images.
* Image drawing.
* Animations.
* Font rendering.
* Check against 256 colours, dump out the loaded palette if debugging enabled.
* Fix build.
* AVR is not the intended audience.
* `qmk format-c`
* Generation fix.
* First batch of docs.
* More docs and examples.
* Review comments.
* Public API documentation.
Diffstat (limited to 'lib/python/qmk/cli')
-rw-r--r-- | lib/python/qmk/cli/__init__.py | 4 | ||||
-rw-r--r-- | lib/python/qmk/cli/painter/__init__.py | 2 | ||||
-rw-r--r-- | lib/python/qmk/cli/painter/convert_graphics.py | 86 | ||||
-rw-r--r-- | lib/python/qmk/cli/painter/make_font.py | 87 |
4 files changed, 178 insertions, 1 deletions
diff --git a/lib/python/qmk/cli/__init__.py b/lib/python/qmk/cli/__init__.py index 5f65e677e5..85baa238a8 100644 --- a/lib/python/qmk/cli/__init__.py +++ b/lib/python/qmk/cli/__init__.py @@ -16,7 +16,8 @@ import_names = { # A mapping of package name to importable name 'pep8-naming': 'pep8ext_naming', 'pyusb': 'usb.core', - 'qmk-dotty-dict': 'dotty_dict' + 'qmk-dotty-dict': 'dotty_dict', + 'pillow': 'PIL' } safe_commands = [ @@ -67,6 +68,7 @@ subcommands = [ 'qmk.cli.multibuild', 'qmk.cli.new.keyboard', 'qmk.cli.new.keymap', + 'qmk.cli.painter', 'qmk.cli.pyformat', 'qmk.cli.pytest', 'qmk.cli.via2json', diff --git a/lib/python/qmk/cli/painter/__init__.py b/lib/python/qmk/cli/painter/__init__.py new file mode 100644 index 0000000000..d1a225346c --- /dev/null +++ b/lib/python/qmk/cli/painter/__init__.py @@ -0,0 +1,2 @@ +from . import convert_graphics +from . import make_font diff --git a/lib/python/qmk/cli/painter/convert_graphics.py b/lib/python/qmk/cli/painter/convert_graphics.py new file mode 100644 index 0000000000..bbc30d26ff --- /dev/null +++ b/lib/python/qmk/cli/painter/convert_graphics.py @@ -0,0 +1,86 @@ +"""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 milc import cli +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.') +@cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.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.subcommand('Converts an input image to something QMK understands') +def painter_convert_graphics(cli): + """Converts an image file to a format that Quantum Painter understands. + + This command uses the `qmk.painter` module to generate a Quantum Painter image defintion from an image. The generated definitions are written to a files next to the input -- `INPUT.c` and `INPUT.h`. + """ + # Work out the input file + if cli.args.input != '-': + cli.args.input = normpath(cli.args.input) + + # Error checking + if not cli.args.input.exists(): + cli.log.error('Input image file does not exist!') + cli.print_usage() + return False + + # Work out the output directory + if len(cli.args.output) == 0: + cli.args.output = cli.args.input.parent + cli.args.output = normpath(cli.args.output) + + # Ensure we have a valid format + if cli.args.format not in valid_formats.keys(): + cli.log.error('Output format %s is invalid. Allowed values: %s' % (cli.args.format, ', '.join(valid_formats.keys()))) + cli.print_usage() + return False + + # Work out the encoding parameters + format = valid_formats[cli.args.format] + + # Load the input image + input_img = Image.open(cli.args.input) + + # 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) + out_bytes = out_data.getvalue() + + # 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)}) + + # Render and write the header file + header_text = render_header(subs) + header_file = cli.args.output / (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") + 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 new file mode 100644 index 0000000000..0762843fd3 --- /dev/null +++ b/lib/python/qmk/cli/painter/make_font.py @@ -0,0 +1,87 @@ +"""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 milc import cli + + +@cli.argument('-f', '--font', required=True, help='Specify input font file.') +@cli.argument('-o', '--output', required=True, help='Specify output image path.') +@cli.argument('-s', '--size', default=12, help='Specify font size. Default 12.') +@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.') +@cli.argument('-a', '--no-aa', arg_only=True, action='store_true', help='Disable anti-aliasing on fonts.') +@cli.subcommand('Converts an input font to something QMK understands') +def painter_make_font_image(cli): + # Create the font object + font = QFFFont(cli) + # Read from the input file + cli.args.font = normpath(cli.args.font) + font.generate_image(cli.args.font, cli.args.size, include_ascii_glyphs=(not cli.args.no_ascii), unicode_glyphs=cli.args.unicode_glyphs, use_aa=(False if cli.args.no_aa else True)) + # Render out the data + font.save_to_image(normpath(cli.args.output)) + + +@cli.argument('-i', '--input', help='Specify input graphic file.') +@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.') +@cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.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.subcommand('Converts an input font image to something QMK firmware understands') +def painter_convert_font_image(cli): + # Work out the format + format = valid_formats[cli.args.format] + + # Create the font object + font = QFFFont(cli.log) + + # Read from the input file + cli.args.input = normpath(cli.args.input) + font.read_from_image(cli.args.input, include_ascii_glyphs=(not cli.args.no_ascii), unicode_glyphs=cli.args.unicode_glyphs) + + # Work out the output directory + if len(cli.args.output) == 0: + cli.args.output = cli.args.input.parent + cli.args.output = normpath(cli.args.output) + + # Render out the data + out_data = BytesIO() + font.save_to_qff(format, (False if cli.args.no_rle else True), out_data) + + # 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': out_data.getbuffer().nbytes, + 'bytes_lines': render_bytes(out_data.getbuffer().tobytes()), + 'format': cli.args.format, + } + + # Render the license + subs.update({'license': render_license(subs)}) + + # Render and write the header file + header_text = render_header(subs) + header_file = cli.args.output / (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") + with open(source_file, 'w') as source: + print(f"Writing {source_file}...") + source.write(source_text) + source.close() |