logo

qmk_firmware

custom branch of QMK firmware git clone https://anongit.hacktivis.me/git/qmk_firmware.git

make_font.py (4049B)


  1. """This script automates the conversion of font files into a format QMK firmware understands.
  2. """
  3. from io import BytesIO
  4. from qmk.path import normpath
  5. from qmk.painter_qff import _generate_font_glyphs_list, QFFFont
  6. from qmk.painter import generate_subs, render_header, render_source, valid_formats
  7. from milc import cli
  8. @cli.argument('-f', '--font', required=True, help='Specify input font file.')
  9. @cli.argument('-o', '--output', required=True, help='Specify output image path.')
  10. @cli.argument('-s', '--size', default=12, help='Specify font size. Default 12.')
  11. @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.')
  12. @cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.')
  13. @cli.argument('-a', '--no-aa', arg_only=True, action='store_true', help='Disable anti-aliasing on fonts.')
  14. @cli.subcommand('Converts an input font to something QMK understands')
  15. def painter_make_font_image(cli):
  16. # Create the font object
  17. font = QFFFont(cli)
  18. # Read from the input file
  19. cli.args.font = normpath(cli.args.font)
  20. 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))
  21. # Render out the data
  22. font.save_to_image(normpath(cli.args.output))
  23. @cli.argument('-i', '--input', help='Specify input graphic file.')
  24. @cli.argument('-o', '--output', default='', help='Specify output directory. Defaults to same directory as input.')
  25. @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.')
  26. @cli.argument('-u', '--unicode-glyphs', default='', help='Also generate the specified unicode glyphs.')
  27. @cli.argument('-f', '--format', required=True, help=f'Output format, valid types: {", ".join(valid_formats.keys())}')
  28. @cli.argument('-r', '--no-rle', arg_only=True, action='store_true', help='Disable the use of RLE to minimise converted image size.')
  29. @cli.argument('-w', '--raw', arg_only=True, action='store_true', help='Writes out the QFF file as raw data instead of c/h combo.')
  30. @cli.subcommand('Converts an input font image to something QMK firmware understands')
  31. def painter_convert_font_image(cli):
  32. # Work out the format
  33. format = valid_formats[cli.args.format]
  34. # Create the font object
  35. font = QFFFont(cli.log)
  36. # Read from the input file
  37. cli.args.input = normpath(cli.args.input)
  38. font.read_from_image(cli.args.input, include_ascii_glyphs=(not cli.args.no_ascii), unicode_glyphs=cli.args.unicode_glyphs)
  39. # Work out the output directory
  40. if len(cli.args.output) == 0:
  41. cli.args.output = cli.args.input.parent
  42. cli.args.output = normpath(cli.args.output)
  43. # Render out the data
  44. out_data = BytesIO()
  45. font.save_to_qff(format, not cli.args.no_rle, out_data)
  46. out_bytes = out_data.getvalue()
  47. if cli.args.raw:
  48. raw_file = cli.args.output / f"{cli.args.input.stem}.qff"
  49. with open(raw_file, 'wb') as raw:
  50. raw.write(out_bytes)
  51. return
  52. # Work out the text substitutions for rendering the output data
  53. metadata = {"glyphs": _generate_font_glyphs_list(not cli.args.no_ascii, cli.args.unicode_glyphs)}
  54. subs = generate_subs(cli, out_bytes, font_metadata=metadata, command_name="painter_convert_font_image")
  55. # Render and write the header file
  56. header_text = render_header(subs)
  57. header_file = cli.args.output / f"{cli.args.input.stem}.qff.h"
  58. with open(header_file, 'w') as header:
  59. print(f"Writing {header_file}...")
  60. header.write(header_text)
  61. # Render and write the source file
  62. source_text = render_source(subs)
  63. source_file = cli.args.output / f"{cli.args.input.stem}.qff.c"
  64. with open(source_file, 'w') as source:
  65. print(f"Writing {source_file}...")
  66. source.write(source_text)