Skip to content

Commit ee1a2e6

Browse files
committed
Remove pdfcrop from source
- `pdfcrop` causes a lot of problem, removed from source - implemented a new crop function for pdf2svg, pdftoedit is not yet supported.
1 parent 287e682 commit ee1a2e6

File tree

1 file changed

+39
-31
lines changed

1 file changed

+39
-31
lines changed

writetex.py

Lines changed: 39 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@
55
An Latex equation editor for Inkscape.
66
77
:Author: WANG Longqi <[email protected]>
8-
:Date: 2017-03-31
9-
:Version: v1.5.2
8+
:Date: 2017-08-12
9+
:Version: v1.6.0
1010
1111
This file is a part of WriteTeX extension for Inkscape. For more information,
1212
please refer to http://wanglongqi.github.io/WriteTeX.
@@ -18,7 +18,8 @@
1818
import sys
1919
import copy
2020
import subprocess
21-
from distutils import spawn
21+
import re
22+
# from distutils import spawn
2223
WriteTexNS = u'http://wanglongqi.github.io/WriteTeX'
2324
# from textext
2425
SVG_NS = u"http://www.w3.org/2000/svg"
@@ -125,7 +126,6 @@ def effect(self):
125126
out_file = os.path.join(tmp_dir, "writetex.out")
126127
err_file = os.path.join(tmp_dir, "writetex.err")
127128
aux_file = os.path.join(tmp_dir, "writetex.aux")
128-
crop_file = os.path.join(tmp_dir, "writetex-crop.pdf")
129129

130130
if self.options.preline == "true":
131131
preamble = self.options.preamble
@@ -164,28 +164,6 @@ def effect(self):
164164
subprocess.call(self.options.latexcmd.format(
165165
tmp_dir=tmp_dir, tex_file=tex_file, out_file=out_file), shell=True)
166166

167-
try:
168-
if not isinstance(spawn.find_executable('pdfcrop'),type(None)):
169-
# Here is a bug in pdfcrop, no idea how to fix.
170-
crop_cmd = 'pdfcrop "%s"' % pdf_file
171-
crop = subprocess.Popen(crop_cmd,
172-
stdout=subprocess.PIPE,
173-
stderr=subprocess.PIPE,
174-
shell=True)
175-
out = crop.communicate()
176-
if len(out[1]) > 0:
177-
inkex.errormsg("Error in pdfcrop:")
178-
inkex.errormsg(" CMD executed: %s" % crop_cmd)
179-
for msg in out:
180-
inkex.errormsg(msg)
181-
inkex.errormsg("Process will continue without crop")
182-
183-
if os.path.exists(crop_file):
184-
os.remove(pdf_file)
185-
os.rename(crop_file, pdf_file)
186-
except:
187-
pass
188-
189167
if not os.path.exists(pdf_file):
190168
print >>sys.stderr, "Latex error: check your latex file and preamble."
191169
print >>sys.stderr, open(log_file).read()
@@ -229,6 +207,9 @@ def svg_to_group(self, svgin):
229207
if tag in ['g', 'path', 'line']:
230208
child = svg_to_group(self, child)
231209
svgout.append(child)
210+
211+
# TODO: add crop range code here.
212+
232213
return svgout
233214

234215
doc = inkex.etree.parse(svg_file)
@@ -276,6 +257,9 @@ def svg_to_group(self, svgin):
276257
self.current_layer.append(newnode)
277258

278259
def merge_pdf2svg_svg(self, svg_file):
260+
# This is the smallest point coordinates assumed
261+
MAX_XY = [-10000000, -10000000]
262+
279263
def svg_to_group(self, svgin):
280264
target = {}
281265
for node in svgin.xpath('//*[@id]'):
@@ -290,6 +274,11 @@ def svg_to_group(self, svgin):
290274
node.attrib['x'], node.attrib['y'])
291275
for i in target[href].iterchildren():
292276
i.attrib['transform'] = trans
277+
x, y = self.parse_transform(trans)
278+
if x > MAX_XY[0]:
279+
MAX_XY[0] = x
280+
if y > MAX_XY[1]:
281+
MAX_XY[1] = y
293282
p.append(copy.copy(i))
294283

295284
svgout = inkex.etree.Element(inkex.addNS('g', 'WriteTexNS'))
@@ -303,6 +292,7 @@ def svg_to_group(self, svgin):
303292

304293
doc = inkex.etree.parse(svg_file)
305294
svg = doc.getroot()
295+
inkex.errormsg(simpletransform.computeBBox(svg))
306296
newnode = svg_to_group(self, svg)
307297
newnode.attrib['{%s}text' %
308298
WriteTexNS] = self.text.encode('string-escape')
@@ -330,8 +320,8 @@ def svg_to_group(self, svgin):
330320
else:
331321
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
332322
self.options.scale, self.options.scale,
333-
self.view_center[0]-self.width/6,
334-
self.view_center[1]-self.height/6)
323+
self.view_center[0]-MAX_XY[0]*self.options.scale,
324+
self.view_center[1]-MAX_XY[1]*self.options.scale)
335325
newnode.attrib['style'] = node.attrib['style']
336326
except:
337327
pass
@@ -342,9 +332,27 @@ def svg_to_group(self, svgin):
342332
self.current_layer.append(newnode)
343333
newnode.attrib['transform'] = 'matrix(%f,0,0,%f,%f,%f)' % (
344334
self.options.scale, self.options.scale,
345-
self.view_center[0]-self.width/6,
346-
self.view_center[1]-self.height/6)
347-
335+
self.view_center[0]-MAX_XY[0]*self.options.scale,
336+
self.view_center[1]-MAX_XY[1]*self.options.scale)
337+
338+
@staticmethod
339+
def parse_transform(transf):
340+
if transf == "" or transf is None:
341+
return(0, 0)
342+
stransf = transf.strip()
343+
result = re.match(
344+
"(translate|scale|rotate|skewX|skewY|matrix)\s*\(([^)]*)\)\s*,?",
345+
stransf)
346+
if result.group(1) == "translate":
347+
args = result.group(2).replace(',', ' ').split()
348+
dx = float(args[0])
349+
if len(args) == 1:
350+
dy = 0.0
351+
else:
352+
dy = float(args[1])
353+
return (dx, dy)
354+
else:
355+
return (0, 0)
348356

349357
if __name__ == '__main__':
350358
e = WriteTex()

0 commit comments

Comments
 (0)