update upload data
[PyX.git] / pyx / dvi / vffile.py
blob3c5ce91c7cbcb20d9effa51491a1c13544e08244
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2002-2007 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2002-2007 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
23 from pyx import reader
24 import texfont
26 _VF_LONG_CHAR = 242 # character packet (long version)
27 _VF_FNTDEF1234 = 243 # font definition
28 _VF_PRE = 247 # preamble
29 _VF_POST = 248 # postamble
30 _VF_ID = 202 # VF id byte
32 class VFError(Exception): pass
34 class vffile:
36 def __init__(self, file, scale, tfmconv, pyxconv, debug=0):
37 self.scale = scale
38 self.tfmconv = tfmconv
39 self.pyxconv = pyxconv
40 self.debug = debug
41 self.fonts = {} # used fonts
42 self.widths = {} # widths of defined chars
43 self.chardefs = {} # dvi chunks for defined chars
45 afile = reader.stringreader(file.read())
47 cmd = afile.readuchar()
48 if cmd == _VF_PRE:
49 if afile.readuchar() != _VF_ID: raise VFError
50 comment = afile.read(afile.readuchar())
51 self.cs = afile.readuint32()
52 self.ds = afile.readuint32()
53 else:
54 raise VFError
56 while 1:
57 cmd = afile.readuchar()
58 if cmd >= _VF_FNTDEF1234 and cmd < _VF_FNTDEF1234 + 4:
59 # font definition
60 if cmd == _VF_FNTDEF1234:
61 num = afile.readuchar()
62 elif cmd == _VF_FNTDEF1234+1:
63 num = afile.readuint16()
64 elif cmd == _VF_FNTDEF1234+2:
65 num = afile.readuint24()
66 elif cmd == _VF_FNTDEF1234+3:
67 num = afile.readint32()
68 c = afile.readint32()
69 s = afile.readint32() # relative scaling used for font (fix_word)
70 d = afile.readint32() # design size of font
71 fontname = afile.read(afile.readuchar() + afile.readuchar())
73 # rescaled size of font: s is relative to the scaling
74 # of the virtual font itself. Note that realscale has
75 # to be a fix_word (like s)
76 # XXX: check rounding
77 reals = int(round(self.scale * (16*self.ds/16777216L) * s))
79 # print ("defining font %s -- VF scale: %g, VF design size: %d, relative font size: %d => real size: %d" %
80 # (fontname, self.scale, self.ds, s, reals)
81 # )
83 # XXX allow for virtual fonts here too
84 self.fonts[num] = texfont.TeXfont(fontname, c, reals, d, self.tfmconv, self.pyxconv, self.debug > 1)
85 elif cmd == _VF_LONG_CHAR:
86 # character packet (long form)
87 pl = afile.readuint32() # packet length
88 cc = afile.readuint32() # char code (assumed unsigned, but anyhow only 0 <= cc < 255 is actually used)
89 tfm = afile.readuint24() # character width
90 dvi = afile.read(pl) # dvi code of character
91 self.widths[cc] = tfm
92 self.chardefs[cc] = dvi
93 elif cmd < _VF_LONG_CHAR:
94 # character packet (short form)
95 cc = afile.readuchar() # char code
96 tfm = afile.readuint24() # character width
97 dvi = afile.read(cmd)
98 self.widths[cc] = tfm
99 self.chardefs[cc] = dvi
100 elif cmd == _VF_POST:
101 break
102 else:
103 raise VFError
105 afile.close()
107 def getfonts(self):
108 return self.fonts
110 def getchar(self, cc):
111 return self.chardefs[cc]