u8g2-copy/tools/font/build/build.c

344 lines
8.3 KiB
C
Raw Normal View History

2015-11-07 04:07:14 +08:00
2015-11-07 16:27:18 +08:00
#include <stdio.h>
#include <string.h>
2015-11-11 05:57:47 +08:00
#include <stdlib.h>
#include <unistd.h>
2015-11-07 16:27:18 +08:00
2015-11-07 04:07:14 +08:00
#define BM_T 1 /* Transparent = build mode 0 proportional */
#define BM_H 2 /* Common Height = build mode 1 */
#define BM_M 4 /* Monospace = build mode 2 */
#define BM_8 8 /* 8x8 = build mode 3 */
#define FM_C 1 /* u8g2 compressed font */
#define FM_8 2 /* u8x8 uncompressed font */
2015-11-07 16:27:18 +08:00
#define MM_F 1 /* full */
#define MM_R 2 /* reduced */
#define MM_N 4 /* numbers */
2015-11-13 02:48:50 +08:00
/*===================================*/
int add_to_str(char **dest, const char *s)
{
void *t;
if ( *dest == NULL )
{
*dest = strdup(s);
if ( *dest == NULL )
return 0;
}
else
{
t = realloc(*dest, strlen(*dest) + strlen(s) + 1);
if ( t == NULL )
return 0;
*dest = (char *)t;
strcat(*dest, s);
}
return 1;
}
2015-11-11 05:57:47 +08:00
/*
2015-11-13 02:48:50 +08:00
copy file from source_file_name to dest_file_name
*/
int file_copy(const char *source_file_name, const char *dest_file_name)
{
int ch;
FILE *source_fp;
FILE *dest_fp;
source_fp = fopen(source_file_name, "r");
dest_fp = fopen(dest_file_name, "w");
if ( source_fp == NULL || dest_fp == NULL )
return 0;
while( ( ch = fgetc(source_fp) ) != EOF )
fputc(ch, dest_fp);
fclose(source_fp);
fclose(dest_fp);
return 1;
}
2015-11-11 05:57:47 +08:00
2015-11-13 02:48:50 +08:00
/*
Insert "text" between lines "start_line" and "end_line" of file "filename"
2015-11-11 05:57:47 +08:00
*/
2015-11-13 02:48:50 +08:00
int insert_into_file(const char *filename, const char *text, const char *start_line, const char *end_line)
{
static char line[1024*4];
const char *tmpname = "tmp.h";
FILE *source_fp;
FILE *dest_fp;
if ( file_copy(filename, tmpname) == 0 )
return 0;
source_fp = fopen(tmpname, "r");
dest_fp = fopen(filename, "w");
if ( source_fp == NULL || dest_fp == NULL )
return 0;
for(;;)
{
if ( fgets(line, 1024*4, source_fp) == NULL )
break;
if ( strncmp(line, start_line, strlen(start_line)) == 0 )
{
fputs(line, dest_fp);
fputs(text, dest_fp);
fputs("\n", dest_fp);
for(;;)
{
if ( fgets(line, 1024*4, source_fp) == NULL )
break;
if ( strncmp(line, end_line, strlen(end_line)) == 0 )
{
fputs(line, dest_fp);
break;
}
}
}
else
{
fputs(line, dest_fp);
}
}
fclose(source_fp);
fclose(dest_fp);
return 1;
}
/*===================================*/
2015-11-07 16:27:18 +08:00
2015-11-07 04:07:14 +08:00
struct groupinfo
{
char *groupname;
};
struct fontinfo
{
int is_ttf; /* 0 = bdf, 1= ttf */
char *filename; /* filename including extension */
2015-11-07 16:27:18 +08:00
char *name;
int group; /* group-index */
2015-11-07 04:07:14 +08:00
int pt; /* point size, ttf only */
int build_mode; /* Or'd BM_T, BM_H, BM_M, BM_8 */
int font_mode; /* Or'd FM_C and FM_8 */
2015-11-07 16:27:18 +08:00
int map_mode; /* Or'd MM_F, FM_N and FM_R */
2015-11-07 04:07:14 +08:00
};
2015-11-10 05:30:06 +08:00
typedef void (*cbfn_t)(int i, int fm, char *fms, int bm, char *bms, int mm, char *mms);
2015-11-07 04:07:14 +08:00
2015-11-07 16:27:18 +08:00
struct groupinfo gi[] = {
{ "X11" },
2015-11-13 06:03:08 +08:00
{ "fontstruct" },
2015-11-07 16:27:18 +08:00
};
2015-11-07 04:18:07 +08:00
struct fontinfo fi[] = {
2015-11-13 06:03:08 +08:00
{ 0, "6x10.bdf", "6x10", 0, 0, BM_T|BM_M, FM_C, MM_F|MM_R|MM_N },
{ 0, "amstrad_cpc.bdf", "amstrad_cpc", 1, 0, BM_8, FM_C|FM_8, MM_F|MM_R|MM_N }
2015-11-07 16:27:18 +08:00
};
char *bdf_path = "../bdf/";
2015-11-11 05:57:47 +08:00
char *bdfconv_path = "../bdfconv/bdfconv";
2015-11-10 05:30:06 +08:00
FILE *u8g2_font_list_fp;
FILE *u8x8_font_list_fp;
2015-11-13 02:48:50 +08:00
char *u8g2_prototypes = NULL;
char *u8x8_prototypes = NULL;
2015-11-07 16:27:18 +08:00
char target_font_identifier[1024];
char bdf_cmd[2048];
2015-11-13 02:48:50 +08:00
char font_prototype[2048];
2015-11-07 16:27:18 +08:00
2015-11-10 05:30:06 +08:00
void bdfconv(int i, int fm, char *fms, int bm, char *bms, int mm, char *mms)
2015-11-07 16:27:18 +08:00
{
2015-11-11 05:57:47 +08:00
strcpy(bdf_cmd, bdfconv_path);
strcat(bdf_cmd, " ");
2015-11-07 16:27:18 +08:00
if ( fm == FM_C ) strcat(bdf_cmd, " -f 1");
if ( fm == FM_8 ) strcat(bdf_cmd, " -f 2");
if ( bm == BM_T ) strcat(bdf_cmd, " -b 0");
if ( bm == BM_H ) strcat(bdf_cmd, " -b 1");
if ( bm == BM_M ) strcat(bdf_cmd, " -b 2");
if ( bm == BM_8 ) strcat(bdf_cmd, " -b 3");
if ( mm == MM_F ) strcat(bdf_cmd, " -m '32-255>32'");
if ( mm == MM_R ) strcat(bdf_cmd, " -m '32-127>32'");
if ( mm == MM_N ) strcat(bdf_cmd, " -m '32,42-58>32'");
strcat(bdf_cmd, " ");
strcat(bdf_cmd, bdf_path);
strcat(bdf_cmd, fi[i].filename);
strcat(bdf_cmd, " -n ");
strcat(bdf_cmd, target_font_identifier);
strcat(bdf_cmd, " -o ");
2015-11-11 05:57:47 +08:00
//strcat(bdf_cmd, target_font_identifier);
strcat(bdf_cmd, "font");
strcat(bdf_cmd, ".c");
2015-11-13 02:48:50 +08:00
/*
fprintf(out_fp, "const uint8_t %s[%d] U8X8_FONT_SECTION(\"%s\") \n", fontname, bf->target_cnt, fontname);
fprintf(out_fp, "const uint8_t %s[%d] U8G2_FONT_SECTION(\"%s\") \n", fontname, bf->target_cnt, fontname);
*/
strcpy(font_prototype, "const uint8_t ");
strcat(font_prototype, target_font_identifier);
strcat(font_prototype, " []");
2015-11-11 05:57:47 +08:00
if ( fm == FM_8 )
2015-11-13 02:48:50 +08:00
{
2015-11-11 05:57:47 +08:00
strcat(bdf_cmd, " && cat font.c >>u8x8_fonts.c");
2015-11-13 02:48:50 +08:00
strcat(font_prototype, " U8X8_FONT_SECTION(\"");
strcat(font_prototype, target_font_identifier);
strcat(font_prototype, "\");\n");
add_to_str(&u8x8_prototypes, font_prototype);
}
2015-11-11 05:57:47 +08:00
else
2015-11-13 02:48:50 +08:00
{
2015-11-11 05:57:47 +08:00
strcat(bdf_cmd, " && cat font.c >>u8g2_fonts.c");
2015-11-13 02:48:50 +08:00
strcat(font_prototype, " U8G2_FONT_SECTION(\"");
strcat(font_prototype, target_font_identifier);
strcat(font_prototype, "\");\n");
add_to_str(&u8g2_prototypes, font_prototype);
}
2015-11-07 16:27:18 +08:00
2015-11-11 05:57:47 +08:00
printf("%s\n", bdf_cmd);
system(bdf_cmd);
2015-11-10 05:30:06 +08:00
}
void fontlist_identifier(int i, int fm, char *fms, int bm, char *bms, int mm, char *mms)
{
if ( fm == FM_C )
{
fprintf(u8g2_font_list_fp, " %s,\n", target_font_identifier);
}
if ( fm == FM_8 )
{
fprintf(u8x8_font_list_fp, " %s,\n", target_font_identifier);
}
}
void fontlist_name(int i, int fm, char *fms, int bm, char *bms, int mm, char *mms)
{
if ( fm == FM_C )
{
fprintf(u8g2_font_list_fp, " \"%s\",\n", target_font_identifier);
}
if ( fm == FM_8 )
{
fprintf(u8x8_font_list_fp, " \"%s\",\n", target_font_identifier);
}
2015-11-07 16:27:18 +08:00
}
2015-11-10 05:30:06 +08:00
void gen_font(int i, int fm, char *fms, int bm, char *bms, int mm, char *mms, cbfn_t cb )
{
if ( fm == FM_8 )
if ( bm != BM_8 )
return;
strcpy(target_font_identifier, fms);
strcat(target_font_identifier, "_");
strcat(target_font_identifier, fi[i].name);
strcat(target_font_identifier, "_");
strcat(target_font_identifier, bms);
strcat(target_font_identifier, mms);
cb(i, fm,fms,bm,bms,mm,mms);
}
void map_font(int i, int fm, char *fms, int bm, char *bms, cbfn_t cb)
2015-11-07 16:27:18 +08:00
{
if ( (fi[i].map_mode & MM_F) != 0 )
2015-11-10 05:30:06 +08:00
gen_font(i, fm, fms, bm, bms, MM_F, "f", cb);
2015-11-07 16:27:18 +08:00
if ( (fi[i].map_mode & MM_R) != 0 )
2015-11-10 05:30:06 +08:00
gen_font(i, fm, fms, bm, bms, MM_R, "r", cb);
2015-11-07 16:27:18 +08:00
if ( (fi[i].map_mode & MM_N) != 0 )
2015-11-10 05:30:06 +08:00
gen_font(i, fm, fms, bm, bms, MM_N, "n", cb);
2015-11-07 16:27:18 +08:00
}
2015-11-10 05:30:06 +08:00
void build_font(int i, int fm, char *fms, cbfn_t cb)
2015-11-07 16:27:18 +08:00
{
if ( (fi[i].build_mode & BM_T) != 0 )
2015-11-10 05:30:06 +08:00
map_font(i, fm, fms, BM_T, "t", cb);
2015-11-07 16:27:18 +08:00
if ( (fi[i].build_mode & BM_H) != 0 )
2015-11-10 05:30:06 +08:00
map_font(i, fm, fms, BM_H, "h", cb);
2015-11-07 16:27:18 +08:00
if ( (fi[i].build_mode & BM_M) != 0 )
2015-11-10 05:30:06 +08:00
map_font(i, fm, fms, BM_M, "m", cb);
2015-11-07 16:27:18 +08:00
if ( (fi[i].build_mode & BM_8) != 0 )
2015-11-10 05:30:06 +08:00
map_font(i, fm, fms, BM_8, "8", cb);
2015-11-07 16:27:18 +08:00
}
2015-11-10 05:30:06 +08:00
void process_font(int i, cbfn_t cb)
2015-11-07 16:27:18 +08:00
{
if ( (fi[i].font_mode & FM_C) != 0 )
2015-11-10 05:30:06 +08:00
build_font(i, FM_C, "u8g2", cb);
2015-11-07 16:27:18 +08:00
if ( (fi[i].font_mode & FM_8) != 0 )
2015-11-10 05:30:06 +08:00
build_font(i, FM_8, "u8x8", cb);
2015-11-07 16:27:18 +08:00
}
2015-11-10 05:30:06 +08:00
void do_font_loop(cbfn_t cb)
2015-11-07 16:27:18 +08:00
{
int i, cnt;
cnt = sizeof(fi)/sizeof(*fi);
for( i = 0; i < cnt; i++ )
{
2015-11-10 05:30:06 +08:00
process_font(i, cb);
2015-11-07 16:27:18 +08:00
}
}
int main(void)
{
2015-11-11 05:57:47 +08:00
unlink("u8x8_fonts.c");
unlink("u8g2_fonts.c");
2015-11-10 05:30:06 +08:00
do_font_loop(bdfconv);
u8g2_font_list_fp = fopen("u8g2_font_list.c", "w");
u8x8_font_list_fp = fopen("u8x8_font_list.c", "w");
fprintf(u8g2_font_list_fp, "/* u8g2_font_list.c */\n");
fprintf(u8x8_font_list_fp, "/* u8x8_font_list.c */\n");
fprintf(u8g2_font_list_fp, "#include \"u8g2.h\"\n");
fprintf(u8x8_font_list_fp, "#include \"u8g2.h\"\n");
fprintf(u8g2_font_list_fp, "uint8_t *u8g2_font_list[] = {\n");
fprintf(u8x8_font_list_fp, "uint8_t *u8x8_font_list[] = {\n");
do_font_loop(fontlist_identifier);
fprintf(u8g2_font_list_fp, " NULL\n};\n");
fprintf(u8x8_font_list_fp, " NULL\n};\n");
fprintf(u8g2_font_list_fp, "uint8_t *u8g2_font_names[] = {\n");
fprintf(u8x8_font_list_fp, "uint8_t *u8x8_font_names[] = {\n");
do_font_loop(fontlist_name);
fprintf(u8g2_font_list_fp, " NULL\n};\n");
fprintf(u8x8_font_list_fp, " NULL\n};\n");
fclose(u8g2_font_list_fp);
fclose(u8x8_font_list_fp);
2015-11-13 02:48:50 +08:00
2015-11-13 06:03:08 +08:00
insert_into_file("../../../csrc/u8g2.h", u8g2_prototypes, "/* start font list */", "/* end font list */");
insert_into_file("../../../csrc/u8x8.h", u8x8_prototypes, "/* start font list */", "/* end font list */");
2015-11-13 02:48:50 +08:00
2015-11-07 16:27:18 +08:00
return 0;
}