update upload data
[PyX.git] / pyx / pattern.py
blob57e4daed628d2254b4aa9d976e14c508505c88dc
1 # -*- encoding: utf-8 -*-
4 # Copyright (C) 2002-2011 Jörg Lehmann <joergl@users.sourceforge.net>
5 # Copyright (C) 2002-2011 André Wobst <wobsta@users.sourceforge.net>
7 # This file is part of PyX (http://pyx.sourceforge.net/).
9 # PyX is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 2 of the License, or
12 # (at your option) any later version.
14 # PyX is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
19 # You should have received a copy of the GNU General Public License
20 # along with PyX; if not, write to the Free Software
21 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 import cStringIO, math, warnings
24 import attr, canvas, path, pdfwriter, pswriter, style, unit, trafo
25 import bbox as bboxmodule
27 class _marker: pass
29 # TODO: pattern should not derive from canvas but wrap a canvas
31 class pattern(canvas._canvas, attr.exclusiveattr, style.fillstyle):
33 def __init__(self, painttype=1, tilingtype=1, xstep=None, ystep=None,
34 bbox=None, trafo=None, bboxenlarge=5*unit.t_pt, **kwargs):
35 canvas._canvas.__init__(self, **kwargs)
36 attr.exclusiveattr.__init__(self, pattern)
37 self.id = "pattern%d" % id(self)
38 self.patterntype = 1
39 if painttype not in (1, 2):
40 raise ValueError("painttype must be 1 or 2")
41 self.painttype = painttype
42 if tilingtype not in (1, 2, 3):
43 raise ValueError("tilingtype must be 1, 2, or 3")
44 self.tilingtype = tilingtype
45 self.xstep = xstep
46 self.ystep = ystep
47 self.patternbbox = bbox
48 self.patterntrafo = trafo
49 self.bboxenlarge = bboxenlarge
51 def __call__(self, painttype=_marker, tilingtype=_marker, xstep=_marker, ystep=_marker,
52 bbox=_marker, trafo=_marker, bboxenlarge=_marker):
53 if painttype is _marker:
54 painttype = self.painttype
55 if tilingtype is _marker:
56 tilingtype = self.tilingtype
57 if xstep is _marker:
58 xstep = self.xstep
59 if ystep is _marker:
60 ystep = self.ystep
61 if bbox is _marker:
62 bbox = self.bbox
63 if trafo is _marker:
64 trafo = self.trafo
65 if bboxenlarge is _marker:
66 bboxenlarge = self.bboxenlarge
67 return pattern(painttype, tilingtype, xstep, ystep, bbox, trafo, bboxenlarge)
69 def bbox(self):
70 return bboxmodule.empty()
72 def processPS(self, file, writer, context, registry, bbox):
73 # process pattern, letting it register its resources and calculate the bbox of the pattern
74 patternfile = cStringIO.StringIO()
75 realpatternbbox = bboxmodule.empty()
76 canvas._canvas.processPS(self, patternfile, writer, pswriter.context(), registry, realpatternbbox)
77 patternproc = patternfile.getvalue()
78 patternfile.close()
80 if self.xstep is None:
81 xstep = unit.topt(realpatternbbox.width())
82 else:
83 xstep = unit.topt(self.xstep)
84 if self.ystep is None:
85 ystep = unit.topt(realpatternbbox.height())
86 else:
87 ystep = unit.topt(self.ystep)
88 if not xstep:
89 raise ValueError("xstep in pattern cannot be zero")
90 if not ystep:
91 raise ValueError("ystep in pattern cannot be zero")
92 patternbbox = self.patternbbox or realpatternbbox.enlarged(self.bboxenlarge)
94 patternprefix = "\n".join(("<<",
95 "/PatternType %d" % self.patterntype,
96 "/PaintType %d" % self.painttype,
97 "/TilingType %d" % self.tilingtype,
98 "/BBox [%g %g %g %g]" % patternbbox.highrestuple_pt(),
99 "/XStep %g" % xstep,
100 "/YStep %g" % ystep,
101 "/PaintProc {\nbegin\n"))
102 patterntrafostring = self.patterntrafo is None and "matrix" or str(self.patterntrafo)
103 patternsuffix = "end\n} bind\n>>\n%s\nmakepattern" % patterntrafostring
105 registry.add(pswriter.PSdefinition(self.id, "".join((patternprefix, patternproc, patternsuffix))))
107 # activate pattern
108 file.write("%s setpattern\n" % self.id)
110 def processPDF(self, file, writer, context, registry, bbox):
111 # we need to keep track of the resources used by the pattern, hence
112 # we create our own registry, which we merge immediately in the main registry
113 patternregistry = pdfwriter.PDFregistry()
115 patternfile = cStringIO.StringIO()
116 realpatternbbox = bboxmodule.empty()
117 canvas._canvas.processPDF(self, patternfile, writer, pdfwriter.context(), patternregistry, realpatternbbox)
118 patternproc = patternfile.getvalue()
119 patternfile.close()
121 registry.mergeregistry(patternregistry)
123 if self.xstep is None:
124 xstep = unit.topt(realpatternbbox.width())
125 else:
126 xstep = unit.topt(self.xstep)
127 if self.ystep is None:
128 ystep = unit.topt(realpatternbbox.height())
129 else:
130 ystep = unit.topt(self.ystep)
131 if not xstep:
132 raise ValueError("xstep in pattern cannot be zero")
133 if not ystep:
134 raise ValueError("ystep in pattern cannot be zero")
135 patternbbox = self.patternbbox or realpatternbbox.enlarged(5*unit.pt)
136 patterntrafo = self.patterntrafo or trafo.trafo()
138 registry.add(PDFpattern(self.id, self.patterntype, self.painttype, self.tilingtype,
139 patternbbox, xstep, ystep, patterntrafo, patternproc, writer, registry, patternregistry))
141 # activate pattern
142 if context.colorspace != "Pattern":
143 # we only set the fill color space (see next comment)
144 file.write("/Pattern cs\n")
145 context.colorspace = "Pattern"
146 if context.strokeattr:
147 # using patterns as stroke colors doesn't seem to work, so
148 # we just don't do this...
149 warnings.warn("ignoring stroke color for patterns in PDF")
150 if context.fillattr:
151 file.write("/%s scn\n"% self.id)
154 pattern.clear = attr.clearclass(pattern)
157 _base = 0.1 * unit.v_cm
159 class hatched(pattern):
160 def __init__(self, dist, angle, strokestyles=[]):
161 pattern.__init__(self, painttype=1, tilingtype=1, xstep=dist, ystep=100*unit.t_pt, bbox=None, trafo=trafo.rotate(angle))
162 self.strokestyles = attr.mergeattrs([style.linewidth.THIN] + strokestyles)
163 attr.checkattrs(self.strokestyles, [style.strokestyle])
164 self.dist = dist
165 self.angle = angle
166 self.stroke(path.line_pt(0, -50, 0, 50), self.strokestyles)
168 def __call__(self, dist=None, angle=None, strokestyles=None):
169 if dist is None:
170 dist = self.dist
171 if angle is None:
172 angle = self.angle
173 if strokestyles is None:
174 strokestyles = self.strokestyles
175 return hatched(dist, angle, strokestyles)
177 hatched0 = hatched(_base, 0)
178 hatched0.SMALL = hatched0(_base/math.sqrt(64))
179 hatched0.SMALL = hatched0(_base/math.sqrt(64))
180 hatched0.SMALl = hatched0(_base/math.sqrt(32))
181 hatched0.SMAll = hatched0(_base/math.sqrt(16))
182 hatched0.SMall = hatched0(_base/math.sqrt(8))
183 hatched0.Small = hatched0(_base/math.sqrt(4))
184 hatched0.small = hatched0(_base/math.sqrt(2))
185 hatched0.normal = hatched0(_base)
186 hatched0.large = hatched0(_base*math.sqrt(2))
187 hatched0.Large = hatched0(_base*math.sqrt(4))
188 hatched0.LArge = hatched0(_base*math.sqrt(8))
189 hatched0.LARge = hatched0(_base*math.sqrt(16))
190 hatched0.LARGe = hatched0(_base*math.sqrt(32))
191 hatched0.LARGE = hatched0(_base*math.sqrt(64))
193 hatched45 = hatched(_base, 45)
194 hatched45.SMALL = hatched45(_base/math.sqrt(64))
195 hatched45.SMALl = hatched45(_base/math.sqrt(32))
196 hatched45.SMAll = hatched45(_base/math.sqrt(16))
197 hatched45.SMall = hatched45(_base/math.sqrt(8))
198 hatched45.Small = hatched45(_base/math.sqrt(4))
199 hatched45.small = hatched45(_base/math.sqrt(2))
200 hatched45.normal = hatched45(_base)
201 hatched45.large = hatched45(_base*math.sqrt(2))
202 hatched45.Large = hatched45(_base*math.sqrt(4))
203 hatched45.LArge = hatched45(_base*math.sqrt(8))
204 hatched45.LARge = hatched45(_base*math.sqrt(16))
205 hatched45.LARGe = hatched45(_base*math.sqrt(32))
206 hatched45.LARGE = hatched45(_base*math.sqrt(64))
208 hatched90 = hatched(_base, 90)
209 hatched90.SMALL = hatched90(_base/math.sqrt(64))
210 hatched90.SMALl = hatched90(_base/math.sqrt(32))
211 hatched90.SMAll = hatched90(_base/math.sqrt(16))
212 hatched90.SMall = hatched90(_base/math.sqrt(8))
213 hatched90.Small = hatched90(_base/math.sqrt(4))
214 hatched90.small = hatched90(_base/math.sqrt(2))
215 hatched90.normal = hatched90(_base)
216 hatched90.large = hatched90(_base*math.sqrt(2))
217 hatched90.Large = hatched90(_base*math.sqrt(4))
218 hatched90.LArge = hatched90(_base*math.sqrt(8))
219 hatched90.LARge = hatched90(_base*math.sqrt(16))
220 hatched90.LARGe = hatched90(_base*math.sqrt(32))
221 hatched90.LARGE = hatched90(_base*math.sqrt(64))
223 hatched135 = hatched(_base, 135)
224 hatched135.SMALL = hatched135(_base/math.sqrt(64))
225 hatched135.SMALl = hatched135(_base/math.sqrt(32))
226 hatched135.SMAll = hatched135(_base/math.sqrt(16))
227 hatched135.SMall = hatched135(_base/math.sqrt(8))
228 hatched135.Small = hatched135(_base/math.sqrt(4))
229 hatched135.small = hatched135(_base/math.sqrt(2))
230 hatched135.normal = hatched135(_base)
231 hatched135.large = hatched135(_base*math.sqrt(2))
232 hatched135.Large = hatched135(_base*math.sqrt(4))
233 hatched135.LArge = hatched135(_base*math.sqrt(8))
234 hatched135.LARge = hatched135(_base*math.sqrt(16))
235 hatched135.LARGe = hatched135(_base*math.sqrt(32))
236 hatched135.LARGE = hatched135(_base*math.sqrt(64))
239 class crosshatched(pattern):
240 def __init__(self, dist, angle, strokestyles=[]):
241 pattern.__init__(self, painttype=1, tilingtype=1, xstep=dist, ystep=dist, bbox=None, trafo=trafo.rotate(angle))
242 self.strokestyles = attr.mergeattrs([style.linewidth.THIN] + strokestyles)
243 attr.checkattrs(self.strokestyles, [style.strokestyle])
244 self.dist = dist
245 self.angle = angle
246 self.stroke(path.line_pt(0, 0, 0, unit.topt(dist)), self.strokestyles)
247 self.stroke(path.line_pt(0, 0, unit.topt(dist), 0), self.strokestyles)
249 def __call__(self, dist=None, angle=None, strokestyles=None):
250 if dist is None:
251 dist = self.dist
252 if angle is None:
253 angle = self.angle
254 if strokestyles is None:
255 strokestyles = self.strokestyles
256 return crosshatched(dist, angle, strokestyles)
258 crosshatched0 = crosshatched(_base, 0)
259 crosshatched0.SMALL = crosshatched0(_base/math.sqrt(64))
260 crosshatched0.SMALl = crosshatched0(_base/math.sqrt(32))
261 crosshatched0.SMAll = crosshatched0(_base/math.sqrt(16))
262 crosshatched0.SMall = crosshatched0(_base/math.sqrt(8))
263 crosshatched0.Small = crosshatched0(_base/math.sqrt(4))
264 crosshatched0.small = crosshatched0(_base/math.sqrt(2))
265 crosshatched0.normal = crosshatched0
266 crosshatched0.large = crosshatched0(_base*math.sqrt(2))
267 crosshatched0.Large = crosshatched0(_base*math.sqrt(4))
268 crosshatched0.LArge = crosshatched0(_base*math.sqrt(8))
269 crosshatched0.LARge = crosshatched0(_base*math.sqrt(16))
270 crosshatched0.LARGe = crosshatched0(_base*math.sqrt(32))
271 crosshatched0.LARGE = crosshatched0(_base*math.sqrt(64))
273 crosshatched45 = crosshatched(_base, 45)
274 crosshatched45.SMALL = crosshatched45(_base/math.sqrt(64))
275 crosshatched45.SMALl = crosshatched45(_base/math.sqrt(32))
276 crosshatched45.SMAll = crosshatched45(_base/math.sqrt(16))
277 crosshatched45.SMall = crosshatched45(_base/math.sqrt(8))
278 crosshatched45.Small = crosshatched45(_base/math.sqrt(4))
279 crosshatched45.small = crosshatched45(_base/math.sqrt(2))
280 crosshatched45.normal = crosshatched45
281 crosshatched45.large = crosshatched45(_base*math.sqrt(2))
282 crosshatched45.Large = crosshatched45(_base*math.sqrt(4))
283 crosshatched45.LArge = crosshatched45(_base*math.sqrt(8))
284 crosshatched45.LARge = crosshatched45(_base*math.sqrt(16))
285 crosshatched45.LARGe = crosshatched45(_base*math.sqrt(32))
286 crosshatched45.LARGE = crosshatched45(_base*math.sqrt(64))
289 class PDFpattern(pdfwriter.PDFobject):
291 def __init__(self, name, patterntype, painttype, tilingtype, bbox, xstep, ystep, trafo,
292 patternproc, writer, registry, patternregistry):
293 self.patternregistry = patternregistry
294 pdfwriter.PDFobject.__init__(self, "pattern", name)
295 registry.addresource("Pattern", name, self)
297 self.name = name
298 self.patterntype = patterntype
299 self.painttype = painttype
300 self.tilingtype = tilingtype
301 self.bbox = bbox
302 self.xstep = xstep
303 self.ystep = ystep
304 self.trafo = trafo
305 self.patternproc = patternproc
307 def write(self, file, writer, registry):
308 file.write("<<\n"
309 "/Type /Pattern\n"
310 "/PatternType %d\n" % self.patterntype)
311 file.write("/PaintType %d\n" % self.painttype)
312 file.write("/TilingType %d\n" % self.tilingtype)
313 file.write("/BBox [%d %d %d %d]\n" % self.bbox.lowrestuple_pt())
314 file.write("/XStep %f\n" % self.xstep)
315 file.write("/YStep %f\n" % self.ystep)
316 file.write("/Matrix %s\n" % str(self.trafo))
317 file.write("/Resources ")
318 self.patternregistry.writeresources(file)
319 if writer.compress:
320 import zlib
321 content = zlib.compress(self.patternproc)
322 else:
323 content = self.patternproc
325 file.write("/Length %i\n" % len(content))
326 if writer.compress:
327 file.write("/Filter /FlateDecode\n")
328 file.write(">>\n"
329 "stream\n")
330 file.write(content)
331 file.write("endstream\n")