
This was already suggested in #22. In addition to potentially being a little more efficient, this also avoids the problem of files referenced from the SVG not being found, since the SVG is now rendered while in the artwork directory, so relative paths inside the SVG are still correct. Please note that the pipe functionality of Inkscape requires a relatively new version of Inkscape, i.e. the version from Debian Buster is not sufficient (the Buster backport from Debian should be though). Unfortunately, the same does not work when using ImageMagick, since it seems like they use different delegates/libraries to render the SVG based on how it is passed, i.e. when passed as file, it got rendered with Inkscape on my machine, when passing it as blob, it seemed to be some internal library or another delegate which did not seem to support the same feature set as Inkscape, which resulted in inferior output. Therefore, a temporary file is still used for ImageMagick. However, the issue of included images that could be solved for Inkscape with these changes still persists, since at least when using the Inkscape delegate, ImageMagick seems to create a temporary symbolic link in /tmp, which prevents that the Inkscape delegate finds included images.
48 lines
1.8 KiB
Python
48 lines
1.8 KiB
Python
# vim: tabstop=4 shiftwidth=4 expandtab
|
|
import builtins
|
|
import cssutils
|
|
import logging
|
|
import os
|
|
import difflib
|
|
import sys
|
|
from lxml import etree
|
|
from xml.sax.saxutils import escape as xmlescape
|
|
|
|
cssutils.ser.prefs.lineSeparator = ' '
|
|
cssutils.log.setLevel(logging.FATAL)
|
|
|
|
class SVGTemplate:
|
|
def __init__(self, task):
|
|
self.task = task
|
|
|
|
def __enter__(self):
|
|
with builtins.open(os.path.join(self.task.workdir, self.task.infile), 'r') as fp:
|
|
self.svgstr = fp.read()
|
|
return self
|
|
|
|
def replacetext(self):
|
|
for key in self.task.parameters.keys():
|
|
self.svgstr = self.svgstr.replace(key, xmlescape(str(self.task.parameters[key])))
|
|
|
|
def transform(self, frame):
|
|
parser = etree.XMLParser(huge_tree=True)
|
|
svg = etree.fromstring(self.svgstr.encode('utf-8'), parser)
|
|
# apply the replace-pairs to the input text, by finding the specified xml-elements by their id and modify their css-parameter the correct value
|
|
for replaceinfo in frame:
|
|
(id, type, key, value) = replaceinfo
|
|
for el in svg.findall(".//*[@id='" + id.replace("'", "\\'") + "']"):
|
|
if type == 'style':
|
|
style = cssutils.parseStyle(el.attrib['style'] if 'style' in el.attrib else '')
|
|
style[key] = str(value)
|
|
el.attrib['style'] = style.cssText
|
|
elif type == 'attr':
|
|
el.attrib[key] = str(value)
|
|
elif type == 'text':
|
|
el.text = str(value)
|
|
# if '$subtitle' in task.parameters and task.parameters['$subtitle'] == '':
|
|
# child = svg.findall(".//*[@id='subtitle']")[0]
|
|
# child.getparent().remove(child)
|
|
self.svgstr = etree.tostring(svg, encoding='unicode')
|
|
|
|
def __exit__(self, exception_type, exception_value, traceback):
|
|
pass
|