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 cStringIO
, warnings
25 import bbox
, pswriter
, pdfwriter
, trafo
, style
, unit
30 def __init__(self
, width
, height
, name
=None):
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())
50 def __init__(self
, canvas
, pagename
=None, paperformat
=None, rotated
=0, centered
=1, fittosize
=0,
51 margin
=1*unit
.t_cm
, bboxenlarge
=1*unit
.t_pt
, bbox
=None):
53 self
.pagename
= pagename
54 # support for depricated string specification of paper formats
58 self
.paperformat
= paperformat
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
67 self
.bboxenlarge
= bboxenlarge
70 def process(self
, processMethod
, contentfile
, writer
, context
, registry
, bbox
):
73 # check whether we expect a page trafo and use a temporary canvasfile to insert the
74 # pagetrafo in front after the bbox was calculated
75 expectpagetrafo
= self
.paperformat
and (self
.rotated
or self
.centered
or self
.fittosize
)
77 canvasfile
= cStringIO
.StringIO()
79 canvasfile
= contentfile
81 getattr(style
.linewidth
.normal
, processMethod
)(canvasfile
, writer
, context
, registry
, bbox
)
82 getattr(self
.canvas
, processMethod
)(canvasfile
, writer
, context
, registry
, bbox
)
84 # usually its the bbox of the canvas enlarged by self.bboxenlarge, but
85 # it might be a different bbox as specified in the page constructor
87 bbox
.set(self
.pagebbox
)
89 bbox
.enlarge(self
.bboxenlarge
)
94 # calculate the pagetrafo
95 paperwidth
, paperheight
= self
.paperformat
.width
, self
.paperformat
.height
97 # center (optionally rotated) output on page
99 pagetrafo
= trafo
.rotate(90).translated(paperwidth
, 0)
100 if self
.centered
or self
.fittosize
:
101 if not self
.fittosize
and (bbox
.height() > paperwidth
or bbox
.width() > paperheight
):
102 warnings
.warn("content exceeds the papersize")
103 pagetrafo
= pagetrafo
.translated(-0.5*(paperwidth
- bbox
.height()) + bbox
.bottom(),
104 0.5*(paperheight
- bbox
.width()) - bbox
.left())
106 if not self
.fittosize
and (bbox
.width() > paperwidth
or bbox
.height() > paperheight
):
107 warnings
.warn("content exceeds the papersize")
108 pagetrafo
= trafo
.translate(0.5*(paperwidth
- bbox
.width()) - bbox
.left(),
109 0.5*(paperheight
- bbox
.height()) - bbox
.bottom())
113 if 2*self
.margin
> paperwidth
or 2*self
.margin
> paperheight
:
114 raise ValueError("Margins too broad for selected paperformat. Aborting.")
116 paperwidth
-= 2 * self
.margin
117 paperheight
-= 2 * self
.margin
119 # scale output to pagesize - margins
121 sfactor
= min(unit
.topt(paperheight
)/bbox
.width_pt(), unit
.topt(paperwidth
)/bbox
.height_pt())
123 sfactor
= min(unit
.topt(paperwidth
)/bbox
.width_pt(), unit
.topt(paperheight
)/bbox
.height_pt())
125 pagetrafo
= pagetrafo
.scaled(sfactor
, sfactor
, self
.margin
+ 0.5*paperwidth
, self
.margin
+ 0.5*paperheight
)
127 # apply the pagetrafo and write it to the contentfile
128 bbox
.transform(pagetrafo
)
129 pagetrafofile
= cStringIO
.StringIO()
130 # context, bbox, registry are just passed as stubs (the trafo should not touch them)
131 getattr(pagetrafo
, processMethod
)(pagetrafofile
, writer
, context
, registry
, bbox
)
132 contentfile
.write(pagetrafofile
.getvalue())
133 pagetrafofile
.close()
135 contentfile
.write(canvasfile
.getvalue())
141 """holds a collection of page instances which are output as pages of a document"""
143 def __init__(self
, pages
=[]):
146 def append(self
, page
):
147 self
.pages
.append(page
)
149 def writeEPSfile(self
, filename
, *args
, **kwargs
):
150 pswriter
.epswriter(self
, filename
, *args
, **kwargs
)
152 def writePSfile(self
, filename
, *args
, **kwargs
):
153 pswriter
.pswriter(self
, filename
, *args
, **kwargs
)
155 def writePDFfile(self
, filename
, *args
, **kwargs
):
156 pdfwriter
.PDFwriter(self
, filename
, *args
, **kwargs
)
158 def writetofile(self
, filename
, *args
, **kwargs
):
159 if filename
.endswith(".eps"):
160 self
.writeEPSfile(filename
, *args
, **kwargs
)
161 elif filename
.endswith(".ps"):
162 self
.writePSfile(filename
, *args
, **kwargs
)
163 elif filename
.endswith(".pdf"):
164 self
.writePDFfile(filename
, *args
, **kwargs
)
166 raise ValueError("unknown file extension")