⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 epstool.c

📁 GSview 4.6 PostScript previewer。Ghostscript在MS-Windows, OS/2 and Unix下的图形化接口
💻 C
📖 第 1 页 / 共 3 页
字号:
	/* calculate page size */
	if (calc_bbox) {
	   if (dsc->page_media && !ptsize) {
	       width = (int)(dsc->page_media->width*(long)resolution/72L);
	       height = (int)(dsc->page_media->height*(long)resolution/72L);
	   }
	   else {
	       width = (int)(((long)ptwidth)*resolution/72L);
	       height = (int)(((long)ptheight)*resolution/72L);
	   }
	}
	else {
	   width = (int)((dsc->bbox->urx - dsc->bbox->llx)*(long)resolution/72L);
	   height = (int)((dsc->bbox->ury - dsc->bbox->lly)*(long)resolution/72L);
	}
	/* cope with EPS files with and without showpage */
	fprintf(tempfile, " /EPSTOOL_save save def\r\n /showpage {} def\r\n");
	fprintf(tempfile, " count /EPSTOOL_count exch def\r\n");
	fprintf(tempfile, " /EPSTOOL_countdictstack countdictstack def\r\n");

	/* copy page to temporary file */
	psfile_extract_header(tempfile);
	if (dsc->page_count != 0) {
	    psfile_extract_page(tempfile, page);
	    if ((dsc->page_count > 1) || (dsc->page_pages > 1))
		fprintf(stderr,"Can't handle multiple page PostScript files\n");
	}
	else {
	    psfile_extract_page(tempfile, -1);
	}
	/* cope with EPS files with and without showpage */
	fprintf(tempfile, "\n count EPSTOOL_count sub {pop} repeat\r\n");
	fprintf(tempfile, " countdictstack EPSTOOL_countdictstack sub {end} repeat\r\n");
	fprintf(tempfile, " EPSTOOL_save restore\r\n showpage\r\n");
	fprintf(tempfile, "\nquit\n");
	fclose(tempfile);
#ifdef UNIX
	sprintf(gscommand, "\042%s\042 %s -dNOPAUSE -dQUIET -sDEVICE=%s -sOutputFile=\042%s\042 -r%d -g%dx%d %s",
	   gsname, gsarg, devname, bmpname, resolution, width, height, tempname);
#else
	sprintf(gscommand, "-dNOPAUSE\n-dQUIET\n-sDEVICE=%s\n-sOutputFile=\042%s\042\n-r%d\n-g%dx%d\n\042%s\042",
	   devname, bmpname, resolution, width, height, tempname);
	if (!quiet) {
	    fputs(gscommand, stderr);
	    fputs("\n", stderr);
	}
	fputs(gscommand, rspfile);
	fclose(rspfile);
#if defined(__WIN32__) || defined(__EMX__)
	sprintf(gscommand, "\042%s\042 %s @%s", gsname, gsarg, rspname);
#else
	sprintf(gscommand, "%s %s @%s", gsname, gsarg, rspname);
#endif
#endif
	if (!quiet)
	    fprintf(stderr,"%s\n", gscommand);
	system(gscommand);

	if (!debug) {
	    unlink(rspname);
	    unlink(tempname);
	}

	if (op == TIFFGS) {
	    strcpy(upname, bmpname);
	    code = make_eps_user();	/* create user TIFF preview */
	    if (!debug)
		unlink(bmpname);
	}
	else {
	    if (!load_bitmap()) {
		if (!debug)
		    unlink(bmpname);
		fprintf(stderr, "no bitmap\n");
		return 1;
	    }
	    if (!debug)
		unlink(bmpname);
	    /* now create new file with preview */
	    if (op == INTERCHANGE)
		code = make_eps_interchange(calc_bbox, oname);
	    else if (op == TIFF4)
		code = make_eps_tiff(IDM_MAKEEPST4, calc_bbox, oname);
	    else if (op == TIFF6U)
		code = make_eps_tiff(IDM_MAKEEPST6U, calc_bbox, oname);
	    else if (op == TIFF6P)
		code = make_eps_tiff(IDM_MAKEEPST6P, calc_bbox, oname);
	    else if (op == WMF)
		code = make_eps_metafile(calc_bbox, oname);
	    else if (op == COPY)
		code = make_eps_copy();
	    else
		fprintf(stderr, "Unknown operation %d\n", op);
	}

	if (*ename) {
	    gfile_close(psfile.file);
	    psfile.file = NULL;
	    if (!debug)
	        unlink(ename);	/* remove temporary file */
	}

	if (!quiet)
	    fprintf(stderr, "Operation %s\n", 
		code ? "failed" : "was successful");

	return code;
}

