Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

quick fix for version 1 api change #20

Merged
merged 1 commit into from
Apr 13, 2020
Merged
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
8 changes: 4 additions & 4 deletions writetex.inx
Original file line number Diff line number Diff line change
Expand Up @@ -4,25 +4,25 @@
<id>tk.whywhow.writetex</id>
<dependency type="executable" location="extensions">inkex.py</dependency>
<param name="preamble" type="string" _gui-text="Preamble File: "></param>
<param name="read-as-line" type="boolean" _gui-text="Trade above as contents">false</param>
<param name="read-as-line" type="bool" _gui-text="Trade above as contents">false</param>
<param name="scale" type="float" precision="2" min="0.1" max="50" _gui-text="Scale Factor">1</param>
<param name="action" type="notebook">
<page name="new" _gui-text="New TeX String">
<param name="formula" type="string" _gui-text="LateX Source: "></param>
<param name="rescale" type="boolean" _gui-text="Rescale the node.">false</param>
<param name="rescale" type="bool" _gui-text="Rescale the node.">false</param>
</page>
<page name="newfile" _gui-text="New from File">
<param name="inputfile" type="string" _gui-text="LateX Source File: "></param>
</page>
<page name="viewold" _gui-text="View Original TeX">
<_param name="old" type="description">Choose this tab if you would like to obtain the original TeX string.</_param>
<param name="tosvg" type="boolean" _gui-text="Write output to Figure"></param>
<param name="tosvg" type="bool" _gui-text="Write output to Figure"></param>
</page>
<page name="help" _gui-text="Help">
<_param name="help" type="description">You need at least one LaTeX command and one PDFtoSVG command to execute this plugin correctly. Please visit http://writetex.tk for more information. If you have any suggestion, feel free to open an issue in the repository.</_param>
</page>
</param>
<param name="pdftosvg" type="enum" _gui-text="PDF to SVG converter: ">
<param name="pdftosvg" type="optiongroup" appearance="combo" _gui-text="PDF to SVG converter: ">
<_item value="1">PDF2SVG</_item>
<_item value="2">PStoEDIT</_item>
<_item value="2">PDFtoCAIRO</_item>
Expand Down
101 changes: 51 additions & 50 deletions writetex.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import copy
import subprocess
import re
from lxml import etree
# from distutils import spawn
WriteTexNS = u'http://wanglongqi.github.io/WriteTeX'
# from textext
Expand All @@ -30,43 +31,43 @@ class WriteTex(inkex.Effect):

def __init__(self):
inkex.Effect.__init__(self)
self.OptionParser.add_option("-f", "--formula",
action="store", type="string",
self.arg_parser.add_argument("-f", "--formula",
action="store", type=str,
dest="formula", default="",
help="LaTeX formula")
self.OptionParser.add_option("-p", "--preamble",
action="store", type="string",
self.arg_parser.add_argument("-p", "--preamble",
action="store", type=str,
dest="preamble", default="",
help="Preamble File")
self.OptionParser.add_option("--read-as-line",
action="store", type="string",
self.arg_parser.add_argument("--read-as-line",
action="store", type=str,
dest="preline", default="",
help="Read preamble as string")
self.OptionParser.add_option("-s", "--scale",
action="store", type="string",
self.arg_parser.add_argument("-s", "--scale",
action="store", type=str,
dest="scale", default="",
help="Scale Factor")
self.OptionParser.add_option("-i", "--inputfile",
action="store", type="string",
self.arg_parser.add_argument("-i", "--inputfile",
action="store", type=str,
dest="inputfile", default="",
help="Read From File")
self.OptionParser.add_option("-c", "--pdftosvg",
action="store", type="string",
self.arg_parser.add_argument("-c", "--pdftosvg",
action="store", type=str,
dest="pdftosvg", default="",
help="PDFtoSVG Converter")
self.OptionParser.add_option("--action", action="store",
type="string", dest="action",
self.arg_parser.add_argument("--action", action="store",
type=str, dest="action",
default=None, help="")
self.OptionParser.add_option("-r", "--rescale",
action="store", type="string",
self.arg_parser.add_argument("-r", "--rescale",
action="store", type=str,
dest="rescale", default="",
help="Rescale the object")
self.OptionParser.add_option("-l", "--latexcmd",
action="store", type="string",
self.arg_parser.add_argument("-l", "--latexcmd",
action="store", type=str,
dest="latexcmd", default="xelatex",
help="Latex command used to compile")
self.OptionParser.add_option("-t", "--tosvg",
action="store", type="string",
self.arg_parser.add_argument("-t", "--tosvg",
action="store", type=str,
dest="tosvg", default="false",
help="Write output directly to a new node in svg file")

Expand All @@ -75,23 +76,23 @@ def effect(self):
action = self.options.action.strip("\"")
if action == "viewold":
for i in self.options.ids:
node = self.selected[i]
node = self.svg.selected[i]
if node.tag != '{%s}g' % SVG_NS:
continue
if '{%s}text' % WriteTexNS in node.attrib:
if self.options.tosvg == "true":
doc = inkex.etree.fromstring(
doc = etree.fromstring(
'<text x="%g" y="%g">%s</text>' % (
self.view_center[0],
self.view_center[1],
self.svg.namedview.center[0],
self.svg.namedview.center[1],
node.attrib.get(
'{%s}text' % WriteTexNS, '').decode('string-escape')))
'{%s}text' % WriteTexNS, '')))
p = node.getparent()
# p.remove(node)
p.append(doc)
else:
print(node.attrib.get(
'{%s}text' % WriteTexNS, '').decode('string-escape'), file=sys.stderr)
'{%s}text' % WriteTexNS, ''), file=sys.stderr)
return
print("No text find.", file=sys.stderr)
return
Expand Down Expand Up @@ -197,9 +198,9 @@ def svg_to_group(self, svgin):
innode = svgin.tag.rsplit('}', 1)[-1]
# replace svg with group by select specific elements
if innode == 'svg':
svgout = inkex.etree.Element(inkex.addNS('g', 'WriteTexNS'))
svgout = etree.Element(inkex.addNS('g'))
else:
svgout = inkex.etree.Element(inkex.addNS(innode, 'WriteTexNS'))
svgout = etree.Element(inkex.addNS(innode))
for att in svgin.attrib:
svgout.attrib[att] = svgin.attrib[att]

Expand All @@ -213,16 +214,16 @@ def svg_to_group(self, svgin):

return svgout

doc = inkex.etree.parse(svg_file)
doc = etree.parse(svg_file)
svg = doc.getroot()
newnode = svg_to_group(self, svg)
newnode.attrib['{%s}text' %
WriteTexNS] = self.text.encode('string-escape')
WriteTexNS] = self.text

replace = False

for i in self.options.ids:
node = self.selected[i]
node = self.svg.selected[i]
if node.tag != '{%s}g' % SVG_NS:
continue
if '{%s}text' % WriteTexNS in node.attrib:
Expand All @@ -234,16 +235,16 @@ def svg_to_group(self, svgin):
if self.options.rescale == 'true':
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
800*self.options.scale, 800*self.options.scale,
self.view_center[0],
self.view_center[1])
self.svg.namedview.center[0],
self.svg.namedview.center[1])
else:
if 'transform' in node.attrib:
newnode.attrib['transform'] = node.attrib['transform']
else:
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
800*self.options.scale, 800*self.options.scale,
self.view_center[0],
self.view_center[1])
self.svg.namedview.center[0],
self.svg.namedview.center[1])
newnode.attrib['style'] = node.attrib['style']
except:
pass
Expand All @@ -253,9 +254,9 @@ def svg_to_group(self, svgin):
else:
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
800*self.options.scale, 800*self.options.scale,
self.view_center[0],
self.view_center[1])
self.current_layer.append(newnode)
self.svg.namedview.center[0],
self.svg.namedview.center[1])
self.svg.get_current_layer().append(newnode)

