Last active
November 13, 2025 12:53
-
-
Save MihailJP/4a73f6b1c71e7a990a5df33fc006cb8a to your computer and use it in GitHub Desktop.
Fontforge Python script to decompose nested references into unnested ones
This file contains hidden or 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
| #!/usr/bin/env fontforge | |
| import fontforge, psMat | |
| def decomposeNestedRefs(font): | |
| """Decomposes nested references into simple unnested ones within a font. | |
| Nested references are known to cause problems in certain environments. | |
| This function recursively decomposes such references into single-level ones.""" | |
| while True: | |
| nestedRefsFound = False | |
| for glyph in font.glyphs(): | |
| decomposedRef = [] | |
| decomposedLayers = [] | |
| for ref in glyph.references: | |
| (srcglyph, matrix, _) = ref | |
| if len(font[srcglyph].references) > 0: | |
| fontforge.logWarning("Glyph " + glyph.glyphname + " has a nested reference to " + srcglyph) | |
| for srcref in font[srcglyph].references: | |
| decomposedRef += [(srcref[0], psMat.compose(srcref[1], matrix), False)] | |
| for layer in range(0, font[srcglyph].layer_cnt): | |
| glyph.layers[layer] += font[srcglyph].layers[layer].dup().transform(matrix) | |
| nestedRefsFound = True | |
| else: | |
| decomposedRef += [ref] | |
| glyph.references = tuple(decomposedRef) | |
| if not nestedRefsFound: | |
| break |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment