-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgen_incs.c
187 lines (159 loc) · 5.95 KB
/
gen_incs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <raylib.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#define ISSPACE(a) ((a) == ' ' || (a) == '\t')
typedef struct {uint8_t *data; ptrdiff_t len;} s8;
static s8
read_whole_file(char *name, s8 *mem)
{
s8 res = {0};
FILE *fp = fopen(name, "r");
if (!fp) {
fputs("Failed to open file!\n", stdout);
exit(1);
}
fseek(fp, 0, SEEK_END);
res.len = ftell(fp);
rewind(fp);
if (mem->len < res.len) {
fputs("Not enough space for reading file!\n", stdout);
exit(1);
}
res.data = mem->data;
res.len = fread(res.data, 1, res.len, fp);
fclose(fp);
mem->data += res.len;
mem->len -= res.len;
return res;
}
static s8
get_line(s8 *s)
{
s8 res = {.data = s->data};
while (s->len && s->data[0] != '\n') {
s->data++;
s->len--;
res.len++;
}
/* NOTE: skip over trailing \n */
s->data++;
s->len--;
return res;
}
/* NOTE: modified from raylib */
static void
export_font_as_code(char *font_path, char *output_name, int font_size, s8 mem)
{
s8 raw = read_whole_file(font_path, &mem);
Font font = {0};
font.baseSize = font_size;
font.glyphCount = 95;
font.glyphPadding = 4;
font.glyphs = LoadFontData(raw.data, raw.len, font.baseSize, 0, font.glyphCount, FONT_DEFAULT);
if (font.glyphs == NULL) {
printf("Failed to load font data: %s\n", font_path);
exit(1);
}
Image atlas = GenImageFontAtlas(font.glyphs, &font.recs, font.glyphCount, font.baseSize,
font.glyphPadding, 0);
FILE *fp = fopen(output_name, "w");
if (fp == NULL) {
printf("Failed to open output font file: %s\n", output_name);
exit(1);
}
char suffix[256];
strncpy(suffix, GetFileNameWithoutExt(output_name), 256 - 1);
#define TEXT_BYTES_PER_LINE 16
int image_data_size = GetPixelDataSize(atlas.width, atlas.height, atlas.format);
int comp_data_size = 0;
unsigned char *comp_data = CompressData(atlas.data, image_data_size, &comp_data_size);
// Save font image data (compressed)
fprintf(fp, "#define COMPRESSED_DATA_SIZE_FONT_%s %i\n\n", TextToUpper(suffix), comp_data_size);
fprintf(fp, "// Font image pixels data compressed (DEFLATE)\n");
fprintf(fp, "// NOTE: Original pixel data simplified to GRAYSCALE\n");
fprintf(fp, "static unsigned char fontData_%s[COMPRESSED_DATA_SIZE_FONT_%s] = { ", suffix, TextToUpper(suffix));
for (int i = 0; i < comp_data_size - 1; i++) fprintf(fp, ((i%TEXT_BYTES_PER_LINE == 0)? "0x%02x,\n " : "0x%02x, "), comp_data[i]);
fprintf(fp, "0x%02x };\n\n", comp_data[comp_data_size - 1]);
RL_FREE(comp_data);
// Save font recs data
fprintf(fp, "// Font characters rectangles data\n");
fprintf(fp, "static Rectangle fontRecs_%s[%i] = {\n", suffix, font.glyphCount);
for (int i = 0; i < font.glyphCount; i++)
fprintf(fp, " { %1.0f, %1.0f, %1.0f , %1.0f },\n", font.recs[i].x, font.recs[i].y, font.recs[i].width, font.recs[i].height);
fprintf(fp, "};\n\n");
// Save font glyphs data
// NOTE: Glyphs image data not saved (grayscale pixels), it could be generated from image and recs
fprintf(fp, "// Font glyphs info data\n");
fprintf(fp, "// NOTE: No glyphs.image data provided\n");
fprintf(fp, "static GlyphInfo fontGlyphs_%s[%i] = {\n", suffix, font.glyphCount);
for (int i = 0; i < font.glyphCount; i++)
fprintf(fp, " { %i, %i, %i, %i, { 0 }},\n", font.glyphs[i].value, font.glyphs[i].offsetX, font.glyphs[i].offsetY, font.glyphs[i].advanceX);
fprintf(fp, "};\n\n");
// Custom font loading function
fprintf(fp, "// Font loading function: %s\n", suffix);
fprintf(fp, "static Font LoadFont_%s(void)\n{\n", suffix);
fprintf(fp, " Font font = { 0 };\n\n");
fprintf(fp, " font.baseSize = %i;\n", font.baseSize);
fprintf(fp, " font.glyphCount = %i;\n", font.glyphCount);
fprintf(fp, " font.glyphPadding = %i;\n\n", font.glyphPadding);
fprintf(fp, " // Custom font loading\n");
fprintf(fp, " // NOTE: Compressed font image data (DEFLATE), it requires DecompressData() function\n");
fprintf(fp, " int fontDataSize_%s = 0;\n", suffix);
fprintf(fp, " unsigned char *data = DecompressData(fontData_%s, COMPRESSED_DATA_SIZE_FONT_%s, &fontDataSize_%s);\n", suffix, TextToUpper(suffix), suffix);
fprintf(fp, " Image imFont = { data, %i, %i, 1, %i };\n\n", atlas.width, atlas.height, atlas.format);
fprintf(fp, " // Load texture from image\n");
fprintf(fp, " font.texture = LoadTextureFromImage(imFont);\n");
fprintf(fp, " UnloadImage(imFont); // Uncompressed data can be unloaded from memory\n\n");
fprintf(fp, " // Assign glyph recs and info data directly\n");
fprintf(fp, " // WARNING: This font data must not be unloaded\n");
fprintf(fp, " font.recs = fontRecs_%s;\n", suffix);
fprintf(fp, " font.glyphs = fontGlyphs_%s;\n\n", suffix);
fprintf(fp, " return font;\n");
fprintf(fp, "}\n");
fclose(fp);
}
int
main(void)
{
static uint8_t mem[2u * 1024u * 1024u];
s8 smem = {.data = mem, .len = sizeof(mem)};
SetTraceLogLevel(LOG_NONE);
int font_sizes[] = { FONT_SIZE, FONT_SIZE/2 };
for (int i = 0; i < sizeof(font_sizes)/sizeof(*font_sizes); i++) {
s8 tmem = smem;
s8 rmem = smem;
size_t tlen = snprintf((char *)tmem.data, tmem.len, "lora_sb_%d_inc.h", i);
rmem.len -= (tlen + 1);
rmem.data += (tlen + 1);
export_font_as_code("assets/Lora-SemiBold.ttf", (char *)tmem.data, font_sizes[i], rmem);
}
FILE *out_file = fopen("external/include/shader_inc.h", "w");
if (!out_file) {
fputs("Failed to open necessary files!\n", stdout);
return 1;
}
s8 shader_data = read_whole_file(HSV_LERP_SHADER_NAME, &smem);
s8 s = shader_data;
/* NOTE: skip over license notice */
s8 line = get_line(&s);
fputs("static char *g_hsv_shader_text =\n\t", out_file);
do {
line = get_line(&s);
while (line.len && ISSPACE(*line.data)) {
line.data++;
line.len--;
}
if (line.len) {
fputc('"', out_file);
fwrite(line.data, 1, line.len, out_file);
fputs("\\n\"\n\t", out_file);
}
} while (s.len > 0);
fputs(";\n", out_file);
fclose(out_file);
return 0;
}