commit 0d6d5af

seiko  ·  2026-05-29 15:45:07 +0000 UTC
parent 17f12f0
add bdf drawing support
1 files changed,  +42, -15
+42, -15
 1@@ -3,12 +3,29 @@
 2 
 3 #include "draw.h"
 4 
 5+static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
 6+		int bw, int bh, int x, int y, int aa, uint32_t fg);
 7 static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
 8                   uint32_t fg);
 9 static Glyph* get_glyph(Fontface* f, uint32_t cp);
10 
11 static Glyph glyphs[GLYPH_CACHE_MAX];
12 
13+static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
14+		int bw, int bh, int x, int y, int aa, uint32_t fg)
15+{
16+	for (int by = 0; by < bh; by++) {
17+		for (int bx = 0; bx < bw; bx++) {
18+			uint8_t a = bmp[by * bw + bx];
19+
20+			if (!aa)
21+				a = (a > 128) ? 255 : 0;
22+
23+			blend(dst, w, h, x + bx, y + by, a, fg);
24+		}
25+	}
26+}
27+
28 /**
29  * @brief blend a pixel using alpha compositong
30  *
31@@ -122,27 +139,37 @@ void draw_rune(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
32 }
33 
34 void draw_codepoint(Fontface* f, uint32_t* dst, int w, int h, int x,
35-                    int baseline, uint32_t cp, uint32_t fg)
36+		int baseline, uint32_t cp, uint32_t fg)
37 {
38-	Glyph* g;
39-
40-	if (!f || !f->font || !dst)
41+	if (!f || !dst)
42 		return;
43 
44-	g = get_glyph(f, cp);
45-	if (!g)
46-		return;
47+	if (f->kind == FONT_BDF) {
48+		if (!f->bdf || cp >= BDF_GLYPHS_MAX)
49+			return;
50 
51-	for (int by = 0; by < g->h; by++) {
52-		for (int bx = 0; bx < g->w; bx++) {
53-			uint8_t a = g->bmp[(by * g->w) + bx];
54-			uint8_t aa = f->aa ? a : (a > 128 ? 255 : 0);
55+		BdfGlyph* g = &f->bdf[cp];
56+		if (!g->valid)
57+			return;
58 
59-			int dst_x = x + g->lsb + bx;
60-			int dst_y = baseline + g->yoff + by;
61+		draw_bitmap(
62+			dst, w, h, g->bmp, g->w, g->h, x + g->xoff,
63+			baseline - g->yoff - g->h, 1, fg
64+		);
65 
66-			blend(dst, w, h, dst_x, dst_y, aa, fg);
67-		}
68+		return;
69 	}
70+
71+	if (!f->font)
72+		return;
73+
74+	Glyph* g = get_glyph(f, cp);
75+	if (!g)
76+		return;
77+
78+	draw_bitmap(
79+		dst, w, h, g->bmp, g->w, g->h, x + g->lsb,
80+		baseline + g->yoff, f->aa, fg
81+	);
82 }
83