]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/common.c
2 * "$Id: common.c 5997 2006-10-02 15:33:05Z mike $"
4 * Common filter routines for the Common UNIX Printing System (CUPS).
6 * Copyright 1997-2006 by Easy Software Products.
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
15 * Attn: CUPS Licensing Information
16 * Easy Software Products
17 * 44141 Airport View Drive, Suite 204
18 * Hollywood, Maryland 20636 USA
20 * Voice: (301) 373-9600
21 * EMail: cups-info@cups.org
22 * WWW: http://www.cups.org
24 * This file is subject to the Apple OS-Developed Software exception.
28 * SetCommonOptions() - Set common filter options for media size,
30 * UpdatePageVars() - Update the page variables for the orientation.
31 * WriteComment() - Write a DSC comment.
32 * WriteCommon() - Write common procedures...
33 * WriteLabelProlog() - Write the prolog with the classification
35 * WriteLabels() - Write the actual page labels.
39 * Include necessary headers...
50 int Orientation
= 0, /* 0 = portrait, 1 = landscape, etc. */
51 Duplex
= 0, /* Duplexed? */
52 LanguageLevel
= 1, /* Language level of printer */
53 ColorDevice
= 1; /* Do color text? */
54 float PageLeft
= 18.0f
, /* Left margin */
55 PageRight
= 594.0f
, /* Right margin */
56 PageBottom
= 36.0f
, /* Bottom margin */
57 PageTop
= 756.0f
, /* Top margin */
58 PageWidth
= 612.0f
, /* Total page width */
59 PageLength
= 792.0f
; /* Total page length */
63 * 'SetCommonOptions()' - Set common filter options for media size, etc.
66 ppd_file_t
* /* O - PPD file */
68 int num_options
, /* I - Number of options */
69 cups_option_t
*options
, /* I - Options */
70 int change_size
) /* I - Change page size? */
72 ppd_file_t
*ppd
; /* PPD file */
73 ppd_size_t
*pagesize
; /* Current page size */
74 const char *val
; /* Option value */
78 setlocale(LC_TIME
, "");
81 ppd
= ppdOpenFile(getenv("PPD"));
84 cupsMarkOptions(ppd
, num_options
, options
);
86 if ((pagesize
= ppdPageSize(ppd
, NULL
)) != NULL
)
88 PageWidth
= pagesize
->width
;
89 PageLength
= pagesize
->length
;
90 PageTop
= pagesize
->top
;
91 PageBottom
= pagesize
->bottom
;
92 PageLeft
= pagesize
->left
;
93 PageRight
= pagesize
->right
;
95 fprintf(stderr
, "DEBUG: Page = %.0fx%.0f; %.0f,%.0f to %.0f,%.0f\n",
96 PageWidth
, PageLength
, PageLeft
, PageBottom
, PageRight
, PageTop
);
101 ColorDevice
= ppd
->color_device
;
102 LanguageLevel
= ppd
->language_level
;
105 if ((val
= cupsGetOption("landscape", num_options
, options
)) != NULL
)
107 if (strcasecmp(val
, "no") != 0 && strcasecmp(val
, "off") != 0 &&
108 strcasecmp(val
, "false") != 0)
110 if (ppd
&& ppd
->landscape
> 0)
116 else if ((val
= cupsGetOption("orientation-requested", num_options
, options
)) != NULL
)
119 * Map IPP orientation values to 0 to 3:
123 * 5 = -90 degrees = 3
124 * 6 = 180 degrees = 2
127 Orientation
= atoi(val
) - 3;
128 if (Orientation
>= 2)
132 if ((val
= cupsGetOption("page-left", num_options
, options
)) != NULL
)
134 switch (Orientation
& 3)
137 PageLeft
= (float)atof(val
);
140 PageBottom
= (float)atof(val
);
143 PageRight
= PageWidth
- (float)atof(val
);
146 PageTop
= PageLength
- (float)atof(val
);
151 if ((val
= cupsGetOption("page-right", num_options
, options
)) != NULL
)
153 switch (Orientation
& 3)
156 PageRight
= PageWidth
- (float)atof(val
);
159 PageTop
= PageLength
- (float)atof(val
);
162 PageLeft
= (float)atof(val
);
165 PageBottom
= (float)atof(val
);
170 if ((val
= cupsGetOption("page-bottom", num_options
, options
)) != NULL
)
172 switch (Orientation
& 3)
175 PageBottom
= (float)atof(val
);
178 PageLeft
= (float)atof(val
);
181 PageTop
= PageLength
- (float)atof(val
);
184 PageRight
= PageWidth
- (float)atof(val
);
189 if ((val
= cupsGetOption("page-top", num_options
, options
)) != NULL
)
191 switch (Orientation
& 3)
194 PageTop
= PageLength
- (float)atof(val
);
197 PageRight
= PageWidth
- (float)atof(val
);
200 PageBottom
= (float)atof(val
);
203 PageLeft
= (float)atof(val
);
211 if (ppdIsMarked(ppd
, "Duplex", "DuplexNoTumble") ||
212 ppdIsMarked(ppd
, "Duplex", "DuplexTumble") ||
213 ppdIsMarked(ppd
, "JCLDuplex", "DuplexNoTumble") ||
214 ppdIsMarked(ppd
, "JCLDuplex", "DuplexTumble") ||
215 ppdIsMarked(ppd
, "EFDuplex", "DuplexNoTumble") ||
216 ppdIsMarked(ppd
, "EFDuplex", "DuplexTumble") ||
217 ppdIsMarked(ppd
, "KD03Duplex", "DuplexNoTumble") ||
218 ppdIsMarked(ppd
, "KD03Duplex", "DuplexTumble"))
226 * 'UpdatePageVars()' - Update the page variables for the orientation.
232 float temp
; /* Swapping variable */
235 switch (Orientation
& 3)
237 case 0 : /* Portait */
240 case 1 : /* Landscape */
242 PageLeft
= PageBottom
;
250 PageWidth
= PageLength
;
254 case 2 : /* Reverse Portrait */
255 temp
= PageWidth
- PageLeft
;
256 PageLeft
= PageWidth
- PageRight
;
259 temp
= PageLength
- PageBottom
;
260 PageBottom
= PageLength
- PageTop
;
264 case 3 : /* Reverse Landscape */
265 temp
= PageWidth
- PageLeft
;
266 PageLeft
= PageWidth
- PageRight
;
269 temp
= PageLength
- PageBottom
;
270 PageBottom
= PageLength
- PageTop
;
274 PageLeft
= PageBottom
;
282 PageWidth
= PageLength
;
290 * 'WriteCommon()' - Write common procedures...
296 puts("% x y w h ESPrc - Clip to a rectangle.\n"
297 "userdict/ESPrc/rectclip where{pop/rectclip load}\n"
298 "{{newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
299 "neg 0 rlineto closepath clip newpath}bind}ifelse put");
300 puts("% x y w h ESPrf - Fill a rectangle.\n"
301 "userdict/ESPrf/rectfill where{pop/rectfill load}\n"
302 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
303 "neg 0 rlineto closepath fill grestore}bind}ifelse put");
304 puts("% x y w h ESPrs - Stroke a rectangle.\n"
305 "userdict/ESPrs/rectstroke where{pop/rectstroke load}\n"
306 "{{gsave newpath 4 2 roll moveto 1 index 0 rlineto 0 exch rlineto\n"
307 "neg 0 rlineto closepath stroke grestore}bind}ifelse put");
312 * 'WriteLabelProlog()' - Write the prolog with the classification
317 WriteLabelProlog(const char *label
, /* I - Page label */
318 float bottom
, /* I - Bottom position in points */
319 float top
, /* I - Top position in points */
320 float width
) /* I - Width in points */
322 const char *classification
; /* CLASSIFICATION environment variable */
323 const char *ptr
; /* Temporary string pointer */
327 * First get the current classification...
330 if ((classification
= getenv("CLASSIFICATION")) == NULL
)
332 if (strcmp(classification
, "none") == 0)
336 * If there is nothing to show, bind an empty 'write labels' procedure
340 if (!classification
[0] && (label
== NULL
|| !label
[0]))
342 puts("userdict/ESPwl{}bind put");
347 * Set the classification + page label string...
351 if (strcmp(classification
, "confidential") == 0)
352 printf("/ESPpl(CONFIDENTIAL");
353 else if (strcmp(classification
, "classified") == 0)
354 printf("/ESPpl(CLASSIFIED");
355 else if (strcmp(classification
, "secret") == 0)
356 printf("/ESPpl(SECRET");
357 else if (strcmp(classification
, "topsecret") == 0)
358 printf("/ESPpl(TOP SECRET");
359 else if (strcmp(classification
, "unclassified") == 0)
360 printf("/ESPpl(UNCLASSIFIED");
365 for (ptr
= classification
; *ptr
; ptr
++)
366 if (*ptr
< 32 || *ptr
> 126)
367 printf("\\%03o", *ptr
);
368 else if (*ptr
== '_')
372 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
381 if (classification
[0])
385 * Quote the label string as needed...
388 for (ptr
= label
; *ptr
; ptr
++)
389 if (*ptr
< 32 || *ptr
> 126)
390 printf("\\%03o", *ptr
);
393 if (*ptr
== '(' || *ptr
== ')' || *ptr
== '\\')
403 * Then get a 14 point Helvetica-Bold font...
406 puts("userdict/ESPpf /Helvetica-Bold findfont 14 scalefont put");
409 * Finally, the procedure to write the labels on the page...
412 puts("userdict/ESPwl{");
413 puts(" ESPpf setfont");
414 printf(" ESPpl stringwidth pop dup 12 add exch -0.5 mul %.0f add\n",
417 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", bottom
- 2.0);
418 printf(" dup 6 sub %.0f 3 index 20 ESPrf\n", top
- 18.0);
420 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", bottom
- 2.0);
421 printf(" dup 6 sub %.0f 3 index 20 ESPrs\n", top
- 18.0);
422 printf(" dup %.0f moveto ESPpl show\n", bottom
+ 2.0);
423 printf(" %.0f moveto ESPpl show\n", top
- 14.0);
430 * 'WriteLabels()' - Write the actual page labels.
434 WriteLabels(int orient
) /* I - Orientation of the page */
436 float width
, /* Width of page */
437 length
; /* Length of page */
442 if ((orient
^ Orientation
) & 1)
455 case 1 : /* Landscape */
456 printf("%.1f 0.0 translate 90 rotate\n", length
);
458 case 2 : /* Reverse Portrait */
459 printf("%.1f %.1f translate 180 rotate\n", width
, length
);
461 case 3 : /* Reverse Landscape */
462 printf("0.0 %.1f translate -90 rotate\n", width
);
472 * 'WriteTextComment()' - Write a DSC text comment.
476 WriteTextComment(const char *name
, /* I - Comment name ("Title", etc.) */
477 const char *value
) /* I - Comment value */
479 int len
; /* Current line length */
483 * DSC comments are of the form:
487 * The name and value must be limited to 7-bit ASCII for most printers,
488 * so we escape all non-ASCII and ASCII control characters as described
489 * in the Adobe Document Structuring Conventions specification.
492 printf("%%%%%s: (", name
);
493 len
= 5 + strlen(name
);
497 if (*value
< ' ' || *value
>= 127)
500 * Escape this character value...
503 if (len
>= 251) /* Keep line < 254 chars */
506 printf("\\%03o", *value
& 255);
509 else if (*value
== '\\')
512 * Escape the backslash...
515 if (len
>= 253) /* Keep line < 254 chars */
525 * Put this character literally...
528 if (len
>= 254) /* Keep line < 254 chars */
543 * End of "$Id: common.c 5997 2006-10-02 15:33:05Z mike $".