📄 epstool.c
字号:
break;
case 'c':
if (got_op) {
fprintf(stderr,"Can't select two operations");
return 1;
}
op = COPY;
got_op = TRUE;
break;
case 'k':
if (got_op) {
fprintf(stderr,"Can't select two operations");
return 1;
}
op = BITMAP;
got_op = TRUE;
break;
case 'w':
if (got_op) {
fprintf(stderr,"Can't select two operations");
return 1;
}
op = WMF;
got_op = TRUE;
break;
case 'u':
if (got_op) {
fprintf(stderr,"Can't select two operations");
return 1;
}
op = USER;
got_op = TRUE;
if (argp[2])
strcpy(upname, argp+2);
else {
fprintf(stderr,"Missing input filename for -u\n");
return 1;
}
break;
case 'p':
if (got_op) {
fprintf(stderr,"Can't select two operations");
return 1;
}
op = EXTRACTPS;
got_op = TRUE;
break;
case 'v':
if (got_op) {
fprintf(stderr,"Can't select two operations");
return 1;
}
op = EXTRACTPRE;
got_op = TRUE;
break;
default:
fprintf(stderr,"Unknown option %s\n", argp);
return 1;
}
}
else {
/* input filename */
if (*iname) {
fprintf(stderr,"Only one input file permitted\n");
return 1;
}
strcpy(iname, argp);
}
}
option.xdpi = option.ydpi = resolution;
if (*iname == '\0') {
fprintf(stderr, "No input file specified");
return 1;
}
if (!got_op) {
fprintf(stderr, "No operation specified");
return 1;
}
return 0;
}
void
do_help(void)
{
fprintf(stderr,"Usage: epstool [option] operation filename\n");
fprintf(stderr," Copyright (C) 1995-2002, Ghostgum Software Pty Ltd. All rights reserved.\n");
fprintf(stderr," Version: %s\n", szVersion);
fprintf(stderr," Options:\n");
fprintf(stderr," -aargs Ghostscript arguments\n");
fprintf(stderr," -b Calculate BoundingBox from image\n");
fprintf(stderr,", -e Ignore DSC warnings (dangerous)\n");
fprintf(stderr,", -e2 Ignore DSC errors (very dangerous)\n");
fprintf(stderr," -gcommand Ghostscript command\n");
fprintf(stderr," -nnumber Page number to extract\n");
fprintf(stderr," -ofilename Output filename\n");
fprintf(stderr," -q Quiet (no messages)\n");
fprintf(stderr," -rnumber Preview resolution in dpi\n");
fprintf(stderr," -sWIDTHxHEIGHT Size of page used with -b\n");
fprintf(stderr," -zdevice Ghostscript device name\n");
fprintf(stderr," Operations: (one only)\n");
fprintf(stderr," -i Add Interchange preview (EPSI)\n");
fprintf(stderr," -t4 Add TIFF4 preview (DOS EPS)\n");
fprintf(stderr," -t6u Add TIFF6 uncompressed (DOS EPS)\n");
fprintf(stderr," -t6p Add TIFF6 packbits (DOS EPS)\n");
fprintf(stderr," -tg Add GS TIFF preview (DOS EPS)\n");
fprintf(stderr," -w Add WMF preview (DOS EPS)\n");
fprintf(stderr," -ufilename Add user supplied preview (DOS EPS)\n");
fprintf(stderr," -p Extract PostScript (DOS EPS)\n");
fprintf(stderr," -v Extract Preview (DOS EPS)\n");
fprintf(stderr," -c Copy without preview (use with -b)\n");
fprintf(stderr," -k Convert to bitmap\n");
}
const char *err_msgs[] = {"", "No preview in input file", "Preview file is not TIFF or Windows Metafile", ""};
void
gserror(UINT id, const char *str, UINT icon, int sound)
{
fprintf(stderr, "%s %s\n", err_msgs[id], str ? str : "");
}
/* Create and open a scratch file with a given name prefix. */
/* Write the actual file name at fname. */
FILE *
gp_open_scratch_file(const char *prefix, char *fname, const char *mode)
{ char *temp;
int fd;
if ( (temp = getenv("TEMP")) == NULL )
#if defined(UNIX) || defined(__UNIX) || defined(__unix)
strcpy(fname, "/tmp");
#else
gs_getcwd(fname, MAXSTR);
#endif
else
strcpy(fname, temp);
/* Prevent X's in path from being converted by mktemp. */
for ( temp = fname; *temp; temp++ ) {
*temp = (char)tolower(*temp);
if (*temp == '/')
*temp = DIRSEP;
}
if ( strlen(fname) && (fname[strlen(fname)-1] != DIRSEP ) ) {
fname[strlen(fname)+1] = '\0';
fname[strlen(fname)] = DIRSEP;
}
strcat(fname, prefix);
strcat(fname, "XXXXXX");
#if defined(UNIX) || defined(OS2)
fd = mkstemp(fname);
return fdopen(fd, mode);
#else
mktemp(fname);
return fopen(fname, mode);
#endif
}
char *
gs_getcwd(char *dirname, int size)
{
#ifdef __EMX__
return _getcwd2(dirname, size);
#else
return getcwd(dirname, size);
#endif
}
void gs_addmess(const char *str)
{
fputs(str, stderr);
}
/* general purpose read file into memory */
/* should work for files > 64k under MSDOS */
/* malloc's memory to hold file contents and returns pointer to this memory */
char GVFAR *
read_file(char *fname)
{
FILE *f;
LONG length, nread, count;
char GVFAR *base;
char GVHUGE *bp;
if ( (f = fopen(fname, READBIN)) == (FILE *)NULL ) {
fprintf(stderr, "Can't open %s\n", fname);
return NULL;
}
fseek(f, 0, SEEK_END);
length = ftell(f);
fseek(f, 0, SEEK_SET);
if (length == 0) {
fprintf(stderr, "File %s is empty\n", fname);
}
#ifdef MSDOS /* I hate segmented architectures */
if ( (base = (char GVFAR *)farmalloc(length)) == (char *)NULL )
#else
if ( (base = (char GVFAR *)malloc(length)) == (char *)NULL )
#endif
{
fprintf(stderr, "Can't malloc memory to hold file %s\n", fname);
fclose(f);
return NULL;
}
bp = base;
while (length > 0) {
#ifdef MSDOS
/* get smaller of 16k, length, remaining bytes in segment */
count = min( min(16384, length), (DWORD)(65536UL-((WORD)(bp))) );
#else
count = length;
#endif
nread = fread(bp, 1, (int)count, f);
if (nread == 0) {
fprintf(stderr, "Can't read file %s\n", fname);
fclose(f);
free(base);
return NULL;
}
length -= nread;
bp += nread;
}
fclose(f);
return base;
}
BOOL
load_bitmap(void)
{
LPBITMAPFILE pbmf;
/* extract some info about bitmap */
pbmf = (LPBITMAPFILE)read_file(bmpname);
if (pbmf == NULL)
return FALSE;
switch (*(char *)(pbmf)) {
case 'B': /* BMP format */
bitmap_pbmi = (LPBITMAP2)( (char *)pbmf + BITMAPFILE_LENGTH );
break;
case 'P': /* PBMPLUS format */
bitmap_pbmi = (LPBITMAP2)(pbmf); /* a KLUDGE */
break;
default:
fprintf(stderr,"Unknown bitmap format\n");
return FALSE;
}
return TRUE;
}
/* Copy the header to file f */
/* change first line to EPSF if needed */
void
psfile_extract_header(FILE *f)
{
char text[DSC_LINE_LENGTH+1];
BOOL pages_written = FALSE;
if ((dsc->begincomments == dsc->endcomments) && dsc->dcs2) {
/* DSC 2.0, being treated as multiple pages */
return;
}
gfile_seek(psfile.file, dsc->begincomments, gfile_begin);
ps_fgets(text, DSC_LINE_LENGTH, psfile.file);
if (dsc->epsf)
fputs(text,f);
else {
switch(text[11]) {
case '1':
fputs("%!PS-Adobe-1.0 EPSF-1.0\r\n",f);
break;
case '2':
fputs("%!PS-Adobe-2.0 EPSF-2.0\r\n",f);
break;
default:
fputs("%!PS-Adobe-3.0 EPSF-3.0\r\n",f);
}
}
while (ps_copy_find(f, psfile.file, dsc->endcomments,
text, sizeof(text), "%%Pages:")) {
if (pages_written)
continue;
fprintf(f, "%%%%Pages: 1\r\n");
pages_written = TRUE;
}
}
/* Copy the selected page and trailer to file f */
void
psfile_extract_page(FILE *f, int page)
{
int i;
long position;
char line[DSC_LINE_LENGTH+1];
/* don't copy preview because we might be adding our own */
ps_copy(f, psfile.file, dsc->begindefaults, dsc->enddefaults);
ps_copy(f, psfile.file, dsc->beginprolog, dsc->endprolog);
ps_copy(f, psfile.file, dsc->beginsetup, dsc->endsetup);
/* map page number to zero based index */
if (dsc->page_count > 0) {
if (dsc->page_order == CDSC_DESCEND)
i = dsc->page_count - page;
else
i = page - 1;
gfile_seek(psfile.file, (LONG)dsc->page[i].begin, gfile_begin);
ps_copy_find(f, psfile.file, dsc->page[i].end,
line, sizeof(line), "%%Page:");
if (!dsc->dcs2)
fprintf(f, "%%%%Page: %s 1\r\n",
dsc->page[i].label);
position = gfile_get_position(psfile.file);
ps_copy(f, psfile.file, position, dsc->page[i].end);
}
gfile_seek(psfile.file,(LONG)dsc->begintrailer, gfile_begin);
while (ps_copy_find(f, psfile.file, dsc->endtrailer,
/* copy trailer, removing %%%Pages: since it is now in comments */
line, sizeof(line), "%%Pages:")) {
}
}
/* Copy the header to file f */
/* change bbox line if present, or add bbox line */
void
copy_eps_bbox_header(FILE *f)
{
char text[DSC_LINE_LENGTH+1];
BOOL bbox_written = FALSE;
long position;
CDSC *dsc = psfile.dsc;
gfile_seek(psfile.file, (LONG)dsc->begincomments, gfile_begin);
/* make sure first line is EPS */
ps_fgets(text, DSC_LINE_LENGTH, psfile.file);
if (dsc->epsf)
fputs(text,f);
else {
switch(text[11]) {
case '1':
fputs("%!PS-Adobe-1.0 EPSF-1.0\r\n",f);
break;
case '2':
fputs("%!PS-Adobe-2.0 EPSF-2.0\r\n",f);
break;
default:
fputs("%!PS-Adobe-3.0 EPSF-3.0\r\n",f);
}
}
position = gfile_get_position(psfile.file);
if (dsc->bbox) {
/* BoundingBox was in original file, replace it */
while ( ps_copy_find(f, psfile.file, dsc->endcomments,
text, sizeof(text), "%%BoundingBox:") ) {
if (bbox_written)
continue;
fprintf(f, "%%%%BoundingBox: %d %d %d %d\r\n",
bbox.llx, bbox.lly, bbox.urx, bbox.ury);
bbox_written = TRUE;
}
}
else {
/* BoundingBox was not in original file, add it */
ps_fgets(text, DSC_LINE_LENGTH, psfile.file);
fputs(text,f);
fprintf(f, "%%%%BoundingBox: %d %d %d %d\r\n",
bbox.llx, bbox.lly, bbox.urx, bbox.ury);
ps_copy(f, psfile.file, position, dsc->endcomments);
}
if (ignored_dsc_warning)
fputs("\
% The user who created this EPS file recklessly ignored a warning about\n\
% incorrect DSC comments. Blame them when things go wrong...\n", f);
}
/* copy psfile, updating %%BoundingBox */
int
make_eps_copy(void)
{
char epsname[MAXSTR];
FILE *epsfile;
PREBMAP prebmap;
PSBBOX devbbox; /* in pixel units */
unsigned char *pbitmap;
int code;
if ( (pbitmap = (unsigned char *)get_bitmap()) == (unsigned char *)NULL) {
return 1;
}
if (*pbitmap == 'P')
code = scan_pbmplus(&prebmap, pbitmap);
else
code = scan_dib(&prebmap, pbitmap);
if (code) {
release_bitmap();
return code;
}
strcpy(epsname, oname);
if (*epsname!='\0')
epsfile = fopen(epsname,"wb");
else
epsfile = stdout;
if (epsfile == (FILE *)NULL) {
release_bitmap();
return 1;
}
if (calc_bbox) {
scan_bbox(&prebmap, &devbbox);
if (!devbbox.valid) {
devbbox.urx = prebmap.width;
devbbox.ury = prebmap.height;
devbbox.llx = devbbox.lly = 0;
}
/* copy to global bbox as if obtained by PS to EPS */
bbox.llx = (int)(devbbox.llx * 72.0 / option.xdpi - 0.5);
bbox.lly = (int)(devbbox.lly * 72.0 / option.ydpi - 0.5);
bbox.urx = (int)(devbbox.urx * 72.0 / option.xdpi + 1.5);
bbox.ury = (int)(devbbox.ury * 72.0 / option.ydpi + 1.5);
bbox.valid = TRUE;
copy_eps_bbox_header(epsfile); /* adjust %%BoundingBox: comment */
ps_copy(epsfile, psfile.file, dsc->endcomments, dsc->endtrailer);
}
else {
ps_copy(epsfile, psfile.file, dsc->begincomments,
psfile.dsc->endcomments);
}
if (*epsname!='\0')
fclose(epsfile);
release_bitmap();
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -