Applied upstream as r3028 r3025 r3024
[PyX/mjg.git] / pyx / dvi / vffile.py
blob25c183ac8db3598a196902e69413a7a18be605b5
1 # -*- coding: ISO-8859-1 -*-
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, filename, scale, tfmconv, pyxconv, debug=0):
37 self.filename = filename
38 self.scale = scale
39 self.tfmconv = tfmconv
40 self.pyxconv = pyxconv
41 self.debug = debug
42 self.fonts = {} # used fonts
43 self.widths = {} # widths of defined chars
44 self.chardefs = {} # dvi chunks for defined chars
46 afile = reader.reader(self.filename)
48 cmd = afile.readuchar()
49 if cmd == _VF_PRE:
50 if afile.readuchar() != _VF_ID: raise VFError
51 comment = afile.read(afile.readuchar())
52 self.cs = afile.readuint32()
53 self.ds = afile.readuint32()
54 else:
55 raise VFError
57 while 1:
58 cmd = afile.readuchar()
59 if cmd >= _VF_FNTDEF1234 and cmd < _VF_FNTDEF1234 + 4:
60 # font definition
61 if cmd == _VF_FNTDEF1234:
62 num = afile.readuchar()
63 elif cmd == _VF_FNTDEF1234+1:
64 num = afile.readuint16()
65 elif cmd == _VF_FNTDEF1234+2:
66 num = afile.readuint24()
67 elif cmd == _VF_FNTDEF1234+3:
68 num = afile.readint32()
69 c = afile.readint32()
70 s = afile.readint32() # relative scaling used for font (fix_word)
71 d = afile.readint32() # design size of font
72 fontname = afile.read(afile.readuchar() + afile.readuchar())
74 # rescaled size of font: s is relative to the scaling
75 # of the virtual font itself. Note that realscale has
76 # to be a fix_word (like s)
77 # XXX: check rounding
78 reals = int(round(self.scale * (16*self.ds/16777216L) * s))
80 # print ("defining font %s -- VF scale: %g, VF design size: %d, relative font size: %d => real size: %d" %
81 # (fontname, self.scale, self.ds, s, reals)
82 # )
84 # XXX allow for virtual fonts here too
85 self.fonts[num] = texfont.TeXfont(fontname, c, reals, d, self.tfmconv, self.pyxconv, self.debug > 1)
86 elif cmd == _VF_LONG_CHAR:
87 # character packet (long form)
88 pl = afile.readuint32() # packet length
89 cc = afile.readuint32() # char code (assumed unsigned, but anyhow only 0 <= cc < 255 is actually used)
90 tfm = afile.readuint24() # character width
91 dvi = afile.read(pl) # dvi code of character
92 self.widths[cc] = tfm
93 self.chardefs[cc] = dvi
94 elif cmd < _VF_LONG_CHAR:
95 # character packet (short form)
96 cc = afile.readuchar() # char code
97 tfm = afile.readuint24() # character width
98 dvi = afile.read(cmd)
99 self.widths[cc] = tfm
100 self.chardefs[cc] = dvi
101 elif cmd == _VF_POST:
102 break
103 else:
104 raise VFError
106 afile.close()
108 def getfonts(self):
109 return self.fonts
111 def getchar(self, cc):
112 return self.chardefs[cc]