summaryrefslogtreecommitdiff
path: root/dviware/dvibook/libtex/font_subr.c
blob: 97e82fc8c14b1a00980ca348504766f3e4f49c0c (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
/*
 * 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: /usr/src/local/tex/local/mctex/lib/RCS/font_subr.c,v 2.8 89/10/30 04:08:52 chris Exp $";
#endif

/*
 * Subroutines common to all fonts.
 */

#include "types.h"
#include "error.h"
#include "font.h"

static struct glyph *freeglyphs;

char	*malloc();
extern	errno;

/*
 * Set up the font structures to note that a font has glyphs in
 * the half-open interval [low, high).
 *
 * SHOULD I ALLOW ADDITIONS TO THE RANGE VIA SUBSEQUENT CALLS TO
 * FontHasGlyphs?
 */
FontHasGlyphs(f, low, high)
	register struct font *f;
	register int low, high;
{
	register struct glyph **gp;

	/* record bounds */
	f->f_lowch = low;
	f->f_highch = high;

	/*
	 * Allocate space for all the glyph pointers, and set
	 * them all to NULL.
	 */
	if (low >= high)	/* no glyphs */
		gp = NULL;
	else {
		gp = (struct glyph **) malloc((unsigned) (high - low) *
			sizeof (*gp));
		if (gp == NULL)
			return (-1);
	}
	f->f_glybase = gp;
	f->f_gly = gp - low;
	while (++low <= high)
		*gp++ = NULL;
	return (0);
}

/*
 * AllocGlyph allocates a new glyph.  ReleaseGlyph puts one onto the free
 * list.  We maintain a local list of free glyphs.
 */
#define ReleaseGlyph(g)	\
	((g)->g_un.g_next = freeglyphs, freeglyphs = (g))

static struct glyph *
AllocGlyph(n)
	int n;
{
	register struct glyph *g;
	register int i;

	if ((g = freeglyphs) == NULL) {
		g = (struct glyph *) malloc((unsigned) (128 * sizeof (*g)));
		if (g == NULL)
			error(1, errno, "out of glyph memory");
		g += (i = 128);
		while (--i >= 0) {
			g--;
			ReleaseGlyph(g);
		}
	}
	freeglyphs = g->g_un.g_next;
	g->g_flags = 0;
	g->g_raster = NULL;
	g->g_index = n;
	g->g_xescapement = NO_ESCAPEMENT;/* default, if not set by font */
	g->g_yescapement = NO_ESCAPEMENT;
	return (g);
}

/*
 * Free one glyph.
 */
void
FreeGlyph(f, g)
	struct font *f;
	register struct glyph *g;
{
	int n = g->g_index;

#ifdef notdef
	(*f->f_ops->fo_freegly)(f, n, n);
#endif
	if (g->g_raster != NULL)
		free(g->g_raster);
	ReleaseGlyph(g);
	f->f_gly[n] = NULL;
}

/*
 * Free a font.
 */
void
FreeFont(f)
	register struct font *f;
{
	register struct glyph *g;
	register int i;

#ifdef notdef
	(*f->f_ops->fo_freegly)(f, f->f_lowch, f->f_highch);
#endif
	for (i = f->f_lowch; i < f->f_highch; i++) {
		if ((g = f->f_gly[i]) == NULL)
			continue;
		if (g->g_raster != NULL)
			free(g->g_raster);
		ReleaseGlyph(g);
	}
	if (f->f_glybase != NULL)
		free((char *)f->f_glybase);
	(*f->f_ops->fo_freefont)(f);
	free(f->f_path);
	free(f->f_font);
	free((char *)f);
}

/*
 * Get glyph `c' in font `f'.  We pull in a few adjacent glyphs here
 * under the (perhaps naive) assumption that things will go faster
 * that way.
 *
 * TODO:
 *	try different adjacency values
 *	make adjacency a font attribute? (or an op)
 */
#define	ADJ	4		/* must be a power of 2 */
#define	GET_ADJ(c, l, h) ((h) = ADJ + ((l) = (c) & ~(ADJ - 1)))

struct glyph *
GetGlyph(f, c)
	register struct font *f;
	int c;
{
	register int i, h, l;

	GET_ADJ(c, l, h);
	if (l < f->f_lowch)
		l = f->f_lowch;
	if (h > f->f_highch)
		h = f->f_highch;
	if (l >= h)
		return (NULL);
	for (i = l; i < h; i++)
		if (f->f_gly[i] == NULL)
			f->f_gly[i] = AllocGlyph(i);

	if ((*f->f_ops->fo_getgly)(f, l, h)) {
		/*
		 * I do not know what to do about this just yet, so:
		 */
		panic("getgly fails and I am confused ... help!");
	}

	/*
	 * Apply the appropriate scale factor to the TFM widths.
	 * This makes them come out in scaled points, instead of FIXes.
	 */
	ScaleGlyphs(f, l, h);	/* ??? */

	return (f->f_gly[c]);
}

/*
 * Get the raster for glyph g in font f at rotation r.
 */
char *
GetRaster(g, f, r)
	register struct glyph *g;
	register struct font *f;
	int r;
{
	int l, h;

	/* abort if caller did not ask for rasters in advance */
	if ((f->f_flags & FF_RASTERS) == 0)
		panic("GetRaster(%s)", f->f_path);

	/*
	 * If there is no raster, get one.  Blank characters,
	 * however, never have rasters.
	 */
	if (g->g_raster == NULL) {
		if (!HASRASTER(g))
			return (NULL);
		/*
		 * THE FOLLOWING DEPENDS ON THE ADJACENCY MATCHING THAT IN
		 * GetGlyph() ABOVE.
		 */
		GET_ADJ(g->g_index, l, h);
		if (l < f->f_lowch)
			l = f->f_lowch;
		if (h > f->f_highch)
			h = f->f_highch;
		if ((*f->f_ops->fo_rasterise)(f, l, h))
			error(1, 0, "rasterise fails (out of memory?)");
	}
	if (g->g_rotation != r)
		SetRotation(g, r);
	return (g->g_raster);
}

void
FreeRaster(g)
	struct glyph *g;
{

	if (g->g_raster) {
		free((g)->g_raster);
		g->g_raster = NULL;
	}
}

/*
 * Return a TeX-style font name, including scale factor.
 * SHOULD I BOTHER WITH \magstep STYLE NAMES?
 */
char *
Font_TeXName(f)
	register struct font *f;
{
	static char result[200];

	if (f->f_scaled == 1000)
		return (f->f_font);
	(void) sprintf(result, "%s scaled %d", f->f_font, f->f_scaled);
	return (result);
}