From 268f61c764c429144d579beb5347e4bcc01b8ee1 Mon Sep 17 00:00:00 2001 From: Benoit Seignovert Date: Thu, 16 Jan 2020 12:26:03 -0800 Subject: [PATCH 1/8] Change `OptionParser` to `ArgumentParser` --- extension/latextext.py | 79 ++++++++++++++++++++++-------------------- 1 file changed, 41 insertions(+), 38 deletions(-) diff --git a/extension/latextext.py b/extension/latextext.py index 4cc9aa5..c146660 100755 --- a/extension/latextext.py +++ b/extension/latextext.py @@ -607,26 +607,21 @@ def render(self, latex_code, preamble_file=None, package_list="", fontsize=10, s # Init for standalone or Inkscape extension run mode # commandline options shared by standalone application and inkscape extension -def add_options(parser): - parser.add_option("-o", "--outfile", dest="outfile", +def add_arguments(parser): + parser.add_argument("-o", "--outfile", dest="outfile", help="write to output file or directory", metavar="FILE") - parser.add_option("-p", "--preamble", dest="preamble", + parser.add_argument("-p", "--preamble", dest="preamble", help="latex preamble file", metavar="FILE") - parser.add_option("-k", "--packages", dest="packages", - help="comma separated list of additional latex packages to be loaded", metavar="LIST") - parser.add_option("-f", "--fontsize", dest="fontsize", type="int", + parser.add_argument("-k", "--packages", dest="packages", + help="comma separated list of additional latex packages to be loaded", + metavar="LIST") + parser.add_argument("-f", "--fontsize", dest="fontsize", type=int, help="latex base font size") - parser.add_option("-s", "--scale", dest="scale", type="float", + parser.add_argument("-s", "--scale", dest="scale", type=float, help="apply additional scaling") - parser.add_option("-d", "--depth", dest="depth", type="int", + parser.add_argument("-d", "--depth", dest="depth", type=int, help="maximum search depth for grouped text elements") - parser.add_option("-n", "--newline", dest="newline", - action="store_true", - help="insert \\\\ at every line break") - parser.add_option("-m", "--math", dest="math", - action="store_true", - help="encapsulate all text in math mode") - parser.add_option("-c", "--clean", + parser.add_argument("-c", "--clean", action="store_true", dest="clean", help="remove all renderings") @@ -636,16 +631,15 @@ def add_options(parser): class RenderLatexEffect(inkex.Effect): def __init__(self): inkex.Effect.__init__(self) - add_options(self.OptionParser) - self.OptionParser.set_conflict_handler("resolve") - self.OptionParser.add_option("-l", "--log", type='inkbool', - action="store", dest="debug", default=False, + add_arguments(self.arg_parser) + self.arg_parser.add_argument("-l", "--log", type=inkex.utils.Boolean, + dest="debug", default=False, help="show log messages in inkscape") - self.OptionParser.add_option("-n", "--newline", dest="newline", - action="store", type='inkbool', + self.arg_parser.add_argument("-n", "--newline", dest="newline", + type=inkex.utils.Boolean, help="insert \newline at every line break") - self.OptionParser.add_option("-m", "--math", type='inkbool', - action="store", dest="math", + self.arg_parser.add_argument("-m", "--math", type=inkex.utils.Boolean, + dest="math", help="encapsulate all text in math mode") def effect(self): @@ -657,39 +651,48 @@ def effect(self): # Create a standalone commandline application def main_standalone(): # parse commandline arguments - from optparse import OptionParser - parser = OptionParser(usage="usage: %prog [options] SVGfile(s)") - add_options(parser) - parser.add_option("-v", "--verbose", default=False, + import argparse + parser = argparse.ArgumentParser(conflict_handler='resolve') + add_arguments(parser) + parser.add_argument("-n", "--newline", dest="newline", + action="store_true", + help="insert \\\\ at every line break") + parser.add_argument("-m", "--math", dest="math", + action="store_true", + help="encapsulate all text in math mode") + parser.add_argument("-v", "--verbose", default=False, action="store_true", dest="verbose") - (options, args) = parser.parse_args() + parser.add_argument("svg", type=str, nargs='+', + metavar="FILE", + help="SVGfile(s)") + args = parser.parse_args() - if options.verbose is True: + if args.verbose is True: set_log_level(log_level_debug) # expand wildcards - args = [glob.glob(arg) if '*' in arg else arg for arg in args] + files = [glob.glob(arg) if '*' in arg else arg for arg in args.svg] - if len(args) < 1: + if len(files) < 1: log_error('No input file specified! Call with -h argument for usage instructions.') sys.exit(1) - elif len(args) > 2 and options.outfile and not os.path.isdir(options.outfile): + elif len(files) > 2 and args.outfile and not os.path.isdir(args.outfile): log_error('If more than one input file is specified -o/--outfile has to point to a directory.') sys.exit(1) # main loop, run the SVG processor for each input file - for infile in args: - if options.outfile: - if os.path.isdir(options.outfile): - outfile = os.path.join(options.outfile, os.path.basename(infile)) + for infile in files: + if args.outfile: + if os.path.isdir(args.outfile): + outfile = os.path.join(args.outfile, os.path.basename(infile)) else: - outfile = options.outfile + outfile = args.outfile else: outfile = infile log_info("Rendering " + infile + " -> " + outfile) - svgprocessor = SvgProcessor(infile, options) + svgprocessor = SvgProcessor(infile, args) try: result = svgprocessor.run() From 9772ab039e5b3013450aee7feecdd46e125204d1 Mon Sep 17 00:00:00 2001 From: Benoit Seignovert Date: Thu, 16 Jan 2020 12:27:01 -0800 Subject: [PATCH 2/8] Fix depreciation warnings --- extension/latextext.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/extension/latextext.py b/extension/latextext.py index c146660..aa329bf 100755 --- a/extension/latextext.py +++ b/extension/latextext.py @@ -70,7 +70,7 @@ def log_message(msg_level, *msg): print(*msg) else: for m in msg: - inkex.debug(m) + inkex.utils.debug(m) def set_log_level(l): @@ -711,6 +711,6 @@ def main_standalone(): if STANDALONE is False: # run the extension effect = RenderLatexEffect() - effect.affect() + effect.run() else: main_standalone() From 25d1543dd16071ebf589cd3ab77ea944c9fa0a38 Mon Sep 17 00:00:00 2001 From: Benoit Seignovert Date: Thu, 16 Jan 2020 12:27:30 -0800 Subject: [PATCH 3/8] Fix macOS paths --- extension/latextext.py | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/extension/latextext.py b/extension/latextext.py index aa329bf..c74f13e 100755 --- a/extension/latextext.py +++ b/extension/latextext.py @@ -13,7 +13,7 @@ from lxml import etree -MAC = "Mac OS" +MAC = "Darwin" WINDOWS = "Windows" PLATFORM = platform.system() @@ -587,8 +587,18 @@ def render(self, latex_code, preamble_file=None, package_list="", fontsize=10, s # Convert PDF to SVG if PLATFORM == WINDOWS: PDF2SVG_PATH = os.path.join(os.path.realpath(EXT_PATH), 'pdf2svg') + if PLATFORM == MAC: + if os.path.exists('/opt/local/bin/pdf2svg'): + PDF2SVG_PATH = '/opt/local/bin' + elif os.path.exists('/usr/local/bin/pdf2svg'): + PDF2SVG_PATH = '/usr/local/bin' + elif shutil.which("pdf2svg"): + PDF2SVG_PATH = os.path.dirname(shutil.which("pdf2svg")) + else: + log_error('PDF2SVG_PATH not found.') else: PDF2SVG_PATH = '' + self._exec_command([os.path.join(PDF2SVG_PATH, 'pdf2svg'), os.path.join(tmp_path, 'tmp.pdf'), os.path.join(tmp_path, 'tmp.svg'), '1']) tree = etree.parse(os.path.join(tmp_path, 'tmp.svg')) From 6d3d05dc9b88a3d2bbd33ec1e149d1f6284e18f7 Mon Sep 17 00:00:00 2001 From: Benoit Seignovert Date: Thu, 16 Jan 2020 12:28:10 -0800 Subject: [PATCH 4/8] Fix Python3 matrix multiplication --- extension/latextext.py | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/extension/latextext.py b/extension/latextext.py index c74f13e..34f90b5 100755 --- a/extension/latextext.py +++ b/extension/latextext.py @@ -16,6 +16,7 @@ MAC = "Darwin" WINDOWS = "Windows" PLATFORM = platform.system() +PY3 = int(platform.python_version()[0]) >= 3 STANDALONE = False LOG_LEVEL = 3 @@ -95,9 +96,7 @@ class SvgTransformer: # matrix multiplication helper function def _matmult(self, a, b): - zip_b = zip(*b) - # uncomment next line if python 3 : - # zip_b = list(zip_b) + zip_b = list(zip(*b)) if PY3 else zip(*b) return [[sum(ele_a * ele_b for ele_a, ele_b in zip(row_a, col_b)) for col_b in zip_b] for row_a in a] @@ -227,8 +226,8 @@ def __init__(self, infile, options): self.options = options self.svg_input = infile - self.defaults = dict2obj({"scale": 1.0, "depth": 0.0, "fontsize": 10, - "preamble": "","packages": "amsmath,amssymb","math": False, + self.defaults = dict2obj({"scale": 1.0, "depth": 0.0, "fontsize": 10, + "preamble": "","packages": "amsmath,amssymb","math": False, "newline": False}) # load from file or use existing document root @@ -619,21 +618,21 @@ def render(self, latex_code, preamble_file=None, package_list="", fontsize=10, s # commandline options shared by standalone application and inkscape extension def add_arguments(parser): parser.add_argument("-o", "--outfile", dest="outfile", - help="write to output file or directory", metavar="FILE") + help="write to output file or directory", metavar="FILE") parser.add_argument("-p", "--preamble", dest="preamble", - help="latex preamble file", metavar="FILE") + help="latex preamble file", metavar="FILE") parser.add_argument("-k", "--packages", dest="packages", help="comma separated list of additional latex packages to be loaded", metavar="LIST") parser.add_argument("-f", "--fontsize", dest="fontsize", type=int, - help="latex base font size") + help="latex base font size") parser.add_argument("-s", "--scale", dest="scale", type=float, - help="apply additional scaling") + help="apply additional scaling") parser.add_argument("-d", "--depth", dest="depth", type=int, - help="maximum search depth for grouped text elements") + help="maximum search depth for grouped text elements") parser.add_argument("-c", "--clean", - action="store_true", dest="clean", - help="remove all renderings") + action="store_true", dest="clean", + help="remove all renderings") if STANDALONE is False: @@ -671,7 +670,7 @@ def main_standalone(): action="store_true", help="encapsulate all text in math mode") parser.add_argument("-v", "--verbose", default=False, - action="store_true", dest="verbose") + action="store_true", dest="verbose") parser.add_argument("svg", type=str, nargs='+', metavar="FILE", help="SVGfile(s)") From 1f1a1e575650fba22f36baa7328066da81af4b17 Mon Sep 17 00:00:00 2001 From: Paul d'Aoust Date: Fri, 12 Jul 2024 10:11:35 -0700 Subject: [PATCH 5/8] make compatible with Inkscape 1.3 --- extension/latextext.py | 10 ++-------- extension/latextext_gtk3.inx | 1 - 2 files changed, 2 insertions(+), 9 deletions(-) diff --git a/extension/latextext.py b/extension/latextext.py index 34f90b5..794d466 100755 --- a/extension/latextext.py +++ b/extension/latextext.py @@ -79,14 +79,8 @@ def set_log_level(l): LOG_LEVEL = l -###################### -# Check if we are in the inkscape extension folder -try: - import inkex - EXT_PATH = os.path.abspath(os.path.split(inkex.__file__)[0]) - STANDALONE = False -except ImportError: - STANDALONE = True +import inkex +STANDALONE = False ###################### diff --git a/extension/latextext_gtk3.inx b/extension/latextext_gtk3.inx index 7a6763b..1bb5592 100644 --- a/extension/latextext_gtk3.inx +++ b/extension/latextext_gtk3.inx @@ -4,7 +4,6 @@ org.inkscape.render.latextext_gtk3 latextext.py latextext_gtk3.py - inkex.py all From 138964c423d6bd8aa32d7f1a2d781508f91126e7 Mon Sep 17 00:00:00 2001 From: Paul d'Aoust Date: Fri, 12 Jul 2024 10:22:32 -0700 Subject: [PATCH 6/8] get rid of deprecation warnings --- extension/latextext_gtk3.py | 23 ++++++++++++----------- 1 file changed, 12 insertions(+), 11 deletions(-) diff --git a/extension/latextext_gtk3.py b/extension/latextext_gtk3.py index dfcb158..f772343 100755 --- a/extension/latextext_gtk3.py +++ b/extension/latextext_gtk3.py @@ -53,7 +53,7 @@ def prepare_dialog(self, options): row_count = 0 box0 = Gtk.Box(spacing=6) - grid.attach(Gtk.Label("Preamble File"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="Preamble File"), 0, row_count, 1, 1) self.entryPreamble = Gtk.Entry() self.entryPreamble.set_text(options.preamble) self.entryPreamble.set_hexpand(True) @@ -65,14 +65,14 @@ def prepare_dialog(self, options): grid.attach(box0, 1, row_count, 1, 1) row_count += 1 - grid.attach(Gtk.Label("Additional Packages"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="Additional Packages"), 0, row_count, 1, 1) self.entryPackages = Gtk.Entry() self.entryPackages.set_text(options.packages) self.entryPackages.set_hexpand(True) grid.attach(self.entryPackages, 1, row_count, 1, 1) row_count += 1 - grid.attach(Gtk.Label("Document base font size"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="Document base font size"), 0, row_count, 1, 1) self.entryFontsize = Gtk.SpinButton.new_with_range(1, 32, 1) self.entryFontsize.set_value(options.fontsize) self.entryFontsize.set_hexpand(False) @@ -82,29 +82,29 @@ def prepare_dialog(self, options): self.entryScale = Gtk.SpinButton.new_with_range(0.01, 20.0, 0.05) self.entryScale.set_digits(2) self.entryScale.set_value(options.scale) - grid.attach(Gtk.Label("Scale factor"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="Scale factor"), 0, row_count, 1, 1) grid.attach(self.entryScale, 1, row_count, 1, 1) row_count += 1 self.entryDepth = Gtk.SpinButton.new_with_range(0, 100, 1) self.entryDepth.set_value(options.depth) - grid.attach(Gtk.Label("SVG/XML tree max. depth"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="SVG/XML tree max. depth"), 0, row_count, 1, 1) grid.attach(self.entryDepth, 1, row_count, 1, 1) row_count += 1 - grid.attach(Gtk.Label("Add \\\\ at every line break"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="Add \\\\ at every line break"), 0, row_count, 1, 1) self.btnNewline = Gtk.CheckButton() self.btnNewline.set_active(options.newline) grid.attach(self.btnNewline, 1, row_count, 1, 1) row_count += 1 - grid.attach(Gtk.Label("Encapsulate all text with ($..$)"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="Encapsulate all text with ($..$)"), 0, row_count, 1, 1) self.btnMath = Gtk.CheckButton() self.btnMath.set_active(options.math) grid.attach(self.btnMath, 1, row_count, 1, 1) row_count += 1 - grid.attach(Gtk.Label("Show log messages"), 0, row_count, 1, 1) + grid.attach(Gtk.Label(label="Show log messages"), 0, row_count, 1, 1) self.btnShowLog = Gtk.CheckButton() grid.attach(self.btnShowLog, 1, row_count, 1, 1) @@ -128,10 +128,11 @@ def prepare_dialog(self, options): self.set_default(btnApply) def on_select_preamble(self, widget): - dialog = Gtk.FileChooserDialog("Please choose a Latex preamble file", self, + dialog = Gtk.FileChooserDialog(self, Gtk.FileChooserAction.OPEN, (Gtk.STOCK_CANCEL, Gtk.ResponseType.CANCEL, - Gtk.STOCK_OPEN, Gtk.ResponseType.OK)) + Gtk.STOCK_OPEN, Gtk.ResponseType.OK), + label="Please choose a Latex preamble file") dialog.set_keep_above(True) dialog.set_modal(True) response = dialog.run() @@ -174,4 +175,4 @@ def effect(self): if __name__ == "__main__": effect = RenderLatexEffectGTK3() - effect.affect() + effect.run() From 54dde58e886c9f1c71406d86e0d93d9f5272588f Mon Sep 17 00:00:00 2001 From: Paul d'Aoust Date: Fri, 12 Jul 2024 10:29:05 -0700 Subject: [PATCH 7/8] make it possible to run standalone again --- extension/latextext.py | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/extension/latextext.py b/extension/latextext.py index 794d466..34f90b5 100755 --- a/extension/latextext.py +++ b/extension/latextext.py @@ -79,8 +79,14 @@ def set_log_level(l): LOG_LEVEL = l -import inkex -STANDALONE = False +###################### +# Check if we are in the inkscape extension folder +try: + import inkex + EXT_PATH = os.path.abspath(os.path.split(inkex.__file__)[0]) + STANDALONE = False +except ImportError: + STANDALONE = True ###################### From fb98a582a2687b79c8bb55adadf1d6375ba263f0 Mon Sep 17 00:00:00 2001 From: Paul d'Aoust Date: Fri, 12 Jul 2024 10:29:16 -0700 Subject: [PATCH 8/8] one more deprecation warning, this time in Python --- extension/latextext.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/extension/latextext.py b/extension/latextext.py index 34f90b5..6edb168 100755 --- a/extension/latextext.py +++ b/extension/latextext.py @@ -468,7 +468,7 @@ def run(self): if txt_empty: log_debug("Empty text element, skipping...") continue - if self.options.math and latex_string[0] is not '$': + if self.options.math and latex_string[0] != '$': latex_string = '$' + latex_string + '$' log_debug(latex_string) rendergroup = lat2svg.render(latex_string, self.options.preamble, self.options.packages, self.options.fontsize, self.options.scale)