summaryrefslogtreecommitdiff
path: root/dviware/dvitops/code.c
blob: dfa792f30caef67c9a5ee407608bdb624b2b70c0 (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
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
#include "dvitops.h"

static char rcsid[] = "$Header: /usr/jjc/dvitops/RCS/code.c,v 1.5 90/08/14 13:07:42 jjc Rel $";

static struct {
	char *name;
	int width;
	int height;
} paper_table[] = 
{
"letter", 612, 792,
"lettersmall", 612, 792,
"a4", 595, 842,
"A4", 595, 842,
"a4small", 595, 842,
"legal", 612, 1008,
"b5", 516, 729,
"b4", 729, 1032,
"tabloid", 792, 1224,
"ledger", 1224, 792,
"statement", 396, 612,
"executive", 540, 720,
"a3", 842, 1190,
"a5", 420, 595,
"folio", 612, 936,
"quarto", 610, 780,
"10x17", 720, 1224, 
NULL };

#define MAXCOLS 70
 
static FILE *psfp;
static int cols = 0; /* the number of characters on the current line */
 
#define NS 200
typedef struct string {
	size_t i; /* index into pool */
	int n; /* number of characters */
	int region;
	integer h;
	integer v;
	struct string *next;
} STRING;
 
struct rule {
	int region;
	integer a,b,h,v;
	struct rule *next;
} *rule_list = NULL;
 
static unsigned char *pool = NULL;
static size_t pool_size = 0, pool_used = 0;
 
STRING *string_list[MAXFONTS]; /* one list of strings for each font */
 
STRING *free_string_list;
 
#ifdef PROTO
void ps_string(unsigned char *s, int n);
void print_string_list(STRING *s);
static void print_rule_list(void);
#else
void ps_string();
void print_string_list();
static void print_rule_list();
#endif
 
static int page_number = 0;
int landscape = FALSE;
int magnification = 0;
 
void print_string_list(s)
STRING *s;
{
	int cc;
	integer  v;
	integer ox = 0, oy = 0; /* current origin */
	int r = NO_REGION; /* the current region */
	if (s == NULL)
		return;
	v = s->v + 1;  /* use the 'Z' command not the 'X' command */
	while (s != NULL) {
		if (s->region != r) {
			fprintf(psfp, "%% region %d\n", s->region);
			if (r != NO_REGION)
				fputs("grestore\n", psfp);
			if (s->region != NO_REGION) {
				fputs("gsave\n", psfp);
				do_transform(s->region, &ox, &oy, psfp);
			}
			else
				ox = oy = 0;
			r = s->region;
			v = s->v + 1; /* use the 'Z' command not the 'X' command */
		}
		if (s->v == v) {
			cols = put_dim((long)(s->h - ox), psfp);
			cc = 'X';
		}
		else {
			cols = put_dim((long)(s->h - ox), psfp);
			putc(' ', psfp);
			cols++;
			cols += put_dim((long)(s->v - oy), psfp);
			cc = 'Z';
		}
		v = s->v;
		ps_string(pool + s->i, s->n);
		putc(cc, psfp);
		putc('\n', psfp);
		s = s->next;
	}
	if (r != NO_REGION)
		fputs("grestore\n", psfp);
}
 
void set_string(p, n, f, h, v)
char *p;
int n, f;
integer h, v;
{
	STRING *s;
	if (!free_string_list) {
		int i;
		free_string_list = (STRING *)malloc(sizeof(STRING)*NS);
		if (free_string_list == NULL)
			out_of_memory();
		for (i = 0; i < NS - 1; ++i)
			free_string_list[i].next = free_string_list + i + 1;
		free_string_list[NS - 1].next = NULL;
	}
	s = free_string_list;
	free_string_list = free_string_list->next;
	if (pool == NULL || pool_size - pool_used < n) {
		if (pool == NULL) {
			pool_size = 5000;
			pool = (unsigned char *)malloc(pool_size);
			pool_used = 0;
		}
		else {
			if (pool_used > SIZE_MAX - n)
				message(FATAL_ERROR, "too many characters on the page");
			pool_size = pool_used + n;
			if (pool_size > SIZE_MAX/2)
				pool_size = SIZE_MAX;
			else
				pool_size <<= 1;
			pool = (unsigned char *)realloc((char *)pool, pool_size);
		}
		if (pool == NULL)
			out_of_memory();
	}
	s->i = pool_used;
	memcpy((char *)(pool + pool_used), p, n);
	pool_used += n;
	s->h = h;
	s->v = v;
	s->n = n;
	s->next = string_list[f];
	s->region = current_region;
	string_list[f] = s;
}
 
void set_rule(a, b, h, v)
integer a, b, h, v;
{
	struct rule *p;
	if (a <= 0 || b <= 0)
		return;
	p = (struct rule *)malloc(sizeof(struct rule));
	if (p == NULL)
		out_of_memory();
	p->a = a;
	p->b = b;
	p->h = h;
	p->v = v;
	p->region = current_region;
	p->next = rule_list;
	rule_list = p;
}
 
static void print_rule_list()
{
	int r = NO_REGION;
	integer ox = 0, oy = 0;
	struct rule *p = rule_list;
	while (p != NULL) {
		struct rule *q = p;
		if (p->region != r) {
			fprintf(psfp, "%% region %d\n", p->region);
			if (r != NO_REGION)
				fputs("grestore\n", psfp);
			if (p->region != NO_REGION) {
				fputs("gsave\n", psfp);
				do_transform(p->region, &ox, &oy, psfp);
			}
			else
				ox = oy = 0;
			r = p->region;
		}
		put_dim((long)(p->a), psfp);
		putc(' ', psfp);
		put_dim((long)(p->b), psfp);
		putc(' ', psfp);
		put_dim((long)(p->h - ox), psfp);
		putc(' ', psfp);
		put_dim((long)(p->v - oy), psfp);
		fputs(" R\n", psfp);
		p = p->next;
		free((char *)q);
	}
	rule_list = NULL;
	if (r != NO_REGION)
		fputs("grestore\n", psfp);
}
 
void eop(page)
struct page_info *page;
{
	char page_label[100];
	int i, j;
	char *ptr;
	int page_height, page_width;
	for (i = 0; paper_table[i].name != NULL; i++)
		if (strcmp(paper_table[i].name, page->paper) == 0)
			break;
	if (paper_table[i].name == NULL) {
		page_height = paper_table[0].height;
		page_width = paper_table[0].width;
		message(ERROR, "unrecognised page type %s", optarg);
	}
	else {
		page_height = paper_table[i].height;
		page_width = paper_table[i].width;
	}
	ptr = page_label;
	for (i = 9; page->count[i] == 0 && i > 0; --i)
		;
	for (j = 0; j < i; ++j) {
		sprintf(ptr, "%ld.", (long)(page->count[j]));
		ptr += strlen(ptr);
	}
	sprintf(ptr, "%ld", (long)page->count[i]);
	if (!quiet) 
		fprintf(stderr, "[%s", page_label);
	++page_number;
	fprintf(psfp, "\n%%%%Page: %s %d\n", page_label, page_number);
	fputs("dvitops begin\n", psfp);
	fprintf(psfp, "/#copies %d def BP\n", page->copies);
	p_form_list(psfp);
	if (landscape) {
		int n;
		fprintf(psfp, "%d landscape\n", page_width);
		n = page_height;
		page_height = page_width;
		page_width = n;
		landscape = FALSE;
	}
	if (magnification > 0) {
		page->mag = magnification;
		magnification = 0;
	}
	fprintf(psfp, "%ld ", (long)(page->num));
	put_dim((long)(page->den), psfp);
	fprintf(psfp, " %d %g %g %d SC\n", 
			page->mag, page->hoffset, page->voffset, page_height);
	p_inline_list(psfp);
	p_import_list(psfp, page);
#ifdef TPIC_SUPPORT
	p_tpic_list(psfp, page);
#endif
	print_rule_list();
	for (j = 0; j < MAXFONTS; ++j) {
		int emitted = FALSE;
		for (i = j; i != EOF; i = same_font(i)) {
			STRING *p = NULL;
			STRING *q = string_list[i];
			STRING *last = q;
			if (string_list[i] == NULL)
				continue;
			/* reverse the string list */
			while (q != NULL) {
				STRING *temp = q;
				q = q->next;
				temp->next = p;
				p = temp;
			}
			if (!f_is_blank(i)) {
				if (!emitted) {
					fputs("BO\n", psfp);
					f_emit(i, psfp);
					emitted = TRUE;
				}
				f_set(i, psfp);
				print_string_list(p);
			}
			last->next = free_string_list;
			free_string_list = p;
			string_list[i] = NULL;
		}
		if (emitted)
			fputs("EO\n", psfp);
	}
	pool_used = 0;
	fputs("\nEP end", psfp);
	if (ferror(psfp))
		message(FATAL_ERROR, "write error: disk full?");
	if (!quiet)
		fputs("] ", stderr);
	pool_used = 0;
	current_region = NO_REGION;
	nregions = 0;
}
 
void trailer()
{
	fputs("\n%%Trailer\n", psfp);
	if (!quiet)
		fputc('\n', stderr);
}
 
void prologue(fp, npages, is_reverse, paper)
FILE *fp;
int npages, is_reverse;
char *paper;
{
	time_t t;
	psfp = fp;
	(void)time(&t);
	fputs("%!PS-Adobe-2.0\n", psfp);
	fputs("%%Creator: dvitops\n", psfp);
	f_comment(psfp);
	fprintf(psfp, "%%%%CreationDate: %s", ctime(&t));
	fprintf(psfp, "%%%%DocumentPaperSizes: %s\n", paper);
	fprintf(psfp, "%%%%Pages: %d %d\n", npages, (is_reverse ? -1 : 1));
	fputs("%%EndComments\n", psfp);
	p_special_prologues(psfp);
	if (include_file("dvitops.pro", psfp) == EOF)
		message(FATAL_ERROR, "can't open dvitops.pro");
	fputs("dvitops begin\n", psfp);
	f_prologue(psfp);
	fputs("end\n", psfp);
	fputs("%%EndProlog\n", psfp);
	fprintf(psfp, "%%%%BeginSetup\n");
	fprintf(psfp, "%%%%PaperSize: %s\n", paper);
	fprintf(psfp, "%%%%EndSetup\n");
}


/* we asssume all seven-bit printable ascii characters are representable in the
   host character set */

#define ASCII_LEFT_PARENTHESIS 050
#define ASCII_RIGHT_PARENTHESIS 051
#define ASCII_BACKSLASH 0134

void ps_string(s, n)
unsigned char *s;
int n;
{
	cols++;
	putc('(', psfp);
	while (n--) {
		unsigned char c = *s++;
		if (c == ASCII_LEFT_PARENTHESIS 
			|| c == ASCII_RIGHT_PARENTHESIS 
			|| c == ASCII_BACKSLASH) {
			if (cols + 2 > MAXCOLS)
				cols = 0, fputs("\\\n", psfp);
			fputc('\\', psfp); fputc(XCHAR(c), psfp);
			cols += 2;
		}
		else if (c >= 040 && c < 0177) {
			if (cols + 1 > MAXCOLS)
				cols = 0, fputs("\\\n", psfp);
			putc(XCHAR(c), psfp);
			cols++;
		}
		else {
			if (cols + 4 > MAXCOLS)
				cols = 0, fputs("\\\n", psfp);
			fprintf(psfp, "\\%03o", c);
			cols += 4;
		}
	}
	putc(')', psfp);
}

static int fixed_point = 4;

#define LONG_DIGITS 10

int put_dim(n, fp)
long n;
FILE *fp;
{
	int len;
	/* room for a -, LONG_DIGITS digits, a decimal point,
	   and a terminating '\0' */
	char buf[LONG_DIGITS + 3];			
	char *p = buf + LONG_DIGITS + 2;
	int point = 0;
	buf[LONG_DIGITS + 2] = '\0';
	if (n >= 0) {
		do {
			*--p = '0' + (n % 10);
			n /= 10;
			if (++point == fixed_point)
				*--p = '.';
		} while (n != 0 || point < fixed_point);
	}
	else {						/* n < 0 */
		do {
			*--p = '0' - (n % 10);
			n /= 10;
			if (++point == fixed_point)
				*--p = '.';
		} while (n != 0 || point < fixed_point);
		*--p = '-';
	}
	if (fixed_point > 0) {
		char *q;
		/* there must be a dot, so this will terminate */
		for (q = buf + LONG_DIGITS + 2; q[-1] == '0'; --q)
			;
		if (q[-1] == '.') {
			if (q - 1 == p) {
				q[-1] = '0';
				q[0] = '\0';
			}
			else
				q[-1] = '\0';
		}
		else
			*q = '\0';
	}
	len = strlen(p);
	fputs(p, fp);
	return len;
}

/*
Local Variables:
tab-width: 4
c-indent-level: 4
c-continued-statement-offset: 4
c-brace-offset: -4
c-argdecl-indent: 0
c-label-offset: -4
End:
*/