Adjust media size for post script output
This commit is contained in:
parent
db38122125
commit
db2b753446
|
@ -3,8 +3,26 @@ import subprocess
|
||||||
import tempfile
|
import tempfile
|
||||||
import os
|
import os
|
||||||
import cairo
|
import cairo
|
||||||
|
import re
|
||||||
from boxes import svgutil
|
from boxes import svgutil
|
||||||
|
|
||||||
|
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)))
|
||||||
|
|
||||||
class Formats:
|
class Formats:
|
||||||
|
|
||||||
pstoedit = "/usr/bin/pstoedit"
|
pstoedit = "/usr/bin/pstoedit"
|
||||||
|
@ -65,9 +83,10 @@ class Formats:
|
||||||
svg = svgutil.SVGFile(filename)
|
svg = svgutil.SVGFile(filename)
|
||||||
svg.getEnvelope()
|
svg.getEnvelope()
|
||||||
svg.rewriteViewPort()
|
svg.rewriteViewPort()
|
||||||
elif fmt == "ps":
|
|
||||||
pass
|
|
||||||
else:
|
else:
|
||||||
|
ps = PSFile(filename)
|
||||||
|
ps.adjustDocumentMedia()
|
||||||
|
if fmt not in ("svg", "ps"):
|
||||||
fd, tmpfile = tempfile.mkstemp()
|
fd, tmpfile = tempfile.mkstemp()
|
||||||
cmd = [self.pstoedit] + self.formats[fmt] + [filename, tmpfile]
|
cmd = [self.pstoedit] + self.formats[fmt] + [filename, tmpfile]
|
||||||
err = subprocess.call(cmd)
|
err = subprocess.call(cmd)
|
||||||
|
|
Loading…
Reference in New Issue