angle -> relange in arc
[PyX/mjg.git] / pyx / prolog.py
blobad074c108c27363ea73484e497d13486f8c18041
1 #!/usr/bin/env python
4 # Copyright (C) 2003 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2003 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 """prolog module:
25 Snippets included in the prolog of a PostScript file, which can be
26 used as return values of PSOp instances.
28 """
30 import pykpathsea, t1strip
33 # Abstract base class
36 class prologitem:
38 """Part of the PostScript prolog"""
40 def merge(self, other):
41 """ try to merge self with other prologitem
43 If the merge succeeds, return None. Otherwise return other.
44 Raise ValueError, if conflicts arise!"""
46 pass
48 def write(self, file):
49 """ write self in file """
50 pass
53 # Different variants of prolog items
56 class definition(prologitem):
58 """ PostScript function definition included in the prolog """
60 def __init__(self, id, body):
61 self.id = id
62 self.body = body
64 def merge(self, other):
65 if not isinstance(other, definition):
66 return other
67 if self.id==other.id:
68 if self.body==other.body:
69 return None
70 raise ValueError("Conflicting function definitions!")
71 else:
72 return other
74 def write(self, file):
75 file.write("%%%%BeginRessource: %s\n" % self.id)
76 file.write("%(body)s /%(id)s exch def\n" % self.__dict__)
77 file.write("%%EndRessource\n")
80 class fontdefinition(prologitem):
82 """ PostScript font definition included in the prolog """
84 def __init__(self, font):
85 self.basepsname = font.getbasepsname()
86 self.fontfile = font.getfontfile()
87 self.encfilename = font.getencodingfile()
88 self.usedchars = font.usedchars
90 def merge(self, other):
91 if not isinstance(other, fontdefinition):
92 return other
93 if self.basepsname==other.basepsname and self.encfilename==other.encfilename:
94 for i in range(len(self.usedchars)):
95 self.usedchars[i] = self.usedchars[i] or other.usedchars[i]
96 return None
97 else:
98 return other
100 def write(self, file):
101 if self.fontfile:
102 file.write("%%%%BeginFont: %s\n" % self.basepsname)
103 file.write("%Included char codes:")
104 for i in range(len(self.usedchars)):
105 if self.usedchars[i]:
106 file.write(" %d" % i)
107 file.write("\n")
108 pfbpath = pykpathsea.find_file(self.fontfile, pykpathsea.kpse_type1_format)
109 if pfbpath is None:
110 raise RuntimeError("cannot find type 1 font %s" % self.fontfile)
111 if self.encfilename is not None:
112 encpath = pykpathsea.find_file(self.encfilename, pykpathsea.kpse_tex_ps_header_format)
113 if encpath is None:
114 raise RuntimeError("cannot find font encoding file %s" % self.encfilename)
115 t1strip.t1strip(file, pfbpath, self.usedchars, encpath)
116 else:
117 t1strip.t1strip(file, pfbpath, self.usedchars)
118 file.write("%%EndFont\n")
121 class fontencoding(prologitem):
123 """ PostScript font re-encoding vector included in the prolog """
125 def __init__(self, font):
126 self.name = font.getencoding()
127 self.filename = font.getencodingfile()
129 def merge(self, other):
130 if not isinstance(other, fontencoding):
131 return other
132 if self.name==other.name:
133 if self.filename==other.filename:
134 return None
135 raise ValueError("Conflicting encodings!")
136 else:
137 return other
139 def write(self, file):
140 file.write("%%%%BeginProcSet: %s\n" % self.name)
141 path = pykpathsea.find_file(self.filename, pykpathsea.kpse_tex_ps_header_format)
142 encfile = open(path, "r")
143 file.write(encfile.read())
144 encfile.close()
147 class fontreencoding(prologitem):
149 """ PostScript font re-encoding directive included in the prolog """
151 def __init__(self, font):
152 self.psname = font.getpsname()
153 self.basepsname = font.getbasepsname()
154 self.encoding = font.getencoding()
156 def merge(self, other):
157 if not isinstance(other, fontreencoding):
158 return other
159 if self.psname==other.psname:
160 if self.basepsname==other.basepsname and self.encoding==other.encoding:
161 return None
162 raise ValueError("Conflicting font reencodings!")
163 else:
164 return other
166 def write(self, file):
167 file.write("%%%%BeginProcSet: %s\n" % self.psname)
168 file.write("/%s /%s %s ReEncodeFont\n" % (self.basepsname, self.psname, self.encoding))
169 file.write("%%EndProcSet\n")