andyras
andyras

Reputation: 15930

Creating a one-pixel border of whitespace in EPS

I am trying to generate an eps for inclusion in a LaTeX document with a border of 1 pixel of whitespace around the contents of the figure. The figure is a plot I am producing with gnuplot using the postscript terminal:

set terminal postscript enhanced eps color colortext 14 size 19cm,15cm font 'Courier-Bold,30'

This figure has lots of whitespace, and I'd like to cut it down to 1 pixel. I can use the epstool utility to crop it down to a zero-whitespace border:

epstool --bbox --copy input.eps output.eps

I can't find a way to add 1 pixel of whitespace without editing the .eps file manually to change the boundingbox. What would be nice would be an option to a utility like -l (--loose) to ps2eps which does exactly what I want.

Upvotes: 1

Views: 447

Answers (2)

rici
rici

Reputation: 241891

(Added at last minute: just saw your answer, so you probably don't need this)

It's easy to do in awk:

awk '/^%%(HiRes)?BoundingBox:/{print $1, $2-1, $3-1, $4+2, $5+2;next}{print}'

Upvotes: 2

andyras
andyras

Reputation: 15930

I ended up writing a python function to do the bounding box expansion:

def expand_boundingbox(epsfile, outfile):
    with open(epsfile, 'r') as f:
        with open(outfile, 'w') as o:
            lines = f.readlines()
            for line in lines:
                line = line.split()
                if line[0] == '%%BoundingBox:':
                    line[1] = str(int(line[1]) - 1)
                    line[2] = str(int(line[2]) - 1)
                    line[3] = str(int(line[3]) + 2)
                    line[4] = str(int(line[4]) + 2)
                if line[0] == '%%HiResBoundingBox:':
                    line[1] = str(float(line[1]) - 1.0)
                    line[2] = str(float(line[2]) - 1.0)
                    line[3] = str(float(line[3]) + 2.0)
                    line[4] = str(float(line[4]) + 2.0)
                line = ' '.join(line)
                o.write(line+'\n')

Upvotes: 0

Related Questions