commit 704cad4

uint  ·  2026-07-13 19:37:36 +0000 UTC
parent 7869c92
remove vector fonts and libschrift dep
7 files changed,  +26, -203
+1, -1
1@@ -2,7 +2,7 @@ CC = cc
2 CFLAGS = -std=c99 -Wall -Wextra
3 CPPFLAGS = -Isource/include -Ilibmaus/include
4 
5-LDFLAGS = -lschrift -lgrapheme -lm
6+LDFLAGS = -lgrapheme
7 
8 include platform.mk
9 
+0, -2
1@@ -6,8 +6,6 @@
2 #include <stdlib.h>
3 
4 static const char* font_path = "/home/seiko/programming/c/cterm/Xanh.bdf";
5-static const float font_size = 24.0f; /* Bitmap fonts ignore font_size */
6-static const int   antialias = true;  /* Bitmap fonts are not affected */
7 
8 static const int   win_width = 80;  /* in columns */
9 static const int   win_height = 24; /* in rows */
+1, -3
 1@@ -15,7 +15,6 @@
 2 #include <unistd.h>
 3 
 4 #include <maus.h>
 5-#include <schrift.h>
 6 
 7 #include "config.h"
 8 #include "draw.h"
 9@@ -110,9 +109,8 @@ static void init(void)
