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

📄 gsprint.cpp

📁 GSview 4.6 PostScript previewer。Ghostscript在MS-Windows, OS/2 and Unix下的图形化接口
💻 CPP
📖 第 1 页 / 共 2 页
字号:
/* 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 Free Public 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.
*/

// gsprint.cpp - An external printer driver for Windows Ghostscript
//
// Tells Ghostscript to output using a BMP device and write 
// to a pipe using -sOutputFile="%handle%XXXXXXXX".
// We read from the pipe and write GDI commands to the
// actual printer.


#include "gvcver.h"	// pick up BETA settings

#define STRICT
#include <windows.h>
#include <commdlg.h>
#include <winspool.h>
#include <stdio.h>
#include <stdlib.h>
#include <process.h>
#ifdef BETA
#include <time.h>
#endif
extern "C" {
#include "gvcfile.h"
BOOL find_gs(char *gspath, int len, int minver, BOOL bDLL); /* gvwgsver.cpp */
}
#include "gvwdib.h"
#include "gvwpdib.h"


HANDLE hPipeRd; 	/* We read printer output from this one */
HANDLE hPipeWr;
#ifdef NOTUSED
PROCESS_INFORMATION piProcInfo;
#endif
BOOL global_debug;

#define COPYRIGHT TEXT("Copyright (C) 2003, Ghostgum Software Pty Ltd.  All Rights Reserved.\n")
#define VERSION TEXT("2003-09-03 gsprint 1.6\n")

#define MAXSTR 256

typedef struct tagGSPRINT_OPTION {
    int colour;	// one of the following
#define MONO 0
#define GREY 1
#define COLOUR 2

    int orientation; // one of the following
#define DEFAULT 0
#define PORTRAIT 1
#define LANDSCAPE 2

    BOOL duplex;
    int copies;

    BOOL query;		// true if printer setup to be shown
    BOOL printer;	// printer specified
    char printer_name[MAXSTR];

    char args[4096];	// all arguments for gsprint and gs
    int args_end;	// character index to end of args
    char gs[MAXSTR];	// Path and filename of command line Ghostscript
    char options[2048];	// options for gs

    int from;		// 0 = first
    int to;		// 0 = last
    int even_odd;	// one of the following
#define ALL_PAGES 0
#define ODD_PAGES 1
#define EVEN_PAGES 2

    BOOL twoup;

    char document_name[MAXSTR];
    BOOL debug;
    BOOL debug_gdi;
} GSPRINT_OPTION;


#ifdef BETA
int
beta_expired(void)
{
  time_t today = time(NULL);
  struct tm *t;
  t = localtime(&today);
  if (t->tm_year+1900 < BETA_YEAR)
    return 0;
  if (t->tm_year+1900 > BETA_YEAR)
    return 1;    /* beta copy has expired */
  if (t->tm_mon+1 < BETA_MONTH)
    return 0;
  if (t->tm_mon+1 > BETA_MONTH)
    return 1;    /* beta copy has expired */
  if (t->tm_mday < BETA_DAY)
    return 0;
  return 1;    /* beta copy has expired */
}

int beta(void)
{
  if (beta_expired()) {
	fprintf(stdout, "\nThis TEST version has expired.\n");
    return 1;
  }
  return 0;
}
#endif /* BETA */

void print_id()
{
    fputs(COPYRIGHT, stdout);
	fputs(VERSION, stdout);
#ifdef BETA
	fprintf(stdout, "This a TEST version of gsprint.  It will disable on %04d-%02d-%02d\n", BETA_YEAR, BETA_MONTH, BETA_DAY);
#endif
}

void print_help()
{
    fprintf(stdout, "\
Usage:  gsprint [options] filename\n\
 -mono                  Render in monochrome as 1bit/pixel\n\
 -grey or -gray         Render in greyscale as 8bits/pixel\n\
 -colour or -color      Render in colour as 24bits/pixel\n\
 -query                 Show printer setup dialog\n\
 -printer \042name\042        Print to the specified printer\n\
 -ghostscript \042name\042    Path and filename of command line Ghostscript\n\
 -config \042name\042         Read options from this file instead of gsprint.cfg\n\
 -odd                   Print only odd pages\n\
 -even                  Print only even pages\n\
 -all                   Print all pages\n\
 -from NN               First page to print is NN\n\
 -to NN                 Last page to print is NN\n\
 -twoup                 2 pages per sheet\n\
 -portrait              Portrait orientation\n\
 -landscape             Landscape orientation\n\
 -duplex                Duplex (vertical axis)\n\
 -copies NN             Print NN copies (if supported)\n\
 \042filename\042             The PostScript/PDF file to print\n\
");
}

