ttVisitor.py 1.0 KB

1234567891011121314151617181920212223242526272829303132
  1. """Specialization of fontTools.misc.visitor to work with TTFont."""
  2. from fontTools.misc.visitor import Visitor
  3. from fontTools.ttLib import TTFont
  4. class TTVisitor(Visitor):
  5. def visitAttr(self, obj, attr, value, *args, **kwargs):
  6. if isinstance(value, TTFont):
  7. return False
  8. super().visitAttr(obj, attr, value, *args, **kwargs)
  9. def visit(self, obj, *args, **kwargs):
  10. if hasattr(obj, "ensureDecompiled"):
  11. obj.ensureDecompiled(recurse=False)
  12. super().visit(obj, *args, **kwargs)
  13. @TTVisitor.register(TTFont)
  14. def visit(visitor, font, *args, **kwargs):
  15. # Some objects have links back to TTFont; even though we
  16. # have a check in visitAttr to stop them from recursing
  17. # onto TTFont, sometimes they still do, for example when
  18. # someone overrides visitAttr.
  19. if hasattr(visitor, "font"):
  20. return False
  21. visitor.font = font
  22. for tag in font.keys():
  23. visitor.visit(font[tag], *args, **kwargs)
  24. del visitor.font
  25. return False