10 	setlocale(LC_CTYPE, "");
11 
12 	/* font */
13-	if (font_load(&font, font_path, font_size) < 0)
14+	if (font_load(&font, font_path) < 0)
15 		die(1, "failed to load font");
16-	font.aa = antialias;
17 
18 	winw = win_width*font.cellw;
19 	winh = win_height*font.cellh;
+5, -73
  1@@ -1,15 +1,9 @@
  2-#include <stdlib.h>
  3-#include <string.h>
  4-
  5 #include "draw.h"
  6 
  7 static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
  8 		int bw, int bh, int x, int y, int aa, uint32_t fg);
  9 static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
 10                   uint32_t fg);
 11-static Glyph* get_glyph(Fontface* f, uint32_t cp);
 12-
 13-static Glyph glyphs[GLYPH_CACHE_MAX];
 14 
 15 static void draw_bitmap(uint32_t* dst, int w, int h, uint8_t* bmp,
 16 		int bw, int bh, int x, int y, int aa, uint32_t fg)
 17@@ -63,52 +57,6 @@ static void blend(uint32_t* dst, int w, int h, int x, int y, uint8_t alpha,
 18 	px[3] = 255;
 19 }
 20 
 21-/** TODO
 22- */
 23-static Glyph* get_glyph(Fontface* f, uint32_t cp)
 24-{
 25-	SFT_Glyph glyph;
 26-	SFT_GMetrics gm;
 27-	SFT_Image img;
 28-	Glyph* g;
 29-
 30-	g = &glyphs[cp % GLYPH_CACHE_MAX];
 31-	if (g->valid && g->cp == cp)
 32-		return g;
 33-	free(g->bmp);
 34-	memset(g, 0, sizeof(*g));
 35-
 36-	if (sft_lookup(&f->sft, cp, &glyph) < 0)
 37-		return NULL;
 38-	if (sft_gmetrics(&f->sft, glyph, &gm) < 0)
 39-		return NULL;
 40-	if (gm.minWidth <= 0 || gm.minHeight <= 0)
 41-		return NULL;
 42-
 43-	g->bmp = calloc(gm.minWidth * gm.minHeight, 1);
 44-	if (!g->bmp)
 45-		return NULL;
 46-
 47-	img.pixels = g->bmp;
 48-	img.width = gm.minWidth;
 49-	img.height = gm.minHeight;
 50-
 51-	if (sft_render(&f->sft, glyph, img) < 0) {
 52-		free(g->bmp);
 53-		memset(g, 0, sizeof(*g));
 54-		return NULL;
 55-	}
 56-
 57-	g->w = gm.minWidth;
 58-	g->h = gm.minHeight;
 59-	g->lsb = gm.leftSideBearing;
 60-	g->yoff = gm.yOffset;
 61-	g->cp = cp;
 62-	g->valid = true;
 63-
 64-	return g;
 65-}
 66-
 67 void draw_clear(uint32_t* dst, int w, int h, uint32_t col)
 68 {
 69 	for (int i = 0; i < w*h; i++)
 70@@ -144,32 +92,16 @@ void draw_codepoint(Fontface* f, uint32_t* dst, int w, int h, int x,
 71 	if (!f || !dst)
 72 		return;
 73 
 74-	if (f->kind == FONT_BDF) {
 75-		if (!f->bdf || cp >= BDF_GLYPHS_MAX)
 76-			return;
 77-
 78-		BdfGlyph* g = &f->bdf[cp];
 79-		if (!g->valid)
 80-			return;
 81-
 82-		draw_bitmap(
 83-			dst, w, h, g->bmp, g->w, g->h, x + g->xoff,
 84-			baseline - g->yoff - g->h, 1, fg
 85-		);
 86-
 87-		return;
 88-	}
 89-
 90-	if (!f->font)
 91+	if (!f->bdf || cp >= BDF_GLYPHS_MAX)
 92 		return;
 93 
 94-	Glyph* g = get_glyph(f, cp);
 95-	if (!g)
 96+	BdfGlyph* g = &f->bdf[cp];
 97+	if (!g->valid)
 98 		return;
 99 
100 	draw_bitmap(
101-		dst, w, h, g->bmp, g->w, g->h, x + g->lsb,
102-		baseline + g->yoff, f->aa, fg
103+		dst, w, h, g->bmp, g->w, g->h, x + g->xoff,
104+		baseline - g->yoff - g->h, 1, fg
105 	);
106 }
107 
+17, -94
  1@@ -1,17 +1,14 @@
  2 #include <ctype.h>
  3-#include <math.h>
  4 #include <stdint.h>
  5 #include <stdio.h>
  6 #include <stdlib.h>
  7 #include <string.h>
  8 
  9 #include "font.h"
 10-#include "schrift.h"
 11 
 12 static int load_bdf(Fontface* f, const char* path);
 13-static int load_sft(Fontface* f, const char* path, double size);
 14 static int strcicmp(char const* a, char const* b);
 15-static int type(const char* path);
 16+static bool is_bdf(const char* path);
 17 
 18 /*
 19  * @brief load a bdf font
 20@@ -23,8 +20,6 @@ static int type(const char* path);
 21  */
 22 static int load_bdf(Fontface* f, const char* path)
 23 {
 24-	f->kind = FONT_BDF;
 25-
 26 	FILE* fp;
 27 	fp = fopen(path, "r");
 28 	if (!fp)
 29@@ -167,7 +162,6 @@ static int load_bdf(Fontface* f, const char* path)
 30 	f->asc = asc;
 31 	f->dsc = -dsc;
 32 	f->cellh = asc + dsc;
 33-	f->size = f->cellh;
 34 
 35 	if (f->cellw <= 0 || f->cellh <= 0) {
 36 		font_free(f);
 37@@ -177,67 +171,6 @@ static int load_bdf(Fontface* f, const char* path)
 38 	return 0;
 39 }
 40 
 41-/*
 42- * @brief load an sft font
 43- *
 44- * @param f fontface to load font into
 45- * @param path path to load font from
 46- * @param size size to load font as
 47- *
 48- * @return -1=failed to load font, 0=success
 49- */
 50-static int load_sft(Fontface* f, const char* path, double size)
 51-{
 52-	f->kind = FONT_SFT;
 53-
 54-	SFT_LMetrics lm;
 55-	SFT_Glyph glyph;
 56-	SFT_GMetrics gm;
 57-
 58-	if (!f || !path || size <= 0)
 59-		return -1;
 60-
 61-	f->font = sft_loadfile(path);
 62-	if (!f->font)
 63-		return -1;
 64-
 65-	/* configure font */
 66-	f->size = size;
 67-	f->sft.font = f->font;
 68-	f->sft.xScale = size;
 69-	f->sft.yScale = size;
 70-	f->sft.xOffset = 0;
 71-	f->sft.yOffset = 0;
 72-	f->sft.flags = SFT_DOWNWARD_Y;
 73-
 74-	/* get font line metrics */
 75-	if (sft_lmetrics(&f->sft, &lm) < 0)
 76-		goto fail;
 77-
 78-	f->asc = lm.ascender;
 79-	f->dsc = lm.descender;
 80-
 81-	/* cell height */
 82-	f->cellh = (int)ceil(lm.ascender - lm.descender + lm.lineGap);
 83-
 84-	/* cell width */
 85-	if (sft_lookup(&f->sft, 'A', &glyph) < 0)
 86-		goto fail;
 87-	if (sft_gmetrics(&f->sft, glyph, &gm) < 0)
 88-		goto fail;
 89-
 90-	f->cellw = (int)ceil(gm.advanceWidth);
 91-
 92-	if (f->cellw <= 0 || f->cellh <= 0)
 93-		goto fail;
 94-	
 95-	return 0;
 96-
 97-fail:
 98-	font_free(f);
 99-	return -1;
100-}
101-
102 /**
103  * @brief case insensitive string compare
104  *
105@@ -263,50 +196,42 @@ static int strcicmp(char const* a, char const* b)
106 }
107 
108 /**
109- * @brief determine the type of font being used
110+ * @brief determine if the font path points to a BDF font
111  * 
112- * @note only BDF or !BDF right now
113- *
114  * @param path path to font
115  *
116- * @return -1=failed to determine, FONT_BDF, FONT_SFT
117+ * @return true if path ends in .bdf
118  */
119-static int type(const char* path)
120+static bool is_bdf(const char* path)
121 {
122 	size_t pl = strlen(path);
123 
124 	if (pl < 4)
125-		return -1;
126+		return false;
127 
128-	if (strcicmp(path + pl - 4, ".bdf") == 0)
129-		return FONT_BDF;
130-	return FONT_SFT;
131+	return strcicmp(path + pl - 4, ".bdf") == 0;
132 }
133 
134-int font_load(Fontface* f, const char* path, double size)
135+int font_load(Fontface* f, const char* path)
136 {
137-	int t;
138+	int ret;
139 
140 	if (!f || !path)
141 		return -1;
142 
143 	memset(f, 0, sizeof(*f));
144 
145-	t = type(path);
146-	if (t < 0)
147+	if (!is_bdf(path))
148 		return -1;
149 
150-	if (t == FONT_BDF) {
151-		f->bdf = calloc(BDF_GLYPHS_MAX, sizeof(*f->bdf));
152-		if (!f->bdf)
153-			return -1;
154-		return load_bdf(f, path);
155-	}
156-
157-	if (size <= 0)
158+	f->bdf = calloc(BDF_GLYPHS_MAX, sizeof(*f->bdf));
159+	if (!f->bdf)
160 		return -1;
161 
162-	return load_sft(f, path, size);
163+	ret = load_bdf(f, path);
164+	if (ret < 0)
165+		font_free(f);
166+	return ret;
167 }
168 
169 void font_free(Fontface* f)
170@@ -314,10 +239,7 @@ void font_free(Fontface* f)
171 	if (!f)
172 		return;
173 
174-	if (f->font)
175-		sft_freefont(f->font);
176-
177-	if (f->kind == FONT_BDF) {
178+	if (f->bdf) {
179 		for (int i = 0; i < BDF_GLYPHS_MAX; i++)
180 			free(f->bdf[i].bmp);
181 		free(f->bdf);
182@@ -325,3 +247,4 @@ void font_free(Fontface* f)
183 
184 	memset(f, 0, sizeof(*f));
185 }
186+
+0, -13
 1@@ -5,19 +5,6 @@
 2 
 3 #include "font.h"
 4 
 5-#define GLYPH_CACHE_MAX 256
 6-
 7-#define Glyph _Glyph /* Xrender defines the same */
 8-typedef struct {
 9-	bool           valid;
10-	uint32_t       cp;
11-	int            w;
12-	int            h;
13-	int            lsb;
14-	int            yoff;
15-	uint8_t*       bmp;
16-} Glyph;
17-
18 /**
19  * @brief clear a buffer size wxh
20  *
+2, -17
 1@@ -2,16 +2,10 @@
 2 #define FONT_H
 3 
 4 #include <stdbool.h>
 5-
 6-#include <schrift.h>
 7+#include <stdint.h>
 8 
 9 #define BDF_GLYPHS_MAX (65536)
10 
11-typedef enum {
12-	FONT_BDF,
13-	FONT_SFT,
14-} FontKind;
15-
16 typedef struct {
17 	bool          valid;
18 	uint8_t*      bmp;
19@@ -24,19 +18,12 @@ typedef struct {
20 typedef BdfFont BdfGlyph;
21 
22 typedef struct {
23-	FontKind      kind;
24-
25-	SFT_Font*     font;
26-	SFT           sft;
27-
28 	BdfFont*      bdf;
29 
30-	double        size;
31 	double        asc;   /* ascent */
32 	double        dsc;   /* descent */
33 	int           cellw;
34 	int           cellh;
35-	bool          aa;    /* anti-aliasing */
36 } Fontface;
37 
38 /**
39@@ -44,11 +31,9 @@ typedef struct {
40  *
41  * @param f font struct to write info to
42  * @param path font path
43- * @param size size of font
44- *
45  * @return 0=success, -1=failed
46  */
47-int font_load(Fontface* f, const char* path, double size);
48+int font_load(Fontface* f, const char* path);
49 
50 /**
51  * @brief free a loaded font