]>
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 * 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 "LICENSE.txt"
10 * which should have been included with this file. If this file is
11 * file is missing or damaged, see the license at "http://www.cups.org/".
13 * This file is subject to the Apple OS-Developed Software exception.
17 * Include necessary headers...
28 int Orientation
= 0, /* 0 = portrait, 1 = landscape, etc. */
29 Duplex
= 0, /* Duplexed? */
30 LanguageLevel
= 1, /* Language level of printer */
31 ColorDevice
= 1; /* Do color text? */
32 float PageLeft
= 18.0f
, /* Left margin */
33 PageRight
= 594.0f
, /* Right margin */
34 PageBottom
= 36.0f
, /* Bottom margin */
35 PageTop
= 756.0f
, /* Top margin */
36 PageWidth
= 612.0f
, /* Total page width */
37 PageLength
= 792.0f
; /* Total page length */
41 * 'SetCommonOptions()' - Set common filter options for media size, etc.
44 ppd_file_t
* /* O - PPD file */
46 int num_options
, /* I - Number of options */
47 cups_option_t
*options
, /* I - Options */
48 int change_size
) /* I - Change page size? */
50 ppd_file_t
*ppd
; /* PPD file */
51 ppd_size_t
*pagesize
; /* Current page size */
52 const char *val
; /* Option value */
56 setlocale(LC_TIME
, "");
59 ppd
= ppdOpenFile(getenv("PPD"));
62 cupsMarkOptions(ppd
, num_options
, options
);
64 if ((pagesize
= ppdPageSize(ppd
, NULL
)) != NULL
)
66 PageWidth
= pagesize
->width
;
67 PageLength
= pagesize
->length
;
68 PageTop
= pagesize
->top
;
69 PageBottom
= pagesize
->bottom
;
70 PageLeft
= pagesize
->left
;
71 PageRight
= pagesize
->right
;
73 fprintf(stderr
, "DEBUG: Page = %.0fx%.0f; %.0f,%.0f to %.0f,%.0f\n",
74 PageWidth
, PageLength
, PageLeft
, PageBottom
, PageRight
, PageTop
);
79 ColorDevice
= ppd
->color_device
;
80 LanguageLevel
= ppd
->language_level
;
83 if ((val
= cupsGetOption("landscape", num_options
, options
)) != NULL
)
85 if (_cups_strcasecmp(val
, "no") != 0 && _cups_strcasecmp(val
, "off") != 0 &&
86 _cups_strcasecmp(val
, "false") != 0)
88 if (ppd
&& ppd
->landscape
> 0)
94 else if ((val
= cupsGetOption("orientation-requested", num_options
, options
)) != NULL
)
97 * Map IPP orientation values to 0 to 3:
101 * 5 = -90 degrees = 3
102 * 6 = 180 degrees = 2
105 Orientation
= atoi(val
) - 3;
106 if (Orientation
>= 2)
110 if ((val
= cupsGetOption("page-left", num_options
, options
)) != NULL
)
112 switch (Orientation
& 3)
115 PageLeft
= (float)atof(val
);
118 PageBottom
= (float)atof(val
);
121 PageRight
= PageWidth
- (float)atof(val
);
124 PageTop
= PageLength
- (float)atof(val
);
129 if ((val
= cupsGetOption("page-right", num_options
, options
)) != NULL
)
131 switch (Orientation
& 3)
134 PageRight
= PageWidth
- (float)atof(val
);
137 PageTop
= PageLength
- (float)atof(val
);
140 PageLeft
= (float)atof(val
);
143 PageBottom
= (float)atof(val
);
148 if ((val
= cupsGetOption("page-bottom", num_options
, options
)) != NULL
)
150 switch (Orientation
& 3)
153 PageBottom
= (float)atof(val
);
156 PageLeft
= (float)atof(val
);
159 PageTop
= PageLength
- (float)atof(val
);
162 PageRight
= PageWidth
- (float)atof(val
);
167 if ((val
= cupsGetOption("page-top", num_options
, options
)) != NULL
)
169 switch (Orientation
& 3)
172 PageTop
= PageLength
- (float)atof(val
);
175 PageRight
= PageWidth
- (float)atof(val
);
178 PageBottom
= (float)atof(val
);
181 PageLeft
= (float)atof(val
);
189 if (ppdIsMarked(ppd
, "Duplex", "DuplexNoTumble") ||
190 ppdIsMarked(ppd
, "Duplex", "DuplexTumble") ||
191 ppdIsMarked(ppd
, "JCLDuplex", "DuplexNoTumble") ||
192 ppdIsMarked(ppd
, "JCLDuplex", "DuplexTumble") ||
193 ppdIsMarked(ppd
, "EFDuplex", "DuplexNoTumble") ||
194 ppdIsMarked(ppd
, "EFDuplex", "DuplexTumble") ||
195 ppdIsMarked(ppd
, "KD03Duplex", "DuplexNoTumble") ||
196 ppdIsMarked(ppd
, "KD03Duplex", "DuplexTumble"))
204 * 'UpdatePageVars()' - Update the page variables for the orientation.
210 float temp
; /* Swapping variable */
213 switch (Orientation
& 3)
215 case 0 : /* Portait */
218 case 1 : /* Landscape */
220 PageLeft
= PageBottom
;
228 PageWidth
= PageLength
;
232 case 2 : /* Reverse Portrait */
233 temp
= PageWidth
- PageLeft
;
234 PageLeft
= PageWidth
- PageRight
;
237 temp
= PageLength
- PageBottom
;
238 PageBottom
= PageLength
- PageTop
;
242 case 3 : /* Reverse Landscape */
243 temp
= PageWidth
- PageLeft
;
244 PageLeft
= PageWidth
- PageRight
;
247 temp
= PageLength
- PageBottom
;
248 PageBottom
= PageLength
- PageTop
;
252 PageLeft
= PageBottom
;
260 PageWidth
= PageLength
;
268 * 'WriteCommon()' - Write common procedures...
274 puts("% x y w h ESPrc - Clip to a rectangle.\n"
275 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
276 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
277 "neg 0 rlineto closepath clip newpath}bind}ifelse put");
278 puts("% x y w h ESPrf - Fill a rectangle.\n"
279 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
280 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
281 "neg 0 rlineto closepath fill grestore}bind}ifelse put");
282 puts("% x y w h ESPrs - Stroke a rectangle.\n"
283 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
284 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
285 "neg 0 rlineto closepath stroke grestore}bind}ifelse put");
290 * 'WriteLabelProlog()' - Write the prolog with the classification
295 WriteLabelProlog(const char *label
, /* I - Page label */
296 float bottom
, /* I - Bottom position in points */
297 float top
, /* I - Top position in points */
298 float width
) /* I - Width in points */
300 const char *classification
; /* CLASSIFICATION environment variable */
301 const char *ptr
; /* Temporary string pointer */
305 * First get the current classification...
308 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
310 if (strcmp(classification
, "none") == 0)
314 * If there is nothing to show, bind an empty 'write labels' procedure
318 if (!classification
[0] && (label
== NULL
|| !label
[0]))
320 puts("userdict/ESPwl{}bind put");
325 * Set the classification + page label string...
329 if (strcmp(classification
, "confidential") == 0)
330 printf("/ESPpl(CONFIDENTIAL");
331 else if (strcmp(classification
, "classified") == 0)
332 printf("/ESPpl(CLASSIFIED");
333 else if (strcmp(classification
, "secret") == 0)
334 printf("/ESPpl(SECRET");
335 else if (strcmp(classification
, "topsecret") == 0)
336 printf("/ESPpl(TOP SECRET");
337 else if (strcmp(classification
, "unclassified") == 0)
338 printf("/ESPpl(UNCLASSIFIED");
343 for (ptr
= classification
; *ptr
; ptr
++)
344 if (*ptr
< 32 || *ptr
> 126)
345 printf("\\%03o", *ptr
);
346 else if (*ptr
== '_')
350 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
359 if (classification
[0])
363 * Quote the label string as needed...
366 for (ptr
= label
; *ptr
; ptr
++)
367 if (*ptr
< 32 || *ptr
> 126)
368 printf("\\%03o", *ptr
);
371 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
381 * Then get a 14 point Helvetica-Bold font...
384 puts("userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put");
387 * Finally, the procedure to write the labels on the page...
390 puts("userdict/ESPwl{");
391 puts(" ESPpf setfont");
392 printf(" ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
395 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
396 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
398 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
399 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
400 printf(" dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
401 printf(" %.0f moveto ESPpl show\n", top
- 14.0);
408 * 'WriteLabels()' - Write the actual page labels.
412 WriteLabels(int orient
) /* I - Orientation of the page */
414 float width
, /* Width of page */
415 length
; /* Length of page */
420 if ((orient
^ Orientation
) & 1)
433 case 1 : /* Landscape */
434 printf("%.1f 0.0 translate 90 rotate\n", length
);
436 case 2 : /* Reverse Portrait */
437 printf("%.1f %.1f translate 180 rotate\n", width
, length
);
439 case 3 : /* Reverse Landscape */
440 printf("0.0 %.1f translate -90 rotate\n", width
);
450 * 'WriteTextComment()' - Write a DSC text comment.
454 WriteTextComment(const char *name
, /* I - Comment name ("Title", etc.) */
455 const char *value
) /* I - Comment value */
457 int len
; /* Current line length */
461 * DSC comments are of the form:
465 * The name and value must be limited to 7-bit ASCII for most printers,
466 * so we escape all non-ASCII and ASCII control characters as described
467 * in the Adobe Document Structuring Conventions specification.
470 printf("%%%%%s: (", name
);
471 len
= 5 + (int)strlen(name
);
475 if (*value
< ' ' || *value
>= 127)
478 * Escape this character value...
481 if (len
>= 251) /* Keep line < 254 chars */
484 printf("\\%03o", *value
& 255);
487 else if (*value
== '\\')
490 * Escape the backslash...
493 if (len
>= 253) /* Keep line < 254 chars */
503 * Put this character literally...
506 if (len
>= 254) /* Keep line < 254 chars */