13
13
#define CHAR_END 126
14
14
#define CHARS_PER_ROW 19
15
15
16
+ #define FMT_MAX 256
17
+
18
+ static char * fmt (char * fmt , ...) {
19
+
20
+ static char buf [FMT_MAX ];
21
+ va_list args ;
22
+
23
+ va_start (args , fmt );
24
+ vsnprintf (buf , FMT_MAX , fmt , args );
25
+ va_end (args );
26
+
27
+ return buf ;
28
+
29
+ }
30
+
16
31
bool streq (const char * s1 , const char * s2 ) {
17
32
return strcmp (s1 , s2 ) == 0 ;
18
33
}
@@ -25,17 +40,43 @@ int main(int argc, char* argv[]) {
25
40
}
26
41
27
42
int font_size = FONT_SIZE ;
43
+ int chars_per_row = CHARS_PER_ROW ;
44
+ bool embed = false;
45
+ char embed_char = '*' ;
46
+ char * name = "font" ;
28
47
char * ttf_file = NULL ;
29
48
30
49
for (int i = 1 ; i < argc ; i ++ ) {
31
50
char * arg = argv [i ];
32
51
int len = strlen (arg );
33
- if (streq (arg , "--size" )) {
52
+ if (streq (arg , "-s" ) || streq ( arg , "- -size" )) {
34
53
if (i + 1 >= argc ) {
35
54
fprintf (stderr , "size not defined" );
36
55
return EXIT_FAILURE ;
37
56
}
38
57
font_size = atoi (argv [i + 1 ]);
58
+ } else if (streq (arg , "-r" ) || streq (arg , "--row" )) {
59
+ if (i + 1 >= argc ) {
60
+ fprintf (stderr , "row not defined" );
61
+ return EXIT_FAILURE ;
62
+ }
63
+ chars_per_row = atoi (argv [i + 1 ]);
64
+ } else if (streq (arg , "-c" ) || streq (arg , "--char" )) {
65
+ if (i + 1 >= argc ) {
66
+ fprintf (stderr , "char not defined" );
67
+ return EXIT_FAILURE ;
68
+ }
69
+ embed_char = argv [i + 1 ][0 ];
70
+ } else if (streq (arg , "-n" ) || streq (arg , "--name" )) {
71
+ if (i + 1 >= argc ) {
72
+ fprintf (stderr , "name not defined" );
73
+ return EXIT_FAILURE ;
74
+ }
75
+ name = argv [i + 1 ];
76
+ } else if (streq (arg , "-e" ) || streq (arg , "--embed" )) {
77
+ embed = true;
78
+ } else if (streq (arg , "-o" ) || streq (arg , "--output" )) {
79
+ // TODO
39
80
} else if (!ttf_file ) {
40
81
ttf_file = arg ;
41
82
}
@@ -87,8 +128,8 @@ int main(int argc, char* argv[]) {
87
128
int char_height = ascent - descent ;
88
129
89
130
int total_chars = CHAR_END - CHAR_START + 1 ;
90
- int rows = (total_chars + CHARS_PER_ROW - 1 ) / CHARS_PER_ROW ; // Round up
91
- int img_width = CHARS_PER_ROW * char_width ;
131
+ int rows = (total_chars + chars_per_row - 1 ) / chars_per_row ; // Round up
132
+ int img_width = chars_per_row * char_width ;
92
133
int img_height = rows * char_height ;
93
134
94
135
// allocate bitmap
@@ -136,11 +177,26 @@ int main(int argc, char* argv[]) {
136
177
137
178
}
138
179
139
- printf ("width: %d, height: %d\n" , char_width , char_height );
140
-
141
- // save image
142
- stbi_write_png ("font.png" , img_width , img_height , 1 , bitmap , img_width );
143
- printf ("bitmap font saved as font.png\n" );
180
+ if (embed ) {
181
+ printf ("const char* font[] = {\n" );
182
+ for (int y = 0 ; y < img_height ; y ++ ) {
183
+ printf (" \"" );
184
+ for (int x = 0 ; x < img_width ; x ++ ) {
185
+ int i = y * img_width + x ;
186
+ printf ("%c" , bitmap [i ] == 0 ? ' ' : embed_char );
187
+ }
188
+ printf ("\"\n" );
189
+ }
190
+ printf ("};\n" );
191
+ printf ("\n" );
192
+ printf ("int font_width = %d;\n" , char_width );
193
+ printf ("int font_height = %d;\n" , char_height );
194
+ } else {
195
+ // save image
196
+ char * file_name = fmt ("%s_%dx%d.png" , name , char_width , char_height );
197
+ stbi_write_png (file_name , img_width , img_height , 1 , bitmap , img_width );
198
+ printf ("bitmap font saved as %s\n" , file_name );
199
+ }
144
200
145
201
free (font_file_buf );
146
202
free (bitmap );
0 commit comments