// add one argument to opt->args
BOOL add_arg(GSPRINT_OPTION *opt, char *str)
{
    int i;
    if (strlen(str) + 2 < sizeof(opt->args) - opt->args_end) {
	strcpy(&opt->args[opt->args_end], str);
	// remove trailing spaces
	i = opt->args_end + strlen(str) - 1;
	while (i>=0 && opt->args[i] == ' ')
	    i--;
	i++;
	opt->args[i] = '\0';
	opt->args_end = i+1;
	opt->args[opt->args_end] = '\0';	// double trailing null
	return TRUE;
    }
    fprintf(stdout, "Too many arguments\n");
    return FALSE;
}

// read options from config file
BOOL read_config(GSPRINT_OPTION *opt, FILE *f)
{
    char buf[MAXSTR];
    while (fgets(buf, sizeof(buf)-1, f)) {
	// remove trailing \n
	if (strlen(buf))
	    buf[strlen(buf)-1] = '\0';
	if (strcmp(buf, "-config") == 0) {
	    fprintf(stdout, "recursive config files not permitted\n");
	    return FALSE;
	}
	if (!add_arg(opt, buf))
	    return FALSE;
    }

    return TRUE;
}

// collect all arguments together in opt->args
BOOL collect_args(GSPRINT_OPTION *opt, int argc, char *argv[])
{
    int i;
    for (i=1; i < argc; i++) {
	if (strcmp(argv[i], "-config") == 0) {
	    if (i+1 < argc) {
		i++;
		FILE *f = fopen(argv[i], "r");
		if (f == (FILE *)NULL) {
		    fprintf(stdout, "Can't open config file \042%s\042\n", argv[i]);
		    return FALSE;
		}
		if (!read_config(opt, f)) {
		    fclose(f);
		    return FALSE;
		}
		fclose(f);
	    }
	    else {
		fprintf(stdout, "missing -config filename\n");
		return FALSE;
	    }
	}
	else {
	    if (!add_arg(opt, argv[i]))
		return FALSE;
	}
    }
    return TRUE;
}

void missing_arg(char *thisarg)
{
    fprintf(stdout, "Missing %s argument\n", thisarg);
}

char enable_twoup[] = 
" -c \042\
 /gsview_twoup_dict 8 dict def\
 gsview_twoup_dict begin\
 currentpagedevice /PageSize get dup\
 /twidth exch 0 get def\
 /theight exch 1 get def\
 /tland twidth theight gt def\
 twidth theight tland {exch} if\
 2 copy 1.414214 div gt {2 div exch div}{div}ifelse\
 /tscale exch def\
 tland\
 {\
 theight tscale div twidth sub 2 div /tx2 exch def /tx1 tx2 def\
 twidth 2 div tscale div theight sub 2 div /ty2 exch def\
 /ty1 twidth 2 div tscale div ty2 add def\
 }\
 {\
 twidth tscale div theight sub 2 div /ty1 exch def /ty2 ty1 def\
 theight 2 div tscale div twidth sub 2 div /tx1 exch def\
 /tx2 theight 2 div tscale div tx1 add def\
 }\
 ifelse\
 end\
 <<\
 /BeginPage{gsview_twoup_dict begin 90 rotate 0 twidth neg translate\
 tscale tscale scale 1 and 0 eq{tx1 ty1}{tx2 ty2}ifelse translate end}\
 /EndPage{2 eq {pop false}{1 and 0 ne}ifelse}\
 >> setpagedevice\042 -f ";

char disable_twoup[] =
" -c \042<< /BeginPage{} /EndPage{} >> setpagedevice\042 -f ";

void add_twoup(GSPRINT_OPTION *opt)
{
    char *p = opt->twoup ? enable_twoup : disable_twoup;
    if (strlen(p) + strlen(opt->options) + 3 < sizeof(opt->options))
	strcat(opt->options, p);
    else
	fprintf(stdout, "Argument too long while adding twoup\n");
}

