9 static char Egreg[] = "my memory of truetype is fading";
10 static char Enoent[] = "not found";
12 enum { MAXSUB = 0x100 };
14 typedef struct TFont TFont;
15 typedef struct TSubfont TSubfont;
40 typedef struct FidAux FidAux;
53 TFont fontl = {.next = &fontl, .prev = &fontl};
61 if(v == nil) sysfatal("malloc: %r");
62 setmalloctag(v, getcallerpc(&n));
77 r->ofcall.qid = (Qid){0, 0, QTDIR};
78 r->fid->qid = r->ofcall.qid;
79 r->fid->aux = emalloc(sizeof(FidAux));
95 fmtprint(&fmt, "%d\t%d\n", f->ttf->ascentpx + f->ttf->descentpx, f->ttf->ascentpx);
97 for(c = u->cmap; c < u->cmap + u->ncmap; c++){
98 for(k = c->start; k < c->end; k += MAXSUB){
99 s = emalloc(sizeof(TSubfont));
101 if(c->start == 0) got0 = 1;
102 s->end = k + MAXSUB - 1;
106 s->qid = (Qid){qidgen(), 0, 0};
107 s->next = f->sub[c->start >> 8 & 0xff];
108 f->sub[c->start >> 8 & 0xff] = s;
109 fmtprint(&fmt, "%#.4ux\t%#.4ux\ts.%.4ux-%.4ux\n", s->start, s->end, s->start, s->end);
113 s = emalloc(sizeof(TSubfont));
117 s->qid = (Qid){qidgen(), 0, 0};
120 fmtprint(&fmt, "%#.4ux\t%#.4ux\ts.%.4ux-%.4ux\n", 0, 0, 0, 0);
122 f->fontfile = fmtstrflush(&fmt);
123 f->nfontfile = strlen(f->fontfile);
127 blit(uchar *t, int x, int y, int tstride, uchar *s, int w, int h)
136 for(sy = 0; sy < h; sy++, ty++){
138 tp = t + ty * tstride + (tx >> 3);
140 for(sx = 0; sx < w; sx += 8){
141 b |= *sp++ << 8 - (tx & 7);
150 compilesub(TFont *f, TSubfont *s)
152 int n, i, w, x, h, g, sz;
158 n = s->end - s->start + 1;
159 gs = emalloc9p(sizeof(TTGlyph *) * n);
161 h = t->ascentpx + t->descentpx;
162 for(i = 0; i < n; i++){
163 if(s->start + i == 0)
166 g = ttffindchar(t, s->start + i);
167 gs[i] = ttfgetglyph(t, g, 1);
170 sz = 5 * 12 + (w+7>>3) * h + 3 * 12 + (n + 1) * 6;
172 p = d + sprint(d, "%11s %11d %11d %11d %11d ", "k1", 0, 0, w, h);
174 for(i = 0; i < n; i++){
175 blit((uchar*)p, x, t->ascentpx - gs[i]->ymaxpx, w+7>>3, gs[i]->bit, gs[i]->width, gs[i]->height);
179 p += sprint(p, "%11d %11d %11d ", n, h, t->ascentpx);
181 for(i = 0; i < n; i++){
186 *p++ = gs[i]->xminpx;
187 *p++ = gs[i]->advanceWidthpx;
194 for(i = 0; i < n; i++)
208 for(f = fontl.next; f != &fontl; f = f->next)
209 if(strcmp(f->name9p, name) == 0)
211 d = strrchr(name, '.');
214 werrstr("invalid file name");
217 sz = strtol(d + 1, &p, 10);
218 if(d[1] == 0 || *p != 0)
220 buf = estrdup9p(name);
222 ttf = ttfopen(buf, sz, 0);
227 f = emalloc(sizeof(TFont));
229 f->name9p = strdup(name);
232 f->qid = (Qid){qidgen(), 0, QTDIR};
233 f->fileqid = (Qid){qidgen(), 0, 0};
235 f->prev = fontl.prev;
243 fsclone(Fid *old, Fid *new)
245 new->aux = emalloc(sizeof(FidAux));
246 *(FidAux*)new->aux = *(FidAux*)old->aux;
261 findsubfont(TFont *f, char *name)
268 if(name[0] != 's' || name[1] != '.' || name[2] == '-')
270 a = strtol(name + 2, &p, 16);
273 b = strtol(p + 1, &q, 16);
274 if(p + 1 == q || *q != 0)
276 snprint(buf, nelem(buf), "s.%.4ux-%.4ux", a, b);
277 if(strcmp(buf, name) != 0)
279 for(s = f->sub[a>>8&0xff]; s != nil; s = s->next)
280 if(s->start == a && s->end == b)
286 fswalk(Fid *fid, char *name, Qid *qid)
288 static char errbuf[ERRMAX];
295 fa->f = tryfont(name);
297 rerrstr(errbuf, nelem(errbuf));
302 fid->qid = fa->f->qid;
306 if(strcmp(name, "font") == 0){
308 fid->qid = fa->f->fileqid;
309 *qid = fa->f->fileqid;
312 fa->sub = findsubfont(fa->f, name);
316 fid->qid = fa->sub->qid;
331 r->d.uid = estrdup9p(getuser());
332 r->d.gid = estrdup9p(getuser());
333 r->d.muid = estrdup9p(getuser());
334 r->d.mtime = r->d.atime = time(0);
335 r->d.qid = r->fid->qid;
339 r->d.name = estrdup9p("/");
344 r->d.name = estrdup9p(fa->f->name9p);
349 r->d.name = estrdup9p("font");
350 r->d.length = fa->f->nfontfile;
355 r->d.name = smprint("s.%.4ux-%.4ux", fa->sub->start, fa->sub->end);
356 r->d.length = fa->sub->ndata;
365 fontdirread(int n, Dir *d, void *aux)
371 d->name = estrdup9p("font");
372 d->uid = estrdup9p(getuser());
373 d->gid = estrdup9p(getuser());
374 d->muid = estrdup9p(getuser());
376 d->qid = fa->f->fileqid;
377 d->mtime = d->atime = time(0);
378 d->length = fa->f->nfontfile;
396 dirread9p(r, fontdirread, fa);
400 readbuf(r, fa->f->fontfile, fa->f->nfontfile);
404 if(fa->sub->data == nil)
405 compilesub(fa->f, fa->sub);
406 readbuf(r, fa->sub->data, fa->sub->ndata);
420 .destroyfid = fsdestroyfid,
424 main(int argc, char **argv)
431 unmount(nil, "/n/ttf");
432 postmountsrv(&fssrv, nil, "/n/ttf", 0);