def _get_font_style(self, size, anchor, dash_array): style = StyleBuilder() style.setStrokeDashArray(dash_array) style.setFontFamily(fontfamily="Verdana") style.setFontSize("%dpx" % size) style.setTextAnchor(anchor) return style
def draw_tree(width, height): global nameStyle nameStyle = StyleBuilder() nameStyle.setFontFamily(fontfamily=dc.nameFont) nameStyle.setFontSize("%spt" % dc.nameFontSize) nameStyle.setTextAnchor("left") nameStyle = nameStyle.getStyle() svg = Svg(width=width, height=height) # draw nodes for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] draw_node(svg, node, name) # draw branches for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] isLeaf = (node.children == []) if (isLeaf): continue numChildren = len(node.children) for (i, child) in enumerate(node.children): rootFrac = 0.4 + (0.2 * i) / (numChildren - 1) sinkFrac = 0.5 if (orientation == "T2B"): (rootX, rootY) = (node.x + rootFrac * dc.nodeWdt, node.y + dc.nodeHgt) (sinkX, sinkY) = (child.x + sinkFrac * dc.nodeWdt, child.y) draw_vert_branch(svg, "%s_branch_%d" % (name, i), rootX, rootY, sinkX, sinkY) else: # if (orientation == "L2R"): (rootX, rootY) = (node.x + dc.nodeWdt, node.y + rootFrac * dc.nodeHgt) (sinkX, sinkY) = (child.x, child.y + sinkFrac * dc.nodeHgt) draw_horz_branch(svg, "%s_branch_%d" % (name, i), rootX, rootY, sinkX, sinkY) return svg
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 genLabels(self): labels = G() labelStyle = StyleBuilder() labelStyle.setTextAnchor('middle') labelStyle.setFontSize(10) lineStyle = StyleBuilder() lineStyle.setStroke('#a9a9a9') dimLabelA = str(self.dimA) + ' nm' dimLabelB = str(self.dimB) + ' nm' # horizontal label cornerHL = self.view.getTopLeft() cornerHR = self.view.getTopRight() hLabelStart = [cornerHL[0], cornerHL[1] + 5] hLabelEnd = [cornerHR[0], cornerHR[1] + 5] hLine = Line(hLabelStart[0], hLabelStart[1], hLabelEnd[0], hLabelEnd[1]) hLabel = Text(dimLabelA, (hLabelStart[0] + hLabelEnd[0])/2, hLabelEnd[1] + 20) hLine.set_style(lineStyle.getStyle()) hLabel.set_style(labelStyle.getStyle()) cornerVL = self.view.getTopLeft() cornerVR = self.view.getBottomLeft() vLabelStart = [cornerVL[0] - 5, cornerVL[1]] vLabelEnd = [cornerVR[0] - 5, cornerVR[1]] vLine = Line(vLabelStart[0], vLabelStart[1], vLabelEnd[0], vLabelEnd[1]) vLabel = Text(dimLabelB, vLabelEnd[0] - 30, (vLabelStart[1] + vLabelEnd[1])/2) vLine.set_style(lineStyle.getStyle()) vLabel.set_style(labelStyle.getStyle()) labels.addElement(hLine) labels.addElement(hLabel) labels.addElement(vLine) labels.addElement(vLabel) #center = [self.gridCoord[0] + self.dimA*SCALING_FACTOR/2, self.gridCoord[1] + self.dimB*SCALING_FACTOR + LABEL_SHIFT] #dimLabel = str(self.dimA) + 'nm x ' +str(self.dimB) + 'nm' #labels = Text(dimLabel, center[0], center[1]) #labels.set_style(labelStyle.getStyle()) return labels
def Scalebars(smin, smax, xmin, xmax): c_lines = StyleBuilder(config['scaleline_style']) c_times = StyleBuilder(config['scale_style']) c_times.setFontSize(str(config['scale_font_height']) + 'px') g_scale = G() g_scale_lines = G() g_scale_times = G() g_scale_lines.set_style(c_lines.getStyle()) g_scale_times.set_style(c_times.getStyle()) g_scale_times.setAttribute('xml:space', 'preserve') # seconds value of largest minute smaller than first time start = (int(smin) / 60) * 60 # seconds value of smallest minute larger than last time end = 60 + ((int(smax) / 60) * 60) # if the max time wasn't exactly on a minute interval, add one more if end < smax + 60: end += 60; # one scale bar for each minute in the range, including last for s in range(start, end, 60): y = config['vscale'] * (s - smin) sline = Line(xmin, y, xmax, y) if config['scaleleft']: # note hard coded expectation of 7 char max scale label lx = xmin - 5 - (7 * config['scale_font_width']) else: lx = xmax + 5 stime = Text(time(s), lx, y + 6) g_scale_lines.addElement(sline) g_scale_times.addElement(stime) g_scale.addElement(g_scale_lines) g_scale.addElement(g_scale_times) return g_scale
def genLabels(self): labels = G() labelStyle = StyleBuilder() labelStyle.setTextAnchor('middle') labelStyle.setFontSize(10) lineStyle = StyleBuilder() lineStyle.setStroke('#a9a9a9') dimLabelA = str(self.x/SCALING_FACTOR) + ' nm' dimLabelB = str(self.z/SCALING_FACTOR) + ' nm' # horizontal label cornerHL = self.hCSlice.getTopLeft() cornerHR = self.hCSlice.getTopRight() hLabelStart = [cornerHL[0], cornerHL[1] + 5] hLabelEnd = [cornerHR[0], cornerHR[1] + 5] hLine = Line(hLabelStart[0], hLabelStart[1], hLabelEnd[0], hLabelEnd[1]) hLabel = Text(dimLabelA, (hLabelStart[0] + hLabelEnd[0])/2, hLabelEnd[1] + 20) hLine.set_style(lineStyle.getStyle()) hLabel.set_style(labelStyle.getStyle()) cornerVL = self.hCSlice.getTopLeft() cornerVR = self.hCSlice.getBottomLeft() vLabelStart = [cornerVL[0] - 5, cornerVL[1]] vLabelEnd = [cornerVR[0] - 5, cornerVR[1]] vLine = Line(vLabelStart[0], vLabelStart[1], vLabelEnd[0], vLabelEnd[1]) vLabel = Text(dimLabelB, vLabelEnd[0] - 30, (vLabelStart[1] + vLabelEnd[1])/2) vLine.set_style(lineStyle.getStyle()) vLabel.set_style(labelStyle.getStyle()) labels.addElement(hLine) labels.addElement(hLabel) labels.addElement(vLine) labels.addElement(vLabel) return labels
actual_page_width = config['pagewidth'] total_label_width = 0 for r in races: total_label_width += r['wmax_label'] * config['label_font_width'] while total_label_width > actual_page_width: actual_page_width += config['pagewidth'] gap_count = len(races) - 1 scale_label_width = ((7 * config['scale_font_width']) + 5 if config['scalebars'] else 0) free_space = actual_page_width - total_label_width - scale_label_width - (gap_count * 2 * config['gutter']) calc_linespan = free_space / gap_count config['linespan'] = calc_linespan # SVG styles s_label = StyleBuilder(config['label_style']) s_label.setFontSize(str(config['label_font_height']) + 'px') # SVG Groups g_label = G() g_label.set_style(s_label.getStyle()) g_label.setAttribute('xml:space', 'preserve') g_linkline = G() g_linkline.set_style(StyleBuilder(config['linkline_style']).getStyle()) g_weaklink = G() g_weaklink.set_style(StyleBuilder(config['weaklink_style']).getStyle()) g_underline = G() g_underline.set_style(StyleBuilder(config['underline_style']).getStyle()) for r in range(0, len(races)):
def draw_tree(): if (dc.nameFontSize < 16): scale = dc.nameFontSize / 16.0 nameCapsHgt = dc.nameCapsHgt * scale nameDescHgt = dc.nameDescHgt * scale nameFontLineHgt = dc.nameFontLineHgt * scale nameStyle = StyleBuilder() nameStyle.setFontFamily(fontfamily=dc.nameFont) nameStyle.setFontSize("%spt" % dc.nameFontSize) nameStyle.setTextAnchor("left") nameStyle = nameStyle.getStyle() svg = Svg() # draw nodes for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] isLeaf = (node.left == None) yLine = node.y + nameCapsHgt + 1 ob = SvgRect(node.x, node.y, dc.nodeWidth, dc.nodeHeight, id="%s_box" % name) ob.set_stroke(dc.lineColor) ob.set_stroke_width(dc.lineThickness) if (isLeaf): ob.set_fill(dc.leafFillColor) else: ob.set_fill(dc.nodeFillColor) svg.addElement(ob) ob = SvgText("%s" % name, node.x + 1, yLine, id="%s_name" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsUnion")): ob = SvgText("U:" + bits_to_string(node.numBits, node.bitsUnion), node.x + 1, yLine, id="%s_Bunion" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsIntersection")): ob = SvgText( "I:" + bits_to_string(node.numBits, node.bitsIntersection), node.x + 1, yLine, id="%s_Bintersection" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsAll")): ob = SvgText("A:" + bits_to_string(node.numBits, node.bitsAll), node.x + 1, yLine, id="%s_Ball" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt if (hasattr(node, "bitsSome")): ob = SvgText("S:" + bits_to_string(node.numBits, node.bitsSome), node.x + 1, yLine, id="%s_Bsome" % node) ob.set_style(nameStyle) svg.addElement(ob) yLine += nameFontLineHgt # draw branches for depth in depthToNames: for name in depthToNames[depth]: node = nameToNode[name] if (node.left == None): continue (leftStartX, leftStartY) = (node.x + 0.4 * dc.nodeWidth, node.y + dc.nodeHeight) (rightStartX, rightStartY) = (node.x + 0.6 * dc.nodeWidth, node.y + dc.nodeHeight) (leftEndX, leftEndY) = (node.left.x + 0.5 * dc.nodeWidth, node.right.y) (rightEndX, rightEndY) = (node.right.x + 0.5 * dc.nodeWidth, node.right.y) draw_branch(svg, "%s_left_branch" % node, leftStartX, leftStartY, leftEndX, leftEndY) draw_branch(svg, "%s_right_branch" % node, rightStartX, rightStartY, rightEndX, rightEndY) return svg
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);