• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from __future__ import print_function, division, absolute_import
2from fontTools.misc.py23 import *
3from fontTools.misc.xmlWriter import XMLWriter
4from fontTools.ttLib.tables.ttProgram import Program
5from fontTools.misc.textTools import deHexStr
6import array
7import os
8import re
9import unittest
10
11CURR_DIR = os.path.abspath(os.path.dirname(os.path.realpath(__file__)))
12DATA_DIR = os.path.join(CURR_DIR, 'data')
13
14TTPROGRAM_TTX = os.path.join(DATA_DIR, "ttProgram.ttx")
15#TTPROGRAM_BIN = os.path.join(DATA_DIR, "ttProgram.bin")
16
17BYTECODE = deHexStr(
18    '403b3a393837363534333231302f2e2d2c2b2a292827262524232221201f1e1d1c1b1a'
19    '191817161514131211100f0e0d0c0b0a090807060504030201002c01b0184358456ab0'
20    '194360b0462344231020b0464ef04d2fb000121b21231133592d2c01b0184358b0052b'
21    'b000134bb0145058b100403859b0062b1b21231133592d2c01b01843584eb0032510f2'
22    '21b000124d1b2045b00425b00425234a6164b0285258212310d61bb0032510f221b000'
23    '1259592d2cb01a435821211bb00225b0022549b00325b003254a612064b01050582121'
24    '211bb00325b0032549b0005058b0005058b8ffe238211bb0103821591bb0005258b01e'
25    '38211bb8fff03821595959592d2c01b0184358b0052bb000134bb0145058b90000ffc0'
26    '3859b0062b1b21231133592d2c4e018a10b146194344b00014b10046e2b00015b90000'
27    'fff03800b0003cb0282bb0022510b0003c2d2c0118b0002fb00114f2b00113b001154d'
28    'b000122d2c01b0184358b0052bb00013b90000ffe038b0062b1b21231133592d2c01b0'
29    '18435845646a23456469b01943646060b0462344231020b046f02fb000121b2121208a'
30    '208a525811331b212159592d2c01b10b0a432343650a2d2c00b10a0b4323430b2d2c00'
31    'b0462370b101463e01b0462370b10246453ab10200080d2d2cb0122bb0022545b00225'
32    '456ab0408b60b0022523442121212d2cb0132bb0022545b00225456ab8ffc08c60b002'
33    '2523442121212d2cb000b0122b2121212d2cb000b0132b2121212d2c01b00643b00743'
34    '650a2d2c2069b04061b0008b20b12cc08a8cb8100062602b0c642364615c58b0036159'
35    '2d2cb1000325456854b01c4b505a58b0032545b0032545606820b004252344b0042523'
36    '441bb00325204568208a2344b00325456860b003252344592d2cb00325204568208a23'
37    '44b003254564686560b00425b0016023442d2cb00943588721c01bb01243588745b011'
38    '2bb0472344b0477ae41b038a45186920b04723448a8a8720b0a05158b0112bb0472344'
39    'b0477ae41b21b0477ae4595959182d2c208a4523456860442d2c456a422d2c01182f2d'
40    '2c01b0184358b00425b00425496423456469b0408b6120b080626ab00225b00225618c'
41    'b0194360b0462344218a10b046f6211b21212121592d2c01b0184358b0022545b00225'
42    '4564606ab00325456a6120b00425456a208a8b65b0042523448cb00325234421211b20'
43    '456a4420456a44592d2c012045b00055b018435a584568234569b0408b6120b080626a'
44    '208a236120b003258b65b0042523448cb00325234421211b2121b0192b592d2c018a8a'
45    '45642345646164422d2cb00425b00425b0192bb0184358b00425b00425b00325b01b2b'
46    '01b0022543b04054b0022543b000545a58b003252045b040614459b0022543b00054b0'
47    '022543b040545a58b004252045b04060445959212121212d2c014b525843b002254523'
48    '61441b2121592d2c014b525843b00225452360441b2121592d2c4b525845441b212159'
49    '2d2c0120b003252349b04060b0206320b000525823b002253823b002256538008a6338'
50    '1b212121212159012d2c4b505845441b2121592d2c01b005251023208af500b0016023'
51    'edec2d2c01b005251023208af500b0016123edec2d2c01b0062510f500edec2d2c4623'
52    '46608a8a462320468a608a61b8ff8062232010238ab14b4b8a70456020b0005058b001'
53    '61b8ffba8b1bb0468c59b0106068013a2d2c2045b00325465258b0022546206861b003'
54    '25b003253f2321381b2111592d2c2045b00325465058b0022546206861b00325b00325'
55    '3f2321381b2111592d2c00b00743b006430b2d2c8a10ec2d2cb00c4358211b2046b000'
56    '5258b8fff0381bb0103859592d2c20b0005558b8100063b003254564b00325456461b0'
57    '005358b0021bb04061b00359254569535845441b2121591b21b0022545b00225456164'
58    'b028515845441b212159592d2c21210c6423648bb84000622d2c21b08051580c642364'
59    '8bb82000621bb200402f2b59b002602d2c21b0c051580c6423648bb81555621bb20080'
60    '2f2b59b002602d2c0c6423648bb84000626023212d2c4b5358b00425b0042549642345'
61    '6469b0408b6120b080626ab00225b00225618cb0462344218a10b046f6211b218a1123'
62    '1220392f592d2cb00225b002254964b0c05458b8fff838b008381b2121592d2cb01343'
63    '58031b02592d2cb0134358021b03592d2cb00a2b2310203cb0172b2d2cb00225b8fff0'
64    '38b0282b8a102320d023b0102bb0054358c01b3c59201011b00012012d2c4b53234b51'
65    '5a58381b2121592d2c01b0022510d023c901b00113b0001410b0013cb001162d2c01b0'
66    '0013b001b0032549b0031738b001132d2c4b53234b515a5820458a60441b2121592d2c'
67    '20392f2d')
68
69
70class TestFont(object):
71    disassembleInstructions = True
72
73
74class ProgramTest(unittest.TestCase):
75
76    def test__bool__(self):
77        p = Program()
78        assert not bool(p)
79
80        bc = array.array("B", [0])
81        p.fromBytecode(bc)
82        assert bool(p)
83
84        assert p.bytecode.pop() == 0
85        assert not bool(p)
86
87        p = Program()
88        asm = ['SVTCA[0]']
89        p.fromAssembly(asm)
90        assert bool(p)
91
92        assert p.assembly.pop() == 'SVTCA[0]'
93        assert not bool(p)
94
95    def test_roundtrip(self):
96        p = Program()
97        p.fromBytecode(BYTECODE)
98        asm = p.getAssembly(preserve=True)
99        p.fromAssembly(asm)
100        assert BYTECODE == p.getBytecode()
101
102    def test_xml_indentation(self):
103        with open(TTPROGRAM_TTX, 'r', encoding='utf-8') as f:
104            ttProgramXML = f.read()
105        p = Program()
106        p.fromBytecode(BYTECODE)
107        ttfont = TestFont()
108        buf = UnicodeIO()
109        writer = XMLWriter(buf, newlinestr='\n')
110        try:
111            p.toXML(writer, ttfont)
112        finally:
113            output_string = buf.getvalue()
114        assert output_string == ttProgramXML
115
116
117if __name__ == '__main__':
118    import sys
119    sys.exit(unittest.main())
120