Skip to content

Commit

Permalink
Merge pull request #11 from googlefonts/compile-fontmake
Browse files Browse the repository at this point in the history
Add fontmake output action
  • Loading branch information
justvanrossum authored Mar 20, 2024
2 parents 2e0d416 + 1eed8ce commit 6affd20
Show file tree
Hide file tree
Showing 8 changed files with 5,497 additions and 18 deletions.
5 changes: 3 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ authors = [
]
keywords = ["font", "fonts"]
license = {text = "GNU General Public License v3"}
dependencies = ["fontra"]
dependencies = ["fontra", "fontmake"]
dynamic = ["version"]
requires-python = ">=3.10"
classifiers = [
Expand All @@ -37,7 +37,8 @@ fontra-compile = "fontra_compile.__main__:main"


[project.entry-points."fontra.workflow.actions"]
fontra_compile = "fontra_compile.workflow_action"
compile_varc = "fontra_compile.compile_varc_action"
compile_fontmake = "fontra_compile.compile_fontmake_action"


[tool.hatch.build.targets.wheel]
Expand Down
8 changes: 4 additions & 4 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
mypy==1.8.0
pre-commit==3.3.3
pytest==7.3.2
types-PyYAML==6.0.12.12
mypy==1.9.0
pre-commit==3.6.2
pytest==8.1.1
types-PyYAML==6.0.12.20240311
3 changes: 2 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
fonttools==4.43.0
fonttools==4.50.0
fontmake==3.8.1
git+https://github.com/googlefonts/fontra.git
git+https://github.com/googlefonts/fontra-rcjk.git
63 changes: 63 additions & 0 deletions src/fontra_compile/compile_fontmake_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
import os
import pathlib
import subprocess
import tempfile
from contextlib import aclosing, asynccontextmanager
from dataclasses import dataclass, field
from typing import AsyncGenerator

from fontra.backends import newFileSystemBackend
from fontra.backends.copy import copyFont
from fontra.core.protocols import ReadableFontBackend
from fontra.workflow.actions import OutputActionProtocol, registerActionClass


@registerActionClass("compile-fontmake")
@dataclass(kw_only=True)
class CompileFontMakeAction:
destination: str
options: dict[str, str] = field(default_factory=dict)
input: ReadableFontBackend | None = field(init=False, default=None)

@asynccontextmanager
async def connect(
self, input: ReadableFontBackend
) -> AsyncGenerator[ReadableFontBackend | OutputActionProtocol, None]:
self.input = input
try:
yield self
finally:
self.input = None

async def process(self, outputDir: os.PathLike = pathlib.Path()) -> None:
assert self.input is not None
outputDir = pathlib.Path(outputDir)
outputFontPath = outputDir / self.destination

with tempfile.TemporaryDirectory() as tmpDirName:
tmpDir = pathlib.Path(tmpDirName)

designspacePath = tmpDir / "temp.designspace"

dsBackend = newFileSystemBackend(designspacePath)

async with aclosing(dsBackend):
await copyFont(self.input, dsBackend)

# assert 0, [p.name for p in tmpDir.iterdir()]
command = [
"fontmake",
"-m",
os.fspath(designspacePath),
"-o",
"variable",
"--output-path",
os.fspath(outputFontPath),
]

for option, value in self.options.items():
command.append(f"--{option}")
if value:
command.append(value)

subprocess.run(command, check=True)
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from .builder import Builder


@registerActionClass("fontra-compile")
@registerActionClass("compile-varc")
@dataclass(kw_only=True)
class FontraCompileAction:
destination: str
Expand Down
Loading

0 comments on commit 6affd20

Please sign in to comment.