aboutsummaryrefslogtreecommitdiff
path: root/troff/troff.d/dpost.d/ps_include.c
blob: bd65398f26724ff1e5568ba1e33f308e3f361a8a (plain) (blame)
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
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*	Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T	*/
/*	  All Rights Reserved  	*/


/*	from OpenSolaris "ps_include.c	1.5	05/06/08 SMI"	 SVr4.0 1.3		*/

/*
 * Portions Copyright (c) 2005 Gunnar Ritter, Freiburg i. Br., Germany
 *
 * Sccsid @(#)ps_include.c	1.10 (gritter) 10/15/06
 */

/*
 *
 * Picture inclusion code for PostScript printers.
 *
 */


#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "gen.h"
#include "ext.h"
#include "path.h"
#include "asciitype.h"
#include "global.h"


#define var(x)		fprintf(fout, "/%s %g def\n", #x, x)
#define	has(word)	_has(buf, word)
#define grab(n)		((Section *)(nglobal \
			? realloc((char *)global, n*sizeof(Section)) \
			: calloc(n, sizeof(Section))))


static char	*buf;
static size_t	bufsize;
typedef struct {long start, end;} Section;

static void copy(FILE *, FILE *, Section *);
static char *_has(char *, const char *);
static void addfonts(char *);

/*****************************************************************************/


void
ps_include(


    const char	*name,			/* file name */
    FILE	*fin, FILE *fout,	/* input and output files */
    int		page_no,		/* physical page number from *fin */
    int		whiteout,		/* erase picture area */
    int		outline,		/* draw a box around it and */
    int		scaleboth,		/* scale both dimensions - if not zero */
    double	cx, double cy,		/* center of the picture and */
    double	sx, double sy,		/* its size - in current coordinates */
    double	ax, double ay,		/* left-right, up-down adjustment */
    double	rot			/* rotation - in clockwise degrees */
)


