Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 32 additions & 3 deletions extension/latextext.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,8 @@ def __init__(self, infile, options):
self.svg_input = infile

self.defaults = dict2obj({"scale": 1.0, "depth": 0.0, "fontsize": 10,
"preamble": "","packages": "amsmath,amssymb","math": False,
"newline": False})
"preamble": "", "packages": "amsmath,amssymb", "math": False,
"newline": False, "active_layer": "all"})

# load from file or use existing document root
if isinstance(infile, str):
Expand Down Expand Up @@ -279,6 +279,10 @@ def __init__(self, infile, options):
self.options.math = self.defaults.math
if self.options.newline is None:
self.options.newline = self.defaults.newline
if self.options.active_layer is None:
self.options.active_layer = self.defaults.active_layer

self.options.layers = self.get_layers()

def add_id_prefix(self, node, prefix):
for el in node.xpath('//*[attribute::id]', namespaces=NSS):
Expand Down Expand Up @@ -399,6 +403,10 @@ def get_parameters(self, render_layer):
if self.options.math is None and math is not None:
self.options.math = math in ('True', 'true')

active_layer = render_layer.attrib.get('{%s}active_layer' % RENDLTX_NS, None)
if self.options.active_layer is None and active_layer is not None:
self.options.active_layer = active_layer

def store_parameters(self, render_layer):
if render_layer is None:
return
Expand All @@ -424,6 +432,19 @@ def store_parameters(self, render_layer):
if self.options.math is not None:
render_layer.attrib['{%s}math' % RENDLTX_NS] = str(self.options.math)

if self.options.active_layer is not None:
render_layer.attrib['{%s}active_layer' % RENDLTX_NS] = str(self.options.active_layer)

def get_layers(self):
log_debug("Collecting layers:")
layers = []
for layer in self.docroot.findall("{%s}g" % SVG_NS):
if layer.attrib['id'] != 'ltx-render-layer':
layer_id = layer.attrib['id']
layer_name = layer.attrib.get('{%s}label' % INKSCAPE_NS)
layers.append((layer_id, layer_name))
return layers

def run(self):

lat2svg = Latex2SvgRenderer()
Expand All @@ -446,7 +467,13 @@ def run(self):
else:
line_ending = '\n'

text_nodes = self.docroot.findall('.//{%s}text' % SVG_NS)
active_layer = self.options.active_layer
log_debug("Selected layer id: " + active_layer)
if self.options.active_layer is None or active_layer == "all":
text_nodes = self.docroot.findall('.//{%s}text' % SVG_NS)
else:
text_nodes = self.docroot.findall(".//svg:g[@id='" + active_layer + "']/svg:text", NSS)

log_debug(str(len(text_nodes)) + " text nodes were found.")
for txt in text_nodes:
if self.options.depth > 0 and txt.xpath('count(ancestor::*)') > self.options.depth + 1:
Expand Down Expand Up @@ -620,6 +647,8 @@ def add_options(parser):
help="apply additional scaling")
parser.add_option("-d", "--depth", dest="depth", type="int",
help="maximum search depth for grouped text elements")
parser.add_option("-l", "--layer", dest="active_layer", type="string",
help="select text on specified layer id")
parser.add_option("-n", "--newline", dest="newline",
action="store_true",
help="insert \\\\ at every line break")
Expand Down
20 changes: 20 additions & 0 deletions extension/latextext_gtk3.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,25 @@ def prepare_dialog(self, options):
grid.attach(Gtk.Label("SVG/XML tree max. depth"), 0, row_count, 1, 1)
grid.attach(self.entryDepth, 1, row_count, 1, 1)

layer_list = Gtk.ListStore(str, str)
layer_list.append(['All layers','all'])
active_index = 0
i = 0
for layer in self.options.layers:
i += 1
if self.options.active_layer == layer[0]:
active_index = i
layer_list.append([layer[1], layer[0]])

row_count += 1
grid.attach(Gtk.Label("Selected layer"), 0, row_count, 1, 1)
self.comboLayer = Gtk.ComboBox.new_with_model(layer_list)
renderer_text = Gtk.CellRendererText()
self.comboLayer.pack_start(renderer_text, True)
self.comboLayer.add_attribute(renderer_text, "text", 0)
self.comboLayer.set_active(active_index)
grid.attach(self.comboLayer, 1, row_count, 1, 1)

row_count += 1
grid.attach(Gtk.Label("Add \\\\ at every line break"), 0, row_count, 1, 1)
self.btnNewline = Gtk.CheckButton()
Expand Down Expand Up @@ -152,6 +171,7 @@ def on_btnApply_clicked(self, widget):
self.options.packages = self.entryPackages.get_text()
self.options.math = self.btnMath.get_active()
self.options.newline = self.btnNewline.get_active()
self.options.active_layer = self.comboLayer.get_model()[self.comboLayer.get_active_iter()][1]
if self.btnShowLog.get_active() is True:
set_log_level(log_level_debug)
try:
Expand Down