summaryrefslogtreecommitdiff
path: root/Build/source/texk/seetexk/font.c
blob: dbaee944e4a1834a79b9972f9482c66aed771911 (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
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
/*
 * Copyright (c) 1987, 1989 University of Maryland
 * Department of Computer Science.  All rights reserved.
 * Permission to copy for any purpose is hereby granted
 * so long as this copyright notice remains intact.
 */

#ifndef lint
static char rcsid[] = "$Header: /homes/grunwald/Src/SeeTeX/libtex/RCS/font.c,v 1.2 1992/07/22 16:26:02 grunwald Exp grunwald $";
#endif

/*
 * Routines for working with fonts.  In particular, the configuration
 * dependent code is here.
 *
 * Specific fonts (GF, PXL, etc.) have functions in separate files.
 */

#include <stdio.h>
#include <errno.h>
#include <ctype.h>
#include "types.h"
#include "conv.h"
#include "error.h"
#include "font.h"

/*
 * Define the default configuration file.
 * Also define the maximum path name length.
 */
#ifndef FONTDESC
/* #define FONTDESC "/usr/local/lib/tex/fontdesc" */
	you must define FONTDESC in the Makefile
#endif

#define	PATHLEN	1024

/*
 * A font configuration.  The font list is ordered.
 *
 * A specifier is typically a particular print engine, since
 * different engines need slightly different fonts.
 */
struct fontconf {
	struct	fontconf *fc_next;
	struct	fontops *fc_ops;
	char	*fc_path;	/* path, with metacharacters */
	char	*fc_spec;	/* specifier */
	int	fc_slop;	/* slop value */
};

/*
 * EQ is a fast way to check for string equivalence.
 */
#define	EQ(a, b) (*(a) == *(b) && strcmp(a, b) == 0)

/*
 * Private variables and functions.
 */
static	int didinit;		/* true => initialised already */
static	char *cfname;		/* config file name, for errors */
static	int cfline;		/* config file line, likewise */
static	struct fontops *fontops;/* font operations code: list head */
static	struct fontconf *fonts;	/* font list */
static	struct fontconf **nextfc;/* used during initialisation */
static	char spec_any[] = "*";	/* the `anything' specifier */

static void readconf(), badcf(), setfenv(), setfont();
static struct font *getafont();

/*
 * Imports.
 */
extern	int errno;
char	*getenv(), *malloc(), *strncpy(), *strsave();

/*
 * You may get warnings from lint about sprintf(), which is of type
 * `char *' in early 4BSD releases.  Ignore the warning.
 */

/*
 * Here, alas, we know about all the kinds of fonts.
 * This also means that every DVI interpreter pulls in
 * the full set of font manipulation routines.
 *
 * PERHAPS THIS SHOULD BE CONFIGURABLE.
 */
#define	ADDFONT(x) { \
	extern struct fontops x; \
	x.fo_next = fontops; \
	fontops = &x; \
}

void
fontinit(file)
	char *file;
{

	if (didinit) {
		/*
		 * Could free the old configuration and fire up
		 * a new one, but for now . . .
		 */
		error(1, 0, "attempt to reinit fonts");
		/* NOTREACHED */
	}
	didinit++;
	ADDFONT(invisops);
	ADDFONT(tfmops);
	ADDFONT(blankops);
	ADDFONT(boxops);
	ADDFONT(pxlops);
	ADDFONT(gfops);
	ADDFONT(pkops);
	nextfc = &fonts;
	if (file == NULL)
		if ((file = getenv(CONFENV)) == NULL)
			file = FONTDESC;
	readconf(file);
}

/*
 * Find a font's operations, given its name.
 */
static struct fontops *
findops(name)
	register char *name;
{
	register struct fontops *fo;

	for (fo = fontops; fo != NULL; fo = fo->fo_next)
		if (EQ(fo->fo_name, name))
			return (fo);
	return (NULL);
}

/*
 * Read the named configuration file.  The file is split into
 * lines, and lines are split into words; if the first word is
 * "font", this is a fontconf, and we read the remainder of the
 * words and make a fontconf entry.
 */
static void
readconf(name)
	char *name;
{
	register FILE *f;	/* config file */
	register int c;		/* char and word counter */
	int isenv;		/* true => doing `fontenv', not `font' */
	char *env;		/* value from getenv() */
	struct fontconf proto;	/* prototype fontconf */
	char *v[20];		/* word vector */
	char line[BUFSIZ];	/* input line */

	if ((f = fopen(name, "r")) == NULL)
		error(1, -1, "cannot read font configuration file \"%s\"",
		    name);
	cfname = name;
	cfline = 0;
	while (fgets(line, sizeof (line), f) != NULL) {
		cfline++;
		if ((c = strlen(line)) > 0) {
			if (line[--c] != '\n')
				badcf("line too long");
			line[c] = 0;
		}
		if ((c = split(line, v, sizeof v / sizeof *v)) < 0)
			badcf("too many words");
		/* skip things that are not fonts */
		if (c == 0)
			continue;
		if (EQ(v[0], "font"))
			isenv = 0;
		else if (EQ(v[0], "fontenv"))
			isenv = 1;
		else
			continue;
		switch (c) {
		case 1:
			badcf("missing font typename");
			/* NOTREACHED */
		case 2:
			badcf("missing font spec (engine)");
			/* NOTREACHED */
		case 3:
			badcf("missing slop value");
			/* NOTREACHED */
		case 4:
			badcf(isenv ? "need environment variable" :
			    "need pathname");
			/* NOTREACHED */
		case 5:
			if (isenv)
				badcf("need suffix");
			break;
		case 6:
			if (isenv)
				break;
		default:
			error(0, 0,
			    "%s, line %d: warning: %d extra word(s) ignored",
			    cfname, cfline, c - 5);
			break;
		}
		if ((proto.fc_ops = findops(v[1])) == NULL) {
			error(0, 0,
			    "\"%s\", line %d: unknown font type \"%s\" ignored",
			    cfname, cfline, v[1]);
			continue;
		}

		/*
		 * For fontenv, if the environment variable is not actually
		 * set in the environment, just skip over this line.
		 */
		
		/* Debugging (LOG) */
		printf(" texsunLOG: ");
		printf(v[0]);
		printf(" .");
		printf(v[1]);
		printf(" ");
		if (isenv)
			printf("'");
		printf(v[4]);
		if (isenv)
			printf("'");
		printf(" does:\n");

		if (isenv && (env = getenv(v[4])) == NULL)
			continue;
		proto.fc_next = NULL;
		proto.fc_spec = EQ(v[2], spec_any) ? NULL : strsave(v[2]);
		proto.fc_slop = atoi(v[3]);
		if (proto.fc_slop < 0)	/* quietly enforce proper slops */
			proto.fc_slop = 0;
		if (isenv) {
			printf("  = ");
			printf(env);
			printf("\n");
			setfenv(&proto, env, v[5]);
			}
		else
			setfont(&proto, strsave(v[4]));
	}
}

/*
 * Handle fontenv lines by appending as many configuration entries
 * as there are components in the given environment variable.
 */
static void
setfenv(pfc, env, suf)
	struct fontconf *pfc;
	char *env, *suf;
{
	register char *s, *t;
	register int len, suflen = strlen(suf);
	register char *mem, *slash;
	char *munch = strsave(env);	/* a copy we can write on */
	struct fontops *fo;

	/*
	 * Loop over the path.  Turn `foo' into `foo/<suf1><suf2>';
	 * turn an empty string into just <suf1><suf2>.  Put these
	 * names in fresh memory and set them up as places to search.
	 */

	for (s = munch; (t = s) != NULL; s = t) {
		for (;;) {
			if (*t == ':') {
				*t++ = 0;
				len = t - s;	/* includes trailing NUL */
				break;
			}
			if (*t++ == 0) {
				len = t - s;	/* includes trailing NUL */
				t = NULL;
				break;
			}
		}
		if (len != 0) {
			slash = "/";
			len++;
		} else
			slash = "";
		if ((mem = malloc(len + suflen)) == NULL)
			error(1, -1, "out of memory while saving $%s", env);
		(void) sprintf(mem, "%s%s%s", s, slash, suf);
		setfont(pfc, mem);
	}
	free(munch);
}

