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
25 Snippets included in the prolog of a PostScript file, which can be
26 used as return values of PSOp instances.
30 import pykpathsea
, t1strip
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!"""
48 def write(self
, file):
49 """ write self in file """
53 # Different variants of prolog items
56 class definition(prologitem
):
58 """ PostScript function definition included in the prolog """
60 def __init__(self
, id, body
):
64 def merge(self
, other
):
65 if not isinstance(other
, definition
):
68 if self
.body
==other
.body
:
70 raise ValueError("Conflicting function definitions!")
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
):
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
]
100 def write(self
, file):
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
)
108 pfbpath
= pykpathsea
.find_file(self
.fontfile
, pykpathsea
.kpse_type1_format
)
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
)
114 raise RuntimeError("cannot find font encoding file %s" % self
.encfilename
)
115 t1strip
.t1strip(file, pfbpath
, self
.usedchars
, encpath
)
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
):
132 if self
.name
==other
.name
:
133 if self
.filename
==other
.filename
:
135 raise ValueError("Conflicting encodings!")
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())
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
):
159 if self
.psname
==other
.psname
:
160 if self
.basepsname
==other
.basepsname
and self
.encoding
==other
.encoding
:
162 raise ValueError("Conflicting font reencodings!")
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")