BOOL process_args(GSPRINT_OPTION *opt)
{
    char *thisarg = opt->args;
    char *nextarg;
    while (*thisarg) {
        nextarg = thisarg + strlen(thisarg) + 1;
	if (strcmp(thisarg, "-help") == 0) {
	    print_help();
	    return FALSE;
	}
	else if (strcmp(thisarg, "-debug") == 0) {
	    opt->debug = TRUE;
	}
	else if (strcmp(thisarg, "-debug_gdi") == 0) {
	    opt->debug_gdi = TRUE;
	}
	else if (strcmp(thisarg, "-mono") == 0) {
	    opt->colour = MONO;
	}
	else if (strcmp(thisarg, "-grey") == 0) {
	    opt->colour = GREY;
	}
	else if (strcmp(thisarg, "-gray") == 0) {
	    opt->colour = GREY;
	}
	else if (strcmp(thisarg, "-colour") == 0) {
	    opt->colour = COLOUR;
	}
	else if (strcmp(thisarg, "-color") == 0) {
	    opt->colour = COLOUR;
	}
	else if (strcmp(thisarg, "-duplex") == 0) {
	    opt->duplex = TRUE;
	}
	else if (strcmp(thisarg, "-portrait") == 0) {
	    opt->orientation = PORTRAIT;
	}
	else if (strcmp(thisarg, "-landscape") == 0) {
	    opt->orientation = LANDSCAPE;
	}
	else if (strcmp(thisarg, "-copies") == 0) {
	    if (*nextarg) {
		opt->copies = atoi(nextarg);
		thisarg = nextarg;
	    }
	    else {
		missing_arg(thisarg);
		return FALSE;
	    }
	}
	else if (strcmp(thisarg, "-all") == 0) {
	    opt->even_odd = ALL_PAGES;
	    opt->from = 0;
	    opt->to = 0;
	}
	else if (strcmp(thisarg, "-odd") == 0) {
	    opt->even_odd = ODD_PAGES;
	}
	else if (strcmp(thisarg, "-even") == 0) {
	    opt->even_odd = EVEN_PAGES;
	}
	else if (strcmp(thisarg, "-from") == 0) {
	    if (*nextarg) {
		opt->from = atoi(nextarg);
		thisarg = nextarg;
	    }
	    else {
		missing_arg(thisarg);
		return FALSE;
	    }
	}
	else if (strcmp(thisarg, "-to") == 0) {
	    if (*nextarg) {
		opt->to = atoi(nextarg);
		thisarg = nextarg;
	    }
	    else {
		missing_arg(thisarg);
		return FALSE;
	    }
	}
	else if (strcmp(thisarg, "-twoup") == 0) {
	    opt->twoup = TRUE;
	    add_twoup(opt);
	}
	else if (strcmp(thisarg, "-notwoup") == 0) {
	    BOOL old_twoup = opt->twoup;
	    opt->twoup = FALSE;
	    if (old_twoup)
		add_twoup(opt);
	}
	else if (strcmp(thisarg, "-ghostscript") == 0) {
	    if (*nextarg) {
		if (strlen(thisarg) + 1 < sizeof(opt->gs)) {
		    strcpy(opt->gs, nextarg);
		    thisarg = nextarg;
		}
		else  {
		    fprintf(stdout, "Argument of -ghostscript is too long\n");
		    return FALSE;
		}
	    }
	    else {
		missing_arg(thisarg);
		return FALSE;
	    }
	}
	else if (strcmp(thisarg, "-printer") == 0) {
	    if (*nextarg) {
		if (strlen(thisarg) + 1 < sizeof(opt->printer_name)) {
		    strcpy(opt->printer_name, nextarg);
		    opt->printer = TRUE;
		    thisarg = nextarg;
		}
		else  {
		    fprintf(stdout, "Argument of -printer is too long\n");
		    return FALSE;
		}
	    }
	    else {
		missing_arg(thisarg);
		return FALSE;
	    }
	}
	else if (strcmp(thisarg, "-noprinter") == 0) {
	    opt->printer = FALSE;
	}
	else if (strcmp(thisarg, "-query") == 0) {
	    opt->query = TRUE;
	}
	else if (strcmp(thisarg, "-noquery") == 0) {
	    opt->query = FALSE;
	}
	else {
	    // Something for Ghostscript
	    if (strlen(thisarg) + 5 < 
		sizeof(opt->options) - strlen(opt->options) ) {
		strcat(opt->options, " ");
		if ((thisarg[0] != '\042') && (thisarg[1] != '-')) {
		    /* filename, not quoted */
		    strcat(opt->options,"\042");
		    strcat(opt->options, thisarg);
		    strcat(opt->options,"\042");
		}
		else 
		    strcat(opt->options, thisarg);
	    }
	    else  {
		fprintf(stdout, "Argument too long: \042%s\042\n", thisarg);
		return FALSE;
	    }
	    if ( ((thisarg[0] == '\042') && (thisarg[1] != '-'))
		    || (thisarg[0] != '-') ) {
		// probably a filename - use as the document name
		if (strlen(thisarg) < sizeof(opt->document_name)-1)
		    strcpy(opt->document_name, thisarg);
	    }
	}
	
	if (*thisarg)
	    thisarg = thisarg + strlen(thisarg) + 1;
    }
    return TRUE; 
} 

