Add new export-options and svg_embed_png option
New set of options to tweak the export, and the first one: a way to embed PNG images inside the resulting SVG file.master
parent
863411f6ca
commit
6bc8e8c9e3
|
@ -146,6 +146,7 @@ def generate_pic(
|
|||
outform,
|
||||
log = log,
|
||||
cachedir = dir_cache,
|
||||
options = { "svg_embed_png": options.get("svg_embed_png",False) },
|
||||
)
|
||||
|
||||
if pic is None:
|
||||
|
@ -247,6 +248,12 @@ def main():
|
|||
default = [],
|
||||
help = "Template-specific options",
|
||||
)
|
||||
top8_parser.add_argument(
|
||||
"--export-options", "-E",
|
||||
action = "append",
|
||||
default = [],
|
||||
help = "Export options (like svg_embed_png)",
|
||||
)
|
||||
|
||||
top8_parser.add_argument(
|
||||
"--lkrz-file", "-f",
|
||||
|
@ -463,6 +470,7 @@ def main():
|
|||
args.outfile,
|
||||
log = log,
|
||||
cachedir = args.cachedir,
|
||||
options={"svg_embed_png": "svg_embed_png" in args.export_options},
|
||||
)
|
||||
|
||||
if rv is None:
|
||||
|
|
|
@ -1,7 +1,9 @@
|
|||
import base64
|
||||
import io
|
||||
import os
|
||||
import subprocess
|
||||
import tempfile
|
||||
import urllib
|
||||
|
||||
import jinja2
|
||||
|
||||
|
@ -13,12 +15,14 @@ def generate_outfile(
|
|||
outfilename,
|
||||
log = None,
|
||||
cachedir = None,
|
||||
options = {},
|
||||
):
|
||||
|
||||
# Template rendering
|
||||
# -------------------------------------------------------------------------
|
||||
log.info("Generating SVG using '{}' template".format(templatename))
|
||||
log.debug("Context : {}".format(context))
|
||||
log.debug("Options : {}".format(options))
|
||||
|
||||
log.debug(
|
||||
"Initializing jinja2 with template dir '{}'" \
|
||||
|
@ -43,7 +47,34 @@ def generate_outfile(
|
|||
# To SVG
|
||||
# -------------------------------------------------------------------------
|
||||
if outfilename.suffix == ".svg":
|
||||
|
||||
if options.get("svg_embed_png", False):
|
||||
log.debug("embedding png images")
|
||||
|
||||
with open(outfilename, "w") as out:
|
||||
for line in jj2_tpl.render(context).splitlines():
|
||||
l = line.strip()
|
||||
|
||||
if ( l.startswith("xlink:href=\"file://")
|
||||
and l.endswith(".png\"") ):
|
||||
image_url = urllib.parse.urlparse(
|
||||
"=".join( l.split("=")[1:] )[1:-1],
|
||||
)
|
||||
log.debug("embedding image '{}'".format(image_url.path))
|
||||
with open(image_url.path, "rb") as image:
|
||||
line = "xlink:href=\"data:image/png;base64,{d}\"" \
|
||||
.format(
|
||||
d = base64 \
|
||||
.b64encode(image.read()) \
|
||||
.decode("ascii"),
|
||||
)
|
||||
|
||||
out.write(line)
|
||||
out.write("\n")
|
||||
|
||||
else:
|
||||
jj2_tpl.stream(context).dump( str(outfilename) )
|
||||
|
||||
return outfilename
|
||||
|
||||
# To PNG with inkscape
|
||||
|
@ -156,6 +187,7 @@ def generate_pic(
|
|||
outform="svg",
|
||||
log = None,
|
||||
cachedir = None,
|
||||
options = {},
|
||||
):
|
||||
|
||||
if outform not in ["svg", "png"]:
|
||||
|
@ -191,7 +223,36 @@ def generate_pic(
|
|||
# To SVG
|
||||
# -------------------------------------------------------------------------
|
||||
if outform == "svg":
|
||||
return jj2_tpl.render(context)
|
||||
|
||||
out = io.StringIO()
|
||||
|
||||
if options.get("svg_embed_png", False):
|
||||
log.debug("embedding png images")
|
||||
|
||||
for line in jj2_tpl.render(context).splitlines():
|
||||
l = line.strip()
|
||||
|
||||
if ( l.startswith("xlink:href=\"")
|
||||
and l.endswith(".png\"") ):
|
||||
image_url = urllib.parse.urlparse(
|
||||
"=".join( l.split("=")[1:] )[1:-1],
|
||||
)
|
||||
log.debug("embedding image '{}'".format(image_url.path))
|
||||
with open(image_url.path, "rb") as image:
|
||||
line = "xlink:href=\"data:image/png;base64,{d}\"" \
|
||||
.format(
|
||||
d = base64 \
|
||||
.b64encode(image.read()) \
|
||||
.decode("ascii"),
|
||||
)
|
||||
|
||||
out.write(line)
|
||||
out.write("\n")
|
||||
else:
|
||||
out.write(jj2_tpl.render(context))
|
||||
|
||||
out.seek(0)
|
||||
return out
|
||||
|
||||
# To PNG with inkscape
|
||||
# -------------------------------------------------------------------------
|
||||
|
|
Loading…
Reference in New Issue