Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
Download
7638 views
1
/* cmapclean.c -- parse a CMap file and write it back out */
2
3
#include <stdio.h>
4
#include <string.h>
5
6
/* We never want to build memento versions of the cmapdump util */
7
#undef MEMENTO
8
9
#include "mupdf/pdf.h"
10
11
#include "../source/fitz/context.c"
12
#include "../source/fitz/error.c"
13
#include "../source/fitz/memory.c"
14
#include "../source/fitz/string.c"
15
#include "../source/fitz/buffer.c"
16
#include "../source/fitz/stream-open.c"
17
#include "../source/fitz/stream-read.c"
18
#include "../source/fitz/printf.c"
19
20
#include "../source/pdf/pdf-lex.c"
21
#include "../source/pdf/pdf-cmap.c"
22
#include "../source/pdf/pdf-cmap-parse.c"
23
24
struct cidrange {
25
unsigned int lo, hi, v;
26
};
27
28
static int cmpcidrange(const void *va, const void *vb)
29
{
30
unsigned int a = ((const struct cidrange *)va)->lo;
31
unsigned int b = ((const struct cidrange *)vb)->lo;
32
return a < b ? -1 : a > b ? 1 : 0;
33
}
34
35
static void pc(unsigned int c)
36
{
37
if (c <= 0xff) printf("<%02x>", c);
38
else if (c <= 0xffff) printf("<%04x>", c);
39
else if (c <= 0xffffff) printf("<%06x>", c);
40
else printf("<%010x>", c);
41
}
42
43
int
44
main(int argc, char **argv)
45
{
46
fz_context *ctx;
47
fz_stream *fi;
48
pdf_cmap *cmap;
49
int k, m, n, i;
50
struct cidrange *r;
51
52
if (argc != 2)
53
{
54
fprintf(stderr, "usage: cmapclean input.cmap\n");
55
return 1;
56
}
57
58
ctx = fz_new_context(NULL, NULL, FZ_STORE_UNLIMITED);
59
if (!ctx)
60
{
61
fprintf(stderr, "cannot initialise context\n");
62
return 1;
63
}
64
65
fi = fz_open_file(ctx, argv[1]);
66
cmap = pdf_load_cmap(ctx, fi);
67
fz_close(fi);
68
69
printf("begincmap\n");
70
printf("/CMapName /%s def\n", cmap->cmap_name);
71
printf("/WMode %d def\n", cmap->wmode);
72
if (cmap->usecmap_name[0])
73
printf("/%s usecmap\n", cmap->usecmap_name);
74
75
if (cmap->codespace_len)
76
{
77
printf("begincodespacerange\n");
78
for (k = 0; k < cmap->codespace_len; k++)
79
{
80
if (cmap->codespace[k].n == 1)
81
printf("<%02x> <%02x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
82
else if (cmap->codespace[k].n == 2)
83
printf("<%04x> <%04x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
84
else if (cmap->codespace[k].n == 3)
85
printf("<%06x> <%06x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
86
else if (cmap->codespace[k].n == 4)
87
printf("<%08x> <%08x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
88
else
89
printf("<%x> <%x>\n", cmap->codespace[k].low, cmap->codespace[k].high);
90
}
91
printf("endcodespacerange\n");
92
}
93
94
n = cmap->rlen + cmap->xlen;
95
r = malloc(n * sizeof *r);
96
i = 0;
97
98
for (k = 0; k < cmap->rlen; k++) {
99
r[i].lo = cmap->ranges[k].low;
100
r[i].hi = cmap->ranges[k].high;
101
r[i].v = cmap->ranges[k].out;
102
++i;
103
}
104
105
for (k = 0; k < cmap->xlen; k++) {
106
r[i].lo = cmap->xranges[k].low;
107
r[i].hi = cmap->xranges[k].high;
108
r[i].v = cmap->xranges[k].out;
109
++i;
110
}
111
112
qsort(r, n, sizeof *r, cmpcidrange);
113
114
if (n)
115
{
116
printf("begincidchar\n");
117
for (i = 0; i < n; ++i)
118
{
119
for (k = r[i].lo, m = r[i].v; k <= r[i].hi; ++k, ++m)
120
{
121
pc(k);
122
printf("%u\n", m);
123
}
124
}
125
printf("endcidchar\n");
126
}
127
128
if (cmap->mlen > 0)
129
{
130
printf("beginbfchar\n");
131
for (k = 0; k < cmap->mlen; k++)
132
{
133
pc(cmap->mranges[k].low);
134
135
printf("<");
136
for (m = 0; m < cmap->mranges[k].len; ++m)
137
printf("%04x", cmap->mranges[k].out[m]);
138
printf(">\n");
139
}
140
printf("endbfchar\n");
141
}
142
143
printf("endcmap\n");
144
145
fz_free_context(ctx);
146
return 0;
147
}
148
149
void fz_new_font_context(fz_context *ctx)
150
{
151
}
152
153
void fz_drop_font_context(fz_context *ctx)
154
{
155
}
156
157
fz_font_context *fz_keep_font_context(fz_context *ctx)
158
{
159
return NULL;
160
}
161
162
void fz_new_colorspace_context(fz_context *ctx)
163
{
164
}
165
166
void fz_drop_colorspace_context(fz_context *ctx)
167
{
168
}
169
170
fz_colorspace_context *fz_keep_colorspace_context(fz_context *ctx)
171
{
172
return NULL;
173
}
174
175
void fz_new_aa_context(fz_context *ctx)
176
{
177
}
178
179
void fz_free_aa_context(fz_context *ctx)
180
{
181
}
182
183
void fz_copy_aa_context(fz_context *dst, fz_context *src)
184
{
185
}
186
187
void *fz_keep_storable(fz_context *ctx, fz_storable *s)
188
{
189
return s;
190
}
191
192
void fz_drop_storable(fz_context *ctx, fz_storable *s)
193
{
194
}
195
196
void fz_new_store_context(fz_context *ctx, unsigned int max)
197
{
198
}
199
200
void fz_drop_store_context(fz_context *ctx)
201
{
202
}
203
204
fz_store *fz_keep_store_context(fz_context *ctx)
205
{
206
return NULL;
207
}
208
209
int fz_store_scavenge(fz_context *ctx, unsigned int size, int *phase)
210
{
211
return 0;
212
}
213
214
void fz_new_glyph_cache_context(fz_context *ctx)
215
{
216
}
217
218
void fz_drop_glyph_cache_context(fz_context *ctx)
219
{
220
}
221
222
fz_glyph_cache *fz_keep_glyph_cache(fz_context *ctx)
223
{
224
return NULL;
225
}
226
227
void fz_new_document_handler_context(fz_context *ctx)
228
{
229
}
230
231
void fz_drop_document_handler_context(fz_context *ctx)
232
{
233
}
234
235
fz_document_handler_context *fz_keep_document_handler_context(fz_context *ctx)
236
{
237
return NULL;
238
}
239
240