commit 55e3c7e

uint  ·  2026-05-07 16:14:00 +0000 UTC
parent 4635d73
move blend, font_draw_codepoint -> draw.h
5 files changed,  +112, -108
+6, -6
 1@@ -82,16 +82,16 @@ static void run(void)
 2 		draw_cursor(pixels, WIDTH, HEIGHT, 3, 0, font.cellw, font.cellh, rgba(255, 255, 255, 255));
 3 
 4 		/* text */
 5-		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
 6+		draw_codepoint(&font, pixels, WIDTH, HEIGHT,
 7 				0 * font.cellw, (int)font.asc, 'r', rgba(0, 0, 0, 255));
 8 
 9-		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
10-				1 * font.cellw, (int)font.asc, 'g', rgba(0, 0, 0, 255));
11+		draw_codepoint(&font, pixels, WIDTH, HEIGHT,
12+		   	1 * font.cellw, (int)font.asc, 'g', rgba(0, 0, 0, 255));
13 
14-		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
15-				2 * font.cellw, (int)font.asc, 'b', rgba(0, 0, 0, 255));
16+		draw_codepoint(&font, pixels, WIDTH, HEIGHT,
17+		   	2 * font.cellw, (int)font.asc, 'b', rgba(0, 0, 0, 255));
18 
19-		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
20+		draw_codepoint(&font, pixels, WIDTH, HEIGHT,
21 				3 * font.cellw, (int)font.asc, 'c', rgba(0, 0, 0, 255));
22 
23 		RGFW_window_blitSurface(win, surf);
+89, -0
 1@@ -1,5 +1,47 @@
 2+#include <stdlib.h>
 3+
 4 #include "draw.h"
 5 
 6+static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
 7+                  uint32_t fg);
 8+
 9+/**
10+ * @brief blend a pixel using alpha compositong
11+ *
12+ * @param dst buffer to write to
13+ * @param x pixel x-position
14+ * @param y piyel y-position
15+ * @param alpha coverage from glyph
16+ * @param fg foreground colour
17+ */
18+static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
19+                  uint32_t fg)
20+{
21+	uint8_t* px;
22+	uint8_t fg_r;
23+	uint8_t fg_g;
24+	uint8_t fg_b;
25+
26+	if (x < 0 || x >= w || y < 0 || y >= h || alpha == 0)
27+		/* transparent/oob */
28+		return;
29+
30+	/* ptr to px at (x, y) */
31+	px = (uint8_t*)&dst[y * w + x];
32+
33+	/* unpack fg colour*/
34+	fg_r = (uint8_t)(fg & 0xff);
35+	fg_g = (uint8_t)((fg >> 8) & 0xff);
36+	fg_b = (uint8_t)((fg >> 16) & 0xff);
37+
38+	/* alpha blend each channel */
39+	px[0] = (uint8_t)((fg_r * alpha + px[0] * (255 - alpha)) / 255);
40+	px[1] = (uint8_t)((fg_g * alpha + px[1] * (255 - alpha)) / 255);
41+	px[2] = (uint8_t)((fg_b * alpha + px[2] * (255 - alpha)) / 255);
42+
43+	px[3] = 255;
44+}
45+
46 void draw_clear(uint32_t* dst, int w, int h, uint32_t col)
47 {
48 	for (int i = 0; i < w*h; i++)
49@@ -35,3 +77,50 @@ void draw_cursor(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
50 	draw_cell(dst, w, h, cl, rw, cw, ch, fg);
51 }
52 
53+void draw_codepoint(Font* f, uint32_t* dst, int w, int h, int x,
54+                         int baseline, uint32_t cp, uint32_t fg)
55+{
56+	SFT_Glyph glyph;
57+	SFT_GMetrics gm;
58+	SFT_Image img;
59+	uint8_t* bmp;
60+	int bx;
61+	int by;
62+
63+	/* get glyph metics */
64+	if (!f || !f->font || !dst)
65+		return;
66+	if (sft_lookup(&f->sft, cp, &glyph) < 0)
67+		return;
68+	if (sft_gmetrics(&f->sft, glyph, &gm) < 0)
69+		return;
70+	if (gm.minWidth <= 0 || gm.minHeight <= 0)
71+		return;
72+
73+	bmp = calloc(gm.minWidth*gm.minHeight, 1);
74+	if (!bmp)
75+		return;
76+
77+	img.pixels = bmp;
78+	img.width = gm.minWidth;
79+	img.height = gm.minHeight;
80+
81+	if (sft_render(&f->sft, glyph, img) == 0) {
82+		/* copy bmp to dst */
83+		for (by = 0; by < gm.minHeight; by++) {
84+			for (bx = 0; bx < gm.minWidth; bx++) {
85+				/* TODO: proper aa controls */
86+				uint8_t a = bmp[(by*gm.minWidth) + bx]; /* alpha */
87+				uint8_t aa = f->aa ? a : (a > 128 ? 255 : 0);
88+
89+				/* translate bpm pixels to dst pixels */
90+				int dst_x = x + (int)gm.leftSideBearing + bx;
91+				int dst_y = baseline + gm.yOffset + by;
92+
93+				blend(dst, w, h, dst_x, dst_y, aa, fg);
94+			}
95+		}
96+	}
97+	free(bmp);
98+}
99+
+0, -87
 1@@ -4,46 +4,6 @@
 2 #include <stdlib.h>
 3 #include <string.h>
 4 
 5-static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
 6-                  uint32_t fg);
 7-
 8-/**
 9- * @brief blend a pixel using alpha compositong
10- *
11- * @param dst buffer to write to
12- * @param x pixel x-position
13- * @param y piyel y-position
14- * @param alpha coverage from glyph
15- * @param fg foreground colour
16- */
17-static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
18-                  uint32_t fg)
19-{
20-	uint8_t* px;
21-	uint8_t fg_r;
22-	uint8_t fg_g;
23-	uint8_t fg_b;
24-
25-	if (x < 0 || x >= w || y < 0 || y >= h || alpha == 0)
26-		/* transparent/oob */
27-		return;
28-
29-	/* ptr to px at (x, y) */
30-	px = (uint8_t*)&dst[y * w + x];
31-
32-	/* unpack fg colour*/
33-	fg_r = (uint8_t)(fg & 0xff);
34-	fg_g = (uint8_t)((fg >> 8) & 0xff);
35-	fg_b = (uint8_t)((fg >> 16) & 0xff);
36-
37-	/* alpha blend each channel */
38-	px[0] = (uint8_t)((fg_r * alpha + px[0] * (255 - alpha)) / 255);
39-	px[1] = (uint8_t)((fg_g * alpha + px[1] * (255 - alpha)) / 255);
40-	px[2] = (uint8_t)((fg_b * alpha + px[2] * (255 - alpha)) / 255);
41-
42-	px[3] = 255;
43-}
44-
45 int font_load(Font* f, const char* path, double size)
46 {
47 	SFT_LMetrics lm;
48@@ -102,50 +62,3 @@ void font_free(Font* f)
49 	memset(f, 0, sizeof(*f));
50 }
51 
52-void font_draw_codepoint(Font* f, uint32_t* dst, int w, int h, int x,
53-                         int baseline, uint32_t cp, uint32_t fg)
54-{
55-	SFT_Glyph glyph;
56-	SFT_GMetrics gm;
57-	SFT_Image img;
58-	uint8_t* bmp;
59-	int bx;
60-	int by;
61-
62-	/* get glyph metics */
63-	if (!f || !f->font || !dst)
64-		return;
65-	if (sft_lookup(&f->sft, cp, &glyph) < 0)
66-		return;
67-	if (sft_gmetrics(&f->sft, glyph, &gm) < 0)
68-		return;
69-	if (gm.minWidth <= 0 || gm.minHeight <= 0)
70-		return;
71-
72-	bmp = calloc(gm.minWidth*gm.minHeight, 1);
73-	if (!bmp)
74-		return;
75-
76-	img.pixels = bmp;
77-	img.width = gm.minWidth;
78-	img.height = gm.minHeight;
79-
80-	if (sft_render(&f->sft, glyph, img) == 0) {
81-		/* copy bmp to dst */
82-		for (by = 0; by < gm.minHeight; by++) {
83-			for (bx = 0; bx < gm.minWidth; bx++) {
84-				/* TODO: proper aa controls */
85-				uint8_t a = bmp[(by*gm.minWidth) + bx]; /* alpha */
86-				uint8_t aa = f->aa ? a : (a > 128 ? 255 : 0);
87-
88-				/* translate bpm pixels to dst pixels */
89-				int dst_x = x + (int)gm.leftSideBearing + bx;
90-				int dst_y = baseline + gm.yOffset + by;
91-
92-				blend(dst, w, h, dst_x, dst_y, aa, fg);
93-			}
94-		}
95-	}
96-	free(bmp);
97-}
98-
+17, -0
 1@@ -3,6 +3,8 @@
 2 
 3 #include <stdint.h>
 4 
 5+#include "font.h"
 6+
 7 /* TODO: proper structs and faster drawing with damage for drawing shaepes*/
 8 
 9 /**
10@@ -47,5 +49,20 @@ void draw_cell(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
11 void draw_cursor(uint32_t* dst, int w, int h, int cl, int rw, int cw, int ch,
12                  uint32_t fg);
13 
14+/**
15+ * @brief draw one codepoint to buffer
16+ *
17+ * @param f font to draw from
18+ * @param dst destination buffer
19+ * @param w buffer width
20+ * @param h buffer height
21+ * @param x x-position to draw at
22+ * @param baseline text baseline y-position
23+ * @param cp (unicode) codepoint to draw
24+ * @param fg foreground colour
25+ */
26+void draw_codepoint(Font* f, uint32_t* dst, int w, int h, int x,
27+                         int baseline, uint32_t cp, uint32_t fg);
28+
29 #endif /* DRAW_H */
30 
+0, -15
 1@@ -34,20 +34,5 @@ int font_load(Font* f, const char* path, double size);
 2  */
 3 void font_free(Font* f);
 4 
 5-/**
 6- * @brief draw one codepoint to buffer
 7- *
 8- * @param f font to draw from
 9- * @param dst destination buffer
10- * @param w buffer width
11- * @param h buffer height
12- * @param x x-position to draw at
13- * @param baseline text baseline y-position
14- * @param cp (unicode) codepoint to draw
15- * @param fg foreground colour
16- */
17-void font_draw_codepoint(Font* f, uint32_t* dst, int w, int h, int x,
18-                         int baseline, uint32_t cp, uint32_t fg);
19-
20 #endif /* FONT_H */
21