{

    static int	gotinclude;

    int		foundpage = 0;		/* found the page when non zero */
    int		nglobal = 0;		/* number of global defs so far */
    int		maxglobal = 0;		/* and the number we've got room for */
    Section	prolog, page, trailer;	/* prologue, page, and trailer offsets */
    Section	*global = 0;		/* offsets for all global definitions */
    double	llx, lly;		/* lower left and */
    double	urx, ury;		/* upper right corners - default coords */
    double	w = whiteout != 0;	/* mostly for the var() macro */
    double	o = outline != 0;
    double	s = scaleboth != 0;
    int		i;			/* loop index */
    int		lno = 0;
    int		epsf = 0;
    int		hires = 0;
    int		state = 0;
    int		indoc = 0;
    char	*bp, *cp;
    enum {
	    NORMAL,
	    DOCUMENTFONTS,
	    DOCUMENTNEEDEDRESOURCES,
    } cont = NORMAL;


/*
 *
 * Reads a PostScript file (*fin), and uses structuring comments to locate the
 * prologue, trailer, global definitions, and the requested page. After the whole
 * file is scanned, the  special ps_include PostScript definitions are copied to
 * *fout, followed by the prologue, global definitions, the requested page, and
 * the trailer. Before returning the initial environment (saved in PS_head) is
 * restored.
 *
 * By default we assume the picture is 8.5 by 11 inches, but the BoundingBox
 * comment, if found, takes precedence.
 *
 */

	if (gotinclude == 0 && access(PSINCLUDEFILE, 04) == 0) {
		doglobal(PSINCLUDEFILE);
		gotinclude++;
	}

	llx = lly = 0;			/* default BoundingBox - 8.5x11 inches */
	urx = 72 * 8.5;
	ury = 72 * 11.0;

	/* section boundaries and bounding box */

	prolog.start = prolog.end = 0;
	page.start = page.end = 0;
	trailer.start = 0;
	fseek(fin, 0L, SEEK_SET);

	while ( psgetline(&buf, &bufsize, NULL, fin) != NULL ) {
		if (++lno == 1 && strncmp(buf, "%!PS-", 5) == 0) {
			for (bp = buf; !spacechar(*bp&0377); bp++);
			while (*bp && *bp != '\n' && *bp != '\r' &&
					spacechar(*bp&0377))
				bp++;
			if (strncmp(bp, "EPSF-", 5) == 0)
				epsf++;
		}
		if (state == 0 && (*buf == '\n' || has("%%EndComments") ||
				buf[0] != '%' || buf[1] == ' ' ||
				buf[1] == '\t' || buf[1] == '\r' ||
				buf[1] == '\n')) {
			state = 1;
			continue;
		}
		if (buf[0] != '%' || buf[1] != '%')
			continue;
		if (state != 1 && (bp = has("%%+")) != NULL) {
			switch (cont) {
			case DOCUMENTFONTS:
				addfonts(bp);
				break;
			case DOCUMENTNEEDEDRESOURCES:
				goto needres;
			case NORMAL: /* TODO: case is not in original code */
				break;
			}
			continue;
		} else
			cont = NORMAL;
		if (has("%%Page: ")) {
			if (!foundpage)
				page.start = ftell(fin);
			sscanf(buf, "%*s %*s %d", &i);
			if (i == page_no)
				foundpage = 1;
			else if (foundpage && page.end <= page.start)
				page.end = ftell(fin);
		} else if (has("%%EndPage: ")) {
			sscanf(buf, "%*s %*s %d", &i);
			if (i == page_no) {
				foundpage = 1;
				page.end = ftell(fin);
			}
			if (!foundpage)
				page.start = ftell(fin);
		} else if (state != 1 && !indoc &&
				has("%%BoundingBox:") && !hires) {
			sscanf(buf, "%%%%BoundingBox: %lf %lf %lf %lf", &llx, &lly, &urx, &ury);
			if (epsf)
				epsf++;
		} else if (state != 1 && !indoc && has("%%HiResBoundingBox:")) {
			sscanf(buf, "%%%%HiResBoundingBox: %lf %lf %lf %lf", &llx, &lly, &urx, &ury);
			hires++;
			if (epsf)
				epsf++;
		} else if (has("%%LanguageLevel:")) {
			int	n;
			sscanf(buf, "%%%%LanguageLevel: %d", &n);
			LanguageLevel = max(LanguageLevel, n);
		} else if ((bp = has("%%DocumentNeededFonts:")) != NULL ||
				(bp = has("%%DocumentFonts:")) != NULL) {
			cont = DOCUMENTFONTS;
			addfonts(bp);
		} else if ((bp = has("%%DocumentNeededResources:")) != NULL) {
		needres:
			if ((cp = _has(bp, "font")))
				addfonts(cp);
			else {
				for (cp = bp; *cp && *cp != '\n' &&
						*cp != '\r'; cp++);
				*cp = '\0';
				needresource("%s", bp);
			}
			cont = DOCUMENTNEEDEDRESOURCES;
		} else if (indoc == 0 && (has("%%EndProlog") ||
				has("%%EndSetup") || has("%%EndDocumentSetup")))
			prolog.end = page.start = ftell(fin);
		else if (indoc == 0 && has("%%EOF"))
			break;
		else if (state == 1 && indoc == 0 && has("%%Trailer")) {
			trailer.start = ftell(fin);
			state = 2;
		} else if (state == 1 && has("%%BeginDocument:"))
			indoc++;
		else if (state == 1 && indoc > 0 && has("%%EndDocument"))
			indoc--;
		else if (state == 1 && (cp = has("%%BeginBinary:")) != NULL) {
			if ((i = strtol(cp, &cp, 10)) > 0)
				psskip(i, fin);
		} else if (state == 1 && (cp = has("%%BeginData:")) != NULL) {
			if ((i = strtol(cp, &cp, 10)) > 0) {
				while (*cp == ' ' || *cp == '\t')
					cp++;
				while (*cp && *cp != ' ' && *cp != '\t')
					cp++;
				while (*cp == ' ' || *cp == '\t')
					cp++;
				if (strncmp(cp, "Bytes", 5) == 0)
					psskip(i, fin);
				else if (strncmp(cp, "Lines", 5) == 0) {
					while (i-- && psgetline(&buf,
						&bufsize, NULL, fin) != NULL);
				}
			}
		} else if (has("%%BeginGlobal")) {
			if (page.end <= page.start) {
				if (nglobal >= maxglobal) {
					maxglobal += 20;
					global = grab(maxglobal);
				}
				global[nglobal].start = ftell(fin);
			}
		} else if (has("%%EndGlobal"))
			if (page.end <= page.start)
				global[nglobal++].end = ftell(fin);
	}

	fseek(fin, 0L, SEEK_END);
	if (trailer.start == 0)
		trailer.start = ftell(fin);
	trailer.end = ftell(fin);

	if (page.end <= page.start)
		page.end = trailer.start;

/*
fprintf(stderr, "prolog=(%d,%d)\n", prolog.start, prolog.end);
fprintf(stderr, "page=(%d,%d)\n", page.start, page.end);
for(i = 0; i < nglobal; i++)
	fprintf(stderr, "global[%d]=(%d,%d)\n", i, global[i].start, global[i].end);
fprintf(stderr, "trailer=(%d,%d)\n", trailer.start, trailer.end);
*/

	/* all output here */
	fprintf(fout, "_ps_include_head\n");
	var(llx); var(lly); var(urx); var(ury); var(w); var(o); var(s);
	var(cx); var(cy); var(sx); var(sy); var(ax); var(ay); var(rot);
	fprintf(fout, "_ps_include_setup\n");
	if (epsf >= 2) {
		size_t	len;
		rewind(fin);
		fprintf(fout, "%%%%BeginDocument: %s\n", name);
		while (psgetline(&buf, &bufsize, &len, fin) != NULL) {
			if (has("%%BeginPreview:")) {
				while (psgetline(&buf, &bufsize, &len, fin)
						!= NULL &&
						!has("%%EndPreview"));
				continue;
			}
			fwrite(buf, 1, len, fout);
		}
		fprintf(fout, "%%%%EndDocument\n");
	} else {
		copy(fin, fout, &prolog);
		for(i = 0; i < nglobal; i++)
			copy(fin, fout, &global[i]);
		copy(fin, fout, &page);
		copy(fin, fout, &trailer);
	}
	fprintf(fout, "_ps_include_tail\n");

	if(nglobal)
		free(global);

}

