Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto generate font #1097

Merged
merged 31 commits into from
May 10, 2022
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
5dc16fa
Added font auto-generate script
yehoshuapw Apr 18, 2022
8abbc65
Added FontAwesome5-Solid+Brands+Regular.woff to git
yehoshuapw Apr 18, 2022
7b0a5d6
fontgen: Added ability to choose fonts with .c
yehoshuapw Apr 18, 2022
83780b4
fontgen: added missing requested font check
yehoshuapw Apr 19, 2022
cea34b9
fontgen: move features into fonts
yehoshuapw Apr 19, 2022
3bb597f
fontgen: removed ability of removing .c ext
yehoshuapw Apr 19, 2022
4b8424c
fontgen: use patch file for jetbrains 0 fix
yehoshuapw Apr 19, 2022
147cfc3
fontgen: simplify enabled fonts
yehoshuapw Apr 19, 2022
36a1250
fontgen: remove .c from requested font if there
yehoshuapw Apr 19, 2022
dadbb9a
fontgen: minor changes
yehoshuapw Apr 19, 2022
fbe98d7
fontgen: simplfy json after removed external features key
yehoshuapw Apr 20, 2022
7aa36a0
fonts: update README.md to match new method
yehoshuapw Apr 20, 2022
d6faf91
fontgen: generate fonts at runtime with CMake
yehoshuapw Apr 20, 2022
c7b157e
Added lv_font_conv to workflow main
yehoshuapw Apr 20, 2022
5a16e1b
fontgen: make simulator build fonts too
yehoshuapw Apr 20, 2022
b9c35de
fontgen: generate font .c files in build dir
yehoshuapw Apr 20, 2022
6d57001
fontgen: move lv_font_conv doc
yehoshuapw Apr 24, 2022
f645cbc
fontgen: remove "generating the fonts" section
yehoshuapw Apr 24, 2022
95ff573
fontgen: remove "feature" feature
yehoshuapw Apr 24, 2022
97bdfd7
fontgen: lock version at current
yehoshuapw Apr 24, 2022
f31adfb
fontgen: install npm, lv_font_conv in Dockerfile
yehoshuapw Apr 24, 2022
c48a7b1
fotngen: check for lv_font_conv
yehoshuapw Apr 27, 2022
7d81157
fontgen: assume plain .patch for single string patch
yehoshuapw Apr 27, 2022
0a45043
fontgen: remove double-asterisk in readme
yehoshuapw Apr 27, 2022
415c4d3
fontgen: verify lv_font_conv at cmake
yehoshuapw Apr 27, 2022
0fcbf22
fontgen: remove advanced (format string, process as list) from patching
yehoshuapw Apr 27, 2022
08491cb
fontgen: changes to allow CMake to work from other project
yehoshuapw Apr 28, 2022
e21d92b
fongen: don't pre-build fonts
yehoshuapw Apr 28, 2022
24b7950
docker: bump ubuntu to 20.04 and node to 18
yehoshuapw May 8, 2022
8becd02
update docker to 22, and use python3 by default
yehoshuapw May 10, 2022
c455087
fontgen: update README.md to remove patch advanced options
yehoshuapw May 10, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
30 changes: 30 additions & 0 deletions src/displayapp/fonts/fix_jetbrains_mono_bold_20_zero.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
#!/usr/bin/env python
yehoshuapw marked this conversation as resolved.
Show resolved Hide resolved

import sys

with open('jetbrains_mono_bold_20.c', 'r') as fd:
src = fd.read()

zero_area = src.find('U+0030 "0"')
zero_data_start = src[zero_area:].find('\n') + zero_area + 1
zero_data_end = src[zero_data_start:].find('\n\n')

curr = src[zero_data_start:zero_data_start+zero_data_end]

OLD=""" 0x3f, 0x1f, 0xef, 0x3f, 0x87, 0xe1, 0xf8, 0x7f,
0xdf, 0xf7, 0xe1, 0xf8, 0x7e, 0x1f, 0xcf, 0x7f,
0x8f, 0xc0,"""
NEW=""" 0x3f, 0x1f, 0xef, 0x3f, 0x87, 0xe1, 0xf8, 0x7e,
0xdf, 0xb7, 0xe1, 0xf8, 0x7e, 0x1f, 0xcf, 0x7f,
0x8f, 0xc0,"""

if curr != OLD:
if curr == NEW:
print('Already patched')
sys.exit()
sys.exit('Target differers from expected data')


