Hi Michael,

On 29.06.06, Michael J Gruber wrote:
> That code worked around the time of PyX 0.6.3 but not much longer, I
> guess. Back then there was only PS which is why getting the PS fragment
> in translate_pt() worked that way. Now the canvas doesn't know whether
> it's going to be output as PS or PDF (or SVG...).
> 
> I couldn't resist taking a shot at this. The attached code derives an
> insertstyle from graph.style.symbol which can be used in exactly the
> same way (this is different from the original code you mention) but
> inserts references in the PS instead of repeating the paths (following
> Andre's ideas). The savings are only on the PS side, not in python
> (there are canvas instances for each symbol being drawn).

Indeed.

> P.S.: Works with PyX 0.9 and SVN, not with 0.8.1
> P.P.S.: Code is not optimal, I know... I said "shot" ;)

Hey, not bad for a "shot".

> import cStringIO
> from pyx import *
> from pyx import pswriter
>  
>  
> class ref(canvas.canvasitem):
>     """a stupid object insertable into a canvas; it needs to get all the
>     information in the constructor"""
> 
>     def __init__(self, bbox, output, insertcanvas):
>         self._bbox = bbox
>         self._output = output
>         self._insertcanvas = insertcanvas
> 
>     def bbox(self):
>         return self._bbox
> 
>     def processPS(self, file, writer, context, registry, bbox):

You also have to update the bounding box, i.e.
          bbox += self._bbox

>         stringfile = cStringIO.StringIO()
>         canvas._canvas.processPS(self._insertcanvas, stringfile, writer, 
> context, registry, bbox)
>         canvasproc = "gsave\nmatrix translate concat\n%sgrestore\n" % 
> stringfile.getvalue()
>         stringfile.close()
>         registry.add(pswriter.PSdefinition(self._insertcanvas._id, "{\n" + 
> canvasproc + "}"))
>         file.write(self._output)
> 
>     # need help here
>     def processPDF(self, file, writer, context, registry, bbox): 
>         raise NotImplementedError("ref canvasitem not implemented for PDF")

This goes along the lines of the PS case.

> class translateablecanvas(canvas._canvas):
>     "a canvas which is efficiently translateable"
>      
>     def __init__(self, *args, **kwargs):
>         canvas._canvas.__init__(self, *args, **kwargs)
>         self._id = "symbol%d" % id(self)
>  
>     def translate_pt(self, x_pt, y_pt):
>         """returns an insertable object which stores only the position and
>         a reference to a prolog definition"""
>         return ref(self.bbox().transformed(trafo.translate_pt(x_pt, y_pt)), 
> "%g %g %s\n" % (x_pt, y_pt, self._id), self)
>  
> class insertstyle(graph.style.symbol):
>     """a graph style which calls a translate_pt method of the symbol to insert
>     the result instead of calling the symbol to get a path to be drawn"""
> 
>     def selectstyle(self, privatedata, sharedata, graph, selectindex, 
> selecttotal):
>         privatedata.symbol = attr.selectattr(self.symbol, selectindex, 
> selecttotal)
>         privatedata.size_pt = unit.topt(attr.selectattr(self.size, 
> selectindex, selecttotal))
>         privatedata.insertcanvas = translateablecanvas()
>         if self.symbolattrs is not None:
>             privatedata.symbolattrs = 
> attr.selectattrs(self.defaultsymbolattrs + self.symbolattrs, selectindex, 
> selecttotal)
>         else:
>             privatedata.symbolattrs = None
>         # stroke symbol on insertcanvas instead of symbolcanvas
>         privatedata.symbol(privatedata.insertcanvas, 0, 0, 
> privatedata.size_pt, privatedata.symbolattrs)
> 
>     def drawpoint(self, privatedata, sharedata, graph, point):
>         if sharedata.vposvalid and privatedata.symbolattrs is not None:
>             x_pt, y_pt = graph.vpos_pt(*sharedata.vpos)
>             # use more efficient version of 
> privatedata.symbolcanvas.insert(privatedata.insertcanvas,[trafo.translate_pt(x_pt,
>  y_pt)])
>             
> privatedata.symbolcanvas.insert(privatedata.insertcanvas.translate_pt(x_pt, 
> y_pt))
> 
>     # key_pt is inherited from graph.style.symbol because reimplementing 
> would save only one instance

Looks good.

        Jörg

Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
PyX-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/pyx-user

Reply via email to