1 |
/* Copyright (c) 1992 Regents of the University of California */ |
2 |
|
3 |
#ifndef lint |
4 |
static char SCCSid[] = "$SunId$ LBL"; |
5 |
#endif |
6 |
|
7 |
/* |
8 |
* Polygonal font handling routines |
9 |
*/ |
10 |
|
11 |
#include "standard.h" |
12 |
|
13 |
#include "font.h" |
14 |
|
15 |
#define galloc(nv) (GLYPH *)malloc(sizeof(GLYPH)+2*sizeof(GORD)*(nv)) |
16 |
|
17 |
|
18 |
extern char *libpath; /* list of library directories */ |
19 |
|
20 |
static FONT *fontlist = NULL; /* list of loaded fonts */ |
21 |
|
22 |
|
23 |
FONT * |
24 |
getfont(fname) /* return font fname */ |
25 |
char *fname; |
26 |
{ |
27 |
char buf[16]; |
28 |
FILE *fp; |
29 |
char *pathname, *err; |
30 |
int gn, ngv; |
31 |
register int gv; |
32 |
register GLYPH *g; |
33 |
GORD *gp; |
34 |
register FONT *f; |
35 |
|
36 |
for (f = fontlist; f != NULL; f = f->next) |
37 |
if (!strcmp(f->name, fname)) |
38 |
return(f); |
39 |
/* load the font file */ |
40 |
if ((pathname = getpath(fname, libpath, R_OK)) == NULL) { |
41 |
sprintf(errmsg, "cannot find font file \"%s\"", fname); |
42 |
error(USER, errmsg); |
43 |
} |
44 |
f = (FONT *)calloc(1, sizeof(FONT)); |
45 |
if (f == NULL) |
46 |
goto memerr; |
47 |
f->name = savestr(fname); |
48 |
if ((fp = fopen(pathname, "r")) == NULL) { |
49 |
sprintf(errmsg, "cannot open font file \"%s\"", |
50 |
pathname); |
51 |
error(SYSTEM, errmsg); |
52 |
} |
53 |
while (fgetword(buf,sizeof(buf),fp) != NULL) { /* get each glyph */ |
54 |
if (!isint(buf)) |
55 |
goto nonint; |
56 |
gn = atoi(buf); |
57 |
if (gn < 0 || gn > 255) { |
58 |
err = "illegal"; |
59 |
goto fonterr; |
60 |
} |
61 |
if (f->fg[gn] != NULL) { |
62 |
err = "duplicate"; |
63 |
goto fonterr; |
64 |
} |
65 |
if (fgetword(buf,sizeof(buf),fp) == NULL || !isint(buf) || |
66 |
(ngv = atoi(buf)) < 0 || ngv > 32000) { |
67 |
err = "bad # vertices for"; |
68 |
goto fonterr; |
69 |
} |
70 |
g = galloc(ngv); |
71 |
if (g == NULL) |
72 |
goto memerr; |
73 |
g->nverts = ngv; |
74 |
g->left = g->right = g->top = g->bottom = 128; |
75 |
ngv *= 2; |
76 |
gp = gvlist(g); |
77 |
while (ngv--) { |
78 |
if (fgetword(buf,sizeof(buf),fp) == NULL || |
79 |
!isint(buf) || |
80 |
(gv = atoi(buf)) < 0 || gv > 255) { |
81 |
err = "bad vertex for"; |
82 |
goto fonterr; |
83 |
} |
84 |
*gp++ = gv; |
85 |
if (ngv & 1) { /* follow x limits */ |
86 |
if (gv < g->left) |
87 |
g->left = gv; |
88 |
else if (gv > g->right) |
89 |
g->right = gv; |
90 |
} else { /* follow y limits */ |
91 |
if (gv < g->bottom) |
92 |
g->bottom = gv; |
93 |
else if (gv > g->top) |
94 |
g->top = gv; |
95 |
} |
96 |
} |
97 |
f->fg[gn] = g; |
98 |
} |
99 |
fclose(fp); |
100 |
f->next = fontlist; |
101 |
return(fontlist = f); |
102 |
nonint: |
103 |
sprintf(errmsg, "non-integer in font file \"%s\"", pathname); |
104 |
error(USER, errmsg); |
105 |
fonterr: |
106 |
sprintf(errmsg, "%s character (%d) in font file \"%s\"", |
107 |
err, gn, pathname); |
108 |
error(USER, errmsg); |
109 |
memerr: |
110 |
error(SYSTEM, "out of memory in fontglyph"); |
111 |
} |
112 |
|
113 |
|
114 |
int |
115 |
squeeztext(sp, tp, f, cis) /* squeeze text line */ |
116 |
short *sp; /* returned character spacing */ |
117 |
char *tp; /* text line */ |
118 |
FONT *f; /* font */ |
119 |
int cis; /* intercharacter spacing */ |
120 |
{ |
121 |
int linelen; |
122 |
register GLYPH *gp; |
123 |
|
124 |
gp = NULL; |
125 |
while (*tp && (gp = f->fg[*tp++&0xff]) == NULL) |
126 |
*sp++ = 0; |
127 |
cis /= 2; |
128 |
linelen = *sp = 0; |
129 |
while (gp != NULL) { |
130 |
if (gp->nverts) { /* regular character */ |
131 |
linelen += *sp++ += cis - gp->left; |
132 |
*sp = gp->right + cis; |
133 |
} else { /* space */ |
134 |
linelen += *sp++; |
135 |
*sp = 256; |
136 |
} |
137 |
gp = NULL; |
138 |
while (*tp && (gp = f->fg[*tp++&0xff]) == NULL) { |
139 |
linelen += *sp++; |
140 |
*sp = 0; |
141 |
} |
142 |
} |
143 |
linelen += *sp; |
144 |
return(linelen); |
145 |
} |
146 |
|
147 |
|
148 |
proptext(sp, tp, f, cis, nsi) /* space line proportionally */ |
149 |
short *sp; /* returned character spacing */ |
150 |
char *tp; /* text line */ |
151 |
FONT *f; /* font */ |
152 |
int cis; /* target intercharacter spacing */ |
153 |
int nsi; /* minimum number of spaces for indent */ |
154 |
{ |
155 |
} |
156 |
|
157 |
|