- path module
[PyX/mjg.git] / pyx / document.py
blob31ee5b71f146bef88ff913c52926323efad85198
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2005 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2005 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 warnings
25 import pswriter, pdfwriter, trafo, unit
28 class paperformat:
30 def __init__(self, width, height, name=None):
31 self.width = width
32 self.height = height
33 self.name = name
35 paperformat.A4 = paperformat(210 * unit.t_mm, 297 * unit.t_mm, "A4")
36 paperformat.A3 = paperformat(297 * unit.t_mm, 420 * unit.t_mm, "A3")
37 paperformat.A2 = paperformat(420 * unit.t_mm, 594 * unit.t_mm, "A2")
38 paperformat.A1 = paperformat(594 * unit.t_mm, 840 * unit.t_mm, "A1")
39 paperformat.A0 = paperformat(840 * unit.t_mm, 1188 * unit.t_mm, "A0")
40 paperformat.A0b = paperformat(910 * unit.t_mm, 1370 * unit.t_mm, None) # dedicated to our friends in Augsburg
41 paperformat.Letter = paperformat(8.5 * unit.t_inch, 11 * unit.t_inch, "Letter")
42 paperformat.Legal = paperformat(8.5 * unit.t_inch, 14 * unit.t_inch, "Legal")
44 def _paperformatfromstring(name):
45 return getattr(paperformat, name.capitalize())
48 class page:
50 def __init__(self, canvas, pagename=None, paperformat=None, rotated=0, centered=1, fittosize=0,
51 margin=1 * unit.t_cm, bboxenlarge=0, bbox=None):
52 self.canvas = canvas
53 self.pagename = pagename
54 # support for depricated string specification of paper formats
55 try:
56 paperformat + ""
57 except:
58 self.paperformat = paperformat
59 else:
60 self.paperformat = _paperformatfromstring(paperformat)
61 warnings.warn("specification of paperformat by string is deprecated, use document.paperformat.%s instead" % paperformat.capitalize(), DeprecationWarning)
63 self.rotated = rotated
64 self.centered = centered
65 self.fittosize = fittosize
66 self.margin = margin
67 self.bboxenlarge = bboxenlarge
68 self.pagebbox = bbox
70 def processPS(self, file, writer, context, registry, bbox):
71 self.canvas.processPS(file, writer, context, registry, bbox)
72 # usually its the bbox of the canvas enlarged by self.bboxenlarge, but
73 # it might be a different bbox as specified in the page constructor
74 if self.pagebbox:
75 # we have to modify the bbox in place
76 # XXX maybe there should be a bbox.set/replace routine which accepts another bbox as argument
77 bbox.llx_pt = self.pagebbox.llx_pt
78 bbox.lly_pt = self.pagebbox.lly_pt
79 bbox.urx_pt = self.pagebbox.urx_pt
80 bbox.ury_pt = self.pagebbox.ury_pt
81 elif bbox:
82 bbox.enlarge(self.bboxenlarge)
84 def pagetrafo(self, bbox):
85 """ calculate a trafo which rotates and fits a canvas on the page
87 The canvas extents are described by bbox.
88 """
89 # XXX should we provide a method for bbox != empty (or use __zero__)
90 if bbox.llx_pt is not None and self.paperformat and (self.rotated or self.centered or self.fittosize):
91 paperwidth, paperheight = self.paperformat.width, self.paperformat.height
93 # center (optionally rotated) output on page
94 if self.rotated:
95 atrafo = trafo.rotate(90).translated(paperwidth, 0)
96 if self.centered or self.fittosize:
97 if not self.fittosize and (bbox.height() > paperwidth or bbox.width() > paperheight):
98 warnings.warn("content exceeds the papersize")
99 atrafo = atrafo.translated(-0.5*(paperwidth - bbox.height()) + bbox.bottom(),
100 0.5*(paperheight - bbox.width()) - bbox.left())
101 else:
102 if self.centered or self.fittosize:
103 if not self.fittosize and (bbox.width() > paperwidth or bbox.height() > paperheight):
104 warnings.warn("content exceeds the papersize")
105 atrafo = trafo.translate(0.5*(paperwidth - bbox.width()) - bbox.left(),
106 0.5*(paperheight - bbox.height()) - bbox.bottom())
107 else:
108 return None # no page transformation needed
110 if self.fittosize:
112 if 2*self.margin > paperwidth or 2*self.margin > paperheight:
113 raise ValueError("Margins too broad for selected paperformat. Aborting.")
115 paperwidth -= 2 * self.margin
116 paperheight -= 2 * self.margin
118 # scale output to pagesize - margins
119 if self.rotated:
120 sfactor = min(unit.topt(paperheight)/bbox.width_pt(), unit.topt(paperwidth)/bbox.height_pt())
121 else:
122 sfactor = min(unit.topt(paperwidth)/bbox.width_pt(), unit.topt(paperheight)/bbox.height_pt())
124 atrafo = atrafo.scaled(sfactor, sfactor, self.margin + 0.5*paperwidth, self.margin + 0.5*paperheight)
126 return atrafo
128 return None # no page transformation needed
131 class document:
133 """holds a collection of page instances which are output as pages of a document"""
135 def __init__(self, pages=[]):
136 self.pages = pages
138 def append(self, page):
139 self.pages.append(page)
141 def writeEPSfile(self, filename, *args, **kwargs):
142 pswriter.epswriter(self, filename, *args, **kwargs)
144 def writePSfile(self, filename, *args, **kwargs):
145 pswriter.pswriter(self, filename, *args, **kwargs)
147 def writePDFfile(self, filename, *args, **kwargs):
148 pdfwriter.PDFwriter(self, filename, *args, **kwargs)
150 def writetofile(self, filename, *args, **kwargs):
151 if filename.endswith(".eps"):
152 self.writeEPSfile(filename, *args, **kwargs)
153 elif filename.endswith(".ps"):
154 self.writePSfile(filename, *args, **kwargs)
155 elif filename.endswith(".pdf"):
156 self.writePDFfile(filename, *args, **kwargs)
157 else:
158 raise ValueError("unknown file extension")