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,
|
outform,
|
||||||
log = log,
|
log = log,
|
||||||
cachedir = dir_cache,
|
cachedir = dir_cache,
|
||||||
|
options = { "svg_embed_png": options.get("svg_embed_png",False) },
|
||||||
)
|
)
|
||||||
|
|
||||||
if pic is None:
|
if pic is None:
|
||||||
|
@ -247,6 +248,12 @@ def main():
|
||||||
default = [],
|
default = [],
|
||||||
help = "Template-specific options",
|
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(
|
top8_parser.add_argument(
|
||||||
"--lkrz-file", "-f",
|
"--lkrz-file", "-f",
|
||||||
|
@ -463,6 +470,7 @@ def main():
|
||||||
args.outfile,
|
args.outfile,
|
||||||
log = log,
|
log = log,
|
||||||
cachedir = args.cachedir,
|
cachedir = args.cachedir,
|
||||||
|
options={"svg_embed_png": "svg_embed_png" in args.export_options},
|
||||||
)
|
)
|
||||||
|
|
||||||
if rv is None:
|
if rv is None:
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
|
import base64
|
||||||
import io
|
import io
|
||||||
import os
|
import os
|
||||||
import subprocess
|
import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
|
import urllib
|
||||||
|
|
||||||
import jinja2
|
import jinja2
|
||||||
|
|
||||||
|
@ -13,12 +15,14 @@ def generate_outfile(
|
||||||
outfilename,
|
outfilename,
|
||||||
log = None,
|
log = None,
|
||||||
cachedir = None,
|
cachedir = None,
|
||||||
|
options = {},
|
||||||
):
|
):
|
||||||
|
|
||||||
# Template rendering
|
# Template rendering
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
log.info("Generating SVG using '{}' template".format(templatename))
|
log.info("Generating SVG using '{}' template".format(templatename))
|
||||||
log.debug("Context : {}".format(context))
|
log.debug("Context : {}".format(context))
|
||||||
|
log.debug("Options : {}".format(options))
|
||||||
|
|
||||||
log.debug(
|
log.debug(
|
||||||
"Initializing jinja2 with template dir '{}'" \
|
"Initializing jinja2 with template dir '{}'" \
|
||||||
|
@ -43,7 +47,34 @@ def generate_outfile(
|
||||||
# To SVG
|
# To SVG
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
if outfilename.suffix == ".svg":
|
if outfilename.suffix == ".svg":
|
||||||
jj2_tpl.stream(context).dump( str(outfilename) )
|
|
||||||
|
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
|
return outfilename
|
||||||
|
|
||||||
# To PNG with inkscape
|
# To PNG with inkscape
|
||||||
|
@ -156,6 +187,7 @@ def generate_pic(
|
||||||
outform="svg",
|
outform="svg",
|
||||||
log = None,
|
log = None,
|
||||||
cachedir = None,
|
cachedir = None,
|
||||||
|
options = {},
|
||||||
):
|
):
|
||||||
|
|
||||||
if outform not in ["svg", "png"]:
|
if outform not in ["svg", "png"]:
|
||||||
|
@ -191,7 +223,36 @@ def generate_pic(
|
||||||
# To SVG
|
# To SVG
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
if outform == "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
|
# To PNG with inkscape
|
||||||
# -------------------------------------------------------------------------
|
# -------------------------------------------------------------------------
|
||||||
|
|
Loading…
Reference in New Issue