add ...
[PyX/mjg.git] / pyx / pattern.py
blob6dedfe1bfae09e71b6c9c75a708a611c94022e1d
1 #!/usr/bin/env python
2 # -*- coding: ISO-8859-1 -*-
5 # Copyright (C) 2002-2006 Jörg Lehmann <joergl@users.sourceforge.net>
6 # Copyright (C) 2002-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 from __future__ import nested_scopes
26 import cStringIO, math, warnings
27 import attr, canvas, path, pdfwriter, pswriter, style, unit, trafo
28 import bbox as bboxmodule
30 class _marker: pass
32 # TODO: pattern should not derive from canvas but wrap a canvas
34 class pattern(canvas._canvas, attr.exclusiveattr, style.fillstyle):
36 def __init__(self, painttype=1, tilingtype=1, xstep=None, ystep=None, bbox=None, trafo=None, **kwargs):
37 canvas._canvas.__init__(self, **kwargs)
38 attr.exclusiveattr.__init__(self, pattern)
39 self.id = "pattern%d" % id(self)
40 self.patterntype = 1
41 if painttype not in (1,2):
42 raise ValueError("painttype must be 1 or 2")
43 self.painttype = painttype
44 if tilingtype not in (1,2,3):
45 raise ValueError("tilingtype must be 1, 2 or 3")
46 self.tilingtype = tilingtype
47 self.xstep = xstep
48 self.ystep = ystep
49 self.patternbbox = bbox
50 self.patterntrafo = trafo
52 def __call__(self, painttype=_marker, tilingtype=_marker, xstep=_marker, ystep=_marker,
53 bbox=_marker, trafo=_marker):
54 if painttype is _marker:
55 painttype = self.painttype
56 if tilingtype is _marker:
57 tilingtype = self.tilingtype
58 if xstep is _marker:
59 xstep = self.xstep
60 if ystep is _marker:
61 ystep = self.ystep
62 if bbox is _marker:
63 bbox = self.bbox
64 if trafo is _marker:
65 trafo = self.trafo
66 return pattern(painttype, tilingtype, xstep, ystep, bbox, trafo)
68 def bbox(self):
69 return bboxmodule.empty()
71 def processPS(self, file, writer, context, registry, bbox):
72 # process pattern, letting it register its resources and calculate the bbox of the pattern
73 patternfile = cStringIO.StringIO()
74 realpatternbbox = bboxmodule.empty()
75 canvas._canvas.processPS(self, patternfile, writer, pswriter.context(), registry, realpatternbbox)
76 patternproc = patternfile.getvalue()
77 patternfile.close()
79 if self.xstep is None:
80 xstep = unit.topt(realpatternbbox.width())
81 else:
82 xstep = unit.topt(self.xstep)
83 if self.ystep is None:
84 ystep = unit.topt(realpatternbbox.height())
85 else:
86 ystep = unit.topt(self.ystep)
87 if not xstep:
88 raise ValueError("xstep in pattern cannot be zero")
89 if not ystep:
90 raise ValueError("ystep in pattern cannot be zero")
91 patternbbox = self.patternbbox or realpatternbbox.enlarged(5*unit.pt)
93 patternprefix = "\n".join(("<<",
94 "/PatternType %d" % self.patterntype,
95 "/PaintType %d" % self.painttype,
96 "/TilingType %d" % self.tilingtype,
97 "/BBox[%d %d %d %d]" % patternbbox.lowrestuple_pt(),
98 "/XStep %g" % xstep,
99 "/YStep %g" % ystep,
100 "/PaintProc {\nbegin\n"))
101 patterntrafostring = self.patterntrafo is None and "matrix" or str(self.patterntrafo)
102 patternsuffix = "end\n} bind\n>>\n%s\nmakepattern" % patterntrafostring
104 registry.add(pswriter.PSdefinition(self.id, "".join((patternprefix, patternproc, patternsuffix))))
106 # activate pattern
107 file.write("%s setpattern\n" % self.id)
109 def processPDF(self, file, writer, context, registry, bbox):
110 # we need to keep track of the resources used by the pattern, hence
111 # we create our own registry, which we merge immediately in the main registry
112 patternregistry = pdfwriter.PDFregistry()
114 patternfile = cStringIO.StringIO()
115 realpatternbbox = bboxmodule.empty()
116 canvas._canvas.processPDF(self, patternfile, writer, pdfwriter.context(), patternregistry, realpatternbbox)
117 patternproc = patternfile.getvalue()
118 patternfile.close()
120 registry.mergeregistry(patternregistry)
122 if self.xstep is None:
123 xstep = unit.topt(realpatternbbox.width())
124 else:
125 xstep = unit.topt(self.xstep)
126 if self.ystep is None:
127 ystep = unit.topt(realpatternbbox.height())
128 else:
129 ystep = unit.topt(self.ystep)
130 if not xstep:
131 raise ValueError("xstep in pattern cannot be zero")
132 if not ystep:
133 raise ValueError("ystep in pattern cannot be zero")
134 patternbbox = self.patternbbox or realpatternbbox.enlarged(5*unit.pt)
135 patterntrafo = self.patterntrafo or trafo.trafo()
137 registry.add(PDFpattern(self.id, self.patterntype, self.painttype, self.tilingtype,
138 patternbbox, xstep, ystep, patterntrafo, patternproc, writer, registry, patternregistry))
140 # activate pattern
141 if context.colorspace != "Pattern":
142 # we only set the fill color space (see next comment)
143 file.write("/Pattern cs\n")
144 context.colorspace = "Pattern"
145 if context.strokeattr:
146 # using patterns as stroke colors doesn't seem to work, so
147 # we just don't do this...
148 warnings.warn("ignoring stroke color for patterns in PDF")
149 if context.fillattr:
150 file.write("/%s scn\n"% self.id)
153 pattern.clear = attr.clearclass(pattern)
156 _base = 0.1 * unit.v_cm
158 class hatched(pattern):
159 def __init__(self, dist, angle, strokestyles=[]):
160 pattern.__init__(self, painttype=1, tilingtype=1, xstep=dist, ystep=100*unit.t_pt, bbox=None, trafo=trafo.rotate(angle))
161 self.strokestyles = attr.mergeattrs([style.linewidth.THIN] + strokestyles)
162 attr.checkattrs(self.strokestyles, [style.strokestyle])
163 self.dist = dist
164 self.angle = angle
165 self.stroke(path.line_pt(0, -50, 0, 50), self.strokestyles)
167 def __call__(self, dist=None, angle=None, strokestyles=None):
168 if dist is None:
169 dist = self.dist
170 if angle is None:
171 angle = self.angle
172 if strokestyles is None:
173 strokestyles = self.strokestyles
174 return hatched(dist, angle, strokestyles)
176 hatched0 = hatched(_base, 0)
177 hatched0.SMALL = hatched0(_base/math.sqrt(64))
178 hatched0.SMALL = hatched0(_base/math.sqrt(64))
179 hatched0.SMALl = hatched0(_base/math.sqrt(32))
180 hatched0.SMAll = hatched0(_base/math.sqrt(16))
181 hatched0.SMall = hatched0(_base/math.sqrt(8))
182 hatched0.Small = hatched0(_base/math.sqrt(4))
183 hatched0.small = hatched0(_base/math.sqrt(2))
184 hatched0.normal = hatched0(_base)
185 hatched0.large = hatched0(_base*math.sqrt(2))
186 hatched0.Large = hatched0(_base*math.sqrt(4))
187 hatched0.LArge = hatched0(_base*math.sqrt(8))
188 hatched0.LARge = hatched0(_base*math.sqrt(16))
189 hatched0.LARGe = hatched0(_base*math.sqrt(32))
190 hatched0.LARGE = hatched0(_base*math.sqrt(64))
192 hatched45 = hatched(_base, 45)
193 hatched45.SMALL = hatched45(_base/math.sqrt(64))
194 hatched45.SMALl = hatched45(_base/math.sqrt(32))
195 hatched45.SMAll = hatched45(_base/math.sqrt(16))
196 hatched45.SMall = hatched45(_base/math.sqrt(8))
197 hatched45.Small = hatched45(_base/math.sqrt(4))
198 hatched45.small = hatched45(_base/math.sqrt(2))
199 hatched45.normal = hatched45(_base)
200 hatched45.large = hatched45(_base*math.sqrt(2))
201 hatched45.Large = hatched45(_base*math.sqrt(4))
202 hatched45.LArge = hatched45(_base*math.sqrt(8))
203 hatched45.LARge = hatched45(_base*math.sqrt(16))
204 hatched45.LARGe = hatched45(_base*math.sqrt(32))
205 hatched45.LARGE = hatched45(_base*math.sqrt(64))
207 hatched90 = hatched(_base, 90)
208 hatched90.SMALL = hatched90(_base/math.sqrt(64))
209 hatched90.SMALl = hatched90(_base/math.sqrt(32))
210 hatched90.SMAll = hatched90(_base/math.sqrt(16))
211 hatched90.SMall = hatched90(_base/math.sqrt(8))
212 hatched90.Small = hatched90(_base/math.sqrt(4))
213 hatched90.small = hatched90(_base/math.sqrt(2))
214 hatched90.normal = hatched90(_base)
215 hatched90.large = hatched90(_base*math.sqrt(2))
216 hatched90.Large = hatched90(_base*math.sqrt(4))
217 hatched90.LArge = hatched90(_base*math.sqrt(8))
218 hatched90.LARge = hatched90(_base*math.sqrt(16))
219 hatched90.LARGe = hatched90(_base*math.sqrt(32))
220 hatched90.LARGE = hatched90(_base*math.sqrt(64))
222 hatched135 = hatched(_base, 135)
223 hatched135.SMALL = hatched135(_base/math.sqrt(64))
224 hatched135.SMALl = hatched135(_base/math.sqrt(32))
225 hatched135.SMAll = hatched135(_base/math.sqrt(16))
226 hatched135.SMall = hatched135(_base/math.sqrt(8))
227 hatched135.Small = hatched135(_base/math.sqrt(4))
228 hatched135.small = hatched135(_base/math.sqrt(2))
229 hatched135.normal = hatched135(_base)
230 hatched135.large = hatched135(_base*math.sqrt(2))
231 hatched135.Large = hatched135(_base*math.sqrt(4))
232 hatched135.LArge = hatched135(_base*math.sqrt(8))
233 hatched135.LARge = hatched135(_base*math.sqrt(16))
234 hatched135.LARGe = hatched135(_base*math.sqrt(32))
235 hatched135.LARGE = hatched135(_base*math.sqrt(64))
238 class crosshatched(pattern):
239 def __init__(self, dist, angle, strokestyles=[]):
240 pattern.__init__(self, painttype=1, tilingtype=1, xstep=dist, ystep=dist, bbox=None, trafo=trafo.rotate(angle))
241 self.strokestyles = attr.mergeattrs([style.linewidth.THIN] + strokestyles)
242 attr.checkattrs(self.strokestyles, [style.strokestyle])
243 self.dist = dist
244 self.angle = angle
245 self.stroke(path.line_pt(0, 0, 0, unit.topt(dist)), self.strokestyles)
246 self.stroke(path.line_pt(0, 0, unit.topt(dist), 0), self.strokestyles)
248 def __call__(self, dist=None, angle=None, strokestyles=None):
249 if dist is None:
250 dist = self.dist
251 if angle is None:
252 angle = self.angle
253 if strokestyles is None:
254 strokestyles = self.strokestyles
255 return crosshatched(dist, angle, strokestyles)
257 crosshatched0 = crosshatched(_base, 0)
258 crosshatched0.SMALL = crosshatched0(_base/math.sqrt(64))
259 crosshatched0.SMALl = crosshatched0(_base/math.sqrt(32))
260 crosshatched0.SMAll = crosshatched0(_base/math.sqrt(16))
261 crosshatched0.SMall = crosshatched0(_base/math.sqrt(8))
262 crosshatched0.Small = crosshatched0(_base/math.sqrt(4))
263 crosshatched0.small = crosshatched0(_base/math.sqrt(2))
264 crosshatched0.normal = crosshatched0
265 crosshatched0.large = crosshatched0(_base*math.sqrt(2))
266 crosshatched0.Large = crosshatched0(_base*math.sqrt(4))
267 crosshatched0.LArge = crosshatched0(_base*math.sqrt(8))
268 crosshatched0.LARge = crosshatched0(_base*math.sqrt(16))
269 crosshatched0.LARGe = crosshatched0(_base*math.sqrt(32))
270 crosshatched0.LARGE = crosshatched0(_base*math.sqrt(64))
272 crosshatched45 = crosshatched(_base, 45)
273 crosshatched45.SMALL = crosshatched45(_base/math.sqrt(64))
274 crosshatched45.SMALl = crosshatched45(_base/math.sqrt(32))
275 crosshatched45.SMAll = crosshatched45(_base/math.sqrt(16))
276 crosshatched45.SMall = crosshatched45(_base/math.sqrt(8))
277 crosshatched45.Small = crosshatched45(_base/math.sqrt(4))
278 crosshatched45.small = crosshatched45(_base/math.sqrt(2))
279 crosshatched45.normal = crosshatched45
280 crosshatched45.large = crosshatched45(_base*math.sqrt(2))
281 crosshatched45.Large = crosshatched45(_base*math.sqrt(4))
282 crosshatched45.LArge = crosshatched45(_base*math.sqrt(8))
283 crosshatched45.LARge = crosshatched45(_base*math.sqrt(16))
284 crosshatched45.LARGe = crosshatched45(_base*math.sqrt(32))
285 crosshatched45.LARGE = crosshatched45(_base*math.sqrt(64))
288 class PDFpattern(pdfwriter.PDFobject):
290 def __init__(self, name, patterntype, painttype, tilingtype, bbox, xstep, ystep, trafo,
291 patternproc, writer, registry, patternregistry):
292 self.patternregistry = patternregistry
293 pdfwriter.PDFobject.__init__(self, "pattern", name)
294 registry.addresource("Pattern", name, self)
296 self.name = name
297 self.patterntype = patterntype
298 self.painttype = painttype
299 self.tilingtype = tilingtype
300 self.bbox = bbox
301 self.xstep = xstep
302 self.ystep = ystep
303 self.trafo = trafo
304 self.patternproc = patternproc
306 def write(self, file, writer, registry):
307 file.write("<<\n"
308 "/Type /Pattern\n"
309 "/PatternType %d\n" % self.patterntype)
310 file.write("/PaintType %d\n" % self.painttype)
311 file.write("/TilingType %d\n" % self.tilingtype)
312 file.write("/BBox [%d %d %d %d]\n" % self.bbox.lowrestuple_pt())
313 file.write("/XStep %f\n" % self.xstep)
314 file.write("/YStep %f\n" % self.ystep)
315 file.write("/Matrix %s\n" % str(self.trafo))
316 self.patternregistry.writeresources(file)
317 if writer.compress:
318 import zlib
319 content = zlib.compress(self.patternproc)
320 else:
321 content = self.patterproc
323 file.write("/Length %i\n" % len(content))
324 if writer.compress:
325 file.write("/Filter /FlateDecode\n")
326 file.write(">>\n"
327 "stream\n")
328 file.write(content)
329 file.write("endstream\n")