You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

87 lines
4.1 KiB

  1. """This script automates the conversion of font files into a format QMK firmware understands.
  2. """
  3. import re
  4. import datetime
  5. from io import BytesIO
  6. from qmk.path import normpath
  7. from qmk.painter_qff import QFFFont
  8. from qmk.painter import render_header, render_source, render_license, render_bytes, valid_formats
  9. from milc import cli
  10. @cli.argument('-f', '--font', required=True, help='Specify input font file.')
  11. @cli.argument('-o', '--output', required=True, help='Specify output image path.')
  12. @cli.argument('-s', '--size', default=12, help='Specify font size. Default 12.')
  13. @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.')
  14. @cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.')
  15. @cli.argument('-a', '--no-aa', arg_only=True, action='store_true', help='Disable anti-aliasing on fonts.')
  16. @cli.subcommand('Converts an input font to something QMK understands')
  17. def painter_make_font_image(cli):
  18. # Create the font object
  19. font = QFFFont(cli)
  20. # Read from the input file
  21. cli.args.font = normpath(cli.args.font)
  22. 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))
  23. # Render out the data
  24. font.save_to_image(normpath(cli.args.output))
  25. @cli.argument('-i', '--input', help='Specify input graphic file.')
  26. @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.')
  27. @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.')
  28. @cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.')
  29. @cli.argument('-f', '--format', required=True, help='Output format, valid types: %s' % (', '.join(valid_formats.keys())))
  30. @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disable the use of RLE to minimise converted image size.')
  31. @cli.subcommand('Converts an input font image to something QMK firmware understands')
  32. def painter_convert_font_image(cli):
  33. # Work out the format
  34. format = valid_formats[cli.args.format]
  35. # Create the font object
  36. font = QFFFont(cli.log)
  37. # Read from the input file
  38. cli.args.input = normpath(cli.args.input)
  39. font.read_from_image(cli.args.input, include_ascii_glyphs=(not cli.args.no_ascii), unicode_glyphs=cli.args.unicode_glyphs)
  40. # Work out the output directory
  41. if len(cli.args.output) == 0:
  42. cli.args.output = cli.args.input.parent
  43. cli.args.output = normpath(cli.args.output)
  44. # Render out the data
  45. out_data = BytesIO()
  46. font.save_to_qff(format, (False if cli.args.no_rle else True), out_data)
  47. # Work out the text substitutions for rendering the output data
  48. subs = {
  49. 'generated_type': 'font',
  50. 'var_prefix': 'font',
  51. 'generator_command': f'qmk painter-convert-font-image -i {cli.args.input.name} -f {cli.args.format}',
  52. 'year': datetime.date.today().strftime("%Y"),
  53. 'input_file': cli.args.input.name,
  54. 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem),
  55. 'byte_count': out_data.getbuffer().nbytes,
  56. 'bytes_lines': render_bytes(out_data.getbuffer().tobytes()),
  57. 'format': cli.args.format,
  58. }
  59. # Render the license
  60. subs.update({'license': render_license(subs)})
  61. # Render and write the header file
  62. header_text = render_header(subs)
  63. header_file = cli.args.output / (cli.args.input.stem + ".qff.h")
  64. with open(header_file, 'w') as header:
  65. print(f"Writing {header_file}...")
  66. header.write(header_text)
  67. header.close()
  68. # Render and write the source file
  69. source_text = render_source(subs)
  70. source_file = cli.args.output / (cli.args.input.stem + ".qff.c")
  71. with open(source_file, 'w') as source:
  72. print(f"Writing {source_file}...")
  73. source.write(source_text)
  74. source.close()