diff options
| author | Michele Bini <michele.bini@gmail.com> | 2022-06-06 18:27:54 (GMT) |
|---|---|---|
| committer | Michele Bini <michele.bini@gmail.com> | 2022-06-06 23:09:58 (GMT) |
| commit | 7c45f185a32c7bcff47c8fda1acdec82d3213717 (patch) | |
| tree | 53b0419fe2a19e6ec5c89d0be0a11dcd8d4ceb00 /src/displayapp/fonts/generate.py | |
| parent | 94b1b330fc1f6e941a797fedabade4e790e28bc2 (diff) | |
| parent | 35dcf8c8607483c104711c9398d47d57147f4389 (diff) | |
Merge remote-tracking branch 'origin/develop' into analog24
Diffstat (limited to 'src/displayapp/fonts/generate.py')
| -rwxr-xr-x | src/displayapp/fonts/generate.py | 75 |
1 files changed, 75 insertions, 0 deletions
diff --git a/src/displayapp/fonts/generate.py b/src/displayapp/fonts/generate.py new file mode 100755 index 0000000..c172df0 --- /dev/null +++ b/src/displayapp/fonts/generate.py @@ -0,0 +1,75 @@ +#!/usr/bin/env python + +import io +import sys +import json +import shutil +import typing +import os.path +import argparse +import subprocess + +class Source(object): + def __init__(self, d): + self.file = d['file'] + if not os.path.exists(self.file): + self.file = os.path.join(os.path.dirname(sys.argv[0]), self.file) + self.range = d.get('range') + self.symbols = d.get('symbols') + + +def gen_lvconv_line(lv_font_conv: str, dest: str, size: int, bpp: int, sources: typing.List[Source], compress:bool=False): + args = [lv_font_conv, '--size', str(size), '--output', dest, '--bpp', str(bpp), '--format', 'lvgl'] + if not compress: + args.append('--no-compress') + for source in sources: + args.extend(['--font', source.file]) + if source.range: + args.extend(['--range', source.range]) + if source.symbols: + args.extend(['--symbols', source.symbols]) + + return args + +def main(): + ap = argparse.ArgumentParser(description='auto generate LVGL font files from fonts') + ap.add_argument('config', type=str, help='config file to use') + ap.add_argument('-f', '--font', type=str, action='append', help='Choose specific fonts to generate (default: all)', default=[]) + ap.add_argument('--lv-font-conv', type=str, help='Path to "lv_font_conf" executable', default="lv_font_conv") + args = ap.parse_args() + + if not shutil.which(args.lv_font_conv): + sys.exit(f"Missing lv_font_conv. Make sure it's findable (in PATH) or specify it manually") + if not os.path.exists(args.config): + sys.exit(f'Error: the config file {args.config} does not exist.') + if not os.access(args.config, os.R_OK): + sys.exit(f'Error: the config file {args.config} is not accessible (permissions?).') + with open(args.config, 'r') as fd: + data = json.load(fd) + + fonts_to_run = set(data.keys()) + + if args.font: + enabled_fonts = set() + for font in args.font: + enabled_fonts.add(font[:-2] if font.endswith('.c') else font) + d = enabled_fonts.difference(fonts_to_run) + if d: + print(f'Warning: requested font{"s" if len(d)>1 else ""} missing: {" ".join(d)}') + fonts_to_run = fonts_to_run.intersection(enabled_fonts) + + for name in fonts_to_run: + font = data[name] + sources = font.pop('sources') + patches = font.pop('patches') if 'patches' in font else [] + font['sources'] = [Source(thing) for thing in sources] + line = gen_lvconv_line(args.lv_font_conv, f'{name}.c', **font) + subprocess.check_call(line) + if patches: + for patch in patches: + subprocess.check_call(['/usr/bin/patch', name+'.c', patch]) + + + +if __name__ == '__main__': + main() |
