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
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
)
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
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
66 return pattern(painttype
, tilingtype
, xstep
, ystep
, bbox
, trafo
)
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()
79 if self
.xstep
is None:
80 xstep
= unit
.topt(realpatternbbox
.width())
82 xstep
= unit
.topt(self
.xstep
)
83 if self
.ystep
is None:
84 ystep
= unit
.topt(realpatternbbox
.height())
86 ystep
= unit
.topt(self
.ystep
)
88 raise ValueError("xstep in pattern cannot be zero")
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(),
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
))))
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()
120 registry
.mergeregistry(patternregistry
)
122 if self
.xstep
is None:
123 xstep
= unit
.topt(realpatternbbox
.width())
125 xstep
= unit
.topt(self
.xstep
)
126 if self
.ystep
is None:
127 ystep
= unit
.topt(realpatternbbox
.height())
129 ystep
= unit
.topt(self
.ystep
)
131 raise ValueError("xstep in pattern cannot be zero")
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
))
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")
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
=1000*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
])
165 self
.stroke(path
.line_pt(0, -500, 0, 500), self
.strokestyles
)
167 def __call__(self
, dist
=None, angle
=None, strokestyles
=None):
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
])
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):
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
)
297 self
.patterntype
= patterntype
298 self
.painttype
= painttype
299 self
.tilingtype
= tilingtype
304 self
.patternproc
= patternproc
306 def write(self
, file, writer
, registry
):
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)
319 content
= zlib
.compress(self
.patternproc
)
321 content
= self
.patterproc
323 file.write("/Length %i\n" % len(content
))
325 file.write("/Filter /FlateDecode\n")
329 file.write("endstream\n")