]>
Commit | Line | Data |
---|---|---|
ef416fc2 | 1 | /* |
c0e1af83 | 2 | * "$Id: hpgl-prolog.c 6420 2007-03-30 20:00:59Z mike $" |
ef416fc2 | 3 | * |
4 | * HP-GL/2 prolog routines for for the Common UNIX Printing System (CUPS). | |
5 | * | |
c0e1af83 | 6 | * Copyright 1993-2007 by Easy Software Products. |
ef416fc2 | 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); | |
2abf387c | 73 | WriteTextComment("Title", title); |
74 | WriteTextComment("For", user); | |
ef416fc2 | 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 | { | |
c0e1af83 | 91 | fprintf(stderr, |
92 | "DEBUG: Unable to open HPGL prolog \"%s\" for reading - %s\n", | |
ef416fc2 | 93 | filename, strerror(errno)); |
94 | exit(1); | |
95 | } | |
96 | ||
97 | while (fgets(line, sizeof(line), prolog) != NULL) | |
98 | fputs(line, stdout); | |
99 | ||
100 | fclose(prolog); | |
101 | ||
102 | puts("%%EndProlog"); | |
103 | ||
104 | IN_initialize(0, NULL); | |
105 | } | |
106 | ||
107 | ||
108 | /* | |
109 | * 'OutputTrailer()' - Output the PostScript trailer... | |
110 | */ | |
111 | ||
112 | void | |
113 | OutputTrailer(void) | |
114 | { | |
115 | if (PageDirty) | |
116 | PG_advance_page(0, NULL); | |
117 | ||
118 | puts("%%Trailer"); | |
119 | printf("%%%%Pages: %d\n", PageCount); | |
120 | puts("%%EOF"); | |
121 | } | |
122 | ||
123 | ||
124 | /* | |
125 | * 'Outputf()' - Write a formatted string to the output file, creating the | |
126 | * page header as needed... | |
127 | */ | |
128 | ||
129 | int /* O - Number of bytes written */ | |
130 | Outputf(const char *format, /* I - Printf-style string */ | |
131 | ...) /* I - Additional args as needed */ | |
132 | { | |
133 | va_list ap; /* Argument pointer */ | |
134 | int bytes; /* Number of bytes written */ | |
135 | float iw1[2], iw2[2]; /* Clipping window */ | |
136 | int i; /* Looping var */ | |
137 | ppd_size_t *size; /* Page size */ | |
138 | ppd_option_t *option; /* Page size option */ | |
139 | ppd_choice_t *choice; /* Page size choice */ | |
140 | float width, length; /* Page dimensions */ | |
141 | int landscape; /* Rotate for landscape orientation? */ | |
142 | ||
143 | ||
144 | /* | |
145 | * Write the page header as needed... | |
146 | */ | |
147 | ||
148 | if (!PageDirty) | |
149 | { | |
150 | PageDirty = 1; | |
151 | PageCount ++; | |
152 | ||
153 | printf("%%%%Page: %d %d\n", PageCount, PageCount); | |
154 | ||
155 | landscape = 0; | |
156 | ||
157 | if (!FitPlot && PlotSizeSet) | |
158 | { | |
159 | /* | |
160 | * Set the page size for this page... | |
161 | */ | |
162 | ||
163 | if (PageRotation == 0 || PageRotation == 180) | |
164 | { | |
165 | width = PlotSize[0]; | |
166 | length = PlotSize[1]; | |
167 | } | |
168 | else | |
169 | { | |
170 | width = PlotSize[1]; | |
171 | length = PlotSize[0]; | |
172 | } | |
173 | ||
174 | fprintf(stderr, "DEBUG: hpgltops setting page size (%.0f x %.0f)\n", | |
175 | width, length); | |
176 | ||
177 | if (PPD != NULL) | |
178 | { | |
179 | fputs("DEBUG: hpgltops has a PPD file!\n", stderr); | |
180 | ||
181 | /* | |
182 | * Lookup the closest PageSize and set it... | |
183 | */ | |
184 | ||
185 | for (i = PPD->num_sizes, size = PPD->sizes; i > 0; i --, size ++) | |
186 | if ((fabs(length - size->length) < 36.0 && size->width >= width) || | |
187 | (fabs(length - size->width) < 36.0 && size->length >= width)) | |
188 | break; | |
189 | ||
190 | if (i == 0 && PPD->variable_sizes) | |
191 | { | |
192 | for (i = PPD->num_sizes, size = PPD->sizes; i > 0; i --, size ++) | |
193 | if (strcasecmp(size->name, "custom") == 0) | |
194 | break; | |
195 | } | |
196 | ||
197 | if (i > 0) | |
198 | { | |
199 | /* | |
200 | * Found a matching size... | |
201 | */ | |
202 | ||
203 | option = ppdFindOption(PPD, "PageSize"); | |
204 | choice = ppdFindChoice(option, size->name); | |
205 | ||
206 | puts("%%BeginPageSetup"); | |
207 | printf("%%%%BeginFeature: PageSize %s\n", size->name); | |
208 | ||
209 | if (strcasecmp(size->name, "custom") == 0) | |
210 | { | |
211 | PageLeft = PPD->custom_margins[0]; | |
212 | PageRight = width - PPD->custom_margins[2]; | |
213 | PageWidth = width; | |
214 | PageBottom = PPD->custom_margins[1]; | |
215 | PageTop = length - PPD->custom_margins[3]; | |
216 | PageLength = length; | |
217 | ||
218 | printf("%.0f %.0f 0 0 0\n", width, length); | |
219 | ||
220 | if (choice->code == NULL) | |
221 | { | |
222 | /* | |
223 | * This can happen with certain buggy PPD files that don't include | |
224 | * a CustomPageSize command sequence... We just use a generic | |
225 | * Level 2 command sequence... | |
226 | */ | |
227 | ||
228 | puts("pop pop pop"); | |
229 | puts("<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n"); | |
230 | } | |
231 | else | |
232 | { | |
233 | /* | |
234 | * Use the vendor-supplied command... | |
235 | */ | |
236 | ||
237 | printf("%s\n", choice->code); | |
238 | } | |
239 | } | |
240 | else | |
241 | { | |
242 | if (choice->code) | |
243 | printf("%s\n", choice->code); | |
244 | ||
245 | if (fabs(length - size->width) < 36.0) | |
246 | { | |
247 | /* | |
248 | * Do landscape orientation... | |
249 | */ | |
250 | ||
251 | PageLeft = size->bottom; | |
252 | PageRight = size->top; | |
253 | PageWidth = size->length; | |
254 | PageBottom = size->left; | |
255 | PageTop = size->right; | |
256 | PageLength = size->width; | |
257 | ||
258 | landscape = 1; | |
259 | } | |
260 | else | |
261 | { | |
262 | /* | |
263 | * Do portrait orientation... | |
264 | */ | |
265 | ||
266 | PageLeft = size->left; | |
267 | PageRight = size->right; | |
268 | PageWidth = size->width; | |
269 | PageBottom = size->bottom; | |
270 | PageTop = size->top; | |
271 | PageLength = size->length; | |
272 | } | |
273 | } | |
274 | ||
275 | puts("%%EndFeature"); | |
276 | puts("%%EndPageSetup"); | |
277 | } | |
278 | } | |
279 | else | |
280 | { | |
281 | fputs("DEBUG: hpgltops does not have a PPD file!\n", stderr); | |
282 | ||
283 | puts("%%BeginPageSetup"); | |
284 | printf("%%%%BeginFeature: PageSize w%.0fh%.0f\n", width, length); | |
285 | printf("<</PageSize[%.0f %.0f]/ImageBBox null>>setpagedevice\n", | |
286 | width, length); | |
287 | puts("%%EndFeature"); | |
288 | puts("%%EndPageSetup"); | |
289 | ||
290 | PageLeft = 0.0; | |
291 | PageRight = width; | |
292 | PageWidth = width; | |
293 | PageBottom = 0.0; | |
294 | PageTop = length; | |
295 | PageLength = length; | |
296 | } | |
297 | } | |
298 | ||
299 | define_font(0); | |
300 | define_font(1); | |
301 | ||
302 | printf("%.1f setmiterlimit\n", MiterLimit); | |
303 | printf("%d setlinecap\n", LineCap); | |
304 | printf("%d setlinejoin\n", LineJoin); | |
305 | ||
306 | printf("%.3f %.3f %.3f %.2f SP\n", Pens[1].rgb[0], Pens[1].rgb[1], | |
307 | Pens[1].rgb[2], Pens[1].width * PenScaling); | |
308 | ||
309 | puts("gsave"); | |
310 | ||
311 | if (Duplex && (PageCount & 1) == 0) | |
312 | switch ((PageRotation / 90 + landscape) & 3) | |
313 | { | |
314 | case 0 : | |
315 | printf("%.1f %.1f translate\n", PageWidth - PageRight, PageBottom); | |
316 | break; | |
317 | case 1 : | |
318 | printf("%.0f 0 translate 90 rotate\n", PageLength); | |
319 | printf("%.1f %.1f translate\n", PageLength - PageTop, | |
320 | PageWidth - PageRight); | |
321 | break; | |
322 | case 2 : | |
323 | printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength); | |
324 | printf("%.1f %.1f translate\n", PageLeft, PageLength - PageTop); | |
325 | break; | |
326 | case 3 : | |
327 | printf("0 %.0f translate -90 rotate\n", PageWidth); | |
328 | printf("%.1f %.1f translate\n", PageBottom, PageLeft); | |
329 | break; | |
330 | } | |
331 | else | |
332 | switch ((PageRotation / 90 + landscape) & 3) | |
333 | { | |
334 | case 0 : | |
335 | printf("%.1f %.1f translate\n", PageLeft, PageBottom); | |
336 | break; | |
337 | case 1 : | |
338 | printf("%.0f 0 translate 90 rotate\n", PageLength); | |
339 | printf("%.1f %.1f translate\n", PageBottom, PageWidth - PageRight); | |
340 | break; | |
341 | case 2 : | |
342 | printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength); | |
343 | printf("%.1f %.1f translate\n", PageWidth - PageRight, | |
344 | PageLength - PageTop); | |
345 | break; | |
346 | case 3 : | |
347 | printf("0 %.0f translate -90 rotate\n", PageWidth); | |
348 | printf("%.1f %.1f translate\n", PageLength - PageTop, PageLeft); | |
349 | break; | |
350 | } | |
351 | ||
352 | if (IW1[0] != IW2[0] && IW1[1] != IW2[1]) | |
353 | { | |
354 | iw1[0] = IW1[0] * 72.0f / 1016.0f; | |
355 | iw1[1] = IW1[1] * 72.0f / 1016.0f; | |
356 | iw2[0] = IW2[0] * 72.0f / 1016.0f; | |
357 | iw2[1] = IW2[1] * 72.0f / 1016.0f; | |
358 | ||
359 | printf("initclip MP %.3f %.3f MO %.3f %.3f LI %.3f %.3f LI %.3f %.3f LI CP clip\n", | |
360 | iw1[0], iw1[1], iw1[0], iw2[1], iw2[0], iw2[1], iw2[0], iw1[1]); | |
361 | } | |
362 | } | |
363 | ||
364 | /* | |
365 | * Write the string to the output file... | |
366 | */ | |
367 | ||
368 | va_start(ap, format); | |
369 | bytes = vprintf(format, ap); | |
370 | va_end(ap); | |
371 | ||
372 | return (bytes); | |
373 | } | |
374 | ||
375 | ||
376 | /* | |
c0e1af83 | 377 | * End of "$Id: hpgl-prolog.c 6420 2007-03-30 20:00:59Z mike $". |
ef416fc2 | 378 | */ |