Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7638 views
1
/* fontdump.c -- an "xxd -i" workalike for dumping binary fonts as source code */
2
3
#include <stdio.h>
4
#include <stdlib.h>
5
#include <string.h>
6
7
static int
8
hexdump(FILE *fo, FILE *fi)
9
{
10
int c, n;
11
12
n = 0;
13
c = fgetc(fi);
14
while (c != -1)
15
{
16
n += fprintf(fo, "%d,", c);
17
if (n > 72) {
18
fprintf(fo, "\n");
19
n = 0;
20
}
21
c = fgetc(fi);
22
}
23
24
return n;
25
}
26
27
int
28
main(int argc, char **argv)
29
{
30
FILE *fo;
31
FILE *fi;
32
char fontname[256];
33
char origname[256];
34
char *basename;
35
char *p;
36
int i, len;
37
38
if (argc < 3)
39
{
40
fprintf(stderr, "usage: fontdump output.c input.dat\n");
41
return 1;
42
}
43
44
fo = fopen(argv[1], "wb");
45
if (!fo)
46
{
47
fprintf(stderr, "fontdump: could not open output file '%s'\n", argv[1]);
48
return 1;
49
}
50
51
fprintf(fo, "#ifndef __STRICT_ANSI__\n");
52
fprintf(fo, "#if defined(__linux__) || defined(__FreeBSD__) || defined(__OpenBSD__)\n");
53
fprintf(fo, "#if !defined(__clang__) && !defined(__ICC)\n");
54
fprintf(fo, "#define HAVE_INCBIN\n");
55
fprintf(fo, "#endif\n");
56
fprintf(fo, "#endif\n");
57
fprintf(fo, "#endif\n");
58
59
for (i = 2; i < argc; i++)
60
{
61
fi = fopen(argv[i], "rb");
62
if (!fi)
63
{
64
fclose(fo);
65
fprintf(stderr, "fontdump: could not open input file '%s'\n", argv[i]);
66
return 1;
67
}
68
69
basename = strrchr(argv[i], '/');
70
if (!basename)
71
basename = strrchr(argv[i], '\\');
72
if (basename)
73
basename++;
74
else
75
basename = argv[i];
76
77
strcpy(origname, basename);
78
p = strrchr(origname, '.');
79
if (p) *p = 0;
80
strcpy(fontname, origname);
81
82
p = fontname;
83
while (*p)
84
{
85
if (*p == '/' || *p == '.' || *p == '\\' || *p == '-')
86
*p = '_';
87
p ++;
88
}
89
90
fseek(fi, 0, SEEK_END);
91
len = ftell(fi);
92
fseek(fi, 0, SEEK_SET);
93
94
if (getenv("verbose"))
95
printf("\t{\"%s\",pdf_font_%s,%d},\n", origname, fontname, len);
96
97
fprintf(fo, "\n#ifdef HAVE_INCBIN\n");
98
fprintf(fo, "extern const unsigned char pdf_font_%s[%d];\n", fontname, len);
99
fprintf(fo, "asm(\".globl pdf_font_%s\");\n", fontname);
100
fprintf(fo, "asm(\".balign 8\");\n");
101
fprintf(fo, "asm(\"pdf_font_%s:\");\n", fontname);
102
fprintf(fo, "asm(\".incbin \\\"%s\\\"\");\n", argv[i]);
103
fprintf(fo, "#else\n");
104
fprintf(fo, "static const unsigned char pdf_font_%s[%d] = {\n", fontname, len);
105
hexdump(fo, fi);
106
fprintf(fo, "};\n");
107
fprintf(fo, "#endif\n");
108
109
fclose(fi);
110
}
111
112
if (fclose(fo))
113
{
114
fprintf(stderr, "fontdump: could not close output file '%s'\n", argv[1]);
115
return 1;
116
}
117
118
return 0;
119
}
120
121