File: mpage.c

package info (click to toggle)
mpage 2.5.6-1
  • links: PTS
  • area: main
  • in suites: jessie, jessie-kfreebsd, squeeze, wheezy
  • size: 528 kB
  • ctags: 332
  • sloc: ansic: 3,630; makefile: 85; sh: 69
file content (381 lines) | stat: -rw-r--r-- 12,226 bytes parent folder | download
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
370
371
372
373
374
375
376
377
378
379
380
381
/*
 * mpage.c
 */

/*
 * mpage:    a program to reduce pages of print so that several pages
 *           of output appear on one printed page.
 *
 * Copyright (c) 1994-2004 Marcel J.E. Mol, The Netherlands
 * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
 *  
 *     Permission is granted to anyone to make or distribute verbatim
 *     copies of this document as received, in any medium, provided
 *     that this copyright notice is preserved, and that the
 *     distributor grants the recipient permission for further
 *     redistribution as permitted by this notice.
 *
 */

#include "mpage.h"
#include <string.h>
#include <time.h>
#include <fcntl.h>
#include <locale.h>


#include "encoding.h"

/*
 * Function Declarations
 */
static void ps_title();

char *current_locale;

int
main(argc, argv)
int argc;
char **argv;
{
    FILE *outfd;
    int currarg;
    struct sheet *thelist;
    struct sheet *thesheet;
    char outcommand[LINESIZE]; /* the command which is the output filter */

    current_locale = setlocale(LC_ALL,"");

   
#ifdef __EMX__
    /*
     * wildcard expansion from emx package (used by OS/2)
     */
    _wildcard (&argc, &argv);
#endif

    /*
     * Check page size for /etc/papersize
     */
    check_papersize();

    /*
     * examine the environment for PRINTER (or LPDEST) and MPAGE
     * environment variables
     */
    if ((currarg = do_env()) < 0) {
        usage(currarg);
        exit(1);
    }
        
    if ((currarg = do_args(argc, argv, 0)) < 0) {
        usage(currarg);
        exit(1);
    }

    /*
     * if a print queue was specified then create a print command using
     * the queue, else use standard output.
     */
    if (doprint) {
        if (printque != NULL)
            (void) sprintf(outcommand, "%s %s%s",
                           printprog, printarg, printque);
        else
            (void) strcpy(outcommand, printprog);
        if ((outfd = popen(outcommand, "w")) == NULL) {
            fprintf(stderr, "%s: cannot create pipe for '%s'\n",
                            MPAGE, outcommand);
            perror(MPAGE);
        }
    }
    else
        outfd = stdout;

    /*
     * pick the array of sheet lists based upon the specified option
     */
    if (sheetorder == UPDOWN)
        sheetlist = up_down;
    else
        sheetlist = left_right;

    /*
     * from the array of sheet lists pick the proper sheet list for
     * the given sheetaspect, then select the proper sheet format
     * for the given number of redueced pages per output page
     */
    thelist = sheetlist[sheetaspect];
    thesheet = &(thelist[sheetindex]);

    /* GPN. */
    if (Coli == 1)
        thesheet = &(coli[0]);
    if (Coli == 2)
        thesheet = &(coli[1]);
    if (Coli == 3)
        thesheet = &(coli[2]);

    orientation = (sheetindex + sheetaspect) & 0x01;

    /*
     * if either lines or columns were specified as options, over
     * the default sheets idea of the number of line or columns
     * per reduced page
     */
    if (opt_lines > 0)
        thesheet->sh_plength = opt_lines;
    if (opt_width > 0)
        thesheet->sh_cwidth = opt_width;

    /*
     * Prepare the textbox parameters when needed
     */
    if (opt_textbox) {
        textbox.over = textmargin_left;
        textbox.lift = textmargin_bottom;
        textbox.wide = thesheet->sh_cwidth  - (textmargin_left+textmargin_right);
        textbox.high = thesheet->sh_plength - (textmargin_top+textmargin_bottom);
    }

    /*
     * if there are arguments left over after processing options, then
     * these are names of files to process, else process the standard
     * input
     */
    if (currarg < argc) {
        ps_title(argv[currarg], outfd);
        for ( ; currarg < argc; currarg++)
            do_file(argv[currarg], thesheet, outfd);
    }
    else {
        ps_title("<stdin>", outfd);
        do_stdin(thesheet, outfd);
    }

    /*
     * in case files are merged on sheets, make sure the last page
     * is printed...
     */
    if (points->pp_origin_x != 0 && !opt_file) {
        if (had_ps)
            fprintf(outfd, "showsheet\n");
        else {
            fprintf(outfd, "restore\n");
            fprintf(outfd, "showpage\n");
        }
    }

    /*
     * having processed all input, print out a PS trailer
     * (timing stuff stolen from old adobe troff stuff)
     */
    fprintf(outfd, "%%%%Trailer\n");
    if (opt_verbose) {
        fprintf(outfd, "statusdict begin jobname print flush");
        fprintf(outfd, " (: Job finished:\\n) print\n");
        fprintf(outfd, "(\\tmpage time (s) = ) print flush usertime ");
        fprintf(outfd, "mp_stm sub 1000 div ==\n(\\tmpage pages = ) print");
        fprintf(outfd, " flush pagecount mp_pgc sub ==\nend flush\n");
        /* fprintf(outfd, "showpage\n"); */
    }
    fprintf(outfd, "%%%%Pages: %d\n", ps_outpages);
    if (opt_verbose) {
        fprintf(stderr, "[%s: %d pages, ", MPAGE, ps_outpages);
        if (doprint) {
            if (printque != NULL)
                fprintf(stderr, "print queue %s]\n", printque);
            else
                fprintf(stderr, "on default printer queue]\n");
        }
        else
            fprintf(stderr, "on <stdout>]\n");
    }
    /*
     * proper clean up to make sure the pipe is flushed
     */
    if (doprint)
        (void) pclose(outfd);

    return 0;

} /* main */



