add cairosvg fallback

This commit is contained in:
2021-04-20 01:09:55 +02:00
parent fec87df9c9
commit 65c37e636c
2 changed files with 15 additions and 5 deletions

View File

@@ -5,6 +5,7 @@ from subprocess import Popen, PIPE, STDOUT
from configparser import ConfigParser from configparser import ConfigParser
from re import search, IGNORECASE from re import search, IGNORECASE
from justpy import Div, I, WebPage, SetRoute, parse_html, justpy from justpy import Div, I, WebPage, SetRoute, parse_html, justpy
from cairosvg import svg2svg
APP_NAME = "ControlDeck" APP_NAME = "ControlDeck"
@@ -125,16 +126,27 @@ def config_load(conf=''):
def svg_element(image): def svg_element(image):
svg = '' svg = ''
parse = False
if path.isfile(path.expanduser(image)): if path.isfile(path.expanduser(image)):
try: try:
with open(path.expanduser(image)) as f: with open(path.expanduser(image)) as f:
svg = f.read() svg = f.read()
except Exception as e: except Exception as e:
print(f"{e}") print(f"{e}")
# 1st try direct parsing
try: # svg with custom tags, as inkscape is using, cannot be interpreted try: # svg with custom tags, as inkscape is using, cannot be interpreted
_svg = parse_html(svg) _svg = parse_html(svg)
#print(dir(tmp_svg)) # add_attribute parse = True
#print(tmp2.attributes) except Exception as e:
# 2nd try svg2svg parsing
try: # svg with custom tags, as inkscape is using, cannot be interpreted
svg = svg2svg(bytestring=svg.encode('utf-8')).decode('utf-8')
_svg = parse_html(svg)
parse = True
except Exception as e:
print(f"[Error SVG]: {e}")
_svg = None
if parse:
# set width and height to viewBox to update width and height for scaling # set width and height to viewBox to update width and height for scaling
w = _svg.width if hasattr(_svg, 'width') else "64" w = _svg.width if hasattr(_svg, 'width') else "64"
h = _svg.height if hasattr(_svg, 'height') else "64" h = _svg.height if hasattr(_svg, 'height') else "64"
@@ -142,9 +154,6 @@ def svg_element(image):
_svg.viewBox = vb _svg.viewBox = vb
_svg.width = 64 _svg.width = 64
_svg.height = 64 _svg.height = 64
except Exception as e:
print(f"[Error SVG]: {e}")
_svg = None
return _svg return _svg
class Button(Div): class Button(Div):

View File

@@ -5,6 +5,7 @@ name = ControlDeck
install_requires = install_requires =
justpy justpy
pywebview pywebview
cairosvg
py_modules = py_modules =
controldeck controldeck
controldeck_gui controldeck_gui