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