1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
|
/* cmapdump.c -- parse a CMap file and dump it as a c-struct */
#include "mupdf/fitz.h"
#include "mupdf/pdf.h"
#include <stdio.h>
#include <string.h>
static void
clean(char *p)
{
while (*p)
{
if ((*p == '/') || (*p == '.') || (*p == '\\') || (*p == '-'))
*p = '_';
p ++;
}
}
int
cmapdump_main(int argc, char **argv)
{
pdf_cmap *cmap;
fz_stream *fi;
char name[256];
int i, k;
fz_context *ctx;
if (argc < 2)
{
fprintf(stderr, "usage: cmapdump > out.c [lots of cmap files]\n");
return 1;
}
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
if (!ctx)
{
fprintf(stderr, "cannot initialise context\n");
return 1;
}
printf("/* This is an automatically generated file. Do not edit. */\n");
for (i = 1; i < argc; i++)
{
fi = fz_open_file(ctx, argv[i]);
cmap = pdf_load_cmap(ctx, fi);
fz_drop_stream(ctx, fi);
strcpy(name, cmap->cmap_name);
clean(name);
printf("\n/* %s */\n\n", cmap->cmap_name);
if (cmap->rlen)
{
printf("static const pdf_range cmap_%s_ranges[] = {", name);
for (k = 0; k < cmap->rlen; k++)
{
printf("\n{%u,%u,%u},", cmap->ranges[k].low, cmap->ranges[k].high, cmap->ranges[k].out);
}
printf("\n};\n\n");
}
if (cmap->xlen)
{
printf("static const pdf_xrange cmap_%s_xranges[] = {", name);
for (k = 0; k < cmap->xlen; k++)
{
printf("\n{%u,%u,%u},", cmap->xranges[k].low, cmap->xranges[k].high, cmap->xranges[k].out);
}
printf("\n};\n\n");
}
if (cmap->mlen > 0)
{
printf("static const pdf_mrange cmap_%s_mranges[] = {", name);
for (k = 0; k < cmap->mlen; k++)
{
printf("\n{%u,%u},", cmap->mranges[k].low, cmap->mranges[k].out);
}
printf("\n};\n\n");
}
if (cmap->dlen > 0)
{
int ti = 0, tn = cmap->dict[0];
printf("static const int cmap_%s_table[] = {\n", name);
for (k = 0; k < cmap->dlen; k++)
{
if (ti > tn)
{
printf("\n");
ti = 1;
tn = cmap->dict[k];
}
else
++ti;
printf("%u,", cmap->dict[k]);
}
printf("\n};\n\n");
}
printf("static pdf_cmap cmap_%s = {\n", name);
printf("\t{ -1, pdf_drop_cmap_imp },\n");
printf("\t/* cmapname */ \"%s\",\n", cmap->cmap_name);
printf("\t/* usecmap */ \"%s\", NULL,\n", cmap->usecmap_name);
printf("\t/* wmode */ %u,\n", cmap->wmode);
printf("\t/* codespaces */ %u, {\n", cmap->codespace_len);
if (cmap->codespace_len == 0)
{
printf("\t\t{ 0, 0, 0 },\n");
}
for (k = 0; k < cmap->codespace_len; k++)
{
int n = cmap->codespace[k].n;
printf("\t\t{ %u, 0x%0*x, 0x%0*x },\n", n,
n*2, cmap->codespace[k].low,
n*2, cmap->codespace[k].high);
}
printf("\t},\n");
if (cmap->rlen)
printf("\t%u, %u, (pdf_range*)cmap_%s_ranges,\n", cmap->rlen, cmap->rlen, name);
else
printf("\t0, 0, NULL, /* ranges */\n");
if (cmap->xlen)
printf("\t%u, %u, (pdf_xrange*)cmap_%s_xranges,\n", cmap->xlen, cmap->xlen, name);
else
printf("\t0, 0, NULL, /* xranges */\n");
if (cmap->mlen)
printf("\t%u, %u, (pdf_mrange*)cmap_%s_mranges,\n", cmap->mlen, cmap->mlen, name);
else
printf("\t0, 0, NULL, /* mranges */\n");
if (cmap->dict)
printf("\t%u, %u, (int*)cmap_%s_table,\n", cmap->dlen, cmap->dlen, name);
else
printf("\t0, 0, NULL, /* table */\n");
printf("\t0, 0, 0, NULL /* splay tree */\n");
printf("};\n");
pdf_drop_cmap(ctx, cmap);
}
fz_drop_context(ctx);
return 0;
}
|