aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/fonttools/fontTools/ttLib/tables/_f_p_g_m.py
blob: c21a9d4b6817d33bc68aabb8d866708657d1e068 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from . import DefaultTable
from . import ttProgram


class table__f_p_g_m(DefaultTable.DefaultTable):
    """Font Program table

    The ``fpgm`` table typically contains function defintions that are
    used by font instructions. This Font Program is similar to the Control
    Value Program that is stored in the ``prep`` table, but
    the ``fpgm`` table is only executed one time, when the font is first
    used.

    See also https://learn.microsoft.com/en-us/typography/opentype/spec/fpgm
    """

    def decompile(self, data, ttFont):
        program = ttProgram.Program()
        program.fromBytecode(data)
        self.program = program

    def compile(self, ttFont):
        if hasattr(self, "program"):
            return self.program.getBytecode()
        return b""

    def toXML(self, writer, ttFont):
        self.program.toXML(writer, ttFont)

    def fromXML(self, name, attrs, content, ttFont):
        program = ttProgram.Program()
        program.fromXML(name, attrs, content, ttFont)
        self.program = program

    def __bool__(self):
        """
        >>> fpgm = table__f_p_g_m()
        >>> bool(fpgm)
        False
        >>> p = ttProgram.Program()
        >>> fpgm.program = p
        >>> bool(fpgm)
        False
        >>> bc = bytearray([0])
        >>> p.fromBytecode(bc)
        >>> bool(fpgm)
        True
        >>> p.bytecode.pop()
        0
        >>> bool(fpgm)
        False
        """
        return hasattr(self, "program") and bool(self.program)

    __nonzero__ = __bool__


if __name__ == "__main__":
    import sys
    import doctest

    sys.exit(doctest.testmod().failed)