summaryrefslogtreecommitdiff
path: root/filter/hpgl-prolog.c
blob: 00e512b7a9f18f7bddcb98a593e1121aed487949 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
/*
 * "$Id: hpgl-prolog.c 6649 2007-07-11 21:46:42Z mike $"
 *
 *   HP-GL/2 prolog routines for for the Common UNIX Printing System (CUPS).
 *
 *   Copyright 2007 by Apple Inc.
 *   Copyright 1993-2007 by Easy Software Products.
 *
 *   These coded instructions, statements, and computer programs are the
 *   property of Apple Inc. and are protected by Federal copyright
 *   law.  Distribution and use rights are outlined in the file "LICENSE.txt"
 *   which should have been included with this file.  If this file is
 *   file is missing or damaged, see the license at "http://www.cups.org/".
 *
 *   This file is subject to the Apple OS-Developed Software exception.
 *
 * Contents:
 *
 *  OutputProlog()  - Output the PostScript prolog...
 *  OutputTrailer() - Output the PostScript trailer...
 *  Outputf()       - Write a formatted string to the output file, creating the
 *                    page header as needed...
 */

/*
 * Include necessary headers...
 */

#include "hpgltops.h"
#include <stdarg.h>


/*
 * 'OutputProlog()' - Output the PostScript prolog...
 */

void
OutputProlog(char  *title,	/* I - Job title */
             char  *user,	/* I - Username */
             int   shading)	/* I - Type of shading */
{
  FILE		*prolog;	/* Prolog file */
  char		line[255];	/* Line from prolog file */
  const char	*datadir;	/* CUPS_DATADIR environment variable */
  char		filename[1024];	/* Name of prolog file */
  time_t	curtime;	/* Current time */
  struct tm	*curtm;		/* Current date */


  curtime = time(NULL);
  curtm   = localtime(&curtime);

  puts("%!PS-Adobe-3.0");
  printf("%%%%BoundingBox: %.0f %.0f %.0f %.0f\n",
         PageLeft, PageBottom, PageRight, PageTop);
  puts("%%Pages: (atend)");
  printf("%%%%LanguageLevel: %d\n", LanguageLevel);
  puts("%%DocumentData: Clean7Bit");
  puts("%%DocumentSuppliedResources: procset hpgltops 1.1 0");
  puts("%%DocumentNeededResources: font Courier Helvetica");
  puts("%%Creator: hpgltops/" CUPS_SVERSION);
  strftime(line, sizeof(line), "%c", curtm);
  printf("%%%%CreationDate: %s\n", line);
  WriteTextComment("Title", title);
  WriteTextComment("For", user);
  printf("%%cupsRotation: %d\n", (Orientation & 3) * 90);
  puts("%%EndComments");
  puts("%%BeginProlog");
  printf("/DefaultPenWidth %.2f def\n", PenWidth * 72.0 / 25.4);
  if (!shading)			/* Black only */
    puts("/setrgbcolor { pop pop pop } bind def");
  else if (!ColorDevice)	/* Greyscale */
    puts("/setrgbcolor { 0.08 mul exch 0.61 mul add exch 0.31 mul add setgray } bind def\n");

  if ((datadir = getenv("CUPS_DATADIR")) == NULL)
    datadir = CUPS_DATADIR;

  snprintf(filename, sizeof(filename), "%s/data/HPGLprolog", datadir);

  if ((prolog = fopen(filename, "r")) == NULL)
  {
    fprintf(stderr,
            "DEBUG: Unable to open HPGL prolog \"%s\" for reading - %s\n",
            filename, strerror(errno));
    exit(1);
  }

  while (fgets(line, sizeof(line), prolog) != NULL)
    fputs(line, stdout);

  fclose(prolog);

  puts("%%EndProlog");

  IN_initialize(0, NULL);
}


/*
 * 'OutputTrailer()' - Output the PostScript trailer...
 */

