summaryrefslogtreecommitdiff
path: root/dviware/dvipage/findfile.c
blob: 21d85cd4e15efca8404934145c3d89292faa85c4 (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
/*
 * dvipage: DVI Previewer Program for Suns
 *
 * Neil Hunt (hunt@spar.slb.com)
 *
 * Copyright (c) 1988 Schlumberger Technologies, Inc 1988.
 * Anyone can use this software in any manner they choose,
 * including modification and redistribution, provided they make
 * no charge for it, and these conditions remain unchanged.
 *
 * This program is distributed as is, with all faults (if any), and
 * without any warranty. No author or distributor accepts responsibility
 * to anyone for the consequences of using it, or for whether it serves any
 * particular purpose at all, or any other reason.
 *
 * $Log:	findfile.c,v $
 * Revision 1.1  88/11/28  18:40:44  hunt
 * Initial revision
 * 
 * Based upon `mitdrivers/findfile.c'
 * Copyright 1985 Massachusetts Institute of Technology
 */

#include <stdio.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <sys/file.h>
#include <sys/param.h>		/* For MAXPATHLEN */
#include <suntool/sunview.h>
#include "dvipage.h"

forward bool	find_file_in_path();
forward bool	find_best_file_in_path();
forward bool	scandir();
forward bool	scanpdir();
forward int	strdiff();


/*
 * find_file:
 *	Seaches for a font file in various places.
 *	dirpath is a colon separated list of possible pathnames.
 *	Returns TRUE if a usable file was found.
 */

bool find_font_file(dir_path, fontptr)
char *dir_path;
struct font_entry *fontptr;
{
	int min_df, min_dp, min_dm;

	if(verbose & DEBUG_FONTS)
		fprintf(stderr, "find_font_file(%s .%dgf or .%dpxl)\n",
		  fontptr->n, fontptr->font_gf_mag, fontptr->font_pxl_mag);

	/*
	 * Search for exact match.
	 */
	if(find_file_in_path(dir_path, fontptr))
		return TRUE;

	/*
	 * Search for nearest match.
	 */
	min_df = min_dp = min_dm = MAXINT;
	if(find_best_file_in_path(dir_path, fontptr,
	  &min_df, &min_dp, &min_dm))
	{
		message("Substituted font %s for %s.%d.",
		  fontptr->name, fontptr->n, fontptr->type == TYPE_PXL ?
		  fontptr->font_pxl_mag : fontptr->font_gf_mag);
		return TRUE;
	}

	return FALSE;
}

/*
 * find_file_in_path:
 *	Searches path for a font file.
 *	Returns TRUE if a suitable match has been found.
 */

bool
find_file_in_path(dirpath, fontptr)
char *dirpath;
struct font_entry *fontptr;
{
	char *p;
	char dir[MAXPATHLEN];

	while(*dirpath)
	{
		/*
		 * Copy first/next path prefix to dir[].
		 * Skip over the ':'.
		 */
		for(p = dir; *dirpath; )
		{
			if(*dirpath == ':' || *dirpath == ';')
			{
				dirpath++;
				break;
			}
			else
				*p++ = *dirpath++;
		}
		*p = '\0';

		if(use_pk)
		{
			/*
			 * Try flat structure.
			 */
			sprintf(fontptr->name, "%s/%s.%dpk",
			  dir, fontptr->n, fontptr->font_gf_mag);
			if(access(fontptr->name, R_OK) == 0)
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; OK\n",
					  fontptr->name);
				fontptr->type = TYPE_PK;
				return TRUE;
			}
			else
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; No\n",
					  fontptr->name);
			}

			/*
			 * Try hierarchical structure.
			 */
			sprintf(fontptr->name, "%s/%s/%s.%dpk",
			  dir, fontptr->n, fontptr->n, fontptr->font_gf_mag);
			if(access(fontptr->name, R_OK) == 0)
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; OK\n",
					  fontptr->name);
				fontptr->type = TYPE_PK;
				return TRUE;
			}
			else
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; No\n",
					  fontptr->name);
			}
		}

		if(use_gf)
		{
			/*
			 * Try flat structure.
			 */
			sprintf(fontptr->name, "%s/%s.%dgf",
			  dir, fontptr->n, fontptr->font_gf_mag);
			if(access(fontptr->name, R_OK) == 0)
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; OK\n",
					  fontptr->name);
				fontptr->type = TYPE_GF;
				return TRUE;
			}
			else
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; No\n",
					  fontptr->name);
			}

			/*
			 * Try hierarchical structure.
			 */
			sprintf(fontptr->name, "%s/%s/%s.%dgf",
			  dir, fontptr->n, fontptr->n, fontptr->font_gf_mag);
			if(access(fontptr->name, R_OK) == 0)
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; OK\n",
					  fontptr->name);
				fontptr->type = TYPE_GF;
				return TRUE;
			}
			else
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; No\n",
					  fontptr->name);
			}
		}

		if(use_pxl)
		{
			/*
			 * Try flat structure.
			 */
			sprintf(fontptr->name, "%s/%s.%dpxl",
			  dir, fontptr->n, fontptr->font_pxl_mag);
			if(access(fontptr->name, R_OK) == 0)
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; OK\n",
					  fontptr->name);
				fontptr->type = TYPE_PXL;
				return TRUE;
			}
			else
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; No\n",
					  fontptr->name);
			}

			/*
			 * Try hierarchical structure.
			 */
			sprintf(fontptr->name, "%s/%s/%s.%dpxl",
			  dir, fontptr->n, fontptr->n, fontptr->font_pxl_mag);
			if(access(fontptr->name, R_OK) == 0)
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; OK\n",
					  fontptr->name);
				fontptr->type = TYPE_PXL;
				return TRUE;
			}
			else
			{
				if(verbose & DEBUG_FONTS)
					fprintf(stderr, " Try %s; No\n",
					  fontptr->name);
			}
		}
	}

	return FALSE;
}

