def render_nlpgraphics(renderer, filtered, filepath: str = None, output_type: str = 'SVG'): """Render an NLPInstance into the supported formats. Args: renderer (SingleSentenceRenderer or AlignmentRenderer): The renderer object. filtered (NLPInstance): The filtered NLPInstane to be rendered. filepath (str): The path of the outputfile. output_type (str): The type of the output format. Returns: The bytesting of the rendered object if needed. """ svg_scene = Drawing(size=('100%', '100%')) # default: '100%', '100%' dim = renderer.render(filtered, svg_scene) # Set the actual computed dimension without rerendering svg_scene.attribs['width'] = dim[0] svg_scene.attribs['height'] = dim[1] svg_bytes = svg_scene.tostring().encode('UTF-8') if filepath is not None and output_type == 'SVG': svg_scene.saveas(filepath) elif filepath is None and output_type == 'SVG': return svg_bytes elif output_type == 'PS': cairosvg.svg2ps(bytestring=svg_bytes, write_to=filepath) elif output_type == 'PDF': cairosvg.svg2pdf(bytestring=svg_bytes, write_to=filepath) else: raise ValueError( '{0} not a supported filetype!'.format(output_type))
def test_save_as(self): fn = 'test_drawing.svg' if os.path.exists(fn): os.remove(fn) dwg = Drawing() dwg.saveas(fn) self.assertTrue(os.path.exists(fn)) os.remove(fn)
def test_save_as(self): fn = Path('test_drawing.svg') if fn.exists(): fn.unlink() dwg = Drawing() dwg.saveas(str(fn)) self.assertTrue(fn.exists()) fn.unlink()