-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: rework build system, fix tables
- Loading branch information
Showing
41 changed files
with
372 additions
and
278 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
"""Lilex font library""" | ||
from .build import DEFAULT_FORMATS, SUPPORTED_FORMATS | ||
from .generator import generate_spacers, render_ligatures | ||
from .build import FontFormat, build_family | ||
from .features import OpenTypeFeatures, generate_spacers, render_ligatures | ||
from .glyphs_font import GlyphsFont |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
"""Lilex font builder module""" | ||
from .const import DEFAULT_FORMATS, SUPPORTED_FORMATS | ||
from .make import make | ||
from .build import build_family | ||
from .constants import FontFormat |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
import asyncio | ||
import os | ||
from shutil import rmtree | ||
from tempfile import mkdtemp | ||
|
||
from glyphsLib import GSFont, build_masters | ||
from glyphsLib.builder.axes import find_base_style | ||
|
||
from .constants import FontFormat | ||
from .fontmake import fontmake | ||
from .post_process import post_process | ||
|
||
|
||
def _build_design_space(font: GSFont) -> str: | ||
"""Creates temporary designspace""" | ||
temp_dir = mkdtemp(prefix="lilex") | ||
glyphs_file = os.path.join(temp_dir, "source.glyphs") | ||
ufo_dir = os.path.join(temp_dir, "master_ufo") | ||
file_name = font.familyName | ||
base_style = find_base_style(font.masters) | ||
if base_style != "": | ||
file_name = f"{file_name}-{base_style}" | ||
ds_file = os.path.join(ufo_dir, f"{file_name}.designspace") | ||
font.save(glyphs_file) | ||
build_masters( | ||
glyphs_file, | ||
ufo_dir, | ||
write_skipexportglyphs=True, | ||
designspace_path=ds_file, | ||
) | ||
return (temp_dir, ds_file) | ||
|
||
async def _build_font( | ||
font: GSFont, | ||
output_dir: str, | ||
formats: list[FontFormat] | ||
) -> list[str]: | ||
"""Builds a font format. Returns a list of output files""" | ||
temp_dir, ds_file = _build_design_space(font) | ||
format_tasks = [] | ||
for fmt in formats: | ||
out_dir = os.path.join(output_dir, fmt.value) | ||
format_tasks.append(fontmake(ds_file, out_dir, fmt)) | ||
files = await asyncio.gather(*format_tasks) | ||
rmtree(temp_dir) | ||
return files | ||
|
||
def _group_by_format(output: list[list[tuple[str, list[str]]]]) -> dict[FontFormat, list[str]]: | ||
result = {} | ||
for design_space in output: | ||
for fmt, files in design_space: | ||
if fmt not in result: | ||
result[fmt] = [] | ||
result[fmt].extend(files) | ||
return result | ||
|
||
async def build_family( | ||
fonts: list[GSFont], | ||
output_dir: str, | ||
formats: list[FontFormat]): | ||
"""Builds a font family""" | ||
tasks = [] | ||
for font in fonts: | ||
tasks.append(_build_font(font, output_dir, formats)) | ||
files = await asyncio.gather(*tasks) | ||
await post_process(_group_by_format(files)) | ||
return files |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
"""Lilex builder constants""" | ||
from enum import Enum | ||
|
||
|
||
class FontFormat(Enum): | ||
"""Font format enum""" | ||
TTF = "ttf" | ||
OTF = "otf" | ||
VARIABLE = "variable" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
"""Make helpers""" | ||
import asyncio | ||
import os | ||
import re | ||
|
||
from .constants import FontFormat | ||
from .path import which | ||
|
||
|
||
def _format_variable_path(font_dir: str, family_name: str, axis: list[str]) -> str: | ||
return f"{font_dir}/{family_name}[{','.join(axis)}].ttf" | ||
|
||
async def fontmake( | ||
design_space_path: str, | ||
out_dir: str, | ||
fmt: FontFormat, | ||
axis: list[str] = None | ||
): | ||
"""Wrapper for fontmake""" | ||
if axis is None: | ||
axis = ["wght"] | ||
cmd = [ | ||
which("fontmake"), | ||
f'-m "{design_space_path}"', | ||
f'-o "{fmt.value}"', | ||
"--flatten-components", | ||
"--autohint", | ||
"--filter DecomposeTransformedComponentsFilter" | ||
] | ||
font_name = os.path.basename(design_space_path).split(".")[0] | ||
if fmt == FontFormat.VARIABLE: | ||
cmd.append(f'--output-path "{_format_variable_path(out_dir, font_name, axis)}"') | ||
else: | ||
cmd.append("--interpolate") | ||
cmd.append(f'--output-dir "{out_dir}"') | ||
proc = await asyncio.create_subprocess_shell(" ".join(cmd), | ||
stdout=asyncio.subprocess.PIPE, | ||
stderr=asyncio.subprocess.PIPE) | ||
_, stderr = await proc.communicate() | ||
file_re = re.escape(out_dir) + r"/(.*)" | ||
matches = re.findall(file_re, stderr.decode(), flags=re.MULTILINE) | ||
files = [] | ||
for match in matches: | ||
files.append(os.path.join(out_dir, match)) | ||
return fmt, list(set(files)) |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
"""Path helpers""" | ||
import shutil | ||
|
||
|
||
def which(cmd: str) -> str: | ||
"""shutil.which that throws on None""" | ||
result = shutil.which(cmd) | ||
if result is None: | ||
raise ValueError(f""" | ||
Can't find {cmd}. Make sure you have venv configured with | ||
`make configure` and activated. Alternatively, install {cmd} | ||
externally and check by running `which {cmd}`. | ||
""") | ||
return result |
Oops, something went wrong.