Last active
November 5, 2021 16:43
-
-
Save typoman/f64788ca409e06aa4630389777347b92 to your computer and use it in GitHub Desktop.
Get paths of included feature files in a ufo file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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