Skip to content

Commit edbcfb7

Browse files
committed
improve ttf2bmp
1 parent 68258f4 commit edbcfb7

File tree

1 file changed

+64
-8
lines changed

1 file changed

+64
-8
lines changed

demo/ttf2bmp.c

+64-8
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,21 @@
1313
#define CHAR_END 126
1414
#define CHARS_PER_ROW 19
1515

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+
1631
bool streq(const char* s1, const char* s2) {
1732
return strcmp(s1, s2) == 0;
1833
}
@@ -25,17 +40,43 @@ int main(int argc, char* argv[]) {
2540
}
2641

2742
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";
2847
char* ttf_file = NULL;
2948

3049
for (int i = 1; i < argc; i++) {
3150
char* arg = argv[i];
3251
int len = strlen(arg);
33-
if (streq(arg, "--size")) {
52+
if (streq(arg, "-s") || streq(arg, "--size")) {
3453
if (i + 1 >= argc) {
3554
fprintf(stderr, "size not defined");
3655
return EXIT_FAILURE;
3756
}
3857
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
3980
} else if (!ttf_file) {
4081
ttf_file = arg;
4182
}
@@ -87,8 +128,8 @@ int main(int argc, char* argv[]) {
87128
int char_height = ascent - descent;
88129

89130
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;
92133
int img_height = rows * char_height;
93134

94135
// allocate bitmap
@@ -136,11 +177,26 @@ int main(int argc, char* argv[]) {
136177

137178
}
138179

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+
}
144200

145201
free(font_file_buf);
146202
free(bitmap);

0 commit comments

Comments
 (0)