Debian Codegraph @

Doc/source/misc/visitor.rst

raw
1 ####################################################
2 visitor: Tools for traversing nested data structures
3 ####################################################
4
5 .. contents:: On this page:
6 :local:
7
8 .. rubric:: Overview
9 :heading-level: 2
10
11 The :mod:`.visitor` module provides an extensible `visitor pattern`_, which can
12 be used for traversing, inspecting, and editing nested Python data structures.
13
14 The utility class :class:`Visitor <fontTools.misc.visitor.Visitor>`
15 automatically handles recursion into most common data types, which allows for
16 user code to skip boilerplate and focus on operation logic. This recursion logic
17 is documented in full under the :func:`visit() <fontTools.misc.visitor.Visitor>`
18 function.
19
20 Commonly targeted data structures in font projects and ``fonttools`` include:
21
22 * :class:`TTFont <fontTools.ttLib.ttFont.TTFont>`: Compiled font files
23 * :class:`FeatureFile <fontTools.feaLib.ast.FeatureFile>`: Feature code syntax trees
24
25 Commonly implemented operations include:
26
27 * Summarizing
28 * Subsetting
29 * Scaling
30
31 .. _visitor pattern: https://en.wikipedia.org/wiki/Visitor_pattern
32
33
34 Specializations
35 ---------------
36
37 The :mod:`ttLib.ttVisitor <fontTools.ttLib.ttVisitor>` module provides the
38 :class:`TTVisitor <fontTools.ttLib.ttVisitor.TTVisitor>` class, which handles
39 common edge cases when using visitors with :class:`TTFont
40 <fontTools.ttLib.ttFont.TTFont>` objects. For this reason, it should be
41 preferred in that scenario.
42
43
44 Guide
45 -----
46
47 1. Create a new class extending :class:`Visitor <fontTools.misc.visitor.Visitor>`.
48 2. Register operations for specific types or attributes with the *register* annotations.
49 3. Instantiate the class and call :func:`visit() <fontTools.misc.visitor.Visitor>` on the target object.
50
51
52 Example code
53 ^^^^^^^^^^^^
54
55 One can create a visitor class that checks the case of feature names::
56
57 >>> from fontTools.feaLib.ast import FeatureNameStatement
58 >>> from fontTools.misc.visitor import Visitor
59 >>>
60 >>> class SpellCheckVisitor(Visitor):
61 ... found: list[FeatureNameStatement]
62 ...
63 ... def __init__(self):
64 ... self.found = []
65 >>>
66 >>> @SpellCheckVisitor.register(FeatureNameStatement)
67 ... def visit(visitor: SpellCheckVisitor, statement: FeatureNameStatement):
68 ... # Find name statements that are not in title case.
69 ... if statement.string != statement.string.title():
70 ... visitor.found.append(statement)
71
72 This can then be applied on a feature file object::
73
74 >>> io = StringIO("""
75 ... feature ss01 {
76 ... featureNames {
77 ... name "Monolinear Grotesque";
78 ... };
79 ... } ss01;
80 ...
81 ... feature ss02 {
82 ... featureNames {
83 ... name "Optical Apertures";
84 ... };
85 ... } ss02;
86 ...
87 ... feature ss03 {
88 ... featureNames {
89 ... name "slanted Humanism";
90 ... };
91 ... } ss03;
92 ... """)
93 >>> fea = Parser(io).parse()
94 >>>
95 >>> visitor = SpellCheckVisitor()
96 >>> visitor.visit(fea)
97 >>>
98 >>> for statement in visitor.found:
99 ... print(
100 ... "Found feature name that was not in title case: "
101 ... f"'{statement.string}' at location '{statement.location}'",
102 ... )
103 Found feature name that was not in title case: 'slanted Humanism' at location '<features>:16:13'
104
105
106 Package contents
107 ----------------
108
109 .. automodule:: fontTools.misc.visitor
110 :members:
111 :undoc-members: