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

« back to all changes in this revision

Viewing changes to Lib/fontTools/ttLib/tables/_h_d_m_x.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
import DefaultTable
 
2
import sstruct
 
3
import string
 
4
 
 
5
hdmxHeaderFormat = """
 
6
        >   # big endian!
 
7
        version:        H
 
8
        numRecords:     H
 
9
        recordSize:     l
 
10
"""
 
11
 
 
12
class table__h_d_m_x(DefaultTable.DefaultTable):
 
13
        
 
14
        def decompile(self, data, ttFont):
 
15
                numGlyphs = ttFont['maxp'].numGlyphs
 
16
                glyphOrder = ttFont.getGlyphOrder()
 
17
                dummy, data = sstruct.unpack2(hdmxHeaderFormat, data, self)
 
18
                self.hdmx = {}
 
19
                for i in range(self.numRecords):
 
20
                        ppem = ord(data[0])
 
21
                        maxSize = ord(data[1])
 
22
                        widths = {}
 
23
                        for glyphID in range(numGlyphs):
 
24
                                widths[glyphOrder[glyphID]] = ord(data[glyphID+2])
 
25
                        self.hdmx[ppem] = widths
 
26
                        data = data[self.recordSize:]
 
27
                assert len(data) == 0, "too much hdmx data"
 
28
        
 
29
        def compile(self, ttFont):
 
30
                self.version = 0
 
31
                numGlyphs = ttFont['maxp'].numGlyphs
 
32
                glyphOrder = ttFont.getGlyphOrder()
 
33
                self.recordSize = 4 * ((2 + numGlyphs + 3) / 4)
 
34
                pad = (self.recordSize - 2 - numGlyphs) * "\0"
 
35
                self.numRecords = len(self.hdmx)
 
36
                data = sstruct.pack(hdmxHeaderFormat, self)
 
37
                items = self.hdmx.items()
 
38
                items.sort()
 
39
                for ppem, widths in items:
 
40
                        data = data + chr(ppem) + chr(max(widths.values()))
 
41
                        for glyphID in range(len(glyphOrder)):
 
42
                                width = widths[glyphOrder[glyphID]]
 
43
                                data = data + chr(width)
 
44
                        data = data + pad
 
45
                return data
 
46
        
 
47
        def toXML(self, writer, ttFont):
 
48
                writer.begintag("hdmxData")
 
49
                writer.newline()
 
50
                ppems = self.hdmx.keys()
 
51
                ppems.sort()
 
52
                records = []
 
53
                format = ""
 
54
                for ppem in ppems:
 
55
                        widths = self.hdmx[ppem]
 
56
                        records.append(widths)
 
57
                        format = format + "%4d"
 
58
                glyphNames = ttFont.getGlyphOrder()[:]
 
59
                glyphNames.sort()
 
60
                maxNameLen = max(map(len, glyphNames))
 
61
                format = "%" + `maxNameLen` + 's:' + format + ' ;'
 
62
                writer.write(format % (("ppem",) + tuple(ppems)))
 
63
                writer.newline()
 
64
                writer.newline()
 
65
                for glyphName in glyphNames:
 
66
                        row = []
 
67
                        for ppem in ppems:
 
68
                                widths = self.hdmx[ppem]
 
69
                                row.append(widths[glyphName])
 
70
                        if ";" in glyphName:
 
71
                                glyphName = "\\x3b".join(glyphName.split(";"))
 
72
                        writer.write(format % ((glyphName,) + tuple(row)))
 
73
                        writer.newline()
 
74
                writer.endtag("hdmxData")
 
75
                writer.newline()
 
76
        
 
77
        def fromXML(self, (name, attrs, content), ttFont):
 
78
                if name <> "hdmxData":
 
79
                        return
 
80
                content = string.join(content, "")
 
81
                lines = string.split(content, ";")
 
82
                topRow = string.split(lines[0])
 
83
                assert topRow[0] == "ppem:", "illegal hdmx format"
 
84
                ppems = map(int, topRow[1:])
 
85
                self.hdmx = hdmx = {}
 
86
                for ppem in ppems:
 
87
                        hdmx[ppem] = {}
 
88
                lines = map(string.split, lines[1:])
 
89
                for line in lines:
 
90
                        if not line:
 
91
                                continue
 
92
                        assert line[0][-1] == ":", "illegal hdmx format"
 
93
                        glyphName = line[0][:-1]
 
94
                        if "\\" in glyphName:
 
95
                                from fontTools.misc.textTools import safeEval
 
96
                                glyphName = safeEval('"""' + glyphName + '"""')
 
97
                        line = map(int, line[1:])
 
98
                        assert len(line) == len(ppems), "illegal hdmx format"
 
99
                        for i in range(len(ppems)):
 
100
                                hdmx[ppems[i]][glyphName] = line[i]
 
101