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

📄 epstool.c

📁 GSview 4.6 PostScript previewer。Ghostscript在MS-Windows, OS/2 and Unix下的图形化接口
💻 C
📖 第 1 页 / 共 3 页
字号:
/* Copyright (C) 2000-2003, Ghostgum Software Pty Ltd.  All rights reserved.
  
  This file is part of GSview.
   
  This program is distributed with NO WARRANTY OF ANY KIND.  No author
  or distributor accepts any responsibility for the consequences of using it,
  or for whether it serves any particular purpose or works at all, unless he
  or she says so in writing.  Refer to the GSview Licence (the "Licence") 
  for full details.
   
  Every copy of GSview must include a copy of the Licence, normally in a 
  plain ASCII text file named LICENCE.  The Licence grants you the right 
  to copy, modify and redistribute GSview, but only under certain conditions 
  described in the Licence.  Among other things, the Licence requires that 
  the copyright notice and this notice be preserved on all copies.
*/

/* epstool.c */
#include "epstool.h"

char szVersion[] = "2.3  2003-04-04";

char iname[MAXSTR];
char oname[MAXSTR];
char upname[MAXSTR];
char gsname[MAXSTR] = GSCOMMAND;
char gsarg[MAXSTR];
char bmpname[MAXSTR];
char devname[MAXSTR];
char szScratch[] = "ep";
char szAppName[] = "epstool";
int resolution = 72;
int page = 1;	/* default is page 1 */
BOOL calc_bbox = FALSE;
BOOL got_op = FALSE;
BOOL debug = FALSE;
BOOL quiet = FALSE;
BOOL ptsize = FALSE;
int ptwidth = 612;	/* letter width */
int ptheight = 842;	/* A4 height */
BOOL got_dsc_error = FALSE;
int ignore_dsc_warning = CDSC_ERROR_INFORM;
BOOL ignored_dsc_warning = FALSE;
int op = 0;
#define EXTRACTPS	1
#define EXTRACTPRE	2
#define INTERCHANGE	3
#define TIFF4 		4
#define TIFF6U		5
#define TIFF6P		6
#define TIFFGS		7
#define USER		8
#define WMF		9
#define COPY		10
#define BITMAP 		11

/* KLUDGE variables */
CDSC *dsc;
PSFILE psfile;
OPTION option;
char GVFAR *bitmap_base;
LPBITMAP2 bitmap_pbmi;

/* function prototypes */
BOOL load_bitmap(void);
int scan_args(int argc, char *argv[]);
void do_help(void);
int extract_section(void);
int add_preview(void);
int make_bitmap(void);
void psfile_extract_header(FILE *f);
void psfile_extract_page(FILE *f, int page);
int make_eps_copy(void);
void debug_print(void *caller_data, const char *str);
int show_dsc_error(void *caller_data, CDSC *dsc, unsigned int explanation, 
	const char *line, unsigned int line_len);
char GVFAR * read_file(char *fname);
void copy_eps_bbox_header(FILE *f);

/* in gvwgsver.cpp */
BOOL find_gs(char *gspath, int len, int minver, BOOL bDLL);

/* in dscutil.cpp */
void dsc_display(CDSC *dsc, void (*dfn)(void *ptr, const char *str));

/* KLUDGE functions */
LPBITMAP2 get_bitmap(void)
{
    return bitmap_pbmi;
}
void release_bitmap(void)
{
}
void play_sound(int i)
{
}

char *
psfile_name(PSFILE *psf)
{
    /* don't support gzipped files in epstool */
    /* so return original file name */
    return psf->name;
}

void debug_print(void *caller_data, const char *str)
{
    fputs(str, stdout);
}


