~ubuntu-branches/ubuntu/feisty/fonttools/feisty

« back to all changes in this revision

Viewing changes to MetaTools/build_otData.py

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Fok
  • Date: 2003-11-18 00:53:59 UTC
  • Revision ID: james.westby@ubuntu.com-20031118005359-pqirsxbgdz0f0xmx
Tags: upstream-1.99+2.0b1+cvs20031014
ImportĀ upstreamĀ versionĀ 1.99+2.0b1+cvs20031014

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#! /usr/bin/env python
 
2
 
 
3
 
 
4
"""This script builds the Lib/fontTools/ttLib/tables/otData.py file
 
5
from the OpenType HTML documentation. However, it depends on a slightly
 
6
patched version the the HTML, as there are some inconsistencies in the
 
7
markup and the naming of certain fields. See doco.diff for differences,
 
8
but this is probably against a slightly older version of the documentation
 
9
than what is currently online. The documentation was taken from this URL:
 
10
        http://www.microsoft.com/typography/otspec/default.htm
 
11
"""
 
12
 
 
13
 
 
14
from sgmllib import SGMLParser
 
15
 
 
16
 
 
17
class HTMLParser(SGMLParser):
 
18
        
 
19
        def __init__(self):
 
20
                SGMLParser.__init__(self)
 
21
                self.data = None
 
22
                self.currenttable = None
 
23
                self.lastcaption = None
 
24
        
 
25
        def handle_data(self, data):
 
26
                if self.data is not None:
 
27
                        self.data.append(data)
 
28
        
 
29
        def start_i(self, attrs):
 
30
                if self.currenttable is None:
 
31
                        self.data = []
 
32
        def end_i(self):
 
33
                if self.currenttable is None:
 
34
                        self.lastcaption = " ".join(self.data)
 
35
                        self.data = None
 
36
        
 
37
        def start_b(self, attrs):
 
38
                if self.currenttable is None:
 
39
                        self.data = []
 
40
        def end_b(self):
 
41
                if self.currenttable is None:
 
42
                        self.lastcaption = " ".join(self.data)
 
43
                        self.data = None
 
44
        
 
45
        def start_table(self, attrs):
 
46
                attrs = dict(attrs)
 
47
                if attrs.get('width') in ('455', '460'):
 
48
                        #print "---", attrs
 
49
                        self.currenttable = []
 
50
                else:
 
51
                        self.currenttable = None
 
52
        def end_table(self):
 
53
                if self.currenttable is not None and self.lastcaption is not None:
 
54
                        if self.currenttable[0] == ['Type', 'Name', 'Description'] or \
 
55
                                        self.currenttable[0] == ['Value', 'Type', 'Description']:
 
56
                                caption = self.lastcaption.split()
 
57
                                name = caption[0]
 
58
                                if name == "LookupType" or name == "LookupFlag":
 
59
                                        self.currenttable = None
 
60
                                        return
 
61
                                elif name == "Device":
 
62
                                        if "Tables" in caption:
 
63
                                                # XXX skip this one
 
64
                                                self.currenttable = None
 
65
                                                return
 
66
                                buildTable(name, self.currenttable[1:], self.lastcaption)
 
67
                self.currenttable = None
 
68
        
 
69
        def start_tr(self, attrs):
 
70
                if self.currenttable is not None:
 
71
                        self.currenttable.append([])
 
72
        def end_tr(self):
 
73
                pass
 
74
        
 
75
        def start_td(self, attrs):
 
76
                self.data = []
 
77
        def end_td(self):
 
78
                if self.currenttable is not None and self.data is not None:
 
79
                        self.currenttable[-1].append(" ".join(self.data))
 
80
                        self.data = None
 
81
 
 
82
 
 
83
globalDups = {}
 
84
localDups = {}
 
85
not3 = []
 
86
 
 
87
def buildTable(name, table, caption):
 
88
        if globalDups.has_key(name):
 
89
                globalDups[name].append(caption)
 
90
        else:
 
91
                globalDups[name] = [caption]
 
92
        print "\t(%s, [" % repr(name)
 
93
        allFields = {}
 
94
        for row in table:
 
95
                row = [" ".join(x.split()) for x in row]
 
96
                if len(row) <> 3:
 
97
                        not3.append(row)
 
98
                row = makeRow(row)
 
99
                fieldName = row[1]
 
100
                if allFields.has_key(fieldName):
 
101
                        key = (name, fieldName)
 
102
                        localDups[key] = 1
 
103
                allFields[fieldName] = 1
 
104
                print "\t\t%s," % (tuple(row),)
 
105
        print "\t]),"
 
106
        print
 
107
 
 
108
 
 
109
def makeRow(rawRow):
 
110
        tp, name = rawRow[:2]
 
111
        name = name.strip()
 
112
        rest = tuple(rawRow[2:])
 
113
        if '[' in name:
 
114
                name, repeat = name.split("[")
 
115
                name = name.strip()
 
116
                assert repeat[-1] == "]"
 
117
                repeat = repeat[:-1].split()
 
118
                if repeat[1:]:
 
119
                        repeatOffset = int("".join(repeat[1:]))
 
120
                else:
 
121
                        repeatOffset = 0
 
122
                if not repeat:
 
123
                        repeat = ""
 
124
                else:
 
125
                        repeat = repeat[0]
 
126
        else:
 
127
                repeat = None
 
128
                repeatOffset = None
 
129
        row = (tp, name, repeat, repeatOffset) + rest
 
130
        return row
 
131
 
 
132
 
 
133
if __name__ == "__main__":
 
134
        import sys, os
 
135
        if "-" not in sys.argv:
 
136
                sys.stdout = open("otData.py", "w")
 
137
        print "otData = ["
 
138
        for file in ["chapter2.htm", "gpos.htm", "gsub.htm", "gdef.htm", "base.htm", "jstf.htm"]:
 
139
                name = os.path.splitext(file)[0]
 
140
                if name == "chapter2":
 
141
                        name = "common"
 
142
                print
 
143
                print "\t#"
 
144
                print "\t# %s (generated from %s)" % (name, file)
 
145
                print "\t#"
 
146
                print 
 
147
                p = HTMLParser()
 
148
                p.feed(open(file).read())
 
149
                p.close()
 
150
        print "]"
 
151
        print
 
152
        for k, v in globalDups.items():
 
153
                if len(v) > 1:
 
154
                        print "# XXX duplicate table name:", k, v
 
155
        for (name, fieldName), v in localDups.items():
 
156
                print "# XXX duplicate field name '%s' in table '%s'" % (fieldName, name)
 
157
        for n in not3:
 
158
                print "#XXX", not3
 
159