Skip to content

Commit

Permalink
Merge pull request #671 from anthrotype/cffsubr
Browse files Browse the repository at this point in the history
support subroutinizing with cffsubr; add otf-cff2 static output
  • Loading branch information
anthrotype authored Jun 9, 2020
2 parents 725a213 + 35700df commit 37435a8
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 7 deletions.
9 changes: 9 additions & 0 deletions Lib/fontmake/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,7 @@ def main(args=None):
choices=(
"ufo",
"otf",
"otf-cff2",
"ttf",
"ttf-interpolatable",
"otf-interpolatable",
Expand Down Expand Up @@ -280,6 +281,14 @@ def main(args=None):
help="0 disables all optimizations; 1 specializes the CFF charstring "
"operators; 2 (default) also enables subroutinization",
)
contourGroup.add_argument(
"--subroutinizer",
default=None,
choices=["compreffor", "cffsubr"],
help="name of the library to use for compressing CFF charstrings. "
"Choose between: %(choices)s. By default compreffor is used for CFF 1, "
"and cffsubr for CFF2. NOTE: compreffor doesn't support CFF2.",
)
contourGroup.add_argument(
"--no-optimize-gvar",
dest="optimize_gvar",
Expand Down
16 changes: 13 additions & 3 deletions Lib/fontmake/font_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ def build_variable_font(
output_dir=None,
ttf=True,
optimize_gvar=True,
optimize_cff=CFFOptimization.SPECIALIZE,
use_production_names=None,
reverse_direction=True,
conversion_error=None,
Expand Down Expand Up @@ -333,6 +334,7 @@ def build_variable_font(
useProductionNames=use_production_names,
roundTolerance=cff_round_tolerance,
debugFeatureFile=debug_feature_file,
optimizeCFF=optimize_cff,
inplace=True,
)

Expand All @@ -343,7 +345,7 @@ def _iter_compile(self, ufos, ttf=False, debugFeatureFile=None, **kwargs):
# yields ttFont instances
options = dict(kwargs)
if ttf:
for key in ("optimizeCFF", "roundTolerance"):
for key in ("optimizeCFF", "roundTolerance", "subroutinizer", "cffVersion"):
options.pop(key, None)
compile_func, fmt = ufo2ft.compileTTF, "TTF"
else:
Expand Down Expand Up @@ -387,6 +389,8 @@ def save_otfs(
output_dir=None,
debug_feature_file=None,
inplace=True,
cff_version=1,
subroutinizer=None,
):
"""Build OpenType binaries from UFOs.
Expand Down Expand Up @@ -476,6 +480,8 @@ def save_otfs(
cubicConversionError=conversion_error,
featureWriters=feature_writers,
debugFeatureFile=debug_feature_file,
cffVersion=cff_version,
subroutinizer=subroutinizer,
inplace=True, # avoid extra copy
)

Expand Down Expand Up @@ -1024,6 +1030,9 @@ def run_from_ufos(self, ufos, output=(), **kwargs):
if set(output) == {"ufo"}:
return

if "otf" in output and "otf-cff2" in output:
raise ValueError("'otf' and 'otf-cff2' outputs are mutually exclusive")

# the `ufos` parameter can be a list of UFO objects
# or it can be a path (string) with a glob syntax
ufo_paths = []
Expand All @@ -1041,8 +1050,9 @@ def run_from_ufos(self, ufos, output=(), **kwargs):
)

need_reload = False
if "otf" in output:
self.build_otfs(ufos, **kwargs)
cff_version = 1 if "otf" in output else 2 if "otf-cff2" in output else None
if cff_version is not None:
self.build_otfs(ufos, cff_version=cff_version, **kwargs)
need_reload = True

if "ttf" in output:
Expand Down
5 changes: 3 additions & 2 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
fonttools[lxml,unicode,ufo]==4.10.2
fonttools[lxml,unicode,ufo]==4.11.0
cu2qu==1.6.7
glyphsLib==5.1.10
ufo2ft[pathops]==2.14.0
ufo2ft[pathops]==2.15.0
MutatorMath==2.1.2
fontMath==0.6.0
defcon[lxml]==0.6.0
booleanOperations==0.9.0
ufoLib2==0.7.1
attrs==19.3.0
cffsubr==0.2.6
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@
setup_requires=wheel + ["setuptools_scm"],
python_requires=">=3.6",
install_requires=[
"fonttools[ufo,lxml,unicode]>=4.10.2",
"fonttools[ufo,lxml,unicode]>=4.11.0",
"cu2qu>=1.6.7",
"glyphsLib>=5.1.10",
"ufo2ft>=2.14.0",
"ufo2ft[cffsubr]>=2.15.0",
"fontMath>=0.6.0",
"booleanOperations>=0.9.0",
"ufoLib2>=0.7.1",
Expand Down
32 changes: 32 additions & 0 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -625,3 +625,35 @@ def test_debug_feature_file(data_dir, tmp_path):

assert "### GlyphsUnitTestSans-Regular" in features
assert "### GlyphsUnitTestSans-Black" in features


def test_ufo_to_static_otf_cff2(data_dir, tmp_path):
fontmake.__main__.main(
[
"-u",
str(data_dir / "DesignspaceTest" / "MyFont-Light.ufo"),
"-o",
"otf-cff2",
"--output-dir",
str(tmp_path),
]
)

assert {p.name for p in tmp_path.glob("*.otf")} == {"MyFont-Light.otf"}


def test_static_otf_cffsubr_subroutinizer(data_dir, tmp_path):
fontmake.__main__.main(
[
"-u",
str(data_dir / "DesignspaceTest" / "MyFont-Light.ufo"),
"-o",
"otf",
"--subroutinizer",
"cffsubr",
"--output-dir",
str(tmp_path),
]
)

assert {p.name for p in tmp_path.glob("*.otf")} == {"MyFont-Light.otf"}

0 comments on commit 37435a8

Please sign in to comment.