2016-06-27 09:10:00 +02:00
|
|
|
import subprocess
|
|
|
|
import tempfile
|
|
|
|
import os
|
|
|
|
import cairo
|
2016-08-03 22:18:36 +02:00
|
|
|
import re
|
2016-06-27 09:10:00 +02:00
|
|
|
from boxes import svgutil
|
|
|
|
|
2016-08-03 22:18:36 +02:00
|
|
|
|
2016-08-17 15:07:41 +02:00
|
|
|
class PSFile:
|
2016-08-03 22:18:36 +02:00
|
|
|
def __init__(self, filename):
|
|
|
|
self.filename = filename
|
|
|
|
|
|
|
|
def adjustDocumentMedia(self):
|
|
|
|
with open(self.filename, "r+") as f:
|
|
|
|
s = f.read(1024)
|
|
|
|
m = re.search(r"%%BoundingBox: (\d+) (\d+) (\d+) (\d+)", s)
|
2016-08-17 15:07:41 +02:00
|
|
|
|
2016-08-03 22:18:36 +02:00
|
|
|
if not m:
|
|
|
|
raise ValueError("%%BoundingBox in Postscript file not found")
|
2016-08-17 15:07:41 +02:00
|
|
|
|
2016-08-03 22:18:36 +02:00
|
|
|
x1, y1, x2, y2 = m.groups()
|
|
|
|
m = re.search(r"%%DocumentMedia: \d+x\d+mm ((\d+) (\d+)) 0 \(", s)
|
|
|
|
f.seek(m.start(1))
|
2016-08-17 15:07:41 +02:00
|
|
|
media = "%i %i" % (int(x1) + int(x2), int(y1) + int(y2))
|
|
|
|
f.write(media + " " * (len(m.group(1)) - len(media)))
|
2016-08-03 22:18:36 +02:00
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
|
2016-08-17 15:07:41 +02:00
|
|
|
class Formats:
|
2016-06-27 09:10:00 +02:00
|
|
|
pstoedit = "/usr/bin/pstoedit"
|
|
|
|
|
|
|
|
formats = {
|
2016-08-17 15:07:41 +02:00
|
|
|
"svg": None,
|
|
|
|
"ps": None,
|
|
|
|
"dxf": "-flat 0.1 -f dxf:-mm".split(),
|
|
|
|
"gcode": "-f gcode".split(),
|
|
|
|
"plt": "-f plot-hpgl".split(),
|
|
|
|
"ai": "-f ps2ai".split(),
|
|
|
|
"pdf": "-f pdf".split(),
|
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')],
|
|
|
|
"ps": [('Content-type', 'application/postscript')],
|
|
|
|
"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
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
def __init__(self):
|
|
|
|
pass
|
|
|
|
|
|
|
|
def getFormats(self):
|
|
|
|
if os.path.isfile(self.pstoedit):
|
|
|
|
return sorted(self.formats.keys())
|
|
|
|
else:
|
|
|
|
return ['svg', 'ps']
|
|
|
|
|
|
|
|
def getSurface(self, fmt, filename):
|
|
|
|
|
2016-08-17 15:07:41 +02:00
|
|
|
width = height = 10000 # mm
|
2016-06-27 09:10:00 +02:00
|
|
|
|
|
|
|
if fmt == "svg":
|
|
|
|
surface = cairo.SVGSurface(filename, width, height)
|
|
|
|
mm2pt = 1.0
|
|
|
|
else:
|
|
|
|
mm2pt = 72 / 25.4
|
|
|
|
width *= mm2pt
|
2016-08-17 15:07:41 +02:00
|
|
|
height *= mm2pt # 3.543307
|
2016-06-27 09:10:00 +02:00
|
|
|
surface = cairo.PSSurface(filename, width, height)
|
|
|
|
|
|
|
|
ctx = cairo.Context(surface)
|
|
|
|
ctx.translate(0, height)
|
|
|
|
ctx.scale(mm2pt, -mm2pt)
|
|
|
|
|
|
|
|
ctx.set_source_rgb(0.0, 0.0, 0.0)
|
|
|
|
|
|
|
|
return surface, ctx
|
|
|
|
|
|
|
|
def convert(self, filename, fmt):
|
|
|
|
|
|
|
|
if fmt == 'svg':
|
|
|
|
svg = svgutil.SVGFile(filename)
|
|
|
|
svg.getEnvelope()
|
|
|
|
svg.rewriteViewPort()
|
|
|
|
else:
|
2016-08-03 22:18:36 +02:00
|
|
|
ps = PSFile(filename)
|
|
|
|
ps.adjustDocumentMedia()
|
2016-08-17 15:07:41 +02:00
|
|
|
|
2016-08-03 22:18:36 +02:00
|
|
|
if fmt not in ("svg", "ps"):
|
2017-01-25 19:08:31 +01:00
|
|
|
fd, tmpfile = tempfile.mkstemp(dir=os.path.dirname(filename))
|
2016-06-27 09:10:00 +02:00
|
|
|
cmd = [self.pstoedit] + self.formats[fmt] + [filename, tmpfile]
|
|
|
|
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
|
|
|
|
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)
|