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