From 6bc8e8c9e37ce1d9db318b9a7641b8df0d23868c Mon Sep 17 00:00:00 2001 From: Lertsenem Date: Wed, 6 Jan 2021 09:33:36 +0100 Subject: [PATCH] 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. --- lokrez/__init__.py | 8 ++++++ lokrez/export.py | 65 ++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lokrez/__init__.py b/lokrez/__init__.py index af0e5df..77caf7a 100644 --- a/lokrez/__init__.py +++ b/lokrez/__init__.py @@ -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: diff --git a/lokrez/export.py b/lokrez/export.py index b5936c9..5129954 100644 --- a/lokrez/export.py +++ b/lokrez/export.py @@ -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": - 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 # 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 # -------------------------------------------------------------------------