summaryrefslogtreecommitdiff
path: root/dviware/dvi2pcl/sortfonts.c
blob: 5843842f267c31d1d4c9cd885665241142f4efaf (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
/*
 * Sorts order of fonts used in the dvi-file and not stored permanently in
 * the printers memory in respect to font usage. Therefore a rating value
 * is calculated for each font from their character usage in the document.
 * The fonts are ordered in `fontlist' in order of their rating value
 * `usage'. The function returns the number of different fonts used, but
 * not stored  permanently. As a side effect, for each font the first and
 * last character, used in the document ist stored in font->bc and font->ec
 * resp., to avoid unnecessary searching later on.
 */

#include "globals.h"

int sortfonts(fontlist)
int fontlist[];
{
	int	f;
	int	i;
	int	t;
	int	store_all;
	int	fontnum = 0;
	long	total;
	long	different;
	long	tt;
	long	usage[MAXFONTS];
	charfmt	*u;
	charfmt	*cbase;

	/* Determine rating value for each font used but not perm loaded */
	for (f = 0 ; f < MAXFONTS ; f++)
		if ((font = fontptr[f]) && (font->down < 0)) {
			cbase = font->chr;
			total = different = 0;
			font->bc = 256;
			font->ec = -1;
			for(i = 0 ; i < 256 ; i++)
				if((cbase + i)->use_count)
				{
					font->bc = i;
					break;
				}                      /* First char used */
			for(i = 255 ; i >= font->bc ; i--)
				if((cbase + i)->use_count) {
					font->ec = i;
					break;
				}                      /* Last char used  */
			for(i = font->bc ; i <= font->ec ; i++) {
				u = cbase + i;
				if(u->use_count) {
					different++;
					total += u->use_count;
				}	/* Different used chars */
			}		/* and total usage      */

			fontlist[fontnum] = f;

			/* Calculate rating */
			if(different)
				usage[fontnum++] =
				    (256*(total - different))/different;
			else
				usage[fontnum++] = 0;
		}

	/* Now sort used fonts in order of the usage rating values */
	for(f = fontnum - 1 ; f > 0 ; f--)
		for(i = 0 ; i < f ; i++)
			if(usage[i] < usage[f]) {
				tt = usage[i];
				usage[i] = usage[f];
				usage[f] = tt;
				t  = fontlist[i];
				fontlist[i] = fontlist[f];
				fontlist[f] = t;
			}

	/* Return the number of different fonts not permanently loaded */
	return(fontnum);
}