void
write_error(DWORD err)
{
LPVOID lpMessageBuffer;
    FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
	FORMAT_MESSAGE_FROM_SYSTEM,
	NULL, err,
	MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), /* user default language */
	(LPTSTR) &lpMessageBuffer, 0, NULL);
    if (lpMessageBuffer) {
	fputs((LPTSTR)lpMessageBuffer, stdout);
	LocalFree(LocalHandle(lpMessageBuffer));
	fputs("\r\n", stdout);
    }
}


/* Start child program with redirected standard input and output */
BOOL exec_prog(LPCSTR command)
{
    STARTUPINFO siStartInfo;
#ifndef NOTUSED
    PROCESS_INFORMATION piProcInfo;
#endif
    LPVOID env;

    /* Now create the child process. */

    /* Set up members of STARTUPINFO structure. */

    siStartInfo.cb = sizeof(STARTUPINFO);
    siStartInfo.lpReserved = NULL;
    siStartInfo.lpDesktop = NULL;
    siStartInfo.lpTitle = NULL;  /* use executable name as title */
    siStartInfo.dwX = siStartInfo.dwY = CW_USEDEFAULT;		/* ignored */
    siStartInfo.dwXSize = siStartInfo.dwYSize = CW_USEDEFAULT;	/* ignored */
    siStartInfo.dwXCountChars = 80;
    siStartInfo.dwYCountChars = 25;
    siStartInfo.dwFillAttribute = 0;			/* ignored */
    siStartInfo.dwFlags = 0;
    siStartInfo.wShowWindow = SW_SHOWNORMAL;		/* ignored */
    siStartInfo.cbReserved2 = 0;
    siStartInfo.lpReserved2 = NULL;
    siStartInfo.hStdInput = NULL;
    siStartInfo.hStdOutput = NULL;
    siStartInfo.hStdError = NULL;

    env = NULL;

    /* Create the child process. */

    if (!CreateProcess(NULL,
        (char *)command,  /* command line                       */
        NULL,          /* process security attributes        */
        NULL,          /* primary thread security attributes */
        TRUE,          /* handles are inherited              */
        0,             /* creation flags                     */
        env,           /* environment                        */
        NULL,          /* use parent's current directory     */
        &siStartInfo,  /* STARTUPINFO pointer                */
        &piProcInfo))  /* receives PROCESS_INFORMATION  */
	  return FALSE;

    WaitForInputIdle(piProcInfo.hProcess, 5000);
#ifndef NOTUSED
    CloseHandle(piProcInfo.hProcess);
    CloseHandle(piProcInfo.hThread);
#endif

    return TRUE;
}

// Return a devmode and devnames structure for the specified printer.
// If device == NULL, return for the default printer.
BOOL get_devmode(GSPRINT_OPTION *opt, HANDLE *hdevmode, HANDLE *hdevnames)
{
    char devicebuf[256];
    char driverbuf[512];
    char *device;
    char *driver;
    char *output;
    int length, offset;
    HANDLE hglobal;
    LPDEVNAMES lpdevnames;

    if (opt->printer)
	device = opt->printer_name;
    else {
	// no device specified - use default
	GetProfileString("windows", "device", "", devicebuf, sizeof(devicebuf));
	strtok(devicebuf, ",");
	device = devicebuf;

⌨️ 快捷键说明

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