]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/common.c
2 * Common filter routines for CUPS.
4 * Copyright 2007-2014 by Apple Inc.
5 * Copyright 1997-2006 by Easy Software Products.
7 * Licensed under Apache License v2.0. See the file "LICENSE" for more information.
11 * Include necessary headers...
22 int Orientation
= 0, /* 0 = portrait, 1 = landscape, etc. */
23 Duplex
= 0, /* Duplexed? */
24 LanguageLevel
= 1, /* Language level of printer */
25 ColorDevice
= 1; /* Do color text? */
26 float PageLeft
= 18.0f
, /* Left margin */
27 PageRight
= 594.0f
, /* Right margin */
28 PageBottom
= 36.0f
, /* Bottom margin */
29 PageTop
= 756.0f
, /* Top margin */
30 PageWidth
= 612.0f
, /* Total page width */
31 PageLength
= 792.0f
; /* Total page length */
35 * 'SetCommonOptions()' - Set common filter options for media size, etc.
38 ppd_file_t
* /* O - PPD file */
40 int num_options
, /* I - Number of options */
41 cups_option_t
*options
, /* I - Options */
42 int change_size
) /* I - Change page size? */
44 ppd_file_t
*ppd
; /* PPD file */
45 ppd_size_t
*pagesize
; /* Current page size */
46 const char *val
; /* Option value */
50 setlocale(LC_TIME
, "");
53 ppd
= ppdOpenFile(getenv("PPD"));
56 cupsMarkOptions(ppd
, num_options
, options
);
58 if ((pagesize
= ppdPageSize(ppd
, NULL
)) != NULL
)
60 PageWidth
= pagesize
->width
;
61 PageLength
= pagesize
->length
;
62 PageTop
= pagesize
->top
;
63 PageBottom
= pagesize
->bottom
;
64 PageLeft
= pagesize
->left
;
65 PageRight
= pagesize
->right
;
67 fprintf(stderr
, "DEBUG: Page = %.0fx%.0f; %.0f,%.0f to %.0f,%.0f\n",
68 PageWidth
, PageLength
, PageLeft
, PageBottom
, PageRight
, PageTop
);
73 ColorDevice
= ppd
->color_device
;
74 LanguageLevel
= ppd
->language_level
;
77 if ((val
= cupsGetOption("landscape", num_options
, options
)) != NULL
)
79 if (_cups_strcasecmp(val
, "no") != 0 && _cups_strcasecmp(val
, "off") != 0 &&
80 _cups_strcasecmp(val
, "false") != 0)
82 if (ppd
&& ppd
->landscape
> 0)
88 else if ((val
= cupsGetOption("orientation-requested", num_options
, options
)) != NULL
)
91 * Map IPP orientation values to 0 to 3:
99 Orientation
= atoi(val
) - 3;
100 if (Orientation
>= 2)
104 if ((val
= cupsGetOption("page-left", num_options
, options
)) != NULL
)
106 switch (Orientation
& 3)
109 PageLeft
= (float)atof(val
);
112 PageBottom
= (float)atof(val
);
115 PageRight
= PageWidth
- (float)atof(val
);
118 PageTop
= PageLength
- (float)atof(val
);
123 if ((val
= cupsGetOption("page-right", num_options
, options
)) != NULL
)
125 switch (Orientation
& 3)
128 PageRight
= PageWidth
- (float)atof(val
);
131 PageTop
= PageLength
- (float)atof(val
);
134 PageLeft
= (float)atof(val
);
137 PageBottom
= (float)atof(val
);
142 if ((val
= cupsGetOption("page-bottom", num_options
, options
)) != NULL
)
144 switch (Orientation
& 3)
147 PageBottom
= (float)atof(val
);
150 PageLeft
= (float)atof(val
);
153 PageTop
= PageLength
- (float)atof(val
);
156 PageRight
= PageWidth
- (float)atof(val
);
161 if ((val
= cupsGetOption("page-top", num_options
, options
)) != NULL
)
163 switch (Orientation
& 3)
166 PageTop
= PageLength
- (float)atof(val
);
169 PageRight
= PageWidth
- (float)atof(val
);
172 PageBottom
= (float)atof(val
);
175 PageLeft
= (float)atof(val
);
183 if (ppdIsMarked(ppd
, "Duplex", "DuplexNoTumble") ||
184 ppdIsMarked(ppd
, "Duplex", "DuplexTumble") ||
185 ppdIsMarked(ppd
, "JCLDuplex", "DuplexNoTumble") ||
186 ppdIsMarked(ppd
, "JCLDuplex", "DuplexTumble") ||
187 ppdIsMarked(ppd
, "EFDuplex", "DuplexNoTumble") ||
188 ppdIsMarked(ppd
, "EFDuplex", "DuplexTumble") ||
189 ppdIsMarked(ppd
, "KD03Duplex", "DuplexNoTumble") ||
190 ppdIsMarked(ppd
, "KD03Duplex", "DuplexTumble"))
198 * 'UpdatePageVars()' - Update the page variables for the orientation.
204 float temp
; /* Swapping variable */
207 switch (Orientation
& 3)
209 case 0 : /* Portait */
212 case 1 : /* Landscape */
214 PageLeft
= PageBottom
;
222 PageWidth
= PageLength
;
226 case 2 : /* Reverse Portrait */
227 temp
= PageWidth
- PageLeft
;
228 PageLeft
= PageWidth
- PageRight
;
231 temp
= PageLength
- PageBottom
;
232 PageBottom
= PageLength
- PageTop
;
236 case 3 : /* Reverse Landscape */
237 temp
= PageWidth
- PageLeft
;
238 PageLeft
= PageWidth
- PageRight
;
241 temp
= PageLength
- PageBottom
;
242 PageBottom
= PageLength
- PageTop
;
246 PageLeft
= PageBottom
;
254 PageWidth
= PageLength
;
262 * 'WriteCommon()' - Write common procedures...
268 puts("% x y w h ESPrc - Clip to a rectangle.\n"
269 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
270 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
271 "neg 0 rlineto closepath clip newpath}bind}ifelse put");
272 puts("% x y w h ESPrf - Fill a rectangle.\n"
273 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
274 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
275 "neg 0 rlineto closepath fill grestore}bind}ifelse put");
276 puts("% x y w h ESPrs - Stroke a rectangle.\n"
277 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
278 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
279 "neg 0 rlineto closepath stroke grestore}bind}ifelse put");
284 * 'WriteLabelProlog()' - Write the prolog with the classification
289 WriteLabelProlog(const char *label
, /* I - Page label */
290 float bottom
, /* I - Bottom position in points */
291 float top
, /* I - Top position in points */
292 float width
) /* I - Width in points */
294 const char *classification
; /* CLASSIFICATION environment variable */
295 const char *ptr
; /* Temporary string pointer */
299 * First get the current classification...
302 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
304 if (strcmp(classification
, "none") == 0)
308 * If there is nothing to show, bind an empty 'write labels' procedure
312 if (!classification
[0] && (label
== NULL
|| !label
[0]))
314 puts("userdict/ESPwl{}bind put");
319 * Set the classification + page label string...
323 if (strcmp(classification
, "confidential") == 0)
324 printf("/ESPpl(CONFIDENTIAL");
325 else if (strcmp(classification
, "classified") == 0)
326 printf("/ESPpl(CLASSIFIED");
327 else if (strcmp(classification
, "secret") == 0)
328 printf("/ESPpl(SECRET");
329 else if (strcmp(classification
, "topsecret") == 0)
330 printf("/ESPpl(TOP SECRET");
331 else if (strcmp(classification
, "unclassified") == 0)
332 printf("/ESPpl(UNCLASSIFIED");
337 for (ptr
= classification
; *ptr
; ptr
++)
338 if (*ptr
< 32 || *ptr
> 126)
339 printf("\\%03o", *ptr
);
340 else if (*ptr
== '_')
344 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
353 if (classification
[0])
357 * Quote the label string as needed...
360 for (ptr
= label
; *ptr
; ptr
++)
361 if (*ptr
< 32 || *ptr
> 126)
362 printf("\\%03o", *ptr
);
365 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
375 * Then get a 14 point Helvetica-Bold font...
378 puts("userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put");
381 * Finally, the procedure to write the labels on the page...
384 puts("userdict/ESPwl{");
385 puts(" ESPpf setfont");
386 printf(" ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
389 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
390 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
392 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
393 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
394 printf(" dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
395 printf(" %.0f moveto ESPpl show\n", top
- 14.0);
402 * 'WriteLabels()' - Write the actual page labels.
406 WriteLabels(int orient
) /* I - Orientation of the page */
408 float width
, /* Width of page */
409 length
; /* Length of page */
414 if ((orient
^ Orientation
) & 1)
427 case 1 : /* Landscape */
428 printf("%.1f 0.0 translate 90 rotate\n", length
);
430 case 2 : /* Reverse Portrait */
431 printf("%.1f %.1f translate 180 rotate\n", width
, length
);
433 case 3 : /* Reverse Landscape */
434 printf("0.0 %.1f translate -90 rotate\n", width
);
444 * 'WriteTextComment()' - Write a DSC text comment.
448 WriteTextComment(const char *name
, /* I - Comment name ("Title", etc.) */
449 const char *value
) /* I - Comment value */
451 int len
; /* Current line length */
455 * DSC comments are of the form:
459 * The name and value must be limited to 7-bit ASCII for most printers,
460 * so we escape all non-ASCII and ASCII control characters as described
461 * in the Adobe Document Structuring Conventions specification.
464 printf("%%%%%s: (", name
);
465 len
= 5 + (int)strlen(name
);
469 if (*value
< ' ' || *value
>= 127)
472 * Escape this character value...
475 if (len
>= 251) /* Keep line < 254 chars */
478 printf("\\%03o", *value
& 255);
481 else if (*value
== '\\')
484 * Escape the backslash...
487 if (len
>= 253) /* Keep line < 254 chars */
497 * Put this character literally...
500 if (len
>= 254) /* Keep line < 254 chars */