1 /* Copyright (c) 1987, 1989, 2012 University of Maryland Department of
2    Computer Science.
3 
4    Permission is hereby granted, free of charge, to any person obtaining
5    a copy of this software and associated documentation files (the
6    "Software"), to deal in the Software without restriction, including
7    without limitation, the rights to use, copy, modify, merge, publish,
8    distribute, sublicense, and/or sell copies of the Software, and to
9    permit persons to whom the Software is furnished to do so, subject to
10    the following conditions: The above copyright notice, this permission
11    notice and the disclaimer statement shall be included in all copies
12    or substantial portions of the Software.
13 
14    THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15    EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16    MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
17    IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
18    CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
19    TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
20    SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
21 */
22 
23 #ifndef _MCTEX_FONT_
24 #define _MCTEX_FONT_
25 
26 /*
27  * Font file information, readers, etc.
28  */
29 
30 #ifndef _MCTEX_TYPES_
31 #include "types.h"
32 #endif
33 
34 /*
35  * First, font independent information: per glyph info, and per font
36  * info.
37  */
38 struct glyph {
39 	short	g_flags;	/* see below */
40 	/*
41 	 * The following cannot be used with GetRasterlessFont
42 	 */
43 	short	g_rotation;	/* see below */
44 	char	*g_raster;	/* raster, if known */
45 	/*
46 	 * These, however, do come with rasterless fonts,
47 	 * even though they relate only to the raster.
48 	 */
49 	i32	g_height;	/* height of bounding box */
50 	i32	g_width;	/* width of bounding box */
51 	i32	g_yorigin;	/* y origin (>= 0 -> within box) */
52 	i32	g_xorigin;	/* x origin (>= 0 -> within box) */
53 	/*
54 	 * These of course come with every font.
55 	 */
56 	i32	g_rawtfmwidth;	/* raw TFM width (in FIXes) */
57 	i32	g_tfmwidth;	/* TFM width in scaled points */
58 	/*
59 	 * The X and Y escapements come from PK and GF fonts.
60 	 * They are in scaled pixels (65536 scaled pixels = 1 pixel).
61 	 * If they are not defined by the font, these are set to
62 	 * NO_ESCAPEMENT.
63 	 */
64 	i32	g_xescapement;	/* x escapement (`chardx') */
65 	i32	g_yescapement;	/* y escapement (`chardy') */
66 #define	NO_ESCAPEMENT	((i32)0x89abcdef)
67 	/*
68 	 * This is provided purely for DVI to device conversion programs.
69 	 */
70 	int	g_pixwidth;	/* width in pixels */
71 	/*
72 	 * Mainly for internal use, index is the glyph index within the
73 	 * font.  That is, f->f_gly[i]->g_index == i.
74 	 */
75 	int	g_index;	/* character index */
76 	/*
77 	 * g_details and g_integer are purely for the font reading
78 	 * subroutines to use however they will.  g_next makes lists
79 	 * of glyphs while the glyphs are free.
80 	 */
81 	union {			/* various options */
82 		char	*g_details;	/* details: arbitrary */
83 		i32	g_integer;	/* 32 bit integer */
84 		struct	glyph *g_next;	/* linked list */
85 	} g_un;
86 };
87 
88 /*
89  * Glyph flags.
90  */
91 #define	GF_VALID	0x0001	/* glyph is `real' */
92 #define	GF_SEEN		0x0002	/* glyph has been encountered/loaded */
93 
94 #define	GF_USR0		0x0100	/* reserved to user code */
95 #define	GF_USR1		0x0200	/* reserved to user code */
96 #define	GF_USR2		0x0400	/* reserved to user code */
97 #define	GF_USR3		0x0800	/* reserved to user code */
98 
99 /*
100  * Rotations are in quarter-pi steps, counterclockwise of course.
101  * This order must be maintained; see rotate.c.
102  */
103 #define	ROT_NORM	0		/* no rotation: `normal' position */
104 #define	ROT_LEFT	1		/* 1/4 turn counterclockwise */
105 #define	ROT_DOWN	2		/* 1/2 turn, i.e., upside-down */
106 #define	ROT_RIGHT	3		/* 3/4 turn ccw, or 1/4 turn cw */
107 
108 struct font {
109 	int	f_flags;	/* see below */
110 	struct	fontops *f_ops;	/* operations */
111 	/*
112 	 * f_un is provided for DVI drivers, which typically need
113 	 * some way to translate DVI font numbers into local values.
114 	 * Most seem to need integers.
115 	 */
116 	union {
117 		int	f_int;	/* value if int */
118 		i32	f_i32;	/* value if i32 */
119 		char	*f_ptr;	/* value if pointer */
120 	} f_un;
121 #ifdef notdef
122 	/* for LRU cacheing; not yet implemented */
123 	i32	f_lastuse;
124 #endif
125 	/*
126 	 * f_details is provided for font reading subroutines.
127 	 * It is intended to be cast to a pointer to a structure
128 	 * that is allocated by those routines, and used to keep
129 	 * track of whatever information those routines need to
130 	 * determine glyph boxes and (if asked for) rasters.
131 	 */
132 	char	*f_details;	/* type dependent stuff */
133 	/*
134 	 * f_path is the full pathname to the font file, filled in
135 	 * by GetFont and GetRasterlessFont.  Note that since we
136 	 * hold on to the path until the font is freed, it should be
137 	 * possible to cache glyph accesses on memory-poor machines.
138 	 */
139 	char	*f_path;	/* font file pathname */
140 	/*
141 	 * f_dvimag and f_dvidsz are the magnification and design size
142 	 * values from the DVI file.  f_font and f_scaled correspond to
143 	 * TeX's idea of the proper name for the font (e.g., `cmr10',
144 	 * `cmbx10 scaled 1440').  (Note that f_scaled is just the
145 	 * ratio of f_dvimag and f_dvidsz; you could save a bit of memory
146 	 * by eliminating it and altering the routine Font_TeXName()).
147 	 * f_design_size is the font's design size, or 0 for fake (TFM)
148 	 * fonts.  f_checksum should be set by the font reading routines
149 	 * to the font checksum.  If the value is nonzero, it will be
150 	 * compared to the checksum in the DVI file.
151 	 */
152 	i32	f_dvimag;	/* magnification from DVI file */
153 	i32	f_dvidsz;	/* design size from DVI file */
154 	char	*f_font;	/* TeX's name for the font */
155 	int	f_scaled;	/* the ratio of dvimag to dvidsz, x1000 */
156 	i32	f_design_size;	/* font design size */
157 	i32	f_checksum;	/* font checksum, or 0 */
158 	/*
159 	 * The three values f_pspace, f_nspace, and f_vspace correspond
160 	 * to the amount of positive, negative, and vertical space that
161 	 * limits `kerning' (if horizontal) or `in-line motion' (vertical).
162 	 * f_pspace is equal to 1/6 of f_dvimag, and f_nspace and f_vspace
163 	 * are -4 and 5 times this value respectively.  DVItype puts it
164 	 * this way (slightly edited):
165 	 *
166 	 *	Rounding to the nearest pixel is best done in [this]
167 	 *	manner ... so as to be inoffensive to the eye:  When [a]
168 	 *	horizontal motion is small, like a kern, [the device
169 	 *	position] changes by rounding the kern; but when the
170 	 *	motion is large, [the device position] changes by rounding
171 	 *	the [DVI position] so that accumulated rounding errors
172 	 *	disappear.  We allow a larger space in the negative
173 	 *	direction than in the positive one, because TeX makes
174 	 *	comparatively large backspaces when it positions accents.
175 	 *
176 	 *	Vertical motion is done similarly, but with the threshold
177 	 *	between ``small'' and ``large'' increased by a factor of
178 	 *	five. The idea is to make fractions like ``$1\over2$''
179 	 *	round consistently, but to absorb accumulated rounding
180 	 *	errors in the baseline-skip moves.
181 	 *
182 	 * There is, however, an exception:
183 	 *
184 	 *	A sequence of consecutive rules, or consecutive characters
185 	 *	in a fixed-width font whose width is not an integer number
186 	 *	of pixels, can cause [the device position] to drift far
187 	 *	away from a correctly rounded value.
188 	 *
189 	 * The drift correction is applied after every horizontal or
190 	 * vertical motion, including any resulting from printing a
191 	 * character.
192 	 */
193 	i32	f_pspace;	/* limit on positive kerns */
194 	i32	f_nspace;	/* limit on negative kerns */
195 	i32	f_vspace;	/* limit on vertical `kerns' */
196 
197 	/*
198 	 * If set, f_hppp and f_vppp tell how many pixels there are
199 	 * per scaled point in the font.  For instance, a 300 dpi font
200 	 * has 300pixels/1inch * 1inch/72.27pt * 65536pt/1sp ~=
201 	 * 272046 pixels/sp.
202 	 *
203 	 * If they are zero, the font does not tell us.
204 	 */
205 	i32	f_hppp;		/* horizontal pixels per point */
206 	i32	f_vppp;		/* vertical pixels per point */
207 	/*
208 	 * f_lowch and f_highch bound the region in which f_gly
209 	 * indicies are valid.  Specificially, f_gly[i] may be
210 	 * read or written if and only if i is in the half-open
211 	 * interval [f_lowch..f_highch).  f_gly is an array of
212 	 * pointers to glyph structures.  The structures themselves
213 	 * are not allocated until requested.
214 	 *
215 	 * f_glybase is the actual return from malloc(), since it
216 	 * is theoretically possible for f_gly-f_lowch to become
217 	 * NULL.
218 	 */
219 	int	f_lowch;	/* first character */
220 	int	f_highch;	/* last character, plus 1 */
221 	struct	glyph **f_gly;	/* glyphs */
222 	struct	glyph **f_glybase;
223 };
224 
225 /*
226  * Macros for rounding DVI and device motions per DVItype rules.
227  */
228 #define	F_SMALLH(f, m) ((m) < (f)->f_pspace && (m) > (f)->f_nspace)
229 #define	F_SMALLV(f, m) ((m) < (f)->f_vspace && -(m) < (f)->f_vspace)
230 
231 /*
232  * Font flags.
233  */
234 #define	FF_RASTERS	0x0001	/* font has rasters */
235 #define	FF_USR0		0x0100	/* reserved to user code */
236 #define	FF_USR1		0x0200	/* reserved to user code */
237 #define	FF_USR2		0x0400	/* reserved to user code */
238 #define	FF_USR3		0x0800	/* reserved to user code */
239 
240 /*
241  * Operations on fonts.
242  *
243  * The `fo_dpitomag' field is used as a multiplier for a desired
244  * resolution in dots per inch.  The result of the multiplication
245  * is converted to a font name by multipying by 1000.0 and rounding.
246  * The idea is that PXL files will have a multiplier of 5.0, and
247  * others will have a multiplier of 1.0.  This suffices for the
248  * present, at any rate; in the future, this field may be replaced
249  * with something more general.
250  *
251  * N.B.: more operations may be added as they are discovered to be
252  * useful.
253  */
254 struct	fontops {
255 	char	*fo_name;		/* name, e.g., "gf" */
256 	int	fo_fakefont;		/* 1="box", 2="blank"; else 0 */
257 	double	fo_dpitomag;		/* multiplier */
258 	int	(*fo_read)();		/* open and read the font itself */
259 	int	(*fo_getgly)();		/* obtain specified glyphs (range) */
260 #ifdef notdef
261 	void	(*fo_freegly)();	/* release specified glyphs */
262 #endif
263 	int	(*fo_rasterise)();	/* rasterise specified glyphs */
264 	void	(*fo_freefont)();	/* discard font (free details) */
265 	struct	fontops *fo_next;	/* purely for font.c */
266 };
267 
268 /*
269  * Return a pointer to the glyph information for character `c' in
270  * font `f'.
271  */
272 #define	GLYPH(f, c) \
273 	((c) < (f)->f_lowch || (c) >= (f)->f_highch ? (struct glyph *)0 : \
274 	 ((f)->f_gly[c] ? (f)->f_gly[c] : GetGlyph(f, (int)(c))))
275 
276 /*
277  * True iff glyph `g' is valid.  Useful for checking return values
278  * from GLYPH().
279  */
280 #define	GVALID(g)	((g) && ((g)->g_flags & GF_VALID))
281 
282 /*
283  * True iff glyph g has a raster.
284  */
285 #define	HASRASTER(g)	((g)->g_height && (g)->g_width)
286 
287 /*
288  * Return a pointer to the raster information for glyph `g' in font
289  * `f' at rotation `r'.
290  */
291 #define	RASTER(g, f, r)	((g)->g_rotation == (r) && (g)->g_raster ? \
292 			 (g)->g_raster : GetRaster(g, f, r))
293 
294 /*
295  * Function types.
296  */
297 struct	font *GetFont(char *nm, i32 dvimag, i32 dvidsz, char *dev, char **fname);
298 struct	font *GetRasterlessFont(char *nm, i32 dvimag, i32 dvidsz, char *dev, char **fname);
299 struct	glyph *GetGlyph();
300 char	*GetRaster();
301 void	FreeFont();
302 void	FreeGlyph();
303 void	FreeRaster();
304 char	*Font_TeXName();
305 void	fontinit(char *file);
306 
307 /*
308  * Normally from stdio.h
309  */
310 #ifndef NULL
311 #define	NULL	0
312 #endif
313 
314 /*
315  * The following environment variable overrides the default font
316  * configuration file.  That default is used when fontinit() is not
317  * called, or is passed a null pointer.
318  */
319 #define	CONFENV	"TEXFONTDESC"
320 
321 #endif /* _MCTEX_FONT_ */
322