def get_svg(self, name=u"figure", debug=False): # TODO: is there any problem embedding `Drawing`s within `Drawing`s? container = svgwrite.Drawing(name, (px(self.width()), px(self.height()))) container.viewbox(minx=0, miny=0, width=self.width(), height=self.height()) container.fit() x_pos = self.padding for i in xrange(len(self.elements)): width = self.widths[i] box = svgwrite.container.SVG(x=x_pos, y=0, width=width, height=self.elements[i].height()) box.add(self.svg_contents[i]) if debug: box.add( svgwrite.shapes.Rect(insert=(u"0%", u"0%"), size=(u"100%", u"100%"), fill=u"none", stroke=u"red")) container.add(box) x_pos += width + self.padding return container
def node_layout_unary_grid(self, label, daughter, parent_dir=None): if self.options.debug: border = u"border: 1px solid #848482;" else: border = u"border:none;" line_height = px( self.options.em_to_px(self.options.distance_to_daughter)) e = Element( u"div", align=u"center", style= (u"display:inline-grid;grid-template-columns: 1fr;align-items:start;" + border)) label_cell = SubElement(e, u"div", style=u"grid-column-1;grid-row:1;", align=u"center") label_cell.append(to_html(label, debug=self.options.debug)) line_cell = SubElement( e, u"div", align=u"center", style=u"grid-column-1;grid-row:2;border:0;height:%s;" % line_height) line_cell.append(line_svg(0, 0)) d_cell = SubElement(e, u"div", style=u"grid-column-1;grid-row:3;") d_cell.append(daughter) return e
def node_layout_unary_text(self, label, *daughters, parent_dir=None): if self.options.debug: border = "border: 1px solid #848482;" else: border = "border:none;" line_height = px(self.options.em_to_px( self.options.distance_to_daughter)) e = Element("div", style=("display:inline-grid;grid-template-columns: 1fr;align-items:start;" + border)) row = 1 if parent_dir is not None: if parent_dir < 0: svg = line_svg(-1,0) elif parent_dir > 0: svg = line_svg(1,0) else: svg = line_svg(0,0) line_cell = SubElement(e, "div", style="grid-column:1;grid-row:1;height:%s;" % line_height, align="center") line_cell.append(svg) row += 1 label_cell = SubElement(e, "div", style="grid-column:1;grid-row:%d;justify-self:center;" % row) row += 1 label_cell.append(to_html(label, debug=self.options.debug)) if len(daughters): d_cell = SubElement(e, "div", style="grid-column:1;grid-row:%d;justify-self:center;" % row) d_cell.append(daughters[0]) return e
def node_layout_binary_even(self, label, d1, d2, parent_dir=None): if self.options.debug: border = "border: 1px solid #848482;" else: border = "border:none;" line_height = px(self.options.em_to_px(self.options.distance_to_daughter)) e = Element("div", style="display:inline-grid;grid-template-columns: repeat(2, 1fr);align-items:start;" + border, align="center") label_cell = SubElement(e, "div", style="grid-column:1/3;grid-row:1;grid-gap:0px", align="center") label_cell.append(to_html(label, debug=self.options.debug)) line_cell = SubElement(e, "div", align="center", style="grid-column:1;grid-row:2;height:%s;" % line_height) line_cell.append(line_svg(1, 0)) line2_cell = SubElement(e, "div", align="center", style="grid-column:2;grid-row:2;height:%s;" % line_height) line2_cell.append(line_svg(-1, 0)) d_cell = SubElement(e, "div", style="grid-column:1;grid-row:3;") d_cell.append(d1) d2_cell = SubElement(e, "div", style="grid-column:2;grid-row:3;") d2_cell.append(d2) return e
def get_svg(self, name="figure"): height = self.height() width = self.width() fig_height = self.content.height() container = svgwrite.Drawing(name, (px(width), px(height))) container.viewbox(minx=0, miny=0, width=width, height=height) container.fit() fig_box = svgwrite.container.SVG(x=(self.bracket_width + self.padding + 1), y=2, width=self.content.width(), height=self.content.height()) fig_box.add(self.content.get_svg()) container.add(fig_box) svg_double_bracket(container, 1, 1, self.bracket_width, height-2) svg_double_bracket(container, width-1, 1, -self.bracket_width, height-2) return container
def get_svg(self, name=u"figure"): # TODO: is there any problem embedding `Drawing`s within `Drawing`s? container = svgwrite.Drawing(name, (px(self.width()), px(self.height()))) container.viewbox(minx=0, miny=0, width=self.width(), height=self.height()) container.fit() y_pos = self.padding for i in xrange(len(self.elements)): height = self.elements[i].height() box = svgwrite.container.SVG(x=0, y=y_pos, height=height, width=self.elements[i].width()) box.add(self.svg_contents[i]) inherit_style(box, self.svg_contents[i]) container.add(box) y_pos += height + self.padding return container
def node_layout_binary_text(self, label, d1, d2, parent_dir=None): if self.options.debug: border = "border: 1px solid #848482;" else: border = "border:none;" line_height = px(self.options.em_to_px( self.options.distance_to_daughter)) e = Element("div", style="display:inline-grid;grid-template-columns: repeat(2, auto);align-items:start;" + border) row = 1 if parent_dir is not None: if parent_dir < 0: line = line_svg(-1, 1) line_col = "grid-column:1;" elif parent_dir > 0: line = line_svg(1, -1) line_col = "grid-column:2;" else: line = line_svg(0, 0) line_col = "grid-column:1/3;" line_div = SubElement(e, "div", style=line_col + "grid-row:1;height:%s;" % line_height) line_div.append(line) row += 1 # TODO: this is a shameful stack of hacks to get non-leaf nodes for # binary branches to position right. First, put the displayed label in # a floated cell positioned along the middle grid line in a 0-width # grid cell, and transformed to be centered. Second, show the same # content in a 0-height hidden div that is centered across the whole # grid, in order to get the label to contribute to grid width. Neither # of these tricks by themself does the whole thing: the floated div # alone can lead to non-leaf nodes overlapping, and the centered div # (if it were visible) wouldn't position short labels correctly. A # previous version used just the transform trick on a 1-grid cell, but # this led to bad spacing when the label is too long. label_cell = SubElement(e, "div", style="grid-row:%d;grid-column:1;justify-self:right;width:0;" % row) label_subdiv = to_html(label, debug=self.options.debug) label_dup = copy.deepcopy(label_subdiv) style_append(label_subdiv, "float:right;transform:translate(50%);white-space:nowrap;") label_cell.append(label_subdiv) spacer_cell = SubElement(e, "div", style="grid-row:%d;grid-column:1/3;justify-self:center;height:0;overflow:hidden;padding-right:1em;padding-left:1em;" % row) spacer_cell.append(label_dup) row += 1 d1_cell = SubElement(e, "div", style="grid-column:1;grid-row:%d;justify-self:right;" % row) d1_cell.append(d1) d2_cell = SubElement(e, "div", style="grid-column:2;grid-row:%d;" % row) d2_cell.append(d2) return e
def get_svg(self, name=u"figure", debug=False): width = self.width() height = self.height() fig_width = self.fig.width() caption_width = self.caption_width() container = svgwrite.Drawing(name, (px(width), px(height))) container.viewbox(minx=0, miny=0, width=width, height=height) container.fit() y_pos = self.fig.height() + 0.5 * self.font_size caption_svg = svgwrite.text.Text(self.caption, insert=(u"50%", u"1em"), text_anchor=u"middle", style=self.style_str()) # this next is to keep any font style from impacting the interpretation # of ems in positioning the box. caption_box = svgwrite.container.SVG(x=0, y=y_pos, width=u"100%", height=u"100%") if debug: caption_box.add( svgwrite.shapes.Rect(insert=(u"0%", u"0%"), size=(u"100%", u"100%"), fill=u"none", stroke=u"red")) caption_box.add(caption_svg) if (fig_width > caption_width): fig_x = 0 else: fig_x = (caption_width - fig_width) / 2.0 box = svgwrite.container.SVG(x=fig_x, y=0, width=fig_width, height=self.fig.height()) fig_svg = self.fig.get_svg() box.add(fig_svg) container.add(box) container.add(caption_box) return container
def node_layout_table(self, label, *daughters): if len(daughters) == 0: return to_html(label, debug=self.options.debug) if self.options.debug: border = "border: 1px solid #848482;" else: border = "" line_height = px(self.options.em_to_px( self.options.distance_to_daughter)) e = Element("div", style="display:table;" + border, align="center") if len(daughters) == 1: label_row = SubElement(e, "div", style="display:table-row;") label_cell = SubElement(label_row, "div", style="display:table-cell;", align="center") label_cell.append(to_html(label, debug=self.options.debug)) line_row = SubElement(e, "div", style="display:table-row;") line_cell = SubElement(line_row, "div", align="center", style="display:table-cell;height:%s;" % line_height) line_cell.append(line_svg(0, 0)) d_row = SubElement(e, "div", style="display:table-row;") d_cell = SubElement(d_row, "div", style="display:table-cell;") d_cell.append(daughters[0]) elif len(daughters) == 2: label_row = SubElement(e, "div", style="display:table-row;") label_cell = SubElement(label_row, "div", style="display:table-cell;transform:translate(50%);", align="center") label_cell.append(to_html(label, debug=self.options.debug)) extra_cell = SubElement(label_row, "div", style="display:table-cell;") SubElement(extra_cell, "span") # why is this necessary?? line_row = SubElement(e, "div", style="display:table-row;width:100%;") line_cell = SubElement(line_row, "div", align="center", style="display:table-cell;height:%s;" % line_height) line_cell.append(line_svg(-1, 0)) line2_cell = SubElement(line_row, "div", align="center", style="display:table-cell;height:%s;" % line_height) line2_cell.append(line_svg(1, 0)) d_row = SubElement(e, "div", style="display:table-row;width:100%;") d_cell = SubElement(d_row, "div", style="display:table-cell;") d_cell.append(daughters[0]) d2_cell = SubElement(d_row, "div", style="display:table-cell;") d2_cell.append(daughters[1]) else: raise NotImplementedError( "Trees with >2 daughters are not supported by " "html.DivTreeLayout") return e
def style_str(self): # TODO: generalize caption style return self.font_style + u" font-size: " + px(self.font_size) + u";"