2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2005-2006 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2005-2006 André Wobst <wobsta@users.sourceforge.net>
8 # This file is part of PyX (http://pyx.sourceforge.net/).
10 # PyX is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # PyX is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with PyX; if not, write to the Free Software
22 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
24 import cStringIO
, copy
, warnings
, time
31 import bbox
, unit
, style
, type1font
, version
36 # fallback implementation for Python 2.2 and below
38 return zip(xrange(len(list)), list)
45 # we want to keep the order of the resources
48 self
.procsets
= {"PDF": 1}
50 def add(self
, object):
51 """ register object, merging it with an already registered object of the same type and id """
52 sameobjects
= self
.types
.setdefault(object.type, {})
53 if sameobjects
.has_key(object.id):
54 sameobjects
[object.id].merge(object)
56 self
.objects
.append(object)
57 sameobjects
[object.id] = object
59 def getrefno(self
, object):
60 return self
.types
[object.type][object.id].refno
62 def mergeregistry(self
, registry
):
63 for object in registry
.objects
:
66 def write(self
, file, writer
, catalog
):
67 # first we set all refnos
69 for object in self
.objects
:
73 # second, all objects are written, keeping the positions in the output file
75 for object in self
.objects
:
76 fileposes
.append(file.tell())
77 file.write("%i 0 obj\n" % object.refno
)
78 object.write(file, writer
, self
)
79 file.write("endobj\n")
85 "0000000000 65535 f \n" % refno
)
87 for filepos
in fileposes
:
88 file.write("%010i 00000 n \n" % filepos
)
91 file.write("trailer\n"
94 file.write("/Root %i 0 R\n" % self
.getrefno(catalog
))
95 file.write("/Info %i 0 R\n" % self
.getrefno(catalog
.PDFinfo
))
101 def addresource(self
, resourcetype
, resourcename
, object, procset
=None):
102 self
.resources
.setdefault(resourcetype
, {})[resourcename
] = object
104 self
.procsets
[procset
] = 1
106 def writeresources(self
, file):
107 file.write("/Resources <<\n")
108 file.write("/ProcSet [ %s ]\n" % " ".join(["/%s" % p
for p
in self
.procsets
.keys()]))
110 for resourcetype
, resources
in self
.resources
.items():
111 file.write("/%s <<\n%s\n>>\n" % (resourcetype
, "\n".join(["/%s %i 0 R" % (name
, self
.getrefno(object))
112 for name
, object in resources
.items()])))
118 def __init__(self
, type, _id
=None):
119 """create a PDFobject
120 - type has to be a string describing the type of the object
121 - _id is a unique identification used for the object if it is not None.
122 Otherwise id(self) is used
130 def merge(self
, other
):
133 def write(self
, file, writer
, registry
):
134 raise NotImplementedError("write method has to be provided by PDFobject subclass")
137 class PDFcatalog(PDFobject
):
139 def __init__(self
, document
, writer
, registry
):
140 PDFobject
.__init
__(self
, "catalog")
141 self
.PDFpages
= PDFpages(document
, writer
, registry
)
142 registry
.add(self
.PDFpages
)
143 self
.PDFinfo
= PDFinfo()
144 registry
.add(self
.PDFinfo
)
146 def write(self
, file, writer
, registry
):
149 "/Pages %i 0 R\n" % registry
.getrefno(self
.PDFpages
))
150 if writer
.fullscreen
:
151 file.write("/PageMode /FullScreen\n")
155 class PDFinfo(PDFobject
):
158 PDFobject
.__init
__(self
, "info")
160 def write(self
, file, writer
, registry
):
161 if time
.timezone
< 0:
162 # divmod on positive numbers, otherwise the minutes have a different sign from the hours
163 timezone
= "-%02i'%02i'" % divmod(-time
.timezone
/60, 60)
164 elif time
.timezone
> 0:
165 timezone
= "+%02i'%02i'" % divmod(time
.timezone
/60, 60)
172 if 32 <= ord(c
) <= 127 and c
not in "()[]<>\\":
175 r
+= "\\%03o" % ord(c
)
180 file.write("/Title (%s)\n" % pdfstring(writer
.title
))
182 file.write("/Author (%s)\n" % pdfstring(writer
.author
))
184 file.write("/Subject (%s)\n" % pdfstring(writer
.subject
))
186 file.write("/Keywords (%s)\n" % pdfstring(writer
.keywords
))
187 file.write("/Creator (PyX %s)\n" % version
.version
)
188 file.write("/CreationDate (D:%s%s)\n" % (time
.strftime("%Y%m%d%H%M"), timezone
))
192 class PDFpages(PDFobject
):
194 def __init__(self
, document
, writer
, registry
):
195 PDFobject
.__init
__(self
, "pages")
196 self
.PDFpagelist
= []
197 for pageno
, page
in enumerate(document
.pages
):
198 page
= PDFpage(page
, pageno
, self
, writer
, registry
)
200 self
.PDFpagelist
.append(page
)
202 def write(self
, file, writer
, registry
):
207 ">>\n" % (" ".join(["%i 0 R" % registry
.getrefno(page
)
208 for page
in self
.PDFpagelist
]),
209 len(self
.PDFpagelist
)))
212 class PDFpage(PDFobject
):
214 def __init__(self
, page
, pageno
, PDFpages
, writer
, registry
):
215 PDFobject
.__init
__(self
, "page")
216 self
.PDFpages
= PDFpages
219 # every page uses its own registry in order to find out which
220 # resources are used within the page. However, the
221 # pageregistry is also merged in the global registry
222 self
.pageregistry
= PDFregistry()
224 self
.PDFcontent
= PDFcontent(page
, writer
, self
.pageregistry
)
225 self
.pageregistry
.add(self
.PDFcontent
)
226 registry
.mergeregistry(self
.pageregistry
)
228 def write(self
, file, writer
, registry
):
231 "/Parent %i 0 R\n" % registry
.getrefno(self
.PDFpages
))
232 paperformat
= self
.page
.paperformat
234 file.write("/MediaBox [0 0 %f %f]\n" % (unit
.topt(paperformat
.width
), unit
.topt(paperformat
.height
)))
236 file.write("/MediaBox [%f %f %f %f]\n" % self
.PDFcontent
.bbox
.highrestuple_pt())
237 if self
.PDFcontent
.bbox
and writer
.writebbox
:
238 file.write("/CropBox [%f %f %f %f]\n" % self
.PDFcontent
.bbox
.highrestuple_pt())
239 if self
.page
.rotated
:
240 file.write("/Rotate 90\n")
241 file.write("/Contents %i 0 R\n" % registry
.getrefno(self
.PDFcontent
))
242 self
.pageregistry
.writeresources(file)
246 class PDFcontent(PDFobject
):
248 def __init__(self
, page
, writer
, registry
):
249 PDFobject
.__init
__(self
, registry
, "content")
250 contentfile
= cStringIO
.StringIO()
251 self
.bbox
= bbox
.empty()
253 page
.process("processPDF", contentfile
, writer
, acontext
, registry
, self
.bbox
)
254 self
.content
= contentfile
.getvalue()
257 def write(self
, file, writer
, registry
):
259 content
= zlib
.compress(self
.content
)
261 content
= self
.content
263 "/Length %i\n" % len(content
))
265 file.write("/Filter /FlateDecode\n")
269 file.write("endstream\n")
272 class PDFfont(PDFobject
):
274 def __init__(self
, font
, chars
, writer
, registry
):
275 PDFobject
.__init
__(self
, "font", font
.name
)
276 registry
.addresource("Font", font
.name
, self
, procset
="Text")
278 self
.fontdescriptor
= PDFfontdescriptor(font
, chars
, writer
, registry
)
279 registry
.add(self
.fontdescriptor
)
282 self
.encoding
= PDFencoding(font
.encoding
, writer
, registry
)
283 registry
.add(self
.encoding
)
287 self
.name
= font
.name
288 self
.basefontname
= font
.basefontname
289 self
.metric
= font
.metric
291 def write(self
, file, writer
, registry
):
295 file.write("/Name /%s\n" % self
.name
)
296 file.write("/BaseFont /%s\n" % self
.basefontname
)
297 if self
.fontdescriptor
.fontfile
is not None and self
.fontdescriptor
.fontfile
.usedchars
is not None:
298 usedchars
= self
.fontdescriptor
.fontfile
.usedchars
299 firstchar
= min(usedchars
.keys())
300 lastchar
= max(usedchars
.keys())
301 file.write("/FirstChar %d\n" % firstchar
)
302 file.write("/LastChar %d\n" % lastchar
)
303 file.write("/Widths\n"
305 for i
in range(firstchar
, lastchar
+1):
306 if i
and not (i
% 8):
310 if usedchars
.has_key(i
):
311 file.write("%f" % self
.metric
.getwidth_ds(i
))
316 file.write("/FirstChar 0\n"
321 if i
and not (i
% 8):
326 width
= self
.metric
.getwidth_ds(i
)
327 except (IndexError, AttributeError):
329 file.write("%f" % width
)
331 file.write("/FontDescriptor %d 0 R\n" % registry
.getrefno(self
.fontdescriptor
))
333 file.write("/Encoding %d 0 R\n" % registry
.getrefno(self
.encoding
))
337 class PDFfontdescriptor(PDFobject
):
339 def __init__(self
, font
, chars
, writer
, registry
):
340 PDFobject
.__init
__(self
, "fontdescriptor", font
.basefontname
)
342 if font
.filename
is None:
345 self
.fontfile
= PDFfontfile(font
.basefontname
, font
.filename
, font
.encoding
, chars
, writer
, registry
)
346 registry
.add(self
.fontfile
)
348 self
.name
= font
.basefontname
349 self
.fontinfo
= font
.metric
.fontinfo()
351 def write(self
, file, writer
, registry
):
353 "/Type /FontDescriptor\n"
354 "/FontName /%s\n" % self
.name
)
355 if self
.fontfile
is None:
356 file.write("/Flags 32\n")
358 file.write("/Flags %d\n" % self
.fontfile
.getflags())
359 file.write("/FontBBox [%d %d %d %d]\n" % self
.fontinfo
.fontbbox
)
360 file.write("/ItalicAngle %d\n" % self
.fontinfo
.italicangle
)
361 file.write("/Ascent %d\n" % self
.fontinfo
.ascent
)
362 file.write("/Descent %d\n" % self
.fontinfo
.descent
)
363 file.write("/CapHeight %d\n" % self
.fontinfo
.capheight
)
364 file.write("/StemV %d\n" % self
.fontinfo
.vstem
)
365 if self
.fontfile
is not None:
366 file.write("/FontFile %d 0 R\n" % registry
.getrefno(self
.fontfile
))
370 class PDFfontfile(PDFobject
):
372 def __init__(self
, name
, filename
, encoding
, chars
, writer
, registry
):
373 PDFobject
.__init
__(self
, "fontfile", filename
)
375 self
.filename
= filename
377 self
.encodingfilename
= None
379 self
.encodingfilename
= encoding
.filename
382 self
.usedchars
[char
] = 1
387 def merge(self
, other
):
388 if self
.encodingfilename
== other
.encodingfilename
:
389 self
.usedchars
.update(other
.usedchars
)
391 # TODO: need to resolve the encoding when several encodings are in the play
394 def mkfontfile(self
):
396 self
.font
= font
.t1font
.T1pfbfont(self
.filename
)
399 if self
.font
is None:
401 return self
.font
.getflags()
403 def write(self
, file, writer
, registry
):
404 if self
.font
is None:
407 # XXX: access to the encoding file
408 if self
.encodingfilename
:
409 encodingfile
= type1font
.encodingfile(self
.encodingfilename
, self
.encodingfilename
)
410 usedglyphs
= [encodingfile
.decode(char
)[1:] for char
in self
.usedchars
.keys()]
412 self
.font
._encoding
()
413 usedglyphs
= [self
.font
.encoding
.decode(char
) for char
in self
.usedchars
.keys()]
414 strippedfont
= self
.font
.getstrippedfont(usedglyphs
)
416 strippedfont
= self
.font
417 strippedfont
.outputPDF(file, writer
)
420 class PDFencoding(PDFobject
):
422 def __init__(self
, encoding
, writer
, registry
):
423 PDFobject
.__init
__(self
, "encoding", encoding
.name
)
424 self
.encoding
= encoding
426 def write(self
, file, writer
, registry
):
427 encodingfile
= type1font
.encodingfile(self
.encoding
.name
, self
.encoding
.filename
)
428 encodingfile
.outputPDF(file, writer
)
433 def __init__(self
, document
, filename
,
434 title
=None, author
=None, subject
=None, keywords
=None,
435 fullscreen
=0, writebbox
=0, compress
=1, compresslevel
=6):
436 if not filename
.endswith(".pdf"):
437 filename
= filename
+ ".pdf"
439 file = open(filename
, "wb")
441 raise IOError("cannot open output file")
445 self
.subject
= subject
446 self
.keywords
= keywords
447 self
.fullscreen
= fullscreen
448 self
.writebbox
= writebbox
449 if compress
and not haszlib
:
451 warnings
.warn("compression disabled due to missing zlib module")
452 self
.compress
= compress
453 self
.compresslevel
= compresslevel
455 # the PDFcatalog class automatically builds up the pdfobjects from a document
456 registry
= PDFregistry()
457 catalog
= PDFcatalog(document
, self
, registry
)
458 registry
.add(catalog
)
460 file.write("%%PDF-1.4\n%%%s%s%s%s\n" % (chr(195), chr(182), chr(195), chr(169)))
461 registry
.write(file, self
, catalog
)
468 self
.linewidth_pt
= None
469 # XXX there are both stroke and fill color spaces
470 self
.colorspace
= None
476 def __call__(self
, **kwargs
):
477 newcontext
= copy
.copy(self
)
478 for key
, value
in kwargs
.items():
479 setattr(newcontext
, key
, value
)