libdraw: refine hidpi font selection

Change-Id: Id1e6a2630713024a1925ad1341bb9c846f82e93e
Reviewed-on: https://plan9port-review.googlesource.com/1171
Reviewed-by: Russ Cox <rsc@swtch.com>
This commit is contained in:
Russ Cox 2015-02-17 12:39:36 -05:00
parent 213fc4f6fb
commit 79555a9987
4 changed files with 44 additions and 1 deletions

View file

@ -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 */

View file

@ -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);

View file

@ -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]));

View file

@ -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;
}