From 1bba0e9414ae939c8be0a5043dda511776c741d3 Mon Sep 17 00:00:00 2001 From: Marco Pesenti Gritti Date: Mon, 15 May 2006 19:05:09 +0000 Subject: Merge --- (limited to 'sugar') diff --git a/sugar/chat/SVGdraw.py b/sugar/chat/SVGdraw.py index abcda11..cbd8729 100644 --- a/sugar/chat/SVGdraw.py +++ b/sugar/chat/SVGdraw.py @@ -69,6 +69,7 @@ __version__="1.0" # Anyway the text based approach is about 60 times faster than using the full dom implementation. use_dom_implementation=0 +from xml.parsers import expat import exceptions if use_dom_implementation<>0: @@ -226,7 +227,13 @@ class Attribute: self.value = value self.nsname = nsname self.nsref = nsref - + def __repr__(self): + return "(%s=%s, ns: %s=%s)" % (self.name, self.value, self.nsname, self.nsref) + +def get_attr_value(attrs, name): + return attrs[name] + + class SVGelement: """SVGelement(type,attributes,elements,text,namespace,**args) Creates a arbitrary svg element and is intended to be subclassed not used on its own. @@ -266,9 +273,84 @@ class SVGelement: def setParent(self, parent): self._parent = parent - def addAttribute(self, attribute): + def setAttribute(self, attribute, replace=True): + if not replace and self.hasAttribute(attribute.name): + return self._attributes[attribute.name] = attribute + def delAttribute(self, name): + if name in self._attributes.keys(): + del self._attributes[name] + + def hasAttribute(self, name): + if name in self._attributes.keys(): + return True + return False + + def _construct(attributes): + raise Exception("Can't construct a default object.") + _construct = staticmethod(_construct) + + def _get_namespace(attrname, nslist): + colon_idx = attrname.find(':') + if colon_idx <= 0: + return (attrname, None, None) + nsname = attrname[:colon_idx] + nsref = None + attrname = attrname[colon_idx+1:] + for (ns, val) in nslist: + if ns == nsname: + nsref = val + break + if not nsref: + nsname = None + return (attrname, nsname, nsref) + _get_namespace = staticmethod(_get_namespace) + + _XMLNS_TAG = "xmlns:" + def construct(name, attributes, text=None, cdata=None): + try: + eltClass = elementTable[name] + except KeyError, e: + print "Unknown SVG element %s." % e + return None + # Coalesce namespaces into the attributes themselves + attr_dict = {} + elt_namespace = None + namespaces = [] + tmp_attrs = [] + # Separate namespaces from actual attributes + for attrname, attrvalue in attributes.items(): + if attrname.startswith(SVGelement._XMLNS_TAG): + namespaces.append((attrname[len(SVGelement._XMLNS_TAG):], attrvalue)) + elif attrname.startswith("xmlns"): + # Element-wide attribute + elt_namespace = attrvalue + else: + tmp_attrs.append((attrname, attrvalue)) + + # Create attributes and assign namespaces to them + for (attrname, attrvalue) in tmp_attrs: + nsname = nsref = None + attr = None + # search for its namespace, if any + (attrname, nsname, nsref) = SVGelement._get_namespace(attrname, namespaces) + attr = Attribute(attrname, attrvalue, nsname, nsref) + attr_dict[attrname] = attr + + element = eltClass._construct(attr_dict) + if element: + for attr in attr_dict.values(): + element.setAttribute(attr, replace=False) + if text: + element.text = text + if cdata: + element.cdata = cdata + if not element.namespace and elt_namespace: + element.namespace = elt_namespace + return element + construct = staticmethod(construct) + def toXml(self,level,f): f.write('\t'*level) f.write('<'+self.type) @@ -323,11 +405,16 @@ class tspan(SVGelement): def __repr__(self): s="None: - self.addAttribute(Attribute('x', x)) + self.setAttribute(Attribute('x', x)) if y<>None: - self.addAttribute(Attribute('y', y)) + self.setAttribute(Attribute('y', y)) if fill<>None: - self.addAttribute(Attribute('fill', fill)) + self.setAttribute(Attribute('fill', fill)) if stroke<>None: - self.addAttribute(Attribute('stroke', stroke)) + self.setAttribute(Attribute('stroke', stroke)) if stroke_width<>None: - self.addAttribute(Attribute('stroke-width', stroke_width)) - + self.setAttribute(Attribute('stroke-width', stroke_width)) + + def _construct(attributes): + width = get_attr_value(attributes, 'width') + height = get_attr_value(attributes, 'height') + return rect(width=width.value, height=height.value) + _construct = staticmethod(_construct) + + class ellipse(SVGelement): """e=ellipse(rx,ry,x,y,fill,stroke,stroke_width,**args) @@ -425,17 +525,23 @@ class ellipse(SVGelement): raise ValueError, 'both rx and ry are required' SVGelement.__init__(self,'ellipse',{'rx':rx,'ry':ry},**args) if cx<>None: - self.addAttribute(Attribute('cx', cx)) + self.setAttribute(Attribute('cx', cx)) if cy<>None: - self.addAttribute(Attribute('cy', cy)) + self.setAttribute(Attribute('cy', cy)) if fill<>None: - self.addAttribute(Attribute('fill', fill)) + self.setAttribute(Attribute('fill', fill)) if stroke<>None: - self.addAttribute(Attribute('stroke', stroke)) + self.setAttribute(Attribute('stroke', stroke)) if stroke_width<>None: - self.addAttribute(Attribute('stroke-width', stroke_width)) + self.setAttribute(Attribute('stroke-width', stroke_width)) - + def _construct(attributes): + rx = get_attr_value(attributes, 'rx') + ry = get_attr_value(attributes, 'ry') + return ellipse(rx=rx.value, ry=ry.value) + _construct = staticmethod(_construct) + + class circle(SVGelement): """c=circle(x,y,radius,fill,stroke,stroke_width,**args) @@ -446,15 +552,24 @@ class circle(SVGelement): raise ValueError, 'r is required' SVGelement.__init__(self,'circle',{'r':r},**args) if cx<>None: - self.addAttribute(Attribute('cx', cx)) + self.setAttribute(Attribute('cx', cx)) if cy<>None: - self.addAttribute(Attribute('cy', cy)) + self.setAttribute(Attribute('cy', cy)) if fill<>None: - self.addAttribute(Attribute('fill', fill)) + self.setAttribute(Attribute('fill', fill)) if stroke<>None: - self.addAttribute(Attribute('stroke', stroke)) + self.setAttribute(Attribute('stroke', stroke)) if stroke_width<>None: - self.addAttribute(Attribute('stroke-width', stroke_width)) + self.setAttribute(Attribute('stroke-width', stroke_width)) + + def _construct(attributes): + r = get_attr_value(attributes, 'r') + if int(r.value) == 1: + return point() + else: + return circle(r=r.value) + _construct = staticmethod(_construct) + class point(circle): """p=point(x,y,color) @@ -462,7 +577,7 @@ class point(circle): A point is defined as a circle with a size 1 radius. It may be more efficient to use a very small rectangle if you use many points because a circle is difficult to render. """ - def __init__(self,x,y,fill='black',**args): + def __init__(self,x=None,y=None,fill=None,**args): circle.__init__(self,x,y,1,fill,**args) class line(SVGelement): @@ -473,17 +588,22 @@ class line(SVGelement): def __init__(self,x1=None,y1=None,x2=None,y2=None,stroke=None,stroke_width=None,**args): SVGelement.__init__(self,'line',**args) if x1<>None: - self.addAttribute(Attribute('x1', x1)) + self.setAttribute(Attribute('x1', x1)) if y1<>None: - self.addAttribute(Attribute('y1', y1)) + self.setAttribute(Attribute('y1', y1)) if x2<>None: - self.addAttribute(Attribute('x2', x2)) + self.setAttribute(Attribute('x2', x2)) if y2<>None: - self.addAttribute(Attribute('y2', y2)) + self.setAttribute(Attribute('y2', y2)) if stroke_width<>None: - self.addAttribute(Attribute('stroke-width', stroke_width)) + self.setAttribute(Attribute('stroke-width', stroke_width)) if stroke<>None: - self.addAttribute(Attribute('stroke', stroke)) + self.setAttribute(Attribute('stroke', stroke)) + + def _construct(attributes): + return line() + _construct = staticmethod(_construct) + class polyline(SVGelement): """pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args) @@ -493,11 +613,17 @@ class polyline(SVGelement): def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args): SVGelement.__init__(self,'polyline',{'points':_xypointlist(points)},**args) if fill<>None: - self.addAttribute(Attribute('fill', fill)) + self.setAttribute(Attribute('fill', fill)) if stroke_width<>None: - self.addAttribute(Attribute('stroke-width', stroke_width)) + self.setAttribute(Attribute('stroke-width', stroke_width)) if stroke<>None: - self.addAttribute(Attribute('stroke', stroke)) + self.setAttribute(Attribute('stroke', stroke)) + + def _construct(attributes): + points = get_attr_value(attributes, 'points') + return polyline(points.value) + _construct = staticmethod(_construct) + class polygon(SVGelement): """pl=polyline([[x1,y1],[x2,y2],...],fill,stroke,stroke_width,**args) @@ -507,11 +633,17 @@ class polygon(SVGelement): def __init__(self,points,fill=None,stroke=None,stroke_width=None,**args): SVGelement.__init__(self,'polygon',{'points':_xypointlist(points)},**args) if fill<>None: - self.addAttribute(Attribute('fill', fill)) + self.setAttribute(Attribute('fill', fill)) if stroke_width<>None: - self.addAttribute(Attribute('stroke-width', stroke_width)) + self.setAttribute(Attribute('stroke-width', stroke_width)) if stroke<>None: - self.addAttribute(Attribute('stroke', stroke)) + self.setAttribute(Attribute('stroke', stroke)) + + def _construct(attributes): + points = get_attr_value(attributes, 'points') + return polygon(points.value) + _construct = staticmethod(_construct) + class path(SVGelement): """p=path(path,fill,stroke,stroke_width,**args) @@ -521,14 +653,19 @@ class path(SVGelement): def __init__(self,pathdata,fill=None,stroke=None,stroke_width=None,id=None,**args): SVGelement.__init__(self,'path',{'d':str(pathdata)},**args) if stroke<>None: - self.addAttribute(Attribute('stroke', stroke)) + self.setAttribute(Attribute('stroke', stroke)) if fill<>None: - self.addAttribute(Attribute('fill', fill)) + self.setAttribute(Attribute('fill', fill)) if stroke_width<>None: - self.addAttribute(Attribute('stroke-width', stroke_width)) + self.setAttribute(Attribute('stroke-width', stroke_width)) if id<>None: - self.addAttribute(Attribute('id', id)) + self.setAttribute(Attribute('id', id)) + def _construct(attributes): + pathdata = get_attr_value(attributes, 'd') + return path(pathdata.value) + _construct = staticmethod(_construct) + class text(SVGelement): """t=text(x,y,text,font_size,font_family,**args) @@ -538,17 +675,21 @@ class text(SVGelement): def __init__(self,x=None,y=None,text=None,font_size=None,font_family=None,text_anchor=None,**args): SVGelement.__init__(self,'text',**args) if x<>None: - self.addAttribute(Attribute('x', x)) + self.setAttribute(Attribute('x', x)) if y<>None: - self.addAttribute(Attribute('y', y)) + self.setAttribute(Attribute('y', y)) if font_size<>None: - self.addAttribute(Attribute('font-size', font_size)) + self.setAttribute(Attribute('font-size', font_size)) if font_family<>None: - self.addAttribute(Attribute('font-family', font_family)) + self.setAttribute(Attribute('font-family', font_family)) if text<>None: self.text=text if text_anchor<>None: - self.addAttribute(Attribute('text-anchor', text_anchor)) + self.setAttribute(Attribute('text-anchor', text_anchor)) + + def _construct(attributes): + return text() + _construct = staticmethod(_construct) class textpath(SVGelement): @@ -558,10 +699,18 @@ class textpath(SVGelement): """ def __init__(self,link,text=None,**args): SVGelement.__init__(self,'textPath',**args) - self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef)) + self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef)) if text<>None: self.text=text + def _construct(attributes): + href = get_attr_value(attributes, 'href') + if href and href.nsname == 'xlink': + return textpath(href.value) + return None + _construct = staticmethod(_construct) + + class pattern(SVGelement): """p=pattern(x,y,width,height,patternUnits,**args) @@ -572,15 +721,20 @@ class pattern(SVGelement): def __init__(self,x=None,y=None,width=None,height=None,patternUnits=None,**args): SVGelement.__init__(self,'pattern',**args) if x<>None: - self.addAttribute(Attribute('x', x)) + self.setAttribute(Attribute('x', x)) if y<>None: - self.addAttribute(Attribute('y', y)) + self.setAttribute(Attribute('y', y)) if width<>None: - self.addAttribute(Attribute('width', width)) + self.setAttribute(Attribute('width', width)) if height<>None: - self.addAttribute(Attribute('height', height)) + self.setAttribute(Attribute('height', height)) if patternUnits<>None: - self.addAttribute(Attribute('patternUnits', patternUnits)) + self.setAttribute(Attribute('patternUnits', patternUnits)) + + def _construct(attributes): + return pattern() + _construct = staticmethod(_construct) + class title(SVGelement): """t=title(text,**args) @@ -593,6 +747,11 @@ class title(SVGelement): if text<>None: self.text=text + def _construct(attributes): + return title() + _construct = staticmethod(_construct) + + class description(SVGelement): """d=description(text,**args) @@ -604,6 +763,11 @@ class description(SVGelement): if text<>None: self.text=text + def _construct(attributes): + return description() + _construct = staticmethod(_construct) + + class lineargradient(SVGelement): """lg=lineargradient(x1,y1,x2,y2,id,**args) @@ -613,15 +777,20 @@ class lineargradient(SVGelement): def __init__(self,x1=None,y1=None,x2=None,y2=None,id=None,**args): SVGelement.__init__(self,'linearGradient',**args) if x1<>None: - self.addAttribute(Attribute('x1', x1)) + self.setAttribute(Attribute('x1', x1)) if y1<>None: - self.addAttribute(Attribute('y1', y1)) + self.setAttribute(Attribute('y1', y1)) if x2<>None: - self.addAttribute(Attribute('x2', x2)) + self.setAttribute(Attribute('x2', x2)) if y2<>None: - self.addAttribute(Attribute('y2', y2)) + self.setAttribute(Attribute('y2', y2)) if id<>None: - self.addAttribute(Attribute('id', id)) + self.setAttribute(Attribute('id', id)) + + def _construct(attributes): + return lineargradient() + _construct = staticmethod(_construct) + class radialgradient(SVGelement): """rg=radialgradient(cx,cy,r,fx,fy,id,**args) @@ -632,17 +801,22 @@ class radialgradient(SVGelement): def __init__(self,cx=None,cy=None,r=None,fx=None,fy=None,id=None,**args): SVGelement.__init__(self,'radialGradient',**args) if cx<>None: - self.addAttribute(Attribute('cx', cx)) + self.setAttribute(Attribute('cx', cx)) if cy<>None: - self.addAttribute(Attribute('cy', cy)) + self.setAttribute(Attribute('cy', cy)) if r<>None: - self.addAttribute(Attribute('r', r)) + self.setAttribute(Attribute('r', r)) if fx<>None: - self.addAttribute(Attribute('fx', fx)) + self.setAttribute(Attribute('fx', fx)) if fy<>None: - self.addAttribute(Attribute('fy', fy)) + self.setAttribute(Attribute('fy', fy)) if id<>None: - self.addAttribute(Attribute('id', id)) + self.setAttribute(Attribute('id', id)) + + def _construct(attributes): + return radialgradient() + _construct = staticmethod(_construct) + class stop(SVGelement): """st=stop(offset,stop_color,**args) @@ -652,7 +826,12 @@ class stop(SVGelement): def __init__(self,offset,stop_color=None,**args): SVGelement.__init__(self,'stop',{'offset':offset},**args) if stop_color<>None: - self.addAttribute(Attribute('stop-color', stop_color)) + self.setAttribute(Attribute('stop-color', stop_color)) + + def _construct(attributes): + offset = get_attr_value(attributes, 'offset') + return stop(offset.value) + _construct = staticmethod(_construct) class style(SVGelement): """st=style(type,cdata=None,**args) @@ -661,6 +840,11 @@ class style(SVGelement): """ def __init__(self,type,cdata=None,**args): SVGelement.__init__(self,'style',{'type':type},cdata=cdata, **args) + + def _construct(attributes): + type = get_attr_value(attributes, 'type') + return style(type.value) + _construct = staticmethod(_construct) class image(SVGelement): @@ -677,12 +861,22 @@ class image(SVGelement): else: raise ValueError, 'both height and width are required' SVGelement.__init__(self,'image',{'width':width,'height':height},**args) - self.addAttribute(Attribute('href', url, 'xlink', xlinkNSRef)) + self.setAttribute(Attribute('href', url, 'xlink', xlinkNSRef)) if x<>None: - self.addAttribute(Attribute('x', x)) + self.setAttribute(Attribute('x', x)) if y<>None: - self.addAttribute(Attribute('y', y)) + self.setAttribute(Attribute('y', y)) + def _construct(attributes): + href = get_attr_value(attributes, 'href') + width = get_attr_value(attributes, 'width') + height = get_attr_value(attributes, 'height') + if href and href.nsname == 'xlink': + return image(href.value, width=width.value, height=height.value) + return None + _construct = staticmethod(_construct) + + class cursor(SVGelement): """c=cursor(url,**args) @@ -690,7 +884,14 @@ class cursor(SVGelement): """ def __init__(self,url,**args): SVGelement.__init__(self,'cursor',**args) - self.addAttribute(Attribute('href', url, 'xlink', xlinkNSRef)) + self.setAttribute(Attribute('href', url, 'xlink', xlinkNSRef)) + + def _construct(attributes): + href = get_attr_value(attributes, 'href') + if href and href.nsname == 'xlink': + return cursor(href.value) + return None + _construct = staticmethod(_construct) class marker(SVGelement): @@ -702,17 +903,22 @@ class marker(SVGelement): def __init__(self,id=None,viewBox=None,refx=None,refy=None,markerWidth=None,markerHeight=None,**args): SVGelement.__init__(self,'marker',**args) if id<>None: - self.addAttribute(Attribute('id', id)) + self.setAttribute(Attribute('id', id)) if viewBox<>None: - self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox))) + self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox))) if refx<>None: - self.addAttribute(Attribute('refX', refx)) + self.setAttribute(Attribute('refX', refx)) if refy<>None: - self.addAttribute(Attribute('refY', refy)) + self.setAttribute(Attribute('refY', refy)) if markerWidth<>None: - self.addAttribute(Attribute('markerWidth', markerWidth)) + self.setAttribute(Attribute('markerWidth', markerWidth)) if markerHeight<>None: - self.addAttribute(Attribute('markerHeight', markerHeight)) + self.setAttribute(Attribute('markerHeight', markerHeight)) + + def _construct(attributes): + return marker() + _construct = staticmethod(_construct) + class group(SVGelement): """g=group(id,**args) @@ -723,7 +929,11 @@ class group(SVGelement): def __init__(self,id=None,**args): SVGelement.__init__(self,'g',**args) if id<>None: - self.addAttribute(Attribute('id', id)) + self.setAttribute(Attribute('id', id)) + + def _construct(attributes): + return group() + _construct = staticmethod(_construct) class symbol(SVGelement): """sy=symbol(id,viewbox,**args) @@ -737,9 +947,14 @@ class symbol(SVGelement): def __init__(self,id=None,viewBox=None,**args): SVGelement.__init__(self,'symbol',**args) if id<>None: - self.addAttribute(Attribute('id', id)) + self.setAttribute(Attribute('id', id)) if viewBox<>None: - self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox))) + self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox))) + + def _construct(attributes): + return symbol() + _construct = staticmethod(_construct) + class defs(SVGelement): """d=defs(**args) @@ -749,6 +964,11 @@ class defs(SVGelement): def __init__(self,**args): SVGelement.__init__(self,'defs',**args) + def _construct(attributes): + return defs() + _construct = staticmethod(_construct) + + class switch(SVGelement): """sw=switch(**args) @@ -759,7 +979,11 @@ class switch(SVGelement): def __init__(self,**args): SVGelement.__init__(self,'switch',**args) - + def _construct(attributes): + return switch() + _construct = staticmethod(_construct) + + class use(SVGelement): """u=use(link,x,y,width,height,**args) @@ -767,18 +991,25 @@ class use(SVGelement): """ def __init__(self,link,x=None,y=None,width=None,height=None,**args): SVGelement.__init__(self,'use',**args) - self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef)) + self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef)) if x<>None: - self.addAttribute(Attribute('x', x)) + self.setAttribute(Attribute('x', x)) if y<>None: - self.addAttribute(Attribute('y', y)) + self.setAttribute(Attribute('y', y)) if width<>None: - self.addAttribute(Attribute('width', width)) + self.setAttribute(Attribute('width', width)) if height<>None: - self.addAttribute(Attribute('height', height)) - - + self.setAttribute(Attribute('height', height)) + + def _construct(attributes): + href = get_attr_value(attributes, 'href') + if href and href.nsname == 'xlink': + return use(href.value) + return None + _construct = staticmethod(_construct) + + class link(SVGelement): """a=link(url,**args) @@ -787,7 +1018,15 @@ class link(SVGelement): """ def __init__(self,link='',**args): SVGelement.__init__(self,'a',**args) - self.addAttribute(Attribute('href', link, 'xlink', xlinkNSRef)) + self.setAttribute(Attribute('href', link, 'xlink', xlinkNSRef)) + + def _construct(attributes): + href = get_attr_value(attributes, 'href') + if href and href.nsname == 'xlink': + return link(href.value) + return None + _construct = staticmethod(_construct) + class view(SVGelement): """v=view(id,**args) @@ -796,7 +1035,12 @@ class view(SVGelement): def __init__(self,id=None,**args): SVGelement.__init__(self,'view',**args) if id<>None: - self.addAttribute(Attribute('id', id)) + self.setAttribute(Attribute('id', id)) + + def _construct(attributes): + return view() + _construct = staticmethod(_construct) + class script(SVGelement): """sc=script(type,type,cdata,**args) @@ -806,6 +1050,12 @@ class script(SVGelement): """ def __init__(self,type,cdata=None,**args): SVGelement.__init__(self,'script',{'type':type},cdata=cdata,**args) + + def _construct(attributes): + type = get_attr_value(attributes, 'type') + return script(type.value, cdata=cdata.value) + _construct = staticmethod(_construct) + class animate(SVGelement): """an=animate(attribute,from,to,during,**args) @@ -815,23 +1065,34 @@ class animate(SVGelement): def __init__(self,attribute,fr=None,to=None,dur=None,**args): SVGelement.__init__(self,'animate',{'attributeName':attribute},**args) if fr<>None: - self.addAttribute(Attribute('from', fr)) + self.setAttribute(Attribute('from', fr)) if to<>None: - self.addAttribute(Attribute('to', to)) + self.setAttribute(Attribute('to', to)) if dur<>None: - self.addAttribute(Attribute('dur', dur)) + self.setAttribute(Attribute('dur', dur)) + + def _construct(attributes): + attribute = get_attr_value(attributes, 'attributeName') + return animate(attribute.value) + _construct = staticmethod(_construct) + class animateMotion(SVGelement): """an=animateMotion(pathdata,dur,**args) animates a SVGelement over the given path in dur seconds """ - def __init__(self,pathdata,dur,**args): + def __init__(self,pathdata=None,dur=None,**args): SVGelement.__init__(self,'animateMotion',**args) if pathdata<>None: - self.addAttribute(Attribute('path', str(pathdata))) + self.setAttribute(Attribute('path', str(pathdata))) if dur<>None: - self.addAttribute(Attribute('dur', dur)) + self.setAttribute(Attribute('dur', dur)) + + def _construct(attributes): + return animateMotion() + _construct = staticmethod(_construct) + class animateTransform(SVGelement): """antr=animateTransform(type,from,to,dur,**args) @@ -842,13 +1103,19 @@ class animateTransform(SVGelement): SVGelement.__init__(self,'animateTransform',{'attributeName':'transform'},**args) #As far as I know the attributeName is always transform if type<>None: - self.addAttribute(Attribute('type', type)) + self.setAttribute(Attribute('type', type)) if fr<>None: - self.addAttribute(Attribute('from', fr)) + self.setAttribute(Attribute('from', fr)) if to<>None: - self.addAttribute(Attribute('to', to)) + self.setAttribute(Attribute('to', to)) if dur<>None: - self.addAttribute(Attribute('dur', dur)) + self.setAttribute(Attribute('dur', dur)) + + def _construct(attributes): + return animateTransform() + _construct = staticmethod(_construct) + + class animateColor(SVGelement): """ac=animateColor(attribute,type,from,to,dur,**args) @@ -857,13 +1124,20 @@ class animateColor(SVGelement): def __init__(self,attribute,type=None,fr=None,to=None,dur=None,**args): SVGelement.__init__(self,'animateColor',{'attributeName':attribute},**args) if type<>None: - self.addAttribute(Attribute('type', type)) + self.setAttribute(Attribute('type', type)) if fr<>None: - self.addAttribute(Attribute('from', fr)) + self.setAttribute(Attribute('from', fr)) if to<>None: - self.addAttribute(Attribute('to', to)) + self.setAttribute(Attribute('to', to)) if dur<>None: - self.addAttribute(Attribute('dur', dur)) + self.setAttribute(Attribute('dur', dur)) + + def _construct(attributes): + attribute = get_attr_value(attributes, 'attributeName') + return animateColor(attribute.value) + _construct = staticmethod(_construct) + + class set(SVGelement): """st=set(attribute,to,during,**args) @@ -872,12 +1146,16 @@ class set(SVGelement): def __init__(self,attribute,to=None,dur=None,**args): SVGelement.__init__(self,'set',{'attributeName':attribute},**args) if to<>None: - self.addAttribute(Attribute('to', to)) + self.setAttribute(Attribute('to', to)) if dur<>None: - self.addAttribute(Attribute('dur', dur)) + self.setAttribute(Attribute('dur', dur)) + + def _construct(attributes): + attribute = get_attr_value(attributes, 'attributeName') + return set(attribute.value) + _construct = staticmethod(_construct) - class svg(SVGelement): """s=svg(viewbox,width,height,**args) @@ -896,12 +1174,117 @@ class svg(SVGelement): def __init__(self,viewBox=None, width=None, height=None,**args): SVGelement.__init__(self,'svg',**args) if viewBox<>None: - self.addAttribute(Attribute('viewBox', _viewboxlist(viewBox))) + self.setAttribute(Attribute('viewBox', _viewboxlist(viewBox))) if width<>None: - self.addAttribute(Attribute('width', width)) + self.setAttribute(Attribute('width', width)) if height<>None: - self.addAttribute(Attribute('height', height_)) + self.setAttribute(Attribute('height', height_)) self.namespace="http://www.w3.org/2000/svg" + + def _construct(attributes): + return svg() + _construct = staticmethod(_construct) + + +class ElementHelper(object): + def __init__(self, name, attrs): + self.name = name + self.attrs = attrs + self.cdata = None + self.text = None + +class Stack(object): + def __init__(self): + self._stack = [] + + def push(self, obj): + self._stack.append(obj) + + def pop(self): + if len(self._stack) == 0: + return None + obj = self._stack[-1] + del self._stack[-1] + return obj + + def peek(self): + if len(self._stack) == 0: + return None + return self._stack[-1] + + def is_empty(self): + if len(self._stack) == 0: + return True + return False + + def size(self): + return len(self._stack) + +class SVGImportParser(object): + def __init__(self): + self._indent = 0 + self._cdata = 0 + self._stack = Stack() + self._base = None + self._debug = False + + def getBaseElement(self): + return self._base + + def log(self, msg): + if self._debug: + print " " * self._indent + msg + + def StartElementHandler(self, name, attrs): + self.log("<%s>" % name) + self._indent = self._indent + 1 + import copy + attrs = copy.deepcopy(attrs) + for attrname, attrvalue in attrs.items(): + self.log("%s = %s" % (attrname, attrvalue)) + parent = self._stack.peek() + elt = SVGelement.construct(name, attrs) + if not self._base: + self._base = elt + if parent and elt: + parent.addElement(elt) + self._stack.push(elt) + + def StartCdataSectionHandler(self): + self._cdata = 1 + + def CharacterDataHandler(self, content): + if self._cdata: + self.log("CDATA = '%s'" % content) + elt = self._stack.peek() + if elt: + elt.cdata = elt.cdata + content + else: + if len(content) and not content.isspace(): + self.log("Text = '%s'" % content.strip()) + elt = self._stack.peek() + if elt: + if not elt.text: + elt.text = "" + elt.text = elt.text + content.strip() + + def EndCdataSectionHandler(self): + self._cdata = 0 + + def EndElementHandler(self, name): + self._indent = self._indent - 1 + self.log("" % name) + self._stack.pop() + + def CommentHandler(self, comment): + self.log("Comment = '%s'" % comment) + + def StartNamespaceDeclHandler(self, prefix, uri): + self.log("Namespace = '%s' -> '%s'" % (prefix, uri)) + + def EndNamespaceDeclHandler(self, prefix): + self.log("Namespace = '%s'" % prefix) + class drawing: """d=drawing() @@ -921,6 +1304,30 @@ class drawing: self.svg=svg #Voeg een element toe aan de grafiek toe. + def fromXml(self, xml): + HANDLER_NAMES = [ + 'StartElementHandler', 'EndElementHandler', + 'CharacterDataHandler', + 'StartNamespaceDeclHandler', 'EndNamespaceDeclHandler', + 'CommentHandler', + 'StartCdataSectionHandler', 'EndCdataSectionHandler', + ] + + # Create a parser + parser = expat.ParserCreate() + + handler = SVGImportParser() + for name in HANDLER_NAMES: + setattr(parser, name, getattr(handler, name)) + parser.returns_unicode = 0 + + # Parse the XML + parser.Parse(xml) + base_element = handler.getBaseElement() + if not base_element or not isinstance(base_element, svg): + raise Exception("Base element was not SVG.") + self.setSVG(base_element) + if use_dom_implementation==0: def toXml(self, filename='',compress=False): import cStringIO @@ -1032,13 +1439,58 @@ class drawing: print "SVG well formed" -if __name__=='__main__': - - - d=drawing() +elementTable = { + 'tspan': tspan, + 'tref': tref, + 'rect': rect, + 'ellipse': ellipse, + 'circle': circle, + 'line': line, + 'polyline': polyline, + 'polygon': polygon, + 'path': path, + 'text': text, + 'textPath': textpath, + 'pattern': pattern, + 'title': title, + 'desc': description, + 'linearGradient': lineargradient, + 'radialGradient': radialgradient, + 'stop': stop, + 'style': style, + 'image': image, + 'cursor': cursor, + 'marker': marker, + 'g': group, + 'symbol': symbol, + 'defs': defs, + 'switch': switch, + 'use': use, + 'a': link, + 'view': view, + 'script': script, + 'animate': animate, + 'animateMotion': animateMotion, + 'animateTransform': animateTransform, + 'animateColor': animateColor, + 'set': set, + 'svg': svg +} + +def from_file(document): + import sys + f = open(sys.argv[1], "r") + fc = f.read() + document.fromXml(fc) + print document.toXml() + +def to_file(document): s=svg((0,0,100,100)) r=rect(-100,-100,300,300,'cyan') s.addElement(r) + + text = tspan("Foobar") + s.addElement(text) t=title('SVGdraw Demo') s.addElement(t) @@ -1052,8 +1504,8 @@ if __name__=='__main__': pd.relsmbezier(10,5,0,10) pd.relsmbezier(-10,5,0,10) an=animateMotion(pd,10) - an.addAttribute(Attribute('rotate', 'auto-reverse')) - an.addAttribute(Attribute('repeatCount', "indefinite")) + an.setAttribute(Attribute('rotate', 'auto-reverse')) + an.setAttribute(Attribute('repeatCount', "indefinite")) g.addElement(an) s.addElement(g) for i in range(20,120,20): @@ -1063,7 +1515,14 @@ if __name__=='__main__': for j in range(5,105,10): c=circle(i,j,1,'red','black',.5) s.addElement(c) - d.setSVG(s) + document.setSVG(s) - print d.toXml() + print document.toXml() +def main(): + d=drawing() + from_file(d) +# to_file(d) + +if __name__=='__main__': + main() diff --git a/sugar/p2p/model/__init__.py b/sugar/p2p/model/__init__.py deleted file mode 100644 index e69de29..0000000 --- a/sugar/p2p/model/__init__.py +++ /dev/null -- cgit v0.9.1