15 |
|
#define galloc(nv) (GLYPH *)malloc(sizeof(GLYPH)+2*sizeof(GORD)*(nv)) |
16 |
|
|
17 |
|
|
18 |
< |
extern char *libpath; /* list of library directories */ |
18 |
> |
extern char *getlibpath(); |
19 |
|
|
20 |
|
static FONT *fontlist = NULL; /* list of loaded fonts */ |
21 |
|
|
24 |
|
getfont(fname) /* return font fname */ |
25 |
|
char *fname; |
26 |
|
{ |
27 |
– |
char buf[16]; |
27 |
|
FILE *fp; |
28 |
|
char *pathname, *err; |
29 |
< |
int gn, ngv; |
30 |
< |
register int gv; |
31 |
< |
register GLYPH *g; |
29 |
> |
unsigned wsum, hsum, ngly; |
30 |
> |
int gn, ngv, gv; |
31 |
> |
register GLYPH *g; |
32 |
|
GORD *gp; |
33 |
|
register FONT *f; |
34 |
|
|
36 |
|
if (!strcmp(f->name, fname)) |
37 |
|
return(f); |
38 |
|
/* load the font file */ |
39 |
< |
if ((pathname = getpath(fname, libpath, R_OK)) == NULL) { |
39 |
> |
if ((pathname = getpath(fname, getlibpath(), R_OK)) == NULL) { |
40 |
|
sprintf(errmsg, "cannot find font file \"%s\"", fname); |
41 |
|
error(USER, errmsg); |
42 |
|
} |
49 |
|
pathname); |
50 |
|
error(SYSTEM, errmsg); |
51 |
|
} |
52 |
< |
while (fgetword(buf,sizeof(buf),fp) != NULL) { /* get each glyph */ |
53 |
< |
if (!isint(buf)) |
52 |
> |
wsum = hsum = ngly = 0; |
53 |
> |
while ((ngv = fgetval(fp, 'i', &gn)) != EOF) { /* get each glyph */ |
54 |
> |
if (ngv == 0) |
55 |
|
goto nonint; |
56 |
< |
gn = atoi(buf); |
57 |
< |
if (gn < 0 || gn > 255) { |
56 |
> |
if (gn < 1 || gn > 255) { |
57 |
|
err = "illegal"; |
58 |
|
goto fonterr; |
59 |
|
} |
61 |
|
err = "duplicate"; |
62 |
|
goto fonterr; |
63 |
|
} |
64 |
< |
if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) || |
66 |
< |
(ngv = atoi(buf)) < 0 || ngv > 32000) { |
64 |
> |
if (fgetval(fp, 'i', &ngv) <= 0 || ngv < 0 || ngv > 32000) { |
65 |
|
err = "bad # vertices for"; |
66 |
|
goto fonterr; |
67 |
|
} |
73 |
|
ngv *= 2; |
74 |
|
gp = gvlist(g); |
75 |
|
while (ngv--) { |
76 |
< |
if (fgetword(buf,sizeof(buf),fp) == NULL || |
79 |
< |
!isint(buf) || |
80 |
< |
(gv = atoi(buf)) < 0 || gv > 255) { |
76 |
> |
if (fgetval(fp, 'i', &gv) <= 0 || gv < 0 || gv > 255) { |
77 |
|
err = "bad vertex for"; |
78 |
|
goto fonterr; |
79 |
|
} |
90 |
|
g->top = gv; |
91 |
|
} |
92 |
|
} |
93 |
+ |
if (g->right - g->left && g->top - g->bottom) { |
94 |
+ |
ngly++; |
95 |
+ |
wsum += g->right - g->left; |
96 |
+ |
hsum += g->top - g->bottom; |
97 |
+ |
} |
98 |
|
f->fg[gn] = g; |
99 |
|
} |
100 |
|
fclose(fp); |
101 |
+ |
if (ngly) { |
102 |
+ |
f->mwidth = wsum / ngly; |
103 |
+ |
f->mheight = hsum / ngly; |
104 |
+ |
} |
105 |
|
f->next = fontlist; |
106 |
|
return(fontlist = f); |
107 |
|
nonint: |
116 |
|
} |
117 |
|
|
118 |
|
|
119 |
+ |
freefont(fname) /* free a font (free all if fname==NULL) */ |
120 |
+ |
char *fname; |
121 |
+ |
{ |
122 |
+ |
FONT head; |
123 |
+ |
register FONT *fl, *f; |
124 |
+ |
register int i; |
125 |
+ |
|
126 |
+ |
head.next = fontlist; |
127 |
+ |
fl = &head; |
128 |
+ |
while ((f = fl->next) != NULL) |
129 |
+ |
if (fname == NULL || !strcmp(fname, f->name)) { |
130 |
+ |
fl->next = f->next; |
131 |
+ |
for (i = 0; i < 256; i++) |
132 |
+ |
if (f->fg[i] != NULL) |
133 |
+ |
free((char *)f->fg[i]); |
134 |
+ |
freestr(f->name); |
135 |
+ |
free((char *)f); |
136 |
+ |
} else |
137 |
+ |
fl = f; |
138 |
+ |
fontlist = head.next; |
139 |
+ |
} |
140 |
+ |
|
141 |
+ |
|
142 |
|
int |
143 |
+ |
uniftext(sp, tp, f) /* uniformly space text line */ |
144 |
+ |
register short *sp; /* returned character spacing */ |
145 |
+ |
register char *tp; /* text line */ |
146 |
+ |
register FONT *f; /* font */ |
147 |
+ |
{ |
148 |
+ |
int linelen; |
149 |
+ |
|
150 |
+ |
linelen = *sp++ = 0; |
151 |
+ |
while (*tp) |
152 |
+ |
if (f->fg[*tp++&0xff] == NULL) |
153 |
+ |
*sp++ = 0; |
154 |
+ |
else |
155 |
+ |
linelen += *sp++ = 255; |
156 |
+ |
return(linelen); |
157 |
+ |
} |
158 |
+ |
|
159 |
+ |
|
160 |
+ |
int |
161 |
|
squeeztext(sp, tp, f, cis) /* squeeze text line */ |
162 |
|
short *sp; /* returned character spacing */ |
163 |
|
char *tp; /* text line */ |
165 |
|
int cis; /* intercharacter spacing */ |
166 |
|
{ |
167 |
|
int linelen; |
168 |
< |
register GLYPH *gp; |
168 |
> |
register GLYPH *gp; |
169 |
|
|
170 |
+ |
linelen = 0; |
171 |
|
gp = NULL; |
172 |
|
while (*tp && (gp = f->fg[*tp++&0xff]) == NULL) |
173 |
|
*sp++ = 0; |
174 |
|
cis /= 2; |
175 |
< |
linelen = *sp = 0; |
175 |
> |
*sp = cis; |
176 |
|
while (gp != NULL) { |
177 |
|
if (gp->nverts) { /* regular character */ |
178 |
|
linelen += *sp++ += cis - gp->left; |
179 |
|
*sp = gp->right + cis; |
180 |
|
} else { /* space */ |
181 |
|
linelen += *sp++; |
182 |
< |
*sp = 256; |
182 |
> |
*sp = f->mwidth; |
183 |
|
} |
184 |
|
gp = NULL; |
185 |
|
while (*tp && (gp = f->fg[*tp++&0xff]) == NULL) { |
187 |
|
*sp = 0; |
188 |
|
} |
189 |
|
} |
190 |
< |
linelen += *sp; |
190 |
> |
linelen += *sp += cis; |
191 |
|
return(linelen); |
192 |
|
} |
193 |
|
|
194 |
|
|
195 |
+ |
int |
196 |
|
proptext(sp, tp, f, cis, nsi) /* space line proportionally */ |
197 |
|
short *sp; /* returned character spacing */ |
198 |
|
char *tp; /* text line */ |
200 |
|
int cis; /* target intercharacter spacing */ |
201 |
|
int nsi; /* minimum number of spaces for indent */ |
202 |
|
{ |
203 |
+ |
register char *end, *tab; |
204 |
+ |
GLYPH *gp; |
205 |
+ |
short *nsp; |
206 |
+ |
int alen, len, width; |
207 |
+ |
/* start by squeezing it */ |
208 |
+ |
squeeztext(sp, tp, f, cis); |
209 |
+ |
/* now, realign spacing */ |
210 |
+ |
width = *sp++; |
211 |
+ |
while (*tp) { |
212 |
+ |
len = alen = 0; |
213 |
+ |
nsp = sp; |
214 |
+ |
for (end = tp; *end; end = tab) { |
215 |
+ |
tab = end + 1; |
216 |
+ |
alen += *nsp++; |
217 |
+ |
if (f->fg[*end&0xff]) { |
218 |
+ |
while ((gp = f->fg[*tab&0xff]) != NULL && |
219 |
+ |
gp->nverts == 0) { /* tab in */ |
220 |
+ |
alen += *nsp++; |
221 |
+ |
tab++; |
222 |
+ |
} |
223 |
+ |
len += tab - end; |
224 |
+ |
} |
225 |
+ |
if (nsi && tab - end > nsi) |
226 |
+ |
break; |
227 |
+ |
} |
228 |
+ |
len *= f->mwidth + cis; /* compute target length */ |
229 |
+ |
width += len; |
230 |
+ |
len -= alen; /* necessary adjustment */ |
231 |
+ |
while (sp < nsp) { |
232 |
+ |
alen = len/(nsp-sp); |
233 |
+ |
*sp++ += alen; |
234 |
+ |
len -= alen; |
235 |
+ |
} |
236 |
+ |
tp = tab; |
237 |
+ |
} |
238 |
+ |
return(width); |
239 |
|
} |
156 |
– |
|
157 |
– |
|