/*
 * Turn a prototype fontconf into a real one.
 */
static void
setfont(pfc, path)
	struct fontconf *pfc;
	char *path;
{
	register struct fontconf *fc;

	if ((fc = (struct fontconf *)malloc(sizeof(*fc))) == NULL)
		error(1, -1, "out of memory for font configuration (sorry)");
	*fc = *pfc;
	fc->fc_path = path;
	*nextfc = fc;
	nextfc = &fc->fc_next;

	printf(" texsunLOG: scanning ");
	printf(path);
	printf("\n");
		/* message for found path (logging) */
}

/*
 * Complain about a problem in the configuration file.
 */
static void
badcf(why)
	char *why;
{

	error(1, 0, "\"%s\", line %d: %s", cfname, cfline, why);
	/* NOTREACHED */
}

/*
 * Copy string s into buffer buf, dropping off any numerical suffix.
 * E.g., cmr7 and cmr10 become cmr.  (This is the `%b' operator.)
 */
static void
basify(s, buf, size)
	char *s, *buf;
	int size;
{
	register char *p, *endp;
	register int n;

	for (p = s; *p; p++)
		if (!isdigit(*p))
			endp = p;
	if ((n = endp + 1 - s) >= size)
		error(1, 0, "font name `%s' too long for %%b (sorry)", s);
	(void) strncpy(buf, s, n);
	buf[n] = 0;
}

/*
 * Turn a prototype path, name, and magnification into a full
 * path (expand %f, %m, %b).
 */
static void
pave(result, proto, name, mag)
	char *result, *proto, *name;
	int mag;
{
	register int c;
	register char *s, *d, *p;
	char num[30], fontbase[256];

	d = result;
	p = proto;
	s = NULL;
	num[0] = 0;		/* will need changing for other bases */
	fontbase[0] = 0;

	while (p != NULL) {
		/*
		 * If sourcing from s, take its next character, and
		 * insert it directly.  Otherwise take the next path
		 * character and interpret it.
		 */
		if (s != NULL) {
			if ((c = *s++) == 0) {
				s = NULL;
				continue;
			}
			goto put;
		}
		if ((c = *p++) == 0)
			p = NULL;
		if (c != '%')
			goto put;

		switch (c = *p++) {

		case 'b':
			if (fontbase[0] == 0)
				basify(name, fontbase, sizeof(fontbase));
			s = fontbase;
			continue;

		case 'f':
		case 'n':
		case 's':
			s = name;
			continue;

		case 'd':
		case 'm':
			if (num[0] == 0)
				(void) sprintf(num, "%d", mag);
			s = num;
			continue;

		case 0:
			c = '%';
			p--;
			/* FALLTHROUGH */
		}
put:
		if (d - result >= PATHLEN)
			error(1, 0, "font path `%s' too long (sorry)", proto);
		*d++ = c;
	}
}

/*
 * Given a font name and size, return the first font that fits, along
 * with its name (via fname).  If we cannot find such a font, we set
 * *fname to point to a `canonical' example font name, unless there are
 * are no fonts for the device, in which case we set *fname to NULL.
 */
struct font *
GetFont(nm, dvimag, dvidsz, dev, fname)
	char *nm;
	i32 dvimag, dvidsz;
	char *dev, **fname;
{

	return (getafont(nm, dvimag, dvidsz, dev, fname, 1));
}

/*
 * Same as GetFont, but caller promises never to ask for rasters.
 */
