def test_appending(self): frag_file = "tmp/b.svg" main_file = "tmp/a.svg" main = svg() frag = svg() exp = svg() sb = ShapeBuilder() main.addElement(sb.createRect(0, 0, "200px", "100px")) exp.addElement(sb.createRect(0, 0, "200px", "100px")) frag.addElement(text("Hello World", x=210, y=110)) exp.addElement(text("Hello World", x=210, y=110)) main.save(main_file) frag.save(frag_file) append_svg_to_file(frag_file, main_file) svg_main = parse(main_file) gotS = str(svg_main.getXML()) expS = str(exp.getXML()) print "Got:", gotS print "Exp:", expS #self.assertEqual(exp.getXML(), svg_main.getXML(), "Appended files are equal" ) if gotS != expS: print "Different\nGot:\n", gotS, "\nExp:\n", expS
def test_appending(self): frag_file="tmp/b.svg" main_file="tmp/a.svg" main = svg() frag = svg() exp = svg() sb = ShapeBuilder() main.addElement(sb.createRect(0, 0, "200px", "100px")) exp.addElement(sb.createRect(0, 0, "200px", "100px")) frag.addElement(text("Hello World", x = 210, y = 110)) exp.addElement(text("Hello World", x = 210, y = 110)) main.save(main_file) frag.save(frag_file) append_svg_to_file(frag_file,main_file) svg_main = parse(main_file) gotS =str(svg_main.getXML()) expS = str(exp.getXML()) print"Got:",gotS print"Exp:",expS #self.assertEqual(exp.getXML(), svg_main.getXML(), "Appended files are equal" ) if gotS != expS : print "Different\nGot:\n",gotS,"\nExp:\n",expS
def setup(args): widthmm = "%fmm" % args.width heightmm = "%fmm" % args.height dwg = svg(width=widthmm, height=heightmm) dwg.set_viewBox("0 0 %s %s" % (args.width, args.height)) return dwg
def test(): fout = svg() fout.addElement(translate(diff(), 120, 60)) for elem in branch_section2(): fout.addElement(elem) #fout.addElement(translate(local_work(), 200, 200)) fout.save('/tmp/git.svg')
def test2(): fout = svg('test') r1 = revision() r2 = revision() # import pdb; pdb.set_trace() r2.set_cx(100) r2.set_cy(200) elems = rev_path(r1.get_cx(), r1.get_cy(), r2.get_cx(), r2.get_cy(), "first") for e in elems: fout.addElement(e) fout.addElement(r1) fout.addElement(r2) r3 = revision() r3.set_cx(200) r3.set_cy(50) for e in rev_path(r1.get_cx(), r1.get_cy(), r3.get_cx(), r3.get_cy(), "second"): fout.addElement(e) fout.addElement(r3) n = translate(num("4", GREEN_3), 50, 40) fout.addElement(n) n = scale([translate(num("11", GREEN_1), 50, 40)], 2) fout.addElement(n) fout.save('/tmp/test.svg')
def create_diagram(data, conf): diagram = Group(data, conf) s = svg(width = str(diagram.get_content_width()), height = str(diagram.get_content_height())) m = metadata() #creating metadata elements dctitle = dctitleEl() dctype = dctypeEl() dcformat = dcformatEl() cc = ccEl() cc.addElement(dcformat) cc.addElement(dctype) cc.addElement(dctitle) rdf = rdfEl() rdf.addElement(cc) m.addElement(rdf) s.addElement(m) sn = sodipodiNamedViewEl() s.addElement(sn) d = defs() d.addElement(Arrow("small-right-arrow", 3)) d.addElement(Arrow("normal-right-arrow", 5)) d.addElement(Arrow("large-right-arrow", 7)) s.addElement(d) diagram.render_content(s, 0, 0) xml = s.getXML() return s.wrap_xml(xml)
def setup(state): widthmm = "%fmm" % state.get_param("width") heightmm = "%fmm" % state.get_param("height") dwg = svg(width=widthmm,height=heightmm) dwg.set_viewBox("0 0 %s %s" % (args.width, args.height)) return dwg
def setup(width,length): widthmm = "%fmm" % width lengthmm = "%fmm" % length print width,length dwg = svg(width=widthmm,height=lengthmm) dwg.set_viewBox("0 0 %s %s" % (width, length)) return dwg
def testMultiplePaths(): """ - | - """ t=Turtle() t.penDown() print(t.getPosition()) t.forward(205) print(t.getPosition()) t.right(90) t.forward(205) print(t.getPosition()) t.right(90) t.forward(105) print(t.getPosition()) t.right(90) t.forward(105) print(t.getPosition()) t.penUp() t.moveTo(Vector(300,300)) print(t.getPosition()) t.penDown() t.forward(205) print(t.getPosition()) t.finish() #print (t.getXML()) s=svg(0, 0, 2000, 2000) s=t.addTurtlePathToSVG(s) s.save('./testoutput/testTurtle.svg')
def create_graph(matrix, colordict, xlabels, ylabels): # Create graphics graph = svg('Test graph') # Color data cstart = colordict['scolor'] cend = colordict['ecolor'] # Min max values matrixdata = [float(i[0]) for row in matrix for i in row] matrixmin = min(matrixdata) matrixmax = max(matrixdata) print "Working on range from %s to %s" % (matrixmin, matrixmax) for row in matrix: for sample in row: r = rect(x=sample[1][0], y=sample[1][1], width=10, height=10) r.set_fill(getcol(float(sample[0]), matrixmin, matrixmax, cstart, cend)) graph.addElement(r) return graph
def setup(state): widthmm = "%fmm" % state.get_param("width") heightmm = "%fmm" % state.get_param("height") dwg = svg(width=widthmm, height=heightmm) dwg.set_viewBox("0 0 %s %s" % (args.width, args.height)) return dwg
def setup(args): widthmm = "%fmm" % args.width heightmm = "%fmm" % args.height dwg = svg(width=widthmm,height=heightmm) dwg.set_viewBox("0 0 %s %s" % (args.width, args.height)) return dwg
def setup(): widthmm = "%fmm" % pagewidth heightmm = "%fmm" % pageheight print "page size %dmm x %dmm" % (pagewidth,pageheight) dwg = svg(width=widthmm,height=heightmm) dwg.set_viewBox("0 0 %s %s" % (pagewidth, pageheight)) return dwg
def setup(pagewidth,pageheight): widthmm = "%fmm" % pagewidth heightmm = "%fmm" % pageheight print "page size %dmm x %dmm" % (pagewidth,pageheight) dwg = svg(width=widthmm,height=heightmm) dwg.set_viewBox("0 0 %s %s" % (pagewidth, pageheight)) return dwg
def write(self, path): """ Writes the page to the SVG file with the provided path. """ # Create a new SVG document doc = svg( x=0, y=0, width=mm_to_px(self.width), height=mm_to_px(self.height) ) # Draw all the content: first the background, then the content, # and finally the labels background_group = g() background_group.setAttribute('id', 'background') content_group = g() content_group.setAttribute('id', 'content') label_group = g() label_group.setAttribute('id', 'labels') contour_group = g() contour_group.setAttribute('id', 'contours') my_defs = defs() for c in self.containers: if c.has_background: c.draw_background(background_group) if c.needs_clipping and (c.has_content or c.has_labels): path_id = random_string(16) clprect = rect( x=mm_to_px(c.x), y=mm_to_px(c.y), width=mm_to_px(c.width), height=mm_to_px(c.height) ) clppath = clipPath(id=path_id) clppath.addElement(clprect) my_defs.addElement(clppath) # Draw content with clipping path if c.has_content: container_grp = g() container_grp.set_clip_path('url(#%s)' % path_id) c.draw_content(container_grp) content_group.addElement(container_grp) # The labels on top of the content if c.has_labels: container_grp = g() container_grp.set_clip_path('url(#%s)' % path_id) c.draw_labels(container_grp) label_group.addElement(container_grp) else: if c.has_content: c.draw_content(content_group) if c.has_labels: c.draw_labels(label_group) if c.has_contour: c.draw_contour(contour_group) # Add each of the base groups doc.addElement(my_defs) doc.addElement(background_group) doc.addElement(content_group) doc.addElement(label_group) doc.addElement(contour_group) # Write the SVG document to the file doc.save(path)
def __init__(self, timeline, scene, view_properties, appearence, **kwargs): self._timeline = timeline self._scene = scene self._appearence = appearence self._view_properties = view_properties self._svg = svg(width=scene.width, height=scene.height) self._small_font_style = self._get_small_font_style() self._small_centered_font_style = self._get_small_centered_font_style() self._larger_font_style = self._get_larger_font_style() try: self._shadow_flag = kwargs["shadow"] except KeyError: self._shadow_flag = False
def box_maker(): global opt border = 300 totalx = 2 * opt.length + 2 * opt.depth + border totaly = 2 * opt.depth + opt.length + border s = svg(0, 0, 2000, 2000) # generate the box object a = box(opt) a.gen(s) if opt.show_labels: pass if opt.outline: pass return s.getXML()
def box_maker(): global opt border = 300 totalx = 2 * opt.length + 2 * opt.depth + border totaly = 2 * opt.depth + opt.length + border s=svg(0, 0,2000,2000) # generate the box object a = box(opt) a.gen(s) if opt.show_labels: pass if opt.outline: pass return s.getXML()
def svg(self): if not self.chairs: raise SVGError('You need to calculate the chair distribution.') width, height = self.svg_dimention() # SVG doc s = svg(height="100%", width="100%") s.set_viewBox("0 0 %d %d" % (width, height)) t = title() t.appendTextContent('Parlamento') s.addElement(t) # Create the party groups groups = {} for i, party in enumerate(self.parties): groups[i] = g() groups[i].set_id('%s_group' % party.initials) t = title() t.appendTextContent('Grupo Parlamentar %s' % party.initials) groups[i].addElement(t) # Add the chair shape definition s.addElement(self.defs()) # Distribute the chairs for i, row in enumerate(self.chairs): for j, mp in enumerate(row): angle, x, y = self.hc.chair_location(i, j) x = x + width / 2 y = height - y t = title() t.appendTextContent(mp.name) mp_link = a() # TODO This link is a placeholder, it will be more sensible mp_link._attributes['xlink:href'] = 'http://parlamento.pt' mp_link.addElement(t) mp_link.addElement(self.chair_svg(i, j, mp)) groups[mp.party.order].addElement(mp_link) # Insert the party groups into the svg for i, party in enumerate(self.parties): s.addElement(groups[i]) return s.getXML()
def svg(self): if not self.chairs: raise SVGError('You need to calculate the chair distribution.') width, height = self.svg_dimention() # SVG doc s = svg(height="100%", width="100%") s.set_viewBox("0 0 %d %d" % (width, height)) t = title() t.appendTextContent('Parlamento') s.addElement(t) # Create the party groups groups = {} for i in range(len(self.parties)): party = self.parties[i] groups[i] = g() # groups[i].set_fill(party['color']) groups[i].set_id('%s_group' % party['initials']) t = title() t.appendTextContent('Grupo Parlamentar do %s' % party['initials']) groups[i].addElement(t) # Add the chair shape definition s.addElement(self.defs()) # Distribute the chairs for row in range(len(self.chairs)): for col in range(len(self.chairs[row])): angle, x, y = self.hc.chair_location(row, col) x = x + width / 2 y = height - y groups[self.chairs[row][col]].addElement(self.chair_svg( row, col, self.parties[self.chairs[row][col]]['initials'])) # Insert the party groups into the svg for i in range(len(self.parties)): s.addElement(groups[i]) return s.getXML()
def testLindenMayer(): s=svg(0, 0, 2000, 2000) commands='F+F-F-FF+F+F-F+F+F-F-FF+F+F-F+F+F-F-FF+F+F-F+F+F-F-FF+F+F-F' t=Turtle() t.moveTo(Vector(500,250)) t.penDown() angle=90 distance=40 for cmd in commands: print(cmd) if cmd=='F': t.forward(distance) elif cmd=='+': t.right(angle) elif cmd=='-': t.left(angle) print(t.getPosition()) t.penDown() print (t.getXML()) s=t.addTurtlePathToSVG(s) s.save('./testoutput/testTurtle.svg')
def create(self,w,h): self.dwg = svg(width=w,height=h)
def test(): fout = svg() fout.addElement(translate(diff(), 120, 60)) for elem in branch_section(): fout.addElement(elem) fout.save('/tmp/git.svg')
command = GCode.line_type(gcode_line) movement = None if command == GCode.EXTRUDE: extruder.from_gcode(gcode_line) elif command == GCode.MOVE: movement = cur.gcode_move(gcode_line) #don't treat anything as real until we see the first layer marker if layer_num is None: match = layer_begin.match(gcode_line) if match is not None: layer_num = 0 out = svg("Layer "+str(layer_num)) stats = StatsCollector(layer_num) continue if movement is None: continue if movement.layer_change() > 0: out.save(layer_filename(layer_num)) layer_num += 1 out = svg("Layer "+str(layer_num)) stats.write() stats.new_layer(layer_num) else: myline = movement.toSVG(extruder.getStyle())
verbose = False crop = False for o, a in opts: if o in ("-v", "--verbose"): verbose = True elif o in ("-c", "--crop"): crop = True rtlfile = args[0] if len(args) == 2: svgfile = args[1] else: svgfile = os.path.splitext(rtlfile)[0] + ".svg" if verbose: print "Processing " + rtlfile + "..." global mySVG mySVG = structure.svg(rtlfile, width=21600, height=14400) parser = RTLParser(verbose=verbose) parser.registerHandler("PU", handlePU) parser.parse(rtlfile) if verbose: print "BBox: " + str(parser.bbox) if crop: mySVG.set_preserveAspectRatio("xMidYMid meet") width, height = parser.bbox[1][0] - parser.bbox[0][0], parser.bbox[1][ 1] - parser.bbox[0][1] mySVG.set_viewBox( str(parser.bbox[0][0]) + " " + str(parser.bbox[0][1]) + " " + str(width) + " " + str(height)) mySVG.set_width(width)
def get_svgdoc(paths, rxn_nodes, labels, font_size, font_family, height, width, easy_edit=True, vonda_compatible=False): """ Assemble svg document (easy_edit version, easily editable in inkscape)""" # create svg document doc = svg() doc.set_height(height) doc.set_width(width) # add marker defenitions defs = TextContent(""" <defs> <marker id="product" markerWidth="6" markerHeight="6" refX="0.75" refY="2.5" orient="auto" markerUnits="strokeWidth"> <path d="M1,1 Q3,2.5 1,4 L4,2.5 z " stroke-linejoin="round" stroke-linecap="round" stroke="#000" stroke-width="0.5" fill="#000"/> </marker> <marker id="substrate" markerWidth="6" markerHeight="6" refX="0.75" refY="2.5" orient="auto" markerUnits="strokeWidth"> <circle cx="2.6" cy="2.5" r="1.1" fill="#000"/> </marker> </defs> """) doc.addElement(defs) # add paths for p in paths: pth = path(paths[p]['d']) if vonda_compatible: pth.set_id(str(p[0])) elif easy_edit: pth.set_id('path_{}{}'.format(*p)) else: pth.set_id(str(p)) pth.set_style(paths[p]['style']) doc.addElement(pth) # add labels for n in labels: x = labels[n]['x'] y = labels[n]['y'] txt = text(labels[n]['text'], x, y) txt.set_id(str(n)) txt.set_font_family(font_family) txt.set_font_size(font_size) doc.addElement(txt) # add reaction nodes for c in rxn_nodes: x = rxn_nodes[c]['x'] y = rxn_nodes[c]['y'] crc = circle(x, y, 5) crc.set_id(c) crc.set_style('fill:#999999') doc.addElement(crc) # add reaction value placeholders if easy_edit: txt = text('-0.0e+00', x + 10, y + 4) txt.set_id('rval_' + c) txt.set_font_family(font_family) txt.set_font_size(8) doc.addElement(txt) return doc
def __prepareSVGObject(self): self.__SVGObject = svg()
def draw_country(country_code, capital_coords, file_name, sg_client): country_context = sg_client.get_context(capital_coords[0],capital_coords[1]) #print country_context['features'] for feature in country_context['features']: classifiers = feature['classifiers'][0] print feature['classifiers'] if classifiers['category'] == 'National' and classifiers['type'] == 'Region': print feature['name'] print feature['handle'] print feature['href'] country = sg_client.get_feature(feature['handle']).to_dict() cgeo = country['geometry'] min_x =1000 max_x =-1000 min_y =1000 max_y =-1000 oh=ShapeBuilder() if cgeo['type'] == 'MultiPolygon': # get bounding box for poly in cgeo['coordinates']: geopath = poly[0] min_x, max_x, min_y, max_y = get_bbox(geopath, min_x, max_x, min_y, max_y ) print min_x, min_y, merc_x(min_x), merc_y(min_y),max_x, max_y #transform bounding bo to mercator merc_x_min = merc_x(min_x) merc_y_min = merc_y(min_y) merc_y_max = merc_y(max_y) #scale to image scale = image_width / (merc_x(max_x) - merc_x_min) image_height = (merc_y_max - merc_y_min) * scale mySVG =svg(image_width, image_height) for poly in cgeo['coordinates']: mypoints = '' for p in poly[0]: lng, lat = p x = (merc_x(lng)- merc_x_min ) * scale y = ( (merc_y(lat) -merc_y_min )* -1 * scale) + image_height mypoints += "%s,%s " %(x,y) pl = oh.createPolygon(points=mypoints,stroke=None, fill='red') mySVG.addElement(pl) mySVG.save(file_name) return True elif cgeo['type'] == 'Polygon': min_x, max_x, min_y, max_y = get_bbox(cgeo['coordinates'][0], min_x, max_x, min_y, max_y ) merc_x_min = merc_x(min_x) merc_y_min = merc_y(min_y) merc_y_max = merc_y(max_y) #scale to image scale = image_width / (merc_x(max_x) - merc_x_min) image_height = (merc_y_max - merc_y_min) * scale mySVG =svg(image_width, image_height) mypoints = '' for p in cgeo['coordinates'][0]: lng, lat = p x = (merc_x(lng)- merc_x_min ) * scale y = ( (merc_y(lat) -merc_y_min )* -1 * scale) + image_height mypoints += "%s,%s " %(x,y) pl = oh.createPolygon(points=mypoints,stroke=None, fill='red') mySVG.addElement(pl) mySVG.save(file_name) return True
verbose = False crop = False for o, a in opts: if o in ("-v", "--verbose"): verbose = True elif o in ("-c", "--crop"): crop = True rtlfile = args[0] if len(args) == 2: svgfile = args[1] else: svgfile = os.path.splitext(rtlfile)[0]+".svg" if verbose: print "Processing " + rtlfile + "..." global mySVG mySVG = structure.svg(rtlfile, width=21600, height=14400) parser = RTLParser(verbose=verbose) parser.registerHandler("PU", handlePU) parser.parse(rtlfile) if verbose: print "BBox: " + str(parser.bbox) if crop: mySVG.set_preserveAspectRatio("xMidYMid meet") width,height = parser.bbox[1][0]-parser.bbox[0][0], parser.bbox[1][1]-parser.bbox[0][1] mySVG.set_viewBox(str(parser.bbox[0][0]) + " " + str(parser.bbox[0][1]) + " " + str(width) + " " + str(height)) mySVG.set_width(width) mySVG.set_height(height) # Export svg to same position as input file