def __init__(self, **kwargs): # The input type of the module self.type = 'svg' self.inkscape_size = False # Add args to the module self.check_args_from_theme(kwargs) # Build style for the rectangle beampy_svg_kword = { 'color': 'fill', 'linewidth': 'stroke-width', 'opacity': 'opacity', 'edgecolor': 'stroke' } self.style = '' for kw in beampy_svg_kword: if hasattr(self, kw): self.style += '%s:%s;' % (beampy_svg_kword[kw], getattr(self, kw)) self.cx = convert_unit(self.r) + int(convert_unit(self.linewidth) / 2) self.cy = convert_unit(self.r) + int(convert_unit(self.linewidth) / 2) self.content = '<circle cx="{cx}" cy="{cy}" r="{r}" style="{style}" />' # Store svg definitions self.svgdefs = [] self.svgdefsargs = [] # Register the module self.register()
def __init__(self, x2, y2, **kwargs): # The input type of the module self.type = 'svg' # Add args to the module self.check_args_from_theme(kwargs) self.x2 = x2 self.y2 = y2 # convert unit of x2 and y2 self.x2 = convert_unit(self.x2) self.y2 = convert_unit(self.y2) self.args['x2'] = self.x2 self.args['y2'] = self.y2 # Build style for the rectangle beampy_svg_kword = { 'color': 'stroke', 'linewidth': 'stroke-width', 'opacity': 'opacity' } style = '' for kw in beampy_svg_kword: if hasattr(self, kw): style += '%s:%s;' % (beampy_svg_kword[kw], getattr(self, kw)) self.content = '<line x1="0" y1="0" x2="{x2}px" y2="{y2}px" style="{style}"/>'.format( x2=self.x2, y2=self.y2, style=style) # Register the module self.register()
def pre_render(self): self.content = self.content.format(r=self.r, cx=self.cx, cy=self.cy, style=self.style) self.width = convert_unit(self.r) * 2 + convert_unit(self.linewidth) self.height = convert_unit(self.r) * 2 + convert_unit(self.linewidth) self.update_size(self.width, self.height)
def __init__(self, dx, dy, **kwargs): # The input type of the module self.type = 'svg' self.inkscape_size = True # Add args to the module self.check_args_from_theme(kwargs) self.dx = dx self.dy = dy # convert unit of x2 and y2 self.dx = convert_unit(self.dx) self.dy = convert_unit(self.dy) self.args['dx'] = self.dx self.args['dy'] = self.dy # Build style for the rectangle beampy_svg_kword = { 'color': 'stroke', 'linewidth': 'stroke-width', 'opacity': 'opacity' } self.style = '' for kw in beampy_svg_kword: if hasattr(self, kw): self.style += '%s:%s;' % (beampy_svg_kword[kw], getattr(self, kw)) curslide = document._slides[self.slide_id] base_hline = '<line id="{id}" x1="0" y1="0" x2="{x2}px" y2="{y2}px" style="{style}"/>' self.content = base_hline self.content = self.content.format(x2=curslide.curwidth, y2=0, style=self.style, id='hlineXX')
def render(self): """ The render of an svg part """ #Need to create a temp file if self.inkscape_size: logging.debug('Run inkscape to get svg size') # Need to get the height and width of the svg command tmpsvg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.2" baseProfile="tiny" xmlns:xlink="http://www.w3.org/1999/xlink">' if self.out_svgdefs is not None: tmpsvg += '<defs>%s</defs>' % (' '.join(self.out_svgdefs)) tmpsvg += ' %s</svg>' % (self.content) # Use NamedTemporaryFile, that automatically remove the file on close by default with tempfile.NamedTemporaryFile(mode='w', prefix='beampytmp', suffix='.svg') as f: f.write(tmpsvg) # Need to flush the file to make it's content updated on disk f.file.flush() # Get the dimension of the svg using inkscape svg_width = getsvgwidth(f.name) svg_height = getsvgheight(f.name) else: svg_width = convert_unit(self.width.value) svg_height = convert_unit(self.height.value) #Update the final svg size self.update_size(svg_width, svg_height) #Add the final svg output of the figure self.svgout = self.content #Set rendered flag to true (needed for the cache) self.rendered = True
def __init__(self, **kwargs): # The input type of the module self.type = 'svg' # Add args to the module self.check_args_from_theme(kwargs) # The size can be computed easily if no filter or clip path is given if self.svgclip is not None or self.svgfilter is not None: self.inkscape_size = True else: self.inkscape_size = False # Build style for the rectangle beampy_svg_kword = { 'color': 'fill', 'linewidth': 'stroke-width', 'opacity': 'opacity', 'edgecolor': 'stroke' } self.style = '' for kw in beampy_svg_kword: if hasattr(self, kw): self.style += '%s:%s;' % (beampy_svg_kword[kw], getattr(self, kw)) self.dxdy = int(convert_unit(self.linewidth) / 2) self.content = ''''<rect x="{dx}" y="{dy}" rx="{rx}" ry="{ry}" width="{width}" height="{height}" style="{style}" {filter} {clip}/>''' # Store svg definitions self.svgdefs = [] self.svgdefsargs = [] # Register the module self.register()
def vline(x, **kwargs): """ Create an horizontal line at a given vertical position. Accept all arguments of :py:mod:`beampy.line` Parameters ---------- x : int or float or {'center', 'auto'} or str Horizontal position for the line (the default theme sets this to 'auto'). See positioning system of Beampy. See Also -------- :py:mod:`beampy.line` """ if isinstance(x, str): x = convert_unit(x) x = '%spx' % x return line(x=x, y=0, y2='%spx' % document._height, x2=0, **kwargs)
def hline(y, **kwargs): """ Create an horizontal line at a given horizontal position. Accept all arguments of :py:mod:`beampy.line` Parameters ---------- y : int or float or {'center', 'auto'} or str Vertical position for the line (the default theme sets this to 'auto'). See positioning system of Beampy. See Also -------- :py:mod:`beampy.line` """ if isinstance(y, str): y = convert_unit(y) y = '%spx' % y return line(x=0, y=y, x2='%spx' % document._width, y2=0, **kwargs)