-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcore.py
85 lines (64 loc) · 2.24 KB
/
core.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
import os
import platform
import shlex
import subprocess
from enum import Enum
from pathlib import Path
if os.name == "nt":
gs_cmd = "gswin{}c".format(platform.architecture()[0][:2])
elif os.name == "posix":
gs_cmd = "gs"
class VectorFormat(str, Enum):
SVG = ".svg"
PDF = ".pdf"
EPS = ".eps"
EMF = ".emf"
def eps2pdf(src: Path, dst: Path):
assert src.suffix == VectorFormat.EPS
assert dst.suffix == VectorFormat.PDF
cmd = "{} -dBATCH -dNOPAUSE -sDEVICE=pdfwrite -dEPSCrop -q -sOutputFile={} {}".format(gs_cmd, dst, src)
cmd = shlex.split(cmd, posix=os.name == "posix")
subprocess.run(cmd, capture_output=False)
def emf2svg(src: Path, dst: Path):
assert src.suffix == VectorFormat.EMF
assert dst.suffix == VectorFormat.SVG
cmd = "inkscape --export-filename={} {}".format(dst, src)
cmd = shlex.split(cmd, posix=os.name == "posix")
subprocess.run(cmd, capture_output=False)
def pdf2svg(src: Path, dst: Path):
assert src.suffix == VectorFormat.PDF
assert dst.suffix == VectorFormat.SVG
cmd = "pdf2svg {} {}".format(src, dst)
cmd = shlex.split(cmd, posix=os.name == "posix")
subprocess.run(cmd, capture_output=False)
def svg2any(src: Path, dst: Path):
assert src.suffix == VectorFormat.SVG
cmd = "inkscape --export-filename={} {}".format(dst, src)
cmd = shlex.split(cmd, posix=os.name == "posix")
subprocess.run(cmd, capture_output=False)
def pdf2any(src: Path, dst: Path):
svg_tmp = src.with_suffix(VectorFormat.SVG)
pdf2svg(src, svg_tmp)
svg2any(svg_tmp, dst)
def eps2any(src: Path, dst: Path):
pdf_tmp = src.with_suffix(VectorFormat.PDF)
eps2pdf(src, pdf_tmp)
pdf2any(pdf_tmp, dst)
def emf2any(src: Path, dst: Path):
svg_tmp = src.with_suffix(VectorFormat.SVG)
emf2svg(src, svg_tmp)
svg2any(svg_tmp, dst)
def any2any(src: Path, dst: Path):
match src.suffix:
case VectorFormat.SVG:
svg2any(src, dst)
case VectorFormat.PDF:
pdf2any(src, dst)
case VectorFormat.EPS:
eps2any(src, dst)
case VectorFormat.EMF:
emf2any(src, dst)
case _:
print("unsupported vector format !")
if __name__ == "__main__":
eps2any("demo.eps", "output.emf")