1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
|
#include <u.h>
#include <libc.h>
#include <draw.h>
#include <event.h>
#include <bio.h>
#include "proof.h"
char fname[NFONT][20]; /* font names */
char lastload[NFONT][20]; /* last file name prefix loaded for this font */
Font *fonttab[NFONT][NSIZE]; /* pointers to fonts */
int fmap[NFONT]; /* what map to use with this font */
static void loadfont(int, int);
static void fontlookup(int, char *);
static void buildxheight(Biobuf*);
static void buildmap(Biobuf*);
static void buildtroff(char *);
static void addmap(int, char *, int);
static char *map(Rune*, int);
static void scanstr(char *, char *, char **);
int specfont; /* somehow, number of special font */
#define NMAP 5
#define QUICK 2048 /* char values less than this are quick to look up */
#define eq(s,t) strcmp((char *) s, (char *) t) == 0
int curmap = -1; /* what map are we working on */
typedef struct Link Link;
struct Link /* link names together */
{
uchar *name;
int val;
Link *next;
};
typedef struct Map Map;
struct Map /* holds a mapping from uchar name to index */
{
double xheight;
Rune quick[QUICK]; /* low values get special treatment */
Link *slow; /* other stuff goes into a link list */
};
Map charmap[5];
typedef struct Fontmap Fontmap;
struct Fontmap /* mapping from troff name to filename */
{
char *troffname;
char *prefix;
int map; /* which charmap to use for this font */
char *fallback; /* font to look in if can't find char here */
};
Fontmap fontmap[100];
int pos2fontmap[NFONT]; /* indexed by troff font position, gives Fontmap */
int nfontmap = 0; /* how many are there */
void
dochar(Rune r[])
{
char *s, *fb;
Font *f;
Point p;
int fontno, fm, i;
char buf[10];
fontno = curfont;
if((s = map(r, curfont)) == 0){ /* not on current font */
if ((s = map(r, specfont)) != 0) /* on special font */
fontno = specfont;
else{
/* look for fallback */
fm = pos2fontmap[curfont];
fb = fontmap[fm].fallback;
if(fb){
/* see if fallback is mounted */
for(i = 0; i < NFONT; i++){
if(eq(fb, fontmap[pos2fontmap[i]].troffname)){
s = map(r, i);
if(s){
fontno = i;
goto found;
}
}
}
}
/* no such char; use name itself on defont */
/* this is not a general solution */
p.x = hpos/DIV + xyoffset.x + offset.x;
p.y = vpos/DIV + xyoffset.y + offset.y;
p.y -= font->ascent;
sprint(buf, "%S", r);
string(screen, p, display->black, ZP, font, buf);
return;
}
}
found:
p.x = hpos/DIV + xyoffset.x + offset.x;
p.y = vpos/DIV + xyoffset.y + offset.y;
while ((f = fonttab[fontno][cursize]) == 0)
loadfont(fontno, cursize);
p.y -= f->ascent;
dprint(2, "putting %S at %d,%d font %d, size %d\n", r, p.x, p.y, fontno, cursize);
string(screen, p, display->black, ZP, f, s);
}
static int drawlog2[] = {
0, 0,
1, 1,
2, 2, 2, 2,
3, 3, 3, 3, 3, 3, 3, 3,
4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
5
};
static void
loadfont(int n, int s)
{
char file[100];
int i, fd, t, deep;
static char *try[3] = {"", "times/R.", "pelm/"};
Subfont *f;
Font *ff;
try[0] = fname[n];
dprint(2, "loadfont %d %d\n", n, s);
for (t = 0; t < 3; t++){
i = s * mag * charmap[fmap[n]].xheight/0.72; /* a pixel is 0.72 points */
if (i < MINSIZE)
i = MINSIZE;
dprint(2, "size %d, i %d, mag %g\n", s, i, mag);
for(; i >= MINSIZE; i--){
/* if .font file exists, take that */
sprint(file, "%s/%s%d.font", libfont, try[t], i);
ff = openfont(display, file);
if(ff != 0){
fonttab[n][s] = ff;
dprint(2, "using %s for font %d %d\n", file, n, s);
return;
}
/* else look for a subfont file */
for (deep = drawlog2[screen->depth]; deep >= 0; deep--){
sprint(file, "%s/%s%d.%d", libfont, try[t], i, deep);
dprint(2, "trying %s for %d\n", file, i);
if ((fd = open(file, 0)) >= 0){
f = readsubfont(display, file, fd, 0);
if (f == 0) {
fprint(2, "can't rdsubfontfile %s: %r\n", file);
exits("rdsubfont");
}
close(fd);
ff = mkfont(f, 0);
if(ff == 0){
fprint(2, "can't mkfont %s: %r\n", file);
exits("rdsubfont");
}
fonttab[n][s] = ff;
dprint(2, "using %s for font %d %d\n", file, n, s);
return;
}
}
}
}
fprint(2, "can't find font %s.%d or substitute, quitting\n", fname[n], s);
exits("no font");
}
void
loadfontname(int n, char *s)
{
int i;
Font *f, *g = 0;
if (strcmp(s, fname[n]) == 0)
return;
if(fname[n] && fname[n][0]){
if(lastload[n] && strcmp(lastload[n], fname[n]) == 0)
return;
strcpy(lastload[n], fname[n]);
}
fontlookup(n, s);
for (i = 0; i < NSIZE; i++)
if (f = fonttab[n][i]){
if (f != g) {
freefont(f);
g = f;
}
fonttab[n][i] = 0;
}
}
void
allfree(void)
{
int i;
for (i=0; i<NFONT; i++)
loadfontname(i, "??");
}
void
readmapfile(char *file)
{
Biobuf *fp;
char *p, cmd[100];
if ((fp=Bopen(file, OREAD)) == 0){
fprint(2, "proof: can't open map file %s\n", file);
exits("urk");
}
while((p=Brdline(fp, '\n')) != 0) {
p[Blinelen(fp)-1] = 0;
scanstr(p, cmd, 0);
if(p[0]=='\0' || eq(cmd, "#")) /* skip comments, empty */
continue;
else if(eq(cmd, "xheight"))
buildxheight(fp);
else if(eq(cmd, "map"))
buildmap(fp);
else if(eq(cmd, "special"))
buildtroff(p);
else if(eq(cmd, "troff"))
buildtroff(p);
else
fprint(2, "weird map line %s\n", p);
}
Bterm(fp);
}
static void
buildxheight(Biobuf *fp) /* map goes from char name to value to print via *string() */
{
char *line;
line = Brdline(fp, '\n');
if(line == 0){
fprint(2, "proof: bad map file\n");
exits("map");
}
charmap[curmap].xheight = atof(line);
}
static void
buildmap(Biobuf *fp) /* map goes from char name to value to print via *string() */
{
uchar *p, *line, ch[100];
int val;
Rune r;
curmap++;
if(curmap >= NMAP){
fprint(2, "proof: out of char maps; recompile\n");
exits("charmap");
}
while ((line = Brdline(fp, '\n'))!= 0){
if (line[0] == '\n')
return;
line[Blinelen(fp)-1] = 0;
scanstr((char *) line, (char *) ch, (char **)(void*)&p);
if (ch[0] == '\0') {
fprint(2, "bad map file line '%s'\n", (char*)line);
continue;
}
val = strtol((char *) p, 0, 10);
dprint(2, "buildmap %s (%x %x) %s %d\n", (char*)ch, ch[0], ch[1], (char*)p, val);
chartorune(&r, (char*)ch);
if(utflen((char*)ch)==1 && r<QUICK)
charmap[curmap].quick[r] = val;
else
addmap(curmap, strdup((char *) ch), val); /* put somewhere else */
}
}
static void
addmap(int n, char *s, int val) /* stick a new link on */
{
Link *p = (Link *) malloc(sizeof(Link));
Link *prev = charmap[n].slow;
if(p == 0)
exits("out of memory in addmap");
p->name = (uchar *) s;
p->val = val;
p->next = prev;
charmap[n].slow = p;
}
static void
buildtroff(char *buf) /* map troff names into bitmap filenames */
{ /* e.g., R -> times/R., I -> times/I., etc. */
char *p, cmd[100], name[200], prefix[400], fallback[100];
scanstr(buf, cmd, &p);
scanstr(p, name, &p);
scanstr(p, prefix, &p);
while(*p!=0 && isspace(*p))
p++;
if(*p != 0){
scanstr(p, fallback, &p);
fontmap[nfontmap].fallback = strdup(fallback);
}else
fontmap[nfontmap].fallback = 0;
fontmap[nfontmap].troffname = strdup(name);
fontmap[nfontmap].prefix = strdup(prefix);
fontmap[nfontmap].map = curmap;
dprint(2, "troff name %s is bitmap %s map %d in slot %d fallback %s\n", name, prefix, curmap, nfontmap, fontmap[nfontmap].fallback? fontmap[nfontmap].fallback : "<null>");
nfontmap++;
}
static void
fontlookup(int n, char *s) /* map troff name of s into position n */
{
int i;
for(i = 0; i < nfontmap; i++)
if (eq(s, fontmap[i].troffname)) {
strcpy(fname[n], fontmap[i].prefix);
fmap[n] = fontmap[i].map;
pos2fontmap[n] = i;
if (eq(s, "S"))
specfont = n;
dprint(2, "font %d %s is %s\n", n, s, fname[n]);
return;
}
/* god help us if this font isn't there */
}
static char *
map(Rune rp[], int font) /* figure out mapping for char in this font */
{
static char s[100];
char c[10];
Link *p;
Rune r;
if(rp[1]==0 && rp[0]<QUICK) /* fast lookup */
r = charmap[fmap[font]].quick[rp[0]];
else { /* high-valued or compound character name */
sprint(c, "%S", rp);
r = 0;
for (p = charmap[fmap[font]].slow; p; p = p->next)
if(eq(c, p->name)){
r = p->val;
break;
}
}
if(r == 0){ /* not there */
dprint(2, "didn't find %S font# %d\n", rp, font);
return 0;
}
dprint(2, "map %S to %s font# %d\n", rp, s, font);
s[runetochar(s, &r)] = 0;
return s;
}
static void
scanstr(char *s, char *ans, char **ep)
{
for (; isspace((uchar) *s); s++)
;
for (; *s!=0 && !isspace((uchar) *s); )
*ans++ = *s++;
*ans = 0;
if (ep)
*ep = s;
}
|