summaryrefslogtreecommitdiff
path: root/dviware/dvitops/psfont.c
blob: 3589fb3f424f648023cbf11857199b9ef4d4b641 (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
static char rcsid[] = "$Header: /usr/jjc/dvitops/RCS/psfont.c,v 1.2 89/02/20 14:24:06 jjc Rel $";

#include "util.h"

#define LINE_MAX 512
char *program_name = "psfont";
static char *ws = " \n\r\t";	/* white space */
static char *psfonts;

#ifdef PROTO
int strprefix(char *s1, char *s2);
void fcopy(FILE *in, FILE *out);
void process_fonts(void);
#endif

/* is s1 a prefix of s2? */
int strprefix(s1, s2)
char *s1, *s2;
{
	while (*s1++ == *s2++)
		;
	return s1[-1] == '\0';
}

/* copy infp to outfp */
void fcopy(infp, outfp)
FILE *infp, *outfp;
{
	int c;
	while ((c = getc(infp)) != EOF)
		putc(c, outfp);
}
	
struct string_list {
	struct string_list *next;
	char *s;
	char *t;
};
struct string_list *document_fonts = NULL;

/* map PostScript names to file names */

void process_fonts()
{
	char buf[LINE_MAX];
	FILE *fp;
	fp = xfopen("psfonts.map", FALSE, psfonts, NULL);
	if (fp == 0)
		return;
	while (fgets(buf, LINE_MAX, fp) != 0) {
		char *s, *t;
		s = strchr(buf, '%');
		if (s != NULL)
			*s = '\0';
		s = strtok(buf, ws);
		t = strtok(NULL, ws);
		if (s != 0 && t != 0) {
			struct string_list *q;
			for (q = document_fonts; q != NULL; q = q->next)
				if (strcmp(q->s, s) == 0) {
					q->t = malloc(strlen(t) + 1);
					if (q->t == NULL)
						out_of_memory();
					strcpy(q->t, t);
				}
		}
	}
	fclose(fp);
}


int main(argc, argv)
int argc;
char **argv;
{
	int flag = FALSE;
	int cont = FALSE;
	char *ptr;
	char buf[LINE_MAX];
	psfonts = getenv("PSFONTS");
	if (psfonts == NULL)
		psfonts = PSFONTS;
	if (fgets(buf, LINE_MAX, stdin) == 0)
		exit(0);
	fputs(buf, stdout);
	if (strprefix("%!PS-Adobe-", buf))
		while (fgets(buf, LINE_MAX, stdin) != 0) {
			if (!strprefix("%%", buf)) {
				flag = TRUE;
				fputs("%%EndComments\n", stdout);
				break;
			}
			fputs(buf, stdout);
			if (strprefix("%%EndComments", buf))
				break;
			if (document_fonts == NULL && strprefix("%%DocumentFonts:", buf))
				ptr = strchr(buf, ':') + 1;
			else if (cont && strprefix("%%+", buf))
				ptr = buf + 3;
			else
				ptr = NULL;
			if (ptr != NULL) {
				for (ptr = strtok(ptr, ws);
					 ptr != 0;
					 ptr = strtok(NULL, ws)) {
					struct string_list *p;
					p = (struct string_list *)
						malloc(sizeof(struct string_list));
					if (p == 0)
						out_of_memory();
					p->s = malloc(strlen(ptr) + 1);
					p->t = NULL;
					if (p->s == 0)
						out_of_memory();
					strcpy(p->s, ptr);
					p->next = document_fonts;
					document_fonts = p;
				}
				cont = TRUE;
			}
			else
				cont = FALSE;
		}
	process_fonts();
	while (document_fonts != 0) {
		FILE *fp;
		char *s;
		if (document_fonts->t != NULL)
			s = document_fonts->t;
		else
			s = document_fonts->s;
		fp = xfopen(s, FALSE, psfonts, NULL);
		if (fp != 0) {
			printf("%%%%BeginFont: %s\n", document_fonts->s);
			fcopy(fp, stdout);
			fclose(fp);
			printf("%%%%EndFont\n");
		}
		document_fonts = document_fonts->next;
	}
	if (flag)
		fputs(buf, stdout);
	fcopy(stdin, stdout);
	exit(history);
}