Close source TTFont after instantiateVariableFont rebind
instantiateVariableFont with default inplace=False returns a *new* TTFont; rebinding `font` strands the source TTFont and its file handle open until GC. The trailing font.close() then closes only the new pinned-instance object. Capture the source as a separate reference and close both with nested try/finally: inner finally closes the static instance after save (or on save failure), outer finally closes the source unconditionally.
This commit is contained in:
@@ -81,14 +81,22 @@ def extract_static_instance(source_path: Path, axes: dict, family_name: str, sty
|
|||||||
old.unlink()
|
old.unlink()
|
||||||
|
|
||||||
print(f" Extracting static instance: {family_name}/{style_name} ({axis_key})")
|
print(f" Extracting static instance: {family_name}/{style_name} ({axis_key})")
|
||||||
font = TTFont(str(source_path))
|
# Keep separate handles for the source variable font and the static
|
||||||
# `static` was removed (or never existed) in current fontTools; it raises
|
# instance: instantiateVariableFont with default inplace=False returns a
|
||||||
# TypeError under recent releases. updateFontNames=True keeps the saved
|
# *new* TTFont, so rebinding `font` would otherwise strand the source's
|
||||||
# name table accurate; optimize=False skips a gvar pass that's wasted
|
# file handle open until GC runs.
|
||||||
# when every axis is fully pinned.
|
#
|
||||||
font = instantiateVariableFont(font, axes, updateFontNames=True, optimize=False)
|
# updateFontNames=True keeps the saved name table accurate; optimize=False
|
||||||
font.save(str(cached))
|
# skips a gvar pass that's wasted when every axis is fully pinned.
|
||||||
font.close()
|
source_font = TTFont(str(source_path))
|
||||||
|
try:
|
||||||
|
font = instantiateVariableFont(source_font, axes, updateFontNames=True, optimize=False)
|
||||||
|
try:
|
||||||
|
font.save(str(cached))
|
||||||
|
finally:
|
||||||
|
font.close()
|
||||||
|
finally:
|
||||||
|
source_font.close()
|
||||||
|
|
||||||
return cached
|
return cached
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user