def render(self, svg, x, y): stroke_width = self.conf.connection.thickness shape_builder = ShapeBuilder() connect_y = y + self.connect_y bottom_y = y + self.content.height + 10 tmp = 0 if isinstance(self.content.children[0], QuantityAbove): t = self.content.height - self.content.children[0].content.height tmp = t / 2 elif isinstance(self.content.children[0], Sequence): if isinstance(self.content.children[0].children[0], QuantityAbove): t = self.content.height - self.content.children[0].children[0].content.height tmp = t / 2 self.content.render(svg, x + 20, y - tmp) path_data = "m {0},{1} c 10,0 10,{3} {2},{3}".format(x, connect_y, 20, bottom_y - connect_y) svg.addElement(path(path_data, stroke = "black", fill = "none", stroke_width=stroke_width)) path_data = "m {0},{1} c 10,0 10,{3} {2},{3}".format(x + 20 + self.content.width, bottom_y, 20, connect_y - bottom_y) svg.addElement(path(path_data, stroke = "black", fill = "none", stroke_width=stroke_width)) Line(10, 0, self.conf, arrow=True).render(svg, x + 20 + self.content.width / 2, bottom_y) l = shape_builder.createLine(x + 20, bottom_y, x + 20 + self.content.width, bottom_y, strokewidth=stroke_width) svg.addElement(l) l = shape_builder.createLine(x, connect_y, x + 20, connect_y, strokewidth=stroke_width) svg.addElement(l) l = shape_builder.createLine(x + 20 + self.content.width, connect_y, x + 20 + self.content.width + 20, connect_y, strokewidth=stroke_width) svg.addElement(l)
def drawSnake(self, snake, colour='green'): sb = ShapeBuilder() # draw contour contour = snake.contour for i in range(len(contour)): if i < len(contour)-1: a = contour[i] b = contour[i+1] line = sb.createLine(a[0], a[1], b[0], b[1], stroke=colour, strokewidth=2) self.svgdoc.addElement(line) # draw controlpoints and normals for i in range(len(snake.controlpoints)): cpoint = snake.controlpoints[i] normal = snake.normals[i] # outer circle circle = sb.createCircle(cx=cpoint[0],cy=cpoint[1],r=10,strokewidth=2,stroke=colour) self.svgdoc.addElement(circle) # inner circle, badbwoy! circle = sb.createCircle(cx=cpoint[0],cy=cpoint[1],r=1,strokewidth=1,stroke='yellow') self.svgdoc.addElement(circle) # normal normal = sb.createLine(cpoint[0], cpoint[1], cpoint[0]+normal[0]*10, cpoint[1]+normal[1]*10, strokewidth=2,stroke=colour) self.svgdoc.addElement(normal)
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 createblock(number): colors = {} colors[None] = ('#eee4da', '#776e65') colors[2] = ('#eee4da', '#776e65') colors[4] = ('#ede0c8', '#776e65') colors[8] = ('#f2b179', '#f9f6f2') colors[16] = ('#f59563', '#f9f6f2') colors[32] = ('#f67c5f', '#f9f6f2') colors[64] = ('#f65e3b', '#f9f6f2') colors[128] = ('#edcf72', '#f9f6f2') colors[256] = ('#edcc61', '#f9f6f2') colors[512] = ('#eee4da', '#776e65') colors[1024] = ('#edc53f', '#f9f6f2') colors[2048] = ('#edc22e', '#f9f6f2') canvas = Svg(0, 0, 100, 100) sb = ShapeBuilder() canvas.addElement(sb.createRect(5, 5, 90, 90, fill=colors[number][0])) t = Text(number, 50, 60) t.set_style( "font-family:FreeSans;font-weight:bold;font-size:36px;text-anchor:middle" ) t.set_fill(colors[number][1]) canvas.addElement(t) return canvas.getXML()
def render_content(self, svg, x, y): shape_builder = ShapeBuilder() frame = shape_builder.createRect(x, y, self.content_width, self.content_height, self.content_height / 2 - 1, self.content_height / 2 - 1, strokewidth=self.conf.terminal.thickness) svg.addElement(frame) x += max(1, (self.content_width - self.text.width - 2 * self.padding) / 2) self.text.render(svg, x + self.padding, y + self.padding)
def _draw_line_to_non_period_event(self, view_properties, event, rect): x = self._scene.x_pos_for_time(event.mean_time()) y = rect.Y + rect.Height / 2 stroke = {True: "red", False: "black"}[view_properties.is_selected(event)] line = ShapeBuilder().createLine(x, y, x, self._scene.divider_y, stroke=stroke) circle = ShapeBuilder().createCircle(x, self._scene.divider_y, 2) return line, circle
def between(x,y, x2,y2, outie=10, angle=45, color=PUR_3, width=4): """outie direction is billowing if it were traveling counterclockwise""" points = [] sb = ShapeBuilder() points.append('{0},{1}'.format(x,y)) slope = slope_angle(x,y, x2,y2) out_angle = slope + angle out_r = out_angle* math.pi/180 next_x = x+ outie*math.cos(out_r) next_y = y - outie*math.sin(out_r) points.append('{0},{1}'.format(next_x, next_y)) slope_r = slope*math.pi/180 distance = math.sqrt((x2-x)**2 + (y2-y)**2) flat_len = distance - 2 * outie*(math.cos(out_r)) next_x2 = next_x + flat_len*math.cos(slope_r) next_y2 = next_y - flat_len*math.sin(slope_r) points.append('{0},{1}'.format(next_x2, next_y2)) points.append('{0},{1}'.format(x2,y2)) line = sb.createPolyline(points=' '.join(points)) style = 'fill:{0};stroke-width:{1};stroke:{2};stroke-linecap:round'.format(ALPHA, width, color) line.set_style(style) return [line]
def between(x, y, x2, y2, outie=10, angle=45, color=PUR_3, width=4): """outie direction is billowing if it were traveling counterclockwise""" points = [] sb = ShapeBuilder() points.append('{0},{1}'.format(x, y)) slope = slope_angle(x, y, x2, y2) out_angle = slope + angle out_r = out_angle * math.pi / 180 next_x = x + outie * math.cos(out_r) next_y = y - outie * math.sin(out_r) points.append('{0},{1}'.format(next_x, next_y)) slope_r = slope * math.pi / 180 distance = math.sqrt((x2 - x)**2 + (y2 - y)**2) flat_len = distance - 2 * outie * (math.cos(out_r)) next_x2 = next_x + flat_len * math.cos(slope_r) next_y2 = next_y - flat_len * math.sin(slope_r) points.append('{0},{1}'.format(next_x2, next_y2)) points.append('{0},{1}'.format(x2, y2)) line = sb.createPolyline(points=' '.join(points)) style = 'fill:{0};stroke-width:{1};stroke:{2};stroke-linecap:round'.format( ALPHA, width, color) line.set_style(style) return [line]
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 drawMapFeatures(self, mapfeatures): sb = ShapeBuilder() # draw mapfeatures for mapfeature in mapfeatures: if mapfeature.visible: colour = mapfeature.colour for linestring in mapfeature.coordinates: l = len(linestring) for i in range(len(linestring)): if i < l-1: a = linestring[i] b = linestring[i+1] line = sb.createLine(a[0], a[1], b[0], b[1], stroke='white', strokewidth=2) if self.animate: setelement = builders.set(attributeName="visibility", attributeType="CSS", to="visible", begin="0s", dur="5s", fill="freeze") animelement = builders.animateMotion(path="M 0 0 L -100 100 M -100 100 L 0 0", begin="0s", dur="5s", fill="freeze") line.addElement(setelement) line.addElement(animelement) self.svgdoc.addElement(line)
def render(self, svg, x, y): self.render_content(svg, x + 10, y) shape_builder = ShapeBuilder() Line(10, 0, self.conf, arrow=True).render(svg, x, y + self.content_height / 2) l = shape_builder.createLine(x + self.content_width + 10, y + self.content_height / 2, x + self.content_width + 20, y + self.content_height / 2, strokewidth=self.conf.connection.thickness) svg.addElement(l)
def triangle(x1, y1, x2, y2, x3, y3, fill): points = [] sb = ShapeBuilder() for x, y in [(x1, y1), (x2, y2), (x3, y3), (x1, y1)]: points.append('{0},{1}'.format(x, y)) pl = sb.createPolyline(points=' '.join(points)) style = 'fill:{0}'.format(fill) pl.set_style(style) return [pl]
def triangle(x1,y1, x2,y2, x3,y3, fill): points = [] sb = ShapeBuilder() for x, y in [(x1,y1), (x2,y2), (x3,y3), (x1, y1)]: points.append('{0},{1}'.format(x,y)) pl = sb.createPolyline(points=' '.join(points)) style = 'fill:{0}'.format(fill) pl.set_style(style) return [pl]
def render_frame(self, svg, x, y): shape_builder = ShapeBuilder() frame = shape_builder.createRect(x, y, self.content_width, self.content_height, strokewidth=self.conf.group.thickness) svg.addElement(frame) #header_box = shape_builder.createRect(x, y, self.header_width, self.header_height, fill='black', strokewidth=self.conf.group.thickness) header_box = shape_builder.createRect(x, y, self.header_text.getWidth(), self.header_text.getHeight(), fill='black', strokewidth=self.conf.group.thickness) svg.addElement(header_box) self.header_text.renderHeader(svg, x + self.header_padding, y + self.header_padding)
def render(self, svg, x, y): stroke_width = self.conf.connection.thickness shape_builder = ShapeBuilder() if self.arrow: l = shape_builder.createLine(x, y, x + self.x_diff - self.arrow_width() * stroke_width, y + self.y_diff, strokewidth=stroke_width) l._attributes['marker-end'] = "url(#{0}-right-arrow)".format(self.conf.connection.marker) svg.addElement(l) else: l = shape_builder.createLine(x, y, x + self.x_diff, y + self.y_diff, strokewidth=stroke_width) svg.addElement(l)
def testIterationsWithAttributesChange(): s = Svg() sb = ShapeBuilder() coords = [(1, 1), (200, 200)] for (x, y) in coords: rectangle = sb.createRect(0, 0, 100, 200, fill='000') rectangle.set_x(x) rectangle.set_y(y) s.addElement(rectangle) s.save('./testoutput/iterationsWithAttributesChange.svg')
def testIterationsWithAttributesChange(): s=Svg() sb =ShapeBuilder() coords=[(1,1),(200,200)] for (x,y) in coords: rectangle = sb.createRect(0,0,100,200,fill='000') rectangle.set_x(x) rectangle.set_y(y) s.addElement(rectangle) s.save('./testoutput/iterationsWithAttributesChange.svg')
def _draw_event_rect(self, event, rect): boxBorderColor = self._get_event_border_color(event) if event.is_container() and EXTENDED_CONTAINER_HEIGHT.enabled(): svg_rect = ShapeBuilder().createRect(rect.X, rect.Y - Y_RECT_OFFSET, rect.GetWidth(), rect.GetHeight() + Y_RECT_OFFSET, stroke=boxBorderColor, fill=self._get_event_box_color(event)) else: svg_rect = ShapeBuilder().createRect(rect.X, rect.Y, rect.GetWidth(), rect.GetHeight(), stroke=boxBorderColor, fill=self._get_event_box_color(event)) if self._shadow_flag: svg_rect.set_filter("url(#filterShadow)") return svg_rect
def get_arrow_polygon(self, scaling: float = 1., offset: int = 0, base: int = 35, height: int = 10, colour: str = "white") -> Polygon: """ Returns an SVG polygon shaped like an arrow that represents the Gene """ if self.reversed: start = int((offset - self.get_start()) * scaling) end = int((offset - self.get_end()) * scaling) else: start = int((self.get_start() + offset) * scaling) end = int((self.get_end() + offset) * scaling) start, end = sorted([start, end]) builder = ShapeBuilder() arrow_size = height // 2 if abs(start - end) < arrow_size: if self.strand > -1: points = [(start, base), (end, base - arrow_size), (start, base - height), (start, base)] else: points = [(start, base - arrow_size), (end, base - height), (end, base), (start, base - arrow_size)] else: if self.strand > -1: arrowstart = end - arrow_size points = [(start, base), (arrowstart, base), (end, base - arrow_size), (arrowstart, base - height), (start, base - height), (start, base)] else: arrowstart = start + arrow_size points = [(start, base - arrow_size), (arrowstart, base - height), (end, base - height), (end, base), (arrowstart, base), (start, base - arrow_size)] arrow = builder.createPolygon(strokewidth=1, stroke='black', fill=colour, points=builder.convertTupleArrayToPoints(points)) locus_tag = self.name if self.protein: locus_tag = self.protein.get_id() arrow.setAttribute('description', self._get_description()) arrow.setAttribute('locus_tag', locus_tag) arrow.set_class('clusterblast-orf') return arrow
def render_content(self, svg, x, y): shape_builder = ShapeBuilder() frame = shape_builder.createRect(x, y, self.content_width, self.content_height, self.content_height / 2 - 1, self.content_height / 2 - 1, fill='black', strokewidth=self.conf.invterminal.thickness) svg.addElement(frame) first = True for child in self.children: if first: first = False else: l = shape_builder.createLine(x, y + 3, x, y + self.content_height - 3, stroke = 'white', strokewidth=self.conf.connection.thickness) svg.addElement(l) child.render(svg, x + self.padding, y + self.padding) x += child.width + 2 * self.padding
def chair(self, party): style = 'stroke:black;stroke-width:4.0;fill:%s;' head = ShapeBuilder().createCircle(30, 25, 16) head.set_class('head') head.set_style(style % party.head_color) body = ShapeBuilder().createPolygon('12,50,47,50,30,15') body.set_class('body') body.set_style(style % party.body_color) group = g() group.set_id(party.initials) group.addElement(body) group.addElement(head) return group
class ArrowRenderer(object): def __init__(self, conf): self.__lineType = conf['connection']['style']; self.__thickness = conf['connection']['thickness']; self.__markerSize = conf['connection']['marker']; self.__refLineType = conf['reference']['style']; self.__refMarkerSize = conf['reference']['marker']; self.__refThickness = conf['reference']['thickness']; self.__shapeBuilder = ShapeBuilder() def render(self, startX, startY, endX, endY, arrowMarker, isReference): if isReference: return self.__renderReference(startX, startY, endX, endY, arrowMarker); else: return self.__renderArrow(startX, startY, endX, endY, arrowMarker); def __renderArrow(self, startX, startY, endX, endY, arrowMarker): line = self.__shapeBuilder.createLine(startX, startY, endX, endY); if self.__lineType == "dashed": lineStyle = "stroke-dasharray:8 4 20 4;" elif self.__lineType == 'dotted': lineStyle = "stroke-dasharray:4 4;"; else: lineStyle = ""; line.setAttribute("style", "marker-end:url(#" + arrowMarker + ");fill:none;stroke:black;stroke-width:" + str(self.__thickness) + ";width:3;" + lineStyle); return line; def __renderReference(self, startX, startY, endX, endY, arrowMarker): line = self.__shapeBuilder.createLine(startX, startY, endX, endY); if self.__refLineType == "dashed": lineStyle = "stroke-dasharray:8 4 20 4;" elif self.__refLineType == 'dotted': lineStyle = "stroke-dasharray:4 4;"; else: lineStyle = ""; line.setAttribute("style", "marker-end:url(#" + arrowMarker + ");fill:none;stroke:black;stroke-width:" + str(self.__refThickness) + ";width:3;" + lineStyle); return line;
def get_svg_groups(self, h_offset: int = 0, v_offset: int = 0, scaling: float = 1., screenwidth: int = 1024, colours: Dict[str, str] = None, overview: bool = False, prefix: str = "dummy") -> List[Group]: """ Returns all SVG elements required to draw the Cluster """ if not colours: colours = {} groups = [] group = Group() acc = Text(self.description, 5, 20) acc.set_class("clusterblast-acc") group.addElement(acc) line_y = 35 + v_offset group.addElement(ShapeBuilder().createLine(10, line_y, 10 + (screenwidth * 0.75), line_y, strokewidth=1, stroke="grey")) group.setAttribute('label', self.description) group.set_class('clusterblast-cluster') groups.append(group) base = line_y + 5 offset = h_offset + 10 - self.start # 10 for margin for index, gene in enumerate(self.genes): arrow = gene.get_arrow_polygon(scaling=scaling, offset=offset, base=base, colour=colours.get(gene.name, "white")) arrow.set_id("%s-%s_q%s_%s_%s" % (prefix, self.query_cluster_number, index, self.rank, "all")) group.addElement(arrow) return groups
def get_svg_groups(self, h_offset: int = 0, v_offset: int = 0, scaling: float = 1., screenwidth: int = 1024, colours: Dict[str, str] = None, overview: bool = False, prefix: str = "dummy") -> List[Group]: """ Returns all SVG elements required to draw the Cluster """ if not colours: colours = {} groups = [] group = Group() self._add_label(group, v_offset) line_y = 35 + v_offset group.addElement(ShapeBuilder().createLine(10, line_y, 10 + (screenwidth * 0.75), line_y, strokewidth=1, stroke="grey")) groups.append(group) # Add gene arrows arrow_y = line_y + 5 offset = h_offset - self.start if self.reversed: offset = h_offset + self.end for i, gene in enumerate(self.genes): group = Group() arrow = gene.get_arrow_polygon(scaling=scaling, offset=offset, base=arrow_y, colour=colours.get(gene.name, "white")) if overview: label = "all_" else: label = "h" arrow.set_id("%s-%d_%s%d_%s_%s" % (prefix, self.query_cluster_number, label, self.query_cluster_number, self.rank, i)) group.addElement(arrow) # Can be used for domains group.set_id("a%s_00%s" % (self.query_cluster_number, i)) groups.append(group) return groups
def _draw_vertical_line(self, x, colour): return ShapeBuilder().createLine(x, 0, x, self._scene.height, strokewidth=0.5, stroke=colour)
def _draw_background(self): svg_color = self._map_svg_color(self._appearence.get_bg_colour()[:3]) return ShapeBuilder().createRect(0, 0, self._scene.width, self._scene.height, fill=svg_color)
def _draw_categories_box(self, nbr_of_categories): return ShapeBuilder().createRect( self._get_categories_box_x(), self._get_categories_box_y(nbr_of_categories), self._get_categories_box_width(), self._get_categories_box_height(nbr_of_categories), fill='white')
def _draw_divider_line(self): return ShapeBuilder().createLine(0, self._scene.divider_y, self._scene.width, self._scene.divider_y, strokewidth=0.5, stroke="grey")
def testIterationsWithUse(): s = svg() d = defs() sb = ShapeBuilder() rectangle = sb.createRect(0, 0, 100, 200, fill="000") rectangle.set_id("baseRect") d.addElement(rectangle) s.addElement(d) coords = [(1, 1), (200, 200)] for (x, y) in coords: r = use() r.set_x(x) r.set_y(y) r.set_xlink_href("#baseRect") s.addElement(r) s.save("./testoutput/iterationsWithUse.svg")
def testIterationsWithUse(): s = Svg() d = Defs() sb = ShapeBuilder() rectangle = sb.createRect(0, 0, 100, 200, fill='000') rectangle.set_id('baseRect') d.addElement(rectangle) s.addElement(d) coords = [(1, 1), (200, 200)] for (x, y) in coords: r = Use() r.set_x(x) r.set_y(y) r.set_xlink_href('#baseRect') s.addElement(r) s.save('./testoutput/iterationsWithUse.svg')
def chair(self, id_attr, color_1, color_2): head = ShapeBuilder().createCircle(30, 25, 20, stroke='black', strokewidth=5.0, fill=color_1) head.set_class('head') body = path(pathData="M 19.264266,38.267870 C 12.892238,41.659428 9.0221978,48.396703 6.6126745,55.405840 L 51.476471,55.405840 C 49.270169,48.545436 45.682644,41.911786 39.811885,38.267870 C 33.901416,38.010889 26.459633,38.267870 19.264266,38.267870 z ") body.set_style('stroke-width:5.0;stroke:black;fill:%s;' % color_2) body.set_class('body') th = TransformBuilder() th.setScaling('0.8', '0.8') group = g() group.addElement(body) group.addElement(head) group.set_id(id_attr) group.set_transform(th.getTransform()) return group
def testIterationsWithUse(): s=Svg() d = Defs() sb =ShapeBuilder() rectangle = sb.createRect(0,0,100,200,fill='000') rectangle.set_id('baseRect') d.addElement(rectangle) s.addElement(d) coords=[(1,1),(200,200)] for (x,y) in coords: r=Use() r.set_x(x) r.set_y(y) r.set_xlink_href('#baseRect') s.addElement(r) s.save('./testoutput/iterationsWithUse.svg')
def _draw_category_color_box(self, item_height, x, y, cat): base_color = self._map_svg_color(cat.color) border_color = self._map_svg_color(darken_color(cat.color)) return ShapeBuilder().createRect(x + OUTER_PADDING, y, item_height, item_height, fill=base_color, stroke=border_color)
def _draw_era_strip(self, era): svg_color = self._map_svg_color(era.get_color()[:3]) x, width = self._calc_era_strip_metrics(era) return ShapeBuilder().createRect(x, INNER_PADDING, width, self._scene.height - 2 * INNER_PADDING, fill=svg_color, strokewidth=0)
def render(self, svg, x, y): stroke_width = self.conf.connection.thickness shape_builder = ShapeBuilder() start_x = x start_y = y + self.connect_y end_x = x + self.content_width + 20 x += 20 for child in self.children: child.render(svg, x, y) l = shape_builder.createLine(x + child.width, y + child.connect_y, x + self.content_width, y + child.connect_y, strokewidth=stroke_width) svg.addElement(l) path_data = "m {0},{1} c {2},0 0,{3} {2},{3}".format(start_x, start_y, 20, y + child.connect_y - start_y) svg.addElement(path(path_data, stroke = "black", fill="none", stroke_width=stroke_width)) path_data = "m {0},{1} c {2},0 0,{3} {2},{3}".format(end_x, y + child.connect_y, 20, start_y - (y + child.connect_y)) svg.addElement(path(path_data, stroke = "black", fill="none", stroke_width=stroke_width)) y += child.height + self.padding
def render(self, svg, x, y): self.content.render(svg, x + 20, y + 20) stroke_width = self.conf.connection.thickness connect_y = y + self.connect_y above_y = connect_y - self.content.height_above - 10 path_data = "m {0},{1} c -10,0 -10,{3} 0,{3}".format(x + 20, connect_y, 20, above_y - connect_y) svg.addElement(path(path_data, stroke = "black", fill = "none", stroke_width=stroke_width)) path_data = "m {0},{1} c 10,0 10,{3} 0,{3}".format(x + 20 + self.content.width, above_y, 20, connect_y - above_y) svg.addElement(path(path_data, stroke = "black", fill = "none", stroke_width=stroke_width)) shape_builder = ShapeBuilder() Line(0, 0, self.conf, arrow=True).render(svg, x + 25 + self.content.width / 2, above_y) l = shape_builder.createLine(x + 20, above_y, x + 20 + self.content.width, above_y, strokewidth=stroke_width) svg.addElement(l) l = shape_builder.createLine(x, connect_y, x + 20, connect_y, strokewidth=stroke_width) svg.addElement(l) l = shape_builder.createLine(x + 20 + self.content.width, connect_y, x + 20 + self.content.width + 20, connect_y, strokewidth=stroke_width) svg.addElement(l)
def __init__(self, conf): self.__lineType = conf['connection']['style']; self.__thickness = conf['connection']['thickness']; self.__markerSize = conf['connection']['marker']; self.__refLineType = conf['reference']['style']; self.__refMarkerSize = conf['reference']['marker']; self.__refThickness = conf['reference']['thickness']; self.__shapeBuilder = ShapeBuilder()
def diff(spikes=9, radius=10): points = [] sb = ShapeBuilder() for i, angle in enumerate(xrange(0, 360 + 1, float(180) / spikes)): if i % 2 == 0: r = radius * .8 else: r = radius radians = angle * math.pi / 180 coord = '{0},{1}'.format(r * math.cos(radians), r * math.sin(radians)) print "ANGLE", angle, "COOR", coord, "RAD", radians points.append(coord) print "POINTS", points line = sb.createPolyline(points=' '.join(points)) fill = BLACK stroke = ORANGE_3 style = 'fill:{0};stroke-width:{1};stroke:{2}'.format(fill, 2, stroke) line.set_style(style) return [line]
def diff(spikes=9, radius=10): points = [] sb = ShapeBuilder() for i, angle in enumerate(xrange(0, 360+1, float(180)/spikes)): if i % 2 == 0: r = radius * .8 else: r = radius radians = angle * math.pi/180 coord = '{0},{1}'.format(r*math.cos(radians), r*math.sin(radians)) print "ANGLE", angle, "COOR", coord, "RAD", radians points.append(coord) print "POINTS", points line = sb.createPolyline(points=' '.join(points)) fill = BLACK stroke = ORANGE_3 style = 'fill:{0};stroke-width:{1};stroke:{2}'.format(fill, 2, stroke) line.set_style(style) return [line]
def createblock(number): colors = {} colors[2]=('#eee4da','#776e65') colors[4]=('#ede0c8','#776e65') colors[8]=('#f2b179','#f9f6f2') colors[16]=('#f59563','#f9f6f2') colors[32]=('#f67c5f','#f9f6f2') colors[64]=('#f65e3b','#f9f6f2') colors[128]=('#edcf72','#f9f6f2') colors[256]=('#edcc61','#f9f6f2') colors[512]=('#eee4da','#776e65') colors[1024]=('#edc53f','#f9f6f2') colors[2048]=('#edc22e','#f9f6f2') canvas = Svg(0,0,100,100) sb = ShapeBuilder() canvas.addElement( sb.createRect(5,5,90,90,fill=colors[number][0]) ) t = Text(number,50,60) t.set_style("font-family:FreeSans;font-weight:bold;font-size:36px;text-anchor:middle") t.set_fill(colors[number][1]) canvas.addElement(t) canvas.save('/tmp/try7.svg')
def _draw_contents_indicator(self, event, rect): """ The data contents indicator is a small triangle drawn in the upper right corner of the event rectangle. """ corner_x = rect.X + rect.Width points = "%d,%d %d,%d %d,%d" % \ (corner_x - DATA_INDICATOR_SIZE, rect.Y, corner_x, rect.Y, corner_x, rect.Y + DATA_INDICATOR_SIZE) color = self._get_box_indicator_color(event) indicator = ShapeBuilder().createPolygon(points, fill=color, stroke=color) # TODO (low): Transparency ? return indicator
def __init__(self, conf): self.__shapeBuilder = ShapeBuilder() self.__fontSize = conf['frame']['font']['size']; self.__fontFamily = conf['frame']['font']['name']; self.__alignment = conf['frame']['font']['align']; self.__frameThickness = conf['frame']['thickness']; self.__frameWidth = conf['frame']['width']; self.__framePadding = conf['frame']['padding']; self.__separatorWidth = conf['frame']['separator']['width']; self.__textStyle = StyleBuilder() self.__textStyle.setFontFamily(self.__fontFamily) self.__textStyle.setFontSize(self.__fontSize.__str__() + 'px') self.__LINE_SEPARATOR = 5; self.__resolver = BBCodeResolver();
class NodeRenderer(object): def __init__(self, conf): self.__shapeBuilder = ShapeBuilder() self.__fontSize = conf['frame']['font']['size']; self.__fontFamily = conf['frame']['font']['name']; self.__alignment = conf['frame']['font']['align']; self.__frameThickness = conf['frame']['thickness']; self.__frameWidth = conf['frame']['width']; self.__framePadding = conf['frame']['padding']; self.__separatorWidth = conf['frame']['separator']['width']; self.__textStyle = StyleBuilder() self.__textStyle.setFontFamily(self.__fontFamily) self.__textStyle.setFontSize(self.__fontSize.__str__() + 'px') self.__LINE_SEPARATOR = 5; self.__resolver = BBCodeResolver(); def __createLines(self, values): result = []; for value in values: if isinstance(value, basestring): lines = self.__resolver.resolveString(value); if isinstance(lines, list): for line in lines: result.append(line); elif isinstance(value, int): result.append(value); else: assert 1 == 2 return result; def __determineLineHeight(self, isSeparator): if isSeparator: return self.__separatorWidth + self.__LINE_SEPARATOR; else: return self.__fontSize + self.__LINE_SEPARATOR; def __determineContainterHeight(self, lines): #TODO wrapping! height = 0; for line in lines: if isinstance(line, int): height = height + self.__determineLineHeight(True); else: height = height + self.__determineLineHeight(False); return height + 2 * self.__framePadding; def __prepareNodeContainer(self, startX, startY, width, height, isReference): nodeGroup = g() nodeGroup.set_style(self.__textStyle.getStyle()) if isReference: color = 'gray'; else: color = 'white'; rect = self.__shapeBuilder.createRect(startX, startY, width, height, strokewidth = self.__frameThickness, stroke='black', fill=color) nodeGroup.addElement(rect) return nodeGroup; 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; #pseudo static method def getNodeHeight(self, node): lines = self.__createLines(node['value']); return self.__determineContainterHeight(lines);
from pysvg.builders import Svg, ShapeBuilder, StyleBuilder from pysvg.text import * canvas = Svg(0,0,100,100) sb = ShapeBuilder() canvas.addElement( sb.createRect(5,5,90,90,fill="#00FF00") ) t = Text('Hello!',50,50) t.set_style("font-family:FreeSans;font-weight:bold;font-size:24px;text-anchor:middle") t.set_fill("#FF0000") canvas.addElement(t) canvas.save('/tmp/try7.svg')
from pysvg.builders import Svg, ShapeBuilder, StyleBuilder from pysvg.text import * canvas = Svg(0, 0, 100, 100) sb = ShapeBuilder() canvas.addElement(sb.createRect(5, 5, 90, 90, fill="#00FF00")) t = Text('Hello!', 50, 50) t.set_style( "font-family:FreeSans;font-weight:bold;font-size:24px;text-anchor:middle") t.set_fill("#FF0000") canvas.addElement(t) canvas.save('/tmp/try7.svg')