]>
git.ipfire.org Git - thirdparty/cups.git/blob - filter/hpgl-prolog.c
97ec87f44aab27b89a8d5c71d567958e82d8c4fd
2 * "$Id: hpgl-prolog.c,v 1.36 2002/12/17 18:59:25 swdev Exp $"
4 * HP-GL/2 prolog routines for for the Common UNIX Printing System (CUPS).
6 * Copyright 1993-2003 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-3111 USA
20 * Voice: (301) 373-9603
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 * OutputProlog() - Output the PostScript prolog...
29 * OutputTrailer() - Output the PostScript trailer...
30 * Outputf() - Write a formatted string to the output file, creating the
31 * page header as needed...
35 * Include necessary headers...
43 * 'OutputProlog()' - Output the PostScript prolog...
47 OutputProlog(char *title
, /* I - Job title */
48 char *user
, /* I - Username */
49 int shading
) /* I - Type of shading */
51 FILE *prolog
; /* Prolog file */
52 char line
[255]; /* Line from prolog file */
53 const char *datadir
; /* CUPS_DATADIR environment variable */
54 char filename
[1024]; /* Name of prolog file */
55 time_t curtime
; /* Current time */
56 struct tm
*curtm
; /* Current date */
60 curtm
= localtime(&curtime
);
62 puts("%!PS-Adobe-3.0");
63 printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
64 PageLeft
, PageBottom
, PageRight
, PageTop
);
65 puts("%%Pages: (atend)");
66 printf("%%%%LanguageLevel: %d\n", LanguageLevel
);
67 puts("%%DocumentData: Clean7Bit");
68 puts("%%DocumentSuppliedResources: procset hpgltops 1.1 0");
69 puts("%%DocumentNeededResources: font Courier Helvetica");
70 puts("%%Creator: hpgltops/" CUPS_SVERSION
);
71 strftime(line
, sizeof(line
), CUPS_STRFTIME_FORMAT
, curtm
);
72 printf("%%%%CreationDate: %s\n", line
);
73 printf("%%%%Title: %s\n", title
);
74 printf("%%%%For: %s\n", user
);
75 printf("%%cupsRotation: %d\n", (Orientation
& 3) * 90);
76 puts("%%EndComments");
77 puts("%%BeginProlog");
78 printf("/DefaultPenWidth %.2f def\n", PenWidth
* 72.0 / 25.4);
79 puts("3.0 setmiterlimit");
80 if (!shading
) /* Black only */
81 puts("/setrgbcolor { pop pop pop } bind def");
82 else if (!ColorDevice
) /* Greyscale */
83 puts("/setrgbcolor { 0.08 mul exch 0.61 mul add exch 0.31 mul add setgray } bind def\n");
85 if ((datadir
= getenv("CUPS_DATADIR")) == NULL
)
86 datadir
= CUPS_DATADIR
;
88 snprintf(filename
, sizeof(filename
), "%s/data/HPGLprolog", datadir
);
90 if ((prolog
= fopen(filename
, "r")) == NULL
)
92 fprintf(stderr
, "ERROR: Unable to open HPGL prolog \"%s\" for reading - %s\n",
93 filename
, strerror(errno
));
97 while (fgets(line
, sizeof(line
), prolog
) != NULL
)
104 IN_initialize(0, NULL
);
109 * 'OutputTrailer()' - Output the PostScript trailer...
116 PG_advance_page(0, NULL
);
119 printf("%%%%Pages: %d\n", PageCount
);
125 * 'Outputf()' - Write a formatted string to the output file, creating the
126 * page header as needed...
129 int /* O - Number of bytes written */
130 Outputf(const char *format
, /* I - Printf-style string */
131 ...) /* I - Additional args as needed */
133 va_list ap
; /* Argument pointer */
134 int bytes
; /* Number of bytes written */
135 float iw1
[2], iw2
[2]; /* Clipping window */
136 int i
; /* Looping var */
137 ppd_size_t
*size
; /* Page size */
138 ppd_option_t
*option
; /* Page size option */
139 ppd_choice_t
*choice
; /* Page size choice */
140 float width
, length
; /* Page dimensions */
141 int landscape
; /* Rotate for landscape orientation? */
145 * Write the page header as needed...
153 printf("%%%%Page: %d %d\n", PageCount
, PageCount
);
157 if (!FitPlot
&& PlotSizeSet
)
160 * Set the page size for this page...
163 if (PageRotation
== 0 || PageRotation
== 180)
166 length
= PlotSize
[1];
171 length
= PlotSize
[0];
174 fprintf(stderr
, "DEBUG: hpgltops setting page size (%.0f x %.0f)\n",
179 fputs("DEBUG: hpgltops has a PPD file!\n", stderr
);
182 * Lookup the closest PageSize and set it...
185 for (i
= PPD
->num_sizes
, size
= PPD
->sizes
; i
> 0; i
--, size
++)
186 if ((fabs(length
- size
->length
) < 36.0 && size
->width
>= width
) ||
187 (fabs(length
- size
->width
) < 36.0 && size
->length
>= width
))
190 if (i
== 0 && PPD
->variable_sizes
)
192 for (i
= PPD
->num_sizes
, size
= PPD
->sizes
; i
> 0; i
--, size
++)
193 if (strcasecmp(size
->name
, "custom") == 0)
200 * Found a matching size...
203 option
= ppdFindOption(PPD
, "PageSize");
204 choice
= ppdFindChoice(option
, size
->name
);
206 puts("%%BeginPageSetup");
207 printf("%%%%BeginFeature: PageSize %s\n", size
->name
);
209 if (strcasecmp(size
->name
, "custom") == 0)
211 PageLeft
= PPD
->custom_margins
[0];
212 PageRight
= width
- PPD
->custom_margins
[2];
214 PageBottom
= PPD
->custom_margins
[1];
215 PageTop
= length
- PPD
->custom_margins
[3];
218 printf("%.0f %.0f 0 0 0\n", width
, length
);
220 if (choice
->code
== NULL
)
223 * This can happen with certain buggy PPD files that don't include
224 * a CustomPageSize command sequence... We just use a generic
225 * Level 2 command sequence...
229 puts("<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n");
234 * Use the vendor-supplied command...
237 printf("%s\n", choice
->code
);
243 printf("%s\n", choice
->code
);
245 if (fabs(length
- size
->width
) < 36.0)
248 * Do landscape orientation...
251 PageLeft
= size
->bottom
;
252 PageRight
= size
->top
;
253 PageWidth
= size
->length
;
254 PageBottom
= size
->left
;
255 PageTop
= size
->right
;
256 PageLength
= size
->width
;
263 * Do portrait orientation...
266 PageLeft
= size
->left
;
267 PageRight
= size
->right
;
268 PageWidth
= size
->width
;
269 PageBottom
= size
->bottom
;
271 PageLength
= size
->length
;
275 puts("%%EndFeature");
276 puts("%%EndPageSetup");
281 fputs("DEBUG: hpgltops does not have a PPD file!\n", stderr
);
283 puts("%%BeginPageSetup");
284 printf("%%%%BeginFeature: PageSize w%.0fh%.0f\n", width
, length
);
285 printf("<</PageSize[%.0f %.0f]/ImageBBox null>>setpagedevice\n",
287 puts("%%EndFeature");
288 puts("%%EndPageSetup");
300 " /%s%s%s%s findfont\n"
301 " [ %f %f %f %f 0.0 0.0 ] makefont\n"
304 AlternateFont
.typeface
== 48 ? "Courier" : "Helvetica",
305 (AlternateFont
.weight
!= 0 || AlternateFont
.posture
!= 0) ? "-" : "",
306 AlternateFont
.weight
!= 0 ? "Bold" : "",
307 AlternateFont
.posture
!= 0 ? "Oblique" : "",
308 AlternateFont
.x
* AlternateFont
.height
,
309 -AlternateFont
.y
* AlternateFont
.height
,
310 AlternateFont
.y
* AlternateFont
.height
,
311 AlternateFont
.x
* AlternateFont
.height
);
314 " /%s%s%s%s findfont\n"
315 " [ %f %f %f %f 0.0 0.0 ] makefont\n"
318 StandardFont
.typeface
== 48 ? "Courier" : "Helvetica",
319 (StandardFont
.weight
!= 0 || StandardFont
.posture
!= 0) ? "-" : "",
320 StandardFont
.weight
!= 0 ? "Bold" : "",
321 StandardFont
.posture
!= 0 ? "Oblique" : "",
322 StandardFont
.x
* StandardFont
.height
,
323 -StandardFont
.y
* StandardFont
.height
,
324 StandardFont
.y
* StandardFont
.height
,
325 StandardFont
.x
* StandardFont
.height
);
332 printf("%.1f setmiterlimit\n", MiterLimit
);
333 printf("%d setlinecap\n", LineCap
);
334 printf("%d setlinejoin\n", LineJoin
);
336 printf("%.3f %.3f %.3f %.2f SP\n", Pens
[1].rgb
[0], Pens
[1].rgb
[1],
337 Pens
[1].rgb
[2], Pens
[1].width
* PenScaling
);
341 if (Duplex
&& (PageCount
& 1) == 0)
342 switch ((PageRotation
/ 90 + landscape
) & 3)
345 printf("%.1f %.1f translate\n", PageWidth
- PageRight
, PageBottom
);
348 printf("%.0f 0 translate 90 rotate\n", PageLength
);
349 printf("%.1f %.1f translate\n", PageLength
- PageTop
,
350 PageWidth
- PageRight
);
353 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
354 printf("%.1f %.1f translate\n", PageLeft
, PageLength
- PageTop
);
357 printf("0 %.0f translate -90 rotate\n", PageWidth
);
358 printf("%.1f %.1f translate\n", PageBottom
, PageLeft
);
362 switch ((PageRotation
/ 90 + landscape
) & 3)
365 printf("%.1f %.1f translate\n", PageLeft
, PageBottom
);
368 printf("%.0f 0 translate 90 rotate\n", PageLength
);
369 printf("%.1f %.1f translate\n", PageBottom
, PageWidth
- PageRight
);
372 printf("%.0f %.0f translate 180 rotate\n", PageWidth
, PageLength
);
373 printf("%.1f %.1f translate\n", PageWidth
- PageRight
,
374 PageLength
- PageTop
);
377 printf("0 %.0f translate -90 rotate\n", PageWidth
);
378 printf("%.1f %.1f translate\n", PageLength
- PageTop
, PageLeft
);
382 if (IW1
[0] != IW2
[0] && IW1
[1] != IW2
[1])
384 iw1
[0] = IW1
[0] * 72.0f
/ 1016.0f
;
385 iw1
[1] = IW1
[1] * 72.0f
/ 1016.0f
;
386 iw2
[0] = IW2
[0] * 72.0f
/ 1016.0f
;
387 iw2
[1] = IW2
[1] * 72.0f
/ 1016.0f
;
389 printf("initclip MP %.3f %.3f MO %.3f %.3f LI %.3f %.3f LI %.3f %.3f LI CP clip\n",
390 iw1
[0], iw1
[1], iw1
[0], iw2
[1], iw2
[0], iw2
[1], iw2
[0], iw1
[1]);
395 * Write the string to the output file...
398 va_start(ap
, format
);
399 bytes
= vprintf(format
, ap
);
407 * End of "$Id: hpgl-prolog.c,v 1.36 2002/12/17 18:59:25 swdev Exp $".