commit bc0dd24

uint  ·  2026-05-04 14:59:00 +0000 UTC
parent 5c35547
use glyph's cellw, cellh instead of CELLW, CELLH
3 files changed,  +29, -14
+10, -11
 1@@ -12,8 +12,6 @@
 2 
 3 #define WIDTH  800
 4 #define HEIGHT 600
 5-#define CELLW  18
 6-#define CELLH  32
 7 
 8 Font font;
 9 RGFW_window* win = NULL;
10@@ -59,7 +57,8 @@ static void init(void)
11 		die(1, "failed to create window surface");
12 
13 	/* font */
14-	if (font_load(&font, "/System/Library/Fonts/Supplemental/Andale Mono.ttf", 24.0) < 0)
15+	/* TODO: paths */
16+	if (font_load(&font, "/System/Library/Fonts/Supplemental/Andale Mono.ttf", 48.0) < 0)
17 		die(1, "failed to load font");
18 	font.aa = false;
19 }
20@@ -77,23 +76,23 @@ static void run(void)
21 
22 		draw_clear(pixels, WIDTH, HEIGHT, rgba(0, 0, 0, 255)); /* TODO: opacity */
23 
24-		draw_cell(pixels, WIDTH, HEIGHT, 0, 0, CELLW, CELLH, rgba(255, 0, 0, 255));
25-		draw_cell(pixels, WIDTH, HEIGHT, 1, 0, CELLW, CELLH, rgba(0, 255, 0, 255));
26-		draw_cell(pixels, WIDTH, HEIGHT, 2, 0, CELLW, CELLH, rgba(0, 0, 255, 255));
27-		draw_cursor(pixels, WIDTH, HEIGHT, 3, 0, CELLW, CELLH, rgba(255, 255, 255, 255));
28+		draw_cell(pixels, WIDTH, HEIGHT, 0, 0, font.cellw, font.cellh, rgba(255, 0, 0, 255));
29+		draw_cell(pixels, WIDTH, HEIGHT, 1, 0, font.cellw, font.cellh, rgba(0, 255, 0, 255));
30+		draw_cell(pixels, WIDTH, HEIGHT, 2, 0, font.cellw, font.cellh, rgba(0, 0, 255, 255));
31+		draw_cursor(pixels, WIDTH, HEIGHT, 3, 0, font.cellw, font.cellh, rgba(255, 255, 255, 255));
32 
33 		/* text */
34 		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
35-				0 * CELLW, (int)font.asc, 'r', rgba(0, 0, 0, 255));
36+				0 * font.cellw, (int)font.asc, 'r', rgba(0, 0, 0, 255));
37 
38 		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
39-				1 * CELLW, (int)font.asc, 'g', rgba(0, 0, 0, 255));
40+				1 * font.cellw, (int)font.asc, 'g', rgba(0, 0, 0, 255));
41 
42 		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
43-				2 * CELLW, (int)font.asc, 'b', rgba(0, 0, 0, 255));
44+				2 * font.cellw, (int)font.asc, 'b', rgba(0, 0, 0, 255));
45 
46 		font_draw_codepoint(&font, pixels, WIDTH, HEIGHT,
47-				3 * CELLW, (int)font.asc, 'c', rgba(0, 0, 0, 255));
48+				3 * font.cellw, (int)font.asc, 'c', rgba(0, 0, 0, 255));
49 
50 		RGFW_window_blitSurface(win, surf);
51 	}
+14, -0
 1@@ -47,6 +47,8 @@ static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
 2 int font_load(Font* f, const char* path, double size)
 3 {
 4 	SFT_LMetrics lm;
 5+	SFT_Glyph glyph;
 6+	SFT_GMetrics gm;
 7 
 8 	if (!f || !path || size <= 0)
 9 		return -1;
10@@ -73,6 +75,18 @@ int font_load(Font* f, const char* path, double size)
11 	}
12 	f->asc = lm.ascender;
13 	f->dsc = lm.descender;
14+
15+	/* cell width/height */
16+	f->cellh = (int)(lm.ascender - lm.descender + lm.lineGap + 0.5);
17+
18+	if (sft_lookup(&f->sft, 'A', &glyph) < 0)
19+		return -1;
20+	if (sft_gmetrics(&f->sft, glyph, &gm) < 0)
21+		return -1;
22+
23+	f->cellw = (int)(gm.advanceWidth + 0.5);
24+	if (f->cellw <= 0 || f->cellh <= 0)
25+		return -1;
26 	
27 	return 0;
28 }
+5, -3
 1@@ -9,9 +9,11 @@ typedef struct {
 2 	SFT_Font*     font;
 3 	SFT           sft;
 4 	double        size;
 5-	double        asc; /* ascent */
 6-	double        dsc; /* descent */
 7-	bool          aa;  /* anti-aliasing */
 8+	double        asc;   /* ascent */
 9+	double        dsc;   /* descent */
10+	int           cellw;
11+	int           cellh;
12+	bool          aa;    /* anti-aliasing */
13 } Font;
14 
15 /**