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
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
)
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
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
65 if bboxenlarge
is _marker
:
66 bboxenlarge
= self
.bboxenlarge
67 return pattern(painttype
, tilingtype
, xstep
, ystep
, bbox
, trafo
, bboxenlarge
)
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()
80 if self
.xstep
is None:
81 xstep
= unit
.topt(realpatternbbox
.width())
83 xstep
= unit
.topt(self
.xstep
)
84 if self
.ystep
is None:
85 ystep
= unit
.topt(realpatternbbox
.height())
87 ystep
= unit
.topt(self
.ystep
)
89 raise ValueError("xstep in pattern cannot be zero")
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(),
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
))))
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()
121 registry
.mergeregistry(patternregistry
)
123 if self
.xstep
is None:
124 xstep
= unit
.topt(realpatternbbox
.width())
126 xstep
= unit
.topt(self
.xstep
)
127 if self
.ystep
is None:
128 ystep
= unit
.topt(realpatternbbox
.height())
130 ystep
= unit
.topt(self
.ystep
)
132 raise ValueError("xstep in pattern cannot be zero")
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
))
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")
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
])
166 self
.stroke(path
.line_pt(0, -50, 0, 50), self
.strokestyles
)
168 def __call__(self
, dist
=None, angle
=None, strokestyles
=None):
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
])
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):
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
)
298 self
.patterntype
= patterntype
299 self
.painttype
= painttype
300 self
.tilingtype
= tilingtype
305 self
.patternproc
= patternproc
307 def write(self
, file, writer
, registry
):
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)
321 content
= zlib
.compress(self
.patternproc
)
323 content
= self
.patternproc
325 file.write("/Length %i\n" % len(content
))
327 file.write("/Filter /FlateDecode\n")
331 file.write("endstream\n")