📄 gvcprn.c
字号:
/* Copyright (C) 1993-2004, 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.
*/
/* gvcprn.c */
/* Printer module of PM and Windows GSview */
#include "gvc.h"
#ifndef _MSC_VER /* Brain damaged MSVC++ 5.0 doesn't support POSIX dirent.h */
#include <dirent.h>
#endif
#include <sys/stat.h>
void copy_setup(FILE *f, GFile *infile, int copies) ;
int upp_add_list(char *name, char *buffer, int len, int offset);
int enum_upp(char *path, char *buffer, int len, int offset);
char * uppname_to_model(char *buffer, char *name);
void psfile_epsf_print(FILE *f, int copies);
BOOL gsview_copygfile(FILE *outfile, GFile *infile);
BOOL gsview_copyfile(FILE *outfile, FILE *infile);
void add_copies(FILE *f, int copies)
{
if (copies >= 1) {
fprintf(f, "[{\n%%%%BeginFeature: *NumCopies %d\n", copies);
fprintf(f, "<< /NumCopies %d >> setpagedevice\n", copies);
fprintf(f, "%%%%EndFeature\n} stopped cleartomark\n");
}
}
struct prop_item_s *
get_properties(const char *device)
{
char *entries, *p, *q;
int i, numentry;
struct prop_item_s *proplist;
PROFILE *prf;
entries = (char *)malloc(PROFILE_SIZE);
if (entries == (char *)NULL)
return NULL;
if ( (prf = profile_open(szIniFile)) == (PROFILE *)NULL) {
free(entries);
return NULL;
}
profile_read_string(prf, device, NULL, "", entries, PROFILE_SIZE);
/*
if (strlen(entries) == 0) {
profile_close(prf);
free(entries);
return NULL;
}
*/
p = entries;
for (numentry=0; p!=(char *)NULL && strlen(p)!=0; numentry++)
p += strlen(p) + 1;
proplist = (struct prop_item_s *)malloc((numentry+1) * sizeof(struct prop_item_s));
if (proplist == (struct prop_item_s *)NULL) {
profile_close(prf);
free(entries);
return NULL;
}
p = entries;
for (i=0; i<numentry; i++) {
strcpy(proplist[i].name, p);
profile_read_string(prf, device, p, "", proplist[i].value, sizeof(proplist->value));
q = proplist[i].value;
while ((*q) && (q[strlen(q)-1]==' '))
q[strlen(q)-1] = '\0'; /* remove trailing spaces */
p += strlen(p) + 1;
}
proplist[numentry].name[0] = '\0';
proplist[numentry].value[0] = '\0';
profile_close(prf);
free(entries);
return proplist;
}
char *
get_devices(BOOL convert)
{
char *p;
PROFILE *prf;
const char *section = convert ? CONVERTSECTION: DEVSECTION ;
if ( (prf = profile_open(szIniFile)) == (PROFILE *)NULL)
return (char *)NULL;
if ( (p = (char *)malloc(PROFILE_SIZE)) == (char *)NULL) {
profile_close(prf);
return (char *)NULL;
}
profile_read_string(prf, section, NULL, "", p, PROFILE_SIZE);
if (strlen(p) == 0) {
/* [Devices] section doesn't exist. Initialise from resources */
profile_create_section(prf, section, IDR_DEVICES);
}
profile_read_string(prf, section, NULL, "", p, PROFILE_SIZE);
profile_close(prf);
return p;
}
/* get a filename and spool it for printing */
void
gsview_spool(char *fname, char *port)
{
static char filename[MAXSTR];
if (fname == (char *)NULL) {
if (!get_filename(filename, FALSE, FILTER_ALL, IDS_PRINTFILE, IDS_TOPICPRINT))
return;
}
else {
while (*fname && *fname==' ')
fname++;
strncpy(filename, fname, MAXSTR);
}
if (!gp_printfile(filename, port)) {
play_sound(SOUND_ERROR);
return;
}
}
/* save entire file */
/* added to save files when GSview used as a WWW viewer */
void
gsview_saveas()
{
FILE *f;
char output[MAXSTR];
FILE *infile;
UINT count;
char *buffer;
int filter;
output[0] = '\0';
if (psfile.name[0] == '\0') {
gserror(IDS_NOTOPEN, NULL, MB_ICONEXCLAMATION, SOUND_NOTOPEN);
return;
}
filter = psfile.ispdf ? FILTER_PDF :
( psfile.dsc && psfile.dsc->epsf ? FILTER_EPS : FILTER_PS );
nHelpTopic = IDS_TOPICOPEN;
if (!get_filename(output, TRUE, filter, 0, IDS_TOPICOPEN))
return;
if ((f = fopen(output, "wb")) == (FILE *)NULL) {
return;
}
/* create buffer for PS file copy */
buffer = (char *)malloc(COPY_BUF_SIZE);
if (buffer == (char *)NULL) {
play_sound(SOUND_ERROR);
fclose(f);
unlink(output);
return;
}
/* don't use dfreopen, since that wouldn't work for PDF files */
infile = fopen(psfile_name(&psfile), "rb");
if (infile == (FILE *)NULL) {
play_sound(SOUND_ERROR);
free(buffer);
fclose(f);
unlink(output);
return;
}
info_wait(IDS_WAITWRITE);
while ( (count = fread(buffer, 1, COPY_BUF_SIZE, infile)) != 0 ) {
fwrite(buffer, 1, count, f);
}
free(buffer);
fclose(infile);
fclose(f);
info_wait(IDS_NOWAIT);
return;
}
/* extract a range of pages for later printing */
void
gsview_extract()
{
FILE *f;
static char output[MAXSTR];
int thispage = psfile.pagenum;
CDSC *dsc = psfile.dsc;
if (psfile.name[0] == '\0') {
gserror(IDS_NOTOPEN, NULL, MB_ICONEXCLAMATION, SOUND_NOTOPEN);
return;
}
if (dsc == (CDSC *)NULL) {
gserror(IDS_NOPAGE, NULL, MB_ICONEXCLAMATION, SOUND_NONUMBER);
return;
}
if (psfile.ispdf && (dsc->page_count == 0)) {
TCHAR buf[MAXSTR];
load_string(IDS_PDFNOPAGE, buf, sizeof(buf));
message_box(buf, MB_ICONASTERISK | MB_OK);
return;
}
if (psfile.ispdf) {
TCHAR buf[MAXSTR];
load_string(IDS_USEPDFWRITE, buf, sizeof(buf));
message_box(buf, MB_ICONASTERISK | MB_OK);
return;
}
nHelpTopic = IDS_TOPICOPEN;
if (dsc->page_count != 0)
if (!get_page(&thispage, TRUE, FALSE))
return;
if (!get_filename(output, TRUE, FILTER_PS, 0, IDS_TOPICOPEN))
return;
if ((f = fopen(output, "wb")) == (FILE *)NULL)
return;
load_string(IDS_WAITWRITE, szWait, sizeof(szWait));
info_wait(IDS_WAITWRITE);
if (dfreopen() != 0) {
fclose(f);
unlink(output);
gserror(0, "Couldn't reopen document", MB_ICONEXCLAMATION, SOUND_NOTOPEN);
return;
}
if (dsc->page_count != 0)
psfile_extract(f, 1);
else {
ps_copy(f, psfile.file, dsc->begincomments, dsc->endcomments);
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);
ps_copy(f, psfile.file, dsc->begintrailer, dsc->endtrailer);
}
dfclose();
fclose(f);
info_wait(IDS_NOWAIT);
return;
}
void
copy_setup(FILE *f, GFile *infile, int copies)
{
char line[DSC_LINE_LENGTH+1];
if (copies > 1) {
if (psfile.dsc->beginsetup != psfile.dsc->endsetup) {
gfile_seek(infile, psfile.dsc->beginsetup, gfile_begin);
/* copy up to, but no including %%EndSetup */
ps_copy_find(f, infile, psfile.dsc->endsetup,
line, sizeof(line)-1, "%%EndSetup");
/* insert code for multiple copies */
add_copies(f, copies);
/* copy rest of setup section */
if (strlen(line))
fputs(line, f);
ps_copy(f, infile, -1, psfile.dsc->endsetup);
}
else {
/* setup section was missing - add our own. */
fputs("%%BeginSetup\n", f);
add_copies(f, copies);
fputs("%%EndSetup\n", f);
}
}
else
ps_copy(f, infile, psfile.dsc->beginsetup, psfile.dsc->endsetup);
}
/* Copy the headers, marked pages, and trailer to f */
/* Reverse the page order if needed and possible */
BOOL
psfile_extract(FILE *f, int copies)
{
char line[DSC_LINE_LENGTH+1];
BOOL pages_written = FALSE;
BOOL pageorder_written = FALSE;
int pages = 0;
int page;
int i;
unsigned long position;
CDSC *dsc = psfile.dsc;
int neworder = dsc->page_order;
BOOL reverse = psfile.page_list.reverse;
BOOL end_header;
BOOL line_written;
if (neworder == CDSC_ORDER_UNKNOWN) /* No page order so assume ASCEND */
neworder = CDSC_ASCEND;
/* Don't touch SPECIAL pageorder */
/* reverse means new page order to be DESCEND */
if (reverse) {
if (neworder == CDSC_ASCEND)
neworder = CDSC_DESCEND;
else if (neworder == CDSC_DESCEND) {
/* neworder = DESCEND;*/ /* unchanged */
reverse = FALSE; /* already reversed, don't do it again */
}
}
else {
if (neworder == CDSC_DESCEND) {
neworder = CDSC_ASCEND;
reverse = TRUE; /* reverse it to become ascending */
}
}
/* neworder = page order of the extracted document */
/* reverse = reverse the current page order */
for (i=0; i<(int)dsc->page_count; i++) {
if (psfile.page_list.select[i]) pages++;
}
/* copy header, fixing up %%Pages: and %%PageOrder:
* Write a DSC 3.0 %%Pages: or %%PageOrder: in header,
* even if document was DSC 2.x.
* Remove %%Pages: and %%PageOrder from trailer.
*/
position = gfile_seek(psfile.file, dsc->begincomments, gfile_begin);
while ( position < dsc->endcomments ) {
if (ps_fgets(line, sizeof(line), psfile.file) == 0)
return FALSE;
position = gfile_seek(psfile.file, 0, gfile_current);
end_header = (strncmp(line, "%%EndComments", 13) == 0);
if ((line[0] != '%') && (line[0] != ' ') && (line[0] != '+')
&& (line[0] != '\t') && (line[0] != '\r') && (line[0] != '\n'))
end_header = TRUE;
line_written = FALSE;
if (end_header || strncmp(line, "%%Pages:", 8) == 0) {
if (!pages_written) {
fprintf(f, "%%%%Pages: %d\r\n", pages);
pages_written = TRUE;
}
line_written = !end_header;
}
if (end_header || strncmp(line, "%%PageOrder:", 12) == 0) {
if (!pageorder_written) {
if (neworder == CDSC_ASCEND)
fputs("%%PageOrder: Ascend\r\n", f);
else if (neworder == CDSC_DESCEND)
fputs("%%PageOrder: Descend\r\n", f);
else
fputs("%%PageOrder: Special\r\n", f);
pageorder_written = TRUE;
}
line_written = !end_header;
}
if (!line_written) {
fputs(line, f);
}
}
if (dsc->begincomments != dsc->endcomments) {
if (!pages_written) {
fprintf(f, "%%%%Pages: %d\r\n", pages);
/* pages_written = TRUE; */
}
if (!pageorder_written) {
if (neworder == CDSC_ASCEND)
fputs("%%PageOrder: Ascend\r\n", f);
else if (neworder == CDSC_DESCEND)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -