#include "hpgltops.h"
#include <stdarg.h>
void
OutputProlog(char *title,
char *user,
int shading)
{
FILE *prolog;
char line[255];
const char *datadir;
char filename[1024];
time_t curtime;
struct tm *curtm;
curtime = time(NULL);
curtm = localtime(&curtime);
puts("%!PS-Adobe-3.0");
printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
PageLeft, PageBottom, PageRight, PageTop);
puts("%%Pages: (atend)");
printf("%%%%LanguageLevel: %d\n", LanguageLevel);
puts("%%DocumentData: Clean7Bit");
puts("%%DocumentSuppliedResources: procset hpgltops 1.1 0");
puts("%%DocumentNeededResources: font Courier Helvetica");
puts("%%Creator: hpgltops/" CUPS_SVERSION);
strftime(line, sizeof(line), CUPS_STRFTIME_FORMAT, curtm);
printf("%%%%CreationDate: %s\n", line);
printf("%%%%Title: %s\n", title);
printf("%%%%For: %s\n", user);
printf("%%ESPRotation: %d\n", (Orientation & 3) * 90);
puts("%%EndComments");
puts("%%BeginProlog");
printf("/DefaultPenWidth %.2f def\n", PenWidth * 72.0 / 25.4);
puts("3.0 setmiterlimit");
if (!shading)
puts("/setrgbcolor { pop pop pop } bind def");
else if (!ColorDevice)
puts("/setrgbcolor { 0.08 mul exch 0.61 mul add exch 0.31 mul add setgray } bind def\n");
if ((datadir = getenv("CUPS_DATADIR")) == NULL)
datadir = CUPS_DATADIR;
snprintf(filename, sizeof(filename), "%s/data/HPGLprolog", datadir);
if ((prolog = fopen(filename, "r")) == NULL)
{
fprintf(stderr, "ERROR: Unable to open HPGL prolog \"%s\" for reading - %s\n",
filename, strerror(errno));
exit(1);
}
while (fgets(line, sizeof(line), prolog) != NULL)
fputs(line, stdout);
fclose(prolog);
puts("%%EndProlog");
IN_initialize(0, NULL);
}
void
OutputTrailer(void)
{
if (PageDirty)
PG_advance_page(0, NULL);
puts("%%Trailer");
printf("%%%%Pages: %d\n", PageCount);
puts("%%EOF");
}
int
Outputf(const char *format,
...)
{
va_list ap;
int bytes;
float iw1[2], iw2[2];
int i;
ppd_size_t *size;
ppd_option_t *option;
ppd_choice_t *choice;
float width, length;
int landscape;
if (!PageDirty)
{
PageDirty = 1;
PageCount ++;
printf("%%%%Page: %d %d\n", PageCount, PageCount);
landscape = 0;
if (!FitPlot && PlotSizeSet)
{
if (PageRotation == 0 || PageRotation == 180)
{
width = PlotSize[0];
length = PlotSize[1];
}
else
{
width = PlotSize[1];
length = PlotSize[0];
}
fprintf(stderr, "DEBUG: hpgltops setting page size (%.0f x %.0f)\n",
width, length);
if (PPD != NULL)
{
fputs("DEBUG: hpgltops has a PPD file!\n", stderr);
for (i = PPD->num_sizes, size = PPD->sizes; i > 0; i --, size ++)
if ((fabs(length - size->length) < 36.0 && size->width >= width) ||
(fabs(length - size->width) < 36.0 && size->length >= width))
break;
if (i == 0 && PPD->variable_sizes)
{
for (i = PPD->num_sizes, size = PPD->sizes; i > 0; i --, size ++)
if (strcasecmp(size->name, "custom") == 0)
break;
}
if (i > 0)
{
option = ppdFindOption(PPD, "PageSize");
choice = ppdFindChoice(option, size->name);
puts("%%BeginPageSetup");
printf("%%%%BeginFeature: PageSize %s\n", size->name);
if (strcasecmp(size->name, "custom") == 0)
{
PageLeft = PPD->custom_margins[0];
PageRight = width - PPD->custom_margins[2];
PageWidth = width;
PageBottom = PPD->custom_margins[1];
PageTop = length - PPD->custom_margins[3];
PageLength = length;
printf("%.0f %.0f 0 0 0\n", width, length);
if (choice->code == NULL)
{
puts("pop pop pop");
puts("<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n");
}
else
{
printf("%s\n", choice->code);
}
}
else
{
if (choice->code)
printf("%s\n", choice->code);
if (fabs(length - size->width) < 36.0)
{
PageLeft = size->bottom;
PageRight = size->top;
PageWidth = size->length;
PageBottom = size->left;
PageTop = size->right;
PageLength = size->width;
landscape = 1;
}
else
{
PageLeft = size->left;
PageRight = size->right;
PageWidth = size->width;
PageBottom = size->bottom;
PageTop = size->top;
PageLength = size->length;
}
}
puts("%%EndFeature");
puts("%%EndPageSetup");
}
}
else
{
fputs("DEBUG: hpgltops does not have a PPD file!\n", stderr);
puts("%%BeginPageSetup");
printf("%%%%BeginFeature: PageSize w%.0fh%.0f\n", width, length);
printf("<</PageSize[%.0f %.0f]/ImageBBox null>>setpagedevice\n",
width, length);
puts("%%EndFeature");
puts("%%EndPageSetup");
PageLeft = 0.0;
PageRight = width;
PageWidth = width;
PageBottom = 0.0;
PageTop = length;
PageLength = length;
}
}
printf("/SA {\n"
" /%s%s%s%s findfont\n"
" [ %f %f %f %f 0.0 0.0 ] makefont\n"
" setfont\n"
"} bind def\n",
AlternateFont.typeface == 48 ? "Courier" : "Helvetica",
(AlternateFont.weight != 0 || AlternateFont.posture != 0) ? "-" : "",
AlternateFont.weight != 0 ? "Bold" : "",
AlternateFont.posture != 0 ? "Oblique" : "",
AlternateFont.x * AlternateFont.height,
-AlternateFont.y * AlternateFont.height,
AlternateFont.y * AlternateFont.height,
AlternateFont.x * AlternateFont.height);
printf("/SS {\n"
" /%s%s%s%s findfont\n"
" [ %f %f %f %f 0.0 0.0 ] makefont\n"
" setfont\n"
"} bind def\n",
StandardFont.typeface == 48 ? "Courier" : "Helvetica",
(StandardFont.weight != 0 || StandardFont.posture != 0) ? "-" : "",
StandardFont.weight != 0 ? "Bold" : "",
StandardFont.posture != 0 ? "Oblique" : "",
StandardFont.x * StandardFont.height,
-StandardFont.y * StandardFont.height,
StandardFont.y * StandardFont.height,
StandardFont.x * StandardFont.height);
if (CharFont)
puts("SA");
else
puts("SS");
printf("%.1f setmiterlimit\n", MiterLimit);
printf("%d setlinecap\n", LineCap);
printf("%d setlinejoin\n", LineJoin);
printf("%.3f %.3f %.3f %.2f SP\n", Pens[1].rgb[0], Pens[1].rgb[1],
Pens[1].rgb[2], Pens[1].width * PenScaling);
puts("gsave");
if (Duplex && (PageCount & 1) == 0)
switch ((PageRotation / 90 + landscape) & 3)
{
case 0 :
printf("%.1f %.1f translate\n", PageWidth - PageRight, PageBottom);
break;
case 1 :
printf("%.0f 0 translate 90 rotate\n", PageLength);
printf("%.1f %.1f translate\n", PageLength - PageTop,
PageWidth - PageRight);
break;
case 2 :
printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength);
printf("%.1f %.1f translate\n", PageLeft, PageLength - PageTop);
break;
case 3 :
printf("0 %.0f translate -90 rotate\n", PageWidth);
printf("%.1f %.1f translate\n", PageBottom, PageLeft);
break;
}
else
switch ((PageRotation / 90 + landscape) & 3)
{
case 0 :
printf("%.1f %.1f translate\n", PageLeft, PageBottom);
break;
case 1 :
printf("%.0f 0 translate 90 rotate\n", PageLength);
printf("%.1f %.1f translate\n", PageBottom, PageWidth - PageRight);
break;
case 2 :
printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength);
printf("%.1f %.1f translate\n", PageWidth - PageRight,
PageLength - PageTop);
break;
case 3 :
printf("0 %.0f translate -90 rotate\n", PageWidth);
printf("%.1f %.1f translate\n", PageLength - PageTop, PageLeft);
break;
}
if (IW1[0] != IW2[0] && IW1[1] != IW2[1])
{
iw1[0] = IW1[0] * 72.0f / 1016.0f;
iw1[1] = IW1[1] * 72.0f / 1016.0f;
iw2[0] = IW2[0] * 72.0f / 1016.0f;
iw2[1] = IW2[1] * 72.0f / 1016.0f;
printf("initclip MP %.3f %.3f MO %.3f %.3f LI %.3f %.3f LI %.3f %.3f LI CP clip\n",
iw1[0], iw1[1], iw1[0], iw2[1], iw2[0], iw2[1], iw2[0], iw1[1]);
}
}
va_start(ap, format);
bytes = vprintf(format, ap);
va_end(ap);
return (bytes);
}