Skip to content

Commit

Permalink
fix master ufo paths' file extension if saving as ufoz
Browse files Browse the repository at this point in the history
plus, make sure when we log 'Saving Font.ufoz' then the file extension actually matches what we are saving on disk
  • Loading branch information
anthrotype committed Jul 27, 2023
1 parent adf543b commit df71fa3
Showing 1 changed file with 24 additions and 7 deletions.
31 changes: 24 additions & 7 deletions Lib/fontmake/font_project.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,12 @@
INSTANCE_FILENAME_KEY = "com.github.googlefonts.fontmake.instance_filename"


UFO_STRUCTURE_EXTENSIONS = {
"package": ".ufo",
"zip": ".ufoz",
}


def needs_subsetting(ufo):
if KEEP_GLYPHS_OLD_KEY in ufo.lib or KEEP_GLYPHS_NEW_KEY in ufo.lib:
return True
Expand Down Expand Up @@ -126,6 +132,13 @@ def open_ufo(self, path):
except Exception as e:
raise FontmakeError("Reading UFO source failed", path) from e

@staticmethod
def _fix_ufo_path(path, ufo_structure):
"""Normalizes UFO path and updates extension suffix to match its structure"""
return os.path.normpath(
Path(path).with_suffix(UFO_STRUCTURE_EXTENSIONS[ufo_structure])
)

def save_ufo_as(self, font, path, ufo_structure="package"):
try:
font.save(
Expand Down Expand Up @@ -198,7 +211,9 @@ def build_master_ufos(
# multiple sources can have the same font/filename (but different layer),
# we want to save a font only once
for source in designspace.sources:
ufo_path = os.path.join(master_dir, source.filename)
ufo_path = self._fix_ufo_path(
os.path.join(master_dir, source.filename), ufo_structure
)
source.path = ufo_path
# relative 'filename' would be auto-updated upon writing the designspace
# but we may not always write one out, thus we keep it up-to-date
Expand Down Expand Up @@ -851,7 +866,7 @@ def run_from_glyphs(
raise

def _instance_ufo_path(
self, instance, designspace_path, output_dir=None, ext=".ufo"
self, instance, designspace_path, output_dir=None, ufo_structure="package"
):
"""Return an instance path, optionally overriding output dir or extension"""
# prefer absolute instance.path over relative instance.filename
Expand All @@ -874,8 +889,8 @@ def _instance_ufo_path(
if instance_dir is None:
instance_dir = self._output_dir("ufo", is_instance=True)

instance_path = Path(instance_dir) / f"{Path(instance_path).stem}{ext}"
return os.path.normpath(instance_path)
instance_path = Path(instance_dir) / Path(instance_path).name
return self._fix_ufo_path(instance_path, ufo_structure)

def interpolate_instance_ufos(
self,
Expand Down Expand Up @@ -958,16 +973,18 @@ def interpolate_instance_ufos(
apply_instance_data_to_ufo(instance.font, instance, subDoc)

if save_ufos:
ext = ".ufoz" if ufo_structure == "zip" else ".ufo"
if output_path is not None:
# we don't know in advance how many instances we will generate
# (depends on splitInterpolable and include filter); if we
# overwrite or stop in the middle of the build it'd be worse,
# so we make the output_path unique using #1, #2, etc. suffix
instance_path = makeOutputFileName(output_path, extension=ext)
instance_path = makeOutputFileName(
output_path,
extension=UFO_STRUCTURE_EXTENSIONS[ufo_structure],
)
else:
instance_path = self._instance_ufo_path(
instance, designspace.path, output_dir, ext
instance, designspace.path, output_dir, ufo_structure
)
logger.info("Saving %s", instance_path)
self.save_ufo_as(instance.font, instance_path, ufo_structure)
Expand Down

0 comments on commit df71fa3

Please sign in to comment.