make_font.py 4.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  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.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QFF file as raw data instead of c/h combo.')
  32. @cli.subcommand('Converts an input font image to something QMK firmware understands')
  33. def painter_convert_font_image(cli):
  34. # Work out the format
  35. format = valid_formats[cli.args.format]
  36. # Create the font object
  37. font = QFFFont(cli.log)
  38. # Read from the input file
  39. cli.args.input = normpath(cli.args.input)
  40. font.read_from_image(cli.args.input, include_ascii_glyphs=(not cli.args.no_ascii), unicode_glyphs=cli.args.unicode_glyphs)
  41. # Work out the output directory
  42. if len(cli.args.output) == 0:
  43. cli.args.output = cli.args.input.parent
  44. cli.args.output = normpath(cli.args.output)
  45. # Render out the data
  46. out_data = BytesIO()
  47. font.save_to_qff(format, (False if cli.args.no_rle else True), out_data)
  48. out_bytes = out_data.getvalue()
  49. if cli.args.raw:
  50. raw_file = cli.args.output / (cli.args.input.stem + ".qff")
  51. with open(raw_file, 'wb') as raw:
  52. raw.write(out_bytes)
  53. return
  54. # Work out the text substitutions for rendering the output data
  55. subs = {
  56. 'generated_type': 'font',
  57. 'var_prefix': 'font',
  58. 'generator_command': f'qmk painter-convert-font-image -i {cli.args.input.name} -f {cli.args.format}',
  59. 'year': datetime.date.today().strftime("%Y"),
  60. 'input_file': cli.args.input.name,
  61. 'sane_name': re.sub(r"[^a-zA-Z0-9]", "_", cli.args.input.stem),
  62. 'byte_count': len(out_bytes),
  63. 'bytes_lines': render_bytes(out_bytes),
  64. 'format': cli.args.format,
  65. }
  66. # Render the license
  67. subs.update({'license': render_license(subs)})
  68. # Render and write the header file
  69. header_text = render_header(subs)
  70. header_file = cli.args.output / (cli.args.input.stem + ".qff.h")
  71. with open(header_file, 'w') as header:
  72. print(f"Writing {header_file}...")
  73. header.write(header_text)
  74. header.close()
  75. # Render and write the source file
  76. source_text = render_source(subs)
  77. source_file = cli.args.output / (cli.args.input.stem + ".qff.c")
  78. with open(source_file, 'w') as source:
  79. print(f"Writing {source_file}...")
  80. source.write(source_text)
  81. source.close()