-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathtikz2svg.py
More file actions
198 lines (155 loc) · 4.59 KB
/
tikz2svg.py
File metadata and controls
198 lines (155 loc) · 4.59 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# import latextools
#!/usr/bin/env python
#
# author: github.com/jbenet
# license: MIT
#
# tikz2svg: convert tikz input into svg
# depends on:
# - pdflatex: comes with your tex dist
# - pdf2svg: brew install pdf2svg
import os
import sys
import hashlib
import functools
from subprocess import Popen, PIPE
import pdf2image
def deleteDir(dirPath):
deleteFiles = []
deleteDirs = []
for root, dirs, files in os.walk(dirPath):
for f in files:
deleteFiles.append(os.path.join(root, f))
for d in dirs:
deleteDirs.append(os.path.join(root, d))
for f in deleteFiles:
os.remove(f)
for d in deleteDirs:
os.rmdir(d)
os.rmdir(dirPath)
class cmds(object):
pdflatex = "pdflatex --shell-escape -file-line-error -interaction=nonstopmode --"
pdf2svg = "pdf2svg texput.pdf out.svg"
latex_doc = r"""
\documentclass[tikz]{standalone}
\usepackage{times}
\usepackage{tikz}
\usetikzlibrary{shapes}
\usetikzlibrary{mindmap,shadows,backgrounds,decorations.pathmorphing, decorations.text,positioning}
\usepackage{hyperref}
%(header)s
\begin{document}
%(tikzpicture)s
\end{document}
"""
# util to run command in a subprocess, and communicate with it.
def run(cmd, stdin=None, exit_on_error=True):
# print '>', cmd
p = Popen(cmd, shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
if stdin:
p.stdin.write(stdin)
p.stdin.close()
p.wait()
# error out if necessary
if p.returncode != 0:
print(">", cmd)
print("Error.")
if p.stdout:
print("\n" * 5 + "-" * 20, "STDOUT")
print(p.stdout.read().decode())
if p.stderr:
print("\n" * 5 + "-" * 20, "STDERR")
print(p.stderr.read().decode())
if p.stdin:
print("-" * 20, "STDIN")
print(stdin.decode())
if exit_on_error:
sys.exit(p.returncode)
return p.stdout.read()
# memoize with a file cache.
def memoize_in_file(fn):
@functools.wraps(fn)
def memoized(*args, **kwds):
i = fn.__name__ + str(*args) + str(**kwds)
i = i.encode("utf-8")
h = hashlib.sha1(i).hexdigest()
if os.path.exists(h):
with open(h) as f:
return f.read()
out = fn(*args, **kwds)
with open(h, "w") as f:
f.write(out)
return out
return memoized
# conversion functions
def tikz2tex(tikz):
if "documentclass[tikz]{standalone}" not in str(tikz):
header, picture = tikz.split("\\begin{tikzpicture}")
picture = "\\begin{tikzpicture}" + picture
return latex_doc % {"header": header, "tikzpicture": picture}
else:
print("Document already formatted")
return tikz
def tex2pdf(tex):
return run(cmds.pdflatex.split(" "), stdin=tex)
def pdf2svg():
run(cmds.pdf2svg)
with open("out.svg") as f:
return f.read()
def tikz2img(tikz):
curdir = os.getcwd()
tmp_d = chdir(tikz)
tex = tikz2tex(tikz)
tex2pdf(tex.encode("utf-8"))
image = pdf2image.convert_from_path("texput.pdf")
os.chdir(curdir)
deleteDir(tmp_d)
print(f"Removed tmp directory {tmp_d}")
image = image[0]
return image
# @memoize_in_file
def tikz2svg(tikz):
curdir = os.getcwd()
tmp_d = chdir(tikz)
tex = tikz2tex(tikz)
tex2pdf(tex.encode("utf-8"))
svg = pdf2svg()
os.chdir(curdir)
deleteDir(tmp_d)
print(f"Removed tmp directory {tmp_d}")
return svg
# move to tmp because latex litters :(
def chdir(inp):
hash_dir = hashlib.sha1(inp.encode("utf-8")).hexdigest()
tmp_d = "/tmp/%s" % hash_dir
print(f"Compiling latex in {tmp_d}")
run("mkdir -p %s" % tmp_d, exit_on_error=False)
os.chdir(tmp_d)
return tmp_d
def save_svg(svgcontent, filename):
with open(filename, "w") as f:
f.write(svgcontent)
def compile2svg(tiks_code):
curdir = os.getcwd()
tmp_d = chdir(tiks_code)
svg_content = tikz2svg(tiks_code)
os.chdir(curdir)
deleteDir(tmp_d)
print(f"Removed tmp directory {tmp_d}")
try:
os.remove("out.svg")
except OSError:
pass
outfile_name = fileinput.filename().split(".")[0] + ".svg"
save_svg(svg_content, outfile_name)
print(f"Saved in {outfile_name}")
if __name__ == "__main__":
if "-h" in sys.argv or "--help" in sys.argv:
print("Usage: %s [<file>]" % sys.argv[0])
print("Outputs svg conversion of tikz input (files or stdin).")
sys.exit(0)
import fileinput
lines = "".join([l for l in fileinput.input()])
# compile2svg(lines)
image = tikz2img(lines)
image.save("cleva_compass.png")