The name of the C font structure can be given on the command line.

Fixed small bugs: missing '=' in structure initializer, missing const.
This commit is contained in:
Johns 2009-02-18 21:48:27 +01:00
parent 2f475e8fde
commit 2fac095727
2 changed files with 39 additions and 29 deletions

View File

@ -10,6 +10,7 @@ bdf2c \- converts bdf font files into C include files
.I [-b file] .I [-b file]
.I [-c] .I [-c]
.I [-C file] .I [-C file]
.I [-n name]
.SH DESCRIPTION .SH DESCRIPTION
@ -30,6 +31,10 @@ Creates C header file 'file'.
.TP .TP
.B -b file .B -b file
Read and convert bdf font 'file'. Read and convert bdf font 'file'.
.TP
.B -n name
Name of the C font structure. 'name' should contain only valid identifier characters. f.e. font9x15b
.SH AUTHOR .SH AUTHOR
Johns (2009) <johns98@gmx.net>. Johns (2009) <johns98@gmx.net>.

49
bdf2c.c
View File

@ -48,14 +48,14 @@ void CreateFontHeaderFile(FILE * out)
register int i; register int i;
fprintf(out, "// (c) 2009 Johns, License: AGPLv3\n\n"); fprintf(out, "// (c) 2009 Johns, License: AGPLv3\n\n");
fprintf(out, "\t/// bitmap font structure\n" fprintf(out,
"struct bitmap_font {\n" "\t/// bitmap font structure\n" "struct bitmap_font {\n"
"\tunsigned char Width;\t\t///< max. character width\n" "\tunsigned char Width;\t\t///< max. character width\n"
"\tunsigned char Height;\t\t///< character height\n" "\tunsigned char Height;\t\t///< character height\n"
"\tunsigned short Chars;\t\t///< number of characters in font\n" "\tunsigned short Chars;\t\t///< number of characters in font\n"
"\tunsigned char *Widths;\t\t///< width of each character\n" "\tconst unsigned char *Widths;\t///< width of each character\n"
"\tunsigned short *Index;\t\t///< encoding to character index\n" "\tconst unsigned short *Index;\t///< encoding to character index\n"
"\tunsigned char *Bitmap;\t\t///< bitmap of each character\n" "\tconst unsigned char *Bitmap;\t///< bitmap of each character\n"
"};\n\n"); "};\n\n");
fprintf(out, "\t/// @{ defines to have human readable font files\n"); fprintf(out, "\t/// @{ defines to have human readable font files\n");
@ -124,20 +124,21 @@ void EncodingTable(FILE * out, const char *name,
void Footer(FILE * out, const char *name, int width, int height, int chars) void Footer(FILE * out, const char *name, int width, int height, int chars)
{ {
fprintf(out, "};\n\n"); fprintf(out, "};\n\n");
fprintf(out, "\t/// bitmap font structure\n" fprintf(out,
"struct bitmap_font %s = {\n", name); "\t/// bitmap font structure\n" "const struct bitmap_font %s = {\n",
fprintf(out, "\t.Width %d, .Height %d,\n", width, height); name);
fprintf(out, "\t.Chars %d,\n", chars); fprintf(out, "\t.Width = %d, .Height = %d,\n", width, height);
fprintf(out, "\t.Widths __%s_widths__,\n", name); fprintf(out, "\t.Chars = %d,\n", chars);
fprintf(out, "\t.Index __%s_index__,\n", name); fprintf(out, "\t.Widths = __%s_widths__,\n", name);
fprintf(out, "\t.Bitmap __%s_bitmap__,\n", name); fprintf(out, "\t.Index = __%s_index__,\n", name);
fprintf(out, "\t.Bitmap = __%s_bitmap__,\n", name);
fprintf(out, "};\n\n"); fprintf(out, "};\n\n");
} }
// //
// Read BDF font file. // Read BDF font file.
// //
void ReadBdf(FILE * bdf) void ReadBdf(FILE * bdf, const char *name)
{ {
char linebuf[1024]; char linebuf[1024];
char *s; char *s;
@ -157,9 +158,7 @@ void ReadBdf(FILE * bdf)
int width; int width;
unsigned *width_table; unsigned *width_table;
unsigned *encoding_table; unsigned *encoding_table;
const char *name;
name = "font";
fontboundingbox_width = 0; fontboundingbox_width = 0;
fontboundingbox_height = 0; fontboundingbox_height = 0;
chars = 0; chars = 0;
@ -309,10 +308,10 @@ void PrintVersion(void)
// //
void PrintUsage(void) void PrintUsage(void)
{ {
printf("Usage: bdf2c [OPTIONs]\n" printf("Usage: bdf2c [OPTIONs]\n" "\t-b\tRead bdf file from stdin\n"
"\t-b\tRead bdf file from stdin\n"
"\t-c\tCreate font header on stdout\n" "\t-c\tCreate font header on stdout\n"
"\t-C file\tCreate font header file\n"); "\t-C file\tCreate font header file\n"
"\t-n name\tName of c font variable (place it before -b)\n");
printf("\tOnly idiots print usage on stderr\n"); printf("\tOnly idiots print usage on stderr\n");
} }
@ -321,14 +320,17 @@ void PrintUsage(void)
// //
int main(int argc, char *const argv[]) int main(int argc, char *const argv[])
{ {
const char *name;
name = "font";
// //
// Parse arguments. // Parse arguments.
// //
for (;;) { for (;;) {
switch (getopt(argc, argv, "bcC:h?-")) { switch (getopt(argc, argv, "bcC:n:h?-")) {
case 'b': case 'b':
ReadBdf(stdin); ReadBdf(stdin, name);
break; continue;
case 'c': case 'c':
CreateFontHeaderFile(stdout); CreateFontHeaderFile(stdout);
break; break;
@ -344,7 +346,10 @@ int main(int argc, char *const argv[])
CreateFontHeaderFile(out); CreateFontHeaderFile(out);
fclose(out); fclose(out);
} }
break; continue;
case 'n':
name = optarg;
continue;
case EOF: case EOF:
break; break;