/*
 * ps_title prints a post script header suitable for PS processors
 */
static void
ps_title(name, outfd)
 char *name;
 FILE *outfd;
{
    time_t curr_time;
    char *time_str;
    FILE * charfp;
    char buf[LINESIZE];

    fprintf(outfd, "%%!PS-Adobe-2.0\n");
    fprintf(outfd, "%%%%DocumentFonts: %s Times-Bold\n", fontname);
    fprintf(outfd, "%%%%Title: %s (%s)\n", name, MPAGE);
    fprintf(outfd, "%%%%Creator: %s %s\n", MPAGE, VERSION);
    (void) time(&curr_time);
    time_str = ctime(&curr_time);
    fprintf(outfd, "%%%%CreationDate: %s", time_str);
    fprintf(outfd, "%%%%Orientation: %s\n", orientation ? "Landscape" : "Portrait");
    fprintf(outfd, "%%%%DocumentMedia: %s %d %d\n", media, ps_width, ps_height);
    fprintf(outfd, "%%%%BoundingBox: %d %d %d %d\n",
                   sheetmargin_left, sheetmargin_bottom,
                   ps_width - sheetmargin_right,
                   ps_height - sheetmargin_top);
    fprintf(outfd, "%%%%Pages: (atend)\n");
    fprintf(outfd, "%%%%EndComments\n\n");
    fprintf(outfd, "%%%%BeginProlog\n\n");
    fprintf(outfd, "/mp_stm usertime def\n");
    fprintf(outfd, "/mp_pgc statusdict begin pagecount end def\n");
    fprintf(outfd, "statusdict begin /jobname (%s) def end\n", name);
    if (opt_duplex) {
       fprintf(outfd, "statusdict /setduplexmode known");
       fprintf(outfd, " { statusdict begin true setduplexmode end } if\n");
       if (opt_tumble) {
           fprintf(outfd, "statusdict /settumble known ");
           fprintf(outfd, "{ statusdict begin true settumble end } if\n"); 
       }
    }
    else {
       /*
        * CAN WE DO THIS FOR PRINTERS THAT DO NOT NDERSTAND DUPLEX PRINTING??? 
        * It also seems to break for example the lp -o duplex command.
        * So better switch this part of.
        */
#if 0
       fprintf(outfd, "statusdict /setduplexmode known");
       fprintf(outfd, " { statusdict begin false setduplexmode end } if\n");
#endif
    }

    if (opt_encoding) {
        fprintf(outfd,
                "/reencsmalldict 12 dict def "
                "/ReEncodeSmall { reencsmalldict begin\n"
                "/newcodesandnames exch def "
                "/newfontname exch def "
                "/basefontname exch def\n"
                "/basefontdict basefontname findfont def "
                "/newfont basefontdict maxlength dict def\n"
                "basefontdict "
                "{ exch dup /FID ne "
                  "{ dup /Encoding eq "
                    "{ exch dup length array copy newfont 3 1 roll put } "
                    "{ exch newfont 3 1 roll put }\n"
                    "ifelse }\n"
                "  { pop pop }\n"
                "  ifelse } "
                "forall\n"
                "newfont /FontName newfontname put\n"
                "newcodesandnames aload pop "
                "newcodesandnames length 2 idiv\n"
                "{ newfont /Encoding get 3 1 roll put } "
                "repeat\n"
                "newfontname newfont definefont pop "
                "end } def\n");

        fprintf(outfd, "/charvec [\n");

        if (charvec_file != NULL) {
            if ((charfp = fopen(charvec_file, "r")) == NULL) {
                perror(charvec_file);
                exit(1);
            }
            while (fgets(buf, LINESIZE, charfp) != NULL) {
                if (*buf != '%' && *buf != '\n') {
                    if (first_encoding == -1) {
                        first_encoding = atoi(buf);
                        last_encoding = atoi(strchr(buf, ' '));
# ifdef DEBUG
                        fprintf(stderr, "first=%d, last=%d\n",
                                        first_encoding, last_encoding);
#endif
                    }
                    else
                        fprintf(outfd, "%s", buf);
                }
            }
        }
        else { /* use internal default encoding */
            int i;

            first_encoding = encoding_table_first;
            last_encoding  = encoding_table_last;
            for (i = 0; i <= last_encoding - first_encoding; i++)
                fprintf(outfd, "%s\n", encoding_table[i]);
        }
        fprintf(outfd, "] def\n");
        fprintf(outfd, "/%s /OurCharSet charvec ReEncodeSmall\n", fontname);
    }
    else {
        first_encoding = ' ';
        last_encoding = '~';
    }

    fprintf(outfd, "/textfont /%s findfont %d scalefont def\n",
                   opt_encoding ? "OurCharSet" : fontname, fsize - 1);
    fprintf(outfd, "/textfontbold /%s-Bold findfont %d scalefont def\n",
                   opt_encoding ? "OurCharSet" : fontname, fsize - 1);
    fprintf(outfd, "/fnamefont /Times-Bold findfont %d scalefont def\n", HSIZE);
    fprintf(outfd, "/headerfont /Times-Bold findfont %d scalefont def\n",
                   HSIZE - 2);
    fprintf(outfd, "textfont setfont\n");
    fprintf(outfd, "(a) stringwidth pop /mp_a_x exch def\n");

    if (current_locale){
      if (!strncmp (current_locale, "ja_JP", 5)){
#ifdef KANJI
#ifdef GOTHIC
	fprintf(outfd, "/kanj /GothicBBB-Medium-H findfont %d scalefont def\n", fsize - 1);
	fprintf(outfd, "/han /GothicBBB-Medium.Hankaku findfont %d scalefont def\n", fsize - 1);
#else
	fprintf(outfd, "/kanj /Ryumin-Light-H findfont %d scalefont def\n", fsize - 1);
	fprintf(outfd, "/han /Ryumin-Light.Hankaku findfont %d scalefont def\n", fsize - 1);
#endif
#endif
#ifdef GOTHIC
	fprintf(outfd, "/unijis /GothicBBB-Medium-UniJIS-UTF8-H findfont %d scalefont def\n", fsize - 1);
#else
	fprintf(outfd, "/unijis /Ryumin-Light-UniJIS-UTF8-H findfont %d scalefont def\n", fsize - 1);
#endif
      }
      else if (!strncmp (current_locale, "ko_KR", 5))
	fprintf(outfd, "/uniks /Baekmuk-Batang-UniKS-UTF8-H findfont %d scalefont def\n", fsize - 1);
      else if (!strncmp (current_locale, "zh_CN", 5))
	fprintf(outfd, "/unigb /BousungEG-Light-GB-UniGB-UTF8-H findfont %d scalefont def\n", fsize - 1);
      else if (!strncmp (current_locale, "zh_TW", 5))
	fprintf(outfd, "/unicns /ShanHeiSun-Light-UniCNS-UTF8-H findfont %d scalefont def\n", fsize - 1);
    }

# ifdef DEBUG
    if (Debug_flag & DB_PSMPAGE)
        fprintf(outfd, "(\\t'a' length ) print mp_a_x ==\nflush\n");
# endif
    fprintf(outfd, "%%%%EndProlog\n");

    if (opt_duplex) {
        fprintf(outfd, "%%%%BeginSetup\n");
        if (opt_tumble) {
            fprintf(outfd, "%%%%BeginFeature: *Duplex DuplexTumble\n");
            fprintf(outfd, "<< /Duplex true /Tumble true >> setpagedevice\n");
            fprintf(outfd, "%%%%EndFeature\n");
        }
        else {
            fprintf(outfd, "%%%%BeginFeature: *Duplex DuplexNoTumble\n");
            fprintf(outfd, "<< /Duplex true /Tumble false >> setpagedevice\n");
            fprintf(outfd, "%%%%EndFeature\n");
        }
        fprintf(outfd, "%%%%EndSetup\n");
    }

    return;

} /* ps_title */