Applied upstream as r3028 r3025 r3024
[PyX/mjg.git] / pyx / dvi / tfmfile.py
blobed0d4834057afb01038e0b73aba1470024baae67
1 from pyx import reader
3 class char_info_word:
5 def __init__(self, word):
6 self.width_index = int((word & 0xFF000000L) >> 24) #make sign-safe
7 self.height_index = (word & 0x00F00000) >> 20
8 self.depth_index = (word & 0x000F0000) >> 16
9 self.italic_index = (word & 0x0000FC00) >> 10
10 self.tag = (word & 0x00000300) >> 8
11 self.remainder = (word & 0x000000FF)
14 class TFMfile:
16 def __init__(self, name, debug=0):
17 self.file = reader.reader(name)
20 # read pre header
23 self.lf = self.file.readint16()
24 self.lh = self.file.readint16()
25 self.bc = self.file.readint16()
26 self.ec = self.file.readint16()
27 self.nw = self.file.readint16()
28 self.nh = self.file.readint16()
29 self.nd = self.file.readint16()
30 self.ni = self.file.readint16()
31 self.nl = self.file.readint16()
32 self.nk = self.file.readint16()
33 self.ne = self.file.readint16()
34 self.np = self.file.readint16()
36 if not (self.bc-1 <= self.ec <= 255 and
37 self.ne <= 256 and
38 self.lf == 6+self.lh+(self.ec-self.bc+1)+self.nw+self.nh+self.nd
39 +self.ni+self.nl+self.nk+self.ne+self.np):
40 raise RuntimeError("error in TFM pre-header")
42 if debug:
43 print "lh=%d" % self.lh
46 # read header
49 self.checksum = self.file.readint32()
50 self.designsize = self.file.readint32()
51 assert self.designsize > 0, "invald design size"
52 if self.lh > 2:
53 assert self.lh > 11, "inconsistency in TFM file: incomplete field"
54 self.charcoding = self.file.readstring(40)
55 else:
56 self.charcoding = None
58 if self.lh > 12:
59 assert self.lh > 16, "inconsistency in TFM file: incomplete field"
60 self.fontfamily = self.file.readstring(20)
61 else:
62 self.fontfamily = None
64 if debug:
65 print "(FAMILY %s)" % self.fontfamily
66 print "(CODINGSCHEME %s)" % self.charcoding
67 print "(DESINGSIZE R %f)" % (16.0*self.designsize/16777216L)
69 if self.lh > 17:
70 self.sevenbitsave = self.file.readuchar()
71 # ignore the following two bytes
72 self.file.readint16()
73 facechar = self.file.readuchar()
74 # decode ugly face specification into the Knuth suggested string
75 if facechar < 18:
76 if facechar >= 12:
77 self.face = "E"
78 facechar -= 12
79 elif facechar >= 6:
80 self.face = "C"
81 facechar -= 6
82 else:
83 self.face = "R"
85 if facechar >= 4:
86 self.face = "L" + self.face
87 facechar -= 4
88 elif facechar >= 2:
89 self.face = "B" + self.face
90 facechar -= 2
91 else:
92 self.face = "M" + self.face
94 if facechar == 1:
95 self.face = self.face[0] + "I" + self.face[1]
96 else:
97 self.face = self.face[0] + "R" + self.face[1]
99 else:
100 self.face = None
101 else:
102 self.sevenbitsave = self.face = None
104 if self.lh > 18:
105 # just ignore the rest
106 print self.file.read((self.lh-18)*4)
109 # read char_info
112 self.char_info = [None]*(self.ec+1)
113 for charcode in range(self.bc, self.ec+1):
114 self.char_info[charcode] = char_info_word(self.file.readint32())
115 if self.char_info[charcode].width_index == 0:
116 # disable character if width_index is zero
117 self.char_info[charcode] = None
120 # read widths
123 self.width = [None for width_index in range(self.nw)]
124 for width_index in range(self.nw):
125 self.width[width_index] = self.file.readint32()
128 # read heights
131 self.height = [None for height_index in range(self.nh)]
132 for height_index in range(self.nh):
133 self.height[height_index] = self.file.readint32()
136 # read depths
139 self.depth = [None for depth_index in range(self.nd)]
140 for depth_index in range(self.nd):
141 self.depth[depth_index] = self.file.readint32()
144 # read italic
147 self.italic = [None for italic_index in range(self.ni)]
148 for italic_index in range(self.ni):
149 self.italic[italic_index] = self.file.readint32()
152 # read lig_kern
155 # XXX decode to lig_kern_command
157 self.lig_kern = [None for lig_kern_index in range(self.nl)]
158 for lig_kern_index in range(self.nl):
159 self.lig_kern[lig_kern_index] = self.file.readint32()
162 # read kern
165 self.kern = [None for kern_index in range(self.nk)]
166 for kern_index in range(self.nk):
167 self.kern[kern_index] = self.file.readint32()
170 # read exten
173 # XXX decode to extensible_recipe
175 self.exten = [None for exten_index in range(self.ne)]
176 for exten_index in range(self.ne):
177 self.exten[exten_index] = self.file.readint32()
180 # read param
183 # XXX decode
185 self.param = [None for param_index in range(self.np)]
186 for param_index in range(self.np):
187 self.param[param_index] = self.file.readint32()
189 self.file.close()