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 legend(self, elem, x, y, width, height, label_style): n = len(self.colors.colors) box_height = int(np.floor(float(height) / (n+2))) box_width = min(8, width/2) mark_style = StyleBuilder(self.mark_style) textsize = int(re.findall('([0-9]+)', label_style.get('font-size', "8") )[0]) label_x = x + box_width + self.mark_length + 1 for i in range(n): box = rect( x = mm_to_px(x), y = mm_to_px(y + (n-i-1)*box_height), width = mm_to_px(box_width), height = mm_to_px(box_height) ) s = deepcopy(self.styles[i].style_dict) s['stroke'] = 'black' box.set_style(StyleBuilder(s).getStyle()) elem.addElement(box) if i < (n-1): mark = line( X1=mm_to_px(x+box_width), Y1=mm_to_px(y+(n-i-1)*box_height), X2=mm_to_px(x+box_width+self.mark_length), Y2=mm_to_px(y+(n-i-1)*box_height) ) mark.set_style(mark_style.getStyle()) elem.addElement(mark) label = text( content="%0.*f" % (self.ndecimals, self.limits[i]), x=mm_to_px(label_x), y=mm_to_px(y+(n-i-1)*box_height)+(textsize/2) ) label.set_style(StyleBuilder(label_style).getStyle()) elem.addElement(label) label = text( content="Min: %0.*f" % (self.ndecimals, np.min(self.values)), x=mm_to_px(label_x), y=mm_to_px(y+n*box_height)+(textsize/2) ) label.set_style(StyleBuilder(label_style).getStyle()) elem.addElement(label) label = text( content="Max: %0.*f" % (self.ndecimals, np.max(self.values)), x=mm_to_px(label_x), y=mm_to_px(y+0*box_height)+(textsize/2) ) label.set_style(StyleBuilder(label_style).getStyle()) elem.addElement(label)
def _draw_text(self, my_text, rect, style, center_text=False): my_text = self._encode_text(my_text) x, y = self._calc_text_pos(rect, center_text) label = text(my_text, x, y) label.set_style(style.getStyle()) label.set_lengthAdjust("spacingAndGlyphs") return label
def rev_path(x1,y1, x2,y2, txt=None): elements = [] # x1, y1 = r1.get_cx(), r1.get_cy() # x2, y2 = r2.get_cx(), r2.get_cy(), l = line(x1, y1, x2, y2) elements.append(l) style = 'stroke-width:{0};stroke:{1}'.format(16, ALUM_6) l.set_style(style) if txt: x, y = middle(x1, y1, x2, y2) style2 = StyleBuilder() #style2.setFontFamily('envy code r') style2.setFontFamily('arial') style2.setFontWeight('bold') style2.setFilling(ALUM_1) # shift text left and up by a bit # whole alphabet in this font is 167 px width per_char = 167./26 t = text(txt, -len(txt)/2*per_char, 4) t.set_style(style2.getStyle()) #import pdb; pdb.set_trace() group = rotate([t], slope_angle(x1, y1, x2, y2)) group = translate([group], x, y) elements.append(group) return elements
def rev_path(x1, y1, x2, y2, txt=None): elements = [] # x1, y1 = r1.get_cx(), r1.get_cy() # x2, y2 = r2.get_cx(), r2.get_cy(), l = line(x1, y1, x2, y2) elements.append(l) style = 'stroke-width:{0};stroke:{1}'.format(16, ALUM_6) l.set_style(style) if txt: x, y = middle(x1, y1, x2, y2) style2 = StyleBuilder() #style2.setFontFamily('envy code r') style2.setFontFamily('arial') style2.setFontWeight('bold') style2.setFilling(ALUM_1) # shift text left and up by a bit # whole alphabet in this font is 167 px width per_char = 167. / 26 t = text(txt, -len(txt) / 2 * per_char, 4) t.set_style(style2.getStyle()) #import pdb; pdb.set_trace() group = rotate([t], slope_angle(x1, y1, x2, y2)) group = translate([group], x, y) elements.append(group) return elements
def render(self, svg, x, y): t = text(self.content, x, y + self.height * 3 / 4) t.set_font_size(self.font.size) t.set_font_family(self.font.family) t.set_font_style(self.font.style) t.set_font_weight(self.font.weight) t.set_fill(self.color) svg.addElement(t)
def text(self,content,x,y,size=10,family="Helvetica",fill="none",stroke="#000",transform=None,id=None): text = txt.text(content=content,x=x,y=y,fill=fill,stroke=stroke) text.set_font_size(size); text.set_font_family(family) if transform : text.set_transform( transform ) if id : text.set_id(id) self.doc.addElement(text)
def words(txt, family='arial', weight='bold', color=ALUM_1): style2 = StyleBuilder() #style2.setFontFamily('envy code r') style2.setFontFamily('arial') style2.setFontWeight('bold') style2.setFilling(ALUM_1) t = text(txt, 0, 0) t.set_style(style2.getStyle()) return t
def command(num_txt, cmd, explanation, color): x = 0 y = 0 elems = [scale(num(num_txt, color), 2)] cmd_txt = text(cmd, x+40, y+12) s = StyleBuilder() s.setFontWeight('bold') s.setFontFamily('Bitstream Vera Sans Mono') cmd_txt.set_style(s.getStyle()) elems.append(cmd_txt) exp_txt = text(explanation, x+45, y+27) s = StyleBuilder() #s.setFontWeight('bold') s.setFontFamily('Bitstream Vera Serif') s.setFontSize('10px') exp_txt.set_style(s.getStyle()) elems.append(exp_txt) return elems
def command(num_txt, cmd, explanation, color): x = 0 y = 0 elems = [scale(num(num_txt, color), 2)] cmd_txt = text(cmd, x + 40, y + 12) s = StyleBuilder() s.setFontWeight('bold') s.setFontFamily('Bitstream Vera Sans Mono') cmd_txt.set_style(s.getStyle()) elems.append(cmd_txt) exp_txt = text(explanation, x + 45, y + 27) s = StyleBuilder() #s.setFontWeight('bold') s.setFontFamily('Bitstream Vera Serif') s.setFontSize('10px') exp_txt.set_style(s.getStyle()) elems.append(exp_txt) return elems
def draw_content(self, elem): textsize = int(re.findall('([0-9]+)', self.style.style_dict.get('font-size', "12") )[0]) txt = self.text.split('\n') for i in range(len(txt)): y = mm_to_px(self.y) + textsize + i*1.8*textsize t = text( content=txt[i], x=mm_to_px(self.x), y=y ) t.set_style(self.style.getStyle()) elem.addElement(t)
def draw_content(self, elem): contour = rect( x = mm_to_px(self.x), y = mm_to_px(self.y), width = mm_to_px(self.width), height = mm_to_px(self.height) ) contour.set_style(StyleBuilder(self.container_style).getStyle()) elem.addElement(contour) x = self.x + self.border y = self.y + self.border width = self.width - (2*self.border) height = self.height - (2*self.border) if self.title is not None: textsize = int(re.findall('([0-9]+)', self.title_style.get('font-size', "11") )[0]) t = text( content=self.title, x=mm_to_px(x), y=mm_to_px(y) + textsize ) t.set_style(StyleBuilder(self.title_style).getStyle()) elem.addElement(t) y += int(round(textsize)) if self.name is not None: textsize = int(re.findall('([0-9]+)', self.name_style.get('font-size', "11") )[0]) t = text( content=self.name, x=mm_to_px(x), y=mm_to_px(y) + textsize ) t.set_style(StyleBuilder(self.name_style).getStyle()) elem.addElement(t) y += int(round(textsize)) self.style.legend(elem, x, y, width, height, self.label_style)
def render(self, node, startX, startY, isReference = False): if node['type'] != 'node': raise Exception("Wrong input object. Expected type: 'node'"); lines = self.__createLines(node['value']); height = self.__determineContainterHeight(lines); nodeContainer = self.__prepareNodeContainer(startX, startY, self.__frameWidth, height, isReference) y = startY + self.__framePadding; for line in lines: if isinstance(line, int): # if int, then render horizontal line lineHeight = self.__determineLineHeight(True); x = startX; separatorObj = self.__shapeBuilder.createLine(x, y, x + self.__frameWidth, y, strokewidth = self.__separatorWidth) nodeContainer.addElement(separatorObj) elif isinstance(line, list): #list, because line is list of bbcoded spans lineHeight = self.__determineLineHeight(False); x = startX + self.__framePadding; txtObj = text(None, x, y + self.__fontSize); for txt in line: span = tspan(); span.appendTextContent(txt.getText()); span.setAttribute("style", txt.getStyle()) txtObj.addElement(span) nodeContainer.addElement(txtObj) else: raise Exception("unsupported value type") y = y + lineHeight; return nodeContainer;
def num(txt, color): txt = str(txt) elems = [] r = rect(0, 0, 16, 16) s = StyleBuilder() s.setFilling(color) r.set_style(s.getStyle()) elems.append(r) if txt: style2 = StyleBuilder() #style2.setFontFamily('envy code r') style2.setFontFamily('arial') style2.setFontWeight('bold') style2.setFilling(ALUM_1) # shift text left and up by a bit if len(txt) == 1: x = 5 elif len(txt) == 2: x = 1.5 t = text(txt, x, 12.5) t.set_style(style2.getStyle()) elems.append(t) return elems
def draw_content(self, elem): # Create a new group grp = g() grp.setAttribute('id', 'scalebar') # Find the amount of available width bbox = self.map_container.bbox step_length = (float(self.step) * self.factor) c = [[bbox[0], 0], [bbox[0] + step_length, 0]] px = self.map_container.geo_to_local_coords(c) step_px = abs(px[1][0] - px[0][0]) nsteps = int(floor(float(self.width) / step_px)) # Draw the horizontal line l = path(pathData="M %f %f L %f %f" % ( mm_to_px(self.x), mm_to_px(self.y + self.height), mm_to_px(self.x + nsteps*step_px), mm_to_px(self.y + self.height) ), style=StyleBuilder({'stroke': 'black', 'stroke-width': 0.3}).getStyle()) grp.addElement(l) # Draw the vertical lines and write the text # textsize = int(re.findall('([0-9]+)', # self.style.style_dict.get('font-size', "12") # )[0]) for i in range(nsteps+1): l = path(pathData="M %f %f L %f %f" % ( mm_to_px(self.x + i*step_px), mm_to_px(self.y + self.height), mm_to_px(self.x + i*step_px), mm_to_px(self.y + self.height - 3) ), style=StyleBuilder({'stroke': 'black', 'stroke-width': 0.3}).getStyle()) grp.addElement(l) content = str(i*self.step) if i == nsteps: content += ' ' + self.unit t = text( content=content, x=mm_to_px(self.x + i*step_px), y=mm_to_px(self.y + self.height - 5) ) t.set_style(self.style.getStyle()) grp.addElement(t) elem.addElement(grp)
def _text(self, the_text, x, y): encoded_text = self._encode_text(the_text) return text(encoded_text, x, y)
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