int 
show_dsc_error(void *caller_data, CDSC *dsc, unsigned int explanation, 
	const char *line, unsigned int line_len)
{
    int response = CDSC_RESPONSE_CANCEL;
    int severity;
    char buf[MAXSTR];
    if (explanation > dsc->max_error)
	return CDSC_RESPONSE_OK;

    severity = dsc->severity[explanation];

    /* If debug function provided, copy messages there */
    if (dsc->debug_print_fn) {
	switch (severity) {
	    case CDSC_ERROR_INFORM:
		dsc_debug_print(dsc, "\nDSC Information");
		break;
	    case CDSC_ERROR_WARN:
		dsc_debug_print(dsc, "\nDSC Warning");
		break;
	    case CDSC_ERROR_ERROR:
		dsc_debug_print(dsc, "\nDSC Error");
	}
	dsc_debug_print(dsc, "\n");
	if (explanation <= dsc->max_error) {
	    if (line && line_len) {
		int length = min(line_len, sizeof(buf)-1);
		sprintf(buf, "At line %d:\n", dsc->line_count);
		dsc_debug_print(dsc, buf);
		strncpy(buf, line, length);
		buf[length]='\0';
		dsc_debug_print(dsc, "  ");
		dsc_debug_print(dsc, buf);
	    }
	    dsc_debug_print(dsc, dsc_message[explanation]);
	}
    }

    fputs(dsc_message[explanation], stdout);
    fprintf(stdout, "Assuming Cancel\n");

    if (severity)
	got_dsc_error = TRUE;

    if (severity && (severity <= ignore_dsc_warning)) {
	ignored_dsc_warning = TRUE;
	dsc_debug_print(dsc, "\n****************************************************************");
	dsc_debug_print(dsc, "\nYou have recklessly told epstool to ignore DSC warnings or errors.");
	dsc_debug_print(dsc, "\nA warning has been given and you have ignored it.");
	dsc_debug_print(dsc, "\nIf you distribute this so called EPS file, you name will be mud.");
	dsc_debug_print(dsc, "\n****************************************************************\n\n");
	got_dsc_error = FALSE;
    }

    return response;
}


int
main(int argc, char *argv[])
{
#ifdef __WIN32__
	find_gs(gsname, sizeof(gsname)-1, 550, FALSE);
#endif
#ifdef UNIX
	strcpy(devname, "pbmraw");
#else
	strcpy(devname, "bmpmono");
#endif
	if (scan_args(argc, argv))
	   return 1;
#if defined(__EMX__) || defined(MSDOS) || defined(__WIN32__)
	setmode(fileno(stdout), O_BINARY);
#endif

	strcpy(psfile.name, iname);

	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;
        }

	psfile.dsc = dsc;

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

	if (op==INTERCHANGE || op==TIFF4 || op==TIFF6U || op==TIFF6P || op==TIFFGS 
		|| op==WMF || op==COPY)
	    return add_preview();
	if (op==BITMAP)
	    return make_bitmap();
	if (op==USER)
	    return make_eps_user();
	if (op==EXTRACTPS || op==EXTRACTPRE)
	    return extract_section();

        dsc_free(dsc);
	return 0;
}

int
extract_section(void)
{
	if (op == EXTRACTPS)
	    extract_doseps(IDM_EXTRACTPS);
	else if (op == EXTRACTPRE)
	    extract_doseps(IDM_EXTRACTPRE);
	else
	  fprintf(stderr, "Unknown operation %d\n", op);
	return 0;
}

int
add_preview(void)
{
char ename[MAXSTR];
char tempname[MAXSTR];
FILE *tempfile;
FILE *bmpfile;
char gscommand[MAXSTR+MAXSTR];
char rspname[MAXSTR];
FILE *rspfile;
int width, height;
int code = 0;
	if (!calc_bbox) {
	    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 ((op == TIFFGS) && calc_bbox) {
	    calc_bbox = FALSE;
	    fprintf(stderr, "Can't calculate Bounding Box when using GS TIFF driver\n");
	    fprintf(stderr, "Using %%BoundingBox from EPS file\n");
	}

        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;
	}
	bmpfile = gp_open_scratch_file(szScratch, bmpname, WRITEBIN);
	if (bmpfile == (FILE *)NULL) {
	    fprintf(stderr, "Couldn't open temporary bitmap file %s\n", bmpname);
	    if (!debug) {
		unlink(tempname);
		unlink(rspname);
	    }
	    return 1;
	}
	fclose(bmpfile);
	if (!debug)
	   unlink(bmpname);
	/* offset to bottom left corner of bounding box */
	if (!calc_bbox)
	   fprintf(tempfile, "%d %d translate\r\n", -dsc->bbox->llx, -dsc->bbox->lly);

⌨️ 快捷键说明

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