Skip to content

Commit

Permalink
Merge pull request #27 from googlefonts/add-compile-fontc-action
Browse files Browse the repository at this point in the history
Add compile-fontc output action
  • Loading branch information
justvanrossum authored May 17, 2024
2 parents 5e933eb + e5c75fe commit 593e3ea
Show file tree
Hide file tree
Showing 8 changed files with 5,710 additions and 17 deletions.
3 changes: 2 additions & 1 deletion 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", "fontmake"]
dependencies = ["fontra", "fontmake", "fontc"]
dynamic = ["version"]
requires-python = ">=3.10"
classifiers = [
Expand All @@ -39,6 +39,7 @@ fontra-compile = "fontra_compile.__main__:main"
[project.entry-points."fontra.workflow.actions"]
compile_varc = "fontra_compile.compile_varc_action"
compile_fontmake = "fontra_compile.compile_fontmake_action"
compile_fontc = "fontra_compile.compile_fontc_action"


[tool.hatch.build.targets.wheel]
Expand Down
1 change: 1 addition & 0 deletions requirements-dev.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
mypy==1.10.0
pre-commit==3.7.1
pytest==8.2.0
pytest-asyncio==0.23.3
types-PyYAML==6.0.12.20240311
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
fonttools==4.51.0
fontmake==3.9.0
fontc==0.0.1.post5
git+https://github.com/googlefonts/fontra.git
git+https://github.com/googlefonts/fontra-rcjk.git
22 changes: 22 additions & 0 deletions src/fontra_compile/compile_fontc_action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import os
import subprocess
from dataclasses import dataclass

from fontra.workflow.actions import registerOutputAction

from .compile_fontmake_action import CompileFontMakeAction


@registerOutputAction("compile-fontc")
@dataclass(kw_only=True)
class CompileFontCAction(CompileFontMakeAction):

def compileFromDesignspace(self, designspacePath, outputFontPath, extraArguments):
arguments = [
"fontc",
"--output-file",
os.fspath(outputFontPath),
os.fspath(designspacePath),
]

subprocess.run(arguments + extraArguments, check=True)
52 changes: 39 additions & 13 deletions src/fontra_compile/compile_fontmake_action.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
from fontra.core.protocols import ReadableFontBackend
from fontra.workflow.actions import OutputActionProtocol, registerOutputAction
from fontTools.designspaceLib import DesignSpaceDocument
from fontTools.ufoLib import UFOReaderWriter


@registerOutputAction("compile-fontmake")
Expand Down Expand Up @@ -49,22 +50,27 @@ async def process(
await copyFont(self.input, dsBackend, continueOnError=continueOnError)

addInstances(designspacePath)
addGlyphOrder(designspacePath)

arguments = [
"-m",
os.fspath(designspacePath),
"-o",
"variable",
"--output-path",
os.fspath(outputFontPath),
]

extraArguments = []
for option, value in self.options.items():
arguments.append(f"--{option}")
extraArguments.append(f"--{option}")
if value:
arguments.append(value)
extraArguments.append(value)

self.compileFromDesignspace(designspacePath, outputFontPath, extraArguments)

def compileFromDesignspace(self, designspacePath, outputFontPath, extraArguments):
arguments = [
"-m",
os.fspath(designspacePath),
"-o",
"variable",
"--output-path",
os.fspath(outputFontPath),
]

fontmake_main(arguments)
fontmake_main(arguments + extraArguments)


def addInstances(designspacePath):
Expand Down Expand Up @@ -94,6 +100,8 @@ def addInstances(designspacePath):
for axis in axes
]

axesByName = {axis.name: axis for axis in dsDoc.axes}

for items in itertools.product(*axisLabels):
location = {name: value for (name, valueLabel, value) in items}
nameParts = [valueLabel for (name, valueLabel, value) in items if valueLabel]
Expand All @@ -103,6 +111,24 @@ def addInstances(designspacePath):

# TODO: styleName seems to be ignored, and the instance names are derived
# from axis labels elsewhere. Figure out where this happens.
dsDoc.addInstanceDescriptor(styleName=styleName, userLocation=location)
location = mapLocationForward(location, axesByName)
dsDoc.addInstanceDescriptor(
familyName="Testing", styleName=styleName, location=location
)

dsDoc.write(designspacePath)


def mapLocationForward(location, axes):
return {name: axes[name].map_forward(value) for name, value in location.items()}


def addGlyphOrder(designspacePath):
dsDoc = DesignSpaceDocument.fromfile(designspacePath)
defaultSource = dsDoc.findDefault()
ufo = UFOReaderWriter(defaultSource.path)
lib = ufo.readLib()
if "public.glyphOrder" not in lib:
glyphSet = ufo.getGlyphSet()
lib["public.glyphOrder"] = sorted(glyphSet.keys())
ufo.writeLib(lib)
Loading

0 comments on commit 593e3ea

Please sign in to comment.