forked from chianjin/outline-of-mechanics
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild_figures.py
69 lines (54 loc) · 2.05 KB
/
build_figures.py
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
import glob
import os
import sys
import subprocess
import pathlib
SVG_DIR = 'draft/figure'
PDF_DIR = 'tex/figure'
def check_inkscape(inkscape, cwd=None):
try:
subprocess.run([inkscape, '-V'], cwd)
except FileNotFoundError:
return False
return True
def detect_inkscape():
inkscape = 'inkscape'
if check_inkscape(inkscape):
return inkscape
if 'win32' in sys.platform:
for system_var in ('PROGRAMFILES', 'PROGRAMFILES(X86)'):
system_program_path = os.environ.get(system_var)
if system_program_path:
inkscape = str(pathlib.Path(system_program_path) / 'Inkscape/bin/inkscape')
# print(inkscape)
if check_inkscape(inkscape):
return inkscape
raise FileExistsError('无法找到 inkscape 执行文件,请检查安装位置,或下载安装。')
def generate_batch(svg_dir, pdf_dir):
job_list = []
for svg_file in glob.glob(str(pathlib.Path(svg_dir) / '*.svg')):
pdf_file = pathlib.Path(pdf_dir) / f'{pathlib.Path(svg_file).stem}.pdf'
if pdf_file.exists():
svg_mtime = os.stat(svg_file).st_mtime
pdf_mtime = os.stat(pdf_file).st_mtime
if svg_mtime > pdf_mtime:
job_list.append((svg_file, pdf_file))
else:
job_list.append((svg_file, pdf_file))
batch = ['export-area-drawing', 'export-text-to-path', 'export-dpi=600']
for svg_file, pdf_file in job_list:
print(f'{svg_file} --> {pdf_file}')
batch.append(f'file-open:{svg_file}')
batch.append(f'export-filename:{pdf_file}')
batch.append('export-do')
batch.append('file-close')
batch.append('quit')
return '\n'.join(batch)
def svg2pdf(svg_dir, pdf_dir):
inkscape = detect_inkscape()
batch_text = generate_batch(svg_dir, pdf_dir)
proc = subprocess.Popen([inkscape, '--shell'], stdin=subprocess.PIPE)
proc.stdin.write(batch_text.encode('GBK'))
proc.stdin.flush()
if __name__ == '__main__':
svg2pdf(SVG_DIR, PDF_DIR)