]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/hpgl-prolog.c
4 * HP-GL/2 prolog routines for for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2005 by Easy Software Products.
8 * These coded instructions, statements, and computer programs are the
9 * property of Easy Software Products and are protected by Federal
10 * copyright law. Distribution and use rights are outlined in the file
11 * "LICENSE.txt" which should have been included with this file. If this
12 * file is missing or damaged please contact Easy Software Products
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * OutputProlog() - Output the PostScript prolog...
29 * OutputTrailer() - Output the PostScript trailer...
30 * Outputf() - Write a formatted string to the output file, creating the
31 * page header as needed...
35 * Include necessary headers...
43 * 'OutputProlog()' - Output the PostScript prolog...
47 OutputProlog(char *title
, /* I - Job title */
48 char *user
, /* I - Username */
49 int shading
) /* I - Type of shading */
51 FILE *prolog
; /* Prolog file */
52 char line
[255]; /* Line from prolog file */
53 const char *datadir
; /* CUPS_DATADIR environment variable */
54 char filename
[1024]; /* Name of prolog file */
55 time_t curtime
; /* Current time */
56 struct tm
*curtm
; /* Current date */
60 curtm
= localtime(&curtime
);
62 puts("%!PS-Adobe-3.0");
63 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
64 PageLeft
, PageBottom
, PageRight
, PageTop
);
65 puts("%%Pages: (atend)");
66 printf("%%%%LanguageLevel: %d\n", LanguageLevel
);
67 puts("%%DocumentData: Clean7Bit");
68 puts("%%DocumentSuppliedResources: procset hpgltops 1.1 0");
69 puts("%%DocumentNeededResources: font Courier Helvetica");
70 puts("%%Creator: hpgltops/" CUPS_SVERSION
);
71 strftime(line
, sizeof(line
), "%c", curtm
);
72 printf("%%%%CreationDate: %s\n", line
);
73 printf("%%%%Title: %s\n", title
);
74 printf("%%%%For: %s\n", user
);
75 printf("%%cupsRotation: %d\n", (Orientation
& 3) * 90);
76 puts("%%EndComments");
77 puts("%%BeginProlog");
78 printf("/DefaultPenWidth %.2f def\n", PenWidth
* 72.0 / 25.4);
79 if (!shading
) /* Black only */
80 puts("/setrgbcolor { pop pop pop } bind def");
81 else if (!ColorDevice
) /* Greyscale */
82 puts("/setrgbcolor { 0.08 mul exch 0.61 mul add exch 0.31 mul add setgray } bind def\n");
84 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
85 datadir
= CUPS_DATADIR
;
87 snprintf(filename
, sizeof(filename
), "%s/data/HPGLprolog", datadir
);
89 if ((prolog
= fopen(filename
, "r")) == NULL
)
91 fprintf(stderr
, "ERROR: Unable to open HPGL prolog \"%s\" for reading - %s\n",
92 filename
, strerror(errno
));
96 while (fgets(line
, sizeof(line
), prolog
) != NULL
)
103 IN_initialize(0, NULL
);
108 * 'OutputTrailer()' - Output the PostScript trailer...
115 PG_advance_page(0, NULL
);
118 printf("%%%%Pages: %d\n", PageCount
);
124 * 'Outputf()' - Write a formatted string to the output file, creating the
125 * page header as needed...
128 int /* O - Number of bytes written */
129 Outputf(const char *format
, /* I - Printf-style string */
130 ...) /* I - Additional args as needed */
132 va_list ap
; /* Argument pointer */
133 int bytes
; /* Number of bytes written */
134 float iw1
[2], iw2
[2]; /* Clipping window */
135 int i
; /* Looping var */
136 ppd_size_t
*size
; /* Page size */
137 ppd_option_t
*option
; /* Page size option */
138 ppd_choice_t
*choice
; /* Page size choice */
139 float width
, length
; /* Page dimensions */
140 int landscape
; /* Rotate for landscape orientation? */
144 * Write the page header as needed...
152 printf("%%%%Page: %d %d\n", PageCount
, PageCount
);
156 if (!FitPlot
&& PlotSizeSet
)
159 * Set the page size for this page...
162 if (PageRotation
== 0 || PageRotation
== 180)
165 length
= PlotSize
[1];
170 length
= PlotSize
[0];
173 fprintf(stderr
, "DEBUG: hpgltops setting page size (%.0f x %.0f)\n",
178 fputs("DEBUG: hpgltops has a PPD file!\n", stderr
);
181 * Lookup the closest PageSize and set it...
184 for (i
= PPD
->num_sizes
, size
= PPD
->sizes
; i
> 0; i
--, size
++)
185 if ((fabs(length
- size
->length
) < 36.0 && size
->width
>= width
) ||
186 (fabs(length
- size
->width
) < 36.0 && size
->length
>= width
))
189 if (i
== 0 && PPD
->variable_sizes
)
191 for (i
= PPD
->num_sizes
, size
= PPD
->sizes
; i
> 0; i
--, size
++)
192 if (strcasecmp(size
->name
, "custom") == 0)
199 * Found a matching size...
202 option
= ppdFindOption(PPD
, "PageSize");
203 choice
= ppdFindChoice(option
, size
->name
);
205 puts("%%BeginPageSetup");
206 printf("%%%%BeginFeature: PageSize %s\n", size
->name
);
208 if (strcasecmp(size
->name
, "custom") == 0)
210 PageLeft
= PPD
->custom_margins
[0];
211 PageRight
= width
- PPD
->custom_margins
[2];
213 PageBottom
= PPD
->custom_margins
[1];
214 PageTop
= length
- PPD
->custom_margins
[3];
217 printf("%.0f %.0f 0 0 0\n", width
, length
);
219 if (choice
->code
== NULL
)
222 * This can happen with certain buggy PPD files that don't include
223 * a CustomPageSize command sequence... We just use a generic
224 * Level 2 command sequence...
228 puts("<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n");
233 * Use the vendor-supplied command...
236 printf("%s\n", choice
->code
);
242 printf("%s\n", choice
->code
);
244 if (fabs(length
- size
->width
) < 36.0)
247 * Do landscape orientation...
250 PageLeft
= size
->bottom
;
251 PageRight
= size
->top
;
252 PageWidth
= size
->length
;
253 PageBottom
= size
->left
;
254 PageTop
= size
->right
;
255 PageLength
= size
->width
;
262 * Do portrait orientation...
265 PageLeft
= size
->left
;
266 PageRight
= size
->right
;
267 PageWidth
= size
->width
;
268 PageBottom
= size
->bottom
;
270 PageLength
= size
->length
;
274 puts("%%EndFeature");
275 puts("%%EndPageSetup");
280 fputs("DEBUG: hpgltops does not have a PPD file!\n", stderr
);
282 puts("%%BeginPageSetup");
283 printf("%%%%BeginFeature: PageSize w%.0fh%.0f\n", width
, length
);
284 printf("<</PageSize[%.0f %.0f]/ImageBBox null>>setpagedevice\n",
286 puts("%%EndFeature");
287 puts("%%EndPageSetup");
301 printf("%.1f setmiterlimit\n", MiterLimit
);
302 printf("%d setlinecap\n", LineCap
);
303 printf("%d setlinejoin\n", LineJoin
);
305 printf("%.3f %.3f %.3f %.2f SP\n", Pens
[1].rgb
[0], Pens
[1].rgb
[1],
306 Pens
[1].rgb
[2], Pens
[1].width
* PenScaling
);
310 if (Duplex
&& (PageCount
& 1) == 0)
311 switch ((PageRotation
/ 90 + landscape
) & 3)
314 printf("%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
317 printf("%.0f 0 translate 90 rotate\n", PageLength
);
318 printf("%.1f %.1f translate\n", PageLength
- PageTop
,
319 PageWidth
- PageRight
);
322 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
323 printf("%.1f %.1f translate\n", PageLeft
, PageLength
- PageTop
);
326 printf("0 %.0f translate -90 rotate\n", PageWidth
);
327 printf("%.1f %.1f translate\n", PageBottom
, PageLeft
);
331 switch ((PageRotation
/ 90 + landscape
) & 3)
334 printf("%.1f %.1f translate\n", PageLeft
, PageBottom
);
337 printf("%.0f 0 translate 90 rotate\n", PageLength
);
338 printf("%.1f %.1f translate\n", PageBottom
, PageWidth
- PageRight
);
341 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
342 printf("%.1f %.1f translate\n", PageWidth
- PageRight
,
343 PageLength
- PageTop
);
346 printf("0 %.0f translate -90 rotate\n", PageWidth
);
347 printf("%.1f %.1f translate\n", PageLength
- PageTop
, PageLeft
);
351 if (IW1
[0] != IW2
[0] && IW1
[1] != IW2
[1])
353 iw1
[0] = IW1
[0] * 72.0f
/ 1016.0f
;
354 iw1
[1] = IW1
[1] * 72.0f
/ 1016.0f
;
355 iw2
[0] = IW2
[0] * 72.0f
/ 1016.0f
;
356 iw2
[1] = IW2
[1] * 72.0f
/ 1016.0f
;
358 printf("initclip MP %.3f %.3f MO %.3f %.3f LI %.3f %.3f LI %.3f %.3f LI CP clip\n",
359 iw1
[0], iw1
[1], iw1
[0], iw2
[1], iw2
[0], iw2
[1], iw2
[0], iw1
[1]);
364 * Write the string to the output file...
367 va_start(ap
, format
);
368 bytes
= vprintf(format
, ap
);