just some code cosmetics
[PyX/mjg.git] / pyx / t1strip / fullfont.py
blob2b216235f48a0210534220eca770cc2ca425c4a5
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2004 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
23 _PFB_ASCII = "\200\1"
24 _PFB_BIN = "\200\2"
25 _PFB_DONE = "\200\3"
26 _PFA = "%!"
28 def pfblength(string):
29 if len(string) != 4:
30 raise ValueError("invalid string length")
31 return (ord(string[0]) +
32 ord(string[1])*256 +
33 ord(string[2])*256*256 +
34 ord(string[3])*256*256*256)
36 def fullfont(file, filename):
37 """inserts full pfa or pfb fonts
38 - file is a file instance where the pfa output is written to
39 - pfbfilename is the full filename of the pfa or pfb input file
40 - the input type pfa or pfb is autodetected"""
42 infile = open(filename, "rb")
43 blockid = infile.read(2)
44 if blockid == _PFA:
45 file.write(blockid)
46 file.write(infile.read().replace("\r\n", "\n").replace("\r", "\n"))
47 else:
48 while 1:
49 if len(blockid) != 2:
50 raise RuntimeError("EOF reached while reading blockid")
51 if blockid == _PFB_DONE:
52 if infile.read() != "":
53 raise RuntimeError("trailing characters in pfb file")
54 else:
55 return
56 if blockid != _PFB_ASCII and blockid != _PFB_BIN:
57 raise RuntimeError("invalid blockid")
58 length = pfblength(infile.read(4))
59 block = infile.read(length)
60 if blockid == _PFB_ASCII:
61 file.write(block.replace("\r\n", "\n").replace("\r", "\n"))
62 else:
63 block = [ord(c) for c in block]
64 while len(block) > 32:
65 file.write("%02x"*32 % tuple(block[:32]) + "\n")
66 del block[:32]
67 if len(block):
68 file.write("%02x"*len(block) % tuple(block) + "\n")
69 blockid = infile.read(2)