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
|
|
|
class PSFile:
|
|
|
|
|
|
|
|
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)
|
|
|
|
if not m:
|
|
|
|
raise ValueError("%%BoundingBox in Postscript file not found")
|
|
|
|
x1, y1, x2, y2 = m.groups()
|
|
|
|
m = re.search(r"%%DocumentMedia: \d+x\d+mm ((\d+) (\d+)) 0 \(", s)
|
|
|
|
f.seek(m.start(1))
|
|
|
|
media = "%i %i" % (int(x1)+int(x2), int(y1)+int(y2))
|
|
|
|
f.write(media + " " * (len(m.group(1))-len(media)))
|
|
|
|
|
2016-06-27 09:10:00 +02:00
|
|
|
class Formats:
|
|
|
|
|
|
|
|
pstoedit = "/usr/bin/pstoedit"
|
|
|
|
|
|
|
|
formats = {
|
|
|
|
"svg" : None,
|
|
|
|
"ps" : None,
|
2016-07-22 22:58:18 +02:00
|
|
|
"dxf" : "-flat 0.1 -f dxf:-mm".split(),
|
2016-06-27 09:10:00 +02:00
|
|
|
"gcode" : "-f gcode".split(),
|
2016-07-22 22:58:18 +02:00
|
|
|
"plt" : "-f plot-hpgl".split(),
|
|
|
|
"ai" : "-f ps2ai".split(),
|
|
|
|
"pdf" : "-f pdf".split(),
|
2016-06-27 09:10:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
http_headers = {
|
|
|
|
"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')],
|
|
|
|
|
|
|
|
# "" : [('Content-type', '')],
|
|
|
|
}
|
|
|
|
|
|
|
|
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-06-27 19:40:23 +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
|
|
|
|
height *= mm2pt #3.543307
|
|
|
|
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()
|
|
|
|
if fmt not in ("svg", "ps"):
|
2016-06-27 09:10:00 +02:00
|
|
|
fd, tmpfile = tempfile.mkstemp()
|
|
|
|
cmd = [self.pstoedit] + self.formats[fmt] + [filename, tmpfile]
|
|
|
|
err = subprocess.call(cmd)
|
|
|
|
if err:
|
|
|
|
# XXX show stderr output
|
|
|
|
raise ValueError("Conversion failed. pstoedit returned %i" % err)
|
|
|
|
os.rename(tmpfile, filename)
|