plan9port

fork of plan9port with libvec, libstr and libsdb
Log | Files | Refs | README | LICENSE

commit 79555a9987d62cd15b77df2b4328e963583a160e
parent 213fc4f6fb26bb5781ea3e489bf4cc5c2aca591e
Author: Russ Cox <rsc@swtch.com>
Date:   Tue, 17 Feb 2015 12:39:36 -0500

libdraw: refine hidpi font selection

Change-Id: Id1e6a2630713024a1925ad1341bb9c846f82e93e
Reviewed-on: https://plan9port-review.googlesource.com/1171
Reviewed-by: Russ Cox <rsc@swtch.com>

Diffstat:
Minclude/draw.h | 1+
Msrc/libdraw/buildfont.c | 2++
Msrc/libdraw/mkfont.c | 1+
Msrc/libdraw/openfont.c | 41++++++++++++++++++++++++++++++++++++++++-
4 files changed, 44 insertions(+), 1 deletion(-)

diff --git a/include/draw.h b/include/draw.h @@ -308,6 +308,7 @@ struct Cachesubf struct Font { char *name; + char *namespec; Display *display; short height; /* max height of image, interline spacing */ short ascent; /* top of image to baseline */ diff --git a/src/libdraw/buildfont.c b/src/libdraw/buildfont.c @@ -28,6 +28,7 @@ buildfont(Display *d, char *buf, char *name) fnt->scale = 1; fnt->display = d; fnt->name = strdup(name); + fnt->namespec = strdup(name); fnt->ncache = NFCACHE+NFLOOK; fnt->nsubf = NFSUBF; fnt->cache = malloc(fnt->ncache * sizeof(fnt->cache[0])); @@ -135,6 +136,7 @@ freefont(Font *f) } freeimage(f->cacheimage); free(f->name); + free(f->namespec); free(f->cache); free(f->subf); free(f->sub); diff --git a/src/libdraw/mkfont.c b/src/libdraw/mkfont.c @@ -18,6 +18,7 @@ mkfont(Subfont *subfont, Rune min) font->scale = 1; font->display = subfont->bits->display; font->name = strdup("<synthetic>"); + font->namespec = strdup("<synthetic>"); font->ncache = NFCACHE+NFLOOK; font->nsubf = NFSUBF; font->cache = malloc(font->ncache * sizeof(font->cache[0])); diff --git a/src/libdraw/openfont.c b/src/libdraw/openfont.c @@ -156,6 +156,34 @@ swapfont(Font *targ, Font **oldp, Font **newp) *newp = old; } +static char* +hidpiname(Font *f) +{ + char *p, *q; + int size; + + // If font name has form x,y return y. + p = strchr(f->namespec, ','); + if(p != nil) + return strdup(p+1); + + // If font name is /mnt/font/Name/Size/font, scale Size. + if(strncmp(f->name, "/mnt/font/", 10) == 0) { + p = strchr(f->name+10, '/'); + if(p == nil || *++p < '0' || *p > '9') + goto scale; + q = p; + size = 0; + while('0' <= *q && *q <= '9') + size = size*10 + *q++ - '0'; + return smprint("%.*s%d%s", utfnlen(f->name, p-f->name), f->name, size*2, q); + } + + // Otherwise use pixel doubling. +scale: + return smprint("%d*%s", f->scale*2, f->name); +} + void loadhidpi(Font *f) { @@ -169,7 +197,7 @@ loadhidpi(Font *f) return; } - name = smprint("%d*%s", f->scale*2, f->name); + name = hidpiname(f); fnew = openfont1(f->display, name); if(fnew == nil) return; @@ -183,9 +211,18 @@ Font* openfont(Display *d, char *name) { Font *f; + char *p; + char *namespec; + // If font name has form x,y use x for lodpi, y for hidpi. + name = strdup(name); + namespec = strdup(name); + if((p = strchr(name, ',')) != nil) + *p = '\0'; + f = openfont1(d, name); f->lodpi = f; + f->namespec = namespec; /* add to display list for when dpi changes */ /* d can be nil when invoked from mc. */ @@ -203,6 +240,8 @@ openfont(Display *d, char *name) if(d->dpi >= DefaultDPI*3/2) loadhidpi(f); } + + free(name); return f; }