1: /* font.c Reads a font from a file and stores it on the workstation
2: *
3: * GetFont Takes a font name and stores it
4: * FreeFont Frees the storage taken by a font
5: *
6: * Modification History
7: *
8: * Carver 8601.13 Fix reference to ../libvs100/param.h to be param.h
9: *
10: * Jones 8510.15 Fix ``memory leak'' -- deallocate leftarry in FreeFont
11: *
12: * Carver 8510.03 Increased the allocation size of the left array buffer
13: * by 1 word. Fixes boundary problem.
14: */
15:
16: #include "ddxqvss.h"
17: #include "param.h"
18: #include <errno.h>
19:
20: extern int errno;
21: extern char *ddxfontdir;
22: extern char *ddxfontsuffix;
23: char *Xalloc(), *strcpy(), *strcat();
24: long lseek();
25:
26: #define CHARPERFONT 256
27:
28: FONT *GetFont (name)
29: char *name;
30: {
31: char fontname[256];
32: int fontfile;
33: FontData font;
34: #define chars ((BitMap *) font.f_characters)
35: int fontsize, leftsize, width, i, j;
36: char *fontarea;
37: register short *leftarea, *leftarray;
38: register FONT *fd;
39: register FontPriv *fpriv;
40: int tablesize = (CHARPERFONT + 1) * sizeof(short); /* 8510.03 Carver */
41:
42: strcpy (fontname, ddxfontdir);
43: strcat (fontname, name);
44: strcat (fontname, ddxfontsuffix);
45:
46: if ((fontfile = open (fontname, 0)) == -1 &&
47: (errno != ENOENT || (fontfile = open (name, 0)) == -1)) {
48: errno = EINVAL;
49: return (NULL);
50: }
51:
52: if (read (fontfile, (caddr_t) &font, sizeof (FontData)) != sizeof (FontData)) {
53: close (fontfile);
54: errno = EINVAL;
55: return (NULL);
56: }
57:
58: fontsize = BitmapSize(chars->bm_width, chars->bm_height);
59: fontarea = (char *) Xalloc (fontsize);
60: lseek (fontfile, (long) font.f_characters[0], 0);
61: if (read (fontfile, fontarea, fontsize) != fontsize) {
62: close (fontfile);
63: free (fontarea);
64: errno = EINVAL;
65: return (NULL);
66: }
67:
68: leftarea = (short *) Xalloc (tablesize);
69: bzero(leftarea, tablesize);
70: leftarray = (short *) Xalloc (tablesize);
71: if (font.f_fixedWidth == 0) {
72: leftsize = (font.f_lastChar - font.f_firstChar + 2) * sizeof (short);
73: lseek (fontfile, (long) font.f_leftArray[0], 0);
74: if (read (fontfile, & leftarea[font.f_firstChar], leftsize)
75: != leftsize) {
76: close (fontfile);
77: free (fontarea);
78: free ((caddr_t) leftarea);
79: free ((caddr_t) leftarray);
80: errno = EINVAL;
81: return (NULL);
82: }
83: } else { /* if fixed with font, generate leftarray for use later */
84: j = 0;
85: for (i = font.f_firstChar; i <= font.f_lastChar + 1; i++) {
86: leftarea[i] = j;
87: j += font.f_fixedWidth;
88: }
89: }
90: bcopy(leftarea, leftarray, tablesize);
91:
92: close (fontfile);
93:
94: fd = (FONT *) Xalloc (sizeof (FONT));
95:
96: fd->height = chars->bm_height;
97: fd->first = font.f_firstChar;
98: fd->last = font.f_lastChar;
99:
100: fd->base = font.f_baseline;
101: fd->space = font.f_spaceIndex;
102: fd->space += fd->first;
103: fpriv = (FontPriv *) Xalloc (sizeof (FontPriv));
104: if (fd->avg_width = font.f_fixedWidth) {
105: fd->fixed = 1;
106: fpriv->maxwidth = fd->avg_width;
107: }
108: else
109: fd->fixed = 0;
110:
111: fd->refcnt = 1;
112: fd->data = (caddr_t) fpriv;
113: fpriv->widths = leftarea;
114: fpriv->leftarray = leftarray;
115:
116: if ((fpriv->strike = (BITMAP *) Xalloc(sizeof(BITMAP))) == NULL) {
117: free (fontarea);
118: free ((caddr_t) leftarea);
119: free ((caddr_t) leftarray);
120: free ((caddr_t) fd);
121: free ((caddr_t) fpriv);
122: return (NULL);
123: }
124: fpriv->wpitch = (((chars->bm_width + 15) >> 3) & ~1);
125: fpriv->strike->width = chars->bm_width;
126: fpriv->strike->height = chars->bm_height;
127: fpriv->strike->refcnt = 1;
128: fpriv->strike->data = (caddr_t) fontarea;
129: /*
130: * compute line table for font to eliminate multiply to find beginning
131: * of line.
132: */
133: fpriv->fltable = (char **)Xalloc(chars->bm_height * sizeof(caddr_t));
134: for (i = 0; i < chars->bm_height; i++)
135: fpriv->fltable[i] = ((caddr_t) fontarea) + i * fpriv->wpitch;
136:
137: fd->name = (char *) Xalloc (strlen (name) + 1);
138: strcpy (fd->name, name);
139:
140: fpriv->maxwidth = 0;
141:
142: /* convert the leftarray to the width table */
143: for (i = fd->first; i <= fd->last; i++) {
144: width = fpriv->leftarray[i + 1] - fpriv->leftarray[i];
145: if (width > fpriv->maxwidth) fpriv->maxwidth = width;
146: if (width < 0) {
147: width = 0; /* font sanity check */
148: DeviceError ("Bad font leftarray!\n");
149: }
150: fpriv->widths[i] = width;
151: }
152:
153: fd->avg_width = ((fpriv->leftarray[fd->last] -
154: fpriv->leftarray[fd->first]) / (fd->last - fd->first));
155:
156: /* striketobitmaps(fd);*/
157: return (fd);
158: #undef chars
159: }
160:
161: FreeFont (font)
162: register FONT *font;
163: {
164: register FontPriv *data;
165:
166: data = FDATA(font);
167: #ifdef DoStrikeArray
168: if (data->chrs) free ((caddr_t) data->chrs);
169: #endif
170: if (data->leftarray) free ((caddr_t) data->leftarray);
171: if (data->widths) free ((caddr_t) data->widths);
172: free (data->fltable);
173: FreeBitmap(data->strike);
174: free ((caddr_t) data);
175: free (font->name);
176: free ((caddr_t) font);
177: }
178:
179: /*
180: * this routine converts strike format into an array of bitmaps
181: */
182: #ifdef DoStrikeArray
183: striketobitmaps(fn)
184: FONT *fn;
185: {
186: register FontPriv *fp = FDATA(fn);
187: int fheight = fn->height;
188: int i, j;
189: register long *bits;
190: register int tmp;
191: int length =
192: (fn->last - fn->first + 1) * fheight * sizeof(long);
193: if (fp->maxwidth > 32) return;
194: fp->chrs = bits = (long *)Xalloc( length );
195: bzero(bits, length);
196:
197: for (i = fn->first; i <= fn->last; i++) {
198: register w = fp->widths[i];
199: register offset = fp->leftarray[i];
200: if (w < 0) w = 0; /* sanity check for bad fonts */
201: for (j = 0; j < fheight; j++) {
202: register char *base = fp->fltable[j];
203: tmp = extzv(base, offset, w);
204: *bits = tmp;
205: bits += 1;
206: }
207: }
208: return;
209: }
210: #endif
Defined functions
Defined macros
chars
defined in line
34; used 9 times