Commit c9064181 authored by Sebastian Friedl's avatar Sebastian Friedl
Browse files

Generate (S)CSS stylesheets for webfonts

parent 6685d254
......@@ -161,13 +161,47 @@ def slice_font(style, source, weight, adobe_weight, verbose_weight, italic=False
for flavor in (None, 'woff', 'woff2'):
output_dir = _root_dir / 'out' / f'gypt-{style.lower()}' / f'{(flavor or "ttf").upper()}'
output_fnt = f'GYPT{style}-{(weight != 400 or not italic) * verbose_weight}{italic * "It"}.{flavor or "ttf"}'
output_fnt = f'GYPT{style}-{(weight != 400 or not italic)*verbose_weight}{italic*"It"}.{flavor or "ttf"}'
instance.flavor = flavor
os.makedirs(output_dir, exist_ok=True)
instance.save(output_dir / output_fnt)
def generate_stylesheet(style):
def render(sty, wght, verb_wght, it=False, prefix=''):
return '\n'.join([
f"@font-face {{",
f" font-family: 'GYPT {sty}';",
f" font-weight: {wght};",
f" font-style: {'normal' if not it else 'italic'};",
f" font-stretch: normal;",
f" font-display: swap;",
f" src: url({prefix}WOFF2/GYPT{sty}-{(1 - (wght == 400)*it)*verb_wght}{it*'It'}.woff2) format('woff2'),",
f" url({prefix}WOFF/GYPT{sty}-{(1 - (wght == 400)*it)*verb_wght}{it*'It'}.woff) format('woff'),",
f" url({prefix}TTF/GYPT{sty}-{(1 - (wght == 400)*it)*verb_wght}{it*'It'}.ttf) format('truetype');",
f"}}",
])
chunks = {'css': [], 'scss': []}
for weight, options in INSTANCES.items():
_, verbose_weight = options
chunks['css'].extend([
render(style, weight, verbose_weight, it=False, prefix=''),
render(style, weight, verbose_weight, it=True, prefix=''),
])
chunks['scss'].extend([
render(style, weight, verbose_weight, it=False, prefix=f'#{{$gypt{style.lower()}-font-path}}/'),
render(style, weight, verbose_weight, it=True, prefix=f'#{{$gypt{style.lower()}-font-path}}/'),
])
os.makedirs(out_dir := _root_dir / 'out' / f'gypt-{style.lower()}', exist_ok=True)
for file_format, chunks in chunks.items():
with open(out_dir / f'gypt-{style.lower()}.{file_format}', 'w') as file:
file.write('\n'.join(chunks) + '\n')
def main():
exit_code = 0
......@@ -187,10 +221,13 @@ def main():
p.apply_async(slice_font, (style, source, weight, *options), {'italic': False}, error_callback=term(p))
p.apply_async(slice_font, (style, source, weight, *options), {'italic': True}, error_callback=term(p))
generate_stylesheet(style)
p.close()
p.join()
if exit_code:
# pool has been terminated, exit with error code
sys.exit(exit_code)
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment