aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/troff/t11.c
blob: f930d8b1b7b7c08923c1766b5cf2c441bbf57afd (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
#include "tdef.h"
#include "fns.h"
#include "ext.h"

#define	MAXCH NCHARS		/* maximum number of global char names */
char	*chnames[MAXCH];	/* chnames[n-ALPHABET] -> name of char n */
int	nchnames;		/* number of Cxy names currently seen */

#define	MAXPS	100		/* max number of point sizes */
int	pstab[MAXPS];		/* point sizes */
int	nsizes;			/* number in DESC */

Font	fonts[MAXFONTS+1];	/* font info + ptr to width info */


#define	skipline(f)	while (getc(f) != '\n')

#define	eq(s1, s2)	(strcmp(s1, s2) == 0)

int
getdesc(char *name)
{
	FILE *fin;
	char cmd[100], s[100];
	int i, v;

	if ((fin = fopen(unsharp(name), "r")) == NULL)
		return -1;
	while (fscanf(fin, "%s", cmd) != EOF) {
		if (strcmp(cmd, "res") == 0) {
			fscanf(fin, "%d", &Inch);
		} else if (strcmp(cmd, "hor") == 0) {
			fscanf(fin, "%d", &Hor);
		} else if (strcmp(cmd, "vert") == 0) {
			fscanf(fin, "%d", &Vert);
		} else if (strcmp(cmd, "unitwidth") == 0) {
			fscanf(fin, "%d", &Unitwidth);
		} else if (strcmp(cmd, "sizes") == 0) {
			nsizes = 0;
			while (fscanf(fin, "%d", &v) != EOF && v != 0 && nsizes < MAXPS)
				pstab[nsizes++] = v;
		} else if (strcmp(cmd, "fonts") == 0) {
			fscanf(fin, "%d", &nfonts);
			for (i = 1; i <= nfonts; i++) {
				fscanf(fin, "%s", s);
				fontlab[i] = PAIR(s[0], s[1]);
			}
		} else if (strcmp(cmd, "charset") == 0) {	/* add any names */
			while (fscanf(fin, "%s", s) != EOF)
				chadd(s, Troffchar, Install);
			break;
		}
		/* else 
			just skip anything else */
		skipline(fin);
	}
	fclose(fin);
	return 1;
}

static int checkfont(char *name)
{		/* in case it's not really a font description file */
		/* really paranoid, but consider \f. */
	FILE *fp;
	char buf[300], buf2[300];
	int i, status = -1;

	if ((fp = fopen(unsharp(name), "r")) == NULL)
		return -1;
	for (i = 1; i <= 10; i++) {
		if (fgets(buf, sizeof buf, fp) == NULL)
			break;
		sscanf(buf, "%s", buf2);
		if (buf2[0] == '#') {
			i--;
			continue;
		}
		if (eq(buf2, "name") || eq(buf2, "fontname") ||
		    eq(buf2, "special") || eq(buf2, "charset")) {
			status = 1;
			break;
		}
	}
	fclose(fp);
	return status;
	
}

int
getfont(char *name, int pos)	/* create width tab for font */
{
	FILE *fin;
	Font *ftemp = &fonts[pos];
	Chwid chtemp[MAXCH];
	static Chwid chinit;
	int i, nw, n, wid, kern, code, type;
	char buf[100], ch[100], s1[100], s2[100], s3[100], cmd[300];

	nw = code = 0;
	/* fprintf(stderr, "read font %s onto %d\n", name, pos); */
	if (checkfont(name) == -1)
		return -1;
	if ((fin = fopen(unsharp(name), "r")) == NULL)
		return -1;
	for (i = 0; i < ALPHABET; i++)
		chtemp[i] = chinit;	/* zero out to begin with */
	ftemp->specfont = ftemp->ligfont = 0;
	ftemp->defaultwidth = ftemp->spacewidth = Inch * Unitwidth / 72 / 3; /* should be rounded */
	while (fscanf(fin, "%s", cmd) != EOF) {
		if (strcmp(cmd, "name") == 0)
			fscanf(fin, "%s", ftemp->longname);
		else if (strcmp(cmd, "special") == 0)
			ftemp->specfont = 1;
		else if (strcmp(cmd, "ligatures") == 0) {
			ftemp->ligfont = getlig(fin);
		} else if (strcmp(cmd, "spacewidth") == 0) {
			fscanf(fin, "%d", &ftemp->spacewidth);
		} else if (strcmp(cmd, "defaultwidth") == 0) {
			fscanf(fin, "%d", &ftemp->defaultwidth);
		} else if (strcmp(cmd, "charset") == 0) {
			wchar_t wc;
			skipline(fin);
			nw = ALPHABET;
			while (fgets(buf, sizeof buf, fin) != NULL) {
				sscanf(buf, "%s %s %s %s", ch, s1, s2, s3);
				if (s1[0] != '"') {	/* genuine new character */
					sscanf(s1, "%d", &wid);
					sscanf(s2, "%d", &kern);
					code = strtol(s3, 0, 0);	/* dec/oct/hex */
				}
				/* otherwise it's a synonym for prev character, */
				/* so leave previous values intact */


				/* decide what kind of alphabet it might come from here */


				if (strlen(ch) == 1) {	/* it's ascii */
					n = ch[0];	/* origin includes non-graphics */
					chtemp[n].num = ch[0];
				} else if (ch[0] == '\\' && ch[1] == '0') {
					n = strtol(ch+1, 0, 0);	/* \0octal or \0xhex */
					chtemp[n].num = n;
#ifdef UNICODE
				} else if (mbtowc(&wc, ch, strlen(ch)) > 1) {
					chtemp[nw].num = chadd(ch,  MBchar, Install);
					n = nw;
					nw++;
#endif	/*UNICODE*/
				} else {
					if (strcmp(ch, "---") == 0) { /* no name */
						sprintf(ch, "%d", code);
						type = Number;
					} else
						type = Troffchar;
					chtemp[nw].num = chadd(ch, type, Install);
					n = nw;
					nw++;
				}
				chtemp[n].wid = wid;
				chtemp[n].kern = kern;
				chtemp[n].code = code;
				/*fprintf(stderr, "font %2.2s char %4.4s num %3d wid %2d code %3d\n",
					ftemp->longname, ch, n, wid, code);
				*/
			}
			break;
		}
		skipline(fin);
	}
	fclose(fin);
	chtemp[' '].wid = ftemp->spacewidth;	/* width of space on this font */
	ftemp->nchars = nw;
	if (ftemp->wp)
		free(ftemp->wp);	/* god help us if this wasn't allocated */
	ftemp->wp = (Chwid *) malloc(nw * sizeof(Chwid));
	if (ftemp->wp == NULL)
		return -1;
	for (i = 0; i < nw; i++)
		ftemp->wp[i] = chtemp[i];
/*
 *	printf("%d chars: ", nw);
 *	for (i = 0; i < nw; i++)
 *		if (ftemp->wp[i].num > 0 && ftemp->wp[i].num < ALPHABET) {
 *			printf("%c %d ", ftemp->wp[i].num, ftemp->wp[i].wid);
 *		else if (i >= ALPHABET)
 *			printf("%d (%s) %d ", ftemp->wp[i].num,
 *				chnames[ftemp->wp[i].num-ALPHABET], ftemp->wp[i].wid);
 *	}
 *	printf("\n");
 */
	return 1;
}

int
chadd(char *s, int type, int install)	/* add s to global character name table; */
{					/* or just look it up */

	/* a temporary kludge:  store the "type" as the first character */
	/* of the string, so we can remember from whence it came */

	char *p;
	int i;

/* fprintf(stderr, "into chadd %s %c %c\n", s, type, install); /* */
	for (i = 0; i < nchnames; i++)
		if (type == chnames[i][0] && eq(s, chnames[i]+1)) /* +1 since type at front */
			break;
/* fprintf(stderr, "i %d, nchnames %d\n", i, nchnames); /* */
	if (i < nchnames)		/* found same type and bytes at position i */
		return ALPHABET + i;
	else if (install == Lookup)	/* not found, and we were just looking */
		return -1;

	chnames[nchnames] = p = (char *) malloc(strlen(s)+1+1);	/* type + \0 */
	if (p == NULL) {
		ERROR "out of space adding character %s", s WARN;
		return LEFTHAND;
	}
	if (nchnames >= NCHARS - ALPHABET) {
		ERROR "out of table space adding character %s", s WARN;
		return LEFTHAND;
	}
	strcpy(chnames[nchnames]+1, s);
	chnames[nchnames][0] = type;
/* fprintf(stderr, "installed %c%s at %d\n", type, s, nchnames); /* */
	return nchnames++ + ALPHABET;
}

char *chname(int n)	/* return string for char with index n */
{			/* includes type char at front, to be peeled off elsewhere */
	if (n >= ALPHABET && n < nchnames + ALPHABET)
		return chnames[n-ALPHABET];
	else
		return "";
}

int
getlig(FILE *fin)	/* pick up ligature list */
{
	int lig;
	char temp[200];

	lig = 0;
	while (fscanf(fin, "%s", temp) != EOF && strcmp(temp, "0") != 0) {
		if (strcmp(temp, "fi") == 0)
			lig |= LFI;
		else if (strcmp(temp, "fl") == 0)
			lig |= LFL;
		else if (strcmp(temp, "ff") == 0)
			lig |= LFF;
		else if (strcmp(temp, "ffi") == 0)
			lig |= LFFI;
		else if (strcmp(temp, "ffl") == 0)
			lig |= LFFL;
		else
			fprintf(stderr, "illegal ligature %s ignored\n", temp);
	}
	return lig;
}