/* Convert EPS to an image, using the bounding box as the media size */
int
make_bitmap(void)
{
char ename[MAXSTR];
FILE *tempfile;
char tempname[MAXSTR];
char gscommand[MAXSTR+MAXSTR];
char rspname[MAXSTR];
FILE *rspfile;
int width, height;
int code = 0;
	memset(ename, 0, sizeof(ename));
	memset(tempname, 0, sizeof(tempname));
	memset(rspname, 0, sizeof(rspname));
	if (calc_bbox) {
	    fprintf(stderr, "Need a valid bounding box\n");
	    return 1;
	}
	if (dsc->bbox == NULL) {
	    fprintf(stderr, "Bounding Box is missing\n");
	    return 1;
	}
	if ((dsc->bbox->urx < dsc->bbox->llx) ||
	    (dsc->bbox->ury < dsc->bbox->lly)) {
	    fprintf(stderr, "Bounding Box is inconsistent\n");
	    return 1;
	}
	if ((dsc->bbox->urx == dsc->bbox->llx) ||
	    (dsc->bbox->ury == dsc->bbox->lly)) {
	    fprintf(stderr, "Bounding Box is empty");
	    return 1;
	}

        if ( (dsc->page_count > 1) || (dsc->page_pages > 1)) {
	    /* create temporary file to hold extracted page */
	    tempfile = gp_open_scratch_file(szScratch, ename, WRITEBIN);
	    if (tempfile == (FILE *)NULL) {
	        fprintf(stderr, "Couldn't open temporary file %s\n", ename);
	        return 1;
	    }
	    /* copy page to new file */
	    psfile_extract_header(tempfile);
	    psfile_extract_page(tempfile, page);
	    fclose(tempfile);
	    dsc_free(dsc);
	    dsc = NULL;
	    psfile.dsc = NULL;
	    /* scan new file */
	    strcpy(psfile.name, ename);

	    psfile.dsc = NULL;
	    dsc = NULL;
	    if ((psfile.file = gfile_open(psfile.name, gfile_modeRead))
		== NULL) {
		fprintf(stderr, "Can't open %s\n", psfile.name);
		return 1;
	    }
	    else {
		int code = 0;
		int count;
		char *d;
		if ( (d = (char *) malloc(COPY_BUF_SIZE)) == NULL) {
		    gfile_close(psfile.file);
		    psfile.file = NULL;
		    return 1;
		}

		dsc = dsc_init(NULL);
		if (dsc == (CDSC*)NULL) {
		    fprintf(stderr, "Failed to initalise DSC parser\n"); 
		    return 1;
		}
		dsc_set_debug_function(dsc, debug_print);
		dsc_set_error_function(dsc, show_dsc_error);
	        dsc_set_length(dsc, gfile_get_length(psfile.file));
		while ((count = gfile_read(psfile.file, d, COPY_BUF_SIZE))!=0) {
		    code = dsc_scan_data(dsc, d, count);
		    if ((code == CDSC_ERROR) || (code == CDSC_NOTDSC)) {
			/* not DSC or an error */
			gfile_close(psfile.file);
			psfile.file = NULL;
			dsc_free(dsc);
			dsc = NULL;
			fprintf(stderr, "File %s does not contain DSC comments\n", 
			    psfile.name);
			return 1 ;
		    }
		}
		gfile_close(psfile.file);
		psfile.file = NULL;
		if ((code == CDSC_ERROR) || (code == CDSC_NOTDSC)) {
		    dsc_free(dsc);
		    dsc = NULL;
		    fprintf(stderr, "File %s does not contain DSC comments\n", 
			psfile.name);
		    return 1;
		}
		dsc_fixup(dsc);
		if (debug)
		    dsc_display(dsc, debug_print);
	    }

	    if (got_dsc_error) {
		dsc_free(dsc);
		dsc = NULL;
		fprintf(stderr, "\nAborting: EPS file has fatal errors");
		return 1;
	    }

	    if (dsc == (CDSC *)NULL) {
		fprintf(stderr, "File %s does not contain DSC comments\n", 
		    psfile.name);
		return 1;
	    }

	    psfile.dsc = dsc;


	    if ((psfile.file = gfile_open(psfile.name, gfile_modeRead))
		== NULL) {
	        fprintf(stderr, "Can't open %s\n", psfile.name);
	        return 1;
	    }

	    page = 1;	/* we want the one and only remaining page */
	}

	tempfile = gp_open_scratch_file(szScratch, tempname, WRITEBIN);
	if (tempfile == (FILE *)NULL) {
	    fprintf(stderr, "Couldn't open temporary file %s\n", tempname);
	    return 1;
	}
	rspfile = gp_open_scratch_file(szScratch, rspname, WRITEBIN);
	if (rspfile == (FILE *)NULL) {
	    fprintf(stderr, "Couldn't open temporary response file %s\n", rspname);
	    if (!debug)
		unlink(tempname);
	    return 1;
	}

	/* offset to bottom left corner of bounding box */
	if (!calc_bbox)
	   fprintf(tempfile, "%d %d translate\r\n", -dsc->bbox->llx, -dsc->bbox->lly);
	/* calculate page size */
        width = (int)((dsc->bbox->urx - dsc->bbox->llx)*(long)resolution/72L);
        height = (int)((dsc->bbox->ury - dsc->bbox->lly)*(long)resolution/72L);

	/* cope with EPS files with and without showpage */
	fprintf(tempfile, " /EPSTOOL_save save def\r\n /showpage {} def\r\n");
	fprintf(tempfile, " count /EPSTOOL_count exch def\r\n");
	fprintf(tempfile, " /EPSTOOL_countdictstack countdictstack def\r\n");

	/* copy page to temporary file */
	/* Needed if a file contains a DOS EPS header */
	psfile_extract_header(tempfile);
	if (dsc->page_count != 0) {
	    psfile_extract_page(tempfile, page);
	    if ((dsc->page_count > 1) || (dsc->page_pages > 1))
		fprintf(stderr,"Can't handle multiple page PostScript files\n");
	}
	else {
	    psfile_extract_page(tempfile, -1);
	}
	/* cope with EPS files with and without showpage */
	fprintf(tempfile, "\n count EPSTOOL_count sub {pop} repeat\r\n");
	fprintf(tempfile, " countdictstack EPSTOOL_countdictstack sub {end} repeat\r\n");
	fprintf(tempfile, " EPSTOOL_save restore\r\n showpage\r\n");
	fprintf(tempfile, "\nquit\n");
	fclose(tempfile);
#ifdef UNIX
	sprintf(gscommand, "\042%s\042 %s -dNOPAUSE -dQUIET -sDEVICE=%s -sOutputFile=\042%s\042 -r%d -g%dx%d %s",
	   gsname, gsarg, devname, oname, resolution, width, height, tempname);
#else
	sprintf(gscommand, "-dNOPAUSE\n-dQUIET\n-sDEVICE=%s\n-sOutputFile=\042%s\042\n-r%d\n-g%dx%d\n\042%s\042",
	   devname, oname, resolution, width, height, tempname);
	if (!quiet) {
	    fputs(gscommand, stderr);
	    fputs("\n", stderr);
	}
	fputs(gscommand, rspfile);
	fclose(rspfile);
#if defined(__WIN32__) || defined(__EMX__)
	sprintf(gscommand, "\042%s\042 %s @%s", gsname, gsarg, rspname);
#else
	sprintf(gscommand, "%s %s @%s", gsname, gsarg, rspname);
#endif
#endif
	if (!quiet)
	    fprintf(stderr,"%s\n", gscommand);
	system(gscommand);

	if (*ename) {
	    gfile_close(psfile.file);
	    psfile.file = NULL;
	    if (!debug)
	        unlink(ename);	/* remove temporary file */
	}
	if (!debug) {
	    unlink(rspname);
	    unlink(tempname);
	}

	return code;
}


