commit 549d0d5
seiko
·
2026-05-29 14:14:10 +0000 UTC
parent 8dc8932
separate font_load into bdf and sft functions bdf loader stubbed
2 files changed,
+119,
-44
+99,
-44
1@@ -7,65 +7,42 @@
2 #include "font.h"
3 #include "schrift.h"
4
5+static int load_bdf(Fontface* f, const char* path);
6+static int load_sft(Fontface* f, const char* path, double size);
7 static int strcicmp(char const* a, char const* b);
8 static int type(const char* path);
9
10-/**
11- * @brief case insensitive string compare
12+/*
13+ * @brief load a bdf font
14 *
15- * @param a string1 to compare
16- * @param b string2 to compare
17+ * @param f fontface to load font into
18+ * @param path path to load font from
19 *
20- * @return -1=(a<b), 0=(a==b), 1=(a>b)
21+ * @return -1=failed to load font
22 */
23-static int strcicmp(char const* a, char const* b)
24+static int load_bdf(Fontface* f, const char* path)
25 {
26- int la;
27- int lb;
28- while (*a && *b) {
29- la = tolower((unsigned char)*a);
30- lb = tolower((unsigned char)*b);
31- if (la != lb)
32- return la - lb;
33- a++;
34- b++;
35- }
36-
37- return tolower((unsigned char)*a) - tolower((unsigned char)*b);
38+ /* stub */
39+ return -1;
40 }
41
42-/**
43- * @brief determine the type of font being used
44- *
45- * @note only BDF or !BDF right now
46+/*
47+ * @brief load a bdf font
48 *
49- * @param path path to font
50+ * @param f fontface to load font into
51+ * @param path path to load font from
52+ * @param size size to load font as
53 *
54- * @return -1=failed to determine, 0=BDF, 1=!BDF
55+ * @return -1=failed to load font, 1=success
56 */
57-static int type(const char* path)
58-{
59- size_t pl = strlen(path);
60-
61- if (pl < 3)
62- return -1;
63-
64- char ext[4];
65- memcpy(ext, path + pl - 3, 3);
66- ext[3] = '\0';
67-
68- if (strcicmp(ext, "bdf") == 0)
69- return 0; /* BDF */
70-
71- return 1; /* not BDF */
72-}
73-
74-int font_load(Fontface* f, const char* path, double size)
75+static int load_sft(Fontface* f, const char* path, double size)
76 {
77 SFT_LMetrics lm;
78 SFT_Glyph glyph;
79 SFT_GMetrics gm;
80
81+ f->kind = FONT_SFT;
82+
83 if (!f || !path || size <= 0)
84 return -1;
85 memset(f, 0, sizeof(*f));
86@@ -104,19 +81,97 @@ int font_load(Fontface* f, const char* path, double size)
87 if (f->cellw <= 0 || f->cellh <= 0)
88 goto fail;
89
90- return 0;
91+ return 1;
92
93 fail:
94 font_free(f);
95 return -1;
96 }
97
98+/**
99+ * @brief case insensitive string compare
100+ *
101+ * @param a string1 to compare
102+ * @param b string2 to compare
103+ *
104+ * @return -1=(a<b), 0=(a==b), 1=(a>b)
105+ */
106+static int strcicmp(char const* a, char const* b)
107+{
108+ int la;
109+ int lb;
110+ while (*a && *b) {
111+ la = tolower((unsigned char)*a);
112+ lb = tolower((unsigned char)*b);
113+ if (la != lb)
114+ return la - lb;
115+ a++;
116+ b++;
117+ }
118+
119+ return tolower((unsigned char)*a) - tolower((unsigned char)*b);
120+}
121+
122+/**
123+ * @brief determine the type of font being used
124+ *
125+ * @note only BDF or !BDF right now
126+ *
127+ * @param path path to font
128+ *
129+ * @return -1=failed to determine, FONT_BDF, FONT_SFT
130+ */
131+static int type(const char* path)
132+{
133+ size_t pl = strlen(path);
134+
135+ if (pl < 4)
136+ return -1;
137+
138+ char ext[4];
139+ memcpy(ext, path + pl - 4, 4);
140+ ext[3] = '\0';
141+
142+ if (strcicmp(ext, "bdf") == 0)
143+ return FONT_BDF;
144+
145+ return FONT_SFT;
146+}
147+
148+int font_load(Fontface* f, const char* path, double size)
149+{
150+ int t;
151+
152+ if (!f || !path)
153+ return -1;
154+
155+ memset(f, 0, sizeof(*f));
156+
157+ t = type(path);
158+ if (t < 0)
159+ return -1;
160+
161+ if (t == 0)
162+ return load_bdf(f, path);
163+
164+ if (size <= 0)
165+ return -1;
166+
167+ return load_sft(f, path, size);
168+}
169+
170 void font_free(Fontface* f)
171 {
172 if (!f)
173 return;
174+
175 if (f->font)
176 sft_freefont(f->font);
177+
178+ if (f->kind == FONT_BDF) {
179+ for (int i = 0; i < BDF_GLYPHS_MAX; i++)
180+ free(f->bdf[i].bmp);
181+ }
182+
183 memset(f, 0, sizeof(*f));
184 }
185-
+20,
-0
1@@ -5,9 +5,29 @@
2
3 #include <schrift.h>
4
5+#define BDF_GLYPHS_MAX (65536)
6+
7+typedef enum {
8+ FONT_BDF,
9+ FONT_SFT,
10+} FontKind;
11+
12+typedef struct {
13+ bool valid;
14+ uint8_t* bmp;
15+ int adv; /* advance */
16+ int w;
17+ int h;
18+} BdfFont;
19+
20 typedef struct {
21+ FontKind kind;
22+
23 SFT_Font* font;
24 SFT sft;
25+
26+ BdfFont* bdf;
27+
28 double size;
29 double asc; /* ascent */
30 double dsc; /* descent */