]> git.ipfire.org Git - thirdparty/cups.git/blob - filter/hpgl-prolog.c
Remove svn:keywords since they cause svn_load_dirs.pl to complain about every file.
[thirdparty/cups.git] / filter / hpgl-prolog.c
1 /*
2 * "$Id: hpgl-prolog.c 177 2006-06-21 00:20:03Z jlovell $"
3 *
4 * HP-GL/2 prolog routines for for the Common UNIX Printing System (CUPS).
5 *
6 * Copyright 1993-2005 by Easy Software Products.
7 *
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
13 * at:
14 *
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
19 *
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
23 *
24 * This file is subject to the Apple OS-Developed Software exception.
25 *
26 * Contents:
27 *
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...
32 */
33
34 /*
35 * Include necessary headers...
36 */
37
38 #include "hpgltops.h"
39 #include <stdarg.h>
40
41
42 /*
43 * 'OutputProlog()' - Output the PostScript prolog...
44 */
45
46 void
47 OutputProlog(char *title, /* I - Job title */
48 char *user, /* I - Username */
49 int shading) /* I - Type of shading */
50 {
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 */
57
58
59 curtime = time(NULL);
60 curtm = localtime(&curtime);
61
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");
83
84 if ((datadir = getenv("CUPS_DATADIR")) == NULL)
85 datadir = CUPS_DATADIR;
86
87 snprintf(filename, sizeof(filename), "%s/data/HPGLprolog", datadir);
88
89 if ((prolog = fopen(filename, "r")) == NULL)
90 {
91 fprintf(stderr, "ERROR: Unable to open HPGL prolog \"%s\" for reading - %s\n",
92 filename, strerror(errno));
93 exit(1);
94 }
95
96 while (fgets(line, sizeof(line), prolog) != NULL)
97 fputs(line, stdout);
98
99 fclose(prolog);
100
101 puts("%%EndProlog");
102
103 IN_initialize(0, NULL);
104 }
105
106
107 /*
108 * 'OutputTrailer()' - Output the PostScript trailer...
109 */
110
111 void
112 OutputTrailer(void)
113 {
114 if (PageDirty)
115 PG_advance_page(0, NULL);
116
117 puts("%%Trailer");
118 printf("%%%%Pages: %d\n", PageCount);
119 puts("%%EOF");
120 }
121
122
123 /*
124 * 'Outputf()' - Write a formatted string to the output file, creating the
125 * page header as needed...
126 */
127
128 int /* O - Number of bytes written */
129 Outputf(const char *format, /* I - Printf-style string */
130 ...) /* I - Additional args as needed */
131 {
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? */
141
142
143 /*
144 * Write the page header as needed...
145 */
146
147 if (!PageDirty)
148 {
149 PageDirty = 1;
150 PageCount ++;
151
152 printf("%%%%Page: %d %d\n", PageCount, PageCount);
153
154 landscape = 0;
155
156 if (!FitPlot && PlotSizeSet)
157 {
158 /*
159 * Set the page size for this page...
160 */
161
162 if (PageRotation == 0 || PageRotation == 180)
163 {
164 width = PlotSize[0];
165 length = PlotSize[1];
166 }
167 else
168 {
169 width = PlotSize[1];
170 length = PlotSize[0];
171 }
172
173 fprintf(stderr, "DEBUG: hpgltops setting page size (%.0f x %.0f)\n",
174 width, length);
175
176 if (PPD != NULL)
177 {
178 fputs("DEBUG: hpgltops has a PPD file!\n", stderr);
179
180 /*
181 * Lookup the closest PageSize and set it...
182 */
183
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))
187 break;
188
189 if (i == 0 && PPD->variable_sizes)
190 {
191 for (i = PPD->num_sizes, size = PPD->sizes; i > 0; i --, size ++)
192 if (strcasecmp(size->name, "custom") == 0)
193 break;
194 }
195
196 if (i > 0)
197 {
198 /*
199 * Found a matching size...
200 */
201
202 option = ppdFindOption(PPD, "PageSize");
203 choice = ppdFindChoice(option, size->name);
204
205 puts("%%BeginPageSetup");
206 printf("%%%%BeginFeature: PageSize %s\n", size->name);
207
208 if (strcasecmp(size->name, "custom") == 0)
209 {
210 PageLeft = PPD->custom_margins[0];
211 PageRight = width - PPD->custom_margins[2];
212 PageWidth = width;
213 PageBottom = PPD->custom_margins[1];
214 PageTop = length - PPD->custom_margins[3];
215 PageLength = length;
216
217 printf("%.0f %.0f 0 0 0\n", width, length);
218
219 if (choice->code == NULL)
220 {
221 /*
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...
225 */
226
227 puts("pop pop pop");
228 puts("<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n");
229 }
230 else
231 {
232 /*
233 * Use the vendor-supplied command...
234 */
235
236 printf("%s\n", choice->code);
237 }
238 }
239 else
240 {
241 if (choice->code)
242 printf("%s\n", choice->code);
243
244 if (fabs(length - size->width) < 36.0)
245 {
246 /*
247 * Do landscape orientation...
248 */
249
250 PageLeft = size->bottom;
251 PageRight = size->top;
252 PageWidth = size->length;
253 PageBottom = size->left;
254 PageTop = size->right;
255 PageLength = size->width;
256
257 landscape = 1;
258 }
259 else
260 {
261 /*
262 * Do portrait orientation...
263 */
264
265 PageLeft = size->left;
266 PageRight = size->right;
267 PageWidth = size->width;
268 PageBottom = size->bottom;
269 PageTop = size->top;
270 PageLength = size->length;
271 }
272 }
273
274 puts("%%EndFeature");
275 puts("%%EndPageSetup");
276 }
277 }
278 else
279 {
280 fputs("DEBUG: hpgltops does not have a PPD file!\n", stderr);
281
282 puts("%%BeginPageSetup");
283 printf("%%%%BeginFeature: PageSize w%.0fh%.0f\n", width, length);
284 printf("<</PageSize[%.0f %.0f]/ImageBBox null>>setpagedevice\n",
285 width, length);
286 puts("%%EndFeature");
287 puts("%%EndPageSetup");
288
289 PageLeft = 0.0;
290 PageRight = width;
291 PageWidth = width;
292 PageBottom = 0.0;
293 PageTop = length;
294 PageLength = length;
295 }
296 }
297
298 define_font(0);
299 define_font(1);
300
301 printf("%.1f setmiterlimit\n", MiterLimit);
302 printf("%d setlinecap\n", LineCap);
303 printf("%d setlinejoin\n", LineJoin);
304
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);
307
308 puts("gsave");
309
310 if (Duplex && (PageCount & 1) == 0)
311 switch ((PageRotation / 90 + landscape) & 3)
312 {
313 case 0 :
314 printf("%.1f %.1f translate\n", PageWidth - PageRight, PageBottom);
315 break;
316 case 1 :
317 printf("%.0f 0 translate 90 rotate\n", PageLength);
318 printf("%.1f %.1f translate\n", PageLength - PageTop,
319 PageWidth - PageRight);
320 break;
321 case 2 :
322 printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength);
323 printf("%.1f %.1f translate\n", PageLeft, PageLength - PageTop);
324 break;
325 case 3 :
326 printf("0 %.0f translate -90 rotate\n", PageWidth);
327 printf("%.1f %.1f translate\n", PageBottom, PageLeft);
328 break;
329 }
330 else
331 switch ((PageRotation / 90 + landscape) & 3)
332 {
333 case 0 :
334 printf("%.1f %.1f translate\n", PageLeft, PageBottom);
335 break;
336 case 1 :
337 printf("%.0f 0 translate 90 rotate\n", PageLength);
338 printf("%.1f %.1f translate\n", PageBottom, PageWidth - PageRight);
339 break;
340 case 2 :
341 printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength);
342 printf("%.1f %.1f translate\n", PageWidth - PageRight,
343 PageLength - PageTop);
344 break;
345 case 3 :
346 printf("0 %.0f translate -90 rotate\n", PageWidth);
347 printf("%.1f %.1f translate\n", PageLength - PageTop, PageLeft);
348 break;
349 }
350
351 if (IW1[0] != IW2[0] && IW1[1] != IW2[1])
352 {
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;
357
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]);
360 }
361 }
362
363 /*
364 * Write the string to the output file...
365 */
366
367 va_start(ap, format);
368 bytes = vprintf(format, ap);
369 va_end(ap);
370
371 return (bytes);
372 }
373
374
375 /*
376 * End of "$Id: hpgl-prolog.c 177 2006-06-21 00:20:03Z jlovell $".
377 */