/* wrapeps.c - wrap an eps file for printing */ /* * Copyright (C) 1999 Roger Willcocks * rogerw@centipede.co.uk 28/05/99 * * 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 2 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, write to the Free Software Foundation, Inc., * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. */ /* * This is a minimal program to strip the PostScript portion out of an EPS * file, translate the origin according to the bounding box information, and * append a 'showpage' instruction. The resulting file should be printable. * Usage: wrapeps infile [outfile]; defaults to stdout. */ #include #include #include #ifdef _WIN32 #define BinaryMode O_BINARY #else #define BinaryMode 0 #endif #define BITE 2048 char buffer[BITE+1]; char temp[80]; long longval(unsigned char *p) { return ((((((long)p[3] << 8) | (long)p[2]) << 8) | (long)p[1]) << 8) | (long)p[0]; } int main(int argc, char *argv[]) { int infile, outfile, got; long length; int x, y, w, h; char *ptr; if (argc != 2 && argc != 3) { fprintf(stderr, "usage: wrapeps [ ]\n"); exit(4); } infile = open(argv[1], O_RDONLY|BinaryMode); outfile = 1; /* stdout */ if (argc == 3) outfile = open(argv[2], O_RDWR|O_TRUNC|O_CREAT|BinaryMode, 0666); if (infile < 0 || outfile < 0) exit(3); got = read(infile, buffer, BITE); if (got > 2 && buffer[0] == '%' && buffer[1] == '!') length = 0x7fffffffL; /* the lot */ else if (got > 30 && longval(buffer) == 0xc6d3d0c5L) { /* eps signature */ long offset = longval(buffer + 4); length = longval(buffer + 8) + offset; lseek(infile, offset, 0); got = read(infile, buffer, (length > BITE) ? BITE : length); } else exit(2); /* not an eps file */ buffer[BITE] = 0; /* sic */ if ((ptr = strstr(buffer, "%%BoundingBox: ")) == 0 || sscanf(ptr + 15, "%d %d %d %d", &x, &y, &w, &h) != 4) exit(1); /* no bounding box in 1st BITE characters */ sprintf(temp, "%%!PS-Adobe-1.0\n%d %d translate\n%%%%BeginDocument\n", -x, -y); write(outfile, temp, strlen(temp)); /* move the origin */ do { /* copy the code */ write(outfile, buffer, got); length -= got; } while ((got = read(infile, buffer, (length > BITE) ? BITE : length)) > 0); sprintf(temp, "\n%%%%EndDocument\nshowpage\n"); write(outfile, temp, strlen(temp)); return 0; } /* end */