shithub: puzzles

Download patch

ref: cef5fe0310139ee6d16db580e30cb0bc5657c54b
parent: 3a71a567e26efd18a100f0c000c5173f38c99df2
author: sirjofri <sirjofri@sirjofri.de>
date: Fri May 24 11:05:46 EDT 2024

fixes line thickness issues, adds different font types

--- a/plan9.c
+++ b/plan9.c
@@ -45,6 +45,13 @@
 	va_end(arg);
 }
 
+typedef struct Ft Ft;
+struct Ft {
+	int type;
+	int size;
+	Font *font;
+};
+
 struct frontend {
 	midend *me;
 	Image *background;
@@ -57,6 +64,8 @@
 	Channel *settingschan;
 	int showframe;
 	int timeractive;
+	Ft *fonts;
+	int nfonts;
 };
 
 struct blitter {
@@ -74,6 +83,49 @@
 	SETTINGS = 1,
 };
 
+Font*
+findfontfile(int type, int size)
+{
+	Font *f;
+	char buf[128];
+	char *fixed = "/lib/font/bit/lucidasans/typeunicode.%d.font";
+	char *variable = "/lib/font/bit/lucidasans/unicode.%d.font";
+	
+	/* find font based on size */
+	while (size > 0) {
+		snprint(buf, sizeof(buf), type == FONT_FIXED ? fixed : variable, size);
+		if (access(buf, OREAD)) {
+			f = openfont(display, buf);
+			if (f == nil) {
+				Log("error opening font file %s\n", buf);
+				return font;
+			}
+			return f;
+		}
+	}
+	Log("Unable to find proper font. Falling back to default font\n");
+	return font;
+}
+
+Font*
+findfont(frontend *fe, int type, int size)
+{
+	Ft *n;
+	char buf[128];
+	for (int i = 0; i < fe->nfonts; i++) {
+		if (fe->fonts[i].type == type && fe->fonts[i].size == size)
+			return fe->fonts[i].font;
+	}
+	fe->nfonts++;
+	fe->fonts = realloc(fe->fonts, sizeof(Ft) * fe->nfonts);
+	assert(fe->fonts);
+	n = &fe->fonts[fe->nfonts-1];
+	n->type = type;
+	n->size = size;
+	n->font = findfontfile(type, size);
+	return n->font;
+}
+
 void
 frontend_default_colour(frontend *, float *output)
 {
@@ -139,8 +191,31 @@
 static void p9_draw_text(void *handle, int x, int y, int fonttype, int fontsize, int align, int color, const char *text)
 {
 	// todo: align, fontsize, fonttype
+	Font *f;
+	Point p, size;
 	frontend *fe = (frontend*)handle;
-	string(screen, addpt(Pt(x, y), fe->ZP), fe->colors[color], ZP, font, text);
+	Rectangle cl;
+	
+	f = findfont(fe, fonttype, fontsize);
+	
+	p.x = x;
+	p.y = y;
+	size = stringsize(f, text);
+	
+	if (align & ALIGN_VCENTRE)
+		p.y -= size.y / 2;
+	else
+		p.y -= size.y;
+	
+	if (align & ALIGN_HCENTRE)
+		p.x -= size.x / 2;
+	else if (align & ALIGN_HRIGHT)
+		p.x -= size.x;
+	
+	cl = screen->clipr;
+	screen->clipr = screen->r;
+	string(screen, addpt(p, fe->ZP), fe->colors[color], ZP, f, text);
+	screen->clipr = cl;
 }
 
 static void
@@ -176,10 +251,10 @@
 		points[i].y = coords[i*2+1] + fe->ZP.y;
 	}
 	
-	if (fillcolor > 0)
+	if (fillcolor >= 0)
 		fillpoly(screen, points, npoints, 0, fe->colors[fillcolor], ZP);
-	if (outlinecolor > 0)
-		poly(screen, points, npoints, Endsquare, Endsquare, 1, fe->colors[outlinecolor], ZP);
+	if (outlinecolor >= 0)
+		poly(screen, points, npoints, Endsquare, Endsquare, 0, fe->colors[outlinecolor], ZP);
 	
 	free(points);
 }