]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/common.c
3d45a203783247bd3434564b76414b67c1e831a7
4 * Common filter routines for CUPS.
6 * Copyright 2007-2014 by Apple Inc.
7 * Copyright 1997-2006 by Easy Software Products.
9 * These coded instructions, statements, and computer programs are the
10 * property of Apple Inc. and are protected by Federal copyright
11 * law. Distribution and use rights are outlined in the file "LICENSE.txt"
12 * which should have been included with this file. If this file is
13 * file is missing or damaged, see the license at "http://www.cups.org/".
15 * This file is subject to the Apple OS-Developed Software exception.
19 * Include necessary headers...
30 int Orientation
= 0, /* 0 = portrait, 1 = landscape, etc. */
31 Duplex
= 0, /* Duplexed? */
32 LanguageLevel
= 1, /* Language level of printer */
33 ColorDevice
= 1; /* Do color text? */
34 float PageLeft
= 18.0f
, /* Left margin */
35 PageRight
= 594.0f
, /* Right margin */
36 PageBottom
= 36.0f
, /* Bottom margin */
37 PageTop
= 756.0f
, /* Top margin */
38 PageWidth
= 612.0f
, /* Total page width */
39 PageLength
= 792.0f
; /* Total page length */
43 * 'SetCommonOptions()' - Set common filter options for media size, etc.
46 ppd_file_t
* /* O - PPD file */
48 int num_options
, /* I - Number of options */
49 cups_option_t
*options
, /* I - Options */
50 int change_size
) /* I - Change page size? */
52 ppd_file_t
*ppd
; /* PPD file */
53 ppd_size_t
*pagesize
; /* Current page size */
54 const char *val
; /* Option value */
58 setlocale(LC_TIME
, "");
61 ppd
= ppdOpenFile(getenv("PPD"));
64 cupsMarkOptions(ppd
, num_options
, options
);
66 if ((pagesize
= ppdPageSize(ppd
, NULL
)) != NULL
)
68 PageWidth
= pagesize
->width
;
69 PageLength
= pagesize
->length
;
70 PageTop
= pagesize
->top
;
71 PageBottom
= pagesize
->bottom
;
72 PageLeft
= pagesize
->left
;
73 PageRight
= pagesize
->right
;
75 fprintf(stderr
, "DEBUG: Page = %.0fx%.0f; %.0f,%.0f to %.0f,%.0f\n",
76 PageWidth
, PageLength
, PageLeft
, PageBottom
, PageRight
, PageTop
);
81 ColorDevice
= ppd
->color_device
;
82 LanguageLevel
= ppd
->language_level
;
85 if ((val
= cupsGetOption("landscape", num_options
, options
)) != NULL
)
87 if (_cups_strcasecmp(val
, "no") != 0 && _cups_strcasecmp(val
, "off") != 0 &&
88 _cups_strcasecmp(val
, "false") != 0)
90 if (ppd
&& ppd
->landscape
> 0)
96 else if ((val
= cupsGetOption("orientation-requested", num_options
, options
)) != NULL
)
99 * Map IPP orientation values to 0 to 3:
103 * 5 = -90 degrees = 3
104 * 6 = 180 degrees = 2
107 Orientation
= atoi(val
) - 3;
108 if (Orientation
>= 2)
112 if ((val
= cupsGetOption("page-left", num_options
, options
)) != NULL
)
114 switch (Orientation
& 3)
117 PageLeft
= (float)atof(val
);
120 PageBottom
= (float)atof(val
);
123 PageRight
= PageWidth
- (float)atof(val
);
126 PageTop
= PageLength
- (float)atof(val
);
131 if ((val
= cupsGetOption("page-right", num_options
, options
)) != NULL
)
133 switch (Orientation
& 3)
136 PageRight
= PageWidth
- (float)atof(val
);
139 PageTop
= PageLength
- (float)atof(val
);
142 PageLeft
= (float)atof(val
);
145 PageBottom
= (float)atof(val
);
150 if ((val
= cupsGetOption("page-bottom", num_options
, options
)) != NULL
)
152 switch (Orientation
& 3)
155 PageBottom
= (float)atof(val
);
158 PageLeft
= (float)atof(val
);
161 PageTop
= PageLength
- (float)atof(val
);
164 PageRight
= PageWidth
- (float)atof(val
);
169 if ((val
= cupsGetOption("page-top", num_options
, options
)) != NULL
)
171 switch (Orientation
& 3)
174 PageTop
= PageLength
- (float)atof(val
);
177 PageRight
= PageWidth
- (float)atof(val
);
180 PageBottom
= (float)atof(val
);
183 PageLeft
= (float)atof(val
);
191 if (ppdIsMarked(ppd
, "Duplex", "DuplexNoTumble") ||
192 ppdIsMarked(ppd
, "Duplex", "DuplexTumble") ||
193 ppdIsMarked(ppd
, "JCLDuplex", "DuplexNoTumble") ||
194 ppdIsMarked(ppd
, "JCLDuplex", "DuplexTumble") ||
195 ppdIsMarked(ppd
, "EFDuplex", "DuplexNoTumble") ||
196 ppdIsMarked(ppd
, "EFDuplex", "DuplexTumble") ||
197 ppdIsMarked(ppd
, "KD03Duplex", "DuplexNoTumble") ||
198 ppdIsMarked(ppd
, "KD03Duplex", "DuplexTumble"))
206 * 'UpdatePageVars()' - Update the page variables for the orientation.
212 float temp
; /* Swapping variable */
215 switch (Orientation
& 3)
217 case 0 : /* Portait */
220 case 1 : /* Landscape */
222 PageLeft
= PageBottom
;
230 PageWidth
= PageLength
;
234 case 2 : /* Reverse Portrait */
235 temp
= PageWidth
- PageLeft
;
236 PageLeft
= PageWidth
- PageRight
;
239 temp
= PageLength
- PageBottom
;
240 PageBottom
= PageLength
- PageTop
;
244 case 3 : /* Reverse Landscape */
245 temp
= PageWidth
- PageLeft
;
246 PageLeft
= PageWidth
- PageRight
;
249 temp
= PageLength
- PageBottom
;
250 PageBottom
= PageLength
- PageTop
;
254 PageLeft
= PageBottom
;
262 PageWidth
= PageLength
;
270 * 'WriteCommon()' - Write common procedures...
276 puts("% x y w h ESPrc - Clip to a rectangle.\n"
277 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
278 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
279 "neg 0 rlineto closepath clip newpath}bind}ifelse put");
280 puts("% x y w h ESPrf - Fill a rectangle.\n"
281 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
282 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
283 "neg 0 rlineto closepath fill grestore}bind}ifelse put");
284 puts("% x y w h ESPrs - Stroke a rectangle.\n"
285 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
286 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
287 "neg 0 rlineto closepath stroke grestore}bind}ifelse put");
292 * 'WriteLabelProlog()' - Write the prolog with the classification
297 WriteLabelProlog(const char *label
, /* I - Page label */
298 float bottom
, /* I - Bottom position in points */
299 float top
, /* I - Top position in points */
300 float width
) /* I - Width in points */
302 const char *classification
; /* CLASSIFICATION environment variable */
303 const char *ptr
; /* Temporary string pointer */
307 * First get the current classification...
310 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
312 if (strcmp(classification
, "none") == 0)
316 * If there is nothing to show, bind an empty 'write labels' procedure
320 if (!classification
[0] && (label
== NULL
|| !label
[0]))
322 puts("userdict/ESPwl{}bind put");
327 * Set the classification + page label string...
331 if (strcmp(classification
, "confidential") == 0)
332 printf("/ESPpl(CONFIDENTIAL");
333 else if (strcmp(classification
, "classified") == 0)
334 printf("/ESPpl(CLASSIFIED");
335 else if (strcmp(classification
, "secret") == 0)
336 printf("/ESPpl(SECRET");
337 else if (strcmp(classification
, "topsecret") == 0)
338 printf("/ESPpl(TOP SECRET");
339 else if (strcmp(classification
, "unclassified") == 0)
340 printf("/ESPpl(UNCLASSIFIED");
345 for (ptr
= classification
; *ptr
; ptr
++)
346 if (*ptr
< 32 || *ptr
> 126)
347 printf("\\%03o", *ptr
);
348 else if (*ptr
== '_')
352 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
361 if (classification
[0])
365 * Quote the label string as needed...
368 for (ptr
= label
; *ptr
; ptr
++)
369 if (*ptr
< 32 || *ptr
> 126)
370 printf("\\%03o", *ptr
);
373 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
383 * Then get a 14 point Helvetica-Bold font...
386 puts("userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put");
389 * Finally, the procedure to write the labels on the page...
392 puts("userdict/ESPwl{");
393 puts(" ESPpf setfont");
394 printf(" ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
397 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
398 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
400 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
401 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
402 printf(" dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
403 printf(" %.0f moveto ESPpl show\n", top
- 14.0);
410 * 'WriteLabels()' - Write the actual page labels.
414 WriteLabels(int orient
) /* I - Orientation of the page */
416 float width
, /* Width of page */
417 length
; /* Length of page */
422 if ((orient
^ Orientation
) & 1)
435 case 1 : /* Landscape */
436 printf("%.1f 0.0 translate 90 rotate\n", length
);
438 case 2 : /* Reverse Portrait */
439 printf("%.1f %.1f translate 180 rotate\n", width
, length
);
441 case 3 : /* Reverse Landscape */
442 printf("0.0 %.1f translate -90 rotate\n", width
);
452 * 'WriteTextComment()' - Write a DSC text comment.
456 WriteTextComment(const char *name
, /* I - Comment name ("Title", etc.) */
457 const char *value
) /* I - Comment value */
459 int len
; /* Current line length */
463 * DSC comments are of the form:
467 * The name and value must be limited to 7-bit ASCII for most printers,
468 * so we escape all non-ASCII and ASCII control characters as described
469 * in the Adobe Document Structuring Conventions specification.
472 printf("%%%%%s: (", name
);
473 len
= 5 + (int)strlen(name
);
477 if (*value
< ' ' || *value
>= 127)
480 * Escape this character value...
483 if (len
>= 251) /* Keep line < 254 chars */
486 printf("\\%03o", *value
& 255);
489 else if (*value
== '\\')
492 * Escape the backslash...
495 if (len
>= 253) /* Keep line < 254 chars */
505 * Put this character literally...
508 if (len
>= 254) /* Keep line < 254 chars */