void
OutputTrailer(void)
{
  if (PageDirty)
    PG_advance_page(0, NULL);

  puts("%%Trailer");
  printf("%%%%Pages: %d\n", PageCount);
  puts("%%EOF");
}


/*
 * 'Outputf()' - Write a formatted string to the output file, creating the
 *               page header as needed...
 */

int				/* O - Number of bytes written */
Outputf(const char *format,	/* I - Printf-style string */
        ...)			/* I - Additional args as needed */
{
  va_list	ap;		/* Argument pointer */
  int		bytes;		/* Number of bytes written */
  float		iw1[2], iw2[2];	/* Clipping window */
  int		i;		/* Looping var */
  ppd_size_t	*size;		/* Page size */
  ppd_option_t	*option;	/* Page size option */
  ppd_choice_t	*choice;	/* Page size choice */
  float		width, length;	/* Page dimensions */
  int		landscape;	/* Rotate for landscape orientation? */


 /*
  * Write the page header as needed...
  */

  if (!PageDirty)
  {
    PageDirty = 1;
    PageCount ++;

    printf("%%%%Page: %d %d\n", PageCount, PageCount);

    landscape = 0;

    if (!FitPlot && PlotSizeSet)
    {
     /*
      * Set the page size for this page...
      */

      if (PageRotation == 0 || PageRotation == 180)
      {
	width  = PlotSize[0];
	length = PlotSize[1];
      }
      else
      {
	width  = PlotSize[1];
	length = PlotSize[0];
      }

      fprintf(stderr, "DEBUG: hpgltops setting page size (%.0f x %.0f)\n",
              width, length);

      if (PPD != NULL)
      {
        fputs("DEBUG: hpgltops has a PPD file!\n", stderr);

       /*
	* Lookup the closest PageSize and set it...
	*/

	for (i = PPD->num_sizes, size = PPD->sizes; i > 0; i --, size ++)
	  if ((fabs(length - size->length) < 36.0 && size->width >= width) ||
              (fabs(length - size->width) < 36.0 && size->length >= width))
	    break;

	if (i == 0 && PPD->variable_sizes)
	{
          for (i = PPD->num_sizes, size = PPD->sizes; i > 0; i --, size ++)
	    if (strcasecmp(size->name, "custom") == 0)
	      break;
	} 

	if (i > 0)
	{
	 /*
	  * Found a matching size...
	  */

	  option = ppdFindOption(PPD, "PageSize");
	  choice = ppdFindChoice(option, size->name);

          puts("%%BeginPageSetup");
          printf("%%%%BeginFeature: PageSize %s\n", size->name);

          if (strcasecmp(size->name, "custom") == 0)
	  {
	    PageLeft   = PPD->custom_margins[0];
	    PageRight  = width - PPD->custom_margins[2];
	    PageWidth  = width;
	    PageBottom = PPD->custom_margins[1];
	    PageTop    = length - PPD->custom_margins[3];
	    PageLength = length;

            printf("%.0f %.0f 0 0 0\n", width, length);

	    if (choice->code == NULL)
	    {
	     /*
	      * This can happen with certain buggy PPD files that don't include
	      * a CustomPageSize command sequence...  We just use a generic
	      * Level 2 command sequence...
	      */

	      puts("pop pop pop");
	      puts("<</PageSize[5 -2 roll]/ImagingBBox null>>setpagedevice\n");
	    }
            else
	    {
	     /*
	      * Use the vendor-supplied command...
	      */

	      printf("%s\n", choice->code);
	    }
	  }
	  else
	  {
	    if (choice->code)
              printf("%s\n", choice->code);

	    if (fabs(length - size->width) < 36.0)
	    {
	     /*
              * Do landscape orientation...
	      */

	      PageLeft   = size->bottom;
	      PageRight  = size->top;
	      PageWidth  = size->length;
	      PageBottom = size->left;
	      PageTop    = size->right;
	      PageLength = size->width;

              landscape = 1;
	    }
	    else
	    {
	     /*
              * Do portrait orientation...
	      */

	      PageLeft   = size->left;
	      PageRight  = size->right;
	      PageWidth  = size->width;
	      PageBottom = size->bottom;
	      PageTop    = size->top;
	      PageLength = size->length;
	    }
	  }

	  puts("%%EndFeature");
	  puts("%%EndPageSetup");
	}
      }
      else
      {
        fputs("DEBUG: hpgltops does not have a PPD file!\n", stderr);

        puts("%%BeginPageSetup");
        printf("%%%%BeginFeature: PageSize w%.0fh%.0f\n", width, length);
	printf("<</PageSize[%.0f %.0f]/ImageBBox null>>setpagedevice\n",
	       width, length);
	puts("%%EndFeature");
	puts("%%EndPageSetup");

	PageLeft   = 0.0;
	PageRight  = width;
	PageWidth  = width;
	PageBottom = 0.0;
	PageTop    = length;
	PageLength = length;
      }
    }

    define_font(0);
    define_font(1);

    printf("%.1f setmiterlimit\n", MiterLimit);
    printf("%d setlinecap\n", LineCap);
    printf("%d setlinejoin\n", LineJoin);

    printf("%.3f %.3f %.3f %.2f SP\n", Pens[1].rgb[0], Pens[1].rgb[1],
           Pens[1].rgb[2], Pens[1].width * PenScaling);

    puts("gsave");

    if (Duplex && (PageCount & 1) == 0)
      switch ((PageRotation / 90 + landscape) & 3)
      {
	case 0 :
            printf("%.1f %.1f translate\n", PageWidth - PageRight, PageBottom);
	    break;
	case 1 :
            printf("%.0f 0 translate 90 rotate\n", PageLength);
            printf("%.1f %.1f translate\n", PageLength - PageTop,
	           PageWidth - PageRight);
	    break;
	case 2 :
            printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength);
            printf("%.1f %.1f translate\n", PageLeft, PageLength - PageTop);
	    break;
	case 3 :
            printf("0 %.0f translate -90 rotate\n", PageWidth);
            printf("%.1f %.1f translate\n", PageBottom, PageLeft);
	    break;
      }
    else
      switch ((PageRotation / 90 + landscape) & 3)
      {
	case 0 :
            printf("%.1f %.1f translate\n", PageLeft, PageBottom);
	    break;
	case 1 :
            printf("%.0f 0 translate 90 rotate\n", PageLength);
            printf("%.1f %.1f translate\n", PageBottom, PageWidth - PageRight);
	    break;
	case 2 :
            printf("%.0f %.0f translate 180 rotate\n", PageWidth, PageLength);
            printf("%.1f %.1f translate\n", PageWidth - PageRight,
	            PageLength - PageTop);
	    break;
	case 3 :
            printf("0 %.0f translate -90 rotate\n", PageWidth);
            printf("%.1f %.1f translate\n", PageLength - PageTop, PageLeft);
	    break;
      }

    if (IW1[0] != IW2[0] && IW1[1] != IW2[1])
    {
      iw1[0] = IW1[0] * 72.0f / 1016.0f;
      iw1[1] = IW1[1] * 72.0f / 1016.0f;
      iw2[0] = IW2[0] * 72.0f / 1016.0f;
      iw2[1] = IW2[1] * 72.0f / 1016.0f;

      printf("initclip MP %.3f %.3f MO %.3f %.3f LI %.3f %.3f LI %.3f %.3f LI CP clip\n",
	     iw1[0], iw1[1], iw1[0], iw2[1], iw2[0], iw2[1], iw2[0], iw1[1]);
    }
  }

 /*
  * Write the string to the output file...
  */

  va_start(ap, format);
  bytes = vprintf(format, ap);
  va_end(ap);

  return (bytes);
}


/*
 * End of "$Id: hpgl-prolog.c 6649 2007-07-11 21:46:42Z mike $".
 */