Skip to content

Commit

Permalink
have --output-path create parent directory
Browse files Browse the repository at this point in the history
Fixes #847
  • Loading branch information
anthrotype committed Jul 26, 2023
1 parent b61c1f6 commit 9851713
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
21 changes: 14 additions & 7 deletions Lib/fontmake/font_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ def open_ufo(self, path):
def save_ufo_as(self, font, path, ufo_structure="package"):
try:
font.save(
path,
_ensure_parent_dir(path),
overwrite=True,
validate=self.validate_ufo,
structure=ufo_structure,
Expand Down Expand Up @@ -413,7 +413,7 @@ def build_variable_fonts(
for name, font in fonts.items():
output_path = vf_name_to_output_path[name]
logger.info("Saving %s", output_path)
font.save(output_path)
font.save(_ensure_parent_dir(output_path))

def _iter_compile(self, ufos, ttf=False, debugFeatureFile=None, **kwargs):
# generator function that calls ufo2ft compiler for each ufo and
Expand Down Expand Up @@ -639,7 +639,7 @@ def save_otfs(
otf_path = output_path

logger.info("Saving %s", otf_path)
font.save(otf_path)
font.save(_ensure_parent_dir(otf_path))

# 'subset' is an Optional[bool], can be None, True or False.
# When False, we never subset; when True, we always do; when
Expand All @@ -660,7 +660,11 @@ def save_otfs(
)
try:
logger.info("Autohinting %s", hinted_otf_path)
ttfautohint(otf_path, hinted_otf_path, args=autohint_thisfont)
ttfautohint(
otf_path,
_ensure_parent_dir(hinted_otf_path),
args=autohint_thisfont,
)
except TTFAError:
# copy unhinted font to destination before re-raising error
shutil.copyfile(otf_path, hinted_otf_path)
Expand All @@ -682,7 +686,7 @@ def _save_interpolatable_fonts(self, designspace, output_dir, ttf):
suffix=source.layerName,
)
logger.info("Saving %s", otf_path)
source.font.save(otf_path)
source.font.save(_ensure_parent_dir(otf_path))
source.path = otf_path
source.layerName = None
for instance in designspace.instances:
Expand Down Expand Up @@ -1265,8 +1269,6 @@ def _output_path(
output_dir = self._output_dir(
ext, is_instance, interpolatable, autohinted, is_variable
)
if not os.path.exists(output_dir):
os.makedirs(output_dir)

if suffix:
return os.path.join(output_dir, f"{font_name}-{suffix}.{ext}")
Expand Down Expand Up @@ -1314,3 +1316,8 @@ def _varLib_finder(source, directory="", ext="ttf"):

def _normpath(fname):
return os.path.normcase(os.path.normpath(fname))


def _ensure_parent_dir(path):
Path(path).parent.mkdir(parents=True, exist_ok=True)
return path
5 changes: 3 additions & 2 deletions tests/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -1049,11 +1049,12 @@ def test_main_designspace_v5_can_use_output_path_with_1_vf(data_dir, tmp_path):
"--variable-fonts",
"MutatorSansVariable_Width",
"--output-path",
str(tmp_path / "MySingleVF.ttf"),
str(tmp_path / "output" / "MySingleVF.ttf"),
]
)

assert (tmp_path / "MySingleVF.ttf").exists()
# 'output' subfolder was created automatically
assert (tmp_path / "output" / "MySingleVF.ttf").exists()


def test_main_designspace_v5_dont_interpolate_discrete_axis(data_dir, tmp_path):
Expand Down

0 comments on commit 9851713

Please sign in to comment.