/*
 * find_best_file_in_path:
 *	Finds the best match to the desired font.
 *	Returns TRUE if a suitable match is found.
 */

bool
find_best_file_in_path(dirpath, fontptr, p_min_df, p_min_dp, p_min_dm)
char *dirpath;
struct font_entry *fontptr;
int *p_min_df, *p_min_dp, *p_min_dm;
{
	register char *p;
	char dir[MAXPATHLEN];
	bool status = FALSE;

	if(verbose & DEBUG_FONTS)
		fprintf(stderr, "find_best_font_file(%s .%dgf or %dpxl)\n",
		  fontptr->n, fontptr->font_gf_mag, fontptr->font_pxl_mag);

	/*
	 * Scan over directories in dirpath.
	 */
	while(*dirpath)
	{
		/*
		 * Copy first/next path prefix to dir[].
		 * Skip over the ':'.
		 */
		for(p = dir; *dirpath != '\0'; )
		{
			if(*dirpath == ':' || *dirpath == ';')
			{
				dirpath++;
				break;
			}
			else
				*p++ = *dirpath++;
		}
		*p = '\0';

		/*
		 * Scan the directory.
		 */
		if(scanpdir(dir, fontptr, p_min_df, p_min_dp, p_min_dm))
			status = TRUE;

		/*
		 * Scan any subdirectories.
		 */
		if(scandir(dir, fontptr, p_min_df, p_min_dp, p_min_dm))
			status = TRUE;
	}

	return status;
}

/*
 * scandir:
 *	Scan directory looking for plausible names,
 *	then recurse to subdirectories for plausible point sizes.
 *	Returns TRUE if found a more plausible candidate than previous best.
 */

bool
scandir(dir, fontptr, p_min_df, p_min_dp, p_min_dm)
char *dir;
struct font_entry *fontptr;
int *p_min_df, *p_min_dp, *p_min_dm;
{
	DIR *dirstream;
	struct direct *dirrecord;
	char pdir[MAXPATHLEN];
	bool status = FALSE;

	if(verbose & DEBUG_FONTS)
		fprintf(stderr, " scandir(%s .%dgf or .%dpxl)\n",
		  fontptr->n, fontptr->font_gf_mag, fontptr->font_pxl_mag);

	if(! (dirstream = opendir(dir)))
		return FALSE;

	while(dirrecord = readdir(dirstream))
	{
		if(dirrecord->d_name[0] != '.')
			continue;

		if(strdiff(fontptr->n, dirrecord->d_name) <= *p_min_df)
		{
			sprintf(pdir, "%s/%s", dir, dirrecord->d_name);

			if(scanpdir(pdir, fontptr,
			  p_min_df, p_min_dp, p_min_dm))
				status = TRUE;
		}
	}

	closedir(dirstream);

	return status;
}

/*
 * scanpdir:
 *	Scan directory looking for plausible names and point sizes.
 *	Returns TRUE if found a more plausible candidate than previous best.
 */

