fix call signature
[PyX/mjg.git] / pyx / document.py
blob3288290478c5be2ecd4a5718aeebefd94e4a7c32
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 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=paperformat.A4, rotated=0, centered=1, fittosize=0,
51 margin=1 * unit.t_cm, bboxenlarge=1 * unit.t_pt, 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.bbox = bbox
70 def pagetrafo(self, abbox):
71 """ calculate a trafo which rotates and fits a canvas on the page
73 The canvas extents are described by abbox, which, however, is only used
74 when during page construction no bbox has been specified.
75 """
76 if self.bbox is not None:
77 abbox = self.bbox
78 if self.rotated or self.centered or self.fittosize:
79 paperwidth, paperheight = self.paperformat.width, self.paperformat.height
81 # center (optionally rotated) output on page
82 if self.rotated:
83 atrafo = trafo.rotate(90).translated(paperwidth, 0)
84 if self.centered or self.fittosize:
85 atrafo = atrafo.translated(-0.5*(paperwidth - abbox.height()) + abbox.bottom(),
86 0.5*(paperheight - abbox.width()) - abbox.left())
87 else:
88 if self.centered or self.fittosize:
89 atrafo = trafo.trafo()
90 else:
91 return None # no page transformation needed
92 if self.centered or self.fittosize:
93 atrafo = atrafo.translated(0.5*(paperwidth - abbox.width()) - abbox.left(),
94 0.5*(paperheight - abbox.height()) - abbox.bottom())
96 if self.fittosize:
98 if 2*self.margin > paperwidth or 2*self.margin > paperheight:
99 raise ValueError("Margins too broad for selected paperformat. Aborting.")
101 paperwidth -= 2 * self.margin
102 paperheight -= 2 * self.margin
104 # scale output to pagesize - margins
105 if self.rotated:
106 sfactor = min(unit.topt(paperheight)/abbox.width_pt(), unit.topt(paperwidth)/abbox.height_pt())
107 else:
108 sfactor = min(unit.topt(paperwidth)/abbox.width_pt(), unit.topt(paperheight)/abbox.height_pt())
110 atrafo = atrafo.scaled(sfactor, sfactor, self.margin + 0.5*paperwidth, self.margin + 0.5*paperheight)
112 return atrafo
114 return None # no page transformation needed
117 class document:
119 """holds a collection of page instances which are output as pages of a document"""
121 def __init__(self, pages=[]):
122 self.pages = pages
124 def append(self, page):
125 self.pages.append(page)
127 def writePSfile(self, filename, *args, **kwargs):
128 pswriter.pswriter(self, filename, *args, **kwargs)
130 def writeEPSfile(self, filename, *args, **kwargs):
131 pswriter.epswriter(self, filename, *args, **kwargs)
133 def writePDFfile(self, filename, *args, **kwargs):
134 pdfwriter.PDFwriter(self, filename, *args, **kwargs)
136 def writetofile(self, filename, *args, **kwargs):
137 if filename[-4:] == ".eps":
138 self.writeEPSfile(filename, *args, **kwargs)
139 elif filename[-4:] == ".pdf":
140 self.writePDFfile(filename, *args, **kwargs)
141 else:
142 raise ValueError("unknown file extension")