int 
scan_args(int argc, char *argv[])
{
char *argp;
int count;
	if (argc == 1) {
	    do_help();
	    return 1;
	}
	for (count=1, argp = argv[1]; count < argc; count++, argp=argv[count]) {
	  if (debug)
	    printf("argv[%d]=%s\n", count, argp);
	  if (*argp == '-') {
	    switch(argp[1]) {
		case 'h':
		  do_help();
		  return 1;
		case 'o':
		  if (argp[2])
		      strcpy(oname, argp+2);
		  else {
		      fprintf(stderr,"Missing output filename for -o\n");
		      return 1;
		  }
		  break;
		case 'n':
		  if (argp[2])
		      page = atoi(argp+2);
		  else {
		      fprintf(stderr,"Missing page number for -n\n");
		      page = atoi(argp);
		  }
		  break;
		case 'r':
		  if (argp[2])
		      resolution = atoi(argp+2);
		  else {
		      fprintf(stderr,"Missing resolution for -r\n");
		      return 1;
		  }
		  break;
		case 's':
		   if (argp[2]) {
		     if (sscanf(argp+2, "%dx%d", &ptwidth, &ptheight)
			   == 2)
			ptsize = TRUE;
		     else {
		        fprintf(stderr,"Incorrect size specified with -s\n");
		        return 1;
		     }
		   }
		   break;
		case 'b':
		  calc_bbox = !calc_bbox;
		  break;
		case 'd':
		  debug = !debug;
		  break;
		case 'q':
		  quiet = !quiet;
		  break;
		case 'e':
		  if (isdigit((int)(argp[2])))
		      ignore_dsc_warning = atoi(argp+2);
		  else
		      ignore_dsc_warning = CDSC_ERROR_WARN;
		  if (ignore_dsc_warning)
		    fprintf(stderr, "You have recklessly told epstool to ignore DSC Warnings.\nBad things might happen...\n");
		  break;
		case 'g':
		  if (argp[2])
		    strcpy(gsname, argp+2);
		  else {
		      fprintf(stderr,"Missing Ghostscript command for -g\n");
		      return 1;
		  }
		  break;
		case 'a':
		  if (argp[2])
		    strcpy(gsarg, argp+2);
		  else {
		      fprintf(stderr,"Missing Ghostscript arguments for -a\n");
		      return 1;
		  }
		  break;
		case 't':
		  if (got_op) {
		    fprintf(stderr,"Can't select two operations");
		    return 1;
		  }
		  if (argp[2] && argp[2]=='4') {
		    op = TIFF4;
		    got_op = TRUE;
		  }
		  else if (argp[2] && argp[2]=='6' && argp[3] && toupper(argp[3])=='U') {
		    op = TIFF6U;
		    got_op = TRUE;
		  }
		  else if (argp[2] && argp[2]=='6' && argp[3] && toupper(argp[3])=='P') {
		    op = TIFF6P;
		    got_op = TRUE;
		  }
		  else if (argp[2] && toupper(argp[2])=='G') {
		    op = TIFFGS;
		    got_op = TRUE;
		  }
		  else {
		      fprintf(stderr,"Missing TIFF type or device name for -t\n");
		      return 1;
		  }
		  break;
		case 'i':
		  if (got_op) {
		    fprintf(stderr,"Can't select two operations");
		    return 1;
		  }
		  op = INTERCHANGE;
		  got_op = TRUE;
		  break;
		case 'z':
		  if (argp[2])
		      strcpy(devname, argp+2);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -