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
|
end
|
||||||
|
|
||||||
hulk_env = env "hulk", use: %w[ldc2 x86_64-elf-gcc] do |env|
|
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"] = 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["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"] += %w[src]
|
||||||
|
env["D_IMPORT_PATH"] << env.expand("^/src")
|
||||||
env["LD"] = "x86_64-elf-gcc"
|
env["LD"] = "x86_64-elf-gcc"
|
||||||
env["LDFLAGS"] += %w[-nostdlib -Tsrc/hulk/hulk.ld -Wl,-Map,${_TARGET}.map]
|
env["LDFLAGS"] += %w[-nostdlib -Tsrc/hulk/hulk.ld -Wl,-Map,${_TARGET}.map]
|
||||||
env["LDCMD"] = %w[${LD} -o ${_TARGET} ${LDFLAGS} ${_SOURCES} ${LIBDIRPREFIX}${LIBPATH} ${LIBLINKPREFIX}${LIBS}]
|
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);
|
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)
|
static void generate_bytes(FILE * file, const uint8_t * bytes, int count)
|
||||||
{
|
{
|
||||||
for (int i = 0; i < count; i++)
|
for (int i = 0; i < count; i++)
|
||||||
{
|
{
|
||||||
if (i % 8 == 0)
|
if (i % 8 == 0)
|
||||||
{
|
{
|
||||||
fprintf(file, " ");
|
fprintf(file, " ");
|
||||||
}
|
}
|
||||||
fprintf(file, "0x%02xu,", bytes[i]);
|
fprintf(file, "0x%02xu,", bytes[i]);
|
||||||
if ((i + 1) % 8 == 0)
|
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);
|
FILE * fh = fopen(d_file_name, "wb");
|
||||||
strcpy(h_file_name, c_file_name);
|
fprintf(fh, "module hulk.kfont;\n");
|
||||||
h_file_name[strlen(h_file_name) - 1] = 'h';
|
fprintf(fh, "struct CharInfo {\n");
|
||||||
char * guard = include_guard_name(h_file_name);
|
fprintf(fh, " uint width;\n");
|
||||||
|
fprintf(fh, " uint height;\n");
|
||||||
FILE * h_file = fopen(h_file_name, "wb");
|
fprintf(fh, " uint top;\n");
|
||||||
fprintf(h_file, "#ifndef %s\n", guard);
|
fprintf(fh, " uint left;\n");
|
||||||
fprintf(h_file, "#define %s\n\n", guard);
|
fprintf(fh, " const ubyte[] bitmap;\n");
|
||||||
fprintf(h_file, "#include <stdint.h>\n");
|
fprintf(fh, "};\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(fh, "struct FontInfo {\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(fh, " uint line_height;\n");
|
||||||
fprintf(h_file, "extern const fontgen_font_t kfont;\n");
|
fprintf(fh, " uint advance;\n");
|
||||||
fprintf(h_file, "#endif\n");
|
fprintf(fh, " uint baseline_offset;\n");
|
||||||
fclose(h_file);
|
fprintf(fh, " const CharInfo[] chars;\n");
|
||||||
|
fprintf(fh, "};\n");
|
||||||
FILE * c_file = fopen(c_file_name, "wb");
|
fprintf(fh, "__gshared const FontInfo kfont = {\n");
|
||||||
fprintf(c_file, "#include \"%s\"\n", bare_header_name(h_file_name));
|
fprintf(fh, " %du,\n", line_height);
|
||||||
fprintf(c_file, "#include <stddef.h>\n");
|
fprintf(fh, " %du,\n", max_advance);
|
||||||
|
fprintf(fh, " %du,\n", baseline_offset);
|
||||||
|
fprintf(fh, " [\n");
|
||||||
for (int i = 0; i < N_CHARS; i++)
|
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)
|
if (char_infos[i].width > 0)
|
||||||
{
|
{
|
||||||
fprintf(c_file, "static const uint8_t char_bitmap_%d[] = {\n", i);
|
fprintf(fh, " [\n");
|
||||||
generate_bytes(c_file, char_infos[i].bitmap, char_infos[i].width * char_infos[i].height);
|
generate_bytes(fh, char_infos[i].bitmap, char_infos[i].width * char_infos[i].height);
|
||||||
fprintf(c_file, "};\n");
|
fprintf(fh, " ]\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);
|
|
||||||
}
|
}
|
||||||
else
|
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");
|
fprintf(fh, " ],\n");
|
||||||
for (int i = 0; i < N_CHARS; i++)
|
fprintf(fh, "};\n");
|
||||||
{
|
fclose(fh);
|
||||||
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);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
int main(int argc, char * argv[])
|
int main(int argc, char * argv[])
|
||||||
|
Loading…
x
Reference in New Issue
Block a user