static void
copy(FILE *fin, FILE *fout, Section *s)
{
	size_t	len;

	if (s->end <= s->start)
		return;
	fseek(fin, s->start, SEEK_SET);
	while (ftell(fin) < s->end &&
			psgetline(&buf, &bufsize, &len, fin) != NULL) {
		if (buf[0] == '%')
			putc(' ', fout);
		fwrite(buf, 1, len, fout);
	}
}

static char *
_has(char *buffer, const char *word)
{
	int	n;

	n = strlen(word);
	if (strncmp(buffer, word, n) != 0)
		return NULL;
	if (buffer[n] == ' ' || buffer[n] == '\t' || buffer[n] == '\r' ||
			buffer[n] == '\n' || buffer[n] == 0) {
		while (buffer[n] == ' ' || buffer[n] == '\t')
			n++;
		return &buffer[n];
	}
	return NULL;
}

static void
addfonts(char *line)
{
	char	*lp = line, c;

	do {
		while (*lp == ' ' || *lp == '\t')
			lp++;
		line = lp;
		while (*lp && *lp != ' ' && *lp != '\t' && *lp != '\n' &&
				*lp != '\r')
			lp++;
		c = *lp;
		*lp = '\0';
		if (*line && strcmp(line, "(atend)"))
			documentfont(line);
		*lp = c;
	} while (c && c != '\n' && c != '\r');
}