boxespy/boxes/formats.py

120 lines
3.6 KiB
Python
Raw Normal View History

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 subprocess
import tempfile
import os
import cairo
import re
2016-06-27 09:10:00 +02:00
from boxes import svgutil
2016-08-17 15:07:41 +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)
2016-08-17 15:07:41 +02:00
if not m:
raise ValueError("%%BoundingBox in Postscript file not found")
2016-08-17 15:07:41 +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-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"
_BASE_FORMATS = ['svg', 'svg_Ponoko', 'ps']
2016-06-27 09:10:00 +02:00
formats = {
2016-08-17 15:07:41 +02:00
"svg": None,
"svg_Ponoko": None,
2016-08-17 15:07:41 +02:00
"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')],
"svg_Ponoko": [('Content-type', 'image/svg+xml; charset=utf-8')],
2016-08-17 15:07:41 +02:00
"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 self._BASE_FORMATS
2016-06-27 09:10:00 +02:00
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 in ("svg", "svg_Ponoko"):
2016-06-27 09:10:00 +02:00
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)
return surface, ctx
def convert(self, filename, fmt):
if fmt in ['svg', 'svg_Ponoko']:
2016-06-27 09:10:00 +02:00
svg = svgutil.SVGFile(filename)
svg.getEnvelope()
svg.rewriteViewPort()
else:
ps = PSFile(filename)
ps.adjustDocumentMedia()
2016-08-17 15:07:41 +02:00
if fmt not in self._BASE_FORMATS:
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)