]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/hpgl-prolog.c
2 * "$Id: hpgl-prolog.c 6649 2007-07-11 21:46:42Z mike $"
4 * HP-GL/2 prolog routines for for the Common UNIX Printing System (CUPS).
6 * Copyright 2007 by Apple Inc.
7 * Copyright 1993-2007 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 * OutputProlog() - Output the PostScript prolog...
20 * OutputTrailer() - Output the PostScript trailer...
21 * Outputf() - Write a formatted string to the output file, creating the
22 * page header as needed...
26 * Include necessary headers...
34 * 'OutputProlog()' - Output the PostScript prolog...
38 OutputProlog(char *title
, /* I - Job title */
39 char *user
, /* I - Username */
40 int shading
) /* I - Type of shading */
42 FILE *prolog
; /* Prolog file */
43 char line
[255]; /* Line from prolog file */
44 const char *datadir
; /* CUPS_DATADIR environment variable */
45 char filename
[1024]; /* Name of prolog file */
46 time_t curtime
; /* Current time */
47 struct tm
*curtm
; /* Current date */
51 curtm
= localtime(&curtime
);
53 puts("%!PS-Adobe-3.0");
54 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
55 PageLeft
, PageBottom
, PageRight
, PageTop
);
56 puts("%%Pages: (atend)");
57 printf("%%%%LanguageLevel: %d\n", LanguageLevel
);
58 puts("%%DocumentData: Clean7Bit");
59 puts("%%DocumentSuppliedResources: procset hpgltops 1.1 0");
60 puts("%%DocumentNeededResources: font Courier Helvetica");
61 puts("%%Creator: hpgltops/" CUPS_SVERSION
);
62 strftime(line
, sizeof(line
), "%c", curtm
);
63 printf("%%%%CreationDate: %s\n", line
);
64 WriteTextComment("Title", title
);
65 WriteTextComment("For", user
);
66 printf("%%cupsRotation: %d\n", (Orientation
& 3) * 90);
67 puts("%%EndComments");
68 puts("%%BeginProlog");
69 printf("/DefaultPenWidth %.2f def\n", PenWidth
* 72.0 / 25.4);
70 if (!shading
) /* Black only */
71 puts("/setrgbcolor { pop pop pop } bind def");
72 else if (!ColorDevice
) /* Greyscale */
73 puts("/setrgbcolor { 0.08 mul exch 0.61 mul add exch 0.31 mul add setgray } bind def\n");
75 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
76 datadir
= CUPS_DATADIR
;
78 snprintf(filename
, sizeof(filename
), "%s/data/HPGLprolog", datadir
);
80 if ((prolog
= fopen(filename
, "r")) == NULL
)
83 "DEBUG: Unable to open HPGL prolog \"%s\" for reading - %s\n",
84 filename
, strerror(errno
));
88 while (fgets(line
, sizeof(line
), prolog
) != NULL
)
95 IN_initialize(0, NULL
);
100 * 'OutputTrailer()' - Output the PostScript trailer...
107 PG_advance_page(0, NULL
);
110 printf("%%%%Pages: %d\n", PageCount
);
116 * 'Outputf()' - Write a formatted string to the output file, creating the
117 * page header as needed...
120 int /* O - Number of bytes written */
121 Outputf(const char *format
, /* I - Printf-style string */
122 ...) /* I - Additional args as needed */
124 va_list ap
; /* Argument pointer */
125 int bytes
; /* Number of bytes written */
126 float iw1
[2], iw2
[2]; /* Clipping window */
127 int i
; /* Looping var */
128 ppd_size_t
*size
; /* Page size */
129 ppd_option_t
*option
; /* Page size option */
130 ppd_choice_t
*choice
; /* Page size choice */
131 float width
, length
; /* Page dimensions */
132 int landscape
; /* Rotate for landscape orientation? */
136 * Write the page header as needed...
144 printf("%%%%Page: %d %d\n", PageCount
, PageCount
);
148 if (!FitPlot
&& PlotSizeSet
)
151 * Set the page size for this page...
154 if (PageRotation
== 0 || PageRotation
== 180)
157 length
= PlotSize
[1];
162 length
= PlotSize
[0];
165 fprintf(stderr
, "DEBUG: hpgltops setting page size (%.0f x %.0f)\n",
170 fputs("DEBUG: hpgltops has a PPD file!\n", stderr
);
173 * Lookup the closest PageSize and set it...
176 for (i
= PPD
->num_sizes
, size
= PPD
->sizes
; i
> 0; i
--, size
++)
177 if ((fabs(length
- size
->length
) < 36.0 && size
->width
>= width
) ||
178 (fabs(length
- size
->width
) < 36.0 && size
->length
>= width
))
181 if (i
== 0 && PPD
->variable_sizes
)
183 for (i
= PPD
->num_sizes
, size
= PPD
->sizes
; i
> 0; i
--, size
++)
184 if (strcasecmp(size
->name
, "custom") == 0)
191 * Found a matching size...
194 option
= ppdFindOption(PPD
, "PageSize");
195 choice
= ppdFindChoice(option
, size
->name
);
197 puts("%%BeginPageSetup");
198 printf("%%%%BeginFeature: PageSize %s\n", size
->name
);
200 if (strcasecmp(size
->name
, "custom") == 0)
202 PageLeft
= PPD
->custom_margins
[0];
203 PageRight
= width
- PPD
->custom_margins
[2];
205 PageBottom
= PPD
->custom_margins
[1];
206 PageTop
= length
- PPD
->custom_margins
[3];
209 printf("%.0f %.0f 0 0 0\n", width
, length
);
211 if (choice
->code
== NULL
)
214 * This can happen with certain buggy PPD files that don't include
215 * a CustomPageSize command sequence... We just use a generic
216 * Level 2 command sequence...
220 puts("<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n");
225 * Use the vendor-supplied command...
228 printf("%s\n", choice
->code
);
234 printf("%s\n", choice
->code
);
236 if (fabs(length
- size
->width
) < 36.0)
239 * Do landscape orientation...
242 PageLeft
= size
->bottom
;
243 PageRight
= size
->top
;
244 PageWidth
= size
->length
;
245 PageBottom
= size
->left
;
246 PageTop
= size
->right
;
247 PageLength
= size
->width
;
254 * Do portrait orientation...
257 PageLeft
= size
->left
;
258 PageRight
= size
->right
;
259 PageWidth
= size
->width
;
260 PageBottom
= size
->bottom
;
262 PageLength
= size
->length
;
266 puts("%%EndFeature");
267 puts("%%EndPageSetup");
272 fputs("DEBUG: hpgltops does not have a PPD file!\n", stderr
);
274 puts("%%BeginPageSetup");
275 printf("%%%%BeginFeature: PageSize w%.0fh%.0f\n", width
, length
);
276 printf("<</PageSize[%.0f %.0f]/ImageBBox null>>setpagedevice\n",
278 puts("%%EndFeature");
279 puts("%%EndPageSetup");
293 printf("%.1f setmiterlimit\n", MiterLimit
);
294 printf("%d setlinecap\n", LineCap
);
295 printf("%d setlinejoin\n", LineJoin
);
297 printf("%.3f %.3f %.3f %.2f SP\n", Pens
[1].rgb
[0], Pens
[1].rgb
[1],
298 Pens
[1].rgb
[2], Pens
[1].width
* PenScaling
);
302 if (Duplex
&& (PageCount
& 1) == 0)
303 switch ((PageRotation
/ 90 + landscape
) & 3)
306 printf("%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
309 printf("%.0f 0 translate 90 rotate\n", PageLength
);
310 printf("%.1f %.1f translate\n", PageLength
- PageTop
,
311 PageWidth
- PageRight
);
314 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
315 printf("%.1f %.1f translate\n", PageLeft
, PageLength
- PageTop
);
318 printf("0 %.0f translate -90 rotate\n", PageWidth
);
319 printf("%.1f %.1f translate\n", PageBottom
, PageLeft
);
323 switch ((PageRotation
/ 90 + landscape
) & 3)
326 printf("%.1f %.1f translate\n", PageLeft
, PageBottom
);
329 printf("%.0f 0 translate 90 rotate\n", PageLength
);
330 printf("%.1f %.1f translate\n", PageBottom
, PageWidth
- PageRight
);
333 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
334 printf("%.1f %.1f translate\n", PageWidth
- PageRight
,
335 PageLength
- PageTop
);
338 printf("0 %.0f translate -90 rotate\n", PageWidth
);
339 printf("%.1f %.1f translate\n", PageLength
- PageTop
, PageLeft
);
343 if (IW1
[0] != IW2
[0] && IW1
[1] != IW2
[1])
345 iw1
[0] = IW1
[0] * 72.0f
/ 1016.0f
;
346 iw1
[1] = IW1
[1] * 72.0f
/ 1016.0f
;
347 iw2
[0] = IW2
[0] * 72.0f
/ 1016.0f
;
348 iw2
[1] = IW2
[1] * 72.0f
/ 1016.0f
;
350 printf("initclip MP %.3f %.3f MO %.3f %.3f LI %.3f %.3f LI %.3f %.3f LI CP clip\n",
351 iw1
[0], iw1
[1], iw1
[0], iw2
[1], iw2
[0], iw2
[1], iw2
[0], iw1
[1]);
356 * Write the string to the output file...
359 va_start(ap
, format
);
360 bytes
= vprintf(format
, ap
);
368 * End of "$Id: hpgl-prolog.c 6649 2007-07-11 21:46:42Z mike $".