#include "common.h"
#include <limits.h>
#include <math.h>
#include <cups/file.h>
#include <cups/array.h>
#include <cups/i18n.h>
#include <signal.h>
#define PSTOPS_BORDERNONE 0
#define PSTOPS_BORDERTHICK 1
#define PSTOPS_BORDERSINGLE 2
#define PSTOPS_BORDERSINGLE2 3
#define PSTOPS_BORDERDOUBLE 4
#define PSTOPS_BORDERDOUBLE2 5
#define PSTOPS_LAYOUT_LRBT 0
#define PSTOPS_LAYOUT_LRTB 1
#define PSTOPS_LAYOUT_RLBT 2
#define PSTOPS_LAYOUT_RLTB 3
#define PSTOPS_LAYOUT_BTLR 4
#define PSTOPS_LAYOUT_TBLR 5
#define PSTOPS_LAYOUT_BTRL 6
#define PSTOPS_LAYOUT_TBRL 7
#define PSTOPS_LAYOUT_NEGATEY 1
#define PSTOPS_LAYOUT_NEGATEX 2
#define PSTOPS_LAYOUT_VERTICAL 4
typedef struct
{
char *label;
int bounding_box[4];
off_t offset;
ssize_t length;
int num_options;
cups_option_t *options;
} pstops_page_t;
typedef struct
{
int page;
int bounding_box[4];
int new_bounding_box[4];
int num_options;
cups_option_t *options;
int normal_landscape,
saw_eof,
slow_collate,
slow_duplex,
slow_order,
use_ESPshowpage;
cups_array_t *pages;
cups_file_t *temp;
char tempfile[1024];
int job_id;
const char *user,
*title;
int copies;
const char *ap_input_slot,
*ap_manual_feed;
float brightness;
int collate,
emit_jcl,
fitplot;
float gamma;
const char *input_slot,
*manual_feed;
int mirror,
number_up,
number_up_layout,
output_order,
page_border;
const char *page_label,
*page_ranges,
*page_set;
} pstops_doc_t;
#define is_first_page(p) (doc->number_up == 1 || \
((p) % doc->number_up) == 1)
#define is_last_page(p) (doc->number_up == 1 || \
((p) % doc->number_up) == 0)
#define is_not_last_page(p) (doc->number_up > 1 && \
((p) % doc->number_up) != 0)
static int JobCanceled = 0;
static pstops_page_t *add_page(pstops_doc_t *doc, const char *label);
static void cancel_job(int sig);
static int check_range(pstops_doc_t *doc, int page);
static void copy_bytes(cups_file_t *fp, off_t offset,
size_t length);
static ssize_t copy_comments(cups_file_t *fp, pstops_doc_t *doc,
ppd_file_t *ppd, char *line,
ssize_t linelen, size_t linesize);
static void copy_dsc(cups_file_t *fp, pstops_doc_t *doc,
ppd_file_t *ppd, char *line, ssize_t linelen,
size_t linesize);
static void copy_non_dsc(cups_file_t *fp, pstops_doc_t *doc,
ppd_file_t *ppd, char *line,
ssize_t linelen, size_t linesize);
static ssize_t copy_page(cups_file_t *fp, pstops_doc_t *doc,
ppd_file_t *ppd, int number, char *line,
ssize_t linelen, size_t linesize);
static ssize_t copy_prolog(cups_file_t *fp, pstops_doc_t *doc,
ppd_file_t *ppd, char *line,
ssize_t linelen, size_t linesize);
static ssize_t copy_setup(cups_file_t *fp, pstops_doc_t *doc,
ppd_file_t *ppd, char *line,
ssize_t linelen, size_t linesize);
static ssize_t copy_trailer(cups_file_t *fp, pstops_doc_t *doc,
ppd_file_t *ppd, int number, char *line,
ssize_t linelen, size_t linesize);
static void do_prolog(pstops_doc_t *doc, ppd_file_t *ppd);
static void do_setup(pstops_doc_t *doc, ppd_file_t *ppd);
static void doc_printf(pstops_doc_t *doc, const char *format, ...)
#ifdef __GNUC__
__attribute__ ((__format__ (__printf__, 2, 3)))
#endif
;
static void doc_puts(pstops_doc_t *doc, const char *s);
static void doc_write(pstops_doc_t *doc, const char *s, size_t len);
static void end_nup(pstops_doc_t *doc, int number);
static int include_feature(ppd_file_t *ppd, const char *line,
int num_options,
cups_option_t **options);
static char *parse_text(const char *start, char **end, char *buffer,
size_t bufsize);
static void set_pstops_options(pstops_doc_t *doc, ppd_file_t *ppd,
char *argv[], int num_options,
cups_option_t *options);
static ssize_t skip_page(cups_file_t *fp, char *line, ssize_t linelen,
size_t linesize);
static void start_nup(pstops_doc_t *doc, int number,
int show_border, const int *bounding_box);
static void write_label_prolog(pstops_doc_t *doc, const char *label,
float bottom, float top,
float width);
static void write_labels(pstops_doc_t *doc, int orient);
int
main(int argc,
char *argv[])
{
pstops_doc_t doc;
cups_file_t *fp;
ppd_file_t *ppd;
int num_options;
cups_option_t *options;
char line[8192];
size_t len;
#if defined(HAVE_SIGACTION) && !defined(HAVE_SIGSET)
struct sigaction action;
#endif
setbuf(stderr, NULL);
if (argc < 6 || argc > 7)
{
fprintf(stderr, _("Usage: %s job-id user title copies options [file]\n"),
argv[0]);
return (1);
}
#ifdef HAVE_SIGSET
sigset(SIGTERM, cancel_job);
#elif defined(HAVE_SIGACTION)
memset(&action, 0, sizeof(action));
sigemptyset(&action.sa_mask);
action.sa_handler = cancel_job;
sigaction(SIGTERM, &action, NULL);
#else
signal(SIGTERM, cancel_job);
#endif
if (argc == 6)
fp = cupsFileStdin();
else
{
if ((fp = cupsFileOpen(argv[6], "r")) == NULL)
{
fprintf(stderr, _("ERROR: Unable to open file \"%s\" - %s\n"),
argv[6], strerror(errno));
return (1);
}
}
if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0)
{
fputs(_("ERROR: Empty print file!\n"), stderr);
return (1);
}
options = NULL;
num_options = cupsParseOptions(argv[5], 0, &options);
ppd = SetCommonOptions(num_options, options, 1);
set_pstops_options(&doc, ppd, argv, num_options, options);
ppdEmit(ppd, stdout, PPD_ORDER_EXIT);
if (doc.emit_jcl)
ppdEmitJCL(ppd, stdout, doc.job_id, doc.user, doc.title);
puts("%!PS-Adobe-3.0");
while (!strncmp(line, "\033%-12345X", 9) || !strncmp(line, "@PJL ", 5))
{
fputs("DEBUG: Skipping PJL header...\n", stderr);
while (strstr(line, "ENTER LANGUAGE") == NULL && strncmp(line, "%!", 2))
if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0)
break;
if (!strncmp(line, "%!", 2))
break;
if ((len = cupsFileGetLine(fp, line, sizeof(line))) == 0)
break;
}
if (!strncmp(line, "%!PS-Adobe-", 11))
{
copy_dsc(fp, &doc, ppd, line, len, sizeof(line));
}
else
{
copy_non_dsc(fp, &doc, ppd, line, len, sizeof(line));
}
if (!doc.saw_eof)
puts("%%EOF");
if (doc.emit_jcl)
{
if (ppd && ppd->jcl_end)
ppdEmitJCLEnd(ppd, stdout);
else
putchar(0x04);
}
if (doc.temp)
{
cupsFileClose(doc.temp);
unlink(doc.tempfile);
}
ppdClose(ppd);
cupsFreeOptions(num_options, options);
cupsFileClose(fp);
return (0);
}
static pstops_page_t *
add_page(pstops_doc_t *doc,
const char *label)
{
pstops_page_t *pageinfo;
if (!doc->pages)
doc->pages = cupsArrayNew(NULL, NULL);
if (!doc->pages)
{
fprintf(stderr, _("EMERG: Unable to allocate memory for pages array: %s\n"),
strerror(errno));
exit(1);
}
if ((pageinfo = calloc(1, sizeof(pstops_page_t))) == NULL)
{
fprintf(stderr, _("EMERG: Unable to allocate memory for page info: %s\n"),
strerror(errno));
exit(1);
}
pageinfo->label = strdup(label);
pageinfo->offset = cupsFileTell(doc->temp);
cupsArrayAdd(doc->pages, pageinfo);
doc->page ++;
return (pageinfo);
}
static void
cancel_job(int sig)
{
(void)sig;
JobCanceled = 1;
}
static int
check_range(pstops_doc_t *doc,
int page)
{
const char *range;
int lower, upper;
if (doc->page_set)
{
if (!strcasecmp(doc->page_set, "even") && (page & 1))
return (0);
if (!strcasecmp(doc->page_set, "odd") && !(page & 1))
return (0);
}
if (!doc->page_ranges)
return (1);
for (range = doc->page_ranges; *range != '\0';)
{
if (*range == '-')
{
lower = 1;
range ++;
upper = strtol(range, (char **)&range, 10);
}
else
{
lower = strtol(range, (char **)&range, 10);
if (*range == '-')
{
range ++;
if (!isdigit(*range & 255))
upper = 65535;
else
upper = strtol(range, (char **)&range, 10);
}
else
upper = lower;
}
if (page >= lower && page <= upper)
return (1);
if (*range == ',')
range ++;
else
break;
}
return (0);
}
static void
copy_bytes(cups_file_t *fp,
off_t offset,
size_t length)
{
char buffer[8192];
ssize_t nbytes;
size_t nleft;
nleft = length;
if (cupsFileSeek(fp, offset) < 0)
{
fprintf(stderr,
#ifdef HAVE_LONG_LONG
_("ERROR: Unable to seek to offset %lld in file - %s\n"),
#else
_("ERROR: Unable to seek to offset %ld in file - %s\n"),
#endif
CUPS_LLCAST offset, strerror(errno));
return;
}
while (nleft > 0 || length == 0)
{
if (nleft > sizeof(buffer) || length == 0)
nbytes = sizeof(buffer);
else
nbytes = nleft;
if ((nbytes = cupsFileRead(fp, buffer, nbytes)) < 1)
return;
nleft -= nbytes;
fwrite(buffer, 1, nbytes, stdout);
}
}
static ssize_t
copy_comments(cups_file_t *fp,
pstops_doc_t *doc,
ppd_file_t *ppd,
char *line,
ssize_t linelen,
size_t linesize)
{
int saw_bounding_box,
saw_for,
saw_pages,
saw_title;
saw_bounding_box = 0;
saw_for = 0;
saw_pages = 0;
saw_title = 0;
while (line[0] == '%')
{
while (linelen > 0)
{
linelen --;
if (!isspace(line[linelen] & 255))
break;
else
line[linelen] = '\0';
}
fprintf(stderr, "DEBUG: %s\n", line);
if (!strncmp(line, "%%Pages:", 8))
{
int pages;
if (saw_pages)
fputs(_("ERROR: Duplicate %%Pages: comment seen!\n"), stderr);
saw_pages = 1;
if (Duplex && (pages = atoi(line + 8)) > 0 && pages <= doc->number_up)
{
Duplex = 0;
doc->slow_duplex = 0;
if (cupsGetOption("sides", doc->num_options, doc->options))
doc->num_options = cupsAddOption("sides", "one-sided",
doc->num_options, &(doc->options));
if (cupsGetOption("Duplex", doc->num_options, doc->options))
doc->num_options = cupsAddOption("Duplex", "None",
doc->num_options, &(doc->options));
if (cupsGetOption("EFDuplex", doc->num_options, doc->options))
doc->num_options = cupsAddOption("EFDuplex", "None",
doc->num_options, &(doc->options));
if (cupsGetOption("EFDuplexing", doc->num_options, doc->options))
doc->num_options = cupsAddOption("EFDuplexing", "False",
doc->num_options, &(doc->options));
if (cupsGetOption("KD03Duplex", doc->num_options, doc->options))
doc->num_options = cupsAddOption("KD03Duplex", "None",
doc->num_options, &(doc->options));
if (cupsGetOption("JCLDuplex", doc->num_options, doc->options))
doc->num_options = cupsAddOption("JCLDuplex", "None",
doc->num_options, &(doc->options));
ppdMarkOption(ppd, "Duplex", "None");
ppdMarkOption(ppd, "EFDuplex", "None");
ppdMarkOption(ppd, "EFDuplexing", "False");
ppdMarkOption(ppd, "KD03Duplex", "None");
ppdMarkOption(ppd, "JCLDuplex", "None");
}
}
else if (!strncmp(line, "%%BoundingBox:", 14))
{
if (saw_bounding_box)
fputs(_("ERROR: Duplicate %%BoundingBox: comment seen!\n"), stderr);
else if (strstr(line + 14, "(atend)"))
{
}
else if (sscanf(line + 14, "%d%d%d%d", doc->bounding_box + 0,
doc->bounding_box + 1, doc->bounding_box + 2,
doc->bounding_box + 3) != 4)
{
fputs(_("ERROR: Bad %%BoundingBox: comment seen!\n"), stderr);
doc->bounding_box[0] = (int)PageLeft;
doc->bounding_box[1] = (int)PageBottom;
doc->bounding_box[2] = (int)PageRight;
doc->bounding_box[3] = (int)PageTop;
}
saw_bounding_box = 1;
}
else if (!strncmp(line, "%%For:", 6))
{
saw_for = 1;
doc_printf(doc, "%s\n", line);
}
else if (!strncmp(line, "%%Title:", 8))
{
saw_title = 1;
doc_printf(doc, "%s\n", line);
}
else if (!strncmp(line, "%cupsRotation:", 14))
{
int orient = (atoi(line + 14) / 90) & 3;
if (orient != Orientation)
{
Orientation = (4 - Orientation + orient) & 3;
UpdatePageVars();
Orientation = orient;
}
}
else if (!strcmp(line, "%%EndComments"))
{
linelen = cupsFileGetLine(fp, line, linesize);
break;
}
else if (strncmp(line, "%!", 2) && strncmp(line, "%cups", 5))
doc_printf(doc, "%s\n", line);
if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
break;
}
if (!saw_bounding_box)
fputs(_("ERROR: No %%BoundingBox: comment in header!\n"), stderr);
if (!saw_pages)
fputs(_("ERROR: No %%Pages: comment in header!\n"), stderr);
if (!saw_for)
WriteTextComment("For", doc->user);
if (!saw_title)
WriteTextComment("Title", doc->title);
if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
{
doc_printf(doc, "%%%%Requirements: numcopies(%d)%s%s\n", doc->copies,
doc->collate ? " collate" : "",
Duplex ? " duplex" : "");
doc_printf(doc, "%%RBINumCopies: %d\n", doc->copies);
}
else
{
if (Duplex)
doc_puts(doc, "%%Requirements: duplex\n");
doc_puts(doc, "%RBINumCopies: 1\n");
}
doc_puts(doc, "%%Pages: (atend)\n");
doc_puts(doc, "%%BoundingBox: (atend)\n");
doc_puts(doc, "%%EndComments\n");
return (linelen);
}
static void
copy_dsc(cups_file_t *fp,
pstops_doc_t *doc,
ppd_file_t *ppd,
char *line,
ssize_t linelen,
size_t linesize)
{
int number;
pstops_page_t *pageinfo;
if (strstr(line, "EPSF"))
{
doc->use_ESPshowpage = 1;
doc->number_up = 1;
}
fprintf(stderr, "DEBUG: Before copy_comments - %s", line);
linelen = copy_comments(fp, doc, ppd, line, linelen, linesize);
fprintf(stderr, "DEBUG: Before copy_prolog - %s", line);
linelen = copy_prolog(fp, doc, ppd, line, linelen, linesize);
fprintf(stderr, "DEBUG: Before copy_setup - %s", line);
linelen = copy_setup(fp, doc, ppd, line, linelen, linesize);
while (strncmp(line, "%%Page:", 7) && strncmp(line, "%%Trailer", 9))
{
doc_write(doc, line, linelen);
if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
break;
}
number = 0;
fprintf(stderr, "DEBUG: Before page loop - %s", line);
while (!strncmp(line, "%%Page:", 7))
{
if (JobCanceled)
break;
number ++;
if (check_range(doc, (number - 1) / doc->number_up + 1))
{
fprintf(stderr, "DEBUG: Copying page %d...\n", number);
linelen = copy_page(fp, doc, ppd, number, line, linelen, linesize);
}
else
{
fprintf(stderr, "DEBUG: Skipping page %d...\n", number);
linelen = skip_page(fp, line, linelen, linesize);
}
}
if (number && is_not_last_page(number) && cupsArrayLast(doc->pages) &&
check_range(doc, (number - 1) / doc->number_up + 1))
{
pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages);
start_nup(doc, doc->number_up, 0, doc->bounding_box);
doc_puts(doc, "showpage\n");
end_nup(doc, doc->number_up);
pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset;
}
if (doc->slow_duplex && (doc->page & 1))
{
pageinfo = add_page(doc, "(filler)");
if (!doc->slow_order)
{
if (!ppd || !ppd->num_filters)
fprintf(stderr, "PAGE: %d %d\n", doc->page,
doc->slow_collate ? 1 : doc->copies);
printf("%%%%Page: (filler) %d\n", doc->page);
}
start_nup(doc, doc->number_up, 0, doc->bounding_box);
doc_puts(doc, "showpage\n");
end_nup(doc, doc->number_up);
pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset;
}
number = doc->slow_order ? 0 : doc->page;
if (doc->temp && !JobCanceled && cupsArrayCount(doc->pages) > 0)
{
int copy;
cupsFileClose(doc->temp);
doc->temp = cupsFileOpen(doc->tempfile, "r");
if (doc->slow_collate)
copy = !doc->slow_order;
else
copy = doc->copies - 1;
for (; copy < doc->copies; copy ++)
{
if (JobCanceled)
break;
if (number && doc->emit_jcl && ppd && ppd->jcl_end)
{
puts("%%Trailer");
printf("%%%%Pages: %d\n", cupsArrayCount(doc->pages));
if (doc->number_up > 1 || doc->fitplot)
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
PageLeft, PageBottom, PageRight, PageTop);
else
printf("%%%%BoundingBox: %d %d %d %d\n",
doc->new_bounding_box[0], doc->new_bounding_box[1],
doc->new_bounding_box[2], doc->new_bounding_box[3]);
puts("%%EOF");
ppdEmitJCLEnd(ppd, stdout);
ppdEmitJCL(ppd, stdout, doc->job_id, doc->user, doc->title);
puts("%!PS-Adobe-3.0");
number = 0;
}
if (!number)
{
pageinfo = (pstops_page_t *)cupsArrayFirst(doc->pages);
copy_bytes(doc->temp, 0, pageinfo->offset);
}
pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayLast(doc->pages) :
(pstops_page_t *)cupsArrayFirst(doc->pages);
while (pageinfo)
{
if (JobCanceled)
break;
number ++;
if (!ppd || !ppd->num_filters)
fprintf(stderr, "PAGE: %d %d\n", number,
doc->slow_collate ? 1 : doc->copies);
if (doc->number_up > 1)
{
printf("%%%%Page: (%d) %d\n", number, number);
printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
PageLeft, PageBottom, PageRight, PageTop);
}
else
{
printf("%%%%Page: %s %d\n", pageinfo->label, number);
printf("%%%%PageBoundingBox: %d %d %d %d\n",
pageinfo->bounding_box[0], pageinfo->bounding_box[1],
pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
}
copy_bytes(doc->temp, pageinfo->offset, pageinfo->length);
pageinfo = doc->slow_order ? (pstops_page_t *)cupsArrayPrev(doc->pages) :
(pstops_page_t *)cupsArrayNext(doc->pages);
}
}
}
if (doc->use_ESPshowpage)
puts("userdict/showpage/ESPshowpage load put\n");
if (!JobCanceled)
copy_trailer(fp, doc, ppd, number, line, linelen, linesize);
}
static void
copy_non_dsc(cups_file_t *fp,
pstops_doc_t *doc,
ppd_file_t *ppd,
char *line,
ssize_t linelen,
size_t linesize)
{
int copy;
char buffer[8192];
int bytes;
fputs(_("WARNING: This document does not conform to the Adobe Document "
"Structuring Conventions and may not print correctly!\n"), stderr);
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n", PageLeft, PageBottom,
PageRight, PageTop);
if (doc->slow_collate && doc->copies > 1)
printf("%%%%Pages: %d\n", doc->copies);
else
puts("%%Pages: 1");
WriteTextComment("For", doc->user);
WriteTextComment("Title", doc->title);
if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
{
printf("%%%%Requirements: numcopies(%d)%s%s\n", doc->copies,
doc->collate ? " collate" : "",
Duplex ? " duplex" : "");
printf("%%RBINumCopies: %d\n", doc->copies);
}
else
{
if (Duplex)
puts("%%Requirements: duplex");
puts("%RBINumCopies: 1");
}
puts("%%EndComments");
puts("%%BeginProlog");
do_prolog(doc, ppd);
puts("%%EndProlog");
puts("%%BeginSetup");
do_setup(doc, ppd);
puts("%%EndSetup");
if (!ppd || !ppd->num_filters)
fprintf(stderr, "PAGE: 1 %d\n", doc->temp ? 1 : doc->copies);
puts("%%Page: 1 1");
puts("%%BeginPageSetup");
ppdEmit(ppd, stdout, PPD_ORDER_PAGE);
puts("%%EndPageSetup");
puts("%%BeginDocument: nondsc");
fwrite(line, linelen, 1, stdout);
if (doc->temp)
cupsFileWrite(doc->temp, line, linelen);
while ((bytes = cupsFileRead(fp, buffer, sizeof(buffer))) > 0)
{
fwrite(buffer, 1, bytes, stdout);
if (doc->temp)
cupsFileWrite(doc->temp, buffer, bytes);
}
puts("%%EndDocument");
if (doc->use_ESPshowpage)
{
WriteLabels(Orientation);
puts("ESPshowpage");
}
if (doc->temp && !JobCanceled)
{
cupsFileClose(doc->temp);
doc->temp = cupsFileOpen(doc->tempfile, "r");
for (copy = 1; copy < doc->copies; copy ++)
{
if (JobCanceled)
break;
if (!ppd || !ppd->num_filters)
fputs("PAGE: 1 1\n", stderr);
printf("%%%%Page: %d %d\n", copy + 1, copy + 1);
puts("%%BeginPageSetup");
ppdEmit(ppd, stdout, PPD_ORDER_PAGE);
puts("%%EndPageSetup");
puts("%%BeginDocument: nondsc");
copy_bytes(doc->temp, 0, 0);
puts("%%EndDocument");
if (doc->use_ESPshowpage)
{
WriteLabels(Orientation);
puts("ESPshowpage");
}
}
}
if (doc->use_ESPshowpage)
puts("userdict/showpage/ESPshowpage load put\n");
}
static ssize_t
copy_page(cups_file_t *fp,
pstops_doc_t *doc,
ppd_file_t *ppd,
int number,
char *line,
ssize_t linelen,
size_t linesize)
{
char label[256],
*ptr;
int level;
pstops_page_t *pageinfo;
int first_page;
int has_page_setup;
int bounding_box[4];
first_page = is_first_page(number);
if (!parse_text(line + 7, &ptr, label, sizeof(label)))
{
fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr);
label[0] = '\0';
number = doc->page;
}
else if (strtol(ptr, &ptr, 10) == LONG_MAX || !isspace(*ptr & 255))
{
fputs(_("ERROR: Bad %%Page: comment in file!\n"), stderr);
number = doc->page;
}
if (first_page)
pageinfo = add_page(doc, label);
else
pageinfo = (pstops_page_t *)cupsArrayLast(doc->pages);
if (doc->ap_input_slot || doc->ap_manual_feed)
{
if (doc->page == 1)
{
pageinfo->num_options = cupsAddOption("InputSlot", doc->ap_input_slot,
pageinfo->num_options,
&(pageinfo->options));
pageinfo->num_options = cupsAddOption("ManualFeed",
doc->ap_input_slot ? "False" :
doc->ap_manual_feed,
pageinfo->num_options,
&(pageinfo->options));
}
else if (doc->page == (Duplex + 2))
{
pageinfo->num_options = cupsAddOption("InputSlot", doc->input_slot,
pageinfo->num_options,
&(pageinfo->options));
pageinfo->num_options = cupsAddOption("ManualFeed",
doc->input_slot ? "False" :
doc->manual_feed,
pageinfo->num_options,
&(pageinfo->options));
}
}
memcpy(bounding_box, doc->bounding_box, sizeof(bounding_box));
while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
{
if (!strncmp(line, "%%PageBoundingBox:", 18))
{
if (sscanf(line + 18, "%d%d%d%d", bounding_box + 0,
bounding_box + 1, bounding_box + 2,
bounding_box + 3) != 4)
{
fputs(_("ERROR: Bad %%PageBoundingBox: comment in file!\n"), stderr);
memcpy(bounding_box, doc->bounding_box,
sizeof(bounding_box));
}
else if (doc->number_up == 1 && !doc->fitplot && Orientation)
{
int temp_bbox[4];
memcpy(temp_bbox, bounding_box, sizeof(temp_bbox));
fprintf(stderr, "DEBUG: Orientation = %d\n", Orientation);
fprintf(stderr, "DEBUG: original bounding_box = [ %d %d %d %d ]\n",
bounding_box[0], bounding_box[1],
bounding_box[2], bounding_box[3]);
fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
PageWidth, PageLength);
switch (Orientation)
{
case 1 :
bounding_box[0] = PageLength - temp_bbox[3];
bounding_box[1] = temp_bbox[0];
bounding_box[2] = PageLength - temp_bbox[1];
bounding_box[3] = temp_bbox[2];
break;
case 2 :
bounding_box[0] = PageWidth - temp_bbox[2];
bounding_box[1] = PageLength - temp_bbox[3];
bounding_box[2] = PageWidth - temp_bbox[0];
bounding_box[3] = PageLength - temp_bbox[1];
break;
case 3 :
bounding_box[0] = temp_bbox[1];
bounding_box[1] = PageWidth - temp_bbox[2];
bounding_box[2] = temp_bbox[3];
bounding_box[3] = PageWidth - temp_bbox[0];
break;
}
fprintf(stderr, "DEBUG: updated bounding_box = [ %d %d %d %d ]\n",
bounding_box[0], bounding_box[1],
bounding_box[2], bounding_box[3]);
}
}
#if 0
else if (!strncmp(line, "%%PageCustomColors:", 19) ||
!strncmp(line, "%%PageMedia:", 12) ||
!strncmp(line, "%%PageOrientation:", 18) ||
!strncmp(line, "%%PageProcessColors:", 20) ||
!strncmp(line, "%%PageRequirements:", 18) ||
!strncmp(line, "%%PageResources:", 16))
{
}
#endif
else if (!strncmp(line, "%%PageCustomColors:", 19))
{
}
else if (!strncmp(line, "%%PageMedia:", 12))
{
}
else if (!strncmp(line, "%%PageOrientation:", 18))
{
}
else if (!strncmp(line, "%%PageProcessColors:", 20))
{
}
else if (!strncmp(line, "%%PageRequirements:", 18))
{
}
else if (!strncmp(line, "%%PageResources:", 16))
{
}
else if (!strncmp(line, "%%IncludeFeature:", 17))
{
if (doc->number_up == 1 &&!doc->fitplot)
pageinfo->num_options = include_feature(ppd, line,
pageinfo->num_options,
&(pageinfo->options));
}
else if (strncmp(line, "%%Include", 9))
break;
}
if (doc->number_up == 1)
{
memcpy(pageinfo->bounding_box, bounding_box,
sizeof(pageinfo->bounding_box));
if (bounding_box[0] < doc->new_bounding_box[0])
doc->new_bounding_box[0] = bounding_box[0];
if (bounding_box[1] < doc->new_bounding_box[1])
doc->new_bounding_box[1] = bounding_box[1];
if (bounding_box[2] > doc->new_bounding_box[2])
doc->new_bounding_box[2] = bounding_box[2];
if (bounding_box[3] > doc->new_bounding_box[3])
doc->new_bounding_box[3] = bounding_box[3];
}
if (!doc->slow_order && first_page)
{
if (!ppd || !ppd->num_filters)
fprintf(stderr, "PAGE: %d %d\n", doc->page,
doc->slow_collate ? 1 : doc->copies);
if (doc->number_up > 1)
{
printf("%%%%Page: (%d) %d\n", doc->page, doc->page);
printf("%%%%PageBoundingBox: %.0f %.0f %.0f %.0f\n",
PageLeft, PageBottom, PageRight, PageTop);
}
else
{
printf("%%%%Page: %s %d\n", pageinfo->label, doc->page);
printf("%%%%PageBoundingBox: %d %d %d %d\n",
pageinfo->bounding_box[0], pageinfo->bounding_box[1],
pageinfo->bounding_box[2], pageinfo->bounding_box[3]);
}
}
if (first_page)
doc_puts(doc, "%%BeginPageSetup\n");
if ((has_page_setup = !strncmp(line, "%%BeginPageSetup", 16)) != 0)
{
int feature = 0;
while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
{
if (!strncmp(line, "%%EndPageSetup", 14))
break;
else if (!strncmp(line, "%%BeginFeature:", 15))
{
feature = 1;
if (doc->number_up > 1 || doc->fitplot)
continue;
}
else if (!strncmp(line, "%%EndFeature", 12))
{
feature = 0;
if (doc->number_up > 1 || doc->fitplot)
continue;
}
else if (!strncmp(line, "%%IncludeFeature:", 17))
{
pageinfo->num_options = include_feature(ppd, line,
pageinfo->num_options,
&(pageinfo->options));
continue;
}
else if (!strncmp(line, "%%Include", 9))
continue;
if (line[0] != '%' && !feature)
break;
if (!feature || (doc->number_up == 1 && !doc->fitplot))
doc_write(doc, line, linelen);
}
if (linelen > 0 && !strncmp(line, "%%EndPageSetup", 14))
{
linelen = cupsFileGetLine(fp, line, linesize);
has_page_setup = 0;
}
}
if (first_page)
{
char *page_setup;
if (pageinfo->num_options > 0)
{
int i;
ppd_option_t *option;
int min_order;
char *doc_setup,
*any_setup;
if ((option = ppdFindOption(ppd, "PageRegion")) != NULL)
min_order = option->order;
else
min_order = 999.0f;
for (i = 0; i < pageinfo->num_options; i ++)
if ((option = ppdFindOption(ppd, pageinfo->options[i].name)) != NULL &&
option->order < min_order)
min_order = option->order;
cupsMarkOptions(ppd, pageinfo->num_options, pageinfo->options);
doc_setup = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, min_order);
any_setup = ppdEmitString(ppd, PPD_ORDER_ANY, min_order);
if (doc_setup)
{
doc_puts(doc, doc_setup);
free(doc_setup);
}
if (any_setup)
{
doc_puts(doc, any_setup);
free(any_setup);
}
}
page_setup = ppdEmitString(ppd, PPD_ORDER_PAGE, 0);
if (page_setup)
{
doc_puts(doc, page_setup);
free(page_setup);
}
}
start_nup(doc, number, 1, bounding_box);
if (has_page_setup)
{
int feature = 0;
doc_write(doc, line, linelen);
while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
{
if (!strncmp(line, "%%EndPageSetup", 14))
break;
else if (!strncmp(line, "%%BeginFeature:", 15))
{
feature = 1;
if (doc->number_up > 1 || doc->fitplot)
continue;
}
else if (!strncmp(line, "%%EndFeature", 12))
{
feature = 0;
if (doc->number_up > 1 || doc->fitplot)
continue;
}
else if (!strncmp(line, "%%Include", 9))
continue;
if (!feature || (doc->number_up == 1 && !doc->fitplot))
doc_write(doc, line, linelen);
}
if (linelen > 0 && !strncmp(line, "%%EndPageSetup", 14))
linelen = cupsFileGetLine(fp, line, linesize);
}
if (first_page)
doc_puts(doc, "%%EndPageSetup\n");
level = 0;
do
{
if (level == 0 &&
(!strncmp(line, "%%Page:", 7) ||
!strncmp(line, "%%Trailer", 9) ||
!strncmp(line, "%%EOF", 5)))
break;
else if (!strncmp(line, "%%BeginDocument", 15) ||
!strncmp(line, "%ADO_BeginApplication", 21))
{
doc_write(doc, line, linelen);
level ++;
}
else if ((!strncmp(line, "%%EndDocument", 13) ||
!strncmp(line, "%ADO_EndApplication", 19)) && level > 0)
{
doc_write(doc, line, linelen);
level --;
}
else if (!strncmp(line, "%%BeginBinary:", 14) ||
(!strncmp(line, "%%BeginData:", 12) &&
!strstr(line, "ASCII") && !strstr(line, "Hex")))
{
int bytes;
doc_write(doc, line, linelen);
bytes = atoi(strchr(line, ':') + 1);
while (bytes > 0)
{
if (bytes > linesize)
linelen = cupsFileRead(fp, line, linesize);
else
linelen = cupsFileRead(fp, line, bytes);
if (linelen < 1)
{
line[0] = '\0';
perror("ERROR: Early end-of-file while reading binary data");
return (0);
}
doc_write(doc, line, linelen);
bytes -= linelen;
}
}
else
doc_write(doc, line, linelen);
}
while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0);
end_nup(doc, number);
pageinfo->length = cupsFileTell(doc->temp) - pageinfo->offset;
return (linelen);
}
static ssize_t
copy_prolog(cups_file_t *fp,
pstops_doc_t *doc,
ppd_file_t *ppd,
char *line,
ssize_t linelen,
size_t linesize)
{
while (strncmp(line, "%%BeginProlog", 13))
{
if (!strncmp(line, "%%BeginSetup", 12) || !strncmp(line, "%%Page:", 7))
break;
doc_write(doc, line, linelen);
if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
break;
}
doc_puts(doc, "%%BeginProlog\n");
do_prolog(doc, ppd);
if (!strncmp(line, "%%BeginProlog", 13))
{
while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
{
if (!strncmp(line, "%%EndProlog", 11) ||
!strncmp(line, "%%BeginSetup", 12) ||
!strncmp(line, "%%Page:", 7))
break;
doc_write(doc, line, linelen);
}
if (!strncmp(line, "%%EndProlog", 11))
linelen = cupsFileGetLine(fp, line, linesize);
else
fputs(_("ERROR: Missing %%EndProlog!\n"), stderr);
}
doc_puts(doc, "%%EndProlog\n");
return (linelen);
}
static ssize_t
copy_setup(cups_file_t *fp,
pstops_doc_t *doc,
ppd_file_t *ppd,
char *line,
ssize_t linelen,
size_t linesize)
{
while (strncmp(line, "%%BeginSetup", 12))
{
if (!strncmp(line, "%%Page:", 7))
break;
doc_write(doc, line, linelen);
if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
break;
}
doc_puts(doc, "%%BeginSetup\n");
if (!strncmp(line, "%%BeginSetup", 12))
{
while (strncmp(line, "%%EndSetup", 10))
{
if (!strncmp(line, "%%Page:", 7))
break;
else if (!strncmp(line, "%%IncludeFeature:", 17))
{
if (doc->number_up == 1 && !doc->fitplot)
doc->num_options = include_feature(ppd, line, doc->num_options,
&(doc->options));
}
else if (strncmp(line, "%%BeginSetup", 12))
doc_write(doc, line, linelen);
if ((linelen = cupsFileGetLine(fp, line, linesize)) == 0)
break;
}
if (!strncmp(line, "%%EndSetup", 10))
linelen = cupsFileGetLine(fp, line, linesize);
else
fputs(_("ERROR: Missing %%EndSetup!\n"), stderr);
}
do_setup(doc, ppd);
doc_puts(doc, "%%EndSetup\n");
return (linelen);
}
static ssize_t
copy_trailer(cups_file_t *fp,
pstops_doc_t *doc,
ppd_file_t *ppd,
int number,
char *line,
ssize_t linelen,
size_t linesize)
{
puts("%%Trailer");
while (linelen > 0)
{
if (!strncmp(line, "%%EOF", 5))
break;
else if (strncmp(line, "%%Trailer", 9) &&
strncmp(line, "%%Pages:", 8) &&
strncmp(line, "%%BoundingBox:", 14))
fwrite(line, 1, linelen, stdout);
linelen = cupsFileGetLine(fp, line, linesize);
}
fprintf(stderr, "DEBUG: Wrote %d pages...\n", number);
printf("%%%%Pages: %d\n", number);
if (doc->number_up > 1 || doc->fitplot)
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
PageLeft, PageBottom, PageRight, PageTop);
else
printf("%%%%BoundingBox: %d %d %d %d\n",
doc->new_bounding_box[0], doc->new_bounding_box[1],
doc->new_bounding_box[2], doc->new_bounding_box[3]);
return (linelen);
}
static void
do_prolog(pstops_doc_t *doc,
ppd_file_t *ppd)
{
char *ps;
if (ppd && ppd->patches)
{
doc_puts(doc, "%%BeginFeature: *JobPatchFile 1\n");
doc_puts(doc, ppd->patches);
doc_puts(doc, "\n%%EndFeature\n");
}
if ((ps = ppdEmitString(ppd, PPD_ORDER_PROLOG, 0.0)) != NULL)
{
doc_puts(doc, ps);
free(ps);
}
if (doc->use_ESPshowpage)
doc_puts(doc, "userdict/ESPshowpage/showpage load put\n"
"userdict/showpage{}put\n");
}
static void
do_setup(pstops_doc_t *doc,
ppd_file_t *ppd)
{
char *ps;
doc_puts(doc, "% Disable CTRL-D as an end-of-file marker...\n");
doc_puts(doc, "userdict dup(\\004)cvn{}put (\\004\\004)cvn{}put\n");
cupsMarkOptions(ppd, doc->num_options, doc->options);
if ((ps = ppdEmitString(ppd, PPD_ORDER_DOCUMENT, 0.0)) != NULL)
{
doc_puts(doc, ps);
free(ps);
}
if ((ps = ppdEmitString(ppd, PPD_ORDER_ANY, 0.0)) != NULL)
{
doc_puts(doc, ps);
free(ps);
}
if (doc->copies != 1 && (!doc->collate || !doc->slow_collate))
{
doc_printf(doc, "%%RBIBeginNonPPDFeature: *NumCopies %d\n", doc->copies);
doc_printf(doc,
"%d/languagelevel where{pop languagelevel 2 ge}{false}ifelse\n"
"{1 dict begin/NumCopies exch def currentdict end "
"setpagedevice}\n"
"{userdict/#copies 3 -1 roll put}ifelse\n", doc->copies);
doc_puts(doc, "%RBIEndNonPPDFeature\n");
}
if (doc->number_up > 1)
doc_puts(doc, "userdict/setpagedevice{pop}bind put\n");
if (doc->gamma != 1.0f || doc->brightness != 1.0f)
doc_printf(doc, "{ neg 1 add dup 0 lt { pop 1 } { %.3f exp neg 1 add } "
"ifelse %.3f mul } bind settransfer\n",
doc->gamma, doc->brightness);
doc_puts(doc,
"% x y w h ESPrc - Clip to a rectangle.\n"
"userdict/ESPrc/rectclip where{pop/rectclip load}\n"
"{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
"neg 0 rlineto closepath clip newpath}bind}ifelse put\n");
doc_puts(doc,
"% x y w h ESPrf - Fill a rectangle.\n"
"userdict/ESPrf/rectfill where{pop/rectfill load}\n"
"{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
"neg 0 rlineto closepath fill grestore}bind}ifelse put\n");
doc_puts(doc,
"% x y w h ESPrs - Stroke a rectangle.\n"
"userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
"{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
"neg 0 rlineto closepath stroke grestore}bind}ifelse put\n");
if (doc->number_up == 2 || doc->number_up == 6)
{
if (Orientation & 1)
write_label_prolog(doc, doc->page_label, PageBottom,
PageWidth - PageLength + PageTop, PageLength);
else
write_label_prolog(doc, doc->page_label, PageLeft, PageRight,
PageLength);
}
else
write_label_prolog(doc, doc->page_label, PageBottom, PageTop, PageWidth);
}
static void
doc_printf(pstops_doc_t *doc,
const char *format,
...)
{
va_list ap;
char buffer[1024];
size_t bytes;
va_start(ap, format);
bytes = vsnprintf(buffer, sizeof(buffer), format, ap);
va_end(ap);
if (bytes > sizeof(buffer))
{
fprintf(stderr,
_("ERROR: doc_printf overflow (%d bytes) detected, aborting!\n"),
(int)bytes);
exit(1);
}
doc_write(doc, buffer, bytes);
}
static void
doc_puts(pstops_doc_t *doc,
const char *s)
{
doc_write(doc, s, strlen(s));
}
static void
doc_write(pstops_doc_t *doc,
const char *s,
size_t len)
{
if (!doc->slow_order)
fwrite(s, 1, len, stdout);
if (doc->temp)
cupsFileWrite(doc->temp, s, len);
}
static void
end_nup(pstops_doc_t *doc,
int number)
{
if (doc->number_up > 1)
doc_puts(doc, "userdict/ESPsave get restore\n");
switch (doc->number_up)
{
case 1 :
if (doc->use_ESPshowpage)
{
write_labels(doc, Orientation);
doc_puts(doc, "ESPshowpage\n");
}
break;
case 2 :
case 6 :
if (is_last_page(number) && doc->use_ESPshowpage)
{
if (Orientation & 1)
{
write_labels(doc, Orientation - 1);
}
else if (Orientation == 0)
{
write_labels(doc, doc->normal_landscape ? 1 : 3);
}
else
{
write_labels(doc, doc->normal_landscape ? 3 : 1);
}
doc_puts(doc, "ESPshowpage\n");
}
break;
default :
if (is_last_page(number) && doc->use_ESPshowpage)
{
write_labels(doc, Orientation);
doc_puts(doc, "ESPshowpage\n");
}
break;
}
fflush(stdout);
}
static int
include_feature(
ppd_file_t *ppd,
const char *line,
int num_options,
cups_option_t **options)
{
char name[255],
value[255];
ppd_option_t *option;
if (sscanf(line + 17, "%254s%254s", name, value) != 2)
{
fputs(_("ERROR: Bad %%IncludeFeature: comment!\n"), stderr);
return (num_options);
}
if ((option = ppdFindOption(ppd, name + 1)) == NULL)
{
fprintf(stderr, _("WARNING: Unknown option \"%s\"!\n"), name + 1);
return (num_options);
}
if (option->section == PPD_ORDER_EXIT ||
option->section == PPD_ORDER_JCL)
{
fprintf(stderr, _("WARNING: Option \"%s\" cannot be included via "
"IncludeFeature!\n"), name + 1);
return (num_options);
}
if (!ppdFindChoice(option, value))
{
fprintf(stderr, _("WARNING: Unknown choice \"%s\" for option \"%s\"!\n"),
value, name + 1);
return (num_options);
}
return (cupsAddOption(name + 1, value, num_options, options));
}
static char *
parse_text(const char *start,
char **end,
char *buffer,
size_t bufsize)
{
char *bufptr,
*bufend;
int level;
while (isspace(*start & 255))
start ++;
level = 0;
bufptr = buffer;
bufend = buffer + bufsize - 1;
while (bufptr < bufend)
{
if (isspace(*start & 255) && !level)
break;
*bufptr++ = *start;
if (*start == '(')
level ++;
else if (*start == ')')
{
if (!level)
{
start ++;
break;
}
else
level --;
}
else if (*start == '\\')
{
int i;
for (i = 1;
i <= 3 && isdigit(start[i] & 255) && bufptr < bufend;
*bufptr++ = start[i], i ++);
}
start ++;
}
*bufptr = '\0';
if (end)
*end = (char *)start;
if (bufptr == bufend)
return (NULL);
else
return (buffer);
}
static void
set_pstops_options(
pstops_doc_t *doc,
ppd_file_t *ppd,
char *argv[],
int num_options,
cups_option_t *options)
{
const char *val;
int intval;
ppd_attr_t *attr;
ppd_option_t *option;
ppd_choice_t *choice;
memset(doc, 0, sizeof(pstops_doc_t));
doc->job_id = atoi(argv[1]);
doc->user = argv[2];
doc->title = argv[3];
doc->copies = atoi(argv[4]);
if (ppd && ppd->landscape > 0)
doc->normal_landscape = 1;
doc->bounding_box[0] = (int)PageLeft;
doc->bounding_box[1] = (int)PageBottom;
doc->bounding_box[2] = (int)PageRight;
doc->bounding_box[3] = (int)PageTop;
doc->new_bounding_box[0] = INT_MAX;
doc->new_bounding_box[1] = INT_MAX;
doc->new_bounding_box[2] = INT_MIN;
doc->new_bounding_box[3] = INT_MIN;
doc->ap_input_slot = cupsGetOption("AP_FIRSTPAGE_InputSlot", num_options,
options);
doc->ap_manual_feed = cupsGetOption("AP_FIRSTPAGE_ManualFeed", num_options,
options);
if ((val = cupsGetOption("brightness", num_options, options)) != NULL)
{
intval = atoi(val);
if (intval < 10 || intval > 1000)
{
fprintf(stderr, _("ERROR: Unsupported brightness value %s, using "
"brightness=100!\n"), val);
doc->brightness = 1.0f;
}
else
doc->brightness = intval * 0.01f;
}
else
doc->brightness = 1.0f;
if ((val = cupsGetOption("multiple-document-handling", num_options, options)) != NULL)
{
doc->collate = strcasecmp(val, "separate-documents-uncollated-copies") != 0;
}
if ((val = cupsGetOption("Collate", num_options, options)) != NULL &&
(!strcasecmp(val, "true") ||!strcasecmp(val, "on") ||
!strcasecmp(val, "yes")))
doc->collate = 1;
if ((val = cupsGetOption("emit-jcl", num_options, options)) != NULL &&
(!strcasecmp(val, "false") || !strcasecmp(val, "off") ||
!strcasecmp(val, "no") || !strcmp(val, "0")))
doc->emit_jcl = 0;
else
doc->emit_jcl = 1;
if ((val = cupsGetOption("fitplot", num_options, options)) != NULL &&
(!strcasecmp(val, "true") || !strcasecmp(val, "on") ||
!strcasecmp(val, "yes")))
doc->fitplot = 1;
if ((val = cupsGetOption("gamma", num_options, options)) != NULL)
{
intval = atoi(val);
if (intval < 1 || intval > 10000)
{
fprintf(stderr, _("ERROR: Unsupported gamma value %s, using "
"gamma=1000!\n"), val);
doc->gamma = 1.0f;
}
else
doc->gamma = intval * 0.001f;
}
else
doc->gamma = 1.0f;
if ((choice = ppdFindMarkedChoice(ppd, "InputSlot")) != NULL)
doc->input_slot = choice->choice;
if ((choice = ppdFindMarkedChoice(ppd, "ManualFeed")) != NULL)
doc->manual_feed = choice->choice;
if ((choice = ppdFindMarkedChoice(ppd, "MirrorPrint")) != NULL)
{
val = choice->choice;
choice->marked = 0;
}
else
val = cupsGetOption("mirror", num_options, options);
if (val && (!strcasecmp(val, "true") || !strcasecmp(val, "on") ||
!strcasecmp(val, "yes")))
doc->mirror = 1;
if ((val = cupsGetOption("number-up", num_options, options)) != NULL)
{
switch (intval = atoi(val))
{
case 1 :
case 2 :
case 4 :
case 6 :
case 9 :
case 16 :
doc->number_up = intval;
break;
default :
fprintf(stderr,
_("ERROR: Unsupported number-up value %d, using "
"number-up=1!\n"), intval);
doc->number_up = 1;
break;
}
}
else
doc->number_up = 1;
if ((val = cupsGetOption("number-up-layout", num_options, options)) != NULL)
{
if (!strcasecmp(val, "lrtb"))
doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
else if (!strcasecmp(val, "lrbt"))
doc->number_up_layout = PSTOPS_LAYOUT_LRBT;
else if (!strcasecmp(val, "rltb"))
doc->number_up_layout = PSTOPS_LAYOUT_RLTB;
else if (!strcasecmp(val, "rlbt"))
doc->number_up_layout = PSTOPS_LAYOUT_RLBT;
else if (!strcasecmp(val, "tblr"))
doc->number_up_layout = PSTOPS_LAYOUT_TBLR;
else if (!strcasecmp(val, "tbrl"))
doc->number_up_layout = PSTOPS_LAYOUT_TBRL;
else if (!strcasecmp(val, "btlr"))
doc->number_up_layout = PSTOPS_LAYOUT_BTLR;
else if (!strcasecmp(val, "btrl"))
doc->number_up_layout = PSTOPS_LAYOUT_BTRL;
else
{
fprintf(stderr, _("ERROR: Unsupported number-up-layout value %s, using "
"number-up-layout=lrtb!\n"), val);
doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
}
}
else
doc->number_up_layout = PSTOPS_LAYOUT_LRTB;
if ((val = cupsGetOption("OutputOrder", num_options, options)) != NULL)
{
if (!strcasecmp(val, "Reverse"))
doc->output_order = 1;
}
else if (ppd)
{
if ((choice = ppdFindMarkedChoice(ppd, "OutputBin")) != NULL &&
(attr = ppdFindAttr(ppd, "PageStackOrder", choice->choice)) != NULL &&
attr->value)
doc->output_order = !strcasecmp(attr->value, "Reverse");
else if ((attr = ppdFindAttr(ppd, "DefaultOutputOrder", NULL)) != NULL &&
attr->value)
doc->output_order = !strcasecmp(attr->value, "Reverse");
}
if ((val = cupsGetOption("page-border", num_options, options)) != NULL)
{
if (!strcasecmp(val, "none"))
doc->page_border = PSTOPS_BORDERNONE;
else if (!strcasecmp(val, "single"))
doc->page_border = PSTOPS_BORDERSINGLE;
else if (!strcasecmp(val, "single-thick"))
doc->page_border = PSTOPS_BORDERSINGLE2;
else if (!strcasecmp(val, "double"))
doc->page_border = PSTOPS_BORDERDOUBLE;
else if (!strcasecmp(val, "double-thick"))
doc->page_border = PSTOPS_BORDERDOUBLE2;
else
{
fprintf(stderr, _("ERROR: Unsupported page-border value %s, using "
"page-border=none!\n"), val);
doc->page_border = PSTOPS_BORDERNONE;
}
}
else
doc->page_border = PSTOPS_BORDERNONE;
doc->page_label = cupsGetOption("page-label", num_options, options);
doc->page_ranges = cupsGetOption("page-ranges", num_options, options);
doc->page_set = cupsGetOption("page-set", num_options, options);
if (ppd && ppd->manual_copies && Duplex && doc->copies > 1)
{
doc->collate = 1;
}
if (doc->collate && doc->copies > 1)
{
doc->slow_collate = 1;
if ((choice = ppdFindMarkedChoice(ppd, "Collate")) != NULL &&
!strcasecmp(choice->choice, "True"))
{
if ((option = ppdFindOption(ppd, "Collate")) != NULL &&
!option->conflicted)
doc->slow_collate = 0;
else
ppdMarkOption(ppd, "Collate", "False");
}
}
else
doc->slow_collate = 0;
if (!ppdFindOption(ppd, "OutputOrder") && doc->output_order)
doc->slow_order = 1;
else
doc->slow_order = 0;
if (Duplex &&
(doc->slow_collate || doc->slow_order ||
((attr = ppdFindAttr(ppd, "cupsEvenDuplex", NULL)) != NULL &&
attr->value && !strcasecmp(attr->value, "true"))))
doc->slow_duplex = 1;
else
doc->slow_duplex = 0;
if (doc->slow_order || doc->slow_collate)
{
if ((doc->temp = cupsTempFile2(doc->tempfile,
sizeof(doc->tempfile))) == NULL)
{
fprintf(stderr, _("ERROR: Unable to create temporary file: %s\n"),
strerror(errno));
exit(1);
}
}
if (doc->page_label || getenv("CLASSIFICATION") || doc->number_up > 1 ||
doc->page_border)
{
doc->use_ESPshowpage = 1;
}
fprintf(stderr, "DEBUG: slow_collate=%d, slow_duplex=%d, slow_order=%d\n",
doc->slow_collate, doc->slow_duplex, doc->slow_order);
}
static ssize_t
skip_page(cups_file_t *fp,
char *line,
ssize_t linelen,
size_t linesize)
{
int level;
level = 0;
while ((linelen = cupsFileGetLine(fp, line, linesize)) > 0)
{
if (level == 0 &&
(!strncmp(line, "%%Page:", 7) || !strncmp(line, "%%Trailer", 9)))
break;
else if (!strncmp(line, "%%BeginDocument", 15) ||
!strncmp(line, "%ADO_BeginApplication", 21))
level ++;
else if ((!strncmp(line, "%%EndDocument", 13) ||
!strncmp(line, "%ADO_EndApplication", 19)) && level > 0)
level --;
else if (!strncmp(line, "%%BeginBinary:", 14) ||
(!strncmp(line, "%%BeginData:", 12) &&
!strstr(line, "ASCII") && !strstr(line, "Hex")))
{
int bytes;
bytes = atoi(strchr(line, ':') + 1);
while (bytes > 0)
{
if (bytes > linesize)
linelen = cupsFileRead(fp, line, linesize);
else
linelen = cupsFileRead(fp, line, bytes);
if (linelen < 1)
{
line[0] = '\0';
perror("ERROR: Early end-of-file while reading binary data");
return (0);
}
bytes -= linelen;
}
}
}
return (linelen);
}
static void
start_nup(pstops_doc_t *doc,
int number,
int show_border,
const int *bounding_box)
{
int pos;
int x, y;
float w, l,
tx, ty;
float pagew,
pagel;
int bboxx,
bboxy,
bboxw,
bboxl;
float margin = 0;
if (doc->number_up > 1)
doc_puts(doc, "userdict/ESPsave save put\n");
if (doc->mirror)
doc_printf(doc, "%.1f 0.0 translate -1 1 scale\n", PageWidth);
pos = (number - 1) % doc->number_up;
pagew = PageRight - PageLeft;
pagel = PageTop - PageBottom;
if (doc->fitplot)
{
bboxx = bounding_box[0];
bboxy = bounding_box[1];
bboxw = bounding_box[2] - bounding_box[0];
bboxl = bounding_box[3] - bounding_box[1];
}
else
{
bboxx = 0;
bboxy = 0;
bboxw = PageWidth;
bboxl = PageLength;
}
fprintf(stderr, "DEBUG: pagew = %.1f, pagel = %.1f\n", pagew, pagel);
fprintf(stderr, "DEBUG: bboxx = %d, bboxy = %d, bboxw = %d, bboxl = %d\n",
bboxx, bboxy, bboxw, bboxl);
fprintf(stderr, "DEBUG: PageLeft = %.1f, PageRight = %.1f\n",
PageLeft, PageRight);
fprintf(stderr, "DEBUG: PageTop = %.1f, PageBottom = %.1f\n",
PageTop, PageBottom);
fprintf(stderr, "DEBUG: PageWidth = %.1f, PageLength = %.1f\n",
PageWidth, PageLength);
switch (Orientation)
{
case 1 :
doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", PageLength);
break;
case 2 :
doc_printf(doc, "%.1f %.1f translate 180 rotate\n", PageWidth,
PageLength);
break;
case 3 :
doc_printf(doc, "0.0 %.1f translate -90 rotate\n", PageWidth);
break;
}
if (Duplex && doc->number_up > 1 && ((number / doc->number_up) & 1))
doc_printf(doc, "%.1f %.1f translate\n", PageWidth - PageRight, PageBottom);
else if (doc->number_up > 1 || doc->fitplot)
doc_printf(doc, "%.1f %.1f translate\n", PageLeft, PageBottom);
switch (doc->number_up)
{
default :
if (doc->fitplot)
{
w = pagew;
l = w * bboxl / bboxw;
if (l > pagel)
{
l = pagel;
w = l * bboxw / bboxl;
}
tx = 0.5 * (pagew - w);
ty = 0.5 * (pagel - l);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n", tx, ty,
w / bboxw, l / bboxl);
}
else
w = PageWidth;
break;
case 2 :
if (Orientation & 1)
{
x = pos & 1;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
x = 1 - x;
w = pagel;
l = w * bboxl / bboxw;
if (l > (pagew * 0.5))
{
l = pagew * 0.5;
w = l * bboxw / bboxl;
}
tx = 0.5 * (pagew * 0.5 - l);
ty = 0.5 * (pagel - w);
if (doc->normal_landscape)
doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel);
else
doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
ty, tx + pagew * 0.5 * x, w / bboxw, l / bboxl);
}
else
{
x = pos & 1;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 1 - x;
l = pagew;
w = l * bboxw / bboxl;
if (w > (pagel * 0.5))
{
w = pagel * 0.5;
l = w * bboxl / bboxw;
}
tx = 0.5 * (pagel * 0.5 - w);
ty = 0.5 * (pagew - l);
if (doc->normal_landscape)
doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", pagew);
else
doc_printf(doc, "0.0 %.1f translate -90 rotate\n", pagel);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
tx + pagel * 0.5 * x, ty, w / bboxw, l / bboxl);
}
break;
case 4 :
if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
{
x = (pos / 2) & 1;
y = pos & 1;
}
else
{
x = pos & 1;
y = (pos / 2) & 1;
}
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 1 - x;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
y = 1 - y;
w = pagew * 0.5;
l = w * bboxl / bboxw;
if (l > (pagel * 0.5))
{
l = pagel * 0.5;
w = l * bboxw / bboxl;
}
tx = 0.5 * (pagew * 0.5 - w);
ty = 0.5 * (pagel * 0.5 - l);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
tx + x * pagew * 0.5, ty + y * pagel * 0.5,
w / bboxw, l / bboxl);
break;
case 6 :
if (Orientation & 1)
{
if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
{
x = pos / 3;
y = pos % 3;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 1 - x;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
y = 2 - y;
}
else
{
x = pos & 1;
y = pos / 2;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 1 - x;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
y = 2 - y;
}
w = pagel * 0.5;
l = w * bboxl / bboxw;
if (l > (pagew * 0.333))
{
l = pagew * 0.333;
w = l * bboxw / bboxl;
}
tx = 0.5 * (pagel - 2 * w);
ty = 0.5 * (pagew - 3 * l);
if (doc->normal_landscape)
doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel);
else
doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
tx + x * w, ty + y * l, l / bboxl, w / bboxw);
}
else
{
if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
{
x = pos / 2;
y = pos & 1;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 2 - x;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
y = 1 - y;
}
else
{
x = pos % 3;
y = pos / 3;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 2 - x;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
y = 1 - y;
}
l = pagew * 0.5;
w = l * bboxw / bboxl;
if (w > (pagel * 0.333))
{
w = pagel * 0.333;
l = w * bboxl / bboxw;
}
tx = 0.5 * (pagel - 3 * w);
ty = 0.5 * (pagew - 2 * l);
if (doc->normal_landscape)
doc_printf(doc, "%.1f 0 translate 90 rotate\n", pagew);
else
doc_printf(doc, "0 %.1f translate -90 rotate\n", pagel);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
tx + w * x, ty + l * y, w / bboxw, l / bboxl);
}
break;
case 9 :
if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
{
x = (pos / 3) % 3;
y = pos % 3;
}
else
{
x = pos % 3;
y = (pos / 3) % 3;
}
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 2 - x;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
y = 2 - y;
w = pagew * 0.333;
l = w * bboxl / bboxw;
if (l > (pagel * 0.333))
{
l = pagel * 0.333;
w = l * bboxw / bboxl;
}
tx = 0.5 * (pagew * 0.333 - w);
ty = 0.5 * (pagel * 0.333 - l);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
tx + x * pagew * 0.333, ty + y * pagel * 0.333,
w / bboxw, l / bboxl);
break;
case 16 :
if (doc->number_up_layout & PSTOPS_LAYOUT_VERTICAL)
{
x = (pos / 4) & 3;
y = pos & 3;
}
else
{
x = pos & 3;
y = (pos / 4) & 3;
}
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEX)
x = 3 - x;
if (doc->number_up_layout & PSTOPS_LAYOUT_NEGATEY)
y = 3 - y;
w = pagew * 0.25;
l = w * bboxl / bboxw;
if (l > (pagel * 0.25))
{
l = pagel * 0.25;
w = l * bboxw / bboxl;
}
tx = 0.5 * (pagew * 0.25 - w);
ty = 0.5 * (pagel * 0.25 - l);
doc_printf(doc, "%.1f %.1f translate %.3f %.3f scale\n",
tx + x * pagew * 0.25, ty + y * pagel * 0.25,
w / bboxw, l / bboxl);
break;
}
if (doc->page_border && show_border)
{
int rects;
float fscale;
rects = (doc->page_border & PSTOPS_BORDERDOUBLE) ? 2 : 1;
fscale = PageWidth / w;
margin = 2.25 * fscale;
doc_puts(doc, "gsave\n");
doc_printf(doc, "%.3f setlinewidth 0 setgray newpath\n",
(doc->page_border & PSTOPS_BORDERTHICK) ? 0.5 * fscale :
0.24 * fscale);
for (; rects > 0; rects --, margin += 2 * fscale)
if (doc->number_up > 1)
doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
margin,
margin,
bboxw - 2 * margin,
bboxl - 2 * margin);
else
doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrs\n",
PageLeft + margin,
PageBottom + margin,
PageRight - PageLeft - 2 * margin,
PageTop - PageBottom - 2 * margin);
doc_puts(doc, "grestore\n");
}
if (doc->fitplot)
{
doc_printf(doc, "%d %d translate\n", -bounding_box[0],
-bounding_box[1]);
}
if (doc->fitplot || doc->number_up > 1)
{
doc_printf(doc, "%.1f %.1f %.1f %.1f ESPrc\n",
bboxx + margin, bboxy + margin,
bboxw - 2 * margin, bboxl - 2 * margin);
}
}
static void
write_label_prolog(pstops_doc_t *doc,
const char *label,
float bottom,
float top,
float width)
{
const char *classification;
const char *ptr;
if ((classification = getenv("CLASSIFICATION")) == NULL)
classification = "";
if (strcmp(classification, "none") == 0)
classification = "";
if (!classification[0] && (label == NULL || !label[0]))
{
doc_puts(doc, "userdict/ESPwl{}bind put\n");
return;
}
doc_puts(doc, "userdict");
if (!strcmp(classification, "confidential"))
doc_puts(doc, "/ESPpl(CONFIDENTIAL");
else if (!strcmp(classification, "classified"))
doc_puts(doc, "/ESPpl(CLASSIFIED");
else if (!strcmp(classification, "secret"))
doc_puts(doc, "/ESPpl(SECRET");
else if (!strcmp(classification, "topsecret"))
doc_puts(doc, "/ESPpl(TOP SECRET");
else if (!strcmp(classification, "unclassified"))
doc_puts(doc, "/ESPpl(UNCLASSIFIED");
else
{
doc_puts(doc, "/ESPpl(");
for (ptr = classification; *ptr; ptr ++)
{
if (*ptr < 32 || *ptr > 126)
doc_printf(doc, "\\%03o", *ptr);
else if (*ptr == '_')
doc_puts(doc, " ");
else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
doc_printf(doc, "\\%c", *ptr);
else
doc_printf(doc, "%c", *ptr);
}
}
if (label)
{
if (classification[0])
doc_puts(doc, " - ");
for (ptr = label; *ptr; ptr ++)
{
if (*ptr < 32 || *ptr > 126)
doc_printf(doc, "\\%03o", *ptr);
else if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
doc_printf(doc, "\\%c", *ptr);
else
doc_printf(doc, "%c", *ptr);
}
}
doc_puts(doc, ")put\n");
doc_puts(doc, "userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put\n");
doc_puts(doc, "userdict/ESPwl{\n");
doc_puts(doc, " ESPpf setfont\n");
doc_printf(doc, " ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
width * 0.5f);
doc_puts(doc, " 1 setgray\n");
doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0);
doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0);
doc_puts(doc, " 0 setgray\n");
doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0);
doc_printf(doc, " dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0);
doc_printf(doc, " dup %.0f moveto ESPpl show\n", bottom + 2.0);
doc_printf(doc, " %.0f moveto ESPpl show\n", top - 14.0);
doc_puts(doc, "pop\n");
doc_puts(doc, "}bind put\n");
}
static void
write_labels(pstops_doc_t *doc,
int orient)
{
float width,
length;
doc_puts(doc, "gsave\n");
if ((orient ^ Orientation) & 1)
{
width = PageLength;
length = PageWidth;
}
else
{
width = PageWidth;
length = PageLength;
}
switch (orient & 3)
{
case 1 :
doc_printf(doc, "%.1f 0.0 translate 90 rotate\n", length);
break;
case 2 :
doc_printf(doc, "%.1f %.1f translate 180 rotate\n", width, length);
break;
case 3 :
doc_printf(doc, "0.0 %.1f translate -90 rotate\n", width);
break;
}
doc_puts(doc, "ESPwl\n");
doc_puts(doc, "grestore\n");
}