update upload data
[PyX.git] / pyx / dvi / tfmfile.py
blobbe0832b301fa9d4bb3e4afc2693b711e4cad10f1
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2007-2011 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2007-2011 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 from pyx import reader
26 class char_info_word:
28 def __init__(self, word):
29 self.width_index = int((word & 0xFF000000L) >> 24) #make sign-safe
30 self.height_index = (word & 0x00F00000) >> 20
31 self.depth_index = (word & 0x000F0000) >> 16
32 self.italic_index = (word & 0x0000FC00) >> 10
33 self.tag = (word & 0x00000300) >> 8
34 self.remainder = (word & 0x000000FF)
37 class TFMfile:
39 def __init__(self, file, debug=0):
40 self.file = reader.stringreader(file.read())
43 # read pre header
46 self.lf = self.file.readint16()
47 self.lh = self.file.readint16()
48 self.bc = self.file.readint16()
49 self.ec = self.file.readint16()
50 self.nw = self.file.readint16()
51 self.nh = self.file.readint16()
52 self.nd = self.file.readint16()
53 self.ni = self.file.readint16()
54 self.nl = self.file.readint16()
55 self.nk = self.file.readint16()
56 self.ne = self.file.readint16()
57 self.np = self.file.readint16()
59 if not (self.bc-1 <= self.ec <= 255 and
60 self.ne <= 256 and
61 self.lf == 6+self.lh+(self.ec-self.bc+1)+self.nw+self.nh+self.nd
62 +self.ni+self.nl+self.nk+self.ne+self.np):
63 raise RuntimeError("error in TFM pre-header")
65 if debug:
66 print "lh=%d" % self.lh
69 # read header
72 self.checksum = self.file.readint32()
73 self.designsize = self.file.readint32()
74 assert self.designsize > 0, "invald design size"
75 if self.lh > 2:
76 assert self.lh > 11, "inconsistency in TFM file: incomplete field"
77 self.charcoding = self.file.readstring(40)
78 else:
79 self.charcoding = None
81 if self.lh > 12:
82 assert self.lh > 16, "inconsistency in TFM file: incomplete field"
83 self.fontfamily = self.file.readstring(20)
84 else:
85 self.fontfamily = None
87 if debug:
88 print "(FAMILY %s)" % self.fontfamily
89 print "(CODINGSCHEME %s)" % self.charcoding
90 print "(DESINGSIZE R %f)" % (16.0*self.designsize/16777216L)
92 if self.lh > 17:
93 self.sevenbitsave = self.file.readuchar()
94 # ignore the following two bytes
95 self.file.readint16()
96 facechar = self.file.readuchar()
97 # decode ugly face specification into the Knuth suggested string
98 if facechar < 18:
99 if facechar >= 12:
100 self.face = "E"
101 facechar -= 12
102 elif facechar >= 6:
103 self.face = "C"
104 facechar -= 6
105 else:
106 self.face = "R"
108 if facechar >= 4:
109 self.face = "L" + self.face
110 facechar -= 4
111 elif facechar >= 2:
112 self.face = "B" + self.face
113 facechar -= 2
114 else:
115 self.face = "M" + self.face
117 if facechar == 1:
118 self.face = self.face[0] + "I" + self.face[1]
119 else:
120 self.face = self.face[0] + "R" + self.face[1]
122 else:
123 self.face = None
124 else:
125 self.sevenbitsave = self.face = None
127 if self.lh > 18:
128 # just ignore the rest
129 print self.file.read((self.lh-18)*4)
132 # read char_info
135 self.char_info = [None]*(self.ec+1)
136 for charcode in range(self.bc, self.ec+1):
137 self.char_info[charcode] = char_info_word(self.file.readint32())
138 if self.char_info[charcode].width_index == 0:
139 # disable character if width_index is zero
140 self.char_info[charcode] = None
143 # read widths
146 self.width = [None for width_index in range(self.nw)]
147 for width_index in range(self.nw):
148 self.width[width_index] = self.file.readint32()
151 # read heights
154 self.height = [None for height_index in range(self.nh)]
155 for height_index in range(self.nh):
156 self.height[height_index] = self.file.readint32()
159 # read depths
162 self.depth = [None for depth_index in range(self.nd)]
163 for depth_index in range(self.nd):
164 self.depth[depth_index] = self.file.readint32()
167 # read italic
170 self.italic = [None for italic_index in range(self.ni)]
171 for italic_index in range(self.ni):
172 self.italic[italic_index] = self.file.readint32()
175 # read lig_kern
178 # XXX decode to lig_kern_command
180 self.lig_kern = [None for lig_kern_index in range(self.nl)]
181 for lig_kern_index in range(self.nl):
182 self.lig_kern[lig_kern_index] = self.file.readint32()
185 # read kern
188 self.kern = [None for kern_index in range(self.nk)]
189 for kern_index in range(self.nk):
190 self.kern[kern_index] = self.file.readint32()
193 # read exten
196 # XXX decode to extensible_recipe
198 self.exten = [None for exten_index in range(self.ne)]
199 for exten_index in range(self.ne):
200 self.exten[exten_index] = self.file.readint32()
203 # read param
206 # XXX decode
208 self.param = [None for param_index in range(self.np)]
209 for param_index in range(self.np):
210 self.param[param_index] = self.file.readint32()
212 self.file.close()