1 /*
2 * Common PCL functions for CUPS.
3 *
4 * Copyright 2007 by Apple Inc.
5 * Copyright 1993-2005 by Easy Software Products
6 *
7 * These coded instructions, statements, and computer programs are the
8 * property of Apple Inc. and are protected by Federal copyright
9 * law. Distribution and use rights are outlined in the file "COPYING"
10 * which should have been included with this file.
11 *
12 * Contents:
13 *
14 * pcl_set_media_size() - Set media size using the page size command.
15 * pjl_write() - Write a PJL command string, performing
16 * substitutions as needed.
17 */
18
19 /*
20 * Include necessary headers...
21 */
22
23 #include <cupsfilters/driver.h>
24 #include "pcl-common.h"
25 #include <math.h>
26
27
28 /*
29 * 'pcl_set_media_size()' - Set media size using the page size command.
30 */
31
32 void
pcl_set_media_size(ppd_file_t * ppd,float width,float length)33 pcl_set_media_size(ppd_file_t *ppd, /* I - PPD file */
34 float width, /* I - Width of page */
35 float length) /* I - Length of page */
36 {
37 float l;
38 int l_int;
39
40 if (width < length)
41 l = length;
42 else
43 l = width;
44 l_int = (int)(l + 0.5f);
45 fprintf (stderr, "DEBUG: Width: %f Length: %f Long Edge: %f\n",
46 width, length, l);
47
48 printf("\033&l0O"); /* Set portrait orientation */
49
50 if (!ppd || ppd->model_number & PCL_PAPER_SIZE)
51 {
52 if (l_int >= 418 && l_int <= 420) /* Postcard */
53 printf("\033&l71A"); /* Set page size */
54 else if (l_int >= 539 && l_int <= 541) /* Monarch Envelope */
55 printf("\033&l80A"); /* Set page size */
56 else if (l_int >= 566 && l_int <= 568) /* Double Postcard */
57 printf("\033&l72A"); /* Set page size */
58 else if (l_int >= 594 && l_int <= 596) /* A5 */
59 printf("\033&l25A"); /* Set page size */
60 else if (l_int >= 611 && l_int <= 613) /* Statement */
61 printf("\033&l5A"); /* Set page size */
62 else if (l_int >= 623 && l_int <= 625) /* DL Envelope */
63 printf("\033&l90A"); /* Set page size */
64 else if (l_int >= 648 && l_int <= 650) /* C5 Envelope */
65 printf("\033&l91A"); /* Set page size */
66 else if (l_int >= 683 && l_int <= 685) /* COM-10 Envelope */
67 printf("\033&l81A"); /* Set page size */
68 else if (l_int >= 708 && l_int <= 710) /* B5 Envelope */
69 printf("\033&l100A"); /* Set page size */
70 else if (l_int >= 728 && l_int <= 730) /* B5 */
71 printf("\033&l45A"); /* Set page size */
72 else if (l_int >= 755 && l_int <= 757) /* Executive */
73 printf("\033&l1A"); /* Set page size */
74 else if (l_int >= 791 && l_int <= 793) /* Letter */
75 printf("\033&l2A"); /* Set page size */
76 else if (l_int >= 841 && l_int <= 843) /* A4 */
77 printf("\033&l26A"); /* Set page size */
78 else if (l_int >= 935 && l_int <= 937) /* Foolscap */
79 printf("\033&l23A"); /* Set page size */
80 else if (l_int >= 1007 && l_int <= 1009) /* Legal */
81 printf("\033&l3A"); /* Set page size */
82 else if (l_int >= 1031 && l_int <= 1033) /* B4 */
83 printf("\033&l46A"); /* Set page size */
84 else if (l_int >= 1190 && l_int <= 1192) /* A3 */
85 printf("\033&l27A"); /* Set page size */
86 else if (l_int >= 1223 && l_int <= 1225) /* Tabloid */
87 printf("\033&l6A"); /* Set page size */
88 else
89 {
90 printf("\033&l101A"); /* Set page size */
91 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
92 printf("\033&l%.2fP", l / 12.0); /* Set page length */
93 printf("\033&l%.0fF", l / 12.0); /* Set text length to page */
94 }
95 #if 0
96 switch ((int)(l + 0.5f))
97 {
98 case 419 : /* Postcard */
99 printf("\033&l71A"); /* Set page size */
100 break;
101
102 case 540 : /* Monarch Envelope */
103 printf("\033&l80A"); /* Set page size */
104 break;
105
106 case 567 : /* Double Postcard */
107 printf("\033&l72A"); /* Set page size */
108 break;
109
110 case 595 : /* A5 */
111 printf("\033&l25A"); /* Set page size */
112 break;
113
114 case 612 : /* Statement */
115 printf("\033&l5A"); /* Set page size */
116 break;
117
118 case 624 : /* DL Envelope */
119 printf("\033&l90A"); /* Set page size */
120 break;
121
122 case 649 : /* C5 Envelope */
123 printf("\033&l91A"); /* Set page size */
124 break;
125
126 case 684 : /* COM-10 Envelope */
127 printf("\033&l81A"); /* Set page size */
128 break;
129
130 case 709 : /* B5 Envelope */
131 printf("\033&l100A"); /* Set page size */
132 break;
133
134 case 729 : /* B5 */
135 printf("\033&l45A"); /* Set page size */
136 break;
137
138 case 756 : /* Executive */
139 printf("\033&l1A"); /* Set page size */
140 break;
141
142 case 792 : /* Letter */
143 printf("\033&l2A"); /* Set page size */
144 break;
145
146 case 842 : /* A4 */
147 printf("\033&l26A"); /* Set page size */
148 break;
149
150 case 936 : /* Foolscap */
151 printf("\033&l23A"); /* Set page size */
152 break;
153
154 case 1008 : /* Legal */
155 printf("\033&l3A"); /* Set page size */
156 break;
157
158 case 1032 : /* B4 */
159 printf("\033&l46A"); /* Set page size */
160 break;
161
162 case 1191 : /* A3 */
163 printf("\033&l27A"); /* Set page size */
164 break;
165
166 case 1224 : /* Tabloid */
167 printf("\033&l6A"); /* Set page size */
168 break;
169
170 default :
171 printf("\033&l101A"); /* Set page size */
172 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
173 printf("\033&l%.2fP", l / 12.0);
174 /* Set page length */
175 printf("\033&l%.0fF", l / 12.0);
176 /* Set text length to page */
177 break;
178 }
179 #endif
180 }
181 else
182 {
183 printf("\033&l6D\033&k12H"); /* Set 6 LPI, 10 CPI */
184 printf("\033&l%.2fP", l / 12.0);
185 /* Set page length */
186 printf("\033&l%.0fF", l / 12.0);
187 /* Set text length to page */
188 }
189
190 printf("\033&l0L"); /* Turn off perforation skip */
191 printf("\033&l0E"); /* Reset top margin to 0 */
192 }
193
194
195 /*
196 * 'pjl_write()' - Write a PJL command string, performing substitutions as needed.
197 */
198
199 void
pjl_write(const char * format,const char * value,int job_id,const char * user,const char * title,int num_options,cups_option_t * options)200 pjl_write(const char *format, /* I - Format string */
201 const char *value, /* I - Value for %s */
202 int job_id, /* I - Job ID */
203 const char *user, /* I - Username */
204 const char *title, /* I - Title */
205 int num_options, /* I - Number of options */
206 cups_option_t *options) /* I - Options */
207 {
208 const char *optval; /* Option value */
209 char match[255], /* Match string */
210 *mptr; /* Pointer into match string */
211
212
213 if (!format)
214 return;
215
216 while (*format)
217 {
218 if (*format == '%')
219 {
220 /*
221 * Perform substitution...
222 */
223
224 format ++;
225 switch (*format)
226 {
227 case 'b' : /* job-billing */
228 if ((optval = cupsGetOption("job-billing", num_options,
229 options)) != NULL)
230 fputs(optval, stdout);
231 break;
232
233 case 'h' : /* job-originating-host-name */
234 if ((optval = cupsGetOption("job-originating-host-name",
235 num_options, options)) != NULL)
236 fputs(optval, stdout);
237 break;
238
239 case 'j' : /* job-id */
240 printf("%d", job_id);
241 break;
242
243 case 'n' : /* CR + LF */
244 putchar('\r');
245 putchar('\n');
246 break;
247
248 case 'q' : /* double quote (") */
249 putchar('\"');
250 break;
251
252 case 's' : /* "value" */
253 if (value)
254 fputs(value, stdout);
255 break;
256
257 case 't' : /* job-name */
258 fputs(title, stdout);
259 break;
260
261 case 'u' : /* job-originating-user-name */
262 fputs(user, stdout);
263 break;
264
265 case '?' : /* ?value:string; */
266 /*
267 * Get the match value...
268 */
269
270 for (format ++, mptr = match; *format && *format != ':'; format ++)
271 if (mptr < (match + sizeof(match) - 1))
272 *mptr++ = *format;
273
274 if (!*format)
275 return;
276
277 /*
278 * See if we have a match...
279 */
280
281 format ++;
282 *mptr = '\0';
283
284 if (!value || strcmp(match, value))
285 {
286 /*
287 * Value doesn't match; skip the string that follows...
288 */
289
290 while (*format && *format != ';')
291 format ++;
292 }
293 else
294 {
295 /*
296 * Value matches; copy the string that follows...
297 */
298
299 while (*format && *format != ';')
300 putchar(*format++);
301 }
302
303 if (!*format)
304 return;
305 break;
306
307 default : /* Anything else */
308 putchar('%');
309 case '%' : /* %% = single % */
310 putchar(*format);
311 break;
312 }
313 }
314 else
315 putchar(*format);
316
317 format ++;
318 }
319 }
320
321