with open('jetbrains_mono_bold_20.c', 'r+') as fd:
fd.seek(zero_data_start) #NOTE: since they are both the same size, this is allowed. Otherwise more editing needs to happen.
fd.write(NEW)
90 changes: 90 additions & 0 deletions src/displayapp/fonts/fonts.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
{
"fonts": {
"jetbrains_mono_bold_20": {
"sources": [
{
"font": "JetBrainsMono-Bold.ttf",
"range": "0x20-0x7e, 0x410-0x44f"
},
{
"font": "FontAwesome5-Solid+Brands+Regular.woff",
"range": "0xf293, 0xf294, 0xf244, 0xf240, 0xf242, 0xf243, 0xf241, 0xf54b, 0xf21e, 0xf1e6, 0xf54b, 0xf017, 0xf129, 0xf03a, 0xf185, 0xf560, 0xf001, 0xf3fd, 0xf069, 0xf1fc, 0xf45d, 0xf59f, 0xf5a0, 0xf029, 0xf027, 0xf028, 0xf6a9, 0xf04b, 0xf04c, 0xf048, 0xf051, 0xf095, 0xf3dd, 0xf04d, 0xf2f2, 0xf024, 0xf252, 0xf569, 0xf201, 0xf06e, 0xf015"
}
],
"bpp": 1,
"size": 20,
"patches": ["./fix_jetbrains_mono_bold_20_zero.py"]
},
"jetbrains_mono_42": {
"sources": [
{
"font": "JetBrainsMono-Regular.ttf",
"range": "0x25, 0x30-0x3a"
}
],
"bpp": 1,
"size": 42
},
"jetbrains_mono_76": {
"sources": [
{
"font": "JetBrainsMono-Regular.ttf",
"range": "0x25, 0x2D, 0x2F, 0x30-0x3a"
}
],
"bpp": 1,
"size": 76
},
"jetbrains_mono_extrabold_compressed": {
"sources": [
{
"font": "JetBrainsMono-ExtraBold.ttf",
"range": "0x30-0x3a"
}
],
"bpp": 1,
"size": 80
},
"open_sans_light": {
"sources": [
{
"font": "open_sans_light.ttf",
"symbols": "0123456789"
}
],
"bpp": 1,
"size": 150
},
"lv_font_sys_48": {
"sources": [
{
"font": "icons_sys_48.ttf",
"range": "0xe902, 0xe904-0xe907, 0xe90b-0xe90c"
}
],
"bpp": 1,
"size": 48
},
"lv_font_navi_80": {
"sources": [
{
"font": "navigation.ttf",
"range": "0xe900-0xe929"
}
],
"bpp": 2,
"size": 80,
"compress": true
}
},
"features": {
"hebrew": {
"jetbrains_mono_bold_20": [
{
"font": "SimpleCLM-Medium.ttf",
"range": "0x05D0-0x05EA,0x05F3,0x05F4"
}
]
}
}
yehoshuapw marked this conversation as resolved.
Show resolved Hide resolved
}
80 changes: 80 additions & 0 deletions src/displayapp/fonts/generate.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
#!/usr/bin/env python

import io
import sys
import json
import typing
import os.path
import argparse
import subprocess

class FontArg(object):
def __init__(self, d):
self.font = d['font']
self.range = d.get('range')
self.symbols = d.get('symbols')


def gen_lvconv_line(dest: str, size: int, bpp: int, fonts: typing.List[FontArg], 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 font in fonts:
args.extend(['--font', font.font])
if font.range:
args.extend(['--range', font.range])
if font.symbols:
args.extend(['--symbols', font.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('-e', '--enable', type=str, action='append', help='optional feature to enable in font generation', default=[], metavar='features', dest='features')
ap.add_argument('-f', '--font', type=str, action='append', help='Choose specific fonts to generate (default: all)', default=[])
ap.add_argument('-c', '--removec', action='store_true', help='remove .c extension from font names (given in -f options)', default=False)
args = ap.parse_args()

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)

for enabled_feature in args.features:
if enabled_feature not in data['features']:
sys.exit(f'Error: the requested feature {enabled_feature} does not exist in {args.config}.')

fonts_to_run = args.font
if args.removec and args.font:
fonts_to_run = []
for font in args.font:
if font.endswith('.c'):
fonts_to_run.append(font[:-2])
else:
sys.exit(f'requested to remove .c extension, but {font} does not have it.')
yehoshuapw marked this conversation as resolved.
Show resolved Hide resolved

for (name,font) in data['fonts'].items():
if fonts_to_run and name not in fonts_to_run:
continue
Avamander marked this conversation as resolved.
Show resolved Hide resolved
sources = font.pop('sources')
if 'patches' in font:
patches = font.pop('patches')
else:
patches = None
for enabled_feature in args.features:
if name in data['features'][enabled_feature]:
sources.extend(data['features'][enabled_feature][name])
font['fonts'] = [FontArg(thing) for thing in sources]
line = gen_lvconv_line(f'{name}.c', **font)
subprocess.check_call(line)
if patches:
for patch in patches:
subprocess.check_call(patch)



if __name__ == '__main__':
main()