From db2b7534464fb1b08eef2a45c5468df2916fdfd2 Mon Sep 17 00:00:00 2001 From: Florian Festi Date: Wed, 3 Aug 2016 22:18:36 +0200 Subject: [PATCH] Adjust media size for post script output --- boxes/formats.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/boxes/formats.py b/boxes/formats.py index 0990f7e..635c5ee 100644 --- a/boxes/formats.py +++ b/boxes/formats.py @@ -3,8 +3,26 @@ import subprocess import tempfile import os import cairo +import re 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: pstoedit = "/usr/bin/pstoedit" @@ -65,9 +83,10 @@ class Formats: svg = svgutil.SVGFile(filename) svg.getEnvelope() svg.rewriteViewPort() - elif fmt == "ps": - pass else: + ps = PSFile(filename) + ps.adjustDocumentMedia() + if fmt not in ("svg", "ps"): fd, tmpfile = tempfile.mkstemp() cmd = [self.pstoedit] + self.formats[fmt] + [filename, tmpfile] err = subprocess.call(cmd)