libdraw: refine hidpi font selection

Change-Id: Id1e6a2630713024a1925ad1341bb9c846f82e93e
Reviewed-on: https://plan9port-review.googlesource.com/1171
Reviewed-by: Russ Cox <rsc@swtch.com>
diff --git a/include/draw.h b/include/draw.h
index 329108d..2bc9b4b 100644
--- a/include/draw.h
+++ b/include/draw.h
@@ -308,6 +308,7 @@
 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
index 512bc9d..d3f2e69 100644
--- a/src/libdraw/buildfont.c
+++ b/src/libdraw/buildfont.c
@@ -28,6 +28,7 @@
 	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 @@
 	}
 	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
index cb8ab22..a508b98 100644
--- a/src/libdraw/mkfont.c
+++ b/src/libdraw/mkfont.c
@@ -18,6 +18,7 @@
 	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
index 97102a2..f798983 100644
--- a/src/libdraw/openfont.c
+++ b/src/libdraw/openfont.c
@@ -156,6 +156,34 @@
 	*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 @@
 		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 @@
 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 @@
 		if(d->dpi >= DefaultDPI*3/2)
 			loadhidpi(f);
 	}
+	
+	free(name);
 
 	return f;
 }