Esempio n. 1
0
File: svg.py Progetto: PP-TSD/sdgen
def to_png(data, path=None, conf=None):
    '''
    Generate a svg image(s). This function can generate multiple images.
    @param data: input data
    @param path: path were to save files;
                             if the value is None no file is saved, overwriting is forbidden (raise exception)
    @param conf: configuration to substitute the default one
    @return: a list of tuples, each tuple contains two fields: a name and an image in svg format
    '''
    views_to_render = [data]
    views_to_render.extend(find_non_terminals(data))
    result = []
    for view in views_to_render:
        result.append((view["name"], create_diagram(view, Configuration(conf))))

    if path != None:
        for image in result:
            # replace spaces to underscores
            fstr = image[0].replace(" ", "_")

            file_name = os.path.join(path, fstr + ".svg")
            #if os.path.exists(file_name):
            #    raise Exception, "File already exists!"
            with open(file_name, 'w') as f:
                f.write(image[1].encode('utf-8'))

            #command = 'python CairoSVG-0.3.1//cairosvg.py file_name -f png -o ' + os.path.join(path, image[0] + ".png"
            #os.system(command);
            cairosvg.main(file_name, 'png', 300, os.path.join(path, fstr + ".png"))

    return result
Esempio n. 2
0
    def test_main(args, exit_=False, input_=None):
        """Test main called with given ``args``.

        If ``exit_`` is ``True``, check that ``SystemExit`` is raised. We then
        assume that the program output is an unicode string.

        If ``input_`` is given, use this stream as input stream.

        """
        sys.argv = ['cairosvg.py'] + args
        old_stdin, old_stdout = sys.stdin, sys.stdout

        output_buffer = io.BytesIO()
        if sys.version_info[0] >= 3:
            sys.stdout = io.TextIOWrapper(output_buffer)
        else:
            sys.stdout = output_buffer

        if input_:
            kwargs = {'stdin': open(input_, 'rb')}
            sys.stdin = open(input_, 'rb')
        else:
            kwargs = {}

        if exit_:
            try:
                main()
            except SystemExit:
                pass
            else:
                raise Exception('CairoSVG did not exit')
        else:
            main()

        sys.stdout.flush()
        output = output_buffer.getvalue()
        sys.stdin, sys.stdout = old_stdin, old_stdout
        eq_(output, run(*args, **kwargs))

        return output
Esempio n. 3
0
#http://cairosvg.org/user_documentation/
import cairosvg
cairosvg.main()
Esempio n. 4
0
def svg2png(svgsrc,pngfile):
    cairosvg.main()