Skip to content

Commit

Permalink
Merge pull request #297 from robotools/glyphSet_reset_on_save_from_zi…
Browse files Browse the repository at this point in the history
…pped_to_UFO2

Glyph set reset on save from zipped to ufo2
  • Loading branch information
typemytype committed Jun 16, 2020
2 parents 76a7ac4 + e87cf3d commit da45253
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 5 deletions.
22 changes: 17 additions & 5 deletions Lib/defcon/objects/layerSet.py
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,7 @@ def save(self, writer, saveAs=False, progressBar=None):

def _fontSaveWasCompleted(self):
"""
When saving a UFOZ, the underlying ZipFS object is closed.
On saving a UFO, the underlying fs object is closed.
The objects then stored in layer._glyphSet contain references
to a closed, and therefore unusable, filesystem. To remedy this,
after the save is completed this method will be called and new
Expand All @@ -333,10 +333,22 @@ def _fontSaveWasCompleted(self):
font = self.font
font.close()
font._reader = reader = UFOReader(font.path, validate=font.ufoLibReadValidate)
if reader.fileStructure is UFOFileStructure.ZIP:
for layerName in self.layerOrder:
layer = self[layerName]
layer._glyphSet = reader.getGlyphSet(layerName=layerName, validateRead=self.ufoLibReadValidate)
defaultLayerName = self._defaultLayerName
for layerName in self.layerOrder:
layer = self[layerName]
if reader.formatVersionTuple < (3, 0):
# ufo2 or less has not layers
# map only to the default layer to the ufo2 glyphSet
# set all all other layers internal _glyphSet object to None
if defaultLayerName == layerName:
glyphSet = reader.getGlyphSet(validateRead=self.ufoLibReadValidate)
else:
glyphSet = None
else:
# we do have layers and the must be available after a save action
glyphSet = reader.getGlyphSet(layerName=layerName, validateRead=self.ufoLibReadValidate)

layer._glyphSet = glyphSet

# ------------------------
# Notification Observation
Expand Down
14 changes: 14 additions & 0 deletions Lib/defcon/test/objects/test_font.py
Original file line number Diff line number Diff line change
Expand Up @@ -575,6 +575,20 @@ def test_save_in_place_invalid_ufo(self):
_ = font.layers
font.save()

def test_save_ufo3z_as_ufo2(self):
# https://github.com/robotools/defcon/issues/296
font = Font()
font.layers.defaultLayer.name = "a-custom-layer-name"
font.newLayer("a-new-layer")

with tempfile.TemporaryDirectory() as root:
path_ufo3 = os.path.join(root, "three.ufo")
font.save(path_ufo3, formatVersion=3, structure="zip")

path_ufo2 = os.path.join(root, "two.ufo")
font_ufo2 = Font(path_ufo3)
font_ufo2.save(path_ufo2, formatVersion=2)

def test_testForExternalChanges(self):
for ufo in (u"TestExternalEditing.ufo", u"TestExternalEditing.ufoz"):
path = getTestFontPath(ufo)
Expand Down

0 comments on commit da45253

Please sign in to comment.