reduce length of pattern lines by one order of magnitude to prevent problems with...
[PyX/mjg.git] / pyx / document.py
blob45e320cb3e1f51489c21252c16371e98a699f4c4
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 cStringIO, warnings
25 import bbox, pswriter, pdfwriter, trafo, style, 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=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.pagebbox = bbox
70 def _process(self, processMethod, contentfile, writer, context, registry, bbox):
71 assert not 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)
76 if expectpagetrafo:
77 canvasfile = cStringIO.StringIO()
78 else:
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
86 if self.pagebbox:
87 bbox.set(self.pagebbox)
88 elif bbox:
89 bbox.enlarge(self.bboxenlarge)
91 if expectpagetrafo:
93 if bbox:
94 # calculate the pagetrafo
95 paperwidth, paperheight = self.paperformat.width, self.paperformat.height
97 # center (optionally rotated) output on page
98 if self.rotated:
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())
105 else:
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())
111 if self.fittosize:
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
120 if self.rotated:
121 sfactor = min(unit.topt(paperheight)/bbox.width_pt(), unit.topt(paperwidth)/bbox.height_pt())
122 else:
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())
136 canvasfile.close()
138 def processPS(self, *args):
139 self._process("processPS", *args)
141 def processPDF(self, *args):
142 self._process("processPDF", *args)
145 class document:
147 """holds a collection of page instances which are output as pages of a document"""
149 def __init__(self, pages=[]):
150 self.pages = pages
152 def append(self, page):
153 self.pages.append(page)
155 def writeEPSfile(self, filename, *args, **kwargs):
156 pswriter.epswriter(self, filename, *args, **kwargs)
158 def writePSfile(self, filename, *args, **kwargs):
159 pswriter.pswriter(self, filename, *args, **kwargs)
161 def writePDFfile(self, filename, *args, **kwargs):
162 pdfwriter.PDFwriter(self, filename, *args, **kwargs)
164 def writetofile(self, filename, *args, **kwargs):
165 if filename.endswith(".eps"):
166 self.writeEPSfile(filename, *args, **kwargs)
167 elif filename.endswith(".ps"):
168 self.writePSfile(filename, *args, **kwargs)
169 elif filename.endswith(".pdf"):
170 self.writePDFfile(filename, *args, **kwargs)
171 else:
172 raise ValueError("unknown file extension")