#include "common.h"
#include <locale.h>
int Orientation = 0,
Duplex = 0,
LanguageLevel = 1,
ColorDevice = 1;
float PageLeft = 18.0f,
PageRight = 594.0f,
PageBottom = 36.0f,
PageTop = 756.0f,
PageWidth = 612.0f,
PageLength = 792.0f;
ppd_file_t *
SetCommonOptions(int num_options,
cups_option_t *options,
int change_size)
{
ppd_file_t *ppd;
ppd_size_t *pagesize;
const char *val;
#ifdef LC_TIME
setlocale(LC_TIME, "");
#endif
ppd = ppdOpenFile(getenv("PPD"));
ppdMarkDefaults(ppd);
cupsMarkOptions(ppd, num_options, options);
if ((pagesize = ppdPageSize(ppd, NULL)) != NULL)
{
PageWidth = pagesize->width;
PageLength = pagesize->length;
PageTop = pagesize->top;
PageBottom = pagesize->bottom;
PageLeft = pagesize->left;
PageRight = pagesize->right;
fprintf(stderr, "DEBUG: Page = %.0fx%.0f; %.0f,%.0f to %.0f,%.0f\n",
PageWidth, PageLength, PageLeft, PageBottom, PageRight, PageTop);
}
if (ppd != NULL)
{
ColorDevice = ppd->color_device;
LanguageLevel = ppd->language_level;
}
if ((val = cupsGetOption("landscape", num_options, options)) != NULL)
{
if (strcasecmp(val, "no") != 0 && strcasecmp(val, "off") != 0 &&
strcasecmp(val, "false") != 0)
Orientation = 1;
}
else if ((val = cupsGetOption("orientation-requested", num_options, options)) != NULL)
{
Orientation = atoi(val) - 3;
if (Orientation >= 2)
Orientation ^= 1;
}
if ((val = cupsGetOption("page-left", num_options, options)) != NULL)
{
switch (Orientation & 3)
{
case 0 :
PageLeft = (float)atof(val);
break;
case 1 :
PageBottom = (float)atof(val);
break;
case 2 :
PageRight = PageWidth - (float)atof(val);
break;
case 3 :
PageTop = PageLength - (float)atof(val);
break;
}
}
if ((val = cupsGetOption("page-right", num_options, options)) != NULL)
{
switch (Orientation & 3)
{
case 0 :
PageRight = PageWidth - (float)atof(val);
break;
case 1 :
PageTop = PageLength - (float)atof(val);
break;
case 2 :
PageLeft = (float)atof(val);
break;
case 3 :
PageBottom = (float)atof(val);
break;
}
}
if ((val = cupsGetOption("page-bottom", num_options, options)) != NULL)
{
switch (Orientation & 3)
{
case 0 :
PageBottom = (float)atof(val);
break;
case 1 :
PageLeft = (float)atof(val);
break;
case 2 :
PageTop = PageLength - (float)atof(val);
break;
case 3 :
PageRight = PageWidth - (float)atof(val);
break;
}
}
if ((val = cupsGetOption("page-top", num_options, options)) != NULL)
{
switch (Orientation & 3)
{
case 0 :
PageTop = PageLength - (float)atof(val);
break;
case 1 :
PageRight = PageWidth - (float)atof(val);
break;
case 2 :
PageBottom = (float)atof(val);
break;
case 3 :
PageLeft = (float)atof(val);
break;
}
}
if (change_size)
UpdatePageVars();
if (ppdIsMarked(ppd, "Duplex", "DuplexNoTumble") ||
ppdIsMarked(ppd, "Duplex", "DuplexTumble") ||
ppdIsMarked(ppd, "JCLDuplex", "DuplexNoTumble") ||
ppdIsMarked(ppd, "JCLDuplex", "DuplexTumble") ||
ppdIsMarked(ppd, "EFDuplex", "DuplexNoTumble") ||
ppdIsMarked(ppd, "EFDuplex", "DuplexTumble") ||
ppdIsMarked(ppd, "KD03Duplex", "DuplexNoTumble") ||
ppdIsMarked(ppd, "KD03Duplex", "DuplexTumble"))
Duplex = 1;
return (ppd);
}
void
UpdatePageVars(void)
{
float temp;
switch (Orientation & 3)
{
case 0 :
break;
case 1 :
temp = PageLeft;
PageLeft = PageBottom;
PageBottom = temp;
temp = PageRight;
PageRight = PageTop;
PageTop = temp;
temp = PageWidth;
PageWidth = PageLength;
PageLength = temp;
break;
case 2 :
temp = PageWidth - PageLeft;
PageLeft = PageWidth - PageRight;
PageRight = temp;
temp = PageLength - PageBottom;
PageBottom = PageLength - PageTop;
PageTop = temp;
break;
case 3 :
temp = PageWidth - PageLeft;
PageLeft = PageWidth - PageRight;
PageRight = temp;
temp = PageLength - PageBottom;
PageBottom = PageLength - PageTop;
PageTop = temp;
temp = PageLeft;
PageLeft = PageBottom;
PageBottom = temp;
temp = PageRight;
PageRight = PageTop;
PageTop = temp;
temp = PageWidth;
PageWidth = PageLength;
PageLength = temp;
break;
}
}
void
WriteCommon(void)
{
puts("% 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");
puts("% 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");
puts("% 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");
}
void
WriteLabelProlog(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]))
{
puts("userdict/ESPwl{}bind put");
return;
}
printf("userdict");
if (strcmp(classification, "confidential") == 0)
printf("/ESPpl(CONFIDENTIAL");
else if (strcmp(classification, "classified") == 0)
printf("/ESPpl(CLASSIFIED");
else if (strcmp(classification, "secret") == 0)
printf("/ESPpl(SECRET");
else if (strcmp(classification, "topsecret") == 0)
printf("/ESPpl(TOP SECRET");
else if (strcmp(classification, "unclassified") == 0)
printf("/ESPpl(UNCLASSIFIED");
else
{
printf("/ESPpl(");
for (ptr = classification; *ptr; ptr ++)
if (*ptr < 32 || *ptr > 126)
printf("\\%03o", *ptr);
else
{
if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
putchar('\\');
putchar(*ptr);
}
}
if (label)
{
if (classification[0])
printf(" - ");
for (ptr = label; *ptr; ptr ++)
if (*ptr < 32 || *ptr > 126)
printf("\\%03o", *ptr);
else
{
if (*ptr == '(' || *ptr == ')' || *ptr == '\\')
putchar('\\');
putchar(*ptr);
}
}
puts(")put");
puts("userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put");
puts("userdict/ESPwl{");
puts(" ESPpf setfont");
printf(" ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
width * 0.5f);
puts(" 1 setgray");
printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", bottom - 2.0);
printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", top - 18.0);
puts(" 0 setgray");
printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", bottom - 2.0);
printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", top - 18.0);
printf(" dup %.0f moveto ESPpl show\n", bottom + 2.0);
printf(" %.0f moveto ESPpl show\n", top - 14.0);
puts("pop");
puts("}bind put");
}
void
WriteLabels(int orient)
{
float width,
length;
puts("gsave");
if ((orient ^ Orientation) & 1)
{
width = PageLength;
length = PageWidth;
}
else
{
width = PageWidth;
length = PageLength;
}
switch (orient & 3)
{
case 1 :
printf("%.1f 0.0 translate 90 rotate\n", length);
break;
case 2 :
printf("%.1f %.1f translate 180 rotate\n", width, length);
break;
case 3 :
printf("0.0 %.1f translate -90 rotate\n", width);
break;
}
puts("ESPwl");
puts("grestore");
}