def merge_pdf2svg_svg(self, svg_file):
# This is the smallest point coordinates assumed
Expand All @@ -282,7 +283,7 @@ def svg_to_group(self, svgin):
MAX_XY[1] = y
p.append(copy.copy(i))

svgout = inkex.etree.Element(inkex.addNS('g', 'WriteTexNS'))
svgout = etree.Element(inkex.addNS('g'))
for node in svgin:
if node is svgout:
continue
Expand All @@ -291,16 +292,16 @@ def svg_to_group(self, svgin):
svgout.append(node)
return svgout

doc = inkex.etree.parse(svg_file)
doc = etree.parse(svg_file)
svg = doc.getroot()
newnode = svg_to_group(self, svg)
newnode.attrib['{%s}text' %
WriteTexNS] = self.text.encode('string-escape')
WriteTexNS] = self.text

replace = False

for i in self.options.ids:
node = self.selected[i]
node = self.svg.selected[i]
if node.tag != '{%s}g' % SVG_NS:
continue
if '{%s}text' % WriteTexNS in node.attrib:
Expand All @@ -312,28 +313,28 @@ def svg_to_group(self, svgin):
if self.options.rescale == 'true':
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
self.options.scale, self.options.scale,
self.view_center[0],
self.view_center[1])
self.svg.namedview.center[0],
self.svg.namedview.center[1])
else:
if 'transform' in node.attrib:
newnode.attrib['transform'] = node.attrib['transform']
else:
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
self.options.scale, self.options.scale,
self.view_center[0]-MAX_XY[0]*self.options.scale,
self.view_center[1]-MAX_XY[1]*self.options.scale)
self.svg.namedview.center[0]-MAX_XY[0]*self.options.scale,
self.svg.namedview.center[1]-MAX_XY[1]*self.options.scale)
newnode.attrib['style'] = node.attrib['style']
except:
pass
p = node.getparent()
p.remove(node)
p.append(newnode)
else:
self.current_layer.append(newnode)
self.svg.get_current_layer().append(newnode)
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
self.options.scale, self.options.scale,
self.view_center[0]-MAX_XY[0]*self.options.scale,
self.view_center[1]-MAX_XY[1]*self.options.scale)
self.svg.namedview.center[0]-MAX_XY[0]*self.options.scale,
self.svg.namedview.center[1]-MAX_XY[1]*self.options.scale)

@staticmethod
def parse_transform(transf):
Expand All @@ -357,4 +358,4 @@ def parse_transform(transf):

if __name__ == '__main__':
e = WriteTex()
e.affect()
e.run()