struct font *
GetRasterlessFont(nm, dvimag, dvidsz, dev, fname)
	char *nm;
	i32 dvimag, dvidsz;
	char *dev, **fname;
{

	return (getafont(nm, dvimag, dvidsz, dev, fname, 0));
}

/*
 * NEED TO THINK ABOUT gf NAMING CONVENTIONS HERE: ARE THEY LIKE pxl?
 * WHAT ABOUT OTHERS?
 */
static struct font *
getafont(nm, dvimag, dvidsz, dev, fname, wantrast)
	char *nm;
	i32 dvimag, dvidsz;
	char *dev, **fname;
	int wantrast;
{
	register int slop, fmag;
	register struct font *f;
	register struct fontconf *fc;
	register struct fontops *fo;
	register char *path;
	int fd, scaled;
	double mag;
	static char firstpath[PATHLEN], laterpath[PATHLEN];

	if (!didinit)
		fontinit((char *)NULL);

	/*
	 * The equation below means, approximately, `the font is
	 * magnified by the ratio of the actual size dvimag to the
	 * design size dvidsz, and then further scaled by the
	 * global magnification.'  We multiply this by the printer's
	 * resolution in dots per inch, then use the per-font
	 * conversion factor to convert a dots-per-inch value to
	 * a font name `%m' magnification (extension).
	 */
	mag = (double) dvimag / (double) dvidsz;
	scaled = mag * 1000.0 + 0.5;
	mag *= Conversion.c_mag * Conversion.c_dpi;

	path = firstpath;	/* no canonical font name yet */
	for (fc = fonts; fc != NULL; fc = fc->fc_next) {
		if (dev != NULL && fc->fc_spec != NULL &&
		    !EQ(dev, fc->fc_spec))
			continue;
		/*
		 * Fake fonts are ignored unless there is already
		 * `canonical' real font.  The first non-fake font
		 * is the `canonical' one.
		 */
		fo = fc->fc_ops;
		if (path == firstpath && fo->fo_fakefont)
			continue;
		fmag = mag * fo->fo_dpitomag + 0.5;
		for (slop = 0; slop <= fc->fc_slop; slop++) {
			pave(path, fc->fc_path, nm, fmag + slop);
			if ((fd = open(path, 0)) >= 0)
				goto found;
			path = laterpath;	/* tried at least one now */
			if (slop) {
				pave(path, fc->fc_path, nm, fmag - slop);
				if ((fd = open(path, 0)) >= 0)
					goto found;
			}
		}
	}

	/* not found */
	if (path == firstpath) {
		*fname = NULL;
		errno = ENXIO;
	} else {
		*fname = firstpath;
		errno = ENOENT;
	}
	return (NULL);

found:
	*fname = path;

	/* allocate space for the per-font info, and read it in */
	f = (struct font *)malloc(sizeof *f);
	if (f == NULL) {
		errno = ENOMEM;
		return (NULL);
	}
	f->f_flags = wantrast ? FF_RASTERS : 0;
	f->f_ops = fc->fc_ops;
	f->f_path = strsave(path);
	f->f_font = strsave(nm);
	f->f_dvimag = dvimag;
	f->f_dvidsz = dvidsz;
	f->f_scaled = scaled;
	f->f_pspace = f->f_dvimag / 6;	/* a three-unit "thin space" */
	f->f_nspace = -4 * f->f_pspace;
	f->f_vspace = 5 * f->f_pspace;
	errno = 0;
	if ((*f->f_ops->fo_read)(f, fd)) {
		int e = errno;	/* paranoid */

		free(f->f_path);
		free(f->f_font);
		free((char *)f);
		errno = e;
		return (NULL);
	}
	if (f->f_ops->fo_fakefont) {
		error(0, 0, "Warning: no font for %s", Font_TeXName(f));
		if (firstpath[0])
			error(0, 0, "(wanted, e.g., \"%s\")", firstpath);
		error(0, 0, "I will substitute %s for the missing glyphs",
		    f->f_ops->fo_fakefont == 1 ? "boxes" : "white space");
	}
	return (f);
}