Commit 8d658070 authored by Romain Casati's avatar Romain Casati
Browse files

Using new API in graphviz.

parent 33ca2874
......@@ -7,6 +7,8 @@ import logging
import platform
import subprocess
import basthon
import js
import base64
......@@ -226,32 +228,32 @@ def render(engine, format, filepath, renderer=None, formatter=None, quiet=False,
svg = pipe(engine, 'svg', data, renderer, formatter, quiet)
if format == 'svg':
js.window.Basthon.download(svg, rendered)
basthon.download(svg, rendered)
else:
downloadPngFromSvg(svg, rendered, scale)
download_png_from_svg(svg, rendered, scale)
return rendered
def downloadPngFromSvg(svg, filename, scale):
def download_png_from_svg(svg, filename, scale):
""" Download PNG image converted from bytes svg (encoded). """
svgImage = js.document.createElement('img')
svgDataURL = 'data:image/svg+xml;base64,'
svgDataURL += base64.b64encode(svg).decode()
svg_image = js.document.createElement('img')
svg_data_url = 'data:image/svg+xml;base64,'
svg_data_url += base64.b64encode(svg).decode()
def img_loaded(event):
canvas = js.document.createElement('canvas')
canvas.width = svgImage.width * scale
canvas.height = svgImage.height * scale
canvas.width = svg_image.width * scale
canvas.height = svg_image.height * scale
context = canvas.getContext("2d")
context.drawImage(svgImage, 0, 0, canvas.width, canvas.height)
context.drawImage(svg_image, 0, 0, canvas.width, canvas.height)
data = canvas.toDataURL("image/png")
data = base64.b64decode(data.split(',')[-1])
js.window.Basthon.download(data, filename)
basthon.download(data, filename)
svgImage.onload = img_loaded
svgImage.src = svgDataURL
svg_image.onload = img_loaded
svg_image.src = svg_data_url
def pipe(engine, format, data, renderer=None, formatter=None, quiet=False):
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment