File: page.c

package info (click to toggle)
mpage 2.5.1pre-6
  • links: PTS
  • area: main
  • in suites: woody
  • size: 348 kB
  • ctags: 293
  • sloc: ansic: 2,864; makefile: 79; sh: 68
file content (454 lines) | stat: -rw-r--r-- 11,264 bytes parent folder | download | duplicates (2)
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
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
/*
 * page.c
 */

/*
 * mpage:	a program to reduce pages of print so that several pages
 * 	  	of output appear on one printed page.
 *
 * Written by:
 *   ...!uunet!\                       Mark Hahn, Sr Systems Engineer
 *              >pyrdc!mark            Pyramid Technology Corporation
 * ...!pyramid!/                       Vienna, Va    (703)848-2050
 *
 *
 * Copyright (c) 1988 Mark P. Hahn, Herndon, Virginia
 * Copyright (c) 1994-1997 Marcel J.E. Mol, The Netherlands
 *                    marcel@mesa.nl
 *  
 *     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"



void
set_page()
{
#ifndef LIBPAPER
    int i = select_pagetype(opt_page);
 
    media = paper[i].media;
    ps_width = paper[i].width;
    ps_height = paper[i].height;
#else
    struct paper * ps;
    ps=paperinfo(opt_page);
    if (!ps) {
      fprintf(stderr,"%s: unknown page format: %s\n", 
              MPAGE, opt_page);
      exit(1);
    }
    media = papername(ps);
    ps_width = paperpswidth(ps);
    ps_height = paperpsheight(ps);
#endif
    return;

} /* set_page */



int
select_pagetype(char *pt)
{
    int i = 0;

#ifndef LIBPAPER
    while (paper[i].media && strcasecmp(pt, paper[i].media))
        i++;
    if (paper[i].media)
        opt_page = paper[i].media;
    else
#else
    struct paper * ps;

    strncpy(opt_page,pt,31);
    opt_page[31]='\0';

    ps=paperinfo(opt_page);
    if (!ps)
#endif
        fprintf(stderr, "%s: ignoring unknown paper type: %s\n",
                                MPAGE, pt);
    return i;
} /* select_pagetype */



void
show_pagetypes()
{
    int i = 0;

    printf("Mpage knows about the following paper types:\n");
    printf("Type           Points Wide Points High\n");
    printf("-------------- ----------- -----------\n");
    while (paper[i].media) {
        printf("%-14s %11d %11d\n",
               paper[i].media, paper[i].width, paper[i].height);
        i++;
    }

    return;

} /* show_pagetypes */



/*
 * Bases are the base for each page. e.g the corner points for outlines.
 */
   /*  __________________________
    * | ________________________ |
    * ||                        ||
    * ||                        ||
    * ||                 ybase4 ||
    * ||                        Y|
    * ||                        ||
    * ||                        ||
    * ||                 ybase3 ||
    * ||                        Y|
    * ||                        ||
    * ||                        ||
    * ||                 ybase2 ||
    * ||                        Y|
    * ||                        ||
    * ||                        ||
    * ||xbase1   xbase2  ybase1 ||
    * |X___________X____________Y|
    * |__________________________|
    */

int
xbase1()
{
	Debug(DB_POINTS, "%%xbase1: %d\n", sheetmargin_left + sheetheader_left);
	return sheetmargin_left + sheetheader_left;
} /* xbase1 */



int
xbase2()
{
	Debug(DB_POINTS, "%%xbase2: %d\n",
                (ps_width - sheetmargin_left - sheetmargin_right
                          - sheetheader_left - sheetheader_right) / 2 +
                sheetmargin_left + sheetheader_left);
        return  (ps_width - sheetmargin_left - sheetmargin_right
                          - sheetheader_left - sheetheader_right) / 2 +
                sheetmargin_left + sheetheader_left;
} /* xbase2 */



int
ybase1()
{
	Debug(DB_POINTS, "%%ybase1: %d\n",
               sheetmargin_bottom + sheetheader_bottom);
	return sheetmargin_bottom + sheetheader_bottom;
} /* ybase1 */



int
ybase2()
{
	Debug(DB_POINTS, "%%ybase2: %d\n",
	        (ps_height - sheetmargin_bottom - sheetmargin_top
                           - sheetheader_bottom - sheetheader_top) / 4
	        + sheetmargin_bottom + sheetheader_bottom);
	return (ps_height - sheetmargin_bottom - sheetmargin_top
                          - sheetheader_bottom - sheetheader_top) / 4
	        + sheetmargin_bottom + sheetheader_bottom;
} /* ybase2 */



int
ybase3()
{
	Debug(DB_POINTS, "%%ybase3: %d\n",
	        (ps_height - sheetmargin_bottom - sheetmargin_top
                           - sheetheader_bottom - sheetheader_top) / 2
	        + sheetmargin_bottom + sheetheader_bottom);
	return (ps_height - sheetmargin_bottom - sheetmargin_top
                          - sheetheader_bottom - sheetheader_top) / 2
	        + sheetmargin_bottom + sheetheader_bottom;
} /* ybase3 */



int
ybase4()
{
	Debug(DB_POINTS, "%%ybase4: %d\n",
	        (ps_height - sheetmargin_bottom - sheetmargin_top
                           - sheetheader_bottom - sheetheader_top) * 3 / 4
	        + sheetmargin_bottom + sheetheader_bottom);
	return  (ps_height - sheetmargin_bottom - sheetmargin_top
                           - sheetheader_bottom - sheetheader_top) * 3 / 4
	        + sheetmargin_bottom + sheetheader_bottom;
} /* ybase4 */



int
ytop1()
{
	Debug(DB_POINTS, "%%ytop1: %d\n", ybase2());
	/* return ybase1() + yht4(); */
	return ybase2();
} /* ytop1 */



int
ytop2()
{
	Debug(DB_POINTS, "%%ytop2: %d\n", ybase3());
	/* return ybase2() + yht4(); */
	return ybase3();
} /* ytop2 */



int
ytop3()
{
	Debug(DB_POINTS, "%%ytop3: %d\n", ybase4());
	/* return ybase3() + yht4(); */
	return ybase4();
} /* ytop3 */



int
ytop4()
{
	Debug(DB_POINTS, "%%ytop4: %d\n", 
	       ps_height - sheetmargin_bottom - sheetmargin_top
	                 - sheetheader_bottom - sheetheader_top
	                 + sheetmargin_bottom + sheetheader_bottom);
	/* return ybase4() + yht4(); */
	return ps_height - sheetmargin_bottom - sheetmargin_top
	                 - sheetheader_bottom - sheetheader_top
	                 + sheetmargin_bottom + sheetheader_bottom;
} /* ytop4 */



int
xwid1()
{
	Debug(DB_POINTS, "%%xwid1: %d\n",
	       ps_width - sheetmargin_left - sheetmargin_right
	                - sheetheader_left - sheetheader_right);
	return ps_width - sheetmargin_left - sheetmargin_right
	                - sheetheader_left - sheetheader_right;
} /* xwid1 */



int
xwid2()
{
	Debug(DB_POINTS, "%%xwid2: %d\n",
	       (ps_width - sheetmargin_left - sheetmargin_right
	                 - sheetheader_left - sheetheader_right) / 2);
	return (ps_width - sheetmargin_left - sheetmargin_right
	                 - sheetheader_left - sheetheader_right) / 2;
} /* xwid2 */



int
yht1()
{
	Debug(DB_POINTS, "%%yht1: %d\n",
	       ps_height - sheetmargin_top - sheetmargin_bottom
	                 - sheetheader_top - sheetheader_bottom);
	return ps_height - sheetmargin_top - sheetmargin_bottom
	                 - sheetheader_top - sheetheader_bottom;
} /* yht1 */



int
yht2()
{
	Debug(DB_POINTS, "%%yht2: %d\n",
	       (ps_height - sheetmargin_top - sheetmargin_bottom
	                  - sheetheader_top - sheetheader_bottom) / 2);
	return (ps_height - sheetmargin_top - sheetmargin_bottom
	                  - sheetheader_top - sheetheader_bottom) / 2;
} /* yht2 */



int
yht4()
{
	Debug(DB_POINTS, "%%yht4: %d\n",
	       (ps_height - sheetmargin_top - sheetmargin_bottom
	                  - sheetheader_top - sheetheader_bottom) / 4);
	return (ps_height - sheetmargin_top - sheetmargin_bottom
	                  - sheetheader_top - sheetheader_bottom) / 4;
} /* yht4 */



void
outline_1(outfd)
 FILE *outfd;
{
	/* one page outline */
	fprintf(outfd, "0 setlinewidth\n");
        fprintf(outfd, "%d %d moveto 0 %d rlineto\n",
                       xbase1(), ybase1(), yht1());
	fprintf(outfd, "%d 0 rlineto 0 %d rlineto closepath stroke\n",
                       xwid1(), -yht1());

        return;
} /* outline_1 */



void
outline_2(outfd)
 FILE *outfd;
{
	/* two page outline */
	outline_1(outfd);
	fprintf(outfd, "%d %d moveto %d 0 rlineto stroke\n",
                xbase1(), ybase3(), xwid1());

        return;
} /* outline_2 */



void
outline_4(outfd)
 FILE *outfd;
{
	/* four page outline */
	outline_2(outfd);
	fprintf(outfd, "%d %d moveto 0 %d rlineto stroke\n",
                xbase2(), ybase1(), yht1());

        return;
} /* outline_4 */



void
outline_8(outfd)
 FILE *outfd;
{
	/* eight page outline */
	outline_4(outfd);
	fprintf(outfd, "%d %d moveto %d 0 rlineto stroke\n",
                xbase1(), ybase2(), xwid1());
	fprintf(outfd, "%d %d moveto %d 0 rlineto stroke\n",
                xbase1(), ybase4(), xwid1());

        return;
} /* outline_8 */



void
mp_outline(outfd, asheet)
 FILE *outfd;
 struct sheet *asheet;
{

    if (opt_outline) {
        (*asheet->sh_outline)(outfd);
    }

    return;

} /* mp_outline */



void
sheetheader(outfd, fname)
 FILE *outfd;
 char *fname;
{

    if (opt_sheetheader) {
        fprintf(outfd, "gsave\nheaderfont setfont\n");
        switch (orientation) {
            case PORTRAIT:
                sheetheader_top = 14;
                if (*sheethead != '\0') {
                    fprintf(outfd,
                           "%d %d (%s) stringwidth pop sub 2 div add %d moveto",
                            xbase1(), xwid1(), sheethead,
                            ytop4() + fsize / 4);
                    fprintf(outfd, " (%s) show\n", sheethead);
                }
                else {
                    /*
                     * Filename on the left
                     */
                    fprintf(outfd, "%d %d moveto (%s) show\n",
                            xbase1() + 3, ytop4() + fsize / 4, fname);
                    /*
                     * Pagenumber on the right
                     */
                    fprintf(outfd,
                            "%d (Page %d) stringwidth pop sub 3 sub %d moveto",
                            xbase1() + xwid1(), ps_pagenum,
                            ytop4() + fsize / 4);
                    fprintf(outfd, " (Page %d) show\n", ps_pagenum);
                }
                break;
            case LANDSCAPE:
            case LANDSCAPE_PORTRAIT:
                sheetheader_right = 14;
                if (*sheethead != '\0') {
                    fprintf(outfd,
                           "%d %d (%s) stringwidth pop 2 div add moveto",
                            xbase1() + xwid1() + fsize/4,
                            ybase3(), sheethead);
                    fprintf(outfd, " -90 rotate (%s) show\n", sheethead);
                }
                else {
                    /*
                     * Filename on the left
                     */
                    fprintf(outfd, "%d %d moveto -90 rotate (%s) show\n",
                            xbase1() + xwid1() + fsize/4, ytop4() - 3, fname);
                    /*
                     * Pagenumber on the right
                     */
                    fprintf(outfd, "90 rotate %d %d (Page %d) stringwidth pop ",
                                   xbase1() + xwid1() + fsize/4,
                                   ybase1() + 3, ps_pagenum);
                    fprintf(outfd, "add moveto -90 rotate (Page %d) show\n",
                                   ps_pagenum);
                }
                break;
        }
        fprintf(outfd, "grestore\n");
    }

    return;

} /* sheetheader */