Skip to content

Instantly share code, notes, and snippets.

@typoman
Last active November 5, 2021 16:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save typoman/f64788ca409e06aa4630389777347b92 to your computer and use it in GitHub Desktop.
Save typoman/f64788ca409e06aa4630389777347b92 to your computer and use it in GitHub Desktop.
Get paths of included feature files in a ufo file.
from fontTools.feaLib.ast import IncludeStatement
from fontTools.feaLib.parser import Parser
from io import StringIO
import os
from pathlib import PurePath
"""
Get paths of included feature files in a ufo file.
This script makes an assumption feature are written inside the `ufo.features.text`.
Features could be written inside another file that could exist next to the UFO
package (instead of inside the UFO package). This script doesn't support that yet.
"""
def getIncludedFeautreFilesPaths(f, absolutePaths=True):
"""
If absoulutePaths is True, the abs path of the included files will be returned.
"""
ufoPath = f.path
ufoName = PurePath(ufoPath).stem
ufoRoot = PurePath(ufoPath).parent
featxt = f.features.text or ""
buf = StringIO(featxt)
buf.name = os.path.join(ufoPath, "features.fea")
parser = Parser(buf, set(f.keys()), followIncludes=False)
includeFiles = set()
for s in parser.parse().statements:
if type(s) is IncludeStatement:
path = os.path.join(ufoRoot, s.filename)
normalPath = os.path.normpath(path)
if os.path.exists(normalPath):
if absolutePaths:
includeFiles.add(normalPath)
else:
includeFiles.add(s.filename)
else:
print(f"{ufoName} | Feature file doesn't exist in:\n{normalPath}")
return includeFiles
if __name__ == '__main__':
# works in RF
f = CurrentFont()
for f in getIncludedFeautreFilesPaths(f):
print(f)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment