2022-04-18 12:29:52 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import io
|
|
|
|
import sys
|
|
|
|
import json
|
2022-04-27 11:18:02 -04:00
|
|
|
import shutil
|
2022-04-18 12:29:52 -04:00
|
|
|
import typing
|
|
|
|
import os.path
|
|
|
|
import argparse
|
|
|
|
import subprocess
|
|
|
|
|
2022-04-20 10:15:30 -04:00
|
|
|
class Source(object):
|
2022-04-18 12:29:52 -04:00
|
|
|
def __init__(self, d):
|
2022-04-20 10:15:30 -04:00
|
|
|
self.file = d['file']
|
2022-04-20 14:38:42 -04:00
|
|
|
if not os.path.exists(self.file):
|
|
|
|
self.file = os.path.join(os.path.dirname(sys.argv[0]), self.file)
|
2022-04-18 12:29:52 -04:00
|
|
|
self.range = d.get('range')
|
|
|
|
self.symbols = d.get('symbols')
|
|
|
|
|
|
|
|
|
2022-04-20 10:15:30 -04:00
|
|
|
def gen_lvconv_line(dest: str, size: int, bpp: int, sources: typing.List[Source], compress:bool=False):
|
2022-04-18 12:29:52 -04:00
|
|
|
args = ['lv_font_conv', '--size', str(size), '--output', dest, '--bpp', str(bpp), '--format', 'lvgl']
|
|
|
|
if not compress:
|
|
|
|
args.append('--no-compress')
|
2022-04-20 10:15:30 -04:00
|
|
|
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])
|
2022-04-18 12:29:52 -04:00
|
|
|
|
|
|
|
return args
|
|
|
|
|
|
|
|
def main():
|
2022-04-19 12:51:29 -04:00
|
|
|
ap = argparse.ArgumentParser(description='auto generate LVGL font files from fonts')
|
2022-04-18 12:29:52 -04:00
|
|
|
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=[])
|
|
|
|
args = ap.parse_args()
|
|
|
|
|
2022-04-27 11:18:02 -04:00
|
|
|
if not shutil.which('lv_font_conv'):
|
|
|
|
sys.exit(f'Missing lv_font_conv. (make sure it is installed and in PATH)')
|
2022-04-18 12:29:52 -04:00
|
|
|
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 accessable (permissions?).')
|
|
|
|
with open(args.config, 'r') as fd:
|
|
|
|
data = json.load(fd)
|
|
|
|
|
2022-04-20 10:15:30 -04:00
|
|
|
fonts_to_run = set(data.keys())
|
2022-04-18 13:42:58 -04:00
|
|
|
|
2022-04-19 11:01:12 -04:00
|
|
|
if args.font:
|
2022-04-19 11:05:03 -04:00
|
|
|
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)
|
2022-04-19 07:41:17 -04:00
|
|
|
if d:
|
|
|
|
print(f'Warning: requested font{"s" if len(d)>1 else ""} missing: {" ".join(d)}')
|
2022-04-19 11:05:03 -04:00
|
|
|
fonts_to_run = fonts_to_run.intersection(enabled_fonts)
|
2022-04-19 07:41:17 -04:00
|
|
|
|
2022-04-19 11:01:12 -04:00
|
|
|
for name in fonts_to_run:
|
2022-04-20 10:15:30 -04:00
|
|
|
font = data[name]
|
2022-04-18 12:29:52 -04:00
|
|
|
sources = font.pop('sources')
|
2022-04-19 07:53:57 -04:00
|
|
|
patches = font.pop('patches') if 'patches' in font else []
|
2022-04-20 10:15:30 -04:00
|
|
|
font['sources'] = [Source(thing) for thing in sources]
|
2022-04-18 12:29:52 -04:00
|
|
|
line = gen_lvconv_line(f'{name}.c', **font)
|
|
|
|
subprocess.check_call(line)
|
|
|
|
if patches:
|
|
|
|
for patch in patches:
|
2022-04-27 11:25:03 -04:00
|
|
|
try:
|
|
|
|
# Try and patch, if given a string
|
|
|
|
patch = patch.format(name=name, file=name+'.c')
|
|
|
|
subprocess.check_call(['/usr/bin/patch', name+'.c', patch])
|
|
|
|
continue
|
2022-04-20 10:15:30 -04:00
|
|
|
except: pass
|
2022-04-27 11:25:03 -04:00
|
|
|
try:
|
|
|
|
# otherwise, assume a "advanced" patch, which is a list of strings.
|
|
|
|
patch = [arg.format(name=name, file=name+'.c') for arg in patch]
|
|
|
|
subprocess.check_call(patch)
|
|
|
|
except Exception as e:
|
|
|
|
sys.exit('Failed to patch using "{patch}"\n{err}'.format(patch=patch,err=e))
|
2022-04-18 12:29:52 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|