int _stringnwidth(Font *f, char *s, Rune *r, int len) { int wid, twid, n, max, l; char *name; enum { Max = 64 }; ushort cbuf[Max]; Rune rune, **rptr; char *subfontname, **sptr; Font *def; Subfont *sf; if(s == nil){ s = ""; sptr = nil; }else sptr = &s; if(r == nil){ r = L""; rptr = nil; }else rptr = &r; sf = nil; twid = 0; while(len>0 && (*s || *r)){ max = Max; if(len < max) max = len; n = 0; while((l = cachechars(f, sptr, rptr, cbuf, max, &wid, &subfontname)) <= 0){ if(++n > 10){ if(*r) rune = *r; else chartorune(&rune, s); if(f->name != nil) name = f->name; else name = "unnamed font"; fprint(2, "stringwidth: bad character set for rune 0x%.4ux in %s\n", rune, name); return twid; } if(subfontname){ freesubfont(sf); if((sf=_getsubfont(f->display, subfontname)) == 0){ def = f->display->defaultfont; if(def && f!=def) f = def; else break; } } } agefont(f); twid += wid; len -= l; } freesubfont(sf); return twid; }
void freefont(Font *f) { int i; Cachefont *c; Subfont *s; if(f == 0) return; for(i=0; i<f->nsub; i++){ c = f->sub[i]; free(c->subfontname); free(c->name); free(c); } for(i=0; i<f->nsubf; i++){ s = f->subf[i].f; /* if(s && s!=display->defaultsubfont)*/ /* Plan 9 uses this */ if(s) freesubfont(s); } freeimage(f->cacheimage); free(f->name); free(f->cache); free(f->subf); free(f->sub); free(f); }
void freefont(Font *f) { int i; Cachefont *c; Subfont *s; if(f == 0) return; for(i=0; i<f->nsub; i++){ c = f->sub[i]; free(c->subfontname); free(c->name); free(c); } for(i=0; i<f->nsubf; i++){ s = f->subf[i].f; if(s && (!display || s!=display->defaultsubfont)) freesubfont(s); } freeimage(f->cacheimage); free(f->name); free(f->namespec); free(f->cache); free(f->subf); free(f->sub); if(f->ondisplaylist) { f->ondisplaylist = 0; if(f->next) f->next->prev = f->prev; else f->display->lastfont = f->prev; if(f->prev) f->prev->next = f->next; else f->display->firstfont = f->next; } if(f->lodpi != f) freefont(f->lodpi); if(f->hidpi != f) freefont(f->hidpi); free(f); }
Point _string(Image *dst, Point pt, Image *src, Point sp, Font *f, char *s, Rune *r, int len, Rectangle clipr, Image *bg, Point bgp, Drawop op) { int m, n, wid, max; ushort cbuf[Max], *c, *ec; uchar *b; char *subfontname; char **sptr; Rune **rptr; Font *def; Subfont *sf; if(s == nil){ s = ""; sptr = nil; }else sptr = &s; if(r == nil){ r = (Rune*) L""; rptr = nil; }else rptr = &r; sf = nil; while((*s || *r) && len > 0){ max = Max; if(len < max) max = len; n = cachechars(f, sptr, rptr, cbuf, max, &wid, &subfontname); if(n > 0){ _setdrawop(dst->display, op); m = 47+2*n; if(bg) m += 4+2*4; b = bufimage(dst->display, m); if(b == 0){ fprint(2, "string: %r\n"); break; } if(bg) b[0] = 'x'; else b[0] = 's'; BPLONG(b+1, dst->id); BPLONG(b+5, src->id); BPLONG(b+9, f->cacheimage->id); BPLONG(b+13, pt.x); BPLONG(b+17, pt.y+f->ascent); BPLONG(b+21, clipr.min.x); BPLONG(b+25, clipr.min.y); BPLONG(b+29, clipr.max.x); BPLONG(b+33, clipr.max.y); BPLONG(b+37, sp.x); BPLONG(b+41, sp.y); BPSHORT(b+45, n); b += 47; if(bg){ BPLONG(b, bg->id); BPLONG(b+4, bgp.x); BPLONG(b+8, bgp.y); b += 12; } ec = &cbuf[n]; for(c=cbuf; c<ec; c++, b+=2) BPSHORT(b, *c); pt.x += wid; bgp.x += wid; agefont(f); len -= n; } if(subfontname){ freesubfont(sf); if((sf=_getsubfont(f->display, subfontname)) == 0){ def = f->display ? f->display->defaultfont : nil; if(def && f!=def) f = def; else break; } /* * must not free sf until cachechars has found it in the cache * and picked up its own reference. */ } } return pt; }