-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgen_incs.c
More file actions
180 lines (151 loc) · 6.13 KB
/
Copy pathgen_incs.c
File metadata and controls
180 lines (151 loc) · 6.13 KB
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
#include <raylib.h>
#include "rstd_compiler.h"
#include "rstd_intrinsics.h"
#include "rstd_types.h"
#include "rstd_core.h"
#include "config.h"
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define ISSPACE(a) ((a) == ' ' || (a) == '\t')
function str8
read_whole_file(char *name, str8 *mem)
{
str8 result = {0};
FILE *fp = fopen(name, "r");
if (!fp) {
fputs("Failed to open file!\n", stdout);
exit(1);
}
fseek(fp, 0, SEEK_END);
result.length = ftell(fp);
rewind(fp);
if (mem->length < result.length) {
fputs("Not enough space for reading file!\n", stdout);
exit(1);
}
result.data = mem->data;
result.length = fread(result.data, 1, result.length, fp);
fclose(fp);
mem->data += result.length;
mem->length -= result.length;
return result;
}
/* NOTE: modified from raylib */
function void
export_font_as_code(char *font_path, char *output_name, int font_size, str8 mem)
{
str8 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.length, 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);
}
function void
generate_shader_include(str8 memory)
{
str8 raw = read_whole_file(HSV_LERP_SHADER_NAME, &memory);
// NOTE(rnp): raylib is dumb and wants this to be 0 terminated
raw.data[raw.length++] = 0;
char *output_name = "out/shader_inc.h";
FILE *fp = fopen(output_name, "w");
if (fp == NULL) {
printf("Failed to open output font file: %s\n", output_name);
exit(1);
}
fprintf(fp, "/* See LICENSE for copyright details */\n\n");
fprintf(fp, "// GENERATED CODE\n\n");
fprintf(fp, "read_only global u8 slider_lerp_bytes[] = {\n");
for (s64 i = 0; i < raw.length; i++) {
b32 end_line = (i != 0) && (i % 16) == 0;
if (i != 0) fprintf(fp, end_line ? "," : ", ");
if (end_line) fprintf(fp, "\n");
if ((i % 16) == 0) fprintf(fp, "\t");
fprintf(fp, "0x%02X", raw.data[i]);
}
fprintf(fp, ", 0x00\n");
fprintf(fp, "\n};\n");
fclose(fp);
}
extern s32
main(void)
{
local_persist u8 mem[2u * 1024u * 1024u];
str8 smem = {.data = mem, .length = sizeof(mem)};
SetTraceLogLevel(LOG_NONE);
int font_sizes[] = { FONT_SIZE, FONT_SIZE/2 };
for (unsigned int i = 0; i < sizeof(font_sizes)/sizeof(*font_sizes); i++) {
str8 tmem = smem;
str8 rmem = smem;
size_t tlen = snprintf((char *)tmem.data, tmem.length, "out/lora_sb_%d_inc.h", i);
rmem.length -= (tlen + 1);
rmem.data += (tlen + 1);
export_font_as_code("assets/Lora-SemiBold.ttf", (char *)tmem.data, font_sizes[i], rmem);
}
generate_shader_include(smem);
return 0;
}