Arnd Baecker venit, vidit, dixit 2006-06-28 22:25:
> Hi Andre,
> 
> some time ago you sent me the ``translateablecanvas.py``,
> http://sourceforge.net/mailarchive/message.php?msg_id=9364374
> 
> 
> Unfortunately, it does not work anymore with a recent PyX
> (at some point you already sent me an updated version
> compared to the one at the link,
> but I can't find it in the archive.
> This - updated one - makes use of base.canvasitem, which
> presently should be  canvas.canvasitem, I'd guess.
> Still, my attempts at a  conversion failed as
> quite a few things seem to have changed since then
> and I don't understand the details well enough.

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).

insertstyle should be able to handle changeable attributes but I haven't
tried.

Cheers,
Michael

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" ;)
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):
        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")

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


 
g = graph.graphxy(width=8)
#g.plot(graph.data.function("y(x)=x*x", min=-1, max=1, points=1000),
#       [graph.style.symbol(graph.style.symbol.circle, 
symbolattrs=[deco.filled,color.rgb.blue])])
#g.plot(graph.data.function("y(x)=x*x", min=-1, max=1, points=1000),
#       [graph.style.symbol(graph.style.symbol.circle, 
symbolattrs=[deco.filled,color.rgb.blue, deco.stroked.clear])])
g.plot(graph.data.function("y(x)=x*x", min=-1, max=1, points=1000),
        [insertstyle(graph.style.symbol.circle, 
symbolattrs=[deco.filled,color.rgb.blue, deco.stroked.clear])])
g.writeEPSfile("translateablecanvas")
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