2019-01-11 13:16:19 +01:00
|
|
|
#!/usr/bin/env python3
|
|
|
|
# Copyright (C) 2013-2014 Florian Festi
|
|
|
|
#
|
|
|
|
# This program is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
import os
|
2022-02-07 18:04:10 +01:00
|
|
|
import shutil
|
2022-12-31 15:52:55 +01:00
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
|
2022-03-27 10:40:38 +02:00
|
|
|
from boxes.drawing import SVGSurface, PSSurface, LBRN2Surface, Context
|
2016-06-27 09:10:00 +02:00
|
|
|
|
2022-12-31 15:52:55 +01:00
|
|
|
|
2016-08-17 15:07:41 +02:00
|
|
|
class Formats:
|
2019-01-22 05:40:16 +01:00
|
|
|
|
2022-02-07 18:04:10 +01:00
|
|
|
pstoedit_candidates = ["/usr/bin/pstoedit", "pstoedit", "pstoedit.exe"]
|
2023-02-24 14:34:13 +01:00
|
|
|
ps2pdf_candidates = ["/usr/bin/ps2pdf", "ps2pdf", "ps2pdf.exe"]
|
2016-06-27 09:10:00 +02:00
|
|
|
|
2022-03-27 10:40:38 +02:00
|
|
|
_BASE_FORMATS = ['svg', 'svg_Ponoko', 'ps', 'lbrn2']
|
2019-01-22 05:40:16 +01:00
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
formats = {
|
2016-08-17 15:07:41 +02:00
|
|
|
"svg": None,
|
2019-01-22 05:40:16 +01:00
|
|
|
"svg_Ponoko": None,
|
2016-08-17 15:07:41 +02:00
|
|
|
"ps": None,
|
2022-03-27 10:40:38 +02:00
|
|
|
"lbrn2": None,
|
2023-02-24 14:34:13 +01:00
|
|
|
"dxf": "{pstoedit} -flat 0.1 -f dxf:-mm {input} {output}",
|
|
|
|
"gcode": "{pstoedit} -f gcode {input} {output}",
|
|
|
|
"plt": "{pstoedit} -f plot-hpgl {input} {output}",
|
2023-03-12 13:35:04 +01:00
|
|
|
# "ai": "{pstoedit} -f ps2ai {input} {output}",
|
2023-02-24 14:34:13 +01:00
|
|
|
"pdf": "{ps2pdf} -dEPSCrop {input} {output}",
|
2016-06-27 09:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
http_headers = {
|
2016-08-17 15:07:41 +02:00
|
|
|
"svg": [('Content-type', 'image/svg+xml; charset=utf-8')],
|
2019-01-22 05:40:16 +01:00
|
|
|
"svg_Ponoko": [('Content-type', 'image/svg+xml; charset=utf-8')],
|
2016-08-17 15:07:41 +02:00
|
|
|
"ps": [('Content-type', 'application/postscript')],
|
2022-03-27 10:40:38 +02:00
|
|
|
"lbrn2": [('Content-type', 'application/lbrn2')],
|
2016-08-17 15:07:41 +02:00
|
|
|
"dxf": [('Content-type', 'image/vnd.dxf')],
|
|
|
|
"plt": [('Content-type', ' application/vnd.hp-hpgl')],
|
|
|
|
"gcode": [('Content-type', 'text/plain; charset=utf-8')],
|
2016-06-27 09:10:00 +02:00
|
|
|
|
|
|
|
# "" : [('Content-type', '')],
|
|
|
|
}
|
2016-08-17 15:07:41 +02:00
|
|
|
|
2023-01-08 19:41:02 +01:00
|
|
|
def __init__(self) -> None:
|
2022-02-07 18:04:10 +01:00
|
|
|
for cmd in self.pstoedit_candidates:
|
|
|
|
self.pstoedit = shutil.which(cmd)
|
|
|
|
if self.pstoedit:
|
|
|
|
break
|
2023-02-24 14:34:13 +01:00
|
|
|
for cmd in self.ps2pdf_candidates:
|
|
|
|
self.ps2pdf = shutil.which(cmd)
|
|
|
|
if self.ps2pdf:
|
|
|
|
break
|
2016-06-27 09:10:00 +02:00
|
|
|
|
|
|
|
def getFormats(self):
|
2022-02-07 18:04:10 +01:00
|
|
|
if self.pstoedit:
|
2016-06-27 09:10:00 +02:00
|
|
|
return sorted(self.formats.keys())
|
2022-02-07 18:04:10 +01:00
|
|
|
return self._BASE_FORMATS
|
2016-06-27 09:10:00 +02:00
|
|
|
|
|
|
|
def getSurface(self, fmt, filename):
|
2019-01-22 05:40:16 +01:00
|
|
|
if fmt in ("svg", "svg_Ponoko"):
|
2020-04-20 23:38:00 +02:00
|
|
|
surface = SVGSurface(filename)
|
2022-03-27 22:09:29 +02:00
|
|
|
elif fmt == "lbrn2":
|
|
|
|
surface = LBRN2Surface(filename)
|
2016-06-27 09:10:00 +02:00
|
|
|
else:
|
2022-03-27 22:09:29 +02:00
|
|
|
surface = PSSurface(filename)
|
2016-06-27 09:10:00 +02:00
|
|
|
|
2019-11-23 18:38:23 +01:00
|
|
|
ctx = Context(surface)
|
2016-06-27 09:10:00 +02:00
|
|
|
return surface, ctx
|
|
|
|
|
2019-07-06 11:47:41 +02:00
|
|
|
def convert(self, filename, fmt, metadata=None):
|
2016-06-27 09:10:00 +02:00
|
|
|
|
2019-01-22 05:40:16 +01:00
|
|
|
if fmt not in self._BASE_FORMATS:
|
2017-01-25 19:08:31 +01:00
|
|
|
fd, tmpfile = tempfile.mkstemp(dir=os.path.dirname(filename))
|
2023-02-24 14:34:13 +01:00
|
|
|
cmd = self.formats[fmt].format(
|
|
|
|
pstoedit=self.pstoedit,
|
|
|
|
ps2pdf=self.ps2pdf,
|
|
|
|
input=filename,
|
|
|
|
output=tmpfile).split()
|
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
err = subprocess.call(cmd)
|
2016-08-17 15:07:41 +02:00
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
if err:
|
|
|
|
# XXX show stderr output
|
2019-08-15 23:05:21 +02:00
|
|
|
try:
|
|
|
|
os.unlink(tmpfile)
|
|
|
|
except:
|
|
|
|
pass
|
2016-06-27 09:10:00 +02:00
|
|
|
raise ValueError("Conversion failed. pstoedit returned %i" % err)
|
2016-08-17 15:07:41 +02:00
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
os.rename(tmpfile, filename)
|