Update fontgen to output D instead of C
This commit is contained in:
parent
c3ead56582
commit
5c97acba5f
@ -71,9 +71,14 @@ fontgen_env = env "fontgen", use: "freetype" do |env|
|
||||
end
|
||||
|
||||
hulk_env = env "hulk", use: %w[ldc2 x86_64-elf-gcc] do |env|
|
||||
env.add_builder(FontGen)
|
||||
env.FontGen("^/src/hulk/kfont.d", "font/Hack-Regular.ttf",
|
||||
"fontgen" => fontgen_env.expand("^/fontgen"))
|
||||
env["sources"] = glob("src/hulk/**/*.d")
|
||||
env["sources"] << "^/src/hulk/kfont.d"
|
||||
env["DFLAGS"] += %w[-mtriple=x86_64-unknown-elf --betterC -release -O3 --wi --enable-cross-module-inlining]
|
||||
env["D_IMPORT_PATH"] += %w[src]
|
||||
env["D_IMPORT_PATH"] << env.expand("^/src")
|
||||
env["LD"] = "x86_64-elf-gcc"
|
||||
env["LDFLAGS"] += %w[-nostdlib -Tsrc/hulk/hulk.ld -Wl,-Map,${_TARGET}.map]
|
||||
env["LDCMD"] = %w[${LD} -o ${_TARGET} ${LDFLAGS} ${_SOURCES} ${LIBDIRPREFIX}${LIBPATH} ${LIBLINKPREFIX}${LIBS}]
|
||||
|
@ -62,52 +62,13 @@ static void load_char(FT_Face face, int char_code)
|
||||
char_infos[char_code].width * char_infos[char_code].height);
|
||||
}
|
||||
|
||||
static const char * bare_header_name(const char * h_file_name)
|
||||
{
|
||||
const char * p = rindex(h_file_name, '/');
|
||||
if (p == NULL)
|
||||
{
|
||||
p = h_file_name;
|
||||
}
|
||||
else
|
||||
{
|
||||
p++;
|
||||
}
|
||||
return p;
|
||||
}
|
||||
|
||||
static char * include_guard_name(const char * h_file_name)
|
||||
{
|
||||
const char * p = bare_header_name(h_file_name);
|
||||
char * guard_name = malloc(strlen(p) + 1);
|
||||
strcpy(guard_name, p);
|
||||
char * m = guard_name;
|
||||
while (*m != '\0')
|
||||
{
|
||||
if ('a' <= *m && *m <= 'z')
|
||||
{
|
||||
*m = toupper(*m);
|
||||
}
|
||||
else if (('0' <= *m && *m <= '9') || ('A' <= *m && *m <= 'Z'))
|
||||
{
|
||||
/* no change */
|
||||
}
|
||||
else
|
||||
{
|
||||
*m = '_';
|
||||
}
|
||||
m++;
|
||||
}
|
||||
return guard_name;
|
||||
}
|
||||
|
||||
static void generate_bytes(FILE * file, const uint8_t * bytes, int count)
|
||||
{
|
||||
for (int i = 0; i < count; i++)
|
||||
{
|
||||
if (i % 8 == 0)
|
||||
{
|
||||
fprintf(file, " ");
|
||||
fprintf(file, " ");
|
||||
}
|
||||
fprintf(file, "0x%02xu,", bytes[i]);
|
||||
if ((i + 1) % 8 == 0)
|
||||
@ -125,62 +86,50 @@ static void generate_bytes(FILE * file, const uint8_t * bytes, int count)
|
||||
}
|
||||
}
|
||||
|
||||
static void generate(const char * c_file_name)
|
||||
static void generate(const char * d_file_name)
|
||||
{
|
||||
char * h_file_name = malloc(strlen(c_file_name) + 1);
|
||||
strcpy(h_file_name, c_file_name);
|
||||
h_file_name[strlen(h_file_name) - 1] = 'h';
|
||||
char * guard = include_guard_name(h_file_name);
|
||||
|
||||
FILE * h_file = fopen(h_file_name, "wb");
|
||||
fprintf(h_file, "#ifndef %s\n", guard);
|
||||
fprintf(h_file, "#define %s\n\n", guard);
|
||||
fprintf(h_file, "#include <stdint.h>\n");
|
||||
fprintf(h_file, "typedef struct {\n int width;\n int height;\n int top;\n int left;\n const uint8_t * bitmap;\n} fontgen_char_info_t;\n");
|
||||
fprintf(h_file, "typedef struct {\n int line_height;\n int advance;\n int baseline_offset;\n const fontgen_char_info_t ** char_infos;\n} fontgen_font_t;\n");
|
||||
fprintf(h_file, "extern const fontgen_font_t kfont;\n");
|
||||
fprintf(h_file, "#endif\n");
|
||||
fclose(h_file);
|
||||
|
||||
FILE * c_file = fopen(c_file_name, "wb");
|
||||
fprintf(c_file, "#include \"%s\"\n", bare_header_name(h_file_name));
|
||||
fprintf(c_file, "#include <stddef.h>\n");
|
||||
FILE * fh = fopen(d_file_name, "wb");
|
||||
fprintf(fh, "module hulk.kfont;\n");
|
||||
fprintf(fh, "struct CharInfo {\n");
|
||||
fprintf(fh, " uint width;\n");
|
||||
fprintf(fh, " uint height;\n");
|
||||
fprintf(fh, " uint top;\n");
|
||||
fprintf(fh, " uint left;\n");
|
||||
fprintf(fh, " const ubyte[] bitmap;\n");
|
||||
fprintf(fh, "};\n");
|
||||
fprintf(fh, "struct FontInfo {\n");
|
||||
fprintf(fh, " uint line_height;\n");
|
||||
fprintf(fh, " uint advance;\n");
|
||||
fprintf(fh, " uint baseline_offset;\n");
|
||||
fprintf(fh, " const CharInfo[] chars;\n");
|
||||
fprintf(fh, "};\n");
|
||||
fprintf(fh, "__gshared const FontInfo kfont = {\n");
|
||||
fprintf(fh, " %du,\n", line_height);
|
||||
fprintf(fh, " %du,\n", max_advance);
|
||||
fprintf(fh, " %du,\n", baseline_offset);
|
||||
fprintf(fh, " [\n");
|
||||
for (int i = 0; i < N_CHARS; i++)
|
||||
{
|
||||
fprintf(fh, " CharInfo(\n");
|
||||
fprintf(fh, " %du,\n", char_infos[i].width);
|
||||
fprintf(fh, " %du,\n", char_infos[i].height);
|
||||
fprintf(fh, " %du,\n", char_infos[i].top);
|
||||
fprintf(fh, " %du,\n", char_infos[i].left);
|
||||
if (char_infos[i].width > 0)
|
||||
{
|
||||
fprintf(c_file, "static const uint8_t char_bitmap_%d[] = {\n", i);
|
||||
generate_bytes(c_file, char_infos[i].bitmap, char_infos[i].width * char_infos[i].height);
|
||||
fprintf(c_file, "};\n");
|
||||
}
|
||||
fprintf(c_file, "static const fontgen_char_info_t char_%d = {\n", i);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].width);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].height);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].top);
|
||||
fprintf(c_file, " %d,\n", char_infos[i].left);
|
||||
if (char_infos[i].width > 0)
|
||||
{
|
||||
fprintf(c_file, " char_bitmap_%d,\n", i);
|
||||
fprintf(fh, " [\n");
|
||||
generate_bytes(fh, char_infos[i].bitmap, char_infos[i].width * char_infos[i].height);
|
||||
fprintf(fh, " ]\n");
|
||||
}
|
||||
else
|
||||
{
|
||||
fprintf(c_file, " NULL,\n");
|
||||
fprintf(fh, " []\n");
|
||||
}
|
||||
fprintf(c_file, "};\n\n");
|
||||
fprintf(fh, " ),\n");
|
||||
}
|
||||
fprintf(c_file, "const fontgen_char_info_t * char_infos[] = {\n");
|
||||
for (int i = 0; i < N_CHARS; i++)
|
||||
{
|
||||
fprintf(c_file, " &char_%d,\n", i);
|
||||
}
|
||||
fprintf(c_file, "};\n");
|
||||
fprintf(c_file, "const fontgen_font_t kfont = {\n");
|
||||
fprintf(c_file, " %d,\n", line_height);
|
||||
fprintf(c_file, " %d,\n", max_advance);
|
||||
fprintf(c_file, " %d,\n", baseline_offset);
|
||||
fprintf(c_file, " char_infos,\n");
|
||||
fprintf(c_file, "};\n");
|
||||
fclose(c_file);
|
||||
fprintf(fh, " ],\n");
|
||||
fprintf(fh, "};\n");
|
||||
fclose(fh);
|
||||
}
|
||||
|
||||
int main(int argc, char * argv[])
|
||||
|
Loading…
x
Reference in New Issue
Block a user