| 1 |
import logging |
| 2 |
|
| 3 |
from fontTools.feaLib import ast |
| 4 |
from fontTools.misc.visitor import Visitor |
| 5 |
|
| 6 |
from babelfont.Font import Font |
| 7 |
from babelfont.Glyph import GlyphList |
| 8 |
|
| 9 |
logger = logging.getLogger(__name__) |
| 10 |
|
| 11 |
|
| 12 |
def rename_glyphs(font: Font, args: dict = {}): |
| 13 |
msg = "Renaming glyphs" |
| 14 |
if "mapping" in args: |
| 15 |
mapping = args["mapping"] |
| 16 |
elif "production" in args: |
| 17 |
msg += " to production names" |
| 18 |
mapping = { |
| 19 |
glyph.name: glyph.production_name |
| 20 |
for glyph in font.glyphs |
| 21 |
if glyph.production_name is not None |
| 22 |
} |
| 23 |
else: |
| 24 |
raise ValueError( |
| 25 |
"rename_glyphs requires either a 'mapping' or 'production' argument" |
| 26 |
) |
| 27 |
logger.info(msg) |
| 28 |
|
| 29 |
# Step 1: features |
| 30 |
# 1a) classes |
| 31 |
for cls in font.features.classes.keys(): |
| 32 |
font.features.classes[cls] = [ |
| 33 |
mapping.get(glyph, glyph) for glyph in font.features.classes[cls] |
| 34 |
] |
| 35 |
# 1b) prefixes |
| 36 |
parsed_features = font.features.as_ast(font) |
| 37 |
for prefix in font.features.prefixes.keys(): |
| 38 |
font.features.prefixes[prefix] = _rename_fea( |
| 39 |
parsed_features["prefixes"][prefix], |
| 40 |
mapping, |
| 41 |
).asFea() |
| 42 |
# 1c) features |
| 43 |
font.features.features = [ |
| 44 |
( |
| 45 |
featurename, |
| 46 |
_drop_wrapper(_rename_fea(parsed_ast, mapping)).asFea(), |
| 47 |
) |
| 48 |
for featurename, parsed_ast in parsed_features["features"] |
| 49 |
] |
| 50 |
for glyph in font.glyphs: |
| 51 |
# Step 2: components |
| 52 |
for layer in glyph.layers: |
| 53 |
for component in layer.components: |
| 54 |
component.ref = mapping.get(component.ref, component.ref) |
| 55 |
# Step 3: glyphs |
| 56 |
glyph.name = mapping.get(glyph.name, glyph.name) |
| 57 |
font.glyphs = GlyphList({glyph.name: glyph for glyph in font.glyphs}) |
| 58 |
|
| 59 |
|
| 60 |
class FeaRenameVisitor(Visitor): |
| 61 |
def __init__(self, mapping): |
| 62 |
self.mapping = mapping |
| 63 |
|
| 64 |
|
| 65 |
@FeaRenameVisitor.register(ast.GlyphName) |
| 66 |
def visit(visitor, gn, *args, **kwargs): |
| 67 |
gn.glyph = visitor.mapping.get(gn.glyph, gn.glyph) |
| 68 |
return False |
| 69 |
|
| 70 |
|
| 71 |
@FeaRenameVisitor.register(ast.GlyphClass) |
| 72 |
def visit(visitor, gcd, *args, **kwargs): |
| 73 |
gcd.glyphs = [visitor.mapping.get(glyph, glyph) for glyph in gcd.glyphs] |
| 74 |
return False |
| 75 |
|
| 76 |
|
| 77 |
@FeaRenameVisitor.register(ast.LigatureSubstStatement) |
| 78 |
def visit(visitor, ligature, *args, **kwargs): |
| 79 |
ligature.replacement = visitor.mapping.get( |
| 80 |
ligature.replacement, ligature.replacement |
| 81 |
) |
| 82 |
return True |
| 83 |
|
| 84 |
|
| 85 |
@FeaRenameVisitor.register(ast.SinglePosStatement) |
| 86 |
def visit(visitor, ccp, *args, **kwargs): |
| 87 |
for glyphclass, _valuerecord in ccp.pos: |
| 88 |
visitor.visit(glyphclass) |
| 89 |
return True |
| 90 |
|
| 91 |
|
| 92 |
@FeaRenameVisitor.register(ast.MarkClassDefinition) |
| 93 |
def visit(visitor, mcd, *args, **kwargs): |
| 94 |
visitor.visitAttr(mcd, "anchor", mcd.anchor, *args, **kwargs) |
| 95 |
visitor.visitAttr(mcd, "glyphs", mcd.glyphs, *args, **kwargs) |
| 96 |
return False |
| 97 |
|
| 98 |
|
| 99 |
def _rename_fea(parsed: ast.Block, mapping, wrap=None): |
| 100 |
FeaRenameVisitor(mapping).visit(parsed) |
| 101 |
return parsed |
| 102 |
|
| 103 |
|
| 104 |
def _drop_wrapper(parsed: ast.FeatureFile): |
| 105 |
block = ast.Block() |
| 106 |
block.statements = parsed.statements[0].statements |
| 107 |
return block |