1 |
greg |
2.1 |
#ifndef lint |
2 |
greg |
2.24 |
static const char RCSid[] = "$Id: font.c,v 2.23 2021/11/20 15:53:24 greg Exp $"; |
3 |
greg |
2.1 |
#endif |
4 |
|
|
/* |
5 |
|
|
* Polygonal font handling routines |
6 |
|
|
*/ |
7 |
|
|
|
8 |
greg |
2.13 |
#include "copyright.h" |
9 |
greg |
2.12 |
|
10 |
schorsch |
2.18 |
#include <stdlib.h> |
11 |
|
|
|
12 |
greg |
2.19 |
#include "paths.h" |
13 |
schorsch |
2.17 |
#include "rtio.h" |
14 |
greg |
2.23 |
#include "rterror.h" |
15 |
greg |
2.1 |
#include "font.h" |
16 |
|
|
|
17 |
|
|
#define galloc(nv) (GLYPH *)malloc(sizeof(GLYPH)+2*sizeof(GORD)*(nv)) |
18 |
|
|
|
19 |
greg |
2.12 |
int retainfonts = 0; /* retain loaded fonts? */ |
20 |
greg |
2.1 |
|
21 |
|
|
static FONT *fontlist = NULL; /* list of loaded fonts */ |
22 |
|
|
|
23 |
|
|
|
24 |
|
|
FONT * |
25 |
greg |
2.21 |
getfont( /* return font fname */ |
26 |
|
|
char *fname |
27 |
|
|
) |
28 |
greg |
2.1 |
{ |
29 |
greg |
2.24 |
char embuf[512]; |
30 |
greg |
2.1 |
FILE *fp; |
31 |
schorsch |
2.17 |
char *pathname, *err = NULL; |
32 |
greg |
2.3 |
unsigned wsum, hsum, ngly; |
33 |
greg |
2.11 |
int gn, ngv, gv; |
34 |
greg |
2.21 |
GLYPH *g; |
35 |
greg |
2.1 |
GORD *gp; |
36 |
greg |
2.21 |
FONT *f; |
37 |
greg |
2.1 |
|
38 |
|
|
for (f = fontlist; f != NULL; f = f->next) |
39 |
greg |
2.12 |
if (!strcmp(f->name, fname)) { |
40 |
|
|
f->nref++; |
41 |
greg |
2.1 |
return(f); |
42 |
greg |
2.12 |
} |
43 |
greg |
2.1 |
/* load the font file */ |
44 |
greg |
2.14 |
if ((pathname = getpath(fname, getrlibpath(), R_OK)) == NULL) { |
45 |
greg |
2.24 |
sprintf(embuf, "cannot find font file \"%s\"\n", fname); |
46 |
|
|
eputs(embuf); |
47 |
greg |
2.22 |
return(NULL); |
48 |
|
|
} |
49 |
|
|
if ((fp = fopen(pathname, "r")) == NULL) { |
50 |
greg |
2.24 |
sprintf(embuf, "cannot open font file \"%s\"\n", pathname); |
51 |
|
|
eputs(embuf); |
52 |
greg |
2.22 |
return(NULL); |
53 |
greg |
2.1 |
} |
54 |
|
|
f = (FONT *)calloc(1, sizeof(FONT)); |
55 |
|
|
if (f == NULL) |
56 |
|
|
goto memerr; |
57 |
greg |
2.22 |
strcpy(f->name, fname); |
58 |
greg |
2.12 |
f->nref = 1; |
59 |
|
|
wsum = hsum = ngly = 0; /* get each glyph */ |
60 |
|
|
while ((ngv = fgetval(fp, 'i', (char *)&gn)) != EOF) { |
61 |
greg |
2.11 |
if (ngv == 0) |
62 |
greg |
2.1 |
goto nonint; |
63 |
greg |
2.3 |
if (gn < 1 || gn > 255) { |
64 |
greg |
2.1 |
err = "illegal"; |
65 |
|
|
goto fonterr; |
66 |
|
|
} |
67 |
|
|
if (f->fg[gn] != NULL) { |
68 |
|
|
err = "duplicate"; |
69 |
|
|
goto fonterr; |
70 |
|
|
} |
71 |
greg |
2.12 |
if (fgetval(fp, 'i', (char *)&ngv) <= 0 || |
72 |
|
|
ngv < 0 || ngv > 32000) { |
73 |
greg |
2.1 |
err = "bad # vertices for"; |
74 |
|
|
goto fonterr; |
75 |
|
|
} |
76 |
|
|
g = galloc(ngv); |
77 |
|
|
if (g == NULL) |
78 |
|
|
goto memerr; |
79 |
|
|
g->nverts = ngv; |
80 |
greg |
2.2 |
g->left = g->right = g->top = g->bottom = 128; |
81 |
greg |
2.1 |
ngv *= 2; |
82 |
|
|
gp = gvlist(g); |
83 |
|
|
while (ngv--) { |
84 |
greg |
2.12 |
if (fgetval(fp, 'i', (char *)&gv) <= 0 || |
85 |
|
|
gv < 0 || gv > 255) { |
86 |
greg |
2.1 |
err = "bad vertex for"; |
87 |
|
|
goto fonterr; |
88 |
|
|
} |
89 |
|
|
*gp++ = gv; |
90 |
greg |
2.2 |
if (ngv & 1) { /* follow x limits */ |
91 |
|
|
if (gv < g->left) |
92 |
|
|
g->left = gv; |
93 |
|
|
else if (gv > g->right) |
94 |
|
|
g->right = gv; |
95 |
|
|
} else { /* follow y limits */ |
96 |
|
|
if (gv < g->bottom) |
97 |
|
|
g->bottom = gv; |
98 |
|
|
else if (gv > g->top) |
99 |
|
|
g->top = gv; |
100 |
|
|
} |
101 |
greg |
2.1 |
} |
102 |
greg |
2.3 |
if (g->right - g->left && g->top - g->bottom) { |
103 |
|
|
ngly++; |
104 |
|
|
wsum += g->right - g->left; |
105 |
|
|
hsum += g->top - g->bottom; |
106 |
|
|
} |
107 |
greg |
2.1 |
f->fg[gn] = g; |
108 |
|
|
} |
109 |
|
|
fclose(fp); |
110 |
greg |
2.3 |
if (ngly) { |
111 |
|
|
f->mwidth = wsum / ngly; |
112 |
|
|
f->mheight = hsum / ngly; |
113 |
|
|
} |
114 |
greg |
2.1 |
f->next = fontlist; |
115 |
|
|
return(fontlist = f); |
116 |
|
|
nonint: |
117 |
greg |
2.24 |
sprintf(embuf, "non-integer in font file \"%s\"\n", pathname); |
118 |
|
|
eputs(embuf); |
119 |
greg |
2.22 |
fclose(fp); |
120 |
|
|
return(NULL); |
121 |
greg |
2.1 |
fonterr: |
122 |
greg |
2.24 |
sprintf(embuf, "%s character (%d) in font file \"%s\"\n", |
123 |
greg |
2.1 |
err, gn, pathname); |
124 |
greg |
2.24 |
eputs(embuf); |
125 |
greg |
2.22 |
fclose(fp); |
126 |
|
|
return(NULL); |
127 |
greg |
2.1 |
memerr: |
128 |
greg |
2.23 |
eputs("out of memory in getfont()\n"); |
129 |
greg |
2.22 |
fclose(fp); |
130 |
|
|
return(NULL); |
131 |
greg |
2.1 |
} |
132 |
greg |
2.2 |
|
133 |
|
|
|
134 |
greg |
2.12 |
void |
135 |
greg |
2.21 |
freefont( /* release a font (free all if NULL) */ |
136 |
|
|
FONT *fnt |
137 |
|
|
) |
138 |
greg |
2.6 |
{ |
139 |
|
|
FONT head; |
140 |
greg |
2.21 |
FONT *fl, *f; |
141 |
|
|
int i; |
142 |
greg |
2.12 |
/* check reference count */ |
143 |
schorsch |
2.16 |
if (fnt != NULL && ((fnt->nref-- > 1) | retainfonts)) |
144 |
greg |
2.12 |
return; |
145 |
greg |
2.6 |
head.next = fontlist; |
146 |
|
|
fl = &head; |
147 |
|
|
while ((f = fl->next) != NULL) |
148 |
schorsch |
2.16 |
if ((fnt == NULL) | (fnt == f)) { |
149 |
greg |
2.6 |
fl->next = f->next; |
150 |
|
|
for (i = 0; i < 256; i++) |
151 |
|
|
if (f->fg[i] != NULL) |
152 |
greg |
2.12 |
free((void *)f->fg[i]); |
153 |
|
|
free((void *)f); |
154 |
greg |
2.6 |
} else |
155 |
|
|
fl = f; |
156 |
|
|
fontlist = head.next; |
157 |
|
|
} |
158 |
|
|
|
159 |
|
|
|
160 |
greg |
2.2 |
int |
161 |
greg |
2.21 |
uniftext( /* uniformly space text line */ |
162 |
|
|
short *sp, /* returned character spacing */ |
163 |
|
|
char *tp, /* text line */ |
164 |
|
|
FONT *f /* font */ |
165 |
|
|
) |
166 |
greg |
2.3 |
{ |
167 |
|
|
int linelen; |
168 |
|
|
|
169 |
|
|
linelen = *sp++ = 0; |
170 |
|
|
while (*tp) |
171 |
|
|
if (f->fg[*tp++&0xff] == NULL) |
172 |
|
|
*sp++ = 0; |
173 |
|
|
else |
174 |
greg |
2.10 |
linelen += *sp++ = 255; |
175 |
greg |
2.3 |
return(linelen); |
176 |
|
|
} |
177 |
|
|
|
178 |
|
|
|
179 |
|
|
int |
180 |
greg |
2.21 |
squeeztext( /* squeeze text line */ |
181 |
|
|
short *sp, /* returned character spacing */ |
182 |
|
|
char *tp, /* text line */ |
183 |
|
|
FONT *f, /* font */ |
184 |
|
|
int cis /* intercharacter spacing */ |
185 |
|
|
) |
186 |
greg |
2.2 |
{ |
187 |
|
|
int linelen; |
188 |
greg |
2.21 |
GLYPH *gp; |
189 |
greg |
2.2 |
|
190 |
greg |
2.8 |
linelen = 0; |
191 |
greg |
2.2 |
gp = NULL; |
192 |
|
|
while (*tp && (gp = f->fg[*tp++&0xff]) == NULL) |
193 |
|
|
*sp++ = 0; |
194 |
|
|
cis /= 2; |
195 |
greg |
2.8 |
*sp = cis; |
196 |
greg |
2.2 |
while (gp != NULL) { |
197 |
|
|
if (gp->nverts) { /* regular character */ |
198 |
|
|
linelen += *sp++ += cis - gp->left; |
199 |
|
|
*sp = gp->right + cis; |
200 |
|
|
} else { /* space */ |
201 |
|
|
linelen += *sp++; |
202 |
greg |
2.3 |
*sp = f->mwidth; |
203 |
greg |
2.2 |
} |
204 |
|
|
gp = NULL; |
205 |
|
|
while (*tp && (gp = f->fg[*tp++&0xff]) == NULL) { |
206 |
|
|
linelen += *sp++; |
207 |
|
|
*sp = 0; |
208 |
|
|
} |
209 |
|
|
} |
210 |
greg |
2.3 |
linelen += *sp += cis; |
211 |
greg |
2.2 |
return(linelen); |
212 |
|
|
} |
213 |
|
|
|
214 |
|
|
|
215 |
greg |
2.3 |
int |
216 |
greg |
2.21 |
proptext( /* space line proportionally */ |
217 |
|
|
short *sp, /* returned character spacing */ |
218 |
|
|
char *tp, /* text line */ |
219 |
|
|
FONT *f, /* font */ |
220 |
|
|
int cis, /* target intercharacter spacing */ |
221 |
|
|
int nsi /* minimum number of spaces for indent */ |
222 |
|
|
) |
223 |
greg |
2.2 |
{ |
224 |
greg |
2.21 |
char *end, *tab = NULL; |
225 |
greg |
2.3 |
GLYPH *gp; |
226 |
|
|
short *nsp; |
227 |
|
|
int alen, len, width; |
228 |
|
|
/* start by squeezing it */ |
229 |
|
|
squeeztext(sp, tp, f, cis); |
230 |
|
|
/* now, realign spacing */ |
231 |
greg |
2.7 |
width = *sp++; |
232 |
greg |
2.3 |
while (*tp) { |
233 |
greg |
2.7 |
len = alen = 0; |
234 |
greg |
2.3 |
nsp = sp; |
235 |
|
|
for (end = tp; *end; end = tab) { |
236 |
|
|
tab = end + 1; |
237 |
|
|
alen += *nsp++; |
238 |
|
|
if (f->fg[*end&0xff]) { |
239 |
|
|
while ((gp = f->fg[*tab&0xff]) != NULL && |
240 |
|
|
gp->nverts == 0) { /* tab in */ |
241 |
|
|
alen += *nsp++; |
242 |
|
|
tab++; |
243 |
|
|
} |
244 |
|
|
len += tab - end; |
245 |
|
|
} |
246 |
greg |
2.4 |
if (nsi && tab - end > nsi) |
247 |
greg |
2.3 |
break; |
248 |
|
|
} |
249 |
|
|
len *= f->mwidth + cis; /* compute target length */ |
250 |
|
|
width += len; |
251 |
|
|
len -= alen; /* necessary adjustment */ |
252 |
|
|
while (sp < nsp) { |
253 |
|
|
alen = len/(nsp-sp); |
254 |
|
|
*sp++ += alen; |
255 |
|
|
len -= alen; |
256 |
|
|
} |
257 |
|
|
tp = tab; |
258 |
|
|
} |
259 |
|
|
return(width); |
260 |
greg |
2.2 |
} |