def _draw_stroke(self, cairo_context, tree): """ Draws the tree defined by the edges in one stroke and finishes the stroke. #Draws the tree defined by the edges in one stroke but does not finish the stroke. #Calling cairo_context.stroke_extents() after this function returns will get the bounding box of the stroke. #Calling cairo_context.stroke() will finish the stroke. @param cairo_context: a drawing context @param tree: ... """ for branch in tree.gen_branches(): color = getattr(branch, 'branch_color', None) if color: cairo_context.set_source_rgb(*CairoUtil.hex_string_to_cairo_rgb(color)) cairo_context.move_to(*branch.src_location) cairo_context.line_to(*branch.dst_location) cairo_context.stroke() if color: cairo_context.set_source_rgb(0, 0, 0)
def _draw_stroke(self, cairo_context, tree): """ Draws the tree defined by the edges in one stroke and finishes the stroke. #Draws the tree defined by the edges in one stroke but does not finish the stroke. #Calling cairo_context.stroke_extents() after this function returns will get the bounding box of the stroke. #Calling cairo_context.stroke() will finish the stroke. @param cairo_context: a drawing context @param tree: ... """ for branch in tree.gen_branches(): color = getattr(branch, 'branch_color', None) if color: cairo_context.set_source_rgb( *CairoUtil.hex_string_to_cairo_rgb(color)) cairo_context.move_to(*branch.src_location) cairo_context.line_to(*branch.dst_location) cairo_context.stroke() if color: cairo_context.set_source_rgb(0, 0, 0)
def get_tree_image(tree, max_size, image_format): """ Get the image of the tree. @param tree: something like a SpatialTree @param max_size: (max_width, max_height) @param image_format: a string that determines the image format @return: a string containing the image data """ # rotate and center the tree on (0, 0) tree.fit(max_size) # get the width and height of the tree image xmin, ymin, xmax, ymax = tree.get_extents() width = xmax - xmin height = ymax - ymin # create the surface cairo_helper = CairoUtil.CairoHelper(image_format) surface = cairo_helper.create_surface(width, height) context = cairo.Context(surface) # draw the background context.save() context.set_source_rgb(.9, .9, .9) context.paint() context.restore() # center on the tree context.translate(width/2.0, height/2.0) # draw the branches for branch in tree.gen_branches(): context.save() color = getattr(branch, 'branch_color', None) if color: context.set_source_rgb(*CairoUtil.hex_string_to_cairo_rgb(color)) context.move_to(*branch.src_location) context.line_to(*branch.dst_location) context.stroke() context.restore() # get the image string return cairo_helper.get_image_string()
def get_tree_image(tree, max_size, image_format): """ Get the image of the tree. @param tree: something like a SpatialTree @param max_size: (max_width, max_height) @param image_format: a string that determines the image format @return: a string containing the image data """ # rotate and center the tree on (0, 0) tree.fit(max_size) # get the width and height of the tree image xmin, ymin, xmax, ymax = tree.get_extents() width = xmax - xmin height = ymax - ymin # create the surface cairo_helper = CairoUtil.CairoHelper(image_format) surface = cairo_helper.create_surface(width, height) context = cairo.Context(surface) # draw the background context.save() context.set_source_rgb(.9, .9, .9) context.paint() context.restore() # center on the tree context.translate(width / 2.0, height / 2.0) # draw the branches for branch in tree.gen_branches(): context.save() color = getattr(branch, 'branch_color', None) if color: context.set_source_rgb(*CairoUtil.hex_string_to_cairo_rgb(color)) context.move_to(*branch.src_location) context.line_to(*branch.dst_location) context.stroke() context.restore() # get the image string return cairo_helper.get_image_string()