bool
scanpdir(dir, fontptr, p_min_df, p_min_dp, p_min_dm)
char *dir;
struct font_entry *fontptr;
int *p_min_df, *p_min_dp, *p_min_dm;
{
	DIR *dirstream;
	struct direct *dirrecord;
	char qfamily[MAXPATHLEN];
	char qtype[MAXPATHLEN];
	int qpoint, qmag, df, dp, dm;
	bool status = FALSE;
	char family[MAXPATHLEN];
	int point;

	if(verbose & DEBUG_FONTS)
		fprintf(stderr, " scanpdir(%s .%dgf or .%dpxl)\n",
		  fontptr->n, fontptr->font_gf_mag, fontptr->font_pxl_mag);

	/*
	 * Split out family name, point size and magnification.
	 */
	if(sscanf(fontptr->n, "%[^0123456789.]%d", family, &point) != 2)
	{
		message("Font name \"%s\" appears garbled.", fontptr->n);
		return FALSE;
	}

	if(! (dirstream = opendir(dir)))
		return FALSE;

	while(dirrecord = readdir(dirstream))
	{
		if(sscanf(dirrecord->d_name, "%[^0123456789.]%d.%d%s",
		  qfamily, &qpoint, &qmag, qtype) != 4)
			continue;

		/*
		 * Is this a GF file.
		 */
		if(use_gf && strcmp(qtype, "gf") == 0)
		{
			df = strdiff(family, qfamily);
			dp = abs(point - qpoint);
			dm = abs(fontptr->font_gf_mag - qmag);
			if((df < *p_min_df) ||
			   (df == *p_min_df && dp < *p_min_dp) ||
			   (df == *p_min_df && dp == *p_min_dp &&
			     dm < *p_min_dm))
			{
				*p_min_df = df;
				*p_min_dp = dp;
				*p_min_dm = dm;

				sprintf(fontptr->name, "%s/%s",
				  dir, dirrecord->d_name);
				fontptr->type = TYPE_GF;

				if(verbose & DEBUG_FONTS)
					fprintf(stderr,
					  "  New best match (%d %d %d) is %s\n",
					  df, dp, dm, fontptr->name);

				status = TRUE;
			}
		}

		/*
		 * Is this a PXL file.
		 */
		if(use_pxl && strcmp(qtype, "pxl") == 0)
		{
			df = strdiff(family, qfamily);
			dp = abs(point - qpoint);
			dm = abs(fontptr->font_pxl_mag - qmag);
			if((df < *p_min_df) ||
			   (df == *p_min_df && dp < *p_min_dp) ||
			   (df == *p_min_df && dp == *p_min_dp &&
			     dm < *p_min_dm))
			{
				*p_min_df = df;
				*p_min_dp = dp;
				*p_min_dm = dm;

				sprintf(fontptr->name, "%s/%s",
				  dir, dirrecord->d_name);
				fontptr->type = TYPE_PXL;

				if(verbose & DEBUG_FONTS)
					fprintf(stderr,
					  "  New best match (%d %d %d) is %s\n",
					  df, dp, dm, fontptr->name);

				status = TRUE;
			}
		}

		/*
		 * Is this a PK file.
		 */
		if(use_pk && strcmp(qtype, "pk") == 0)
		{
			df = strdiff(family, qfamily);
			dp = abs(point - qpoint);
			dm = abs(fontptr->font_gf_mag - qmag);
			if((df < *p_min_df) ||
			   (df == *p_min_df && dp < *p_min_dp) ||
			   (df == *p_min_df && dp == *p_min_dp &&
			     dm < *p_min_dm))
			{
				*p_min_df = df;
				*p_min_dp = dp;
				*p_min_dm = dm;

				sprintf(fontptr->name, "%s/%s",
				  dir, dirrecord->d_name);
				fontptr->type = TYPE_PK;

				if(verbose & DEBUG_FONTS)
					fprintf(stderr,
					  "  New best match (%d %d %d) is %s\n",
					  df, dp, dm, fontptr->name);

				status = TRUE;
			}
		}
	}

	closedir(dirstream);

	return status;
}

/*
 * strdiff:
 *	Quantify differences in font names.
 */

int 
strdiff(s1, s2)
char *s1, *s2;
{
	register int diff = 0;

	while(*s1 && *s2)
		diff += abs(*s1++ - *s2++);
	while(*s1)
		diff += *s1++;
	while(*s2)
		diff += *s2++;

	return diff;
}